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