1 #define PETSC_DLL 2 /* 3 This file defines the initialization of PETSc, including PetscInitialize() 4 */ 5 6 #include "petsc.h" /*I "petsc.h" I*/ 7 #include "petscsys.h" 8 9 #if defined(PETSC_USE_LOG) 10 EXTERN PetscErrorCode PetscLogBegin_Private(void); 11 #endif 12 13 /* -----------------------------------------------------------------------------------------*/ 14 15 extern FILE *petsc_history; 16 17 EXTERN PetscErrorCode PetscInitialize_DynamicLibraries(void); 18 EXTERN PetscErrorCode PetscFinalize_DynamicLibraries(void); 19 EXTERN PetscErrorCode PetscFListDestroyAll(void); 20 EXTERN PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int); 21 EXTERN PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int); 22 EXTERN PetscErrorCode PetscLogCloseHistoryFile(FILE **); 23 EXTERN PetscErrorCode PetscOptionsHelpDestroyList(void); 24 25 /* this is used by the _, __, and ___ macros (see include/petscerror.h) */ 26 PetscErrorCode __gierr = 0; 27 28 /* user may set this BEFORE calling PetscInitialize() */ 29 MPI_Comm PETSC_COMM_WORLD = 0; 30 31 /* 32 Declare and set all the string names of the PETSc enums 33 */ 34 const char *PetscTruths[] = {"FALSE","TRUE","PetscTruth","PETSC_",0}; 35 const char *PetscDataTypes[] = {"INT", "DOUBLE", "COMPLEX", 36 "LONG","SHORT", "FLOAT", 37 "CHAR","LOGICAL","ENUM","TRUTH","LONGDOUBLE","PetscDataType","PETSC_",0}; 38 39 PetscCookie PETSC_LARGEST_COOKIE = PETSC_SMALLEST_COOKIE; 40 PetscCookie PETSC_OBJECT_COOKIE = 0; 41 42 PetscTruth PetscPreLoadingUsed = PETSC_FALSE; 43 PetscTruth PetscPreLoadingOn = PETSC_FALSE; 44 45 PetscErrorCode PETSC_DLLEXPORT PetscCookieRegister(PetscCookie *cookie) 46 { 47 if (*cookie == PETSC_DECIDE || *cookie == PETSC_NULL) { 48 *cookie = ++PETSC_LARGEST_COOKIE; 49 } else if (*cookie > 0) { 50 /* Need to check here for montonicity and insert if necessary */ 51 return 0; 52 } else { 53 SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Invalid suggested cookie %d", (int)*cookie); 54 } 55 return 0; 56 } 57 58 /* 59 Checks the options database for initializations related to the 60 PETSc components 61 */ 62 #undef __FUNCT__ 63 #define __FUNCT__ "PetscOptionsCheckInitial_Components" 64 PetscErrorCode PETSC_DLLEXPORT PetscOptionsCheckInitial_Components(void) 65 { 66 PetscTruth flg1; 67 PetscErrorCode ierr; 68 69 PetscFunctionBegin; 70 ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg1);CHKERRQ(ierr); 71 if (flg1) { 72 #if defined (PETSC_USE_LOG) 73 MPI_Comm comm = PETSC_COMM_WORLD; 74 ierr = (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");CHKERRQ(ierr); 75 ierr = (*PetscHelpPrintf)(comm," -log_summary_exclude: <vec,mat,pc.ksp,snes>\n");CHKERRQ(ierr); 76 ierr = (*PetscHelpPrintf)(comm," -info_exclude: <null,vec,mat,pc,ksp,snes,ts>\n");CHKERRQ(ierr); 77 ierr = (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");CHKERRQ(ierr); 78 #endif 79 } 80 PetscFunctionReturn(0); 81 } 82 83 #undef __FUNCT__ 84 #define __FUNCT__ "PetscInitializeNoArguments" 85 /*@C 86 PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without 87 the command line arguments. 88 89 Collective 90 91 Level: advanced 92 93 .seealso: PetscInitialize(), PetscInitializeFortran() 94 @*/ 95 PetscErrorCode PETSC_DLLEXPORT PetscInitializeNoArguments(void) 96 { 97 PetscErrorCode ierr; 98 int argc = 0; 99 char **args = 0; 100 101 PetscFunctionBegin; 102 ierr = PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL); 103 PetscFunctionReturn(ierr); 104 } 105 106 #undef __FUNCT__ 107 #define __FUNCT__ "PetscInitialized" 108 /*@ 109 PetscInitialized - Determine whether PETSc is initialized. 110 111 Level: beginner 112 113 .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran() 114 @*/ 115 PetscErrorCode PETSC_DLLEXPORT PetscInitialized(PetscTruth *isInitialized) 116 { 117 PetscFunctionBegin; 118 PetscValidPointer(isInitialized, 1); 119 *isInitialized = PetscInitializeCalled; 120 PetscFunctionReturn(0); 121 } 122 123 #undef __FUNCT__ 124 #define __FUNCT__ "PetscFinalized" 125 /*@ 126 PetscFinalized - Determine whether PetscFinalize() has been called yet 127 128 Level: developer 129 130 .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran() 131 @*/ 132 PetscErrorCode PETSC_DLLEXPORT PetscFinalized(PetscTruth *isFinalized) 133 { 134 PetscFunctionBegin; 135 PetscValidPointer(isFinalized, 1); 136 *isFinalized = PetscFinalizeCalled; 137 PetscFunctionReturn(0); 138 } 139 140 EXTERN PetscErrorCode PetscOptionsCheckInitial_Private(void); 141 extern PetscTruth PetscBeganMPI; 142 143 /* 144 This function is the MPI reduction operation used to compute the sum of the 145 first half of the datatype and the max of the second half. 146 */ 147 MPI_Op PetscMaxSum_Op = 0; 148 149 EXTERN_C_BEGIN 150 #undef __FUNCT__ 151 #define __FUNCT__ "PetscMaxSum_Local" 152 void PETSC_DLLEXPORT PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype) 153 { 154 PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt; 155 156 PetscFunctionBegin; 157 if (*datatype != MPIU_2INT) { 158 (*PetscErrorPrintf)("Can only handle MPIU_2INT data types"); 159 MPI_Abort(MPI_COMM_WORLD,1); 160 } 161 162 for (i=0; i<count; i++) { 163 xout[2*i] = PetscMax(xout[2*i],xin[2*i]); 164 xout[2*i+1] += xin[2*i+1]; 165 } 166 PetscStackPop; 167 return; 168 } 169 EXTERN_C_END 170 171 /* 172 Returns the max of the first entry owned by this processor and the 173 sum of the second entry. 174 */ 175 #undef __FUNCT__ 176 #define __FUNCT__ "PetscMaxSum" 177 PetscErrorCode PETSC_DLLEXPORT PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum) 178 { 179 PetscMPIInt size,rank; 180 PetscInt *work; 181 PetscErrorCode ierr; 182 183 PetscFunctionBegin; 184 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 185 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 186 ierr = PetscMalloc(2*size*sizeof(PetscInt),&work);CHKERRQ(ierr); 187 ierr = MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);CHKERRQ(ierr); 188 *max = work[2*rank]; 189 *sum = work[2*rank+1]; 190 ierr = PetscFree(work);CHKERRQ(ierr); 191 PetscFunctionReturn(0); 192 } 193 194 /* ----------------------------------------------------------------------------*/ 195 MPI_Op PETSC_DLLEXPORT PetscADMax_Op = 0; 196 197 EXTERN_C_BEGIN 198 #undef __FUNCT__ 199 #define __FUNCT__ "PetscADMax_Local" 200 void PETSC_DLLEXPORT PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 201 { 202 PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out; 203 PetscInt i,count = *cnt; 204 205 PetscFunctionBegin; 206 if (*datatype != MPIU_2SCALAR) { 207 (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types"); 208 MPI_Abort(MPI_COMM_WORLD,1); 209 } 210 211 for (i=0; i<count; i++) { 212 if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) { 213 xout[2*i] = xin[2*i]; 214 xout[2*i+1] = xin[2*i+1]; 215 } 216 } 217 218 PetscStackPop; 219 return; 220 } 221 EXTERN_C_END 222 223 MPI_Op PETSC_DLLEXPORT PetscADMin_Op = 0; 224 225 EXTERN_C_BEGIN 226 #undef __FUNCT__ 227 #define __FUNCT__ "PetscADMin_Local" 228 void PETSC_DLLEXPORT PetscADMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 229 { 230 PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out; 231 PetscInt i,count = *cnt; 232 233 PetscFunctionBegin; 234 if (*datatype != MPIU_2SCALAR) { 235 (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types"); 236 MPI_Abort(MPI_COMM_WORLD,1); 237 } 238 239 for (i=0; i<count; i++) { 240 if (PetscRealPart(xout[2*i]) > PetscRealPart(xin[2*i])) { 241 xout[2*i] = xin[2*i]; 242 xout[2*i+1] = xin[2*i+1]; 243 } 244 } 245 246 PetscStackPop; 247 return; 248 } 249 EXTERN_C_END 250 /* ---------------------------------------------------------------------------------------*/ 251 252 #if defined(PETSC_USE_COMPLEX) 253 MPI_Op PetscSum_Op = 0; 254 255 EXTERN_C_BEGIN 256 #undef __FUNCT__ 257 #define __FUNCT__ "PetscSum_Local" 258 void PETSC_DLLEXPORT PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 259 { 260 PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out; 261 PetscInt i,count = *cnt; 262 263 PetscFunctionBegin; 264 if (*datatype != MPIU_SCALAR) { 265 (*PetscErrorPrintf)("Can only handle MPIU_SCALAR data (i.e. double or complex) types"); 266 MPI_Abort(MPI_COMM_WORLD,1); 267 } 268 269 for (i=0; i<count; i++) { 270 xout[i] += xin[i]; 271 } 272 273 PetscStackPop; 274 return; 275 } 276 EXTERN_C_END 277 #endif 278 279 static int PetscGlobalArgc = 0; 280 static char **PetscGlobalArgs = 0; 281 282 #undef __FUNCT__ 283 #define __FUNCT__ "PetscGetArgs" 284 /*@C 285 PetscGetArgs - Allows you to access the raw command line arguments anywhere 286 after PetscInitialize() is called but before PetscFinalize(). 287 288 Not Collective 289 290 Output Parameters: 291 + argc - count of number of command line arguments 292 - args - the command line arguments 293 294 Level: intermediate 295 296 Notes: 297 This is usually used to pass the command line arguments into other libraries 298 that are called internally deep in PETSc or the application. 299 300 Concepts: command line arguments 301 302 .seealso: PetscFinalize(), PetscInitializeFortran() 303 304 @*/ 305 PetscErrorCode PETSC_DLLEXPORT PetscGetArgs(int *argc,char ***args) 306 { 307 PetscFunctionBegin; 308 if (!PetscGlobalArgs) { 309 SETERRQ(PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()"); 310 } 311 *argc = PetscGlobalArgc; 312 *args = PetscGlobalArgs; 313 PetscFunctionReturn(0); 314 } 315 316 #undef __FUNCT__ 317 #define __FUNCT__ "PetscInitialize" 318 /*@C 319 PetscInitialize - Initializes the PETSc database and MPI. 320 PetscInitialize() calls MPI_Init() if that has yet to be called, 321 so this routine should always be called near the beginning of 322 your program -- usually the very first line! 323 324 Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set 325 326 Input Parameters: 327 + argc - count of number of command line arguments 328 . args - the command line arguments 329 . file - [optional] PETSc database file, defaults to ~username/.petscrc 330 (use PETSC_NULL for default) 331 - help - [optional] Help message to print, use PETSC_NULL for no message 332 333 If you wish PETSc to run on a subcommunicator of MPI_COMM_WORLD, create that 334 communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize() 335 336 Options Database Keys: 337 + -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger 338 . -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected 339 . -on_error_emacs <machinename> causes emacsclient to jump to error file 340 . -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger 341 . -debugger_pause [sleeptime] (in seconds) - Pauses debugger 342 . -stop_for_debugger - Print message on how to attach debugger manually to 343 process and wait (-debugger_pause) seconds for attachment 344 . -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries) 345 . -malloc no - Indicates not to use error-checking malloc 346 . -malloc_debug - check for memory corruption at EVERY malloc or free 347 . -fp_trap - Stops on floating point exceptions (Note that on the 348 IBM RS6000 this slows code by at least a factor of 10.) 349 . -no_signal_handler - Indicates not to trap error signals 350 . -shared_tmp - indicates /tmp directory is shared by all processors 351 . -not_shared_tmp - each processor has own /tmp 352 . -tmp - alternative name of /tmp directory 353 . -get_total_flops - returns total flops done by all processors 354 - -memory_info - Print memory usage at end of run 355 356 Options Database Keys for Profiling: 357 See the Profiling chapter of the users manual for details. 358 + -log_trace [filename] - Print traces of all PETSc calls 359 to the screen (useful to determine where a program 360 hangs without running in the debugger). See PetscLogTraceBegin(). 361 . -info <optional filename> - Prints verbose information to the screen 362 - -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages 363 364 Environmental Variables: 365 + PETSC_TMP - alternative tmp directory 366 . PETSC_SHARED_TMP - tmp is shared by all processes 367 . PETSC_NOT_SHARED_TMP - each process has its own private tmp 368 . PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer 369 - PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to 370 371 372 Level: beginner 373 374 Notes: 375 If for some reason you must call MPI_Init() separately, call 376 it before PetscInitialize(). 377 378 Fortran Version: 379 In Fortran this routine has the format 380 $ call PetscInitialize(file,ierr) 381 382 + ierr - error return code 383 - file - [optional] PETSc database file name, defaults to 384 ~username/.petscrc (use PETSC_NULL_CHARACTER for default) 385 386 Important Fortran Note: 387 In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a 388 null character string; you CANNOT just use PETSC_NULL as 389 in the C version. See the users manual for details. 390 391 392 Concepts: initializing PETSc 393 394 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs() 395 396 @*/ 397 PetscErrorCode PETSC_DLLEXPORT PetscInitialize(int *argc,char ***args,const char file[],const char help[]) 398 { 399 PetscErrorCode ierr; 400 PetscMPIInt flag, size,nodesize; 401 PetscTruth flg; 402 char hostname[256]; 403 404 PetscFunctionBegin; 405 if (PetscInitializeCalled) PetscFunctionReturn(0); 406 407 /* this must be initialized in a routine, not as a constant declaration*/ 408 PETSC_STDOUT = stdout; 409 410 ierr = PetscOptionsCreate();CHKERRQ(ierr); 411 412 /* 413 We initialize the program name here (before MPI_Init()) because MPICH has a bug in 414 it that it sets args[0] on all processors to be args[0] on the first processor. 415 */ 416 if (argc && *argc) { 417 ierr = PetscSetProgramName(**args);CHKERRQ(ierr); 418 } else { 419 ierr = PetscSetProgramName("Unknown Name");CHKERRQ(ierr); 420 } 421 422 423 ierr = MPI_Initialized(&flag);CHKERRQ(ierr); 424 if (!flag) { 425 if (PETSC_COMM_WORLD) SETERRQ(PETSC_ERR_SUP,"You cannot set PETSC_COMM_WORLD if you have not initialized MPI first"); 426 ierr = MPI_Init(argc,args);CHKERRQ(ierr); 427 PetscBeganMPI = PETSC_TRUE; 428 } 429 if (argc && args) { 430 PetscGlobalArgc = *argc; 431 PetscGlobalArgs = *args; 432 } 433 PetscInitializeCalled = PETSC_TRUE; 434 PetscFinalizeCalled = PETSC_FALSE; 435 436 if (!PETSC_COMM_WORLD) { 437 PETSC_COMM_WORLD = MPI_COMM_WORLD; 438 } 439 440 /* Done after init due to a bug in MPICH-GM? */ 441 ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr); 442 443 ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);CHKERRQ(ierr); 444 ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);CHKERRQ(ierr); 445 446 #if defined(PETSC_USE_COMPLEX) 447 /* 448 Initialized the global complex variable; this is because with 449 shared libraries the constructors for global variables 450 are not called; at least on IRIX. 451 */ 452 { 453 #if defined(PETSC_CLANGUAGE_CXX) 454 PetscScalar ic(0.0,1.0); 455 PETSC_i = ic; 456 #else 457 PetscScalar ic; 458 ic = 1.I; 459 PETSC_i = ic; 460 #endif 461 } 462 463 ierr = MPI_Type_contiguous(2,MPIU_REAL,&MPIU_COMPLEX);CHKERRQ(ierr); 464 ierr = MPI_Type_commit(&MPIU_COMPLEX);CHKERRQ(ierr); 465 ierr = MPI_Op_create(PetscSum_Local,1,&PetscSum_Op);CHKERRQ(ierr); 466 #endif 467 468 /* 469 Create the PETSc MPI reduction operator that sums of the first 470 half of the entries and maxes the second half. 471 */ 472 ierr = MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);CHKERRQ(ierr); 473 474 ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);CHKERRQ(ierr); 475 ierr = MPI_Type_commit(&MPIU_2SCALAR);CHKERRQ(ierr); 476 ierr = MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);CHKERRQ(ierr); 477 ierr = MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);CHKERRQ(ierr); 478 479 ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);CHKERRQ(ierr); 480 ierr = MPI_Type_commit(&MPIU_2INT);CHKERRQ(ierr); 481 482 /* 483 Build the options database and check for user setup requests 484 */ 485 ierr = PetscOptionsInsert(argc,args,file);CHKERRQ(ierr); 486 487 /* 488 Print main application help message 489 */ 490 ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr); 491 if (help && flg) { 492 ierr = PetscPrintf(PETSC_COMM_WORLD,help);CHKERRQ(ierr); 493 } 494 ierr = PetscOptionsCheckInitial_Private();CHKERRQ(ierr); 495 496 /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */ 497 #if defined(PETSC_USE_LOG) 498 ierr = PetscLogBegin_Private();CHKERRQ(ierr); 499 #endif 500 501 /* 502 Load the dynamic libraries (on machines that support them), this registers all 503 the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes) 504 */ 505 ierr = PetscInitialize_DynamicLibraries();CHKERRQ(ierr); 506 507 ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); 508 ierr = PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);CHKERRQ(ierr); 509 ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr); 510 ierr = PetscInfo1(0,"Running on machine: %s\n",hostname);CHKERRQ(ierr); 511 512 ierr = PetscOptionsCheckInitial_Components();CHKERRQ(ierr); 513 /* Check the options database for options related to the options database itself */ 514 ierr = PetscOptionsSetFromOptions(); CHKERRQ(ierr); 515 516 ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_node_size",&nodesize,&flg);CHKERRQ(ierr); 517 if (flg) { 518 ierr = PetscOpenMPInitialize(nodesize);CHKERRQ(ierr); 519 } 520 521 PetscFunctionReturn(ierr); 522 } 523 524 525 #undef __FUNCT__ 526 #define __FUNCT__ "PetscFinalize" 527 /*@C 528 PetscFinalize - Checks for options to be called at the conclusion 529 of the program. MPI_Finalize() is called only if the user had not 530 called MPI_Init() before calling PetscInitialize(). 531 532 Collective on PETSC_COMM_WORLD 533 534 Options Database Keys: 535 + -options_table - Calls PetscOptionsPrint() 536 . -options_left - Prints unused options that remain in the database 537 . -options_left no - Does not print unused options that remain in the database 538 . -mpidump - Calls PetscMPIDump() 539 . -malloc_dump - Calls PetscMallocDump() 540 . -malloc_info - Prints total memory usage 541 - -malloc_log - Prints summary of memory usage 542 543 Options Database Keys for Profiling: 544 See the Profiling chapter of the users manual for details. 545 + -log_summary [filename] - Prints summary of flop and timing 546 information to screen. If the filename is specified the 547 summary is written to the file. (for code compiled with 548 PETSC_USE_LOG). See PetscLogPrintSummary(). 549 . -log_all [filename] - Logs extensive profiling information 550 (for code compiled with PETSC_USE_LOG). See PetscLogDump(). 551 . -log [filename] - Logs basic profiline information (for 552 code compiled with PETSC_USE_LOG). See PetscLogDump(). 553 . -log_sync - Log the synchronization in scatters, inner products 554 and norms 555 - -log_mpe [filename] - Creates a logfile viewable by the 556 utility Upshot/Nupshot (in MPICH distribution) 557 558 Level: beginner 559 560 Note: 561 See PetscInitialize() for more general runtime options. 562 563 .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscEnd() 564 @*/ 565 PetscErrorCode PETSC_DLLEXPORT PetscFinalize(void) 566 { 567 PetscErrorCode ierr; 568 PetscMPIInt rank; 569 int nopt; 570 PetscTruth flg1,flg2,flg3; 571 572 PetscFunctionBegin; 573 574 if (!PetscInitializeCalled) { 575 (*PetscErrorPrintf)("PetscInitialize() must be called before PetscFinalize()\n"); 576 PetscFunctionReturn(0); 577 } 578 579 ierr = PetscOpenMPFinalize();CHKERRQ(ierr); 580 581 ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 582 ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_info",&flg2);CHKERRQ(ierr); 583 if (!flg2) { 584 ierr = PetscOptionsHasName(PETSC_NULL,"-memory_info",&flg2);CHKERRQ(ierr); 585 } 586 if (flg2) { 587 ierr = PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");CHKERRQ(ierr); 588 } 589 590 /* Destroy auxiliary packages */ 591 #if defined(PETSC_HAVE_MATHEMATICA) 592 ierr = PetscViewerMathematicaFinalizePackage();CHKERRQ(ierr); 593 #endif 594 #if defined(PETSC_USE_DYNAMIC_LIBRARIES) 595 #if defined(PETSC_HAVE_SIEVE) 596 { 597 PetscErrorCode (*func)(void); 598 char lib[PETSC_MAX_PATH_LEN]; 599 600 ierr = PetscStrcpy(lib,"${PETSC_LIB_DIR}");CHKERRQ(ierr); 601 ierr = PetscStrcat(lib,"/libpetscdm");CHKERRQ(ierr); 602 ierr = PetscDLLibrarySym(PETSC_COMM_WORLD,&DLLibrariesLoaded,lib,"DMFinalizePackage",(void **) &func);CHKERRQ(ierr); 603 if (func) { 604 ierr = (*func)();CHKERRQ(ierr); 605 } 606 } 607 #endif 608 #endif 609 610 /* 611 Destroy all the function registration lists created 612 */ 613 ierr = PetscFinalize_DynamicLibraries();CHKERRQ(ierr); 614 615 #if defined(PETSC_USE_LOG) 616 ierr = PetscOptionsHasName(PETSC_NULL,"-get_total_flops",&flg1);CHKERRQ(ierr); 617 if (flg1) { 618 PetscLogDouble flops = 0; 619 ierr = MPI_Reduce(&_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);CHKERRQ(ierr); 620 ierr = PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);CHKERRQ(ierr); 621 } 622 #endif 623 624 /* 625 Free all objects registered with PetscObjectRegisterDestroy() such ast 626 PETSC_VIEWER_XXX_(). 627 */ 628 ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr); 629 ierr = PetscOptionsHelpDestroyList();CHKERRQ(ierr); 630 631 #if defined(PETSC_USE_DEBUG) 632 if (PetscStackActive) { 633 ierr = PetscStackDestroy();CHKERRQ(ierr); 634 } 635 #endif 636 637 #if defined(PETSC_USE_LOG) 638 { 639 char mname[PETSC_MAX_PATH_LEN]; 640 #if defined(PETSC_HAVE_MPE) 641 mname[0] = 0; 642 ierr = PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr); 643 if (flg1){ 644 if (mname[0]) {ierr = PetscLogMPEDump(mname);CHKERRQ(ierr);} 645 else {ierr = PetscLogMPEDump(0);CHKERRQ(ierr);} 646 } 647 #endif 648 mname[0] = 0; 649 ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr); 650 if (flg1) { 651 if (mname[0]) {ierr = PetscLogPrintSummary(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);} 652 else {ierr = PetscLogPrintSummary(PETSC_COMM_WORLD,0);CHKERRQ(ierr);} 653 } 654 655 mname[0] = 0; 656 ierr = PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr); 657 ierr = PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);CHKERRQ(ierr); 658 if (flg1 || flg2){ 659 if (mname[0]) PetscLogDump(mname); 660 else PetscLogDump(0); 661 } 662 ierr = PetscLogDestroy();CHKERRQ(ierr); 663 } 664 #endif 665 ierr = PetscOptionsHasName(PETSC_NULL,"-no_signal_handler",&flg1);CHKERRQ(ierr); 666 if (!flg1) { ierr = PetscPopSignalHandler();CHKERRQ(ierr);} 667 ierr = PetscOptionsHasName(PETSC_NULL,"-mpidump",&flg1);CHKERRQ(ierr); 668 if (flg1) { 669 ierr = PetscMPIDump(stdout);CHKERRQ(ierr); 670 } 671 ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);CHKERRQ(ierr); 672 ierr = PetscOptionsHasName(PETSC_NULL,"-options_table",&flg2);CHKERRQ(ierr); 673 if (flg2) { 674 if (!rank) {ierr = PetscOptionsPrint(stdout);CHKERRQ(ierr);} 675 } 676 677 /* to prevent PETSc -options_left from warning */ 678 ierr = PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr) 679 ierr = PetscOptionsHasName(PETSC_NULL,"-error_output_stderr",&flg1);CHKERRQ(ierr); 680 681 flg3 = PETSC_FALSE; /* default value is required */ 682 ierr = PetscOptionsGetTruth(PETSC_NULL,"-options_left",&flg3,&flg1);CHKERRQ(ierr); 683 ierr = PetscOptionsAllUsed(&nopt);CHKERRQ(ierr); 684 if (flg3) { 685 if (!flg2) { /* have not yet printed the options */ 686 ierr = PetscOptionsPrint(stdout);CHKERRQ(ierr); 687 } 688 if (!nopt) { 689 ierr = PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");CHKERRQ(ierr); 690 } else if (nopt == 1) { 691 ierr = PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");CHKERRQ(ierr); 692 } else { 693 ierr = PetscPrintf(PETSC_COMM_WORLD,"There are %d unused database options. They are:\n",nopt);CHKERRQ(ierr); 694 } 695 } 696 #if defined(PETSC_USE_DEBUG) 697 if (nopt && !flg3 && !flg1) { 698 ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");CHKERRQ(ierr); 699 ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");CHKERRQ(ierr); 700 ierr = PetscOptionsLeft();CHKERRQ(ierr); 701 } else if (nopt && flg3) { 702 #else 703 if (nopt && flg3) { 704 #endif 705 ierr = PetscOptionsLeft();CHKERRQ(ierr); 706 } 707 708 ierr = PetscOptionsHasName(PETSC_NULL,"-log_history",&flg1);CHKERRQ(ierr); 709 if (flg1) { 710 ierr = PetscLogCloseHistoryFile(&petsc_history);CHKERRQ(ierr); 711 petsc_history = 0; 712 } 713 714 ierr = PetscInfoAllow(PETSC_FALSE,PETSC_NULL);CHKERRQ(ierr); 715 716 /* 717 Free all the registered create functions, such as KSPList, VecList, SNESList, etc 718 */ 719 ierr = PetscFListDestroyAll();CHKERRQ(ierr); 720 721 ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);CHKERRQ(ierr); 722 ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_log",&flg3);CHKERRQ(ierr); 723 if (flg1) { 724 char fname[PETSC_MAX_PATH_LEN]; 725 FILE *fd; 726 727 fname[0] = 0; 728 ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_dump",fname,250,&flg1);CHKERRQ(ierr); 729 if (flg1 && fname[0]) { 730 char sname[PETSC_MAX_PATH_LEN]; 731 732 sprintf(sname,"%s_%d",fname,rank); 733 fd = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname); 734 ierr = PetscMallocDump(fd);CHKERRQ(ierr); 735 fclose(fd); 736 } else { 737 MPI_Comm local_comm; 738 739 ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr); 740 ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr); 741 ierr = PetscMallocDump(stdout);CHKERRQ(ierr); 742 ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr); 743 ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr); 744 } 745 } 746 if (flg3) { 747 char fname[PETSC_MAX_PATH_LEN]; 748 FILE *fd; 749 750 fname[0] = 0; 751 ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_log",fname,250,&flg1);CHKERRQ(ierr); 752 if (flg1 && fname[0]) { 753 char sname[PETSC_MAX_PATH_LEN]; 754 755 sprintf(sname,"%s_%d",fname,rank); 756 fd = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname); 757 ierr = PetscMallocDumpLog(fd);CHKERRQ(ierr); 758 fclose(fd); 759 } else { 760 ierr = PetscMallocDumpLog(stdout);CHKERRQ(ierr); 761 } 762 } 763 /* Can be destroyed only after all the options are used */ 764 ierr = PetscOptionsDestroy();CHKERRQ(ierr); 765 766 PetscGlobalArgc = 0; 767 PetscGlobalArgs = 0; 768 769 #if defined(PETSC_USE_COMPLEX) 770 ierr = MPI_Op_free(&PetscSum_Op);CHKERRQ(ierr); 771 ierr = MPI_Type_free(&MPIU_COMPLEX);CHKERRQ(ierr); 772 #endif 773 ierr = MPI_Type_free(&MPIU_2SCALAR);CHKERRQ(ierr); 774 ierr = MPI_Type_free(&MPIU_2INT);CHKERRQ(ierr); 775 ierr = MPI_Op_free(&PetscMaxSum_Op);CHKERRQ(ierr); 776 ierr = MPI_Op_free(&PetscADMax_Op);CHKERRQ(ierr); 777 ierr = MPI_Op_free(&PetscADMin_Op);CHKERRQ(ierr); 778 779 ierr = PetscInfo(0,"PETSc successfully ended!\n");CHKERRQ(ierr); 780 if (PetscBeganMPI) { 781 ierr = MPI_Finalize();CHKERRQ(ierr); 782 } 783 784 /* 785 786 Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because 787 the communicator has some outstanding requests on it. Specifically if the 788 flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See 789 src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate() 790 is never freed as it should be. Thus one may obtain messages of the form 791 [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the 792 memory was not freed. 793 794 */ 795 ierr = PetscClearMalloc();CHKERRQ(ierr); 796 PetscInitializeCalled = PETSC_FALSE; 797 PetscFinalizeCalled = PETSC_TRUE; 798 PetscFunctionReturn(ierr); 799 } 800 801 /* 802 These may be used in code that ADIC is to be used on 803 */ 804 805 #undef __FUNCT__ 806 #define __FUNCT__ "PetscGlobalMax" 807 /*@C 808 PetscGlobalMax - Computes the maximum value over several processors 809 810 Collective on MPI_Comm 811 812 Input Parameters: 813 + local - the local value 814 - comm - the processors that find the maximum 815 816 Output Parameter: 817 . result - the maximum value 818 819 Level: intermediate 820 821 Notes: 822 These functions are to be used inside user functions that are to be processed with 823 ADIC. PETSc will automatically provide differentiated versions of these functions 824 825 .seealso: PetscGlobalMin(), PetscGlobalSum() 826 @*/ 827 PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal* local,PetscReal* result,MPI_Comm comm) 828 { 829 return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MAX,comm); 830 } 831 832 #undef __FUNCT__ 833 #define __FUNCT__ "PetscGlobalMin" 834 /*@C 835 PetscGlobalMin - Computes the minimum value over several processors 836 837 Collective on MPI_Comm 838 839 Input Parameters: 840 + local - the local value 841 - comm - the processors that find the minimum 842 843 Output Parameter: 844 . result - the minimum value 845 846 Level: intermediate 847 848 Notes: 849 These functions are to be used inside user functions that are to be processed with 850 ADIC. PETSc will automatically provide differentiated versions of these functions 851 852 .seealso: PetscGlobalMax(), PetscGlobalSum() 853 @*/ 854 PetscErrorCode PETSC_DLLEXPORT PetscGlobalMin(PetscReal* local,PetscReal* result,MPI_Comm comm) 855 { 856 return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MIN,comm); 857 } 858 859 #undef __FUNCT__ 860 #define __FUNCT__ "PetscGlobalSum" 861 /*@C 862 PetscGlobalSum - Computes the sum over sever processors 863 864 Collective on MPI_Comm 865 866 Input Parameters: 867 + local - the local value 868 - comm - the processors that find the sum 869 870 Output Parameter: 871 . result - the sum 872 873 Level: intermediate 874 875 Notes: 876 These functions are to be used inside user functions that are to be processed with 877 ADIC. PETSc will automatically provide differentiated versions of these functions 878 879 .seealso: PetscGlobalMin(), PetscGlobalMax() 880 @*/ 881 PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar* local,PetscScalar* result,MPI_Comm comm) 882 { 883 return MPI_Allreduce(local,result,1,MPIU_SCALAR,PetscSum_Op,comm); 884 } 885 886 887