Actual source code: pinit.c

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

 6:  #include petscsys.h

  8: #if defined(PETSC_USE_LOG)
  9: EXTERN PetscErrorCode PetscLogBegin_Private(void);
 10: #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 = MPI_COMM_NULL;

 30: PetscMPIInt Petsc_Counter_keyval   = MPI_KEYVAL_INVALID;
 31: PetscMPIInt Petsc_InnerComm_keyval = MPI_KEYVAL_INVALID;
 32: PetscMPIInt Petsc_OuterComm_keyval = MPI_KEYVAL_INVALID;

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

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

 45: /*
 46:        Checks the options database for initializations related to the 
 47:     PETSc components
 48: */
 51: PetscErrorCode  PetscOptionsCheckInitial_Components(void)
 52: {
 53:   PetscTruth flg1;

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

 72: /*@C
 73:       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
 74:         the command line arguments.

 76:    Collective
 77:   
 78:    Level: advanced

 80: .seealso: PetscInitialize(), PetscInitializeFortran()
 81: @*/
 82: PetscErrorCode  PetscInitializeNoArguments(void)
 83: {
 85:   int            argc = 0;
 86:   char           **args = 0;

 89:   PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL);
 90:   PetscFunctionReturn(ierr);
 91: }

 95: /*@
 96:       PetscInitialized - Determine whether PETSc is initialized.
 97:   
 98: 7   Level: beginner

100: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
101: @*/
102: PetscErrorCode  PetscInitialized(PetscTruth *isInitialized)
103: {
106:   *isInitialized = PetscInitializeCalled;
107:   return(0);
108: }

112: /*@
113:       PetscFinalized - Determine whether PetscFinalize() has been called yet
114:   
115:    Level: developer

117: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
118: @*/
119: PetscErrorCode  PetscFinalized(PetscTruth *isFinalized)
120: {
123:   *isFinalized = PetscFinalizeCalled;
124:   return(0);
125: }

127: EXTERN PetscErrorCode        PetscOptionsCheckInitial_Private(void);

130: /*
131:        This function is the MPI reduction operation used to compute the sum of the 
132:    first half of the datatype and the max of the second half.
133: */
134: MPI_Op PetscMaxSum_Op = 0;

139: void  MPIAPI PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
140: {
141:   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;

144:   if (*datatype != MPIU_2INT) {
145:     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
146:     MPI_Abort(MPI_COMM_WORLD,1);
147:   }

149:   for (i=0; i<count; i++) {
150:     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
151:     xout[2*i+1] += xin[2*i+1];
152:   }
153:   PetscStackPop;
154:   return;
155: }

158: /*
159:     Returns the max of the first entry owned by this processor and the
160: sum of the second entry.

162:     The reason nprocs[2*i] contains lengths nprocs[2*i+1] contains flag of 1 if length is nonzero
163: is so that the PetscMaxSum_Op() can set TWO values, if we passed in only nprocs[i] with lengths
164: there would be no place to store the both needed results.
165: */
168: PetscErrorCode  PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum)
169: {
170:   PetscMPIInt    size,rank;
171:   PetscInt       *work;

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

185: /* ----------------------------------------------------------------------------*/
186: MPI_Op  PetscADMax_Op = 0;

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

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

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

209:   PetscStackPop;
210:   return;
211: }

214: MPI_Op  PetscADMin_Op = 0;

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

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

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

237:   PetscStackPop;
238:   return;
239: }
241: /* ---------------------------------------------------------------------------------------*/

243: #if defined(PETSC_USE_COMPLEX)

245: /*
246:     This operation is only needed when using complex numbers with older MPI that does not support complex numbers
247: */
248: #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
249: MPI_Op MPIU_SUM = 0;

254: void  PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
255: {
256:   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
257:   PetscInt    i,count = *cnt;

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

265:   for (i=0; i<count; i++) {
266:     xout[i] += xin[i];
267:   }

269:   PetscStackPop;
270:   return;
271: }
273: #endif
274: #endif

279: /*
280:    Private routine to delete internal tag/name counter storage when a communicator is freed.

282:    This is called by MPI, not by users.


286: */
287: PetscMPIInt  MPIAPI Petsc_DelCounter(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
288: {

292:   PetscInfo1(0,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
293:   PetscFree(count_val);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
294:   PetscFunctionReturn(MPI_SUCCESS);
295: }

301: /*
302:   This does not actually free anything, it simply marks when a reference count to an internal MPI_Comm reaches zero and the
303:   the external MPI_Comm drops its reference to the internal MPI_Comm

305:   This is called by MPI, not by users.


309: */
310: PetscMPIInt  MPIAPI Petsc_DelComm(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
311: {

315:   PetscInfo1(0,"Deleting PETSc communicator imbedded in a user MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
316:   /* actually don't delete anything because we cannot increase the reference count of the communicator anyways */
317:   PetscFunctionReturn(MPI_SUCCESS);
318: }

321: #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
322: #if !defined(PETSC_WORDS_BIGENDIAN)
328: #endif
329: #endif

331: int  PetscGlobalArgc   = 0;
332: char **PetscGlobalArgs = 0;

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

340:    Not Collective

342:    Output Parameters:
343: +  argc - count of number of command line arguments
344: -  args - the command line arguments

346:    Level: intermediate

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

352:    Concepts: command line arguments
353:    
354: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()

356: @*/
357: PetscErrorCode  PetscGetArgs(int *argc,char ***args)
358: {
360:   if (!PetscInitializeCalled && PetscFinalizeCalled) {
361:     SETERRQ(PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
362:   }
363:   *argc = PetscGlobalArgc;
364:   *args = PetscGlobalArgs;
365:   return(0);
366: }

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

374:    Not Collective

376:    Output Parameters:
377: .  args - the command line arguments

379:    Level: intermediate

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

384:    Concepts: command line arguments
385:    
386: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()

388: @*/
389: PetscErrorCode  PetscGetArguments(char ***args)
390: {
391:   PetscInt       i,argc = PetscGlobalArgc;

395:   if (!PetscInitializeCalled && PetscFinalizeCalled) {
396:     SETERRQ(PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
397:   }
398:   if (!argc) {*args = 0; return(0);}
399:   PetscMalloc(argc*sizeof(char*),args);
400:   for (i=0; i<argc-1; i++) {
401:     PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);
402:   }
403:   (*args)[argc-1] = 0;
404:   return(0);
405: }

409: /*@C
410:    PetscFreeArguments - Frees the memory obtained with PetscGetArguments()

412:    Not Collective

414:    Output Parameters:
415: .  args - the command line arguments 

417:    Level: intermediate

419:    Concepts: command line arguments
420:    
421: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()

423: @*/
424: PetscErrorCode  PetscFreeArguments(char **args)
425: {
426:   PetscInt       i = 0;

430:   if (!args) {return(0);}
431:   while (args[i]) {
432:     PetscFree(args[i]);
433:     i++;
434:   }
435:   PetscFree(args);
436:   return(0);
437: }

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

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

449:    Input Parameters:
450: +  argc - count of number of command line arguments
451: .  args - the command line arguments
452: .  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL to not check for
453:           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
454: -  help - [optional] Help message to print, use PETSC_NULL for no message

456:    If you wish PETSc code to run ONLY on a subcommunicator of MPI_COMM_WORLD, create that
457:    communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize(). Thus if you are running a 
458:    four process job and two processes will run PETSc and have PetscInitialize() and PetscFinalize() and two process will not,
459:    then do this. If ALL processes in the job are using PetscInitialize() and PetscFinalize() then you don't need to do this, even
460:    if different subcommunicators of the job are doing different things with PETSc.

462:    Options Database Keys:
463: +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
464: .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
465: .  -on_error_emacs <machinename> causes emacsclient to jump to error file
466: .  -on_error_abort calls abort() when error detected (no traceback)
467: .  -on_error_mpiabort calls MPI_abort() when error detected
468: .  -error_output_stderr prints error messages to stderr instead of the default stdout
469: .  -error_output_none does not print the error messages (but handles errors in the same way as if this was not called)
470: .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
471: .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
472: .  -stop_for_debugger - Print message on how to attach debugger manually to 
473:                         process and wait (-debugger_pause) seconds for attachment
474: .  -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries)
475: .  -malloc no - Indicates not to use error-checking malloc
476: .  -malloc_debug - check for memory corruption at EVERY malloc or free
477: .  -fp_trap - Stops on floating point exceptions (Note that on the
478:               IBM RS6000 this slows code by at least a factor of 10.)
479: .  -no_signal_handler - Indicates not to trap error signals
480: .  -shared_tmp - indicates /tmp directory is shared by all processors
481: .  -not_shared_tmp - each processor has own /tmp
482: .  -tmp - alternative name of /tmp directory
483: .  -get_total_flops - returns total flops done by all processors
484: -  -memory_info - Print memory usage at end of run

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

494:    Environmental Variables:
495: +   PETSC_TMP - alternative tmp directory
496: .   PETSC_SHARED_TMP - tmp is shared by all processes
497: .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
498: .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
499: -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to


502:    Level: beginner

504:    Notes:
505:    If for some reason you must call MPI_Init() separately, call
506:    it before PetscInitialize().

508:    Fortran Version:
509:    In Fortran this routine has the format
510: $       call PetscInitialize(file,ierr)

512: +   ierr - error return code
513: -  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL_CHARACTER to not check for
514:           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
515:            
516:    Important Fortran Note:
517:    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
518:    null character string; you CANNOT just use PETSC_NULL as 
519:    in the C version.  See the users manual for details.


522:    Concepts: initializing PETSc
523:    
524: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs()

526: @*/
527: PetscErrorCode  PetscInitialize(int *argc,char ***args,const char file[],const char help[])
528: {
530:   PetscMPIInt    flag, size;
531:   PetscInt       nodesize;
532:   PetscTruth     flg;
533:   char           hostname[256];

536:   if (PetscInitializeCalled) return(0);

538:   /* these must be initialized in a routine, not as a constant declaration*/
539:   PETSC_STDOUT = stdout;
540:   PETSC_STDERR = stderr;

542:   PetscOptionsCreate();

544:   /*
545:      We initialize the program name here (before MPI_Init()) because MPICH has a bug in 
546:      it that it sets args[0] on all processors to be args[0] on the first processor.
547:   */
548:   if (argc && *argc) {
549:     PetscSetProgramName(**args);
550:   } else {
551:     PetscSetProgramName("Unknown Name");
552:   }

554:   MPI_Initialized(&flag);
555:   if (!flag) {
556:     if (PETSC_COMM_WORLD != MPI_COMM_NULL) SETERRQ(PETSC_ERR_SUP,"You cannot set PETSC_COMM_WORLD if you have not initialized MPI first");
557:     MPI_Init(argc,args);
558:     PetscBeganMPI = PETSC_TRUE;
559:   }
560:   if (argc && args) {
561:     PetscGlobalArgc = *argc;
562:     PetscGlobalArgs = *args;
563:   }
564:   PetscInitializeCalled = PETSC_TRUE;
565:   PetscFinalizeCalled   = PETSC_FALSE;

567:   if (PETSC_COMM_WORLD == MPI_COMM_NULL) {
568:     PETSC_COMM_WORLD = MPI_COMM_WORLD;
569:   }

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

574:   MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);
575:   MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);

577: #if defined(PETSC_USE_COMPLEX)
578:   /* 
579:      Initialized the global complex variable; this is because with 
580:      shared libraries the constructors for global variables
581:      are not called; at least on IRIX.
582:   */
583:   {
584: #if defined(PETSC_CLANGUAGE_CXX)
585:     PetscScalar ic(0.0,1.0);
586:     PETSC_i = ic;
587: #else
588:     PETSC_i = I;
589: #endif
590:   }

592: #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
593:   MPI_Type_contiguous(2,MPIU_REAL,&MPI_C_DOUBLE_COMPLEX);
594:   MPI_Type_commit(&MPI_C_DOUBLE_COMPLEX);
595:   MPI_Type_contiguous(2,MPI_FLOAT,&MPI_C_COMPLEX);
596:   MPI_Type_commit(&MPI_C_COMPLEX);
597:   MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);
598: #endif
599: #endif

601:   /*
602:      Create the PETSc MPI reduction operator that sums of the first
603:      half of the entries and maxes the second half.
604:   */
605:   MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);

607:   MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);
608:   MPI_Type_commit(&MPIU_2SCALAR);
609:   MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);
610:   MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);

612:   MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);
613:   MPI_Type_commit(&MPIU_2INT);

615:   /*
616:      Attributes to be set on PETSc communicators
617:   */
618:   MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelCounter,&Petsc_Counter_keyval,(void*)0);
619:   MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_InnerComm_keyval,(void*)0);
620:   MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_OuterComm_keyval,(void*)0);

622:   /*
623:      Build the options database
624:   */
625:   PetscOptionsInsert(argc,args,file);

627: 
628:   /*
629:      Print main application help message
630:   */
631:   PetscOptionsHasName(PETSC_NULL,"-help",&flg);
632:   if (help && flg) {
633:     PetscPrintf(PETSC_COMM_WORLD,help);
634:   }
635:   PetscOptionsCheckInitial_Private();
636: 
637:   /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */
638: #if defined(PETSC_USE_LOG)
639:   PetscLogBegin_Private();
640: #endif

642:   /*
643:      Load the dynamic libraries (on machines that support them), this registers all
644:      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
645:   */
646:   PetscInitialize_DynamicLibraries();

648:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
649:   PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);
650:   PetscGetHostName(hostname,256);
651:   PetscInfo1(0,"Running on machine: %s\n",hostname);

653:   PetscOptionsCheckInitial_Components();
654:   /* Check the options database for options related to the options database itself */
655:   PetscOptionsSetFromOptions();

657: #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
658:   /* 
659:       Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI

661:       Currently not used because it is not supported by MPICH.
662:   */
663: #if !defined(PETSC_WORDS_BIGENDIAN)
664:   MPI_Register_datarep((char *)"petsc",PetscDataRep_read_conv_fn,PetscDataRep_write_conv_fn,PetscDataRep_extent_fn,PETSC_NULL);
665: #endif  
666: #endif

668:   PetscOptionsGetInt(PETSC_NULL,"-openmp_spawn_size",&nodesize,&flg);
669:   if (flg) {
670: #if defined(PETSC_HAVE_MPI_COMM_SPAWN)
671:     PetscOpenMPSpawn((PetscMPIInt) nodesize); /* worker nodes never return from here; they go directly to PetscEnd() */
672: #else
673:     SETERRQ(PETSC_ERR_SUP,"PETSc built without MPI 2 (MPI_Comm_spawn) support, use -openmp_merge_size instead");
674: #endif
675:   } else {
676:     PetscOptionsGetInt(PETSC_NULL,"-openmp_merge_size",&nodesize,&flg);
677:     if (flg) {
678:       PetscOpenMPMerge((PetscMPIInt) nodesize,PETSC_NULL,PETSC_NULL);
679:       if (PetscOpenMPWorker) { /* if worker then never enter user code */
680:         PetscEnd();
681:       }
682:     }
683:   }
684:   flg  = PETSC_FALSE;
685:   PetscOptionsGetTruth(PETSC_NULL,"-python",&flg,PETSC_NULL);
686:   if (flg) {PetscPythonInitialize(PETSC_NULL,PETSC_NULL);}

688:   PetscFunctionReturn(ierr);
689: }


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

699:    Collective on PETSC_COMM_WORLD

701:    Options Database Keys:
702: +  -options_table - Calls PetscOptionsPrint()
703: .  -options_left - Prints unused options that remain in the database
704: .  -options_left no - Does not print unused options that remain in the database
705: .  -mpidump - Calls PetscMPIDump()
706: .  -malloc_dump - Calls PetscMallocDump()
707: .  -malloc_info - Prints total memory usage
708: -  -malloc_log - Prints summary of memory usage

710:    Options Database Keys for Profiling:
711:    See the Profiling chapter of the users manual for details.
712: +  -log_summary [filename] - Prints summary of flop and timing
713:         information to screen. If the filename is specified the
714:         summary is written to the file. (for code compiled with 
715:         PETSC_USE_LOG).  See PetscLogPrintSummary().
716: .  -log_all [filename] - Logs extensive profiling information
717:         (for code compiled with PETSC_USE_LOG). See PetscLogDump(). 
718: .  -log [filename] - Logs basic profiline information (for
719:         code compiled with PETSC_USE_LOG).  See PetscLogDump().
720: .  -log_sync - Log the synchronization in scatters, inner products
721:         and norms
722: -  -log_mpe [filename] - Creates a logfile viewable by the 
723:       utility Upshot/Nupshot (in MPICH distribution)

725:    Level: beginner

727:    Note:
728:    See PetscInitialize() for more general runtime options.

730: .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
731: @*/
732: PetscErrorCode  PetscFinalize(void)
733: {
735:   PetscMPIInt    rank;
736:   int            nopt;
737:   PetscTruth     flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
739: 

742:   if (!PetscInitializeCalled) {
743:     (*PetscErrorPrintf)("PetscInitialize() must be called before PetscFinalize()\n");
744:     return(0);
745:   }
746:   PetscInfo(PETSC_NULL,"PetscFinalize() called\n");

748:   PetscOpenMPFinalize();

750:   PetscOptionsGetTruth(PETSC_NULL,"-python",&flg1,PETSC_NULL);
751:   if (flg1) {PetscPythonFinalize();}

753:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
754:   PetscOptionsGetTruth(PETSC_NULL,"-malloc_info",&flg2,PETSC_NULL);
755:   if (!flg2) {
756:     flg2 = PETSC_FALSE;
757:     PetscOptionsGetTruth(PETSC_NULL,"-memory_info",&flg2,PETSC_NULL);
758:   }
759:   if (flg2) {
760:     PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");
761:   }

763:   /*
764:      Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_().
765:   */
766:   PetscObjectRegisterDestroyAll();

768:   /*
769:        Free all the registered create functions, such as KSPList, VecList, SNESList, etc
770:   */
771:   PetscFListDestroyAll();

773:   /* 
774:      Destroy any packages that registered a finalize 
775:   */
776:   PetscRegisterFinalizeAll();

778:   /*
779:      Destroy all the function registration lists created
780:   */
781:   PetscFinalize_DynamicLibraries();

783: #if defined(PETSC_USE_LOG)
784:   flg1 = PETSC_FALSE;
785:   PetscOptionsGetTruth(PETSC_NULL,"-get_total_flops",&flg1,PETSC_NULL);
786:   if (flg1) {
787:     PetscLogDouble flops = 0;
788:     MPI_Reduce(&_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);
789:     PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);
790:   }
791: #endif

793: #if defined(PETSC_USE_DEBUG)
794:   if (PetscStackActive) {
795:     PetscStackDestroy();
796:   }
797: #endif

799: #if defined(PETSC_USE_LOG)
800:   {
801:     char mname[PETSC_MAX_PATH_LEN];
802: #if defined(PETSC_HAVE_MPE)
803:     mname[0] = 0;
804:     PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);
805:     if (flg1){
806:       if (mname[0]) {PetscLogMPEDump(mname);}
807:       else          {PetscLogMPEDump(0);}
808:     }
809: #endif
810:     mname[0] = 0;
811:     PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);
812:     if (flg1) {
813:       if (mname[0])  {PetscLogPrintSummary(PETSC_COMM_WORLD,mname);}
814:       else           {PetscLogPrintSummary(PETSC_COMM_WORLD,0);}
815:     }

817:     PetscOptionsGetString(PETSC_NULL,"-log_detailed",mname,PETSC_MAX_PATH_LEN,&flg1);
818:     if (flg1) {
819:       if (mname[0])  {PetscLogPrintDetailed(PETSC_COMM_WORLD,mname);}
820:       else           {PetscLogPrintDetailed(PETSC_COMM_WORLD,0);}
821:     }

823:     mname[0] = 0;
824:     PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);
825:     PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);
826:     if (flg1 || flg2){
827:       if (mname[0]) PetscLogDump(mname);
828:       else          PetscLogDump(0);
829:     }
830:     PetscLogDestroy();
831:   }
832: #endif
833:   flg1 = PETSC_FALSE;
834:   PetscOptionsGetTruth(PETSC_NULL,"-no_signal_handler",&flg1,PETSC_NULL);
835:   if (!flg1) { PetscPopSignalHandler();}
836:   flg1 = PETSC_FALSE;
837:   PetscOptionsGetTruth(PETSC_NULL,"-mpidump",&flg1,PETSC_NULL);
838:   if (flg1) {
839:     PetscMPIDump(stdout);
840:   }
841:   flg1 = PETSC_FALSE;
842:   flg2 = PETSC_FALSE;
843:   PetscOptionsGetTruth(PETSC_NULL,"-malloc_dump",&flg1,PETSC_NULL);
844:   PetscOptionsGetTruth(PETSC_NULL,"-options_table",&flg2,PETSC_NULL);
845:   if (flg2) {
846:     if (!rank) {PetscOptionsPrint(stdout);}
847:   }

849:   /* to prevent PETSc -options_left from warning */
850:   PetscOptionsHasName(PETSC_NULL,"-nox",&flg1);CHKERRQ(ierr)
851:   PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr)

853:   if (!PetscOpenMPWorker) { /* worker processes skip this because they do not usually process options */
854:     flg3 = PETSC_FALSE; /* default value is required */
855:     PetscOptionsGetTruth(PETSC_NULL,"-options_left",&flg3,&flg1);
856:     PetscOptionsAllUsed(&nopt);
857:     if (flg3) {
858:       if (!flg2) { /* have not yet printed the options */
859:         PetscOptionsPrint(stdout);
860:       }
861:       if (!nopt) {
862:         PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");
863:       } else if (nopt == 1) {
864:         PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");
865:       } else {
866:         PetscPrintf(PETSC_COMM_WORLD,"There are %d unused database options. They are:\n",nopt);
867:       }
868:     }
869: #if defined(PETSC_USE_DEBUG)
870:     if (nopt && !flg3 && !flg1) {
871:       PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");
872:       PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");
873:       PetscOptionsLeft();
874:     } else if (nopt && flg3) {
875: #else 
876:     if (nopt && flg3) {
877: #endif
878:       PetscOptionsLeft();
879:     }
880:   }

882:   flg1 = PETSC_FALSE;
883:   PetscOptionsGetTruth(PETSC_NULL,"-log_history",&flg1,PETSC_NULL);
884:   if (flg1) {
885:     PetscLogCloseHistoryFile(&petsc_history);
886:     petsc_history = 0;
887:   }

889:   PetscInfoAllow(PETSC_FALSE,PETSC_NULL);

891:   flg1 = PETSC_FALSE;
892:   flg3 = PETSC_FALSE;
893:   PetscOptionsGetTruth(PETSC_NULL,"-malloc_dump",&flg1,PETSC_NULL);
894:   PetscOptionsGetTruth(PETSC_NULL,"-malloc_log",&flg3,PETSC_NULL);
895:   if (flg1) {
896:     char fname[PETSC_MAX_PATH_LEN];
897:     FILE *fd;
898:     int  err;

900:     fname[0] = 0;
901:     PetscOptionsGetString(PETSC_NULL,"-malloc_dump",fname,250,&flg1);
902:     if (flg1 && fname[0]) {
903:       char sname[PETSC_MAX_PATH_LEN];

905:       sprintf(sname,"%s_%d",fname,rank);
906:       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
907:       PetscMallocDump(fd);
908:       err = fclose(fd);
909:       if (err) SETERRQ(PETSC_ERR_SYS,"fclose() failed on file");
910:     } else {
911:       MPI_Comm local_comm;

913:       MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);
914:       PetscSequentialPhaseBegin_Private(local_comm,1);
915:         PetscMallocDump(stdout);
916:       PetscSequentialPhaseEnd_Private(local_comm,1);
917:       MPI_Comm_free(&local_comm);
918:     }
919:   }
920:   if (flg3) {
921:     char fname[PETSC_MAX_PATH_LEN];
922:     FILE *fd;
923: 
924:     fname[0] = 0;
925:     PetscOptionsGetString(PETSC_NULL,"-malloc_log",fname,250,&flg1);
926:     if (flg1 && fname[0]) {
927:       char sname[PETSC_MAX_PATH_LEN];
928:       int  err;

930:       sprintf(sname,"%s_%d",fname,rank);
931:       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
932:       PetscMallocDumpLog(fd);
933:       err = fclose(fd);
934:       if (err) SETERRQ(PETSC_ERR_SYS,"fclose() failed on file");
935:     } else {
936:       PetscMallocDumpLog(stdout);
937:     }
938:   }
939:   /* Can be destroyed only after all the options are used */
940:   PetscOptionsDestroy();

942:   PetscGlobalArgc = 0;
943:   PetscGlobalArgs = 0;

945: #if defined(PETSC_USE_COMPLEX)
946: #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
947:   MPI_Op_free(&MPIU_SUM);
948:   MPI_Type_free(&MPI_C_DOUBLE_COMPLEX);
949:   MPI_Type_free(&MPI_C_COMPLEX);
950: #endif
951: #endif
952:   MPI_Type_free(&MPIU_2SCALAR);
953:   MPI_Type_free(&MPIU_2INT);
954:   MPI_Op_free(&PetscMaxSum_Op);
955:   MPI_Op_free(&PetscADMax_Op);
956:   MPI_Op_free(&PetscADMin_Op);

958:   MPI_Keyval_free(&Petsc_Counter_keyval);
959:   MPI_Keyval_free(&Petsc_InnerComm_keyval);
960:   MPI_Keyval_free(&Petsc_OuterComm_keyval);

962:   PetscInfo(0,"PETSc successfully ended!\n");
963:   if (PetscBeganMPI) {
964: #if defined(PETSC_HAVE_MPI_FINALIZED)
965:     PetscMPIInt flag;
966:     MPI_Finalized(&flag);
967:     if (flag) SETERRQ(PETSC_ERR_LIB,"MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()");
968: #endif
969:     MPI_Finalize();
970:   }

972:   if(PETSC_ZOPEFD != NULL){
973:     if (PETSC_ZOPEFD != PETSC_STDOUT) fprintf(PETSC_ZOPEFD, "<<<end>>>");
974:     else fprintf(PETSC_STDOUT, "<<<end>>>");}

976: /*

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

986: */
987:   PetscMallocClear();
988:   PetscInitializeCalled = PETSC_FALSE;
989:   PetscFinalizeCalled   = PETSC_TRUE;
990:   PetscFunctionReturn(ierr);
991: }

993: /*
994:      These may be used in code that ADIC is to be used on
995: */

999: /*@
1000:       PetscGlobalMax - Computes the maximum value over several processors

1002:      Collective on MPI_Comm

1004:    Input Parameters:
1005: +   local - the local value
1006: -   comm - the processors that find the maximum

1008:    Output Parameter:
1009: .   result - the maximum value
1010:   
1011:    Level: intermediate

1013:    Notes:
1014:      These functions are to be used inside user functions that are to be processed with 
1015:    ADIC. PETSc will automatically provide differentiated versions of these functions

1017: .seealso: PetscGlobalMin(), PetscGlobalSum()
1018: @*/
1019: PetscErrorCode  PetscGlobalMax(PetscReal* local,PetscReal* result,MPI_Comm comm)
1020: {
1021:   return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MAX,comm);
1022: }

1026: /*@
1027:       PetscGlobalMin - Computes the minimum value over several processors

1029:      Collective on MPI_Comm

1031:    Input Parameters:
1032: +   local - the local value
1033: -   comm - the processors that find the minimum

1035:    Output Parameter:
1036: .   result - the minimum value
1037:   
1038:    Level: intermediate

1040:    Notes:
1041:      These functions are to be used inside user functions that are to be processed with 
1042:    ADIC. PETSc will automatically provide differentiated versions of these functions

1044: .seealso: PetscGlobalMax(), PetscGlobalSum()
1045: @*/
1046: PetscErrorCode  PetscGlobalMin(PetscReal* local,PetscReal* result,MPI_Comm comm)
1047: {
1048:   return MPI_Allreduce(local,result,1,MPIU_REAL,MPI_MIN,comm);
1049: }

1053: /*@
1054:       PetscGlobalSum - Computes the sum over sever processors

1056:      Collective on MPI_Comm

1058:    Input Parameters:
1059: +   local - the local value
1060: -   comm - the processors that find the sum

1062:    Output Parameter:
1063: .   result - the sum
1064:   
1065:    Level: intermediate

1067:    Notes:
1068:      These functions are to be used inside user functions that are to be processed with 
1069:    ADIC. PETSc will automatically provide differentiated versions of these functions

1071: .seealso: PetscGlobalMin(), PetscGlobalMax()
1072: @*/
1073: PetscErrorCode  PetscGlobalSum(PetscScalar* local,PetscScalar* result,MPI_Comm comm)
1074: {
1075:   return MPI_Allreduce(local,result,1,MPIU_SCALAR,MPIU_SUM,comm);
1076: }