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

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

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

 30: /*
 31:      Declare and set all the string names of the PETSc enums
 32: */
 33: const char *PetscTruths[]    = {"FALSE","TRUE","PetscTruth","PETSC_",0};
 34: const char *PetscDataTypes[] = {"INT", "DOUBLE", "COMPLEX",
 35:                                 "LONG","SHORT",  "FLOAT",
 36:                                 "CHAR","LOGICAL","ENUM","TRUTH","LONGDOUBLE","PetscDataType","PETSC_",0};

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

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

 44: PetscErrorCode PETSC_DLLEXPORT PetscCookieRegister(PetscCookie *cookie)
 45: {
 46:   if (*cookie == PETSC_DECIDE || *cookie == PETSC_NULL) {
 47:     *cookie = ++PETSC_LARGEST_COOKIE;
 48:   } else if (*cookie > 0) {
 49:     /* Need to check here for montonicity and insert if necessary */
 50:     return 0;
 51:   } else {
 52:     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Invalid suggested cookie %d", (int)*cookie);
 53:   }
 54:   return 0;
 55: }

 57: /*
 58:        Checks the options database for initializations related to the 
 59:     PETSc components
 60: */
 63: PetscErrorCode PETSC_DLLEXPORT PetscOptionsCheckInitial_Components(void)
 64: {
 65:   PetscTruth flg1;

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

 84: /*@C
 85:       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
 86:         the command line arguments.

 88:    Collective
 89:   
 90:    Level: advanced

 92: .seealso: PetscInitialize(), PetscInitializeFortran()
 93: @*/
 94: PetscErrorCode PETSC_DLLEXPORT PetscInitializeNoArguments(void)
 95: {
 97:   int            argc = 0;
 98:   char           **args = 0;

101:   PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL);
102:   PetscFunctionReturn(ierr);
103: }

107: /*@
108:       PetscInitialized - Determine whether PETSc is initialized.
109:   
110:    Level: beginner

112: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
113: @*/
114: PetscErrorCode PETSC_DLLEXPORT PetscInitialized(PetscTruth *isInitialized)
115: {
118:   *isInitialized = PetscInitializeCalled;
119:   return(0);
120: }

124: /*@
125:       PetscFinalized - Determine whether PetscFinalize() has been called yet
126:   
127:    Level: developer

129: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
130: @*/
131: PetscErrorCode PETSC_DLLEXPORT PetscFinalized(PetscTruth *isFinalized)
132: {
135:   *isFinalized = PetscFinalizeCalled;
136:   return(0);
137: }

139: EXTERN PetscErrorCode        PetscOptionsCheckInitial_Private(void);

142: /*
143:        This function is the MPI reduction operation used to compute the sum of the 
144:    first half of the datatype and the max of the second half.
145: */
146: MPI_Op PetscMaxSum_Op = 0;

151: void PETSC_DLLEXPORT PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
152: {
153:   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;

156:   if (*datatype != MPIU_2INT) {
157:     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
158:     MPI_Abort(MPI_COMM_WORLD,1);
159:   }

161:   for (i=0; i<count; i++) {
162:     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
163:     xout[2*i+1] += xin[2*i+1];
164:   }
165:   PetscStackPop;
166:   return;
167: }

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

183:   MPI_Comm_size(comm,&size);
184:   MPI_Comm_rank(comm,&rank);
185:   PetscMalloc(2*size*sizeof(PetscInt),&work);
186:   MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);
187:   *max   = work[2*rank];
188:   *sum   = work[2*rank+1];
189:   PetscFree(work);
190:   return(0);
191: }

193: /* ----------------------------------------------------------------------------*/
194: MPI_Op PETSC_DLLEXPORT PetscADMax_Op = 0;

199: void PETSC_DLLEXPORT PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
200: {
201:   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
202:   PetscInt    i,count = *cnt;

205:   if (*datatype != MPIU_2SCALAR) {
206:     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
207:     MPI_Abort(MPI_COMM_WORLD,1);
208:   }

210:   for (i=0; i<count; i++) {
211:     if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) {
212:       xout[2*i]   = xin[2*i];
213:       xout[2*i+1] = xin[2*i+1];
214:     }
215:   }

217:   PetscStackPop;
218:   return;
219: }

222: MPI_Op PETSC_DLLEXPORT PetscADMin_Op = 0;

227: void PETSC_DLLEXPORT PetscADMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
228: {
229:   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
230:   PetscInt    i,count = *cnt;

233:   if (*datatype != MPIU_2SCALAR) {
234:     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
235:     MPI_Abort(MPI_COMM_WORLD,1);
236:   }

238:   for (i=0; i<count; i++) {
239:     if (PetscRealPart(xout[2*i]) > PetscRealPart(xin[2*i])) {
240:       xout[2*i]   = xin[2*i];
241:       xout[2*i+1] = xin[2*i+1];
242:     }
243:   }

245:   PetscStackPop;
246:   return;
247: }
249: /* ---------------------------------------------------------------------------------------*/

251: #if defined(PETSC_USE_COMPLEX)
252: MPI_Op PetscSum_Op = 0;

257: void PETSC_DLLEXPORT PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
258: {
259:   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
260:   PetscInt         i,count = *cnt;

263:   if (*datatype != MPIU_SCALAR) {
264:     (*PetscErrorPrintf)("Can only handle MPIU_SCALAR data (i.e. double or complex) types");
265:     MPI_Abort(MPI_COMM_WORLD,1);
266:   }

268:   for (i=0; i<count; i++) {
269:     xout[i] += xin[i];
270:   }

272:   PetscStackPop;
273:   return;
274: }
276: #endif

278: static int  PetscGlobalArgc   = 0;
279: static char **PetscGlobalArgs = 0;

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

287:    Not Collective

289:    Output Parameters:
290: +  argc - count of number of command line arguments
291: -  args - the command line arguments

293:    Level: intermediate

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

299:    Concepts: command line arguments
300:    
301: .seealso: PetscFinalize(), PetscInitializeFortran()

303: @*/
304: PetscErrorCode PETSC_DLLEXPORT PetscGetArgs(int *argc,char ***args)
305: {
307:   if (!PetscGlobalArgs) {
308:     SETERRQ(PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
309:   }
310:   *argc = PetscGlobalArgc;
311:   *args = PetscGlobalArgs;
312:   return(0);
313: }

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

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

325:    Input Parameters:
326: +  argc - count of number of command line arguments
327: .  args - the command line arguments
328: .  file - [optional] PETSc database file, defaults to ~username/.petscrc
329:           (use PETSC_NULL for default)
330: -  help - [optional] Help message to print, use PETSC_NULL for no message

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

335:    Options Database Keys:
336: +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
337: .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
338: .  -on_error_emacs <machinename> causes emacsclient to jump to error file
339: .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
340: .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
341: .  -stop_for_debugger - Print message on how to attach debugger manually to 
342:                         process and wait (-debugger_pause) seconds for attachment
343: .  -malloc - Indicates use of PETSc error-checking malloc
344: .  -malloc no - Indicates not to use error-checking malloc
345: .  -fp_trap - Stops on floating point exceptions (Note that on the
346:               IBM RS6000 this slows code by at least a factor of 10.)
347: .  -no_signal_handler - Indicates not to trap error signals
348: .  -shared_tmp - indicates /tmp directory is shared by all processors
349: .  -not_shared_tmp - each processor has own /tmp
350: .  -tmp - alternative name of /tmp directory
351: .  -get_total_flops - returns total flops done by all processors
352: -  -memory_info - Print memory usage at end of run

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

362:    Environmental Variables:
363: +   PETSC_TMP - alternative tmp directory
364: .   PETSC_SHARED_TMP - tmp is shared by all processes
365: .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
366: .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
367: -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to


370:    Level: beginner

372:    Notes:
373:    If for some reason you must call MPI_Init() separately, call
374:    it before PetscInitialize().

376:    Fortran Version:
377:    In Fortran this routine has the format
378: $       call PetscInitialize(file,ierr)

380: +   ierr - error return code
381: -   file - [optional] PETSc database file name, defaults to 
382:            ~username/.petscrc (use PETSC_NULL_CHARACTER for default)
383:            
384:    Important Fortran Note:
385:    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
386:    null character string; you CANNOT just use PETSC_NULL as 
387:    in the C version.  See the users manual for details.


390:    Concepts: initializing PETSc
391:    
392: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs()

394: @*/
395: PetscErrorCode PETSC_DLLEXPORT PetscInitialize(int *argc,char ***args,const char file[],const char help[])
396: {
398:   PetscMPIInt    flag, size;
399:   PetscTruth     flg;
400:   char           hostname[256];

403:   if (PetscInitializeCalled) return(0);

405:   /* this must be initialized in a routine, not as a constant declaration*/
406:   PETSC_STDOUT = stdout;

408:   PetscOptionsCreate();

410:   /*
411:      We initialize the program name here (before MPI_Init()) because MPICH has a bug in 
412:      it that it sets args[0] on all processors to be args[0] on the first processor.
413:   */
414:   if (argc && *argc) {
415:     PetscSetProgramName(**args);
416:   } else {
417:     PetscSetProgramName("Unknown Name");
418:   }


421:   MPI_Initialized(&flag);
422:   if (!flag) {
423:     if (PETSC_COMM_WORLD) SETERRQ(PETSC_ERR_SUP,"You cannot set PETSC_COMM_WORLD if you have not initialized MPI first");
424:     MPI_Init(argc,args);
425:     PetscBeganMPI = PETSC_TRUE;
426:   }
427:   if (argc && args) {
428:     PetscGlobalArgc = *argc;
429:     PetscGlobalArgs = *args;
430:   }
431:   PetscInitializeCalled = PETSC_TRUE;
432:   PetscFinalizeCalled   = PETSC_FALSE;

434:   if (!PETSC_COMM_WORLD) {
435:     PETSC_COMM_WORLD = MPI_COMM_WORLD;
436:   }

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

441:   MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);
442:   MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);

444: #if defined(PETSC_USE_COMPLEX)
445:   /* 
446:      Initialized the global complex variable; this is because with 
447:      shared libraries the constructors for global variables
448:      are not called; at least on IRIX.
449:   */
450:   {
451:     PetscScalar ic(0.0,1.0);
452:     PETSC_i = ic;
453:   }
454:   MPI_Type_contiguous(2,MPIU_REAL,&MPIU_COMPLEX);
455:   MPI_Type_commit(&MPIU_COMPLEX);
456:   MPI_Op_create(PetscSum_Local,1,&PetscSum_Op);
457: #endif

459:   /*
460:      Create the PETSc MPI reduction operator that sums of the first
461:      half of the entries and maxes the second half.
462:   */
463:   MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);

465:   MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);
466:   MPI_Type_commit(&MPIU_2SCALAR);
467:   MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);
468:   MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);

470:   MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);
471:   MPI_Type_commit(&MPIU_2INT);

473:   /*
474:      Build the options database and check for user setup requests
475:   */
476:   PetscOptionsInsert(argc,args,file);

478:   /*
479:      Print main application help message
480:   */
481:   PetscOptionsHasName(PETSC_NULL,"-help",&flg);
482:   if (help && flg) {
483:     PetscPrintf(PETSC_COMM_WORLD,help);
484:   }
485:   PetscOptionsCheckInitial_Private();

487:   /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */
488: #if defined(PETSC_USE_LOG)
489:   PetscLogBegin_Private();
490: #endif

492:   /*
493:      Load the dynamic libraries (on machines that support them), this registers all
494:      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
495:   */
496:   PetscInitialize_DynamicLibraries();

498:   /*
499:      Initialize all the default viewers
500:   */
501:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
502:   PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);
503:   PetscGetHostName(hostname,256);
504:   PetscInfo1(0,"Running on machine: %s\n",hostname);

506:   PetscOptionsCheckInitial_Components();

508:   PetscFunctionReturn(ierr);
509: }


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

519:    Collective on PETSC_COMM_WORLD

521:    Options Database Keys:
522: +  -options_table - Calls PetscOptionsPrint()
523: .  -options_left - Prints unused options that remain in the database
524: .  -options_left no - Does not print unused options that remain in the database
525: .  -mpidump - Calls PetscMPIDump()
526: .  -malloc_dump - Calls PetscMallocDump()
527: .  -malloc_info - Prints total memory usage
528: .  -malloc_debug - Calls PetscMallocDebug(), checks allocated memory for corruption while running
529: -  -malloc_log - Prints summary of memory usage

531:    Options Database Keys for Profiling:
532:    See the Profiling chapter of the users manual for details.
533: +  -log_summary [filename] - Prints summary of flop and timing
534:         information to screen. If the filename is specified the
535:         summary is written to the file. (for code compiled with 
536:         PETSC_USE_LOG).  See PetscLogPrintSummary().
537: .  -log_all [filename] - Logs extensive profiling information
538:         (for code compiled with PETSC_USE_LOG). See PetscLogDump(). 
539: .  -log [filename] - Logs basic profiline information (for
540:         code compiled with PETSC_USE_LOG).  See PetscLogDump().
541: .  -log_sync - Log the synchronization in scatters, inner products
542:         and norms
543: -  -log_mpe [filename] - Creates a logfile viewable by the 
544:       utility Upshot/Nupshot (in MPICH distribution)

546:    Level: beginner

548:    Note:
549:    See PetscInitialize() for more general runtime options.

551: .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
552: @*/
553: PetscErrorCode PETSC_DLLEXPORT PetscFinalize(void)
554: {
556:   PetscMPIInt    rank;
557:   int            nopt;
558:   PetscTruth     flg1,flg2,flg3;
559: 

562:   if (!PetscInitializeCalled) {
563:     (*PetscErrorPrintf)("PetscInitialize() must be called before PetscFinalize()\n");
564:     return(0);
565:   }

567:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
568:   PetscOptionsHasName(PETSC_NULL,"-malloc_info",&flg2);
569:   if (!flg2) {
570:     PetscOptionsHasName(PETSC_NULL,"-memory_info",&flg2);
571:   }
572:   if (flg2) {
573:     PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");
574:   }

576:   /* Destroy auxiliary packages */
577: #if defined(PETSC_HAVE_MATHEMATICA)
578:   PetscViewerMathematicaFinalizePackage();
579: #endif

581:   /*
582:      Destroy all the function registration lists created
583:   */
584:   PetscFinalize_DynamicLibraries();

586: #if defined(PETSC_USE_LOG)
587:   PetscOptionsHasName(PETSC_NULL,"-get_total_flops",&flg1);
588:   if (flg1) {
589:     PetscLogDouble flops = 0;
590:     MPI_Reduce(&_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);
591:     PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);
592:   }
593: #endif

595:   /*
596:      Free all objects registered with PetscObjectRegisterDestroy() such ast
597:     PETSC_VIEWER_XXX_().
598:   */
599:   PetscObjectRegisterDestroyAll();

601: #if defined(PETSC_USE_DEBUG)
602:   if (PetscStackActive) {
603:     PetscStackDestroy();
604:   }
605: #endif

607: #if defined(PETSC_USE_LOG)
608:   {
609:     char mname[PETSC_MAX_PATH_LEN];
610: #if defined(PETSC_HAVE_MPE)
611:     mname[0] = 0;
612:     PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);
613:     if (flg1){
614:       if (mname[0]) {PetscLogMPEDump(mname);}
615:       else          {PetscLogMPEDump(0);}
616:     }
617: #endif
618:     mname[0] = 0;
619:     PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);
620:     if (flg1) {
621:       if (mname[0])  {PetscLogPrintSummary(PETSC_COMM_WORLD,mname);}
622:       else           {PetscLogPrintSummary(PETSC_COMM_WORLD,0);}
623:     }

625:     mname[0] = 0;
626:     PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);
627:     PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);
628:     if (flg1 || flg2){
629:       if (mname[0]) PetscLogDump(mname);
630:       else          PetscLogDump(0);
631:     }
632:     PetscLogDestroy();
633:   }
634: #endif
635:   PetscOptionsHasName(PETSC_NULL,"-no_signal_handler",&flg1);
636:   if (!flg1) { PetscPopSignalHandler();}
637:   PetscOptionsHasName(PETSC_NULL,"-mpidump",&flg1);
638:   if (flg1) {
639:     PetscMPIDump(stdout);
640:   }
641:   PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);
642:   PetscOptionsHasName(PETSC_NULL,"-options_table",&flg2);
643:   if (flg2) {
644:     if (!rank) {PetscOptionsPrint(stdout);}
645:   }

647:   /* to prevent PETSc -options_left from warning */
648:   PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr)
649:   PetscOptionsHasName(PETSC_NULL,"-error_output_stderr",&flg1);

651:   flg3 = PETSC_FALSE;
652:   PetscOptionsGetTruth(PETSC_NULL,"-options_left",&flg3,PETSC_NULL);
653:   PetscOptionsAllUsed(&nopt);
654:   if (flg3) {
655:     if (!flg2) { /* have not yet printed the options */
656:       PetscOptionsPrint(stdout);
657:     }
658:     if (!nopt) {
659:       PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");
660:     } else if (nopt == 1) {
661:       PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");
662:     } else {
663:       PetscPrintf(PETSC_COMM_WORLD,"There are %d unused database options. They are:\n",nopt);
664:     }
665:   }
666: #if defined(PETSC_USE_DEBUG)
667:   if (nopt && !flg3) {
668:     PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");
669:     PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");
670:     PetscOptionsLeft();
671:   } else if (nopt && flg3) {
672: #else 
673:   if (nopt && flg3) {
674: #endif
675:     PetscOptionsLeft();
676:   }

678:   PetscOptionsHasName(PETSC_NULL,"-log_history",&flg1);
679:   if (flg1) {
680:     PetscLogCloseHistoryFile(&petsc_history);
681:     petsc_history = 0;
682:   }

684:   PetscInfoAllow(PETSC_FALSE,PETSC_NULL);

686:   /*
687:        Free all the registered create functions, such as KSPList, VecList, SNESList, etc
688:   */
689:   PetscFListDestroyAll();

691:   PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);
692:   PetscOptionsHasName(PETSC_NULL,"-malloc_log",&flg3);
693:   if (flg1) {
694:     char fname[PETSC_MAX_PATH_LEN];
695:     FILE *fd;
696: 
697:     fname[0] = 0;
698:     PetscOptionsGetString(PETSC_NULL,"-malloc_dump",fname,250,&flg1);
699:     if (flg1 && fname[0]) {
700:       char sname[PETSC_MAX_PATH_LEN];

702:       sprintf(sname,"%s_%d",fname,rank);
703:       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
704:       PetscMallocDump(fd);
705:       fclose(fd);
706:     } else {
707:       MPI_Comm local_comm;

709:       MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);
710:       PetscSequentialPhaseBegin_Private(local_comm,1);
711:         PetscMallocDump(stdout);
712:       PetscSequentialPhaseEnd_Private(local_comm,1);
713:       MPI_Comm_free(&local_comm);
714:     }
715:   }
716:   if (flg3) {
717:     char fname[PETSC_MAX_PATH_LEN];
718:     FILE *fd;
719: 
720:     fname[0] = 0;
721:     PetscOptionsGetString(PETSC_NULL,"-malloc_log",fname,250,&flg1);
722:     if (flg1 && fname[0]) {
723:       char sname[PETSC_MAX_PATH_LEN];

725:       sprintf(sname,"%s_%d",fname,rank);
726:       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
727:       PetscMallocDumpLog(fd);
728:       fclose(fd);
729:     } else {
730:       PetscMallocDumpLog(stdout);
731:     }
732:   }
733:   /* Can be destroyed only after all the options are used */
734:   PetscOptionsDestroy();

736:   PetscGlobalArgc = 0;
737:   PetscGlobalArgs = 0;

739: #if defined(PETSC_USE_COMPLEX)
740:   MPI_Op_free(&PetscSum_Op);
741:   MPI_Type_free(&MPIU_COMPLEX);
742: #endif
743:   MPI_Type_free(&MPIU_2SCALAR);
744:   MPI_Type_free(&MPIU_2INT);
745:   MPI_Op_free(&PetscMaxSum_Op);
746:   MPI_Op_free(&PetscADMax_Op);
747:   MPI_Op_free(&PetscADMin_Op);

749:   PetscInfo(0,"PETSc successfully ended!\n");
750:   if (PetscBeganMPI) {
751:     MPI_Finalize();
752:   }

754: /*

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

764: */
765:   PetscClearMalloc();
766:   PetscInitializeCalled = PETSC_FALSE;
767:   PetscFinalizeCalled   = PETSC_TRUE;
768:   PetscFunctionReturn(ierr);
769: }

771: /*
772:      These may be used in code that ADIC is to be used on
773: */

777: /*@C
778:       PetscGlobalMax - Computes the maximum value over several processors

780:      Collective on MPI_Comm

782:    Input Parameters:
783: +   local - the local value
784: -   comm - the processors that find the maximum

786:    Output Parameter:
787: .   result - the maximum value
788:   
789:    Level: intermediate

791:    Notes:
792:      These functions are to be used inside user functions that are to be processed with 
793:    ADIC. PETSc will automatically provide differentiated versions of these functions

795: .seealso: PetscGlobalMin(), PetscGlobalSum()
796: @*/
797: PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal* local,PetscReal* result,MPI_Comm comm)
798: {
799:   return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MAX,comm);
800: }

804: /*@C
805:       PetscGlobalMin - Computes the minimum value over several processors

807:      Collective on MPI_Comm

809:    Input Parameters:
810: +   local - the local value
811: -   comm - the processors that find the minimum

813:    Output Parameter:
814: .   result - the minimum value
815:   
816:    Level: intermediate

818:    Notes:
819:      These functions are to be used inside user functions that are to be processed with 
820:    ADIC. PETSc will automatically provide differentiated versions of these functions

822: .seealso: PetscGlobalMax(), PetscGlobalSum()
823: @*/
824: PetscErrorCode PETSC_DLLEXPORT PetscGlobalMin(PetscReal* local,PetscReal* result,MPI_Comm comm)
825: {
826:   return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MIN,comm);
827: }

831: /*@C
832:       PetscGlobalSum - Computes the sum over sever processors

834:      Collective on MPI_Comm

836:    Input Parameters:
837: +   local - the local value
838: -   comm - the processors that find the sum

840:    Output Parameter:
841: .   result - the sum
842:   
843:    Level: intermediate

845:    Notes:
846:      These functions are to be used inside user functions that are to be processed with 
847:    ADIC. PETSc will automatically provide differentiated versions of these functions

849: .seealso: PetscGlobalMin(), PetscGlobalMax()
850: @*/
851: PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar* local,PetscScalar* result,MPI_Comm comm)
852: {
853:   return MPI_Allreduce(local,result,1,MPIU_SCALAR,PetscSum_Op,comm);
854: }