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: EXTERN PetscErrorCode PetscLogBegin_Private(void);

 11: /* -----------------------------------------------------------------------------------------*/


 15: EXTERN PetscErrorCode PetscInitialize_DynamicLibraries(void);
 16: EXTERN PetscErrorCode PetscFinalize_DynamicLibraries(void);
 17: EXTERN PetscErrorCode PetscFListDestroyAll(void);
 18: EXTERN PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int);
 19: EXTERN PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int);
 20: EXTERN PetscErrorCode PetscLogCloseHistoryFile(FILE **);

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

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

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

 36: /*
 37:        Checks the options database for initializations related to the 
 38:     PETSc components
 39: */
 42: PetscErrorCode PETSC_DLLEXPORT PetscOptionsCheckInitial_Components(void)
 43: {
 44:   MPI_Comm   comm = PETSC_COMM_WORLD;
 45:   PetscTruth flg1;

 49:   PetscOptionsHasName(PETSC_NULL,"-help",&flg1);
 50:   if (flg1) {
 51: #if defined (PETSC_USE_LOG)
 52:     (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");
 53:     (*PetscHelpPrintf)(comm," -log_summary_exclude: <vec,mat,pc.ksp,snes>\n");
 54:     (*PetscHelpPrintf)(comm," -log_info_exclude: <null,vec,mat,pc,ksp,snes,ts>\n");
 55:     (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
 56: #endif
 57:   }
 58:   return(0);
 59: }

 63: /*@C
 64:       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
 65:         the command line arguments.

 67:    Collective
 68:   
 69:    Level: advanced

 71: .seealso: PetscInitialize(), PetscInitializeFortran()
 72: @*/
 73: PetscErrorCode PETSC_DLLEXPORT PetscInitializeNoArguments(void)
 74: {
 76:   int            argc = 0;
 77:   char           **args = 0;

 80:   PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL);
 81:   PetscFunctionReturn(ierr);
 82: }

 86: /*@
 87:       PetscInitialized - Determine whether PETSc is initialized.
 88:   
 89:    Level: beginner

 91: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
 92: @*/
 93: PetscErrorCode PETSC_DLLEXPORT PetscInitialized(PetscTruth *isInitialized)
 94: {
 97:   *isInitialized = PetscInitializeCalled;
 98:   return(0);
 99: }

103: /*@
104:       PetscFinalized - Determine whether PetscFinalize() has been called yet
105:   
106:    Level: developer

108: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
109: @*/
110: PetscErrorCode PETSC_DLLEXPORT PetscFinalized(PetscTruth *isFinalized)
111: {
114:   *isFinalized = PetscFinalizeCalled;
115:   return(0);
116: }

118: EXTERN PetscErrorCode        PetscOptionsCheckInitial_Private(void);

121: /*
122:        This function is the MPI reduction operation used to compute the sum of the 
123:    first half of the datatype and the max of the second half.
124: */
125: MPI_Op PetscMaxSum_Op = 0;

130: void PETSC_DLLEXPORT PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
131: {
132:   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;

135:   if (*datatype != MPIU_2INT) {
136:     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
137:     MPI_Abort(MPI_COMM_WORLD,1);
138:   }

140:   for (i=0; i<count; i++) {
141:     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
142:     xout[2*i+1] += xin[2*i+1];
143:   }
144:   PetscStackPop;
145:   return;
146: }

149: /*
150:     Returns the max of the first entry owned by this processor and the
151: sum of the second entry.
152: */
155: PetscErrorCode PETSC_DLLEXPORT PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum)
156: {
157:   PetscMPIInt    size,rank;
158:   PetscInt       *work;

162:   MPI_Comm_size(comm,&size);
163:   MPI_Comm_rank(comm,&rank);
164:   PetscMalloc(2*size*sizeof(PetscInt),&work);
165:   MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);
166:   *max   = work[2*rank];
167:   *sum   = work[2*rank+1];
168:   PetscFree(work);
169:   return(0);
170: }

172: /* ----------------------------------------------------------------------------*/
173: MPI_Op PETSC_DLLEXPORT PetscADMax_Op = 0;

178: void PETSC_DLLEXPORT PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
179: {
180:   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
181:   PetscInt    i,count = *cnt;

184:   if (*datatype != MPIU_2SCALAR) {
185:     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
186:     MPI_Abort(MPI_COMM_WORLD,1);
187:   }

189:   for (i=0; i<count; i++) {
190:     if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) {
191:       xout[2*i]   = xin[2*i];
192:       xout[2*i+1] = xin[2*i+1];
193:     }
194:   }

196:   PetscStackPop;
197:   return;
198: }

201: MPI_Op PETSC_DLLEXPORT PetscADMin_Op = 0;

206: void PETSC_DLLEXPORT PetscADMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
207: {
208:   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
209:   PetscInt    i,count = *cnt;

212:   if (*datatype != MPIU_2SCALAR) {
213:     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
214:     MPI_Abort(MPI_COMM_WORLD,1);
215:   }

217:   for (i=0; i<count; i++) {
218:     if (PetscRealPart(xout[2*i]) > PetscRealPart(xin[2*i])) {
219:       xout[2*i]   = xin[2*i];
220:       xout[2*i+1] = xin[2*i+1];
221:     }
222:   }

224:   PetscStackPop;
225:   return;
226: }
228: /* ---------------------------------------------------------------------------------------*/

230: #if defined(PETSC_USE_COMPLEX)
231: MPI_Op PetscSum_Op = 0;

236: void PETSC_DLLEXPORT PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
237: {
238:   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
239:   PetscInt         i,count = *cnt;

242:   if (*datatype != MPIU_SCALAR) {
243:     (*PetscErrorPrintf)("Can only handle MPIU_SCALAR data (i.e. double or complex) types");
244:     MPI_Abort(MPI_COMM_WORLD,1);
245:   }

247:   for (i=0; i<count; i++) {
248:     xout[i] += xin[i];
249:   }

251:   PetscStackPop;
252:   return;
253: }
255: #endif

257: static int  PetscGlobalArgc   = 0;
258: static char **PetscGlobalArgs = 0;

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

266:    Not Collective

268:    Output Parameters:
269: +  argc - count of number of command line arguments
270: -  args - the command line arguments

272:    Level: intermediate

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

278:    Concepts: command line arguments
279:    
280: .seealso: PetscFinalize(), PetscInitializeFortran()

282: @*/
283: PetscErrorCode PETSC_DLLEXPORT PetscGetArgs(int *argc,char ***args)
284: {
286:   if (!PetscGlobalArgs) {
287:     SETERRQ(PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
288:   }
289:   *argc = PetscGlobalArgc;
290:   *args = PetscGlobalArgs;
291:   return(0);
292: }

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

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

304:    Input Parameters:
305: +  argc - count of number of command line arguments
306: .  args - the command line arguments
307: .  file - [optional] PETSc database file, defaults to ~username/.petscrc
308:           (use PETSC_NULL for default)
309: -  help - [optional] Help message to print, use PETSC_NULL for no message

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

314:    Options Database Keys:
315: +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
316: .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
317: .  -on_error_emacs <machinename> causes emacsclient to jump to error file
318: .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
319: .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
320: .  -stop_for_debugger - Print message on how to attach debugger manually to 
321:                         process and wait (-debugger_pause) seconds for attachment
322: .  -malloc - Indicates use of PETSc error-checking malloc
323: .  -malloc no - Indicates not to use error-checking malloc
324: .  -fp_trap - Stops on floating point exceptions (Note that on the
325:               IBM RS6000 this slows code by at least a factor of 10.)
326: .  -no_signal_handler - Indicates not to trap error signals
327: .  -shared_tmp - indicates /tmp directory is shared by all processors
328: .  -not_shared_tmp - each processor has own /tmp
329: .  -tmp - alternative name of /tmp directory
330: .  -get_total_flops - returns total flops done by all processors
331: -  -memory_info - Print memory usage at end of run

333:    Options Database Keys for Profiling:
334:    See the Profiling chapter of the users manual for details.
335: +  -log_trace [filename] - Print traces of all PETSc calls
336:         to the screen (useful to determine where a program
337:         hangs without running in the debugger).  See PetscLogTraceBegin().
338: .  -log_info <optional filename> - Prints verbose information to the screen
339: -  -log_info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages

341:    Environmental Variables:
342: +   PETSC_TMP - alternative tmp directory
343: .   PETSC_SHARED_TMP - tmp is shared by all processes
344: .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
345: .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
346: -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to


349:    Level: beginner

351:    Notes:
352:    If for some reason you must call MPI_Init() separately, call
353:    it before PetscInitialize().

355:    Fortran Version:
356:    In Fortran this routine has the format
357: $       call PetscInitialize(file,ierr)

359: +   ierr - error return code
360: -   file - [optional] PETSc database file name, defaults to 
361:            ~username/.petscrc (use PETSC_NULL_CHARACTER for default)
362:            
363:    Important Fortran Note:
364:    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
365:    null character string; you CANNOT just use PETSC_NULL as 
366:    in the C version.  See the users manual for details.


369:    Concepts: initializing PETSc
370:    
371: .seealso: PetscFinalize(), PetscInitializeFortran(), PetescGetArgs()

373: @*/
374: PetscErrorCode PETSC_DLLEXPORT PetscInitialize(int *argc,char ***args,const char file[],const char help[])
375: {
377:   PetscMPIInt    flag, size;
378:   PetscTruth     flg;
379:   char           hostname[256];

382:   if (PetscInitializeCalled) return(0);
383: 
384:   PetscOptionsCreate();

386:   /*
387:      We initialize the program name here (before MPI_Init()) because MPICH has a bug in 
388:      it that it sets args[0] on all processors to be args[0] on the first processor.
389:   */
390:   if (argc && *argc) {
391:     PetscSetProgramName(**args);
392:   } else {
393:     PetscSetProgramName("Unknown Name");
394:   }


397:   MPI_Initialized(&flag);
398:   if (!flag) {
399:     if (PETSC_COMM_WORLD) SETERRQ(PETSC_ERR_SUP,"You cannot set PETSC_COMM_WORLD if you have not initialized MPI first");
400:     MPI_Init(argc,args);
401:     PetscBeganMPI = PETSC_TRUE;
402:   }
403:   if (argc && args) {
404:     PetscGlobalArgc = *argc;
405:     PetscGlobalArgs = *args;
406:   }
407:   PetscInitializeCalled = PETSC_TRUE;
408:   PetscFinalizeCalled   = PETSC_FALSE;

410:   if (!PETSC_COMM_WORLD) {
411:     PETSC_COMM_WORLD = MPI_COMM_WORLD;
412:   }

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

417:   MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);
418:   MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);

420: #if defined(PETSC_USE_COMPLEX)
421:   /* 
422:      Initialized the global complex variable; this is because with 
423:      shared libraries the constructors for global variables
424:      are not called; at least on IRIX.
425:   */
426:   {
427:     PetscScalar ic(0.0,1.0);
428:     PETSC_i = ic;
429:   }
430:   MPI_Type_contiguous(2,MPIU_REAL,&MPIU_COMPLEX);
431:   MPI_Type_commit(&MPIU_COMPLEX);
432:   MPI_Op_create(PetscSum_Local,1,&PetscSum_Op);
433: #endif

435:   /*
436:      Create the PETSc MPI reduction operator that sums of the first
437:      half of the entries and maxes the second half.
438:   */
439:   MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);

441:   MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);
442:   MPI_Type_commit(&MPIU_2SCALAR);
443:   MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);
444:   MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);

446:   MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);
447:   MPI_Type_commit(&MPIU_2INT);

449:   /*
450:      Build the options database and check for user setup requests
451:   */
452:   PetscOptionsInsert(argc,args,file);

454:   /*
455:      Print main application help message
456:   */
457:   PetscOptionsHasName(PETSC_NULL,"-help",&flg);
458:   if (help && flg) {
459:     PetscPrintf(PETSC_COMM_WORLD,help);
460:   }
461:   PetscOptionsCheckInitial_Private();

463:   /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */
464:   PetscLogBegin_Private();

466:   /*
467:      Load the dynamic libraries (on machines that support them), this registers all
468:      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
469:   */
470:   PetscInitialize_DynamicLibraries();

472:   /*
473:      Initialize all the default viewers
474:   */
475:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
476:   PetscLogInfo((0,"PetscInitialize:PETSc successfully started: number of processors = %d\n",size));
477:   PetscGetHostName(hostname,256);
478:   PetscLogInfo((0,"PetscInitialize:Running on machine: %s\n",hostname));

480:   PetscOptionsCheckInitial_Components();

482:   PetscFunctionReturn(ierr);
483: }


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

493:    Collective on PETSC_COMM_WORLD

495:    Options Database Keys:
496: +  -options_table - Calls PetscOptionsPrint()
497: .  -options_left - Prints unused options that remain in the database
498: .  -options_left no - Does not print unused options that remain in the database
499: .  -mpidump - Calls PetscMPIDump()
500: .  -malloc_dump - Calls PetscMallocDump()
501: .  -malloc_info - Prints total memory usage
502: .  -malloc_debug - Calls PetscMallocDebug(), checks allocated memory for corruption while running
503: -  -malloc_log - Prints summary of memory usage

505:    Options Database Keys for Profiling:
506:    See the Profiling chapter of the users manual for details.
507: +  -log_summary [filename] - Prints summary of flop and timing
508:         information to screen. If the filename is specified the
509:         summary is written to the file. (for code compiled with 
510:         PETSC_USE_LOG).  See PetscLogPrintSummary().
511: .  -log_all [filename] - Logs extensive profiling information
512:         (for code compiled with PETSC_USE_LOG). See PetscLogDump(). 
513: .  -log [filename] - Logs basic profiline information (for
514:         code compiled with PETSC_USE_LOG).  See PetscLogDump().
515: .  -log_sync - Log the synchronization in scatters, inner products
516:         and norms
517: -  -log_mpe [filename] - Creates a logfile viewable by the 
518:       utility Upshot/Nupshot (in MPICH distribution)

520:    Level: beginner

522:    Note:
523:    See PetscInitialize() for more general runtime options.

525: .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
526: @*/
527: PetscErrorCode PETSC_DLLEXPORT PetscFinalize(void)
528: {
530:   PetscMPIInt    rank;
531:   int            nopt;
532:   PetscTruth     flg1,flg2,flg3;
533: 

536:   if (!PetscInitializeCalled) {
537:     (*PetscErrorPrintf)("PetscInitialize() must be called before PetscFinalize()\n");
538:     return(0);
539:   }

541:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
542:   PetscOptionsHasName(PETSC_NULL,"-malloc_info",&flg2);
543:   if (!flg2) {
544:     PetscOptionsHasName(PETSC_NULL,"-memory_info",&flg2);
545:   }
546:   if (flg2) {
547:     PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");
548:   }

550:   /* Destroy auxiliary packages */
551: #if defined(PETSC_HAVE_MATHEMATICA)
552:   PetscViewerMathematicaFinalizePackage();
553: #endif
554:   PetscPLAPACKFinalizePackage();

556:   /*
557:      Destroy all the function registration lists created
558:   */
559:   PetscFinalize_DynamicLibraries();

561: #if defined(PETSC_USE_LOG)
562:   PetscOptionsHasName(PETSC_NULL,"-get_total_flops",&flg1);
563:   if (flg1) {
564:     PetscLogDouble flops = 0;
565:     MPI_Reduce(&_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);
566:     PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);
567:   }
568: #endif

570:   /*
571:      Free all objects registered with PetscObjectRegisterDestroy() such ast
572:     PETSC_VIEWER_XXX_().
573:   */
574:   PetscObjectRegisterDestroyAll();

576: #if defined(PETSC_USE_DEBUG)
577:   if (PetscStackActive) {
578:     PetscStackDestroy();
579:   }
580: #endif

582: #if defined(PETSC_USE_LOG)
583:   {
584:     char mname[PETSC_MAX_PATH_LEN];
585: #if defined(PETSC_HAVE_MPE)
586:     mname[0] = 0;
587:     PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);
588:     if (flg1){
589:       if (mname[0]) {PetscLogMPEDump(mname);}
590:       else          {PetscLogMPEDump(0);}
591:     }
592: #endif
593:     mname[0] = 0;
594:     PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);
595:     if (flg1) {
596:       if (mname[0])  {PetscLogPrintSummary(PETSC_COMM_WORLD,mname);}
597:       else           {PetscLogPrintSummary(PETSC_COMM_WORLD,0);}
598:     }

600:     mname[0] = 0;
601:     PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);
602:     PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);
603:     if (flg1 || flg2){
604:       if (mname[0]) PetscLogDump(mname);
605:       else          PetscLogDump(0);
606:     }
607:     PetscLogDestroy();
608:   }
609: #endif
610:   PetscOptionsHasName(PETSC_NULL,"-no_signal_handler",&flg1);
611:   if (!flg1) { PetscPopSignalHandler();}
612:   PetscOptionsHasName(PETSC_NULL,"-mpidump",&flg1);
613:   if (flg1) {
614:     PetscMPIDump(stdout);
615:   }
616:   PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);
617:   PetscOptionsHasName(PETSC_NULL,"-options_table",&flg2);
618:   if (flg2) {
619:     if (!rank) {PetscOptionsPrint(stdout);}
620:   }

622:   /* to prevent PETSc -options_left from warning */
623:   PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr)
624:   PetscOptionsHasName(PETSC_NULL,"-error_output_stderr",&flg1);

626:   PetscOptionsGetTruth(PETSC_NULL,"-options_left",&flg2,&flg1);
627:   PetscOptionsAllUsed(&nopt);
628:   if (flg2) {
629:     PetscOptionsPrint(stdout);
630:     if (!nopt) {
631:       PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");
632:     } else if (nopt == 1) {
633:       PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");
634:     } else {
635:       PetscPrintf(PETSC_COMM_WORLD,"There are %d unused database options. They are:\n",nopt);
636:     }
637:   }
638: #if defined(PETSC_USE_DEBUG)
639:   if (nopt && !flg1 && !flg2) {
640:     PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");
641:     PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");
642:     PetscOptionsLeft();
643:   } else if (nopt && flg2) {
644: #else 
645:   if (nopt && flg2) {
646: #endif
647:     PetscOptionsLeft();
648:   }

650:   PetscOptionsHasName(PETSC_NULL,"-log_history",&flg1);
651:   if (flg1) {
652:     PetscLogCloseHistoryFile(&petsc_history);
653:     petsc_history = 0;
654:   }

656:   PetscLogInfoAllow(PETSC_FALSE,PETSC_NULL);

658:   /*
659:        Free all the registered create functions, such as KSPList, VecList, SNESList, etc
660:   */
661:   PetscFListDestroyAll();

663:   PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);
664:   PetscOptionsHasName(PETSC_NULL,"-malloc_log",&flg3);
665:   if (flg1) {
666:     char fname[PETSC_MAX_PATH_LEN];
667:     FILE *fd;
668: 
669:     fname[0] = 0;
670:     PetscOptionsGetString(PETSC_NULL,"-malloc_dump",fname,250,&flg1);
671:     if (flg1 && fname[0]) {
672:       char sname[PETSC_MAX_PATH_LEN];

674:       sprintf(sname,"%s_%d",fname,rank);
675:       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
676:       PetscMallocDump(fd);
677:       fclose(fd);
678:     } else {
679:       MPI_Comm local_comm;

681:       MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);
682:       PetscSequentialPhaseBegin_Private(local_comm,1);
683:         PetscMallocDump(stdout);
684:       PetscSequentialPhaseEnd_Private(local_comm,1);
685:       MPI_Comm_free(&local_comm);
686:     }
687:   }
688:   if (flg3) {
689:     char fname[PETSC_MAX_PATH_LEN];
690:     FILE *fd;
691: 
692:     fname[0] = 0;
693:     PetscOptionsGetString(PETSC_NULL,"-malloc_log",fname,250,&flg1);
694:     if (flg1 && fname[0]) {
695:       char sname[PETSC_MAX_PATH_LEN];

697:       sprintf(sname,"%s_%d",fname,rank);
698:       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
699:       PetscMallocDumpLog(fd);
700:       fclose(fd);
701:     } else {
702:       PetscMallocDumpLog(stdout);
703:     }
704:   }
705:   /* Can be destroyed only after all the options are used */
706:   PetscOptionsDestroy();

708:   PetscGlobalArgc = 0;
709:   PetscGlobalArgs = 0;

711: #if defined(PETSC_USE_COMPLEX)
712:   MPI_Op_free(&PetscSum_Op);
713:   MPI_Type_free(&MPIU_COMPLEX);
714: #endif
715:   MPI_Type_free(&MPIU_2SCALAR);
716:   MPI_Type_free(&MPIU_2INT);
717:   MPI_Op_free(&PetscMaxSum_Op);
718:   MPI_Op_free(&PetscADMax_Op);
719:   MPI_Op_free(&PetscADMin_Op);

721:   PetscLogInfo((0,"PetscFinalize:PETSc successfully ended!\n"));
722:   if (PetscBeganMPI) {
723:     MPI_Finalize();
724:   }

726: /*

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

736: */
737:   PetscClearMalloc();
738:   PetscInitializeCalled = PETSC_FALSE;
739:   PetscFinalizeCalled   = PETSC_TRUE;
740:   PetscFunctionReturn(ierr);
741: }

743: /*
744:      These may be used in code that ADIC is to be used on
745: */

749: /*@C
750:       PetscGlobalMax - Computes the maximum value over several processors

752:      Collective on MPI_Comm

754:    Input Parameters:
755: +   local - the local value
756: -   comm - the processors that find the maximum

758:    Output Parameter:
759: .   result - the maximum value
760:   
761:    Level: intermediate

763:    Notes:
764:      These functions are to be used inside user functions that are to be processed with 
765:    ADIC. PETSc will automatically provide differentiated versions of these functions

767: .seealso: PetscGlobalMin(), PetscGlobalSum()
768: @*/
769: PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal* local,PetscReal* result,MPI_Comm comm)
770: {
771:   return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MAX,comm);
772: }

776: /*@C
777:       PetscGlobalMin - Computes the minimum value over several processors

779:      Collective on MPI_Comm

781:    Input Parameters:
782: +   local - the local value
783: -   comm - the processors that find the minimum

785:    Output Parameter:
786: .   result - the minimum value
787:   
788:    Level: intermediate

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

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

803: /*@C
804:       PetscGlobalSum - Computes the sum over sever processors

806:      Collective on MPI_Comm

808:    Input Parameters:
809: +   local - the local value
810: -   comm - the processors that find the sum

812:    Output Parameter:
813: .   result - the sum
814:   
815:    Level: intermediate

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

821: .seealso: PetscGlobalMin(), PetscGlobalMax()
822: @*/
823: PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar* local,PetscScalar* result,MPI_Comm comm)
824: {
825:   return MPI_Allreduce(local,result,1,MPIU_SCALAR,PetscSum_Op,comm);
826: }