1 #define PETSC_DESIRE_FEATURE_TEST_MACROS 2 /* 3 This file defines the initialization of PETSc, including PetscInitialize() 4 */ 5 #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/ 6 #include <petscviewer.h> 7 8 #if !defined(PETSC_HAVE_WINDOWS_COMPILERS) 9 #include <petsc/private/valgrind/valgrind.h> 10 #endif 11 12 #if defined(PETSC_HAVE_FORTRAN) 13 #include <petsc/private/fortranimpl.h> 14 #endif 15 16 #if defined(PETSC_HAVE_CUDA) 17 #include <petsc/private/deviceimpl.h> 18 PETSC_EXTERN cudaEvent_t petsc_gputimer_begin; 19 PETSC_EXTERN cudaEvent_t petsc_gputimer_end; 20 #endif 21 22 #if defined(PETSC_HAVE_HIP) 23 #include <petsc/private/deviceimpl.h> 24 PETSC_EXTERN hipEvent_t petsc_gputimer_begin; 25 PETSC_EXTERN hipEvent_t petsc_gputimer_end; 26 #endif 27 28 #if defined(PETSC_USE_GCOV) 29 EXTERN_C_BEGIN 30 void __gcov_flush(void); 31 EXTERN_C_END 32 #endif 33 34 #if defined(PETSC_USE_LOG) 35 PETSC_INTERN PetscErrorCode PetscLogFinalize(void); 36 #endif 37 38 #if defined(PETSC_SERIALIZE_FUNCTIONS) 39 PETSC_INTERN PetscFPT PetscFPTData; 40 PetscFPT PetscFPTData = 0; 41 #endif 42 43 #if defined(PETSC_HAVE_SAWS) 44 #include <petscviewersaws.h> 45 #endif 46 47 /* -----------------------------------------------------------------------------------------*/ 48 49 PETSC_INTERN FILE *petsc_history; 50 51 PETSC_INTERN PetscErrorCode PetscInitialize_DynamicLibraries(void); 52 PETSC_INTERN PetscErrorCode PetscFinalize_DynamicLibraries(void); 53 PETSC_INTERN PetscErrorCode PetscFunctionListPrintAll(void); 54 PETSC_INTERN PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int); 55 PETSC_INTERN PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int); 56 PETSC_INTERN PetscErrorCode PetscCloseHistoryFile(FILE**); 57 58 /* user may set these BEFORE calling PetscInitialize() */ 59 MPI_Comm PETSC_COMM_WORLD = MPI_COMM_NULL; 60 #if defined(PETSC_HAVE_MPI_INIT_THREAD) 61 PetscMPIInt PETSC_MPI_THREAD_REQUIRED = MPI_THREAD_FUNNELED; 62 #else 63 PetscMPIInt PETSC_MPI_THREAD_REQUIRED = 0; 64 #endif 65 66 PetscMPIInt Petsc_Counter_keyval = MPI_KEYVAL_INVALID; 67 PetscMPIInt Petsc_InnerComm_keyval = MPI_KEYVAL_INVALID; 68 PetscMPIInt Petsc_OuterComm_keyval = MPI_KEYVAL_INVALID; 69 PetscMPIInt Petsc_ShmComm_keyval = MPI_KEYVAL_INVALID; 70 71 /* 72 Declare and set all the string names of the PETSc enums 73 */ 74 const char *const PetscBools[] = {"FALSE","TRUE","PetscBool","PETSC_",NULL}; 75 const char *const PetscCopyModes[] = {"COPY_VALUES","OWN_POINTER","USE_POINTER","PetscCopyMode","PETSC_",NULL}; 76 77 PetscBool PetscPreLoadingUsed = PETSC_FALSE; 78 PetscBool PetscPreLoadingOn = PETSC_FALSE; 79 80 PetscInt PetscHotRegionDepth; 81 82 PetscBool PETSC_RUNNING_ON_VALGRIND = PETSC_FALSE; 83 84 #if defined(PETSC_HAVE_THREADSAFETY) 85 PetscSpinlock PetscViewerASCIISpinLockOpen; 86 PetscSpinlock PetscViewerASCIISpinLockStdout; 87 PetscSpinlock PetscViewerASCIISpinLockStderr; 88 PetscSpinlock PetscCommSpinLock; 89 #endif 90 91 /* 92 PetscInitializeNoPointers - Calls PetscInitialize() from C/C++ without the pointers to argc and args 93 94 Collective 95 96 Level: advanced 97 98 Notes: 99 this is called only by the PETSc Julia interface. Even though it might start MPI it sets the flag to 100 indicate that it did NOT start MPI so that the PetscFinalize() does not end MPI, thus allowing PetscInitialize() to 101 be called multiple times from Julia without the problem of trying to initialize MPI more than once. 102 103 Developer Note: Turns off PETSc signal handling to allow Julia to manage signals 104 105 .seealso: PetscInitialize(), PetscInitializeFortran(), PetscInitializeNoArguments() 106 */ 107 PetscErrorCode PetscInitializeNoPointers(int argc,char **args,const char *filename,const char *help) 108 { 109 PetscErrorCode ierr; 110 int myargc = argc; 111 char **myargs = args; 112 113 PetscFunctionBegin; 114 ierr = PetscInitialize(&myargc,&myargs,filename,help);if (ierr) return ierr; 115 ierr = PetscPopSignalHandler();CHKERRQ(ierr); 116 PetscBeganMPI = PETSC_FALSE; 117 PetscFunctionReturn(ierr); 118 } 119 120 /* 121 Used by Julia interface to get communicator 122 */ 123 PetscErrorCode PetscGetPETSC_COMM_SELF(MPI_Comm *comm) 124 { 125 PetscFunctionBegin; 126 *comm = PETSC_COMM_SELF; 127 PetscFunctionReturn(0); 128 } 129 130 /*@C 131 PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without 132 the command line arguments. 133 134 Collective 135 136 Level: advanced 137 138 .seealso: PetscInitialize(), PetscInitializeFortran() 139 @*/ 140 PetscErrorCode PetscInitializeNoArguments(void) 141 { 142 PetscErrorCode ierr; 143 int argc = 0; 144 char **args = NULL; 145 146 PetscFunctionBegin; 147 ierr = PetscInitialize(&argc,&args,NULL,NULL); 148 PetscFunctionReturn(ierr); 149 } 150 151 /*@ 152 PetscInitialized - Determine whether PETSc is initialized. 153 154 Level: beginner 155 156 .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran() 157 @*/ 158 PetscErrorCode PetscInitialized(PetscBool *isInitialized) 159 { 160 *isInitialized = PetscInitializeCalled; 161 return 0; 162 } 163 164 /*@ 165 PetscFinalized - Determine whether PetscFinalize() has been called yet 166 167 Level: developer 168 169 .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran() 170 @*/ 171 PetscErrorCode PetscFinalized(PetscBool *isFinalized) 172 { 173 *isFinalized = PetscFinalizeCalled; 174 return 0; 175 } 176 177 PETSC_INTERN PetscErrorCode PetscOptionsCheckInitial_Private(const char []); 178 179 /* 180 This function is the MPI reduction operation used to compute the sum of the 181 first half of the datatype and the max of the second half. 182 */ 183 MPI_Op MPIU_MAXSUM_OP = 0; 184 185 PETSC_INTERN void MPIAPI MPIU_MaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype) 186 { 187 PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt; 188 189 PetscFunctionBegin; 190 if (*datatype != MPIU_2INT) { 191 (*PetscErrorPrintf)("Can only handle MPIU_2INT data types"); 192 PETSCABORT(MPI_COMM_SELF,PETSC_ERR_ARG_WRONG); 193 } 194 195 for (i=0; i<count; i++) { 196 xout[2*i] = PetscMax(xout[2*i],xin[2*i]); 197 xout[2*i+1] += xin[2*i+1]; 198 } 199 PetscFunctionReturnVoid(); 200 } 201 202 /* 203 Returns the max of the first entry owned by this processor and the 204 sum of the second entry. 205 206 The reason sizes[2*i] contains lengths sizes[2*i+1] contains flag of 1 if length is nonzero 207 is so that the MPIU_MAXSUM_OP() can set TWO values, if we passed in only sizes[i] with lengths 208 there would be no place to store the both needed results. 209 */ 210 PetscErrorCode PetscMaxSum(MPI_Comm comm,const PetscInt sizes[],PetscInt *max,PetscInt *sum) 211 { 212 PetscErrorCode ierr; 213 214 PetscFunctionBegin; 215 #if defined(PETSC_HAVE_MPI_REDUCE_SCATTER_BLOCK) 216 { 217 struct {PetscInt max,sum;} work; 218 ierr = MPI_Reduce_scatter_block((void*)sizes,&work,1,MPIU_2INT,MPIU_MAXSUM_OP,comm);CHKERRMPI(ierr); 219 *max = work.max; 220 *sum = work.sum; 221 } 222 #else 223 { 224 PetscMPIInt size,rank; 225 struct {PetscInt max,sum;} *work; 226 ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr); 227 ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 228 ierr = PetscMalloc1(size,&work);CHKERRQ(ierr); 229 ierr = MPIU_Allreduce((void*)sizes,work,size,MPIU_2INT,MPIU_MAXSUM_OP,comm);CHKERRMPI(ierr); 230 *max = work[rank].max; 231 *sum = work[rank].sum; 232 ierr = PetscFree(work);CHKERRQ(ierr); 233 } 234 #endif 235 PetscFunctionReturn(0); 236 } 237 238 /* ----------------------------------------------------------------------------*/ 239 240 #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16) 241 MPI_Op MPIU_SUM = 0; 242 243 PETSC_EXTERN void MPIAPI PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 244 { 245 PetscInt i,count = *cnt; 246 247 PetscFunctionBegin; 248 if (*datatype == MPIU_REAL) { 249 PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out; 250 for (i=0; i<count; i++) xout[i] += xin[i]; 251 } 252 #if defined(PETSC_HAVE_COMPLEX) 253 else if (*datatype == MPIU_COMPLEX) { 254 PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out; 255 for (i=0; i<count; i++) xout[i] += xin[i]; 256 } 257 #endif 258 else { 259 (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types"); 260 PETSCABORT(MPI_COMM_SELF,PETSC_ERR_ARG_WRONG); 261 } 262 PetscFunctionReturnVoid(); 263 } 264 #endif 265 266 #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16) 267 MPI_Op MPIU_MAX = 0; 268 MPI_Op MPIU_MIN = 0; 269 270 PETSC_EXTERN void MPIAPI PetscMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 271 { 272 PetscInt i,count = *cnt; 273 274 PetscFunctionBegin; 275 if (*datatype == MPIU_REAL) { 276 PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out; 277 for (i=0; i<count; i++) xout[i] = PetscMax(xout[i],xin[i]); 278 } 279 #if defined(PETSC_HAVE_COMPLEX) 280 else if (*datatype == MPIU_COMPLEX) { 281 PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out; 282 for (i=0; i<count; i++) { 283 xout[i] = PetscRealPartComplex(xout[i])<PetscRealPartComplex(xin[i]) ? xin[i] : xout[i]; 284 } 285 } 286 #endif 287 else { 288 (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types"); 289 PETSCABORT(MPI_COMM_SELF,PETSC_ERR_ARG_WRONG); 290 } 291 PetscFunctionReturnVoid(); 292 } 293 294 PETSC_EXTERN void MPIAPI PetscMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 295 { 296 PetscInt i,count = *cnt; 297 298 PetscFunctionBegin; 299 if (*datatype == MPIU_REAL) { 300 PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out; 301 for (i=0; i<count; i++) xout[i] = PetscMin(xout[i],xin[i]); 302 } 303 #if defined(PETSC_HAVE_COMPLEX) 304 else if (*datatype == MPIU_COMPLEX) { 305 PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out; 306 for (i=0; i<count; i++) { 307 xout[i] = PetscRealPartComplex(xout[i])>PetscRealPartComplex(xin[i]) ? xin[i] : xout[i]; 308 } 309 } 310 #endif 311 else { 312 (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_SCALAR data (i.e. double or complex) types"); 313 PETSCABORT(MPI_COMM_SELF,PETSC_ERR_ARG_WRONG); 314 } 315 PetscFunctionReturnVoid(); 316 } 317 #endif 318 319 /* 320 Private routine to delete internal tag/name counter storage when a communicator is freed. 321 322 This is called by MPI, not by users. This is called by MPI_Comm_free() when the communicator that has this data as an attribute is freed. 323 324 Note: this is declared extern "C" because it is passed to MPI_Comm_create_keyval() 325 326 */ 327 PETSC_EXTERN PetscMPIInt MPIAPI Petsc_Counter_Attr_Delete_Fn(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state) 328 { 329 PetscErrorCode ierr; 330 PetscCommCounter *counter=(PetscCommCounter*)count_val; 331 332 PetscFunctionBegin; 333 ierr = PetscInfo1(NULL,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);CHKERRMPI(ierr); 334 ierr = PetscFree(counter->iflags);CHKERRMPI(ierr); 335 ierr = PetscFree(counter);CHKERRMPI(ierr); 336 PetscFunctionReturn(MPI_SUCCESS); 337 } 338 339 /* 340 This is invoked on the outer comm as a result of either PetscCommDestroy() (via MPI_Comm_delete_attr) or when the user 341 calls MPI_Comm_free(). 342 343 This is the only entry point for breaking the links between inner and outer comms. 344 345 This is called by MPI, not by users. This is called when MPI_Comm_free() is called on the communicator. 346 347 Note: this is declared extern "C" because it is passed to MPI_Comm_create_keyval() 348 349 */ 350 PETSC_EXTERN PetscMPIInt MPIAPI Petsc_InnerComm_Attr_Delete_Fn(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state) 351 { 352 PetscErrorCode ierr; 353 union {MPI_Comm comm; void *ptr;} icomm; 354 355 PetscFunctionBegin; 356 if (keyval != Petsc_InnerComm_keyval) SETERRMPI(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Unexpected keyval"); 357 icomm.ptr = attr_val; 358 if (PetscDefined(USE_DEBUG)) { 359 /* Error out if the inner/outer comms are not correctly linked through their Outer/InnterComm attributes */ 360 PetscMPIInt flg; 361 union {MPI_Comm comm; void *ptr;} ocomm; 362 ierr = MPI_Comm_get_attr(icomm.comm,Petsc_OuterComm_keyval,&ocomm,&flg);CHKERRMPI(ierr); 363 if (!flg) SETERRMPI(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner comm does not have OuterComm attribute"); 364 if (ocomm.comm != comm) SETERRMPI(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner comm's OuterComm attribute does not point to outer PETSc comm"); 365 } 366 ierr = MPI_Comm_delete_attr(icomm.comm,Petsc_OuterComm_keyval);CHKERRMPI(ierr); 367 ierr = PetscInfo2(NULL,"User MPI_Comm %ld is being unlinked from inner PETSc comm %ld\n",(long)comm,(long)icomm.comm);CHKERRMPI(ierr); 368 PetscFunctionReturn(MPI_SUCCESS); 369 } 370 371 /* 372 * This is invoked on the inner comm when Petsc_InnerComm_Attr_Delete_Fn calls MPI_Comm_delete_attr(). It should not be reached any other way. 373 */ 374 PETSC_EXTERN PetscMPIInt MPIAPI Petsc_OuterComm_Attr_Delete_Fn(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state) 375 { 376 PetscErrorCode ierr; 377 378 PetscFunctionBegin; 379 ierr = PetscInfo1(NULL,"Removing reference to PETSc communicator embedded in a user MPI_Comm %ld\n",(long)comm);CHKERRMPI(ierr); 380 PetscFunctionReturn(MPI_SUCCESS); 381 } 382 383 PETSC_EXTERN PetscMPIInt MPIAPI Petsc_ShmComm_Attr_Delete_Fn(MPI_Comm,PetscMPIInt,void *,void *); 384 385 #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32) 386 PETSC_EXTERN PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype,MPI_Aint*,void*); 387 PETSC_EXTERN PetscMPIInt PetscDataRep_read_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*); 388 PETSC_EXTERN PetscMPIInt PetscDataRep_write_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*); 389 #endif 390 391 PetscMPIInt PETSC_MPI_ERROR_CLASS=MPI_ERR_LASTCODE,PETSC_MPI_ERROR_CODE; 392 393 PETSC_INTERN int PetscGlobalArgc; 394 PETSC_INTERN char **PetscGlobalArgs; 395 int PetscGlobalArgc = 0; 396 char **PetscGlobalArgs = NULL; 397 PetscSegBuffer PetscCitationsList; 398 399 PetscErrorCode PetscCitationsInitialize(void) 400 { 401 PetscErrorCode ierr; 402 403 PetscFunctionBegin; 404 ierr = PetscSegBufferCreate(1,10000,&PetscCitationsList);CHKERRQ(ierr); 405 ierr = PetscCitationsRegister("@TechReport{petsc-user-ref,\n Author = {Satish Balay and Shrirang Abhyankar and Mark F. Adams and Jed Brown \n and Peter Brune and Kris Buschelman and Lisandro Dalcin and\n Victor Eijkhout and William D. Gropp and Dmitry Karpeyev and\n Dinesh Kaushik and Matthew G. Knepley and Dave A. May and Lois Curfman McInnes\n and Richard Tran Mills and Todd Munson and Karl Rupp and Patrick Sanan\n and Barry F. Smith and Stefano Zampini and Hong Zhang and Hong Zhang},\n Title = {{PETS}c Users Manual},\n Number = {ANL-95/11 - Revision 3.11},\n Institution = {Argonne National Laboratory},\n Year = {2019}\n}\n",NULL);CHKERRQ(ierr); 406 ierr = PetscCitationsRegister("@InProceedings{petsc-efficient,\n Author = {Satish Balay and William D. Gropp and Lois Curfman McInnes and Barry F. Smith},\n Title = {Efficient Management of Parallelism in Object Oriented Numerical Software Libraries},\n Booktitle = {Modern Software Tools in Scientific Computing},\n Editor = {E. Arge and A. M. Bruaset and H. P. Langtangen},\n Pages = {163--202},\n Publisher = {Birkh{\\\"{a}}user Press},\n Year = {1997}\n}\n",NULL);CHKERRQ(ierr); 407 PetscFunctionReturn(0); 408 } 409 410 static char programname[PETSC_MAX_PATH_LEN] = ""; /* HP includes entire path in name */ 411 412 PetscErrorCode PetscSetProgramName(const char name[]) 413 { 414 PetscErrorCode ierr; 415 416 PetscFunctionBegin; 417 ierr = PetscStrncpy(programname,name,sizeof(programname));CHKERRQ(ierr); 418 PetscFunctionReturn(0); 419 } 420 421 /*@C 422 PetscGetProgramName - Gets the name of the running program. 423 424 Not Collective 425 426 Input Parameter: 427 . len - length of the string name 428 429 Output Parameter: 430 . name - the name of the running program 431 432 Level: advanced 433 434 Notes: 435 The name of the program is copied into the user-provided character 436 array of length len. On some machines the program name includes 437 its entire path, so one should generally set len >= PETSC_MAX_PATH_LEN. 438 @*/ 439 PetscErrorCode PetscGetProgramName(char name[],size_t len) 440 { 441 PetscErrorCode ierr; 442 443 PetscFunctionBegin; 444 ierr = PetscStrncpy(name,programname,len);CHKERRQ(ierr); 445 PetscFunctionReturn(0); 446 } 447 448 /*@C 449 PetscGetArgs - Allows you to access the raw command line arguments anywhere 450 after PetscInitialize() is called but before PetscFinalize(). 451 452 Not Collective 453 454 Output Parameters: 455 + argc - count of number of command line arguments 456 - args - the command line arguments 457 458 Level: intermediate 459 460 Notes: 461 This is usually used to pass the command line arguments into other libraries 462 that are called internally deep in PETSc or the application. 463 464 The first argument contains the program name as is normal for C arguments. 465 466 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments() 467 468 @*/ 469 PetscErrorCode PetscGetArgs(int *argc,char ***args) 470 { 471 PetscFunctionBegin; 472 if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()"); 473 *argc = PetscGlobalArgc; 474 *args = PetscGlobalArgs; 475 PetscFunctionReturn(0); 476 } 477 478 /*@C 479 PetscGetArguments - Allows you to access the command line arguments anywhere 480 after PetscInitialize() is called but before PetscFinalize(). 481 482 Not Collective 483 484 Output Parameters: 485 . args - the command line arguments 486 487 Level: intermediate 488 489 Notes: 490 This does NOT start with the program name and IS null terminated (final arg is void) 491 492 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments() 493 494 @*/ 495 PetscErrorCode PetscGetArguments(char ***args) 496 { 497 PetscInt i,argc = PetscGlobalArgc; 498 PetscErrorCode ierr; 499 500 PetscFunctionBegin; 501 if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()"); 502 if (!argc) {*args = NULL; PetscFunctionReturn(0);} 503 ierr = PetscMalloc1(argc,args);CHKERRQ(ierr); 504 for (i=0; i<argc-1; i++) { 505 ierr = PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);CHKERRQ(ierr); 506 } 507 (*args)[argc-1] = NULL; 508 PetscFunctionReturn(0); 509 } 510 511 /*@C 512 PetscFreeArguments - Frees the memory obtained with PetscGetArguments() 513 514 Not Collective 515 516 Output Parameters: 517 . args - the command line arguments 518 519 Level: intermediate 520 521 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments() 522 523 @*/ 524 PetscErrorCode PetscFreeArguments(char **args) 525 { 526 PetscInt i = 0; 527 PetscErrorCode ierr; 528 529 PetscFunctionBegin; 530 if (!args) PetscFunctionReturn(0); 531 while (args[i]) { 532 ierr = PetscFree(args[i]);CHKERRQ(ierr); 533 i++; 534 } 535 ierr = PetscFree(args);CHKERRQ(ierr); 536 PetscFunctionReturn(0); 537 } 538 539 #if defined(PETSC_HAVE_SAWS) 540 #include <petscconfiginfo.h> 541 542 PETSC_INTERN PetscErrorCode PetscInitializeSAWs(const char help[]) 543 { 544 if (!PetscGlobalRank) { 545 char cert[PETSC_MAX_PATH_LEN],root[PETSC_MAX_PATH_LEN],*intro,programname[64],*appline,*options,version[64]; 546 int port; 547 PetscBool flg,rootlocal = PETSC_FALSE,flg2,selectport = PETSC_FALSE; 548 size_t applinelen,introlen; 549 PetscErrorCode ierr; 550 char sawsurl[256]; 551 552 ierr = PetscOptionsHasName(NULL,NULL,"-saws_log",&flg);CHKERRQ(ierr); 553 if (flg) { 554 char sawslog[PETSC_MAX_PATH_LEN]; 555 556 ierr = PetscOptionsGetString(NULL,NULL,"-saws_log",sawslog,sizeof(sawslog),NULL);CHKERRQ(ierr); 557 if (sawslog[0]) { 558 PetscStackCallSAWs(SAWs_Set_Use_Logfile,(sawslog)); 559 } else { 560 PetscStackCallSAWs(SAWs_Set_Use_Logfile,(NULL)); 561 } 562 } 563 ierr = PetscOptionsGetString(NULL,NULL,"-saws_https",cert,sizeof(cert),&flg);CHKERRQ(ierr); 564 if (flg) { 565 PetscStackCallSAWs(SAWs_Set_Use_HTTPS,(cert)); 566 } 567 ierr = PetscOptionsGetBool(NULL,NULL,"-saws_port_auto_select",&selectport,NULL);CHKERRQ(ierr); 568 if (selectport) { 569 PetscStackCallSAWs(SAWs_Get_Available_Port,(&port)); 570 PetscStackCallSAWs(SAWs_Set_Port,(port)); 571 } else { 572 ierr = PetscOptionsGetInt(NULL,NULL,"-saws_port",&port,&flg);CHKERRQ(ierr); 573 if (flg) { 574 PetscStackCallSAWs(SAWs_Set_Port,(port)); 575 } 576 } 577 ierr = PetscOptionsGetString(NULL,NULL,"-saws_root",root,sizeof(root),&flg);CHKERRQ(ierr); 578 if (flg) { 579 PetscStackCallSAWs(SAWs_Set_Document_Root,(root));CHKERRQ(ierr); 580 ierr = PetscStrcmp(root,".",&rootlocal);CHKERRQ(ierr); 581 } else { 582 ierr = PetscOptionsHasName(NULL,NULL,"-saws_options",&flg);CHKERRQ(ierr); 583 if (flg) { 584 ierr = PetscStrreplace(PETSC_COMM_WORLD,"${PETSC_DIR}/share/petsc/saws",root,sizeof(root));CHKERRQ(ierr); 585 PetscStackCallSAWs(SAWs_Set_Document_Root,(root));CHKERRQ(ierr); 586 } 587 } 588 ierr = PetscOptionsHasName(NULL,NULL,"-saws_local",&flg2);CHKERRQ(ierr); 589 if (flg2) { 590 char jsdir[PETSC_MAX_PATH_LEN]; 591 if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"-saws_local option requires -saws_root option"); 592 ierr = PetscSNPrintf(jsdir,sizeof(jsdir),"%s/js",root);CHKERRQ(ierr); 593 ierr = PetscTestDirectory(jsdir,'r',&flg);CHKERRQ(ierr); 594 if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"-saws_local option requires js directory in root directory"); 595 PetscStackCallSAWs(SAWs_Push_Local_Header,());CHKERRQ(ierr); 596 } 597 ierr = PetscGetProgramName(programname,sizeof(programname));CHKERRQ(ierr); 598 ierr = PetscStrlen(help,&applinelen);CHKERRQ(ierr); 599 introlen = 4096 + applinelen; 600 applinelen += 1024; 601 ierr = PetscMalloc(applinelen,&appline);CHKERRQ(ierr); 602 ierr = PetscMalloc(introlen,&intro);CHKERRQ(ierr); 603 604 if (rootlocal) { 605 ierr = PetscSNPrintf(appline,applinelen,"%s.c.html",programname);CHKERRQ(ierr); 606 ierr = PetscTestFile(appline,'r',&rootlocal);CHKERRQ(ierr); 607 } 608 ierr = PetscOptionsGetAll(NULL,&options);CHKERRQ(ierr); 609 if (rootlocal && help) { 610 ierr = PetscSNPrintf(appline,applinelen,"<center> Running <a href=\"%s.c.html\">%s</a> %s</center><br><center><pre>%s</pre></center><br>\n",programname,programname,options,help);CHKERRQ(ierr); 611 } else if (help) { 612 ierr = PetscSNPrintf(appline,applinelen,"<center>Running %s %s</center><br><center><pre>%s</pre></center><br>",programname,options,help);CHKERRQ(ierr); 613 } else { 614 ierr = PetscSNPrintf(appline,applinelen,"<center> Running %s %s</center><br>\n",programname,options);CHKERRQ(ierr); 615 } 616 ierr = PetscFree(options);CHKERRQ(ierr); 617 ierr = PetscGetVersion(version,sizeof(version));CHKERRQ(ierr); 618 ierr = PetscSNPrintf(intro,introlen,"<body>\n" 619 "<center><h2> <a href=\"https://petsc.org/\">PETSc</a> Application Web server powered by <a href=\"https://bitbucket.org/saws/saws\">SAWs</a> </h2></center>\n" 620 "<center>This is the default PETSc application dashboard, from it you can access any published PETSc objects or logging data</center><br><center>%s configured with %s</center><br>\n" 621 "%s",version,petscconfigureoptions,appline);CHKERRQ(ierr); 622 PetscStackCallSAWs(SAWs_Push_Body,("index.html",0,intro)); 623 ierr = PetscFree(intro);CHKERRQ(ierr); 624 ierr = PetscFree(appline);CHKERRQ(ierr); 625 if (selectport) { 626 PetscBool silent; 627 628 ierr = SAWs_Initialize(); 629 /* another process may have grabbed the port so keep trying */ 630 while (ierr) { 631 PetscStackCallSAWs(SAWs_Get_Available_Port,(&port)); 632 PetscStackCallSAWs(SAWs_Set_Port,(port)); 633 ierr = SAWs_Initialize(); 634 } 635 636 ierr = PetscOptionsGetBool(NULL,NULL,"-saws_port_auto_select_silent",&silent,NULL);CHKERRQ(ierr); 637 if (!silent) { 638 PetscStackCallSAWs(SAWs_Get_FullURL,(sizeof(sawsurl),sawsurl)); 639 ierr = PetscPrintf(PETSC_COMM_WORLD,"Point your browser to %s for SAWs\n",sawsurl);CHKERRQ(ierr); 640 } 641 } else { 642 PetscStackCallSAWs(SAWs_Initialize,()); 643 } 644 ierr = PetscCitationsRegister("@TechReport{ saws,\n" 645 " Author = {Matt Otten and Jed Brown and Barry Smith},\n" 646 " Title = {Scientific Application Web Server (SAWs) Users Manual},\n" 647 " Institution = {Argonne National Laboratory},\n" 648 " Year = 2013\n}\n",NULL);CHKERRQ(ierr); 649 } 650 PetscFunctionReturn(0); 651 } 652 #endif 653 654 /* Things must be done before MPI_Init() when MPI is not yet initialized, and can be shared between C init and Fortran init */ 655 PETSC_INTERN PetscErrorCode PetscPreMPIInit_Private(void) 656 { 657 PetscFunctionBegin; 658 #if defined(PETSC_HAVE_HWLOC_SOLARIS_BUG) 659 /* see MPI.py for details on this bug */ 660 (void) setenv("HWLOC_COMPONENTS","-x86",1); 661 #endif 662 PetscFunctionReturn(0); 663 } 664 665 #if defined(PETSC_HAVE_ADIOS) 666 #include <adios.h> 667 #include <adios_read.h> 668 int64_t Petsc_adios_group; 669 #endif 670 #if defined(PETSC_HAVE_OPENMP) 671 #include <omp.h> 672 PetscInt PetscNumOMPThreads; 673 #endif 674 675 #if defined(PETSC_HAVE_DLFCN_H) 676 #include <dlfcn.h> 677 #endif 678 679 /* 680 PetscInitialize_Common - shared code between C and Fortran initialization 681 682 prog: program name 683 file: optional PETSc database file name. Might be in Fortran string format when 'fortran' is true 684 help: program help message 685 fortran: is it called from Fortran initilization (petscinitializef_)? 686 readarguments,len: used when fortran is true 687 */ 688 PETSC_INTERN PetscErrorCode PetscInitialize_Common(const char* prog,const char* file,const char *help,PetscBool fortran,PetscBool readarguments,PetscInt len) 689 { 690 PetscErrorCode ierr; 691 PetscMPIInt size; 692 PetscBool flg = PETSC_TRUE; 693 char hostname[256]; 694 695 /* 696 The checking over compatible runtime libraries is complicated by the MPI ABI initiative 697 https://wiki.mpich.org/mpich/index.php/ABI_Compatibility_Initiative which started with 698 MPICH v3.1 (Released February 2014) 699 IBM MPI v2.1 (December 2014) 700 Intel MPI Library v5.0 (2014) 701 Cray MPT v7.0.0 (June 2014) 702 As of July 31, 2017 the ABI number still appears to be 12, that is all of the versions 703 listed above and since that time are compatible. 704 705 Unfortunately the MPI ABI initiative has not defined a way to determine the ABI number 706 at compile time or runtime. Thus we will need to systematically track the allowed versions 707 and how they are represented in the mpi.h and MPI_Get_library_version() output in order 708 to perform the checking. 709 710 Currently we only check for pre MPI ABI versions (and packages that do not follow the MPI ABI). 711 712 Questions: 713 714 Should the checks for ABI incompatibility be only on the major version number below? 715 Presumably the output to stderr will be removed before a release. 716 */ 717 718 #if defined(PETSC_HAVE_MPI_GET_LIBRARY_VERSION) 719 { 720 char mpilibraryversion[MPI_MAX_LIBRARY_VERSION_STRING]; 721 PetscMPIInt mpilibraryversionlength; 722 ierr = MPI_Get_library_version(mpilibraryversion,&mpilibraryversionlength);if (ierr) return ierr; 723 /* check for MPICH versions before MPI ABI initiative */ 724 #if defined(MPICH_VERSION) 725 #if MPICH_NUMVERSION < 30100000 726 { 727 char *ver,*lf; 728 PetscBool flg = PETSC_FALSE; 729 ierr = PetscStrstr(mpilibraryversion,"MPICH Version:",&ver);if (ierr) return ierr; 730 if (ver) { 731 ierr = PetscStrchr(ver,'\n',&lf);if (ierr) return ierr; 732 if (lf) { 733 *lf = 0; 734 ierr = PetscStrendswith(ver,MPICH_VERSION,&flg);if (ierr) return ierr; 735 } 736 } 737 if (!flg) { 738 PetscInfo1(NULL,"PETSc warning --- MPICH library version \n%s does not match what PETSc was compiled with %.\n",mpilibraryversion,MPICH_VESION); 739 flg = PETSC_TRUE; 740 } 741 } 742 #endif 743 /* check for OpenMPI version, it is not part of the MPI ABI initiative (is it part of another initiative that needs to be handled?) */ 744 #elif defined(OMPI_MAJOR_VERSION) 745 { 746 char *ver,bs[MPI_MAX_LIBRARY_VERSION_STRING],*bsf; 747 PetscBool flg = PETSC_FALSE; 748 #define PSTRSZ 2 749 char ompistr1[PSTRSZ][MPI_MAX_LIBRARY_VERSION_STRING] = {"Open MPI","FUJITSU MPI"}; 750 char ompistr2[PSTRSZ][MPI_MAX_LIBRARY_VERSION_STRING] = {"v","Library "}; 751 int i; 752 for (i=0; i<PSTRSZ; i++) { 753 ierr = PetscStrstr(mpilibraryversion,ompistr1[i],&ver);if (ierr) return ierr; 754 if (ver) { 755 PetscSNPrintf(bs,MPI_MAX_LIBRARY_VERSION_STRING,"%s%d.%d",ompistr2[i],OMPI_MAJOR_VERSION,OMPI_MINOR_VERSION); 756 ierr = PetscStrstr(ver,bs,&bsf);if (ierr) return ierr; 757 if (bsf) flg = PETSC_TRUE; 758 break; 759 } 760 } 761 if (!flg) { 762 PetscInfo3(NULL,"PETSc warning --- Open MPI library version \n%s does not match what PETSc was compiled with %d.%d.\n",mpilibraryversion,OMPI_MAJOR_VERSION,OMPI_MINOR_VERSION); 763 flg = PETSC_TRUE; 764 } 765 } 766 #endif 767 } 768 #endif 769 770 #if defined(PETSC_HAVE_DLSYM) 771 { 772 PetscInt cnt = 0; 773 /* These symbols are currently in the OpenMPI and MPICH libraries; they may not always be, in that case the test will simply not detect the problem */ 774 if (dlsym(RTLD_DEFAULT,"ompi_mpi_init")) cnt++; 775 if (dlsym(RTLD_DEFAULT,"MPID_Abort")) cnt++; 776 if (cnt > 1) { 777 fprintf(stderr,"PETSc Error --- Application was linked against both OpenMPI and MPICH based MPI libraries and will not run correctly\n"); 778 return PETSC_ERR_MPI_LIB_INCOMP; 779 } 780 } 781 #endif 782 783 /* these must be initialized in a routine, not as a constant declaration*/ 784 PETSC_STDOUT = stdout; 785 PETSC_STDERR = stderr; 786 787 /*CHKERRQ can be used from now */ 788 PetscErrorHandlingInitialized = PETSC_TRUE; 789 790 /* on Windows - set printf to default to printing 2 digit exponents */ 791 #if defined(PETSC_HAVE__SET_OUTPUT_FORMAT) 792 _set_output_format(_TWO_DIGIT_EXPONENT); 793 #endif 794 795 ierr = PetscOptionsCreateDefault();CHKERRQ(ierr); 796 797 PetscFinalizeCalled = PETSC_FALSE; 798 799 ierr = PetscSetProgramName(prog);CHKERRQ(ierr); 800 ierr = PetscSpinlockCreate(&PetscViewerASCIISpinLockOpen);CHKERRQ(ierr); 801 ierr = PetscSpinlockCreate(&PetscViewerASCIISpinLockStdout);CHKERRQ(ierr); 802 ierr = PetscSpinlockCreate(&PetscViewerASCIISpinLockStderr);CHKERRQ(ierr); 803 ierr = PetscSpinlockCreate(&PetscCommSpinLock);CHKERRQ(ierr); 804 805 if (PETSC_COMM_WORLD == MPI_COMM_NULL) PETSC_COMM_WORLD = MPI_COMM_WORLD; 806 ierr = MPI_Comm_set_errhandler(PETSC_COMM_WORLD,MPI_ERRORS_RETURN);CHKERRMPI(ierr); 807 808 if (PETSC_MPI_ERROR_CLASS == MPI_ERR_LASTCODE) { 809 ierr = MPI_Add_error_class(&PETSC_MPI_ERROR_CLASS);CHKERRMPI(ierr); 810 ierr = MPI_Add_error_code(PETSC_MPI_ERROR_CLASS,&PETSC_MPI_ERROR_CODE);CHKERRMPI(ierr); 811 } 812 813 /* Done after init due to a bug in MPICH-GM? */ 814 ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr); 815 816 ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);CHKERRMPI(ierr); 817 ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);CHKERRMPI(ierr); 818 819 MPIU_BOOL = MPI_INT; 820 MPIU_ENUM = MPI_INT; 821 MPIU_FORTRANADDR = (sizeof(void*) == sizeof(int)) ? MPI_INT : MPIU_INT64; 822 if (sizeof(size_t) == sizeof(unsigned)) MPIU_SIZE_T = MPI_UNSIGNED; 823 else if (sizeof(size_t) == sizeof(unsigned long)) MPIU_SIZE_T = MPI_UNSIGNED_LONG; 824 #if defined(PETSC_SIZEOF_LONG_LONG) 825 else if (sizeof(size_t) == sizeof(unsigned long long)) MPIU_SIZE_T = MPI_UNSIGNED_LONG_LONG; 826 #endif 827 else {(*PetscErrorPrintf)("PetscInitialize: Could not find MPI type for size_t\n"); return PETSC_ERR_SUP_SYS;} 828 829 /* 830 Initialized the global complex variable; this is because with 831 shared libraries the constructors for global variables 832 are not called; at least on IRIX. 833 */ 834 #if defined(PETSC_HAVE_COMPLEX) 835 { 836 #if defined(PETSC_CLANGUAGE_CXX) && !defined(PETSC_USE_REAL___FLOAT128) 837 PetscComplex ic(0.0,1.0); 838 PETSC_i = ic; 839 #else 840 PETSC_i = _Complex_I; 841 #endif 842 } 843 #endif /* PETSC_HAVE_COMPLEX */ 844 845 /* 846 Create the PETSc MPI reduction operator that sums of the first 847 half of the entries and maxes the second half. 848 */ 849 ierr = MPI_Op_create(MPIU_MaxSum_Local,1,&MPIU_MAXSUM_OP);CHKERRMPI(ierr); 850 851 #if defined(PETSC_USE_REAL___FLOAT128) 852 ierr = MPI_Type_contiguous(2,MPI_DOUBLE,&MPIU___FLOAT128);CHKERRMPI(ierr); 853 ierr = MPI_Type_commit(&MPIU___FLOAT128);CHKERRMPI(ierr); 854 #if defined(PETSC_HAVE_COMPLEX) 855 ierr = MPI_Type_contiguous(4,MPI_DOUBLE,&MPIU___COMPLEX128);CHKERRMPI(ierr); 856 ierr = MPI_Type_commit(&MPIU___COMPLEX128);CHKERRMPI(ierr); 857 #endif 858 ierr = MPI_Op_create(PetscMax_Local,1,&MPIU_MAX);CHKERRMPI(ierr); 859 ierr = MPI_Op_create(PetscMin_Local,1,&MPIU_MIN);CHKERRMPI(ierr); 860 #elif defined(PETSC_USE_REAL___FP16) 861 ierr = MPI_Type_contiguous(2,MPI_CHAR,&MPIU___FP16);CHKERRMPI(ierr); 862 ierr = MPI_Type_commit(&MPIU___FP16);CHKERRMPI(ierr); 863 ierr = MPI_Op_create(PetscMax_Local,1,&MPIU_MAX);CHKERRMPI(ierr); 864 ierr = MPI_Op_create(PetscMin_Local,1,&MPIU_MIN);CHKERRMPI(ierr); 865 #endif 866 867 #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16) 868 ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);CHKERRMPI(ierr); 869 #endif 870 871 ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);CHKERRMPI(ierr); 872 ierr = MPI_Type_commit(&MPIU_2SCALAR);CHKERRMPI(ierr); 873 874 /* create datatypes used by MPIU_MAXLOC, MPIU_MINLOC and PetscSplitReduction_Op */ 875 #if !defined(PETSC_HAVE_MPIUNI) 876 { 877 struct PetscRealInt { PetscReal v; PetscInt i; }; 878 PetscMPIInt blockSizes[2] = {1,1}; 879 MPI_Aint blockOffsets[2] = {offsetof(struct PetscRealInt,v),offsetof(struct PetscRealInt,i)}; 880 MPI_Datatype blockTypes[2] = {MPIU_REAL,MPIU_INT}, tmpStruct; 881 882 ierr = MPI_Type_create_struct(2,blockSizes,blockOffsets,blockTypes,&tmpStruct);CHKERRMPI(ierr); 883 ierr = MPI_Type_create_resized(tmpStruct,0,sizeof(struct PetscRealInt),&MPIU_REAL_INT);CHKERRMPI(ierr); 884 ierr = MPI_Type_free(&tmpStruct);CHKERRMPI(ierr); 885 ierr = MPI_Type_commit(&MPIU_REAL_INT);CHKERRMPI(ierr); 886 } 887 { 888 struct PetscScalarInt { PetscScalar v; PetscInt i; }; 889 PetscMPIInt blockSizes[2] = {1,1}; 890 MPI_Aint blockOffsets[2] = {offsetof(struct PetscScalarInt,v),offsetof(struct PetscScalarInt,i)}; 891 MPI_Datatype blockTypes[2] = {MPIU_SCALAR,MPIU_INT}, tmpStruct; 892 893 ierr = MPI_Type_create_struct(2,blockSizes,blockOffsets,blockTypes,&tmpStruct);CHKERRMPI(ierr); 894 ierr = MPI_Type_create_resized(tmpStruct,0,sizeof(struct PetscScalarInt),&MPIU_SCALAR_INT);CHKERRMPI(ierr); 895 ierr = MPI_Type_free(&tmpStruct);CHKERRMPI(ierr); 896 ierr = MPI_Type_commit(&MPIU_SCALAR_INT);CHKERRMPI(ierr); 897 } 898 #endif 899 900 #if defined(PETSC_USE_64BIT_INDICES) 901 ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);CHKERRMPI(ierr); 902 ierr = MPI_Type_commit(&MPIU_2INT);CHKERRMPI(ierr); 903 #endif 904 ierr = MPI_Type_contiguous(4,MPI_INT,&MPI_4INT);CHKERRMPI(ierr); 905 ierr = MPI_Type_commit(&MPI_4INT);CHKERRMPI(ierr); 906 ierr = MPI_Type_contiguous(4,MPIU_INT,&MPIU_4INT);CHKERRMPI(ierr); 907 ierr = MPI_Type_commit(&MPIU_4INT);CHKERRMPI(ierr); 908 909 /* 910 Attributes to be set on PETSc communicators 911 */ 912 ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_Counter_Attr_Delete_Fn,&Petsc_Counter_keyval,(void*)0);CHKERRMPI(ierr); 913 ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_InnerComm_Attr_Delete_Fn,&Petsc_InnerComm_keyval,(void*)0);CHKERRMPI(ierr); 914 ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_OuterComm_Attr_Delete_Fn,&Petsc_OuterComm_keyval,(void*)0);CHKERRMPI(ierr); 915 ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_ShmComm_Attr_Delete_Fn,&Petsc_ShmComm_keyval,(void*)0);CHKERRMPI(ierr); 916 917 #if defined(PETSC_HAVE_FORTRAN) 918 if (fortran) {ierr = PetscInitFortran_Private(readarguments,file,len);CHKERRQ(ierr);} 919 else 920 #endif 921 {ierr = PetscOptionsInsert(NULL,&PetscGlobalArgc,&PetscGlobalArgs,file);CHKERRQ(ierr);} 922 923 /* call a second time so it can look in the options database */ 924 ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr); 925 926 /* 927 Check system options and print help 928 */ 929 ierr = PetscOptionsCheckInitial_Private(help);CHKERRQ(ierr); 930 931 ierr = PetscCitationsInitialize();CHKERRQ(ierr); 932 933 #if defined(PETSC_HAVE_SAWS) 934 ierr = PetscInitializeSAWs(fortran ? NULL : help);CHKERRQ(ierr); 935 #endif 936 937 /* 938 Load the dynamic libraries (on machines that support them), this registers all 939 the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes) 940 */ 941 ierr = PetscInitialize_DynamicLibraries();CHKERRQ(ierr); 942 943 ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRMPI(ierr); 944 ierr = PetscInfo1(NULL,"PETSc successfully started: number of processors = %d\n",size);CHKERRQ(ierr); 945 ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr); 946 ierr = PetscInfo1(NULL,"Running on machine: %s\n",hostname);CHKERRQ(ierr); 947 #if defined(PETSC_HAVE_OPENMP) 948 { 949 PetscBool omp_view_flag; 950 char *threads = getenv("OMP_NUM_THREADS"); 951 952 if (threads) { 953 ierr = PetscInfo1(NULL,"Number of OpenMP threads %s (given by OMP_NUM_THREADS)\n",threads);CHKERRQ(ierr); 954 (void) sscanf(threads, "%" PetscInt_FMT,&PetscNumOMPThreads); 955 } else { 956 #pragma omp parallel 957 #pragma omp master 958 { 959 PetscNumOMPThreads = (PetscInt) omp_get_num_threads(); 960 } 961 ierr = PetscInfo1(NULL,"Number of OpenMP threads %D (number not set with OMP_NUM_THREADS, chosen by system)\n",PetscNumOMPThreads);CHKERRQ(ierr); 962 } 963 ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"OpenMP options","Sys");CHKERRQ(ierr); 964 ierr = PetscOptionsInt("-omp_num_threads","Number of OpenMP threads to use (can also use environmental variable OMP_NUM_THREADS","None",PetscNumOMPThreads,&PetscNumOMPThreads,&flg);CHKERRQ(ierr); 965 ierr = PetscOptionsName("-omp_view","Display OpenMP number of threads",NULL,&omp_view_flag);CHKERRQ(ierr); 966 ierr = PetscOptionsEnd();CHKERRQ(ierr); 967 if (flg) { 968 ierr = PetscInfo1(NULL,"Number of OpenMP theads %D (given by -omp_num_threads)\n",PetscNumOMPThreads);CHKERRQ(ierr); 969 omp_set_num_threads((int)PetscNumOMPThreads); 970 } 971 if (omp_view_flag) { 972 ierr = PetscPrintf(PETSC_COMM_WORLD,"OpenMP: number of threads %D\n",PetscNumOMPThreads);CHKERRQ(ierr); 973 } 974 } 975 #endif 976 977 #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32) 978 /* 979 Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI 980 981 Currently not used because it is not supported by MPICH. 982 */ 983 if (!PetscBinaryBigEndian()) { 984 ierr = MPI_Register_datarep((char*)"petsc",PetscDataRep_read_conv_fn,PetscDataRep_write_conv_fn,PetscDataRep_extent_fn,NULL);CHKERRMPI(ierr); 985 } 986 #endif 987 988 /* 989 Setup building of stack frames for all function calls 990 */ 991 flg = PETSC_FALSE; 992 ierr = PetscOptionsGetBool(NULL,NULL,"-checkstack",&flg,NULL);CHKERRQ(ierr); 993 #if defined(PETSC_USE_DEBUG) && !defined(PETSC_HAVE_THREADSAFETY) 994 ierr = PetscStackCreate(flg);CHKERRQ(ierr); 995 #endif 996 997 #if defined(PETSC_SERIALIZE_FUNCTIONS) 998 ierr = PetscFPTCreate(10000);CHKERRQ(ierr); 999 #endif 1000 1001 #if defined(PETSC_HAVE_HWLOC) 1002 { 1003 PetscViewer viewer; 1004 ierr = PetscOptionsGetViewer(PETSC_COMM_WORLD,NULL,NULL,"-process_view",&viewer,NULL,&flg);CHKERRQ(ierr); 1005 if (flg) { 1006 ierr = PetscProcessPlacementView(viewer);CHKERRQ(ierr); 1007 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1008 } 1009 } 1010 #endif 1011 1012 flg = PETSC_TRUE; 1013 ierr = PetscOptionsGetBool(NULL,NULL,"-viewfromoptions",&flg,NULL);CHKERRQ(ierr); 1014 if (!flg) {ierr = PetscOptionsPushGetViewerOff(PETSC_TRUE);CHKERRQ(ierr);} 1015 1016 #if defined(PETSC_HAVE_ADIOS) 1017 ierr = adios_init_noxml(PETSC_COMM_WORLD);CHKERRQ(ierr); 1018 ierr = adios_declare_group(&Petsc_adios_group,"PETSc","",adios_stat_default);CHKERRQ(ierr); 1019 ierr = adios_select_method(Petsc_adios_group,"MPI","","");CHKERRQ(ierr); 1020 ierr = adios_read_init_method(ADIOS_READ_METHOD_BP,PETSC_COMM_WORLD,"");CHKERRQ(ierr); 1021 #endif 1022 1023 #if defined(__VALGRIND_H) 1024 PETSC_RUNNING_ON_VALGRIND = RUNNING_ON_VALGRIND? PETSC_TRUE: PETSC_FALSE; 1025 #if defined(PETSC_USING_DARWIN) && defined(PETSC_BLASLAPACK_SDOT_RETURNS_DOUBLE) 1026 if (PETSC_RUNNING_ON_VALGRIND) { 1027 ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING: Running valgrind with the MacOS native BLAS and LAPACK can fail. If it fails suggest configuring with --download-fblaslapack or --download-f2cblaslapack");CHKERRQ(ierr); 1028 } 1029 #endif 1030 #endif 1031 1032 #if (defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_HIP)) && defined(PETSC_EXPERIMENTAL) 1033 ierr = PetscDeviceInitializeDefaultDevices_Internal();CHKERRQ(ierr); 1034 ierr = PetscDeviceContextInitializeRootContext_Internal(PETSC_COMM_WORLD,NULL);CHKERRQ(ierr); 1035 #endif 1036 1037 /* 1038 Set flag that we are completely initialized 1039 */ 1040 PetscInitializeCalled = PETSC_TRUE; 1041 1042 ierr = PetscOptionsHasName(NULL,NULL,"-python",&flg);CHKERRQ(ierr); 1043 if (flg) {ierr = PetscPythonInitialize(NULL,NULL);CHKERRQ(ierr);} 1044 return 0; 1045 } 1046 1047 /*@C 1048 PetscInitialize - Initializes the PETSc database and MPI. 1049 PetscInitialize() calls MPI_Init() if that has yet to be called, 1050 so this routine should always be called near the beginning of 1051 your program -- usually the very first line! 1052 1053 Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set 1054 1055 Input Parameters: 1056 + argc - count of number of command line arguments 1057 . args - the command line arguments 1058 . file - [optional] PETSc database file, append ":yaml" to filename to specify YAML options format. 1059 Use NULL or empty string to not check for code specific file. 1060 Also checks ~/.petscrc, .petscrc and petscrc. 1061 Use -skip_petscrc in the code specific file (or command line) to skip ~/.petscrc, .petscrc and petscrc files. 1062 - help - [optional] Help message to print, use NULL for no message 1063 1064 If you wish PETSc code to run ONLY on a subcommunicator of MPI_COMM_WORLD, create that 1065 communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize(). Thus if you are running a 1066 four process job and two processes will run PETSc and have PetscInitialize() and PetscFinalize() and two process will not, 1067 then do this. If ALL processes in the job are using PetscInitialize() and PetscFinalize() then you don't need to do this, even 1068 if different subcommunicators of the job are doing different things with PETSc. 1069 1070 Options Database Keys: 1071 + -help [intro] - prints help method for each option; if intro is given the program stops after printing the introductory help message 1072 . -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger 1073 . -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected 1074 . -on_error_emacs <machinename> - causes emacsclient to jump to error file 1075 . -on_error_abort - calls abort() when error detected (no traceback) 1076 . -on_error_mpiabort - calls MPI_abort() when error detected 1077 . -error_output_stderr - prints error messages to stderr instead of the default stdout 1078 . -error_output_none - does not print the error messages (but handles errors in the same way as if this was not called) 1079 . -debugger_ranks [rank1,rank2,...] - Indicates ranks to start in debugger 1080 . -debugger_pause [sleeptime] (in seconds) - Pauses debugger 1081 . -stop_for_debugger - Print message on how to attach debugger manually to 1082 process and wait (-debugger_pause) seconds for attachment 1083 . -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries) (deprecated, use -malloc_debug) 1084 . -malloc no - Indicates not to use error-checking malloc (deprecated, use -malloc_debug no) 1085 . -malloc_debug - check for memory corruption at EVERY malloc or free, see PetscMallocSetDebug() 1086 . -malloc_dump - prints a list of all unfreed memory at the end of the run 1087 . -malloc_test - like -malloc_dump -malloc_debug, but only active for debugging builds, ignored in optimized build. May want to set in PETSC_OPTIONS environmental variable 1088 . -malloc_view - show a list of all allocated memory during PetscFinalize() 1089 . -malloc_view_threshold <t> - only list memory allocations of size greater than t with -malloc_view 1090 . -malloc_requested_size - malloc logging will record the requested size rather than size after alignment 1091 . -fp_trap - Stops on floating point exceptions 1092 . -no_signal_handler - Indicates not to trap error signals 1093 . -shared_tmp - indicates /tmp directory is shared by all processors 1094 . -not_shared_tmp - each processor has own /tmp 1095 . -tmp - alternative name of /tmp directory 1096 . -get_total_flops - returns total flops done by all processors 1097 - -memory_view - Print memory usage at end of run 1098 1099 Options Database Keys for Option Database: 1100 + -skip_petscrc - skip the default option files ~/.petscrc, .petscrc, petscrc 1101 . -options_monitor - monitor all set options to standard output for the whole program run 1102 - -options_monitor_cancel - cancel options monitoring hard-wired using PetscOptionsMonitorSet() 1103 1104 Options -options_monitor_{all,cancel} are 1105 position-independent and apply to all options set since the PETSc start. 1106 They can be used also in option files. 1107 1108 See PetscOptionsMonitorSet() to do monitoring programmatically. 1109 1110 Options Database Keys for Profiling: 1111 See Users-Manual: ch_profiling for details. 1112 + -info [filename][:[~]<list,of,classnames>[:[~]self]] - Prints verbose information. See PetscInfo(). 1113 . -log_sync - Enable barrier synchronization for all events. This option is useful to debug imbalance within each event, 1114 however it slows things down and gives a distorted view of the overall runtime. 1115 . -log_trace [filename] - Print traces of all PETSc calls to the screen (useful to determine where a program 1116 hangs without running in the debugger). See PetscLogTraceBegin(). 1117 . -log_view [:filename:format] - Prints summary of flop and timing information to screen or file, see PetscLogView(). 1118 . -log_view_memory - Includes in the summary from -log_view the memory used in each method, see PetscLogView(). 1119 . -log_summary [filename] - (Deprecated, use -log_view) Prints summary of flop and timing information to screen. If the filename is specified the 1120 summary is written to the file. See PetscLogView(). 1121 . -log_exclude: <vec,mat,pc,ksp,snes> - excludes subset of object classes from logging 1122 . -log_all [filename] - Logs extensive profiling information See PetscLogDump(). 1123 . -log [filename] - Logs basic profiline information See PetscLogDump(). 1124 . -log_mpe [filename] - Creates a logfile viewable by the utility Jumpshot (in MPICH distribution) 1125 . -viewfromoptions on,off - Enable or disable XXXSetFromOptions() calls, for applications with many small solves turn this off 1126 - -check_pointer_intensity 0,1,2 - if pointers are checked for validity (debug version only), using 0 will result in faster code 1127 1128 Only one of -log_trace, -log_view, -log_view, -log_all, -log, or -log_mpe may be used at a time 1129 1130 Options Database Keys for SAWs: 1131 + -saws_port <portnumber> - port number to publish SAWs data, default is 8080 1132 . -saws_port_auto_select - have SAWs select a new unique port number where it publishes the data, the URL is printed to the screen 1133 this is useful when you are running many jobs that utilize SAWs at the same time 1134 . -saws_log <filename> - save a log of all SAWs communication 1135 . -saws_https <certificate file> - have SAWs use HTTPS instead of HTTP 1136 - -saws_root <directory> - allow SAWs to have access to the given directory to search for requested resources and files 1137 1138 Environmental Variables: 1139 + PETSC_TMP - alternative tmp directory 1140 . PETSC_SHARED_TMP - tmp is shared by all processes 1141 . PETSC_NOT_SHARED_TMP - each process has its own private tmp 1142 . PETSC_OPTIONS - a string containing additional options for petsc in the form of command line "-key value" pairs 1143 . PETSC_OPTIONS_YAML - (requires configuring PETSc to use libyaml) a string containing additional options for petsc in the form of a YAML document 1144 . PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer 1145 - PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to 1146 1147 Level: beginner 1148 1149 Notes: 1150 If for some reason you must call MPI_Init() separately, call 1151 it before PetscInitialize(). 1152 1153 Fortran Version: 1154 In Fortran this routine has the format 1155 $ call PetscInitialize(file,ierr) 1156 1157 + ierr - error return code 1158 - file - [optional] PETSc database file, also checks ~/.petscrc, .petscrc and petscrc. 1159 Use PETSC_NULL_CHARACTER to not check for code specific file. 1160 Use -skip_petscrc in the code specific file (or command line) to skip ~/.petscrc, .petscrc and petscrc files. 1161 1162 Important Fortran Note: 1163 In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a 1164 null character string; you CANNOT just use NULL as 1165 in the C version. See Users-Manual: ch_fortran for details. 1166 1167 If your main program is C but you call Fortran code that also uses PETSc you need to call PetscInitializeFortran() soon after 1168 calling PetscInitialize(). 1169 1170 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments() 1171 1172 @*/ 1173 PetscErrorCode PetscInitialize(int *argc,char ***args,const char file[],const char help[]) 1174 { 1175 PetscErrorCode ierr; 1176 PetscMPIInt flag; 1177 const char *prog = "Unknown Name"; 1178 1179 if (PetscInitializeCalled) return 0; 1180 ierr = MPI_Initialized(&flag);CHKERRMPI(ierr); 1181 if (!flag) { 1182 if (PETSC_COMM_WORLD != MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"You cannot set PETSC_COMM_WORLD if you have not initialized MPI first"); 1183 ierr = PetscPreMPIInit_Private();CHKERRQ(ierr); 1184 #if defined(PETSC_HAVE_MPI_INIT_THREAD) 1185 { 1186 PetscMPIInt provided; 1187 ierr = MPI_Init_thread(argc,args,PETSC_MPI_THREAD_REQUIRED,&provided);CHKERRMPI(ierr); 1188 } 1189 #else 1190 ierr = MPI_Init(argc,args);CHKERRMPI(ierr); 1191 #endif 1192 PetscBeganMPI = PETSC_TRUE; 1193 } 1194 1195 if (argc && *argc) prog = **args; 1196 if (argc && args) { 1197 PetscGlobalArgc = *argc; 1198 PetscGlobalArgs = *args; 1199 } 1200 ierr = PetscInitialize_Common(prog,file,help,PETSC_FALSE/*C*/,PETSC_FALSE,0);CHKERRQ(ierr); 1201 return 0; 1202 } 1203 1204 #if defined(PETSC_USE_LOG) 1205 PETSC_INTERN PetscObject *PetscObjects; 1206 PETSC_INTERN PetscInt PetscObjectsCounts; 1207 PETSC_INTERN PetscInt PetscObjectsMaxCounts; 1208 PETSC_INTERN PetscBool PetscObjectsLog; 1209 #endif 1210 1211 /* 1212 Frees all the MPI types and operations that PETSc may have created 1213 */ 1214 PetscErrorCode PetscFreeMPIResources(void) 1215 { 1216 PetscErrorCode ierr; 1217 1218 PetscFunctionBegin; 1219 #if defined(PETSC_USE_REAL___FLOAT128) 1220 ierr = MPI_Type_free(&MPIU___FLOAT128);CHKERRMPI(ierr); 1221 #if defined(PETSC_HAVE_COMPLEX) 1222 ierr = MPI_Type_free(&MPIU___COMPLEX128);CHKERRMPI(ierr); 1223 #endif 1224 ierr = MPI_Op_free(&MPIU_MAX);CHKERRMPI(ierr); 1225 ierr = MPI_Op_free(&MPIU_MIN);CHKERRMPI(ierr); 1226 #elif defined(PETSC_USE_REAL___FP16) 1227 ierr = MPI_Type_free(&MPIU___FP16);CHKERRMPI(ierr); 1228 ierr = MPI_Op_free(&MPIU_MAX);CHKERRMPI(ierr); 1229 ierr = MPI_Op_free(&MPIU_MIN);CHKERRMPI(ierr); 1230 #endif 1231 1232 #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16) 1233 ierr = MPI_Op_free(&MPIU_SUM);CHKERRMPI(ierr); 1234 #endif 1235 1236 ierr = MPI_Type_free(&MPIU_2SCALAR);CHKERRMPI(ierr); 1237 ierr = MPI_Type_free(&MPIU_REAL_INT);CHKERRMPI(ierr); 1238 ierr = MPI_Type_free(&MPIU_SCALAR_INT);CHKERRMPI(ierr); 1239 #if defined(PETSC_USE_64BIT_INDICES) 1240 ierr = MPI_Type_free(&MPIU_2INT);CHKERRMPI(ierr); 1241 #endif 1242 ierr = MPI_Type_free(&MPI_4INT);CHKERRMPI(ierr); 1243 ierr = MPI_Type_free(&MPIU_4INT);CHKERRMPI(ierr); 1244 ierr = MPI_Op_free(&MPIU_MAXSUM_OP);CHKERRMPI(ierr); 1245 PetscFunctionReturn(0); 1246 } 1247 1248 /*@C 1249 PetscFinalize - Checks for options to be called at the conclusion 1250 of the program. MPI_Finalize() is called only if the user had not 1251 called MPI_Init() before calling PetscInitialize(). 1252 1253 Collective on PETSC_COMM_WORLD 1254 1255 Options Database Keys: 1256 + -options_view - Calls PetscOptionsView() 1257 . -options_left - Prints unused options that remain in the database 1258 . -objects_dump [all] - Prints list of objects allocated by the user that have not been freed, the option all cause all outstanding objects to be listed 1259 . -mpidump - Calls PetscMPIDump() 1260 . -malloc_dump <optional filename> - Calls PetscMallocDump(), displays all memory allocated that has not been freed 1261 . -malloc_info - Prints total memory usage 1262 - -malloc_view <optional filename> - Prints list of all memory allocated and where 1263 1264 Level: beginner 1265 1266 Note: 1267 See PetscInitialize() for more general runtime options. 1268 1269 .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscEnd() 1270 @*/ 1271 PetscErrorCode PetscFinalize(void) 1272 { 1273 PetscErrorCode ierr; 1274 PetscMPIInt rank; 1275 PetscInt nopt; 1276 PetscBool flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE; 1277 PetscBool flg; 1278 #if defined(PETSC_USE_LOG) 1279 char mname[PETSC_MAX_PATH_LEN]; 1280 #endif 1281 1282 if (!PetscInitializeCalled) { 1283 printf("PetscInitialize() must be called before PetscFinalize()\n"); 1284 return(PETSC_ERR_ARG_WRONGSTATE); 1285 } 1286 PetscFunctionBegin; 1287 ierr = PetscInfo(NULL,"PetscFinalize() called\n");CHKERRQ(ierr); 1288 1289 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr); 1290 #if defined(PETSC_HAVE_ADIOS) 1291 ierr = adios_read_finalize_method(ADIOS_READ_METHOD_BP_AGGREGATE);CHKERRQ(ierr); 1292 ierr = adios_finalize(rank);CHKERRQ(ierr); 1293 #endif 1294 ierr = PetscOptionsHasName(NULL,NULL,"-citations",&flg);CHKERRQ(ierr); 1295 if (flg) { 1296 char *cits, filename[PETSC_MAX_PATH_LEN]; 1297 FILE *fd = PETSC_STDOUT; 1298 1299 ierr = PetscOptionsGetString(NULL,NULL,"-citations",filename,sizeof(filename),NULL);CHKERRQ(ierr); 1300 if (filename[0]) { 1301 ierr = PetscFOpen(PETSC_COMM_WORLD,filename,"w",&fd);CHKERRQ(ierr); 1302 } 1303 ierr = PetscSegBufferGet(PetscCitationsList,1,&cits);CHKERRQ(ierr); 1304 cits[0] = 0; 1305 ierr = PetscSegBufferExtractAlloc(PetscCitationsList,&cits);CHKERRQ(ierr); 1306 ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"If you publish results based on this computation please cite the following:\n");CHKERRQ(ierr); 1307 ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"===========================================================================\n");CHKERRQ(ierr); 1308 ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"%s",cits);CHKERRQ(ierr); 1309 ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"===========================================================================\n");CHKERRQ(ierr); 1310 ierr = PetscFClose(PETSC_COMM_WORLD,fd);CHKERRQ(ierr); 1311 ierr = PetscFree(cits);CHKERRQ(ierr); 1312 } 1313 ierr = PetscSegBufferDestroy(&PetscCitationsList);CHKERRQ(ierr); 1314 1315 #if defined(PETSC_HAVE_SSL) && defined(PETSC_USE_SOCKET_VIEWER) 1316 /* TextBelt is run for testing purposes only, please do not use this feature often */ 1317 { 1318 PetscInt nmax = 2; 1319 char **buffs; 1320 ierr = PetscMalloc1(2,&buffs);CHKERRQ(ierr); 1321 ierr = PetscOptionsGetStringArray(NULL,NULL,"-textbelt",buffs,&nmax,&flg1);CHKERRQ(ierr); 1322 if (flg1) { 1323 if (!nmax) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"-textbelt requires either the phone number or number,\"message\""); 1324 if (nmax == 1) { 1325 ierr = PetscMalloc1(128,&buffs[1]);CHKERRQ(ierr); 1326 ierr = PetscGetProgramName(buffs[1],32);CHKERRQ(ierr); 1327 ierr = PetscStrcat(buffs[1]," has completed");CHKERRQ(ierr); 1328 } 1329 ierr = PetscTextBelt(PETSC_COMM_WORLD,buffs[0],buffs[1],NULL);CHKERRQ(ierr); 1330 ierr = PetscFree(buffs[0]);CHKERRQ(ierr); 1331 ierr = PetscFree(buffs[1]);CHKERRQ(ierr); 1332 } 1333 ierr = PetscFree(buffs);CHKERRQ(ierr); 1334 } 1335 { 1336 PetscInt nmax = 2; 1337 char **buffs; 1338 ierr = PetscMalloc1(2,&buffs);CHKERRQ(ierr); 1339 ierr = PetscOptionsGetStringArray(NULL,NULL,"-tellmycell",buffs,&nmax,&flg1);CHKERRQ(ierr); 1340 if (flg1) { 1341 if (!nmax) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"-tellmycell requires either the phone number or number,\"message\""); 1342 if (nmax == 1) { 1343 ierr = PetscMalloc1(128,&buffs[1]);CHKERRQ(ierr); 1344 ierr = PetscGetProgramName(buffs[1],32);CHKERRQ(ierr); 1345 ierr = PetscStrcat(buffs[1]," has completed");CHKERRQ(ierr); 1346 } 1347 ierr = PetscTellMyCell(PETSC_COMM_WORLD,buffs[0],buffs[1],NULL);CHKERRQ(ierr); 1348 ierr = PetscFree(buffs[0]);CHKERRQ(ierr); 1349 ierr = PetscFree(buffs[1]);CHKERRQ(ierr); 1350 } 1351 ierr = PetscFree(buffs);CHKERRQ(ierr); 1352 } 1353 #endif 1354 1355 #if defined(PETSC_SERIALIZE_FUNCTIONS) 1356 ierr = PetscFPTDestroy();CHKERRQ(ierr); 1357 #endif 1358 1359 #if defined(PETSC_HAVE_SAWS) 1360 flg = PETSC_FALSE; 1361 ierr = PetscOptionsGetBool(NULL,NULL,"-saw_options",&flg,NULL);CHKERRQ(ierr); 1362 if (flg) { 1363 ierr = PetscOptionsSAWsDestroy();CHKERRQ(ierr); 1364 } 1365 #endif 1366 1367 #if defined(PETSC_HAVE_X) 1368 flg1 = PETSC_FALSE; 1369 ierr = PetscOptionsGetBool(NULL,NULL,"-x_virtual",&flg1,NULL);CHKERRQ(ierr); 1370 if (flg1) { 1371 /* this is a crude hack, but better than nothing */ 1372 ierr = PetscPOpen(PETSC_COMM_WORLD,NULL,"pkill -9 Xvfb","r",NULL);CHKERRQ(ierr); 1373 } 1374 #endif 1375 1376 #if !defined(PETSC_HAVE_THREADSAFETY) 1377 ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_info",&flg2,NULL);CHKERRQ(ierr); 1378 if (!flg2) { 1379 flg2 = PETSC_FALSE; 1380 ierr = PetscOptionsGetBool(NULL,NULL,"-memory_view",&flg2,NULL);CHKERRQ(ierr); 1381 } 1382 if (flg2) { 1383 ierr = PetscMemoryView(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");CHKERRQ(ierr); 1384 } 1385 #endif 1386 1387 #if defined(PETSC_USE_LOG) 1388 flg1 = PETSC_FALSE; 1389 ierr = PetscOptionsGetBool(NULL,NULL,"-get_total_flops",&flg1,NULL);CHKERRQ(ierr); 1390 if (flg1) { 1391 PetscLogDouble flops = 0; 1392 ierr = MPI_Reduce(&petsc_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);CHKERRMPI(ierr); 1393 ierr = PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);CHKERRQ(ierr); 1394 } 1395 #endif 1396 1397 #if defined(PETSC_USE_LOG) 1398 #if defined(PETSC_HAVE_MPE) 1399 mname[0] = 0; 1400 ierr = PetscOptionsGetString(NULL,NULL,"-log_mpe",mname,sizeof(mname),&flg1);CHKERRQ(ierr); 1401 if (flg1) { 1402 if (mname[0]) {ierr = PetscLogMPEDump(mname);CHKERRQ(ierr);} 1403 else {ierr = PetscLogMPEDump(0);CHKERRQ(ierr);} 1404 } 1405 #endif 1406 #endif 1407 1408 /* 1409 Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_(). 1410 */ 1411 ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr); 1412 1413 #if defined(PETSC_USE_LOG) 1414 ierr = PetscOptionsPushGetViewerOff(PETSC_FALSE);CHKERRQ(ierr); 1415 ierr = PetscLogViewFromOptions();CHKERRQ(ierr); 1416 ierr = PetscOptionsPopGetViewerOff();CHKERRQ(ierr); 1417 1418 mname[0] = 0; 1419 ierr = PetscOptionsGetString(NULL,NULL,"-log_summary",mname,sizeof(mname),&flg1);CHKERRQ(ierr); 1420 if (flg1) { 1421 PetscViewer viewer; 1422 ierr = (*PetscHelpPrintf)(PETSC_COMM_WORLD,"\n\n WARNING: -log_summary is being deprecated; switch to -log_view\n\n\n");CHKERRQ(ierr); 1423 if (mname[0]) { 1424 ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);CHKERRQ(ierr); 1425 ierr = PetscLogView(viewer);CHKERRQ(ierr); 1426 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1427 } else { 1428 viewer = PETSC_VIEWER_STDOUT_WORLD; 1429 ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_DEFAULT);CHKERRQ(ierr); 1430 ierr = PetscLogView(viewer);CHKERRQ(ierr); 1431 ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 1432 } 1433 } 1434 1435 /* 1436 Free any objects created by the last block of code. 1437 */ 1438 ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr); 1439 1440 mname[0] = 0; 1441 ierr = PetscOptionsGetString(NULL,NULL,"-log_all",mname,sizeof(mname),&flg1);CHKERRQ(ierr); 1442 ierr = PetscOptionsGetString(NULL,NULL,"-log",mname,sizeof(mname),&flg2);CHKERRQ(ierr); 1443 if (flg1 || flg2) {ierr = PetscLogDump(mname);CHKERRQ(ierr);} 1444 #endif 1445 1446 ierr = PetscStackDestroy();CHKERRQ(ierr); 1447 1448 flg1 = PETSC_FALSE; 1449 ierr = PetscOptionsGetBool(NULL,NULL,"-no_signal_handler",&flg1,NULL);CHKERRQ(ierr); 1450 if (!flg1) { ierr = PetscPopSignalHandler();CHKERRQ(ierr);} 1451 flg1 = PETSC_FALSE; 1452 ierr = PetscOptionsGetBool(NULL,NULL,"-mpidump",&flg1,NULL);CHKERRQ(ierr); 1453 if (flg1) { 1454 ierr = PetscMPIDump(stdout);CHKERRQ(ierr); 1455 } 1456 flg1 = PETSC_FALSE; 1457 flg2 = PETSC_FALSE; 1458 /* preemptive call to avoid listing this option in options table as unused */ 1459 ierr = PetscOptionsHasName(NULL,NULL,"-malloc_dump",&flg1);CHKERRQ(ierr); 1460 ierr = PetscOptionsHasName(NULL,NULL,"-objects_dump",&flg1);CHKERRQ(ierr); 1461 ierr = PetscOptionsGetBool(NULL,NULL,"-options_view",&flg2,NULL);CHKERRQ(ierr); 1462 1463 if (flg2) { 1464 PetscViewer viewer; 1465 ierr = PetscViewerCreate(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr); 1466 ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr); 1467 ierr = PetscOptionsView(NULL,viewer);CHKERRQ(ierr); 1468 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1469 } 1470 1471 /* to prevent PETSc -options_left from warning */ 1472 ierr = PetscOptionsHasName(NULL,NULL,"-nox",&flg1);CHKERRQ(ierr); 1473 ierr = PetscOptionsHasName(NULL,NULL,"-nox_warning",&flg1);CHKERRQ(ierr); 1474 1475 flg3 = PETSC_FALSE; /* default value is required */ 1476 ierr = PetscOptionsGetBool(NULL,NULL,"-options_left",&flg3,&flg1);CHKERRQ(ierr); 1477 if (PetscUnlikelyDebug(!flg1)) flg3 = PETSC_TRUE; 1478 if (flg3) { 1479 if (!flg2 && flg1) { /* have not yet printed the options */ 1480 PetscViewer viewer; 1481 ierr = PetscViewerCreate(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr); 1482 ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr); 1483 ierr = PetscOptionsView(NULL,viewer);CHKERRQ(ierr); 1484 ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1485 } 1486 ierr = PetscOptionsAllUsed(NULL,&nopt);CHKERRQ(ierr); 1487 if (nopt) { 1488 ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");CHKERRQ(ierr); 1489 ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");CHKERRQ(ierr); 1490 if (nopt == 1) { 1491 ierr = PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");CHKERRQ(ierr); 1492 } else { 1493 ierr = PetscPrintf(PETSC_COMM_WORLD,"There are %D unused database options. They are:\n",nopt);CHKERRQ(ierr); 1494 } 1495 } else if (flg3 && flg1) { 1496 ierr = PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");CHKERRQ(ierr); 1497 } 1498 ierr = PetscOptionsLeft(NULL);CHKERRQ(ierr); 1499 } 1500 1501 #if defined(PETSC_HAVE_SAWS) 1502 if (!PetscGlobalRank) { 1503 ierr = PetscStackSAWsViewOff();CHKERRQ(ierr); 1504 PetscStackCallSAWs(SAWs_Finalize,()); 1505 } 1506 #endif 1507 1508 #if defined(PETSC_USE_LOG) 1509 /* 1510 List all objects the user may have forgot to free 1511 */ 1512 if (PetscObjectsLog) { 1513 ierr = PetscOptionsHasName(NULL,NULL,"-objects_dump",&flg1);CHKERRQ(ierr); 1514 if (flg1) { 1515 MPI_Comm local_comm; 1516 char string[64]; 1517 1518 ierr = PetscOptionsGetString(NULL,NULL,"-objects_dump",string,sizeof(string),NULL);CHKERRQ(ierr); 1519 ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRMPI(ierr); 1520 ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr); 1521 ierr = PetscObjectsDump(stdout,(string[0] == 'a') ? PETSC_TRUE : PETSC_FALSE);CHKERRQ(ierr); 1522 ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr); 1523 ierr = MPI_Comm_free(&local_comm);CHKERRMPI(ierr); 1524 } 1525 } 1526 #endif 1527 1528 #if defined(PETSC_USE_LOG) 1529 PetscObjectsCounts = 0; 1530 PetscObjectsMaxCounts = 0; 1531 ierr = PetscFree(PetscObjects);CHKERRQ(ierr); 1532 #endif 1533 1534 /* 1535 Destroy any packages that registered a finalize 1536 */ 1537 ierr = PetscRegisterFinalizeAll();CHKERRQ(ierr); 1538 1539 #if defined(PETSC_USE_LOG) 1540 ierr = PetscLogFinalize();CHKERRQ(ierr); 1541 #endif 1542 1543 /* 1544 Print PetscFunctionLists that have not been properly freed 1545 1546 ierr = PetscFunctionListPrintAll();CHKERRQ(ierr); 1547 */ 1548 1549 if (petsc_history) { 1550 ierr = PetscCloseHistoryFile(&petsc_history);CHKERRQ(ierr); 1551 petsc_history = NULL; 1552 } 1553 ierr = PetscOptionsHelpPrintedDestroy(&PetscOptionsHelpPrintedSingleton);CHKERRQ(ierr); 1554 ierr = PetscInfoDestroy();CHKERRQ(ierr); 1555 1556 #if !defined(PETSC_HAVE_THREADSAFETY) 1557 if (!(PETSC_RUNNING_ON_VALGRIND)) { 1558 char fname[PETSC_MAX_PATH_LEN]; 1559 char sname[PETSC_MAX_PATH_LEN]; 1560 FILE *fd; 1561 int err; 1562 1563 flg2 = PETSC_FALSE; 1564 flg3 = PETSC_FALSE; 1565 if (PetscDefined(USE_DEBUG)) {ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_test",&flg2,NULL);CHKERRQ(ierr);} 1566 ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_debug",&flg3,NULL);CHKERRQ(ierr); 1567 fname[0] = 0; 1568 ierr = PetscOptionsGetString(NULL,NULL,"-malloc_dump",fname,sizeof(fname),&flg1);CHKERRQ(ierr); 1569 if (flg1 && fname[0]) { 1570 1571 PetscSNPrintf(sname,sizeof(sname),"%s_%d",fname,rank); 1572 fd = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname); 1573 ierr = PetscMallocDump(fd);CHKERRQ(ierr); 1574 err = fclose(fd); 1575 if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 1576 } else if (flg1 || flg2 || flg3) { 1577 MPI_Comm local_comm; 1578 1579 ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRMPI(ierr); 1580 ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr); 1581 ierr = PetscMallocDump(stdout);CHKERRQ(ierr); 1582 ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr); 1583 ierr = MPI_Comm_free(&local_comm);CHKERRMPI(ierr); 1584 } 1585 fname[0] = 0; 1586 ierr = PetscOptionsGetString(NULL,NULL,"-malloc_view",fname,sizeof(fname),&flg1);CHKERRQ(ierr); 1587 if (flg1 && fname[0]) { 1588 1589 PetscSNPrintf(sname,sizeof(sname),"%s_%d",fname,rank); 1590 fd = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname); 1591 ierr = PetscMallocView(fd);CHKERRQ(ierr); 1592 err = fclose(fd); 1593 if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 1594 } else if (flg1) { 1595 MPI_Comm local_comm; 1596 1597 ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRMPI(ierr); 1598 ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr); 1599 ierr = PetscMallocView(stdout);CHKERRQ(ierr); 1600 ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr); 1601 ierr = MPI_Comm_free(&local_comm);CHKERRMPI(ierr); 1602 } 1603 } 1604 #endif 1605 1606 /* 1607 Close any open dynamic libraries 1608 */ 1609 ierr = PetscFinalize_DynamicLibraries();CHKERRQ(ierr); 1610 1611 /* Can be destroyed only after all the options are used */ 1612 ierr = PetscOptionsDestroyDefault();CHKERRQ(ierr); 1613 1614 PetscGlobalArgc = 0; 1615 PetscGlobalArgs = NULL; 1616 1617 #if defined(PETSC_HAVE_KOKKOS) 1618 if (PetscBeganKokkos) { 1619 ierr = PetscKokkosFinalize_Private();CHKERRQ(ierr); 1620 PetscBeganKokkos = PETSC_FALSE; 1621 PetscKokkosInitialized = PETSC_FALSE; 1622 } 1623 #endif 1624 1625 #if defined(PETSC_HAVE_NVSHMEM) 1626 if (PetscBeganNvshmem) { 1627 ierr = PetscNvshmemFinalize();CHKERRQ(ierr); 1628 PetscBeganNvshmem = PETSC_FALSE; 1629 } 1630 #endif 1631 1632 #if defined(PETSC_HAVE_CUDA) 1633 if (PetscDefaultCudaStream) {cudaError_t cerr = cudaStreamDestroy(PetscDefaultCudaStream);CHKERRCUDA(cerr);} 1634 if (petsc_gputimer_begin) { 1635 cudaError_t cerr = cudaEventDestroy(petsc_gputimer_begin);CHKERRCUDA(cerr); 1636 } 1637 if (petsc_gputimer_end) { 1638 cudaError_t cerr = cudaEventDestroy(petsc_gputimer_end);CHKERRCUDA(cerr); 1639 } 1640 #endif 1641 1642 #if defined(PETSC_HAVE_HIP) 1643 if (PetscDefaultHipStream) {hipError_t cerr = hipStreamDestroy(PetscDefaultHipStream);CHKERRHIP(cerr);} 1644 if (petsc_gputimer_begin) { 1645 hipError_t cerr = hipEventDestroy(petsc_gputimer_begin);CHKERRHIP(cerr); 1646 } 1647 if (petsc_gputimer_end) { 1648 hipError_t cerr = hipEventDestroy(petsc_gputimer_end);CHKERRHIP(cerr); 1649 } 1650 #endif 1651 1652 ierr = PetscFreeMPIResources();CHKERRQ(ierr); 1653 1654 /* 1655 Destroy any known inner MPI_Comm's and attributes pointing to them 1656 Note this will not destroy any new communicators the user has created. 1657 1658 If all PETSc objects were not destroyed those left over objects will have hanging references to 1659 the MPI_Comms that were freed; but that is ok because those PETSc objects will never be used again 1660 */ 1661 { 1662 PetscCommCounter *counter; 1663 PetscMPIInt flg; 1664 MPI_Comm icomm; 1665 union {MPI_Comm comm; void *ptr;} ucomm; 1666 ierr = MPI_Comm_get_attr(PETSC_COMM_SELF,Petsc_InnerComm_keyval,&ucomm,&flg);CHKERRMPI(ierr); 1667 if (flg) { 1668 icomm = ucomm.comm; 1669 ierr = MPI_Comm_get_attr(icomm,Petsc_Counter_keyval,&counter,&flg);CHKERRMPI(ierr); 1670 if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory"); 1671 1672 ierr = MPI_Comm_delete_attr(PETSC_COMM_SELF,Petsc_InnerComm_keyval);CHKERRMPI(ierr); 1673 ierr = MPI_Comm_delete_attr(icomm,Petsc_Counter_keyval);CHKERRMPI(ierr); 1674 ierr = MPI_Comm_free(&icomm);CHKERRMPI(ierr); 1675 } 1676 ierr = MPI_Comm_get_attr(PETSC_COMM_WORLD,Petsc_InnerComm_keyval,&ucomm,&flg);CHKERRMPI(ierr); 1677 if (flg) { 1678 icomm = ucomm.comm; 1679 ierr = MPI_Comm_get_attr(icomm,Petsc_Counter_keyval,&counter,&flg);CHKERRMPI(ierr); 1680 if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory"); 1681 1682 ierr = MPI_Comm_delete_attr(PETSC_COMM_WORLD,Petsc_InnerComm_keyval);CHKERRMPI(ierr); 1683 ierr = MPI_Comm_delete_attr(icomm,Petsc_Counter_keyval);CHKERRMPI(ierr); 1684 ierr = MPI_Comm_free(&icomm);CHKERRMPI(ierr); 1685 } 1686 } 1687 1688 ierr = MPI_Comm_free_keyval(&Petsc_Counter_keyval);CHKERRMPI(ierr); 1689 ierr = MPI_Comm_free_keyval(&Petsc_InnerComm_keyval);CHKERRMPI(ierr); 1690 ierr = MPI_Comm_free_keyval(&Petsc_OuterComm_keyval);CHKERRMPI(ierr); 1691 ierr = MPI_Comm_free_keyval(&Petsc_ShmComm_keyval);CHKERRMPI(ierr); 1692 1693 ierr = PetscSpinlockDestroy(&PetscViewerASCIISpinLockOpen);CHKERRQ(ierr); 1694 ierr = PetscSpinlockDestroy(&PetscViewerASCIISpinLockStdout);CHKERRQ(ierr); 1695 ierr = PetscSpinlockDestroy(&PetscViewerASCIISpinLockStderr);CHKERRQ(ierr); 1696 ierr = PetscSpinlockDestroy(&PetscCommSpinLock);CHKERRQ(ierr); 1697 1698 if (PetscBeganMPI) { 1699 #if defined(PETSC_HAVE_MPI_FINALIZED) 1700 PetscMPIInt flag; 1701 ierr = MPI_Finalized(&flag);CHKERRMPI(ierr); 1702 if (flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()"); 1703 #endif 1704 ierr = MPI_Finalize();CHKERRMPI(ierr); 1705 } 1706 /* 1707 1708 Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because 1709 the communicator has some outstanding requests on it. Specifically if the 1710 flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See 1711 src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate() 1712 is never freed as it should be. Thus one may obtain messages of the form 1713 [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the 1714 memory was not freed. 1715 1716 */ 1717 ierr = PetscMallocClear();CHKERRQ(ierr); 1718 1719 PetscErrorHandlingInitialized = PETSC_FALSE; 1720 PetscInitializeCalled = PETSC_FALSE; 1721 PetscFinalizeCalled = PETSC_TRUE; 1722 #if defined(PETSC_USE_GCOV) 1723 /* 1724 flush gcov, otherwise during CI the flushing continues into the next pipeline resulting in git not being able to delete directories since the 1725 gcov files are still being added to the directories as git tries to remove the directories. 1726 */ 1727 __gcov_flush(); 1728 #endif 1729 PetscFunctionReturn(0); 1730 } 1731 1732 #if defined(PETSC_MISSING_LAPACK_lsame_) 1733 PETSC_EXTERN int lsame_(char *a,char *b) 1734 { 1735 if (*a == *b) return 1; 1736 if (*a + 32 == *b) return 1; 1737 if (*a - 32 == *b) return 1; 1738 return 0; 1739 } 1740 #endif 1741 1742 #if defined(PETSC_MISSING_LAPACK_lsame) 1743 PETSC_EXTERN int lsame(char *a,char *b) 1744 { 1745 if (*a == *b) return 1; 1746 if (*a + 32 == *b) return 1; 1747 if (*a - 32 == *b) return 1; 1748 return 0; 1749 } 1750 #endif 1751