Actual source code: pinit.c

  1: #define PETSC_DLL
  2: /*
  3:    This file defines the initialization of PETSc, including PetscInitialize()
  4: */

 6:  #include petsc.h
 7:  #include petscsys.h

  9: #if defined(PETSC_USE_LOG)
 10: EXTERN PetscErrorCode PetscLogBegin_Private(void);
 11: #endif

 13: /* -----------------------------------------------------------------------------------------*/


 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);

 25: /* this is used by the _, __, and ___ macros (see include/petscerror.h) */
 26: PetscErrorCode __g0;

 28: /* user may set this BEFORE calling PetscInitialize() */
 29: MPI_Comm PETSC_COMM_WORLD = 0;

 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};

 39: PetscCookie PETSC_LARGEST_COOKIE = PETSC_SMALLEST_COOKIE;
 40: PetscCookie PETSC_OBJECT_COOKIE = 0;

 42: PetscTruth PetscPreLoadingUsed = PETSC_FALSE;
 43: PetscTruth PetscPreLoadingOn   = PETSC_FALSE;

 45: PetscErrorCode  PetscCookieRegister(PetscCookie *cookie)
 46: {
 47:   *cookie = ++PETSC_LARGEST_COOKIE;
 48:   return 0;
 49: }

 51: /*
 52:        Checks the options database for initializations related to the 
 53:     PETSc components
 54: */
 57: PetscErrorCode  PetscOptionsCheckInitial_Components(void)
 58: {
 59:   PetscTruth flg1;

 63:   PetscOptionsHasName(PETSC_NULL,"-help",&flg1);
 64:   if (flg1) {
 65: #if defined (PETSC_USE_LOG)
 66:     MPI_Comm   comm = PETSC_COMM_WORLD;
 67:     (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");
 68:     (*PetscHelpPrintf)(comm," -log_summary_exclude: <vec,mat,pc.ksp,snes>\n");
 69:     (*PetscHelpPrintf)(comm," -info_exclude: <null,vec,mat,pc,ksp,snes,ts>\n");
 70:     (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
 71: #endif
 72:   }
 73:   return(0);
 74: }

 78: /*@C
 79:       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
 80:         the command line arguments.

 82:    Collective
 83:   
 84:    Level: advanced

 86: .seealso: PetscInitialize(), PetscInitializeFortran()
 87: @*/
 88: PetscErrorCode  PetscInitializeNoArguments(void)
 89: {
 91:   int            argc = 0;
 92:   char           **args = 0;

 95:   PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL);
 96:   PetscFunctionReturn(ierr);
 97: }

101: /*@
102:       PetscInitialized - Determine whether PETSc is initialized.
103:   
104:    Level: beginner

106: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
107: @*/
108: PetscErrorCode  PetscInitialized(PetscTruth *isInitialized)
109: {
112:   *isInitialized = PetscInitializeCalled;
113:   return(0);
114: }

118: /*@
119:       PetscFinalized - Determine whether PetscFinalize() has been called yet
120:   
121:    Level: developer

123: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
124: @*/
125: PetscErrorCode  PetscFinalized(PetscTruth *isFinalized)
126: {
129:   *isFinalized = PetscFinalizeCalled;
130:   return(0);
131: }

133: EXTERN PetscErrorCode        PetscOptionsCheckInitial_Private(void);

136: /*
137:        This function is the MPI reduction operation used to compute the sum of the 
138:    first half of the datatype and the max of the second half.
139: */
140: MPI_Op PetscMaxSum_Op = 0;

145: void  MPIAPI PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
146: {
147:   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;

150:   if (*datatype != MPIU_2INT) {
151:     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
152:     MPI_Abort(MPI_COMM_WORLD,1);
153:   }

155:   for (i=0; i<count; i++) {
156:     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
157:     xout[2*i+1] += xin[2*i+1];
158:   }
159:   PetscStackPop;
160:   return;
161: }

164: /*
165:     Returns the max of the first entry owned by this processor and the
166: sum of the second entry.
167: */
170: PetscErrorCode  PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum)
171: {
172:   PetscMPIInt    size,rank;
173:   PetscInt       *work;

177:   MPI_Comm_size(comm,&size);
178:   MPI_Comm_rank(comm,&rank);
179:   PetscMalloc(2*size*sizeof(PetscInt),&work);
180:   MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);
181:   *max   = work[2*rank];
182:   *sum   = work[2*rank+1];
183:   PetscFree(work);
184:   return(0);
185: }

187: /* ----------------------------------------------------------------------------*/
188: MPI_Op  PetscADMax_Op = 0;

193: void  MPIAPI PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
194: {
195:   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
196:   PetscInt    i,count = *cnt;

199:   if (*datatype != MPIU_2SCALAR) {
200:     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
201:     MPI_Abort(MPI_COMM_WORLD,1);
202:   }

204:   for (i=0; i<count; i++) {
205:     if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) {
206:       xout[2*i]   = xin[2*i];
207:       xout[2*i+1] = xin[2*i+1];
208:     }
209:   }

211:   PetscStackPop;
212:   return;
213: }

216: MPI_Op  PetscADMin_Op = 0;

221: void  MPIAPI PetscADMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
222: {
223:   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
224:   PetscInt    i,count = *cnt;

227:   if (*datatype != MPIU_2SCALAR) {
228:     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
229:     MPI_Abort(MPI_COMM_WORLD,1);
230:   }

232:   for (i=0; i<count; i++) {
233:     if (PetscRealPart(xout[2*i]) > PetscRealPart(xin[2*i])) {
234:       xout[2*i]   = xin[2*i];
235:       xout[2*i+1] = xin[2*i+1];
236:     }
237:   }

239:   PetscStackPop;
240:   return;
241: }
243: /* ---------------------------------------------------------------------------------------*/

245: #if defined(PETSC_USE_COMPLEX)
246: MPI_Op PetscSum_Op = 0;

251: void  PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
252: {
253:   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
254:   PetscInt         i,count = *cnt;

257:   if (*datatype != MPIU_SCALAR) {
258:     (*PetscErrorPrintf)("Can only handle MPIU_SCALAR data (i.e. double or complex) types");
259:     MPI_Abort(MPI_COMM_WORLD,1);
260:   }

262:   for (i=0; i<count; i++) {
263:     xout[i] += xin[i];
264:   }

266:   PetscStackPop;
267:   return;
268: }
270: #endif

272: static int  PetscGlobalArgc   = 0;
273: static char **PetscGlobalArgs = 0;

277: /*@C
278:    PetscGetArgs - Allows you to access the raw command line arguments anywhere
279:      after PetscInitialize() is called but before PetscFinalize().

281:    Not Collective

283:    Output Parameters:
284: +  argc - count of number of command line arguments
285: -  args - the command line arguments

287:    Level: intermediate

289:    Notes:
290:       This is usually used to pass the command line arguments into other libraries
291:    that are called internally deep in PETSc or the application.

293:    Concepts: command line arguments
294:    
295: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()

297: @*/
298: PetscErrorCode  PetscGetArgs(int *argc,char ***args)
299: {
301:   if (!PetscGlobalArgs) {
302:     SETERRQ(PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
303:   }
304:   *argc = PetscGlobalArgc;
305:   *args = PetscGlobalArgs;
306:   return(0);
307: }

311: /*@C
312:    PetscGetArguments - Allows you to access the  command line arguments anywhere
313:      after PetscInitialize() is called but before PetscFinalize().

315:    Not Collective

317:    Output Parameters:
318: .  args - the command line arguments

320:    Level: intermediate

322:    Notes:
323:       This does NOT start with the program name and IS null terminated (final arg is void)

325:    Concepts: command line arguments
326:    
327: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()

329: @*/
330: PetscErrorCode  PetscGetArguments(char ***args)
331: {
332:   PetscInt       i,argc = PetscGlobalArgc;

336:   if (!PetscGlobalArgs) {
337:     SETERRQ(PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
338:   }
339:   PetscMalloc(argc*sizeof(char*),args);
340:   for (i=0; i<argc-1; i++) {
341:     PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);
342:   }
343:   (*args)[argc-1] = 0;
344:   return(0);
345: }

349: /*@C
350:    PetscFreeArguments - Frees the memory obtained with PetscGetArguments()

352:    Not Collective

354:    Output Parameters:
355: .  args - the command line arguments 

357:    Level: intermediate

359:    Concepts: command line arguments
360:    
361: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()

363: @*/
364: PetscErrorCode  PetscFreeArguments(char **args)
365: {
366:   PetscInt       i = 0;

370:   while (args[i]) {
371:     PetscFree(args[i]);
372:     i++;
373:   }
374:   PetscFree(args);
375:   return(0);
376: }

380: /*@C
381:    PetscInitialize - Initializes the PETSc database and MPI. 
382:    PetscInitialize() calls MPI_Init() if that has yet to be called,
383:    so this routine should always be called near the beginning of 
384:    your program -- usually the very first line! 

386:    Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set

388:    Input Parameters:
389: +  argc - count of number of command line arguments
390: .  args - the command line arguments
391: .  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL to not check for
392:           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
393: -  help - [optional] Help message to print, use PETSC_NULL for no message

395:    If you wish PETSc to run on a subcommunicator of MPI_COMM_WORLD, create that
396:    communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize()

398:    Options Database Keys:
399: +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
400: .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
401: .  -on_error_emacs <machinename> causes emacsclient to jump to error file
402: .  -on_error_abort calls abort() when error detected (no traceback)
403: .  -on_error_mpiabort calls MPI_abort() when error detected
404: .  -error_output_stderr prints error messages to stderr instead of the default stdout
405: .  -error_output_none does not print the error messages (but handles errors in the same way as if this was not called)
406: .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
407: .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
408: .  -stop_for_debugger - Print message on how to attach debugger manually to 
409:                         process and wait (-debugger_pause) seconds for attachment
410: .  -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries)
411: .  -malloc no - Indicates not to use error-checking malloc
412: .  -malloc_debug - check for memory corruption at EVERY malloc or free
413: .  -fp_trap - Stops on floating point exceptions (Note that on the
414:               IBM RS6000 this slows code by at least a factor of 10.)
415: .  -no_signal_handler - Indicates not to trap error signals
416: .  -shared_tmp - indicates /tmp directory is shared by all processors
417: .  -not_shared_tmp - each processor has own /tmp
418: .  -tmp - alternative name of /tmp directory
419: .  -get_total_flops - returns total flops done by all processors
420: -  -memory_info - Print memory usage at end of run

422:    Options Database Keys for Profiling:
423:    See the Profiling chapter of the users manual for details.
424: +  -log_trace [filename] - Print traces of all PETSc calls
425:         to the screen (useful to determine where a program
426:         hangs without running in the debugger).  See PetscLogTraceBegin().
427: .  -info <optional filename> - Prints verbose information to the screen
428: -  -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages

430:    Environmental Variables:
431: +   PETSC_TMP - alternative tmp directory
432: .   PETSC_SHARED_TMP - tmp is shared by all processes
433: .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
434: .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
435: -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to


438:    Level: beginner

440:    Notes:
441:    If for some reason you must call MPI_Init() separately, call
442:    it before PetscInitialize().

444:    Fortran Version:
445:    In Fortran this routine has the format
446: $       call PetscInitialize(file,ierr)

448: +   ierr - error return code
449: -  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_CHARACTER_NULL to not check for
450:           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
451:            
452:    Important Fortran Note:
453:    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
454:    null character string; you CANNOT just use PETSC_NULL as 
455:    in the C version.  See the users manual for details.


458:    Concepts: initializing PETSc
459:    
460: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs()

462: @*/
463: PetscErrorCode  PetscInitialize(int *argc,char ***args,const char file[],const char help[])
464: {
466:   PetscMPIInt    flag, size;
467:   PetscInt       nodesize;
468:   PetscTruth     flg;
469:   char           hostname[256];

472:   if (PetscInitializeCalled) return(0);

474:   /* these must be initialized in a routine, not as a constant declaration*/
475:   PETSC_STDOUT = stdout;
476:   PETSC_STDERR = stderr;

478:   PetscOptionsCreate();

480:   /*
481:      We initialize the program name here (before MPI_Init()) because MPICH has a bug in 
482:      it that it sets args[0] on all processors to be args[0] on the first processor.
483:   */
484:   if (argc && *argc) {
485:     PetscSetProgramName(**args);
486:   } else {
487:     PetscSetProgramName("Unknown Name");
488:   }


491:   MPI_Initialized(&flag);
492:   if (!flag) {
493:     if (PETSC_COMM_WORLD) SETERRQ(PETSC_ERR_SUP,"You cannot set PETSC_COMM_WORLD if you have not initialized MPI first");
494:     MPI_Init(argc,args);
495:     PetscBeganMPI = PETSC_TRUE;
496:   }
497:   if (argc && args) {
498:     PetscGlobalArgc = *argc;
499:     PetscGlobalArgs = *args;
500:   }
501:   PetscInitializeCalled = PETSC_TRUE;
502:   PetscFinalizeCalled   = PETSC_FALSE;

504:   if (!PETSC_COMM_WORLD) {
505:     PETSC_COMM_WORLD = MPI_COMM_WORLD;
506:   }

508:   /* Done after init due to a bug in MPICH-GM? */
509:   PetscErrorPrintfInitialize();

511:   MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);
512:   MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);

514: #if defined(PETSC_USE_COMPLEX)
515:   /* 
516:      Initialized the global complex variable; this is because with 
517:      shared libraries the constructors for global variables
518:      are not called; at least on IRIX.
519:   */
520:   {
521: #if defined(PETSC_CLANGUAGE_CXX)
522:     PetscScalar ic(0.0,1.0);
523:     PETSC_i = ic;
524: #else
525:     PETSC_i = I;
526: #endif
527:   }

529:   MPI_Type_contiguous(2,MPIU_REAL,&MPIU_COMPLEX);
530:   MPI_Type_commit(&MPIU_COMPLEX);
531:   MPI_Op_create(PetscSum_Local,1,&PetscSum_Op);
532: #endif

534:   /*
535:      Create the PETSc MPI reduction operator that sums of the first
536:      half of the entries and maxes the second half.
537:   */
538:   MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);

540:   MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);
541:   MPI_Type_commit(&MPIU_2SCALAR);
542:   MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);
543:   MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);

545:   MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);
546:   MPI_Type_commit(&MPIU_2INT);

548:   /*
549:      Build the options database
550:   */
551:   PetscOptionsInsert(argc,args,file);

553:   /*
554:      Print main application help message
555:   */
556:   PetscOptionsHasName(PETSC_NULL,"-help",&flg);
557:   if (help && flg) {
558:     PetscPrintf(PETSC_COMM_WORLD,help);
559:   }
560:   PetscOptionsCheckInitial_Private();

562:   /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */
563: #if defined(PETSC_USE_LOG)
564:   PetscLogBegin_Private();
565: #endif

567:   /*
568:      Load the dynamic libraries (on machines that support them), this registers all
569:      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
570:   */
571:   PetscInitialize_DynamicLibraries();

573:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
574:   PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);
575:   PetscGetHostName(hostname,256);
576:   PetscInfo1(0,"Running on machine: %s\n",hostname);

578:   PetscOptionsCheckInitial_Components();
579:   /* Check the options database for options related to the options database itself */
580:   PetscOptionsSetFromOptions();

582:   PetscOptionsGetInt(PETSC_NULL,"-openmp_spawn_size",&nodesize,&flg);
583:   if (flg) {
584: #if defined(PETSC_HAVE_MPI_COMM_SPAWN)
585:     PetscOpenMPSpawn((PetscMPIInt) nodesize);
586: #else
587:     SETERRQ(PETSC_ERR_SUP,"PETSc built without MPI 2 (MPI_Comm_spawn) support, use -openmp_merge_size instead");
588: #endif
589:   } else {
590:     PetscOptionsGetInt(PETSC_NULL,"-openmp_merge_size",&nodesize,&flg);
591:     if (flg) {
592:       PetscOpenMPMerge((PetscMPIInt) nodesize);
593:     }
594:   }

596:   PetscFunctionReturn(ierr);
597: }


602: /*@C 
603:    PetscFinalize - Checks for options to be called at the conclusion
604:    of the program. MPI_Finalize() is called only if the user had not
605:    called MPI_Init() before calling PetscInitialize().

607:    Collective on PETSC_COMM_WORLD

609:    Options Database Keys:
610: +  -options_table - Calls PetscOptionsPrint()
611: .  -options_left - Prints unused options that remain in the database
612: .  -options_left no - Does not print unused options that remain in the database
613: .  -mpidump - Calls PetscMPIDump()
614: .  -malloc_dump - Calls PetscMallocDump()
615: .  -malloc_info - Prints total memory usage
616: -  -malloc_log - Prints summary of memory usage

618:    Options Database Keys for Profiling:
619:    See the Profiling chapter of the users manual for details.
620: +  -log_summary [filename] - Prints summary of flop and timing
621:         information to screen. If the filename is specified the
622:         summary is written to the file. (for code compiled with 
623:         PETSC_USE_LOG).  See PetscLogPrintSummary().
624: .  -log_all [filename] - Logs extensive profiling information
625:         (for code compiled with PETSC_USE_LOG). See PetscLogDump(). 
626: .  -log [filename] - Logs basic profiline information (for
627:         code compiled with PETSC_USE_LOG).  See PetscLogDump().
628: .  -log_sync - Log the synchronization in scatters, inner products
629:         and norms
630: -  -log_mpe [filename] - Creates a logfile viewable by the 
631:       utility Upshot/Nupshot (in MPICH distribution)

633:    Level: beginner

635:    Note:
636:    See PetscInitialize() for more general runtime options.

638: .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
639: @*/
640: PetscErrorCode  PetscFinalize(void)
641: {
643:   PetscMPIInt    rank;
644:   int            nopt;
645:   PetscTruth     flg1,flg2,flg3;
646: 

649:   if (!PetscInitializeCalled) {
650:     (*PetscErrorPrintf)("PetscInitialize() must be called before PetscFinalize()\n");
651:     return(0);
652:   }
653:   PetscOpenMPFinalize();

655:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
656:   PetscOptionsHasName(PETSC_NULL,"-malloc_info",&flg2);
657:   if (!flg2) {
658:     PetscOptionsHasName(PETSC_NULL,"-memory_info",&flg2);
659:   }
660:   if (flg2) {
661:     PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");
662:   }

664:   /* Destroy auxiliary packages */
665: #if defined(PETSC_HAVE_MATHEMATICA)
666:   PetscViewerMathematicaFinalizePackage();
667: #endif
668: #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
669: #if defined(PETSC_HAVE_SIEVE)
670:   {
671:     PetscErrorCode (*func)(void);
672:     char lib[PETSC_MAX_PATH_LEN];

674:     PetscStrcpy(lib,"${PETSC_LIB_DIR}");
675:     PetscStrcat(lib,"/libpetscdm");
676:     PetscDLLibrarySym(PETSC_COMM_WORLD,&DLLibrariesLoaded,lib,"DMFinalizePackage",(void **) &func);
677:     if (func) {
678:       (*func)();
679:     }
680:   }
681: #endif
682: #endif

684:   /*
685:      Destroy all the function registration lists created
686:   */
687:   PetscFinalize_DynamicLibraries();

689: #if defined(PETSC_USE_LOG)
690:   PetscOptionsHasName(PETSC_NULL,"-get_total_flops",&flg1);
691:   if (flg1) {
692:     PetscLogDouble flops = 0;
693:     MPI_Reduce(&_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);
694:     PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);
695:   }
696: #endif

698:   /*
699:      Free all objects registered with PetscObjectRegisterDestroy() such ast
700:     PETSC_VIEWER_XXX_().
701:   */
702:   PetscObjectRegisterDestroyAll();
703:   PetscOptionsHelpDestroyList();

705: #if defined(PETSC_USE_DEBUG)
706:   if (PetscStackActive) {
707:     PetscStackDestroy();
708:   }
709: #endif

711: #if defined(PETSC_USE_LOG)
712:   {
713:     char mname[PETSC_MAX_PATH_LEN];
714: #if defined(PETSC_HAVE_MPE)
715:     mname[0] = 0;
716:     PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);
717:     if (flg1){
718:       if (mname[0]) {PetscLogMPEDump(mname);}
719:       else          {PetscLogMPEDump(0);}
720:     }
721: #endif
722:     mname[0] = 0;
723:     PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);
724:     if (flg1) {
725:       if (mname[0])  {PetscLogPrintSummary(PETSC_COMM_WORLD,mname);}
726:       else           {PetscLogPrintSummary(PETSC_COMM_WORLD,0);}
727:     }

729:     PetscOptionsGetString(PETSC_NULL,"-log_detailed",mname,PETSC_MAX_PATH_LEN,&flg1);
730:     if (flg1) {
731:       if (mname[0])  {PetscLogPrintDetailed(PETSC_COMM_WORLD,mname);}
732:       else           {PetscLogPrintDetailed(PETSC_COMM_WORLD,0);}
733:     }

735:     mname[0] = 0;
736:     PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);
737:     PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);
738:     if (flg1 || flg2){
739:       if (mname[0]) PetscLogDump(mname);
740:       else          PetscLogDump(0);
741:     }
742:     PetscLogDestroy();
743:   }
744: #endif
745:   PetscOptionsHasName(PETSC_NULL,"-no_signal_handler",&flg1);
746:   if (!flg1) { PetscPopSignalHandler();}
747:   PetscOptionsHasName(PETSC_NULL,"-mpidump",&flg1);
748:   if (flg1) {
749:     PetscMPIDump(stdout);
750:   }
751:   PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);
752:   PetscOptionsHasName(PETSC_NULL,"-options_table",&flg2);
753:   if (flg2) {
754:     if (!rank) {PetscOptionsPrint(stdout);}
755:   }

757:   /* to prevent PETSc -options_left from warning */
758:   PetscOptionsHasName(PETSC_NULL,"-nox",&flg1);CHKERRQ(ierr)
759:   PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr)

761:   flg3 = PETSC_FALSE; /* default value is required */
762:   PetscOptionsGetTruth(PETSC_NULL,"-options_left",&flg3,&flg1);
763:   PetscOptionsAllUsed(&nopt);
764:   if (flg3) {
765:     if (!flg2) { /* have not yet printed the options */
766:       PetscOptionsPrint(stdout);
767:     }
768:     if (!nopt) {
769:       PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");
770:     } else if (nopt == 1) {
771:       PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");
772:     } else {
773:       PetscPrintf(PETSC_COMM_WORLD,"There are %d unused database options. They are:\n",nopt);
774:     }
775:   }
776: #if defined(PETSC_USE_DEBUG)
777:   if (nopt && !flg3 && !flg1) {
778:     PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");
779:     PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");
780:     PetscOptionsLeft();
781:   } else if (nopt && flg3) {
782: #else 
783:   if (nopt && flg3) {
784: #endif
785:     PetscOptionsLeft();
786:   }

788:   PetscOptionsHasName(PETSC_NULL,"-log_history",&flg1);
789:   if (flg1) {
790:     PetscLogCloseHistoryFile(&petsc_history);
791:     petsc_history = 0;
792:   }

794:   PetscInfoAllow(PETSC_FALSE,PETSC_NULL);

796:   /*
797:        Free all the registered create functions, such as KSPList, VecList, SNESList, etc
798:   */
799:   PetscFListDestroyAll();

801:   PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);
802:   PetscOptionsHasName(PETSC_NULL,"-malloc_log",&flg3);
803:   if (flg1) {
804:     char fname[PETSC_MAX_PATH_LEN];
805:     FILE *fd;
806: 
807:     fname[0] = 0;
808:     PetscOptionsGetString(PETSC_NULL,"-malloc_dump",fname,250,&flg1);
809:     if (flg1 && fname[0]) {
810:       char sname[PETSC_MAX_PATH_LEN];

812:       sprintf(sname,"%s_%d",fname,rank);
813:       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
814:       PetscMallocDump(fd);
815:       fclose(fd);
816:     } else {
817:       MPI_Comm local_comm;

819:       MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);
820:       PetscSequentialPhaseBegin_Private(local_comm,1);
821:         PetscMallocDump(stdout);
822:       PetscSequentialPhaseEnd_Private(local_comm,1);
823:       MPI_Comm_free(&local_comm);
824:     }
825:   }
826:   if (flg3) {
827:     char fname[PETSC_MAX_PATH_LEN];
828:     FILE *fd;
829: 
830:     fname[0] = 0;
831:     PetscOptionsGetString(PETSC_NULL,"-malloc_log",fname,250,&flg1);
832:     if (flg1 && fname[0]) {
833:       char sname[PETSC_MAX_PATH_LEN];

835:       sprintf(sname,"%s_%d",fname,rank);
836:       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
837:       PetscMallocDumpLog(fd);
838:       fclose(fd);
839:     } else {
840:       PetscMallocDumpLog(stdout);
841:     }
842:   }
843:   /* Can be destroyed only after all the options are used */
844:   PetscOptionsDestroy();

846:   PetscGlobalArgc = 0;
847:   PetscGlobalArgs = 0;

849: #if defined(PETSC_USE_COMPLEX)
850:   MPI_Op_free(&PetscSum_Op);
851:   MPI_Type_free(&MPIU_COMPLEX);
852: #endif
853:   MPI_Type_free(&MPIU_2SCALAR);
854:   MPI_Type_free(&MPIU_2INT);
855:   MPI_Op_free(&PetscMaxSum_Op);
856:   MPI_Op_free(&PetscADMax_Op);
857:   MPI_Op_free(&PetscADMin_Op);

859:   PetscInfo(0,"PETSc successfully ended!\n");
860:   if (PetscBeganMPI) {
861:     MPI_Finalize();
862:   }

864: /*

866:      Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because 
867:    the communicator has some outstanding requests on it. Specifically if the 
868:    flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See 
869:    src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate()
870:    is never freed as it should be. Thus one may obtain messages of the form
871:    [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the
872:    memory was not freed.

874: */
875:   PetscClearMalloc();
876:   PetscInitializeCalled = PETSC_FALSE;
877:   PetscFinalizeCalled   = PETSC_TRUE;
878:   PetscFunctionReturn(ierr);
879: }

881: /*
882:      These may be used in code that ADIC is to be used on
883: */

887: /*@C
888:       PetscGlobalMax - Computes the maximum value over several processors

890:      Collective on MPI_Comm

892:    Input Parameters:
893: +   local - the local value
894: -   comm - the processors that find the maximum

896:    Output Parameter:
897: .   result - the maximum value
898:   
899:    Level: intermediate

901:    Notes:
902:      These functions are to be used inside user functions that are to be processed with 
903:    ADIC. PETSc will automatically provide differentiated versions of these functions

905: .seealso: PetscGlobalMin(), PetscGlobalSum()
906: @*/
907: PetscErrorCode  PetscGlobalMax(PetscReal* local,PetscReal* result,MPI_Comm comm)
908: {
909:   return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MAX,comm);
910: }

914: /*@C
915:       PetscGlobalMin - Computes the minimum value over several processors

917:      Collective on MPI_Comm

919:    Input Parameters:
920: +   local - the local value
921: -   comm - the processors that find the minimum

923:    Output Parameter:
924: .   result - the minimum value
925:   
926:    Level: intermediate

928:    Notes:
929:      These functions are to be used inside user functions that are to be processed with 
930:    ADIC. PETSc will automatically provide differentiated versions of these functions

932: .seealso: PetscGlobalMax(), PetscGlobalSum()
933: @*/
934: PetscErrorCode  PetscGlobalMin(PetscReal* local,PetscReal* result,MPI_Comm comm)
935: {
936:   return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MIN,comm);
937: }

941: /*@C
942:       PetscGlobalSum - Computes the sum over sever processors

944:      Collective on MPI_Comm

946:    Input Parameters:
947: +   local - the local value
948: -   comm - the processors that find the sum

950:    Output Parameter:
951: .   result - the sum
952:   
953:    Level: intermediate

955:    Notes:
956:      These functions are to be used inside user functions that are to be processed with 
957:    ADIC. PETSc will automatically provide differentiated versions of these functions

959: .seealso: PetscGlobalMin(), PetscGlobalMax()
960: @*/
961: PetscErrorCode  PetscGlobalSum(PetscScalar* local,PetscScalar* result,MPI_Comm comm)
962: {
963:   return MPI_Allreduce(local,result,1,MPIU_SCALAR,PetscSum_Op,comm);
964: }