Actual source code: pinit.c

petsc-master 2019-09-20
Report Typos and Errors

  2: /*
  3:    This file defines the initialization of PETSc, including PetscInitialize()
  4: */
  5:  #include <petsc/private/petscimpl.h>
  6:  #include <petscvalgrind.h>
  7:  #include <petscviewer.h>

  9: #if defined(PETSC_USE_LOG)
 10: PETSC_INTERN PetscErrorCode PetscLogFinalize(void);
 11: #endif

 13: #if defined(PETSC_SERIALIZE_FUNCTIONS)
 14: PETSC_INTERN PetscFPT PetscFPTData;
 15: PetscFPT PetscFPTData = 0;
 16: #endif

 18: #if defined(PETSC_HAVE_SAWS)
 19:  #include <petscviewersaws.h>
 20: #endif
 21: /* -----------------------------------------------------------------------------------------*/

 23: PETSC_INTERN FILE *petsc_history;

 25: PETSC_INTERN PetscErrorCode PetscInitialize_DynamicLibraries(void);
 26: PETSC_INTERN PetscErrorCode PetscFinalize_DynamicLibraries(void);
 27: PETSC_INTERN PetscErrorCode PetscFunctionListPrintAll(void);
 28: PETSC_INTERN PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int);
 29: PETSC_INTERN PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int);
 30: PETSC_INTERN PetscErrorCode PetscCloseHistoryFile(FILE**);

 32: /* user may set this BEFORE calling PetscInitialize() */
 33: MPI_Comm PETSC_COMM_WORLD = MPI_COMM_NULL;

 35: PetscMPIInt Petsc_Counter_keyval   = MPI_KEYVAL_INVALID;
 36: PetscMPIInt Petsc_InnerComm_keyval = MPI_KEYVAL_INVALID;
 37: PetscMPIInt Petsc_OuterComm_keyval = MPI_KEYVAL_INVALID;
 38: PetscMPIInt Petsc_ShmComm_keyval   = MPI_KEYVAL_INVALID;

 40: /*
 41:      Declare and set all the string names of the PETSc enums
 42: */
 43: const char *const PetscBools[]     = {"FALSE","TRUE","PetscBool","PETSC_",0};
 44: const char *const PetscCopyModes[] = {"COPY_VALUES","OWN_POINTER","USE_POINTER","PetscCopyMode","PETSC_",0};

 46: PetscBool PetscPreLoadingUsed = PETSC_FALSE;
 47: PetscBool PetscPreLoadingOn   = PETSC_FALSE;

 49: PetscInt PetscHotRegionDepth;

 51: #if defined(PETSC_HAVE_THREADSAFETY)
 52: PetscSpinlock PetscViewerASCIISpinLockOpen;
 53: PetscSpinlock PetscViewerASCIISpinLockStdout;
 54: PetscSpinlock PetscViewerASCIISpinLockStderr;
 55: PetscSpinlock PetscCommSpinLock;
 56: #endif

 58: /*
 59:        Checks the options database for initializations related to the
 60:     PETSc components
 61: */
 62: PETSC_INTERN PetscErrorCode  PetscOptionsCheckInitial_Components(void)
 63: {
 64:   PetscBool      flg;

 68:   PetscOptionsHasHelp(NULL,&flg);
 69:   if (flg) {
 70: #if defined(PETSC_USE_LOG)
 71:     MPI_Comm comm = PETSC_COMM_WORLD;
 72:     (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");
 73:     (*PetscHelpPrintf)(comm," -log_exclude: <vec,mat,pc,ksp,snes,tao,ts>\n");
 74:     (*PetscHelpPrintf)(comm," -info_exclude: <null,vec,mat,pc,ksp,snes,tao,ts>\n");
 75:     (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
 76: #endif
 77:   }
 78:   return(0);
 79: }

 81: /*
 82:       PetscInitializeNoPointers - Calls PetscInitialize() from C/C++ without the pointers to argc and args

 84:    Collective

 86:    Level: advanced

 88:     Notes:
 89:     this is called only by the PETSc Julia interface. Even though it might start MPI it sets the flag to
 90:      indicate that it did NOT start MPI so that the PetscFinalize() does not end MPI, thus allowing PetscInitialize() to
 91:      be called multiple times from Julia without the problem of trying to initialize MPI more than once.

 93:      Developer Note: Turns off PETSc signal handling to allow Julia to manage signals

 95: .seealso: PetscInitialize(), PetscInitializeFortran(), PetscInitializeNoArguments()
 96: */
 97: PetscErrorCode  PetscInitializeNoPointers(int argc,char **args,const char *filename,const char *help)
 98: {
100:   int            myargc   = argc;
101:   char           **myargs = args;

104:   PetscInitialize(&myargc,&myargs,filename,help);if (ierr) return ierr;
105:   PetscPopSignalHandler();
106:   PetscBeganMPI = PETSC_FALSE;
107:   PetscFunctionReturn(ierr);
108: }

110: /*
111:       Used by Julia interface to get communicator
112: */
113: PetscErrorCode  PetscGetPETSC_COMM_SELF(MPI_Comm *comm)
114: {
116:   *comm = PETSC_COMM_SELF;
117:   return(0);
118: }

120: /*@C
121:       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
122:         the command line arguments.

124:    Collective

126:    Level: advanced

128: .seealso: PetscInitialize(), PetscInitializeFortran()
129: @*/
130: PetscErrorCode  PetscInitializeNoArguments(void)
131: {
133:   int            argc   = 0;
134:   char           **args = 0;

137:   PetscInitialize(&argc,&args,NULL,NULL);
138:   PetscFunctionReturn(ierr);
139: }

141: /*@
142:       PetscInitialized - Determine whether PETSc is initialized.

144:    Level: beginner

146: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
147: @*/
148: PetscErrorCode PetscInitialized(PetscBool  *isInitialized)
149: {
150:   *isInitialized = PetscInitializeCalled;
151:   return 0;
152: }

154: /*@
155:       PetscFinalized - Determine whether PetscFinalize() has been called yet

157:    Level: developer

159: .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
160: @*/
161: PetscErrorCode  PetscFinalized(PetscBool  *isFinalized)
162: {
163:   *isFinalized = PetscFinalizeCalled;
164:   return 0;
165: }

167: PETSC_INTERN PetscErrorCode PetscOptionsCheckInitial_Private(void);

169: /*
170:        This function is the MPI reduction operation used to compute the sum of the
171:    first half of the datatype and the max of the second half.
172: */
173: MPI_Op MPIU_MAXSUM_OP = 0;

175: PETSC_INTERN void MPIAPI MPIU_MaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
176: {
177:   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;

180:   if (*datatype != MPIU_2INT) {
181:     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
182:     MPI_Abort(MPI_COMM_WORLD,1);
183:   }

185:   for (i=0; i<count; i++) {
186:     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
187:     xout[2*i+1] += xin[2*i+1];
188:   }
189:   PetscFunctionReturnVoid();
190: }

192: /*
193:     Returns the max of the first entry owned by this processor and the
194: sum of the second entry.

196:     The reason sizes[2*i] contains lengths sizes[2*i+1] contains flag of 1 if length is nonzero
197: is so that the MPIU_MAXSUM_OP() can set TWO values, if we passed in only sizes[i] with lengths
198: there would be no place to store the both needed results.
199: */
200: PetscErrorCode  PetscMaxSum(MPI_Comm comm,const PetscInt sizes[],PetscInt *max,PetscInt *sum)
201: {

205: #if defined(PETSC_HAVE_MPI_REDUCE_SCATTER_BLOCK)
206:   {
207:     struct {PetscInt max,sum;} work;
208:     MPI_Reduce_scatter_block((void*)sizes,&work,1,MPIU_2INT,MPIU_MAXSUM_OP,comm);
209:     *max = work.max;
210:     *sum = work.sum;
211:   }
212: #else
213:   {
214:     PetscMPIInt    size,rank;
215:     struct {PetscInt max,sum;} *work;
216:     MPI_Comm_size(comm,&size);
217:     MPI_Comm_rank(comm,&rank);
218:     PetscMalloc1(size,&work);
219:     MPIU_Allreduce((void*)sizes,work,size,MPIU_2INT,MPIU_MAXSUM_OP,comm);
220:     *max = work[rank].max;
221:     *sum = work[rank].sum;
222:     PetscFree(work);
223:   }
224: #endif
225:   return(0);
226: }

228: /* ----------------------------------------------------------------------------*/

230: #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
231: MPI_Op MPIU_SUM = 0;

233: PETSC_EXTERN void PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
234: {
235:   PetscInt i,count = *cnt;

238:   if (*datatype == MPIU_REAL) {
239:     PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
240:     for (i=0; i<count; i++) xout[i] += xin[i];
241:   }
242: #if defined(PETSC_HAVE_COMPLEX)
243:   else if (*datatype == MPIU_COMPLEX) {
244:     PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
245:     for (i=0; i<count; i++) xout[i] += xin[i];
246:   }
247: #endif
248:   else {
249:     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types");
250:     MPI_Abort(MPI_COMM_WORLD,1);
251:   }
252:   PetscFunctionReturnVoid();
253: }
254: #endif

256: #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
257: MPI_Op MPIU_MAX = 0;
258: MPI_Op MPIU_MIN = 0;

260: PETSC_EXTERN void PetscMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
261: {
262:   PetscInt i,count = *cnt;

265:   if (*datatype == MPIU_REAL) {
266:     PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
267:     for (i=0; i<count; i++) xout[i] = PetscMax(xout[i],xin[i]);
268:   }
269: #if defined(PETSC_HAVE_COMPLEX)
270:   else if (*datatype == MPIU_COMPLEX) {
271:     PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
272:     for (i=0; i<count; i++) {
273:       xout[i] = PetscRealPartComplex(xout[i])<PetscRealPartComplex(xin[i]) ? xin[i] : xout[i];
274:     }
275:   }
276: #endif
277:   else {
278:     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types");
279:     MPI_Abort(MPI_COMM_WORLD,1);
280:   }
281:   PetscFunctionReturnVoid();
282: }

284: PETSC_EXTERN void PetscMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
285: {
286:   PetscInt    i,count = *cnt;

289:   if (*datatype == MPIU_REAL) {
290:     PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
291:     for (i=0; i<count; i++) xout[i] = PetscMin(xout[i],xin[i]);
292:   }
293: #if defined(PETSC_HAVE_COMPLEX)
294:   else if (*datatype == MPIU_COMPLEX) {
295:     PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
296:     for (i=0; i<count; i++) {
297:       xout[i] = PetscRealPartComplex(xout[i])>PetscRealPartComplex(xin[i]) ? xin[i] : xout[i];
298:     }
299:   }
300: #endif
301:   else {
302:     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_SCALAR data (i.e. double or complex) types");
303:     MPI_Abort(MPI_COMM_WORLD,1);
304:   }
305:   PetscFunctionReturnVoid();
306: }
307: #endif

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

312:    This is called by MPI, not by users. This is called by MPI_Comm_free() when the communicator that has this  data as an attribute is freed.

314:    Note: this is declared extern "C" because it is passed to MPI_Comm_create_keyval()

316: */
317: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelCounter(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
318: {

322:   PetscInfo1(0,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);CHKERRMPI(ierr);
323:   PetscFree(count_val);CHKERRMPI(ierr);
324:   PetscFunctionReturn(MPI_SUCCESS);
325: }

327: /*
328:   This is invoked on the outer comm as a result of either PetscCommDestroy() (via MPI_Comm_delete_attr) or when the user
329:   calls MPI_Comm_free().

331:   This is the only entry point for breaking the links between inner and outer comms.

333:   This is called by MPI, not by users. This is called when MPI_Comm_free() is called on the communicator.

335:   Note: this is declared extern "C" because it is passed to MPI_Comm_create_keyval()

337: */
338: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelComm_Outer(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
339: {
340:   PetscErrorCode                    ierr;
341:   PetscMPIInt                       flg;
342:   union {MPI_Comm comm; void *ptr;} icomm,ocomm;

345:   if (keyval != Petsc_InnerComm_keyval) SETERRMPI(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Unexpected keyval");
346:   icomm.ptr = attr_val;

348:   MPI_Comm_get_attr(icomm.comm,Petsc_OuterComm_keyval,&ocomm,&flg);CHKERRMPI(ierr);
349:   if (!flg) SETERRMPI(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected reference to outer comm");
350:   if (ocomm.comm != comm) SETERRMPI(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm has reference to non-matching outer comm");
351:   MPI_Comm_delete_attr(icomm.comm,Petsc_OuterComm_keyval);CHKERRMPI(ierr);
352:   PetscInfo1(0,"User MPI_Comm %ld is being freed after removing reference from inner PETSc comm to this outer comm\n",(long)comm);CHKERRMPI(ierr);
353:   PetscFunctionReturn(MPI_SUCCESS);
354: }

356: /*
357:  * This is invoked on the inner comm when Petsc_DelComm_Outer calls MPI_Comm_delete_attr.  It should not be reached any other way.
358:  */
359: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelComm_Inner(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
360: {

364:   PetscInfo1(0,"Removing reference to PETSc communicator embedded in a user MPI_Comm %ld\n",(long)comm);CHKERRMPI(ierr);
365:   PetscFunctionReturn(MPI_SUCCESS);
366: }

368: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelComm_Shm(MPI_Comm,PetscMPIInt,void *,void *);

370: #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
371: PETSC_EXTERN PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype,MPI_Aint*,void*);
372: PETSC_EXTERN PetscMPIInt PetscDataRep_read_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
373: PETSC_EXTERN PetscMPIInt PetscDataRep_write_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
374: #endif

376: PetscMPIInt PETSC_MPI_ERROR_CLASS,PETSC_MPI_ERROR_CODE;

378: PETSC_INTERN int  PetscGlobalArgc;
379: PETSC_INTERN char **PetscGlobalArgs;
380: int  PetscGlobalArgc   = 0;
381: char **PetscGlobalArgs = 0;
382: PetscSegBuffer PetscCitationsList;

384: PetscErrorCode PetscCitationsInitialize(void)
385: {

389:   PetscSegBufferCreate(1,10000,&PetscCitationsList);
390:   PetscCitationsRegister("@TechReport{petsc-user-ref,\n  Author = {Satish Balay and Shrirang Abhyankar and Mark F. Adams and Jed Brown \n            and Peter Brune and Kris Buschelman and Lisandro Dalcin and\n            Victor Eijkhout and William D. Gropp and Dmitry Karpeyev and\n            Dinesh Kaushik and Matthew G. Knepley and Dave A. May and Lois Curfman McInnes\n            and Richard Tran Mills and Todd Munson and Karl Rupp and Patrick Sanan\n            and Barry F. Smith and Stefano Zampini and Hong Zhang and Hong Zhang},\n  Title = {{PETS}c Users Manual},\n  Number = {ANL-95/11 - Revision 3.11},\n  Institution = {Argonne National Laboratory},\n  Year = {2019}\n}\n",NULL);
391:   PetscCitationsRegister("@InProceedings{petsc-efficient,\n  Author = {Satish Balay and William D. Gropp and Lois Curfman McInnes and Barry F. Smith},\n  Title = {Efficient Management of Parallelism in Object Oriented Numerical Software Libraries},\n  Booktitle = {Modern Software Tools in Scientific Computing},\n  Editor = {E. Arge and A. M. Bruaset and H. P. Langtangen},\n  Pages = {163--202},\n  Publisher = {Birkh{\\\"{a}}user Press},\n  Year = {1997}\n}\n",NULL);
392:   return(0);
393: }

395: static char programname[PETSC_MAX_PATH_LEN] = ""; /* HP includes entire path in name */

397: PetscErrorCode  PetscSetProgramName(const char name[])
398: {

402:   PetscStrncpy(programname,name,PETSC_MAX_PATH_LEN);
403:   return(0);
404: }

406: /*@C
407:     PetscGetProgramName - Gets the name of the running program.

409:     Not Collective

411:     Input Parameter:
412: .   len - length of the string name

414:     Output Parameter:
415: .   name - the name of the running program

417:    Level: advanced

419:     Notes:
420:     The name of the program is copied into the user-provided character
421:     array of length len.  On some machines the program name includes
422:     its entire path, so one should generally set len >= PETSC_MAX_PATH_LEN.
423: @*/
424: PetscErrorCode  PetscGetProgramName(char name[],size_t len)
425: {

429:    PetscStrncpy(name,programname,len);
430:   return(0);
431: }

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

437:    Not Collective

439:    Output Parameters:
440: +  argc - count of number of command line arguments
441: -  args - the command line arguments

443:    Level: intermediate

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

449:       The first argument contains the program name as is normal for C arguments.

451: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()

453: @*/
454: PetscErrorCode  PetscGetArgs(int *argc,char ***args)
455: {
457:   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
458:   *argc = PetscGlobalArgc;
459:   *args = PetscGlobalArgs;
460:   return(0);
461: }

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

467:    Not Collective

469:    Output Parameters:
470: .  args - the command line arguments

472:    Level: intermediate

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

477: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()

479: @*/
480: PetscErrorCode  PetscGetArguments(char ***args)
481: {
482:   PetscInt       i,argc = PetscGlobalArgc;

486:   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
487:   if (!argc) {*args = NULL; return(0);}
488:   PetscMalloc1(argc,args);
489:   for (i=0; i<argc-1; i++) {
490:     PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);
491:   }
492:   (*args)[argc-1] = NULL;
493:   return(0);
494: }

496: /*@C
497:    PetscFreeArguments - Frees the memory obtained with PetscGetArguments()

499:    Not Collective

501:    Output Parameters:
502: .  args - the command line arguments

504:    Level: intermediate

506: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()

508: @*/
509: PetscErrorCode  PetscFreeArguments(char **args)
510: {
511:   PetscInt       i = 0;

515:   if (!args) return(0);
516:   while (args[i]) {
517:     PetscFree(args[i]);
518:     i++;
519:   }
520:   PetscFree(args);
521:   return(0);
522: }

524: #if defined(PETSC_HAVE_SAWS)
525: #include <petscconfiginfo.h>

527: PETSC_INTERN PetscErrorCode PetscInitializeSAWs(const char help[])
528: {
529:   if (!PetscGlobalRank) {
530:     char           cert[PETSC_MAX_PATH_LEN],root[PETSC_MAX_PATH_LEN],*intro,programname[64],*appline,*options,version[64];
531:     int            port;
532:     PetscBool      flg,rootlocal = PETSC_FALSE,flg2,selectport = PETSC_FALSE;
533:     size_t         applinelen,introlen;
535:     char           sawsurl[256];

537:     PetscOptionsHasName(NULL,NULL,"-saws_log",&flg);
538:     if (flg) {
539:       char  sawslog[PETSC_MAX_PATH_LEN];

541:       PetscOptionsGetString(NULL,NULL,"-saws_log",sawslog,PETSC_MAX_PATH_LEN,NULL);
542:       if (sawslog[0]) {
543:         PetscStackCallSAWs(SAWs_Set_Use_Logfile,(sawslog));
544:       } else {
545:         PetscStackCallSAWs(SAWs_Set_Use_Logfile,(NULL));
546:       }
547:     }
548:     PetscOptionsGetString(NULL,NULL,"-saws_https",cert,PETSC_MAX_PATH_LEN,&flg);
549:     if (flg) {
550:       PetscStackCallSAWs(SAWs_Set_Use_HTTPS,(cert));
551:     }
552:     PetscOptionsGetBool(NULL,NULL,"-saws_port_auto_select",&selectport,NULL);
553:     if (selectport) {
554:         PetscStackCallSAWs(SAWs_Get_Available_Port,(&port));
555:         PetscStackCallSAWs(SAWs_Set_Port,(port));
556:     } else {
557:       PetscOptionsGetInt(NULL,NULL,"-saws_port",&port,&flg);
558:       if (flg) {
559:         PetscStackCallSAWs(SAWs_Set_Port,(port));
560:       }
561:     }
562:     PetscOptionsGetString(NULL,NULL,"-saws_root",root,PETSC_MAX_PATH_LEN,&flg);
563:     if (flg) {
564:       PetscStackCallSAWs(SAWs_Set_Document_Root,(root));
565:       PetscStrcmp(root,".",&rootlocal);
566:     } else {
567:       PetscOptionsHasName(NULL,NULL,"-saws_options",&flg);
568:       if (flg) {
569:         PetscStrreplace(PETSC_COMM_WORLD,"${PETSC_DIR}/share/petsc/saws",root,PETSC_MAX_PATH_LEN);
570:         PetscStackCallSAWs(SAWs_Set_Document_Root,(root));
571:       }
572:     }
573:     PetscOptionsHasName(NULL,NULL,"-saws_local",&flg2);
574:     if (flg2) {
575:       char jsdir[PETSC_MAX_PATH_LEN];
576:       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"-saws_local option requires -saws_root option");
577:       PetscSNPrintf(jsdir,PETSC_MAX_PATH_LEN,"%s/js",root);
578:       PetscTestDirectory(jsdir,'r',&flg);
579:       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"-saws_local option requires js directory in root directory");
580:       PetscStackCallSAWs(SAWs_Push_Local_Header,());
581:     }
582:     PetscGetProgramName(programname,64);
583:     PetscStrlen(help,&applinelen);
584:     introlen   = 4096 + applinelen;
585:     applinelen += 1024;
586:     PetscMalloc(applinelen,&appline);
587:     PetscMalloc(introlen,&intro);

589:     if (rootlocal) {
590:       PetscSNPrintf(appline,applinelen,"%s.c.html",programname);
591:       PetscTestFile(appline,'r',&rootlocal);
592:     }
593:     PetscOptionsGetAll(NULL,&options);
594:     if (rootlocal && help) {
595:       PetscSNPrintf(appline,applinelen,"<center> Running <a href=\"%s.c.html\">%s</a> %s</center><br><center><pre>%s</pre></center><br>\n",programname,programname,options,help);
596:     } else if (help) {
597:       PetscSNPrintf(appline,applinelen,"<center>Running %s %s</center><br><center><pre>%s</pre></center><br>",programname,options,help);
598:     } else {
599:       PetscSNPrintf(appline,applinelen,"<center> Running %s %s</center><br>\n",programname,options);
600:     }
601:     PetscFree(options);
602:     PetscGetVersion(version,sizeof(version));
603:     PetscSNPrintf(intro,introlen,"<body>\n"
604:                                     "<center><h2> <a href=\"https://www.mcs.anl.gov/petsc\">PETSc</a> Application Web server powered by <a href=\"https://bitbucket.org/saws/saws\">SAWs</a> </h2></center>\n"
605:                                     "<center>This is the default PETSc application dashboard, from it you can access any published PETSc objects or logging data</center><br><center>%s configured with %s</center><br>\n"
606:                                     "%s",version,petscconfigureoptions,appline);
607:     PetscStackCallSAWs(SAWs_Push_Body,("index.html",0,intro));
608:     PetscFree(intro);
609:     PetscFree(appline);
610:     if (selectport) {
611:       PetscBool silent;

613:       SAWs_Initialize();
614:       /* another process may have grabbed the port so keep trying */
615:       while (ierr) {
616:         PetscStackCallSAWs(SAWs_Get_Available_Port,(&port));
617:         PetscStackCallSAWs(SAWs_Set_Port,(port));
618:         SAWs_Initialize();
619:       }

621:       PetscOptionsGetBool(NULL,NULL,"-saws_port_auto_select_silent",&silent,NULL);
622:       if (!silent) {
623:         PetscStackCallSAWs(SAWs_Get_FullURL,(sizeof(sawsurl),sawsurl));
624:         PetscPrintf(PETSC_COMM_WORLD,"Point your browser to %s for SAWs\n",sawsurl);
625:       }
626:     } else {
627:       PetscStackCallSAWs(SAWs_Initialize,());
628:     }
629:     PetscCitationsRegister("@TechReport{ saws,\n"
630:                                   "  Author = {Matt Otten and Jed Brown and Barry Smith},\n"
631:                                   "  Title  = {Scientific Application Web Server (SAWs) Users Manual},\n"
632:                                   "  Institution = {Argonne National Laboratory},\n"
633:                                   "  Year   = 2013\n}\n",NULL);
634:   }
635:   return(0);
636: }
637: #endif

639: /* Things must be done before MPI_Init() when MPI is not yet initialized, and can be shared between C init and Fortran init */
640: PETSC_INTERN PetscErrorCode PetscPreMPIInit_Private(void)
641: {
643: #if defined(PETSC_HAVE_HWLOC_SOLARIS_BUG)
644:     /* see MPI.py for details on this bug */
645:     (void) setenv("HWLOC_COMPONENTS","-x86",1);
646: #endif
647:   return(0);
648: }

650: #if defined(PETSC_HAVE_ADIOS)
651: #include <adios.h>
652: #include <adios_read.h>
653: int64_t Petsc_adios_group;
654: #endif
655: #if defined(PETSC_HAVE_ADIOS2)
656: #include <adios2_c.h>
657: #endif
658: #if defined(PETSC_HAVE_OPENMP)
659: #include <omp.h>
660: PetscInt PetscNumOMPThreads;
661: #endif

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

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

671:    Input Parameters:
672: +  argc - count of number of command line arguments
673: .  args - the command line arguments
674: .  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use NULL to not check for
675:           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
676: -  help - [optional] Help message to print, use NULL for no message

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

684:    Options Database Keys:
685: +  -help [intro] - prints help method for each option; if intro is given the program stops after printing the introductory help message
686: .  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
687: .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
688: .  -on_error_emacs <machinename> - causes emacsclient to jump to error file
689: .  -on_error_abort - calls abort() when error detected (no traceback)
690: .  -on_error_mpiabort - calls MPI_abort() when error detected
691: .  -error_output_stderr - prints error messages to stderr instead of the default stdout
692: .  -error_output_none - does not print the error messages (but handles errors in the same way as if this was not called)
693: .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
694: .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
695: .  -stop_for_debugger - Print message on how to attach debugger manually to
696:                         process and wait (-debugger_pause) seconds for attachment
697: .  -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries) (deprecated, use -malloc_debug)
698: .  -malloc no - Indicates not to use error-checking malloc (deprecated, use -malloc_debug no)
699: .  -malloc_debug - check for memory corruption at EVERY malloc or free, see PetscMallocSetDebug()
700: .  -malloc_dump - prints a list of all unfreed memory at the end of the run
701: .  -malloc_test - like -malloc_dump -malloc_debug, but only active for debugging builds, ignored in optimized build. May want to set in PETSC_OPTIONS environmental variable
702: .  -malloc_view - show a list of all allocated memory during PetscFinalize()
703: .  -malloc_view_threshold <t> - only list memory allocations of size greater than t with -malloc_view
704: .  -fp_trap - Stops on floating point exceptions
705: .  -no_signal_handler - Indicates not to trap error signals
706: .  -shared_tmp - indicates /tmp directory is shared by all processors
707: .  -not_shared_tmp - each processor has own /tmp
708: .  -tmp - alternative name of /tmp directory
709: .  -get_total_flops - returns total flops done by all processors
710: -  -memory_view - Print memory usage at end of run

712:    Options Database Keys for Profiling:
713:    See Users-Manual: Chapter 13 Profiling for details.
714: +  -info <optional filename> - Prints verbose information to the screen
715: .  -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages
716: .  -log_sync - Enable barrier synchronization for all events. This option is useful to debug imbalance within each event,
717:         however it slows things down and gives a distorted view of the overall runtime.
718: .  -log_trace [filename] - Print traces of all PETSc calls to the screen (useful to determine where a program
719:         hangs without running in the debugger).  See PetscLogTraceBegin().
720: .  -log_view [:filename:format] - Prints summary of flop and timing information to screen or file, see PetscLogView().
721: .  -log_view_memory - Includes in the summary from -log_view the memory used in each method, see PetscLogView().
722: .  -log_summary [filename] - (Deprecated, use -log_view) Prints summary of flop and timing information to screen. If the filename is specified the
723:         summary is written to the file.  See PetscLogView().
724: .  -log_exclude: <vec,mat,pc,ksp,snes> - excludes subset of object classes from logging
725: .  -log_all [filename] - Logs extensive profiling information  See PetscLogDump().
726: .  -log [filename] - Logs basic profiline information  See PetscLogDump().
727: .  -log_mpe [filename] - Creates a logfile viewable by the utility Jumpshot (in MPICH distribution)
728: .  -viewfromoptions on,off - Enable or disable XXXSetFromOptions() calls, for applications with many small solves turn this off
729: -  -check_pointer_intensity 0,1,2 - if pointers are checked for validity (debug version only), using 0 will result in faster code

731:     Only one of -log_trace, -log_view, -log_view, -log_all, -log, or -log_mpe may be used at a time

733:    Options Database Keys for SAWs:
734: +  -saws_port <portnumber> - port number to publish SAWs data, default is 8080
735: .  -saws_port_auto_select - have SAWs select a new unique port number where it publishes the data, the URL is printed to the screen
736:                             this is useful when you are running many jobs that utilize SAWs at the same time
737: .  -saws_log <filename> - save a log of all SAWs communication
738: .  -saws_https <certificate file> - have SAWs use HTTPS instead of HTTP
739: -  -saws_root <directory> - allow SAWs to have access to the given directory to search for requested resources and files

741:    Environmental Variables:
742: +   PETSC_TMP - alternative tmp directory
743: .   PETSC_SHARED_TMP - tmp is shared by all processes
744: .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
745: .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
746: -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to


749:    Level: beginner

751:    Notes:
752:    If for some reason you must call MPI_Init() separately, call
753:    it before PetscInitialize().

755:    Fortran Version:
756:    In Fortran this routine has the format
757: $       call PetscInitialize(file,ierr)

759: +   ierr - error return code
760: -  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL_CHARACTER to not check for
761:           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files

763:    Important Fortran Note:
764:    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a