Actual source code: pinit.c

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

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

  8: EXTERN PetscErrorCode PetscLogBegin_Private(void);

 10: /* -----------------------------------------------------------------------------------------*/


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

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

 24: /*
 25:        Checks the options database for initializations related to the 
 26:     PETSc components
 27: */
 30: PetscErrorCode PetscOptionsCheckInitial_Components(void)
 31: {
 32:   MPI_Comm   comm = PETSC_COMM_WORLD;
 33:   PetscTruth flg1;

 37:   PetscOptionsHasName(PETSC_NULL,"-help",&flg1);
 38:   if (flg1) {
 39: #if defined (PETSC_USE_LOG)
 40:     (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");
 41:     (*PetscHelpPrintf)(comm," -log_summary_exclude: <vec,mat,pc.ksp,snes>\n");
 42:     (*PetscHelpPrintf)(comm," -log_info_exclude: <null,vec,mat,pc,ksp,snes,ts>\n");
 43:     (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
 44: #endif
 45:   }
 46:   return(0);
 47: }

 51: /*@C
 52:       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
 53:         the command line arguments.

 55:    Collective
 56:   
 57:    Level: advanced

 59: .seealso: PetscInitialize(), PetscInitializeFortran()
 60: @*/
 61: PetscErrorCode PetscInitializeNoArguments(void)
 62: {
 64:   int            argc = 0;
 65:   char           **args = 0;

 68:   PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL);
 69:   PetscFunctionReturn(ierr);
 70: }

 74: /*@
 75:       PetscInitialized - Determine whether PETSc is initialized.
 76:   
 77:    Level: beginner

 79: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
 80: @*/
 81: PetscErrorCode PetscInitialized(PetscTruth *isInitialized)
 82: {
 85:   *isInitialized = PetscInitializeCalled;
 86:   return(0);
 87: }

 89: EXTERN PetscErrorCode        PetscOptionsCheckInitial_Private(void);

 92: /*
 93:        This function is the MPI reduction operation used to compute the sum of the 
 94:    first half of the datatype and the max of the second half.
 95: */
 96: MPI_Op PetscMaxSum_Op = 0;

101: void PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
102: {
103:   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;

106:   if (*datatype != MPIU_2INT) {
107:     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
108:     MPI_Abort(MPI_COMM_WORLD,1);
109:   }

111:   for (i=0; i<count; i++) {
112:     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
113:     xout[2*i+1] += xin[2*i+1];
114:   }
115:   PetscStackPop;
116:   return;
117: }

120: /*
121:     Returns the max of the first entry owned by this processor and the
122: sum of the second entry.
123: */
126: PetscErrorCode PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum)
127: {
128:   PetscMPIInt    size,rank;
129:   PetscInt       *work;

133:   MPI_Comm_size(comm,&size);
134:   MPI_Comm_rank(comm,&rank);
135:   PetscMalloc(2*size*sizeof(PetscInt),&work);
136:   MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);
137:   *max   = work[2*rank];
138:   *sum   = work[2*rank+1];
139:   PetscFree(work);
140:   return(0);
141: }

143: /* ----------------------------------------------------------------------------*/
144: MPI_Op PetscADMax_Op = 0;

149: void PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
150: {
151:   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
152:   PetscInt    i,count = *cnt;

155:   if (*datatype != MPIU_2SCALAR) {
156:     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
157:     MPI_Abort(MPI_COMM_WORLD,1);
158:   }

160:   for (i=0; i<count; i++) {
161:     if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) {
162:       xout[2*i]   = xin[2*i];
163:       xout[2*i+1] = xin[2*i+1];
164:     }
165:   }

167:   PetscStackPop;
168:   return;
169: }

172: MPI_Op PetscADMin_Op = 0;

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

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

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

195:   PetscStackPop;
196:   return;
197: }
199: /* ---------------------------------------------------------------------------------------*/

201: #if defined(PETSC_USE_COMPLEX)
202: MPI_Op PetscSum_Op = 0;

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

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

218:   for (i=0; i<count; i++) {
219:     xout[i] += xin[i];
220:   }

222:   PetscStackPop;
223:   return;
224: }
226: #endif

228: static int  PetscGlobalArgc   = 0;
229: static char **PetscGlobalArgs = 0;

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

237:    Not Collective

239:    Output Parameters:
240: +  argc - count of number of command line arguments
241: -  args - the command line arguments

243:    Level: intermediate

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

249:    Concepts: command line arguments
250:    
251: .seealso: PetscFinalize(), PetscInitializeFortran()

253: @*/
254: PetscErrorCode PetscGetArgs(int *argc,char ***args)
255: {
257:   if (!PetscGlobalArgs) {
258:     SETERRQ(PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
259:   }
260:   *argc = PetscGlobalArgc;
261:   *args = PetscGlobalArgs;
262:   return(0);
263: }

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

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

275:    Input Parameters:
276: +  argc - count of number of command line arguments
277: .  args - the command line arguments
278: .  file - [optional] PETSc database file, defaults to ~username/.petscrc
279:           (use PETSC_NULL for default)
280: -  help - [optional] Help message to print, use PETSC_NULL for no message

282:    Options Database Keys:
283: +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
284: .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
285: .  -on_error_emacs <machinename> causes emacsclient to jump to error file
286: .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
287: .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
288: .  -stop_for_debugger - Print message on how to attach debugger manually to 
289:                         process and wait (-debugger_pause) seconds for attachment
290: .  -trmalloc - Indicates use of PETSc error-checking malloc
291: .  -trmalloc no - Indicates not to use error-checking malloc
292: .  -fp_trap - Stops on floating point exceptions (Note that on the
293:               IBM RS6000 this slows code by at least a factor of 10.)
294: .  -no_signal_handler - Indicates not to trap error signals
295: .  -shared_tmp - indicates /tmp directory is shared by all processors
296: .  -not_shared_tmp - each processor has own /tmp
297: .  -tmp - alternative name of /tmp directory
298: .  -get_total_flops - returns total flops done by all processors
299: -  -get_resident_set_size - Print memory usage at end of run

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

309:    Environmental Variables:
310: +   PETSC_TMP - alternative tmp directory
311: .   PETSC_SHARED_TMP - tmp is shared by all processes
312: .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
313: .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
314: -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to


317:    Level: beginner

319:    Notes:
320:    If for some reason you must call MPI_Init() separately, call
321:    it before PetscInitialize().

323:    Fortran Version:
324:    In Fortran this routine has the format
325: $       call PetscInitialize(file,ierr)

327: +   ierr - error return code
328: -   file - [optional] PETSc database file name, defaults to 
329:            ~username/.petscrc (use PETSC_NULL_CHARACTER for default)
330:            
331:    Important Fortran Note:
332:    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
333:    null character string; you CANNOT just use PETSC_NULL as 
334:    in the C version.  See the users manual for details.


337:    Concepts: initializing PETSc
338:    
339: .seealso: PetscFinalize(), PetscInitializeFortran(), PetescGetArgs()

341: @*/
342: PetscErrorCode PetscInitialize(int *argc,char ***args,const char file[],const char help[])
343: {
345:   int            flag,dummy_tag;
346:   PetscMPIInt    size;
347:   PetscTruth     flg;
348:   char           hostname[256];

351:   if (PetscInitializeCalled) return(0);

353:   PetscOptionsCreate();

355:   /*
356:      We initialize the program name here (before MPI_Init()) because MPICH has a bug in 
357:      it that it sets args[0] on all processors to be args[0] on the first processor.
358:   */
359:   if (argc && *argc) {
360:     PetscSetProgramName(**args);
361:   } else {
362:     PetscSetProgramName("Unknown Name");
363:   }


366:   MPI_Initialized(&flag);
367:   if (!flag) {
368:     MPI_Init(argc,args);
369:     PetscBeganMPI = PETSC_TRUE;
370:   }
371:   if (argc && args) {
372:     PetscGlobalArgc = *argc;
373:     PetscGlobalArgs = *args;
374:   }
375:   PetscInitializeCalled = PETSC_TRUE;

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

380:   if (!PETSC_COMM_WORLD) {
381:     PETSC_COMM_WORLD = MPI_COMM_WORLD;
382:   }

384:   MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);
385:   MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);

387: #if defined(PETSC_USE_COMPLEX)
388:   /* 
389:      Initialized the global complex variable; this is because with 
390:      shared libraries the constructors for global variables
391:      are not called; at least on IRIX.
392:   */
393:   {
394:     PetscScalar ic(0.0,1.0);
395:     PETSC_i = ic;
396:   }
397:   MPI_Type_contiguous(2,MPIU_REAL,&MPIU_COMPLEX);
398:   MPI_Type_commit(&MPIU_COMPLEX);
399:   MPI_Op_create(PetscSum_Local,1,&PetscSum_Op);
400: #endif

402:   /*
403:      Create the PETSc MPI reduction operator that sums of the first
404:      half of the entries and maxes the second half.
405:   */
406:   MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);

408:   MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);
409:   MPI_Type_commit(&MPIU_2SCALAR);
410:   MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);
411:   MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);

413:   MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);
414:   MPI_Type_commit(&MPIU_2INT);

416:   /*
417:      Build the options database and check for user setup requests
418:   */
419:   PetscOptionsInsert(argc,args,file);

421:   /*
422:      Print main application help message
423:   */
424:   PetscOptionsHasName(PETSC_NULL,"-help",&flg);
425:   if (help && flg) {
426:     PetscPrintf(PETSC_COMM_WORLD,help);
427:   }
428:   PetscOptionsCheckInitial_Private();

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

433:   /*
434:      Initialize PETSC_COMM_SELF and WORLD as a MPI_Comm with the PETSc attribute.
435:     
436:      We delay until here to do it, since PetscMalloc() may not have been
437:      setup before this.
438:   */
439:   PetscCommDuplicate(MPI_COMM_SELF,&PETSC_COMM_SELF,&dummy_tag);
440:   PetscCommDuplicate(PETSC_COMM_WORLD,&PETSC_COMM_WORLD,&dummy_tag);

442:   /*
443:      Load the dynamic libraries (on machines that support them), this registers all
444:      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
445:   */
446:   PetscInitialize_DynamicLibraries();

448:   /*
449:      Initialize all the default viewers
450:   */
451:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
452:   PetscLogInfo(0,"PetscInitialize:PETSc successfully started: number of processors = %d\n",size);
453:   PetscGetHostName(hostname,256);
454:   PetscLogInfo(0,"PetscInitialize:Running on machine: %s\n",hostname);

456:   PetscOptionsCheckInitial_Components();

458:   PetscFunctionReturn(ierr);
459: }


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

469:    Collective on PETSC_COMM_WORLD

471:    Options Database Keys:
472: +  -options_table - Calls PetscOptionsPrint()
473: .  -options_left - Prints unused options that remain in the database
474: .  -options_left no - Does not print unused options that remain in the database
475: .  -mpidump - Calls PetscMPIDump()
476: .  -trdump - Calls PetscTrDump()
477: .  -trinfo - Prints total memory usage
478: .  -trdebug - Calls PetscTrDebug(), checks allocated memory for corruption while running
479: -  -trmalloc_log - Prints summary of memory usage

481:    Options Database Keys for Profiling:
482:    See the Profiling chapter of the users manual for details.
483: +  -log_summary [filename] - Prints summary of flop and timing
484:         information to screen. If the filename is specified the
485:         summary is written to the file. (for code compiled with 
486:         PETSC_USE_LOG).  See PetscLogPrintSummary().
487: .  -log_all [filename] - Logs extensive profiling information
488:         (for code compiled with PETSC_USE_LOG). See PetscLogDump(). 
489: .  -log [filename] - Logs basic profiline information (for
490:         code compiled with PETSC_USE_LOG).  See PetscLogDump().
491: .  -log_sync - Log the synchronization in scatters, inner products
492:         and norms
493: -  -log_mpe [filename] - Creates a logfile viewable by the 
494:       utility Upshot/Nupshot (in MPICH distribution)

496:    Level: beginner

498:    Note:
499:    See PetscInitialize() for more general runtime options.

501: .seealso: PetscInitialize(), PetscOptionsPrint(), PetscTrDump(), PetscMPIDump(), PetscEnd()
502: @*/
503: PetscErrorCode PetscFinalize(void)
504: {
506:   PetscMPIInt    rank;
507:   int            nopt;
508:   PetscLogDouble rss;
509:   PetscTruth     flg1,flg2,flg3;
510: 

513:   if (!PetscInitializeCalled) {
514:     (*PetscErrorPrintf)("PetscInitialize() must be called before PetscFinalize()\n");
515:     return(0);
516:   }
517:   /* Destroy auxiliary packages */
518: #if defined(PETSC_HAVE_MATHEMATICA)
519:   PetscViewerMathematicaFinalizePackage();
520: #endif
521:   PetscPLAPACKFinalizePackage();

523:   /*
524:      Destroy all the function registration lists created
525:   */
526:   PetscFinalize_DynamicLibraries();


529:   PetscOptionsHasName(PETSC_NULL,"-get_resident_set_size",&flg1);
530:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
531:   if (flg1) {
532:     PetscGetResidentSetSize(&rss);
533:     if (rss) {
534:       PetscPrintf(PETSC_COMM_SELF,"[%d] Size of entire process memory %D\n",rank,(PetscInt)rss);
535:     } else {
536:       PetscPrintf(PETSC_COMM_SELF,"[%d] OS does not support computing entire process memory\n",rank);
537:     }
538:   }

540: #if defined(PETSC_USE_LOG)
541:   PetscOptionsHasName(PETSC_NULL,"-get_total_flops",&flg1);
542:   if (flg1) {
543:     PetscLogDouble flops = 0;
544:     MPI_Reduce(&_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);
545:     PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);
546:   }
547: #endif

549:   /*
550:      Free all objects registered with PetscObjectRegisterDestroy() such ast
551:     PETSC_VIEWER_XXX_().
552:   */
553:   PetscObjectRegisterDestroyAll();

555: #if defined(PETSC_USE_STACK)
556:   if (PetscStackActive) {
557:     PetscStackDestroy();
558:   }
559: #endif

561: #if defined(PETSC_USE_LOG)
562:   {
563:     char mname[PETSC_MAX_PATH_LEN];
564: #if defined(PETSC_HAVE_MPE)
565:     mname[0] = 0;
566:     PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);
567:     if (flg1){
568:       if (mname[0]) {PetscLogMPEDump(mname);}
569:       else          {PetscLogMPEDump(0);}
570:     }
571: #endif
572:     mname[0] = 0;
573:     PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);
574:     if (flg1) {
575:       if (mname[0])  {PetscLogPrintSummary(PETSC_COMM_WORLD,mname);}
576:       else           {PetscLogPrintSummary(PETSC_COMM_WORLD,0);}
577:     }

579:     mname[0] = 0;
580:     PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);
581:     PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);
582:     if (flg1 || flg2){
583:       if (mname[0]) PetscLogDump(mname);
584:       else          PetscLogDump(0);
585:     }
586:     PetscLogDestroy();
587:   }
588: #endif
589:   PetscOptionsHasName(PETSC_NULL,"-no_signal_handler",&flg1);
590:   if (!flg1) { PetscPopSignalHandler();}
591:   PetscOptionsHasName(PETSC_NULL,"-mpidump",&flg1);
592:   if (flg1) {
593:     PetscMPIDump(stdout);
594:   }
595:   PetscOptionsHasName(PETSC_NULL,"-trdump",&flg1);
596:   PetscOptionsHasName(PETSC_NULL,"-options_table",&flg2);
597:   if (flg2) {
598:     if (!rank) {PetscOptionsPrint(stdout);}
599:   }

601:   /* to prevent PETSc -options_left from warning */
602:   PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr)
603:   PetscOptionsHasName(PETSC_NULL,"-error_output_stderr",&flg1);

605:   PetscOptionsGetLogical(PETSC_NULL,"-options_left",&flg2,&flg1);
606:   PetscOptionsAllUsed(&nopt);
607:   if (flg2) {
608:     PetscOptionsPrint(stdout);
609:     if (!nopt) {
610:       PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");
611:     } else if (nopt == 1) {
612:       PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");
613:     } else {
614:       PetscPrintf(PETSC_COMM_WORLD,"There are %d unused database options. They are:\n",nopt);
615:     }
616:   }
617: #if defined(PETSC_USE_BOPT_g)
618:   if (nopt && !flg1 && !flg2) {
619:     PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");
620:     PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");
621:     PetscOptionsLeft();
622:   } else if (nopt && flg2) {
623: #else 
624:   if (nopt && flg2) {
625: #endif
626:     PetscOptionsLeft();
627:   }

629:   PetscOptionsHasName(PETSC_NULL,"-log_history",&flg1);
630:   if (flg1) {
631:     PetscLogCloseHistoryFile(&petsc_history);
632:     petsc_history = 0;
633:   }


636:   /*
637:        Destroy PETSC_COMM_SELF/WORLD as a MPI_Comm with the PETSc 
638:      attribute.
639:   */
640:   PetscLogInfoAllow(PETSC_FALSE,PETSC_NULL);
641:   PetscCommDestroy(&PETSC_COMM_SELF);
642:   PetscCommDestroy(&PETSC_COMM_WORLD);

644:   /*
645:        Free all the registered create functions, such as KSPList, VecList, SNESList, etc
646:   */
647:   PetscFListDestroyAll();

649:   PetscOptionsHasName(PETSC_NULL,"-trdump",&flg1);
650:   PetscOptionsHasName(PETSC_NULL,"-trinfo",&flg2);
651:   PetscOptionsHasName(PETSC_NULL,"-trmalloc_log",&flg3);
652:   if (flg1) {
653:     char fname[PETSC_MAX_PATH_LEN];
654:     FILE *fd;
655: 
656:     fname[0] = 0;
657:     PetscOptionsGetString(PETSC_NULL,"-trdump",fname,250,&flg1);
658:     if (flg1 && fname[0]) {
659:       char sname[PETSC_MAX_PATH_LEN];

661:       sprintf(sname,"%s_%d",fname,rank);
662:       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
663:       PetscTrDump(fd);
664:       fclose(fd);
665:     } else {
666:       MPI_Comm local_comm;

668:       MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);
669:       PetscSequentialPhaseBegin_Private(local_comm,1);
670:         PetscTrDump(stdout);
671:       PetscSequentialPhaseEnd_Private(local_comm,1);
672:       MPI_Comm_free(&local_comm);
673:     }
674:   } else if (flg2) {
675:     MPI_Comm       local_comm;
676:     PetscLogDouble maxm;

678:     MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);
679:     PetscTrSpace(PETSC_NULL,PETSC_NULL,&maxm);
680:     PetscSequentialPhaseBegin_Private(local_comm,1);
681:       printf("[%d] Maximum memory used %g\n",rank,maxm);
682:     PetscSequentialPhaseEnd_Private(local_comm,1);
683:     MPI_Comm_free(&local_comm);
684:   }
685:   if (flg3) {
686:     char fname[PETSC_MAX_PATH_LEN];
687:     FILE *fd;
688: 
689:     fname[0] = 0;
690:     PetscOptionsGetString(PETSC_NULL,"-trmalloc_log",fname,250,&flg1);
691:     if (flg1 && fname[0]) {
692:       char sname[PETSC_MAX_PATH_LEN];

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

705:   PetscGlobalArgc = 0;
706:   PetscGlobalArgs = 0;

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

718:   PetscLogInfo(0,"PetscFinalize:PETSc successfully ended!\n");
719:   if (PetscBeganMPI) {
720:     MPI_Finalize();
721:   }

723: /*

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

733: */
734:   PetscClearMalloc();
735:   PetscInitializeCalled = PETSC_FALSE;
736:   PetscFunctionReturn(ierr);
737: }

739: /*
740:      These may be used in code that ADIC is to be used on
741: */

745: /*@C
746:       PetscGlobalMax - Computes the maximum value over several processors

748:      Collective on MPI_Comm

750:    Input Parameters:
751: +   local - the local value
752: -   comm - the processors that find the maximum

754:    Output Parameter:
755: .   result - the maximum value
756:   
757:    Level: intermediate

759:    Notes:
760:      These functions are to be used inside user functions that are to be processed with 
761:    ADIC. PETSc will automatically provide differentiated versions of these functions

763: .seealso: PetscGlobalMin(), PetscGlobalSum()
764: @*/
765: PetscErrorCode PetscGlobalMax(PetscReal* local,PetscReal* result,MPI_Comm comm)
766: {
767:   return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MAX,comm);
768: }

772: /*@C
773:       PetscGlobalMin - Computes the minimum value over several processors

775:      Collective on MPI_Comm

777:    Input Parameters:
778: +   local - the local value
779: -   comm - the processors that find the minimum

781:    Output Parameter:
782: .   result - the minimum value
783:   
784:    Level: intermediate

786:    Notes:
787:      These functions are to be used inside user functions that are to be processed with 
788:    ADIC. PETSc will automatically provide differentiated versions of these functions

790: .seealso: PetscGlobalMax(), PetscGlobalSum()
791: @*/
792: PetscErrorCode PetscGlobalMin(PetscReal* local,PetscReal* result,MPI_Comm comm)
793: {
794:   return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MIN,comm);
795: }

799: /*@C
800:       PetscGlobalSum - Computes the sum over sever processors

802:      Collective on MPI_Comm

804:    Input Parameters:
805: +   local - the local value
806: -   comm - the processors that find the sum

808:    Output Parameter:
809: .   result - the sum
810:   
811:    Level: intermediate

813:    Notes:
814:      These functions are to be used inside user functions that are to be processed with 
815:    ADIC. PETSc will automatically provide differentiated versions of these functions

817: .seealso: PetscGlobalMin(), PetscGlobalMax()
818: @*/
819: PetscErrorCode PetscGlobalSum(PetscScalar* local,PetscScalar* result,MPI_Comm comm)
820: {
821:   return MPI_Allreduce(local,result,1,MPIU_SCALAR,PetscSum_Op,comm);
822: }