1 #define PETSC_DLL 2 /* 3 This file defines the initialization of PETSc, including PetscInitialize() 4 */ 5 6 #include "petscsys.h" /*I "petscsys.h" I*/ 7 8 #if defined(PETSC_HAVE_CUDA) 9 #include <cublas.h> 10 #endif 11 12 #if defined(PETSC_USE_LOG) 13 EXTERN PetscErrorCode PetscLogBegin_Private(void); 14 #endif 15 extern PetscBool PetscOpenMPWorker; 16 17 /* -----------------------------------------------------------------------------------------*/ 18 19 extern FILE *petsc_history; 20 21 EXTERN PetscErrorCode PetscInitialize_DynamicLibraries(void); 22 EXTERN PetscErrorCode PetscFinalize_DynamicLibraries(void); 23 EXTERN PetscErrorCode PetscFListDestroyAll(void); 24 EXTERN PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int); 25 EXTERN PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int); 26 EXTERN PetscErrorCode PetscLogCloseHistoryFile(FILE **); 27 28 /* this is used by the _, __, and ___ macros (see include/petscerror.h) */ 29 PetscErrorCode __gierr = 0; 30 31 /* user may set this BEFORE calling PetscInitialize() */ 32 MPI_Comm PETSC_COMM_WORLD = MPI_COMM_NULL; 33 34 PetscMPIInt Petsc_Counter_keyval = MPI_KEYVAL_INVALID; 35 PetscMPIInt Petsc_InnerComm_keyval = MPI_KEYVAL_INVALID; 36 PetscMPIInt Petsc_OuterComm_keyval = MPI_KEYVAL_INVALID; 37 38 /* 39 Declare and set all the string names of the PETSc enums 40 */ 41 const char *PetscBools[] = {"FALSE","TRUE","PetscBool ","PETSC_",0}; 42 const char *PetscDataTypes[] = {"INT", "DOUBLE", "COMPLEX", 43 "LONG","SHORT", "FLOAT", 44 "CHAR","LOGICAL","ENUM","BOOL","LONGDOUBLE","PetscDataType","PETSC_",0}; 45 46 PetscBool PetscPreLoadingUsed = PETSC_FALSE; 47 PetscBool PetscPreLoadingOn = PETSC_FALSE; 48 49 /* 50 Checks the options database for initializations related to the 51 PETSc components 52 */ 53 #undef __FUNCT__ 54 #define __FUNCT__ "PetscOptionsCheckInitial_Components" 55 PetscErrorCode PETSCSYS_DLLEXPORT PetscOptionsCheckInitial_Components(void) 56 { 57 PetscBool flg1; 58 PetscErrorCode ierr; 59 60 PetscFunctionBegin; 61 ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg1);CHKERRQ(ierr); 62 if (flg1) { 63 #if defined (PETSC_USE_LOG) 64 MPI_Comm comm = PETSC_COMM_WORLD; 65 ierr = (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");CHKERRQ(ierr); 66 ierr = (*PetscHelpPrintf)(comm," -log_summary_exclude: <vec,mat,pc.ksp,snes>\n");CHKERRQ(ierr); 67 ierr = (*PetscHelpPrintf)(comm," -info_exclude: <null,vec,mat,pc,ksp,snes,ts>\n");CHKERRQ(ierr); 68 ierr = (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");CHKERRQ(ierr); 69 #endif 70 } 71 PetscFunctionReturn(0); 72 } 73 74 #undef __FUNCT__ 75 #define __FUNCT__ "PetscInitializeNoArguments" 76 /*@C 77 PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without 78 the command line arguments. 79 80 Collective 81 82 Level: advanced 83 84 .seealso: PetscInitialize(), PetscInitializeFortran() 85 @*/ 86 PetscErrorCode PETSCSYS_DLLEXPORT PetscInitializeNoArguments(void) 87 { 88 PetscErrorCode ierr; 89 int argc = 0; 90 char **args = 0; 91 92 PetscFunctionBegin; 93 ierr = PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL); 94 PetscFunctionReturn(ierr); 95 } 96 97 #undef __FUNCT__ 98 #define __FUNCT__ "PetscInitialized" 99 /*@ 100 PetscInitialized - Determine whether PETSc is initialized. 101 102 7 Level: beginner 103 104 .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran() 105 @*/ 106 PetscErrorCode PETSCSYS_DLLEXPORT PetscInitialized(PetscBool *isInitialized) 107 { 108 PetscFunctionBegin; 109 PetscValidPointer(isInitialized, 1); 110 *isInitialized = PetscInitializeCalled; 111 PetscFunctionReturn(0); 112 } 113 114 #undef __FUNCT__ 115 #define __FUNCT__ "PetscFinalized" 116 /*@ 117 PetscFinalized - Determine whether PetscFinalize() has been called yet 118 119 Level: developer 120 121 .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran() 122 @*/ 123 PetscErrorCode PETSCSYS_DLLEXPORT PetscFinalized(PetscBool *isFinalized) 124 { 125 PetscFunctionBegin; 126 PetscValidPointer(isFinalized, 1); 127 *isFinalized = PetscFinalizeCalled; 128 PetscFunctionReturn(0); 129 } 130 131 EXTERN PetscErrorCode PetscOptionsCheckInitial_Private(void); 132 extern PetscBool PetscBeganMPI; 133 134 /* 135 This function is the MPI reduction operation used to compute the sum of the 136 first half of the datatype and the max of the second half. 137 */ 138 MPI_Op PetscMaxSum_Op = 0; 139 140 EXTERN_C_BEGIN 141 #undef __FUNCT__ 142 #define __FUNCT__ "PetscMaxSum_Local" 143 void PETSCSYS_DLLEXPORT MPIAPI PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype) 144 { 145 PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt; 146 147 PetscFunctionBegin; 148 if (*datatype != MPIU_2INT) { 149 (*PetscErrorPrintf)("Can only handle MPIU_2INT data types"); 150 MPI_Abort(MPI_COMM_WORLD,1); 151 } 152 153 for (i=0; i<count; i++) { 154 xout[2*i] = PetscMax(xout[2*i],xin[2*i]); 155 xout[2*i+1] += xin[2*i+1]; 156 } 157 PetscFunctionReturnVoid(); 158 } 159 EXTERN_C_END 160 161 /* 162 Returns the max of the first entry owned by this processor and the 163 sum of the second entry. 164 165 The reason nprocs[2*i] contains lengths nprocs[2*i+1] contains flag of 1 if length is nonzero 166 is so that the PetscMaxSum_Op() can set TWO values, if we passed in only nprocs[i] with lengths 167 there would be no place to store the both needed results. 168 */ 169 #undef __FUNCT__ 170 #define __FUNCT__ "PetscMaxSum" 171 PetscErrorCode PETSCSYS_DLLEXPORT PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum) 172 { 173 PetscMPIInt size,rank; 174 PetscInt *work; 175 PetscErrorCode ierr; 176 177 PetscFunctionBegin; 178 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 179 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 180 ierr = PetscMalloc(2*size*sizeof(PetscInt),&work);CHKERRQ(ierr); 181 ierr = MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);CHKERRQ(ierr); 182 *max = work[2*rank]; 183 *sum = work[2*rank+1]; 184 ierr = PetscFree(work);CHKERRQ(ierr); 185 PetscFunctionReturn(0); 186 } 187 188 /* ----------------------------------------------------------------------------*/ 189 MPI_Op PETSCSYS_DLLEXPORT PetscADMax_Op = 0; 190 191 EXTERN_C_BEGIN 192 #undef __FUNCT__ 193 #define __FUNCT__ "PetscADMax_Local" 194 void PETSCSYS_DLLEXPORT MPIAPI PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 195 { 196 PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out; 197 PetscInt i,count = *cnt; 198 199 PetscFunctionBegin; 200 if (*datatype != MPIU_2SCALAR) { 201 (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types"); 202 MPI_Abort(MPI_COMM_WORLD,1); 203 } 204 205 for (i=0; i<count; i++) { 206 if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) { 207 xout[2*i] = xin[2*i]; 208 xout[2*i+1] = xin[2*i+1]; 209 } 210 } 211 PetscFunctionReturnVoid(); 212 } 213 EXTERN_C_END 214 215 MPI_Op PETSCSYS_DLLEXPORT PetscADMin_Op = 0; 216 217 EXTERN_C_BEGIN 218 #undef __FUNCT__ 219 #define __FUNCT__ "PetscADMin_Local" 220 void PETSCSYS_DLLEXPORT MPIAPI PetscADMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 221 { 222 PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out; 223 PetscInt i,count = *cnt; 224 225 PetscFunctionBegin; 226 if (*datatype != MPIU_2SCALAR) { 227 (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types"); 228 MPI_Abort(MPI_COMM_WORLD,1); 229 } 230 231 for (i=0; i<count; i++) { 232 if (PetscRealPart(xout[2*i]) > PetscRealPart(xin[2*i])) { 233 xout[2*i] = xin[2*i]; 234 xout[2*i+1] = xin[2*i+1]; 235 } 236 } 237 PetscFunctionReturnVoid(); 238 } 239 EXTERN_C_END 240 /* ---------------------------------------------------------------------------------------*/ 241 242 #if defined(PETSC_USE_COMPLEX) 243 244 /* 245 This operation is only needed when using complex numbers with older MPI that does not support complex numbers 246 */ 247 #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX) 248 MPI_Op MPIU_SUM = 0; 249 250 EXTERN_C_BEGIN 251 #undef __FUNCT__ 252 #define __FUNCT__ "PetscSum_Local" 253 void PETSCSYS_DLLEXPORT PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 254 { 255 PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out; 256 PetscInt i,count = *cnt; 257 258 PetscFunctionBegin; 259 if (*datatype != MPIU_SCALAR) { 260 (*PetscErrorPrintf)("Can only handle MPIU_SCALAR data (i.e. double or complex) types"); 261 MPI_Abort(MPI_COMM_WORLD,1); 262 } 263 264 for (i=0; i<count; i++) { 265 xout[i] += xin[i]; 266 } 267 PetscFunctionReturnVoid(); 268 } 269 EXTERN_C_END 270 #endif 271 #endif 272 273 EXTERN_C_BEGIN 274 #undef __FUNCT__ 275 #define __FUNCT__ "Petsc_DelCounter" 276 /* 277 Private routine to delete internal tag/name counter storage when a communicator is freed. 278 279 This is called by MPI, not by users. 280 281 Note: this is declared extern "C" because it is passed to MPI_Keyval_create() 282 283 */ 284 PetscMPIInt PETSCSYS_DLLEXPORT MPIAPI Petsc_DelCounter(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state) 285 { 286 PetscErrorCode ierr; 287 288 PetscFunctionBegin; 289 ierr = PetscInfo1(0,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr); 290 ierr = PetscFree(count_val);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr); 291 PetscFunctionReturn(MPI_SUCCESS); 292 } 293 EXTERN_C_END 294 295 EXTERN_C_BEGIN 296 #undef __FUNCT__ 297 #define __FUNCT__ "Petsc_DelComm" 298 /* 299 This does not actually free anything, it simply marks when a reference count to an internal MPI_Comm reaches zero and the 300 the external MPI_Comm drops its reference to the internal MPI_Comm 301 302 This is called by MPI, not by users. 303 304 Note: this is declared extern "C" because it is passed to MPI_Keyval_create() 305 306 */ 307 PetscMPIInt PETSCSYS_DLLEXPORT MPIAPI Petsc_DelComm(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state) 308 { 309 PetscErrorCode ierr; 310 311 PetscFunctionBegin; 312 ierr = PetscInfo1(0,"Deleting PETSc communicator imbedded in a user MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr); 313 /* actually don't delete anything because we cannot increase the reference count of the communicator anyways */ 314 PetscFunctionReturn(MPI_SUCCESS); 315 } 316 EXTERN_C_END 317 318 #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32) 319 #if !defined(PETSC_WORDS_BIGENDIAN) 320 EXTERN_C_BEGIN 321 extern PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype,MPI_Aint*,void*); 322 extern PetscMPIInt PetscDataRep_read_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*); 323 extern PetscMPIInt PetscDataRep_write_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*); 324 EXTERN_C_END 325 #endif 326 #endif 327 328 int PetscGlobalArgc = 0; 329 char **PetscGlobalArgs = 0; 330 331 #undef __FUNCT__ 332 #define __FUNCT__ "PetscGetArgs" 333 /*@C 334 PetscGetArgs - Allows you to access the raw command line arguments anywhere 335 after PetscInitialize() is called but before PetscFinalize(). 336 337 Not Collective 338 339 Output Parameters: 340 + argc - count of number of command line arguments 341 - args - the command line arguments 342 343 Level: intermediate 344 345 Notes: 346 This is usually used to pass the command line arguments into other libraries 347 that are called internally deep in PETSc or the application. 348 349 The first argument contains the program name as is normal for C arguments. 350 351 Concepts: command line arguments 352 353 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments() 354 355 @*/ 356 PetscErrorCode PETSCSYS_DLLEXPORT PetscGetArgs(int *argc,char ***args) 357 { 358 PetscFunctionBegin; 359 if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()"); 360 *argc = PetscGlobalArgc; 361 *args = PetscGlobalArgs; 362 PetscFunctionReturn(0); 363 } 364 365 #undef __FUNCT__ 366 #define __FUNCT__ "PetscGetArguments" 367 /*@C 368 PetscGetArguments - Allows you to access the command line arguments anywhere 369 after PetscInitialize() is called but before PetscFinalize(). 370 371 Not Collective 372 373 Output Parameters: 374 . args - the command line arguments 375 376 Level: intermediate 377 378 Notes: 379 This does NOT start with the program name and IS null terminated (final arg is void) 380 381 Concepts: command line arguments 382 383 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments() 384 385 @*/ 386 PetscErrorCode PETSCSYS_DLLEXPORT PetscGetArguments(char ***args) 387 { 388 PetscInt i,argc = PetscGlobalArgc; 389 PetscErrorCode ierr; 390 391 PetscFunctionBegin; 392 if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()"); 393 if (!argc) {*args = 0; PetscFunctionReturn(0);} 394 ierr = PetscMalloc(argc*sizeof(char*),args);CHKERRQ(ierr); 395 for (i=0; i<argc-1; i++) { 396 ierr = PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);CHKERRQ(ierr); 397 } 398 (*args)[argc-1] = 0; 399 PetscFunctionReturn(0); 400 } 401 402 #undef __FUNCT__ 403 #define __FUNCT__ "PetscFreeArguments" 404 /*@C 405 PetscFreeArguments - Frees the memory obtained with PetscGetArguments() 406 407 Not Collective 408 409 Output Parameters: 410 . args - the command line arguments 411 412 Level: intermediate 413 414 Concepts: command line arguments 415 416 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments() 417 418 @*/ 419 PetscErrorCode PETSCSYS_DLLEXPORT PetscFreeArguments(char **args) 420 { 421 PetscInt i = 0; 422 PetscErrorCode ierr; 423 424 PetscFunctionBegin; 425 if (!args) {PetscFunctionReturn(0);} 426 while (args[i]) { 427 ierr = PetscFree(args[i]);CHKERRQ(ierr); 428 i++; 429 } 430 ierr = PetscFree(args);CHKERRQ(ierr); 431 PetscFunctionReturn(0); 432 } 433 434 #undef __FUNCT__ 435 #define __FUNCT__ "PetscInitialize" 436 /*@C 437 PetscInitialize - Initializes the PETSc database and MPI. 438 PetscInitialize() calls MPI_Init() if that has yet to be called, 439 so this routine should always be called near the beginning of 440 your program -- usually the very first line! 441 442 Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set 443 444 Input Parameters: 445 + argc - count of number of command line arguments 446 . args - the command line arguments 447 . file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL to not check for 448 code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files 449 - help - [optional] Help message to print, use PETSC_NULL for no message 450 451 If you wish PETSc code to run ONLY on a subcommunicator of MPI_COMM_WORLD, create that 452 communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize(). Thus if you are running a 453 four process job and two processes will run PETSc and have PetscInitialize() and PetscFinalize() and two process will not, 454 then do this. If ALL processes in the job are using PetscInitialize() and PetscFinalize() then you don't need to do this, even 455 if different subcommunicators of the job are doing different things with PETSc. 456 457 Options Database Keys: 458 + -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger 459 . -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected 460 . -on_error_emacs <machinename> causes emacsclient to jump to error file 461 . -on_error_abort calls abort() when error detected (no traceback) 462 . -on_error_mpiabort calls MPI_abort() when error detected 463 . -error_output_stderr prints error messages to stderr instead of the default stdout 464 . -error_output_none does not print the error messages (but handles errors in the same way as if this was not called) 465 . -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger 466 . -debugger_pause [sleeptime] (in seconds) - Pauses debugger 467 . -stop_for_debugger - Print message on how to attach debugger manually to 468 process and wait (-debugger_pause) seconds for attachment 469 . -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries) 470 . -malloc no - Indicates not to use error-checking malloc 471 . -malloc_debug - check for memory corruption at EVERY malloc or free 472 . -fp_trap - Stops on floating point exceptions (Note that on the 473 IBM RS6000 this slows code by at least a factor of 10.) 474 . -no_signal_handler - Indicates not to trap error signals 475 . -shared_tmp - indicates /tmp directory is shared by all processors 476 . -not_shared_tmp - each processor has own /tmp 477 . -tmp - alternative name of /tmp directory 478 . -get_total_flops - returns total flops done by all processors 479 - -memory_info - Print memory usage at end of run 480 481 Options Database Keys for Profiling: 482 See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details. 483 + -log_trace [filename] - Print traces of all PETSc calls 484 to the screen (useful to determine where a program 485 hangs without running in the debugger). See PetscLogTraceBegin(). 486 . -info <optional filename> - Prints verbose information to the screen 487 - -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages 488 489 Environmental Variables: 490 + PETSC_TMP - alternative tmp directory 491 . PETSC_SHARED_TMP - tmp is shared by all processes 492 . PETSC_NOT_SHARED_TMP - each process has its own private tmp 493 . PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer 494 - PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to 495 496 497 Level: beginner 498 499 Notes: 500 If for some reason you must call MPI_Init() separately, call 501 it before PetscInitialize(). 502 503 Fortran Version: 504 In Fortran this routine has the format 505 $ call PetscInitialize(file,ierr) 506 507 + ierr - error return code 508 - file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL_CHARACTER to not check for 509 code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files 510 511 Important Fortran Note: 512 In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a 513 null character string; you CANNOT just use PETSC_NULL as 514 in the C version. See the <a href="../../docs/manual.pdf">users manual</a> for details. 515 516 If your main program is C but you call Fortran code that also uses PETSc you need to call PetscInitializeFortran() soon after 517 calling PetscInitialize(). 518 519 Concepts: initializing PETSc 520 521 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments() 522 523 @*/ 524 PetscErrorCode PETSCSYS_DLLEXPORT PetscInitialize(int *argc,char ***args,const char file[],const char help[]) 525 { 526 PetscErrorCode ierr; 527 PetscMPIInt flag, size; 528 PetscInt nodesize; 529 PetscBool flg; 530 char hostname[256]; 531 532 PetscFunctionBegin; 533 if (PetscInitializeCalled) PetscFunctionReturn(0); 534 535 /* these must be initialized in a routine, not as a constant declaration*/ 536 PETSC_STDOUT = stdout; 537 PETSC_STDERR = stderr; 538 539 ierr = PetscOptionsCreate();CHKERRQ(ierr); 540 541 /* 542 We initialize the program name here (before MPI_Init()) because MPICH has a bug in 543 it that it sets args[0] on all processors to be args[0] on the first processor. 544 */ 545 if (argc && *argc) { 546 ierr = PetscSetProgramName(**args);CHKERRQ(ierr); 547 } else { 548 ierr = PetscSetProgramName("Unknown Name");CHKERRQ(ierr); 549 } 550 551 ierr = MPI_Initialized(&flag);CHKERRQ(ierr); 552 if (!flag) { 553 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"); 554 ierr = MPI_Init(argc,args);CHKERRQ(ierr); 555 PetscBeganMPI = PETSC_TRUE; 556 } 557 if (argc && args) { 558 PetscGlobalArgc = *argc; 559 PetscGlobalArgs = *args; 560 } 561 PetscFinalizeCalled = PETSC_FALSE; 562 563 if (PETSC_COMM_WORLD == MPI_COMM_NULL) { 564 PETSC_COMM_WORLD = MPI_COMM_WORLD; 565 } 566 ierr = MPI_Errhandler_set(PETSC_COMM_WORLD,MPI_ERRORS_RETURN);CHKERRQ(ierr); 567 568 /* Done after init due to a bug in MPICH-GM? */ 569 ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr); 570 571 ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);CHKERRQ(ierr); 572 ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);CHKERRQ(ierr); 573 574 #if defined(PETSC_USE_COMPLEX) 575 /* 576 Initialized the global complex variable; this is because with 577 shared libraries the constructors for global variables 578 are not called; at least on IRIX. 579 */ 580 { 581 #if defined(PETSC_CLANGUAGE_CXX) 582 PetscScalar ic(0.0,1.0); 583 PETSC_i = ic; 584 #else 585 PETSC_i = I; 586 #endif 587 } 588 589 #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX) 590 ierr = MPI_Type_contiguous(2,MPIU_REAL,&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr); 591 ierr = MPI_Type_commit(&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr); 592 ierr = MPI_Type_contiguous(2,MPI_FLOAT,&MPI_C_COMPLEX);CHKERRQ(ierr); 593 ierr = MPI_Type_commit(&MPI_C_COMPLEX);CHKERRQ(ierr); 594 ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);CHKERRQ(ierr); 595 #endif 596 #endif 597 598 /* 599 Create the PETSc MPI reduction operator that sums of the first 600 half of the entries and maxes the second half. 601 */ 602 ierr = MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);CHKERRQ(ierr); 603 604 ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);CHKERRQ(ierr); 605 ierr = MPI_Type_commit(&MPIU_2SCALAR);CHKERRQ(ierr); 606 ierr = MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);CHKERRQ(ierr); 607 ierr = MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);CHKERRQ(ierr); 608 609 ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);CHKERRQ(ierr); 610 ierr = MPI_Type_commit(&MPIU_2INT);CHKERRQ(ierr); 611 612 /* 613 Attributes to be set on PETSc communicators 614 */ 615 ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelCounter,&Petsc_Counter_keyval,(void*)0);CHKERRQ(ierr); 616 ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_InnerComm_keyval,(void*)0);CHKERRQ(ierr); 617 ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_OuterComm_keyval,(void*)0);CHKERRQ(ierr); 618 619 /* 620 Build the options database 621 */ 622 ierr = PetscOptionsInsert(argc,args,file);CHKERRQ(ierr); 623 624 625 /* 626 Print main application help message 627 */ 628 ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr); 629 if (help && flg) { 630 ierr = PetscPrintf(PETSC_COMM_WORLD,help);CHKERRQ(ierr); 631 } 632 ierr = PetscOptionsCheckInitial_Private();CHKERRQ(ierr); 633 634 /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */ 635 #if defined(PETSC_USE_LOG) 636 ierr = PetscLogBegin_Private();CHKERRQ(ierr); 637 #endif 638 639 /* 640 Load the dynamic libraries (on machines that support them), this registers all 641 the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes) 642 */ 643 ierr = PetscInitialize_DynamicLibraries();CHKERRQ(ierr); 644 645 ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); 646 ierr = PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);CHKERRQ(ierr); 647 ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr); 648 ierr = PetscInfo1(0,"Running on machine: %s\n",hostname);CHKERRQ(ierr); 649 650 ierr = PetscOptionsCheckInitial_Components();CHKERRQ(ierr); 651 /* Check the options database for options related to the options database itself */ 652 ierr = PetscOptionsSetFromOptions();CHKERRQ(ierr); 653 654 #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32) 655 /* 656 Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI 657 658 Currently not used because it is not supported by MPICH. 659 */ 660 #if !defined(PETSC_WORDS_BIGENDIAN) 661 ierr = MPI_Register_datarep((char *)"petsc",PetscDataRep_read_conv_fn,PetscDataRep_write_conv_fn,PetscDataRep_extent_fn,PETSC_NULL);CHKERRQ(ierr); 662 #endif 663 #endif 664 665 ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_spawn_size",&nodesize,&flg);CHKERRQ(ierr); 666 if (flg) { 667 #if defined(PETSC_HAVE_MPI_COMM_SPAWN) 668 ierr = PetscOpenMPSpawn((PetscMPIInt) nodesize);CHKERRQ(ierr); /* worker nodes never return from here; they go directly to PetscEnd() */ 669 #else 670 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"PETSc built without MPI 2 (MPI_Comm_spawn) support, use -openmp_merge_size instead"); 671 #endif 672 } else { 673 ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_merge_size",&nodesize,&flg);CHKERRQ(ierr); 674 if (flg) { 675 ierr = PetscOpenMPMerge((PetscMPIInt) nodesize,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 676 if (PetscOpenMPWorker) { /* if worker then never enter user code */ 677 ierr = PetscEnd(); 678 } 679 } 680 } 681 flg = PETSC_FALSE; 682 ierr = PetscOptionsGetTruth(PETSC_NULL,"-python",&flg,PETSC_NULL);CHKERRQ(ierr); 683 if (flg) {ierr = PetscPythonInitialize(PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);} 684 685 #if defined(PETSC_HAVE_CUDA) 686 cublasInit(); 687 #endif 688 689 #if defined(PETSC_HAVE_AMS) 690 ierr = PetscOptionsHasName(PETSC_NULL,"-ams_publish_objects",&flg);CHKERRQ(ierr); 691 if (flg) { 692 PetscAMSPublishAll = PETSC_TRUE; 693 } 694 #endif 695 696 /* 697 Once we are completedly initialized then we can set this variables 698 */ 699 PetscInitializeCalled = PETSC_TRUE; 700 PetscFunctionReturn(0); 701 } 702 703 704 #undef __FUNCT__ 705 #define __FUNCT__ "PetscFinalize" 706 /*@C 707 PetscFinalize - Checks for options to be called at the conclusion 708 of the program. MPI_Finalize() is called only if the user had not 709 called MPI_Init() before calling PetscInitialize(). 710 711 Collective on PETSC_COMM_WORLD 712 713 Options Database Keys: 714 + -options_table - Calls PetscOptionsPrint() 715 . -options_left - Prints unused options that remain in the database 716 . -options_left no - Does not print unused options that remain in the database 717 . -mpidump - Calls PetscMPIDump() 718 . -malloc_dump - Calls PetscMallocDump() 719 . -malloc_info - Prints total memory usage 720 - -malloc_log - Prints summary of memory usage 721 722 Options Database Keys for Profiling: 723 See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details. 724 + -log_summary [filename] - Prints summary of flop and timing 725 information to screen. If the filename is specified the 726 summary is written to the file. See PetscLogPrintSummary(). 727 . -log_summary_python [filename] - Prints data on of flop and timing usage to a file or screen. 728 See PetscLogPrintSummaryPy(). 729 . -log_all [filename] - Logs extensive profiling information 730 See PetscLogDump(). 731 . -log [filename] - Logs basic profiline information See PetscLogDump(). 732 . -log_sync - Log the synchronization in scatters, inner products 733 and norms 734 - -log_mpe [filename] - Creates a logfile viewable by the 735 utility Upshot/Nupshot (in MPICH distribution) 736 737 Level: beginner 738 739 Note: 740 See PetscInitialize() for more general runtime options. 741 742 .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscEnd() 743 @*/ 744 PetscErrorCode PETSCSYS_DLLEXPORT PetscFinalize(void) 745 { 746 PetscErrorCode ierr; 747 PetscMPIInt rank; 748 int nopt; 749 PetscBool flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE; 750 #if defined(PETSC_HAVE_AMS) 751 PetscBool flg = PETSC_FALSE; 752 #endif 753 754 PetscFunctionBegin; 755 756 if (!PetscInitializeCalled) { 757 printf("PetscInitialize() must be called before PetscFinalize()\n"); 758 PetscFunctionReturn(PETSC_ERR_ARG_WRONGSTATE); 759 } 760 ierr = PetscInfo(PETSC_NULL,"PetscFinalize() called\n"); 761 762 #if defined(PETSC_HAVE_AMS) 763 ierr = PetscOptionsGetTruth(PETSC_NULL,"-options_gui",&flg,PETSC_NULL);CHKERRQ(ierr); 764 if (flg) { 765 ierr = PetscOptionsAMSDestroy();CHKERRQ(ierr); 766 } 767 #endif 768 769 ierr = PetscOpenMPFinalize();CHKERRQ(ierr); 770 771 ierr = PetscOptionsGetTruth(PETSC_NULL,"-python",&flg1,PETSC_NULL);CHKERRQ(ierr); 772 if (flg1) {ierr = PetscPythonFinalize();CHKERRQ(ierr);} 773 774 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 775 ierr = PetscOptionsGetTruth(PETSC_NULL,"-malloc_info",&flg2,PETSC_NULL);CHKERRQ(ierr); 776 if (!flg2) { 777 flg2 = PETSC_FALSE; 778 ierr = PetscOptionsGetTruth(PETSC_NULL,"-memory_info",&flg2,PETSC_NULL);CHKERRQ(ierr); 779 } 780 if (flg2) { 781 ierr = PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");CHKERRQ(ierr); 782 } 783 784 /* 785 Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_(). 786 */ 787 ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr); 788 789 /* 790 Free all the registered create functions, such as KSPList, VecList, SNESList, etc 791 */ 792 ierr = PetscFListDestroyAll();CHKERRQ(ierr); 793 794 /* 795 Destroy any packages that registered a finalize 796 */ 797 ierr = PetscRegisterFinalizeAll();CHKERRQ(ierr); 798 799 /* 800 Destroy all the function registration lists created 801 */ 802 ierr = PetscFinalize_DynamicLibraries();CHKERRQ(ierr); 803 804 #if defined(PETSC_USE_LOG) 805 flg1 = PETSC_FALSE; 806 ierr = PetscOptionsGetTruth(PETSC_NULL,"-get_total_flops",&flg1,PETSC_NULL);CHKERRQ(ierr); 807 if (flg1) { 808 PetscLogDouble flops = 0; 809 ierr = MPI_Reduce(&_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);CHKERRQ(ierr); 810 ierr = PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);CHKERRQ(ierr); 811 } 812 #endif 813 814 #if defined(PETSC_USE_DEBUG) && !defined(PETSC_USE_PTHREAD) 815 if (PetscStackActive) { 816 ierr = PetscStackDestroy();CHKERRQ(ierr); 817 } 818 #endif 819 820 #if defined(PETSC_USE_LOG) 821 { 822 char mname[PETSC_MAX_PATH_LEN]; 823 #if defined(PETSC_HAVE_MPE) 824 mname[0] = 0; 825 ierr = PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr); 826 if (flg1){ 827 if (mname[0]) {ierr = PetscLogMPEDump(mname);CHKERRQ(ierr);} 828 else {ierr = PetscLogMPEDump(0);CHKERRQ(ierr);} 829 } 830 #endif 831 mname[0] = 0; 832 ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr); 833 if (flg1) { 834 if (mname[0]) {ierr = PetscLogPrintSummary(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);} 835 else {ierr = PetscLogPrintSummary(PETSC_COMM_WORLD,0);CHKERRQ(ierr);} 836 } 837 838 mname[0] = 0; 839 ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary_python",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr); 840 if (flg1) { 841 PetscViewer viewer; 842 if (mname[0]) { 843 ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);CHKERRQ(ierr); 844 } else { 845 viewer = PETSC_VIEWER_STDOUT_WORLD; 846 } 847 ierr = PetscLogPrintSummaryPython(viewer);CHKERRQ(ierr); 848 ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); 849 } 850 851 ierr = PetscOptionsGetString(PETSC_NULL,"-log_detailed",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr); 852 if (flg1) { 853 if (mname[0]) {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);} 854 else {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,0);CHKERRQ(ierr);} 855 } 856 857 mname[0] = 0; 858 ierr = PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr); 859 ierr = PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);CHKERRQ(ierr); 860 if (flg1 || flg2){ 861 if (mname[0]) PetscLogDump(mname); 862 else PetscLogDump(0); 863 } 864 ierr = PetscLogDestroy();CHKERRQ(ierr); 865 } 866 #endif 867 flg1 = PETSC_FALSE; 868 ierr = PetscOptionsGetTruth(PETSC_NULL,"-no_signal_handler",&flg1,PETSC_NULL);CHKERRQ(ierr); 869 if (!flg1) { ierr = PetscPopSignalHandler();CHKERRQ(ierr);} 870 flg1 = PETSC_FALSE; 871 ierr = PetscOptionsGetTruth(PETSC_NULL,"-mpidump",&flg1,PETSC_NULL);CHKERRQ(ierr); 872 if (flg1) { 873 ierr = PetscMPIDump(stdout);CHKERRQ(ierr); 874 } 875 flg1 = PETSC_FALSE; 876 flg2 = PETSC_FALSE; 877 ierr = PetscOptionsGetTruth(PETSC_NULL,"-malloc_dump",&flg1,PETSC_NULL);CHKERRQ(ierr); 878 ierr = PetscOptionsGetTruth(PETSC_NULL,"-options_table",&flg2,PETSC_NULL);CHKERRQ(ierr); 879 if (flg2) { 880 if (!rank) {ierr = PetscOptionsPrint(stdout);CHKERRQ(ierr);} 881 } 882 883 /* to prevent PETSc -options_left from warning */ 884 ierr = PetscOptionsHasName(PETSC_NULL,"-nox",&flg1);CHKERRQ(ierr); 885 ierr = PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr); 886 887 if (!PetscOpenMPWorker) { /* worker processes skip this because they do not usually process options */ 888 flg3 = PETSC_FALSE; /* default value is required */ 889 ierr = PetscOptionsGetTruth(PETSC_NULL,"-options_left",&flg3,&flg1);CHKERRQ(ierr); 890 ierr = PetscOptionsAllUsed(&nopt);CHKERRQ(ierr); 891 if (flg3) { 892 if (!flg2) { /* have not yet printed the options */ 893 ierr = PetscOptionsPrint(stdout);CHKERRQ(ierr); 894 } 895 if (!nopt) { 896 ierr = PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");CHKERRQ(ierr); 897 } else if (nopt == 1) { 898 ierr = PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");CHKERRQ(ierr); 899 } else { 900 ierr = PetscPrintf(PETSC_COMM_WORLD,"There are %d unused database options. They are:\n",nopt);CHKERRQ(ierr); 901 } 902 } 903 #if defined(PETSC_USE_DEBUG) 904 if (nopt && !flg3 && !flg1) { 905 ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");CHKERRQ(ierr); 906 ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");CHKERRQ(ierr); 907 ierr = PetscOptionsLeft();CHKERRQ(ierr); 908 } else if (nopt && flg3) { 909 #else 910 if (nopt && flg3) { 911 #endif 912 ierr = PetscOptionsLeft();CHKERRQ(ierr); 913 } 914 } 915 916 flg1 = PETSC_FALSE; 917 ierr = PetscOptionsGetTruth(PETSC_NULL,"-log_history",&flg1,PETSC_NULL);CHKERRQ(ierr); 918 if (flg1) { 919 ierr = PetscLogCloseHistoryFile(&petsc_history);CHKERRQ(ierr); 920 petsc_history = 0; 921 } 922 923 ierr = PetscInfoAllow(PETSC_FALSE,PETSC_NULL);CHKERRQ(ierr); 924 925 flg1 = PETSC_FALSE; 926 flg3 = PETSC_FALSE; 927 ierr = PetscOptionsGetTruth(PETSC_NULL,"-malloc_dump",&flg1,PETSC_NULL);CHKERRQ(ierr); 928 ierr = PetscOptionsGetTruth(PETSC_NULL,"-malloc_log",&flg3,PETSC_NULL);CHKERRQ(ierr); 929 if (flg1) { 930 char fname[PETSC_MAX_PATH_LEN]; 931 FILE *fd; 932 int err; 933 934 fname[0] = 0; 935 ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_dump",fname,250,&flg1);CHKERRQ(ierr); 936 if (flg1 && fname[0]) { 937 char sname[PETSC_MAX_PATH_LEN]; 938 939 sprintf(sname,"%s_%d",fname,rank); 940 fd = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname); 941 ierr = PetscMallocDump(fd);CHKERRQ(ierr); 942 err = fclose(fd); 943 if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 944 } else { 945 MPI_Comm local_comm; 946 947 ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr); 948 ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr); 949 ierr = PetscMallocDump(stdout);CHKERRQ(ierr); 950 ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr); 951 ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr); 952 } 953 } 954 if (flg3) { 955 char fname[PETSC_MAX_PATH_LEN]; 956 FILE *fd; 957 958 fname[0] = 0; 959 ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_log",fname,250,&flg1);CHKERRQ(ierr); 960 if (flg1 && fname[0]) { 961 char sname[PETSC_MAX_PATH_LEN]; 962 int err; 963 964 sprintf(sname,"%s_%d",fname,rank); 965 fd = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname); 966 ierr = PetscMallocDumpLog(fd);CHKERRQ(ierr); 967 err = fclose(fd); 968 if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 969 } else { 970 ierr = PetscMallocDumpLog(stdout);CHKERRQ(ierr); 971 } 972 } 973 /* Can be destroyed only after all the options are used */ 974 ierr = PetscOptionsDestroy();CHKERRQ(ierr); 975 976 PetscGlobalArgc = 0; 977 PetscGlobalArgs = 0; 978 979 #if defined(PETSC_USE_COMPLEX) 980 #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX) 981 ierr = MPI_Op_free(&MPIU_SUM);CHKERRQ(ierr); 982 ierr = MPI_Type_free(&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr); 983 ierr = MPI_Type_free(&MPI_C_COMPLEX);CHKERRQ(ierr); 984 #endif 985 #endif 986 ierr = MPI_Type_free(&MPIU_2SCALAR);CHKERRQ(ierr); 987 ierr = MPI_Type_free(&MPIU_2INT);CHKERRQ(ierr); 988 ierr = MPI_Op_free(&PetscMaxSum_Op);CHKERRQ(ierr); 989 ierr = MPI_Op_free(&PetscADMax_Op);CHKERRQ(ierr); 990 ierr = MPI_Op_free(&PetscADMin_Op);CHKERRQ(ierr); 991 992 ierr = MPI_Keyval_free(&Petsc_Counter_keyval);CHKERRQ(ierr); 993 ierr = MPI_Keyval_free(&Petsc_InnerComm_keyval);CHKERRQ(ierr); 994 ierr = MPI_Keyval_free(&Petsc_OuterComm_keyval);CHKERRQ(ierr); 995 996 ierr = PetscInfo(0,"PETSc successfully ended!\n");CHKERRQ(ierr); 997 if (PetscBeganMPI) { 998 #if defined(PETSC_HAVE_MPI_FINALIZED) 999 PetscMPIInt flag; 1000 ierr = MPI_Finalized(&flag);CHKERRQ(ierr); 1001 if (flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()"); 1002 #endif 1003 ierr = MPI_Finalize();CHKERRQ(ierr); 1004 } 1005 1006 if (PETSC_ZOPEFD){ 1007 if (PETSC_ZOPEFD != PETSC_STDOUT) fprintf(PETSC_ZOPEFD, "<<<end>>>"); 1008 else fprintf(PETSC_STDOUT, "<<<end>>>"); 1009 } 1010 1011 #if defined(PETSC_HAVE_CUDA) 1012 cublasShutdown(); 1013 #endif 1014 /* 1015 1016 Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because 1017 the communicator has some outstanding requests on it. Specifically if the 1018 flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See 1019 src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate() 1020 is never freed as it should be. Thus one may obtain messages of the form 1021 [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the 1022 memory was not freed. 1023 1024 */ 1025 ierr = PetscMallocClear();CHKERRQ(ierr); 1026 PetscInitializeCalled = PETSC_FALSE; 1027 PetscFinalizeCalled = PETSC_TRUE; 1028 PetscFunctionReturn(ierr); 1029 } 1030 1031