Actual source code: init.c

  1: /*

  3:    This file defines part of the initialization of PETSc

  5:   This file uses regular malloc and free because it cannot be known
  6:   what malloc is being used until it has already processed the input.
  7: */
  8: #include <petsc/private/petscimpl.h>
  9: #include <petsc/private/logimpl.h>

 11: #if defined(PETSC_HAVE_UNISTD_H)
 12:   #include <unistd.h>
 13: #endif

 15: /* ------------------------Nasty global variables -------------------------------*/
 16: /*
 17:      Indicates if PETSc started up MPI, or it was
 18:    already started before PETSc was initialized.
 19: */
 20: PetscBool PetscBeganMPI                 = PETSC_FALSE;
 21: PetscBool PetscErrorHandlingInitialized = PETSC_FALSE;
 22: PetscBool PetscInitializeCalled         = PETSC_FALSE;
 23: PetscBool PetscFinalizeCalled           = PETSC_FALSE;

 25: PetscMPIInt PetscGlobalRank = -1;
 26: PetscMPIInt PetscGlobalSize = -1;

 28: #if defined(PETSC_HAVE_KOKKOS)
 29: PetscBool PetscBeganKokkos = PETSC_FALSE;
 30: #endif

 32: #if defined(PETSC_HAVE_NVSHMEM)
 33: PetscBool PetscBeganNvshmem       = PETSC_FALSE;
 34: PetscBool PetscNvshmemInitialized = PETSC_FALSE;
 35: #endif

 37: PetscBool use_gpu_aware_mpi = PetscDefined(HAVE_MPIUNI) ? PETSC_FALSE : PETSC_TRUE;

 39: PetscBool PetscPrintFunctionList = PETSC_FALSE;

 41: #if defined(PETSC_HAVE_COMPLEX)
 42:   #if defined(PETSC_COMPLEX_INSTANTIATE)
 43: template <>
 44: class std::complex<double>; /* instantiate complex template class */
 45:   #endif

 47: /*MC
 48:    PETSC_i - the imaginary number i

 50:    Synopsis:
 51: #include <petscsys.h>
 52:    PetscComplex PETSC_i;

 54:    Level: beginner

 56:    Note:
 57:    Complex numbers are automatically available if PETSc located a working complex implementation

 59: .seealso: `PetscRealPart()`, `PetscImaginaryPart()`, `PetscRealPartComplex()`, `PetscImaginaryPartComplex()`
 60: M*/
 61: PetscComplex PETSC_i;
 62: MPI_Datatype MPIU___COMPLEX128 = 0;
 63: #endif /* PETSC_HAVE_COMPLEX */
 64: #if defined(PETSC_HAVE_REAL___FLOAT128)
 65: MPI_Datatype MPIU___FLOAT128 = 0;
 66: #endif
 67: #if defined(PETSC_HAVE_REAL___FP16)
 68: MPI_Datatype MPIU___FP16 = 0;
 69: #endif
 70: MPI_Datatype MPIU_2SCALAR    = 0;
 71: MPI_Datatype MPIU_REAL_INT   = 0;
 72: MPI_Datatype MPIU_SCALAR_INT = 0;
 73: #if defined(PETSC_USE_64BIT_INDICES)
 74: MPI_Datatype MPIU_2INT       = 0;
 75: MPI_Datatype MPIU_INT_MPIINT = 0;
 76: #endif
 77: MPI_Datatype MPI_4INT  = 0;
 78: MPI_Datatype MPIU_4INT = 0;
 79: MPI_Datatype MPIU_BOOL;
 80: MPI_Datatype MPIU_ENUM;
 81: MPI_Datatype MPIU_FORTRANADDR;
 82: MPI_Datatype MPIU_SIZE_T;

 84: /*
 85:        Function that is called to display all error messages
 86: */
 87: PetscErrorCode (*PetscErrorPrintf)(const char[], ...)          = PetscErrorPrintfDefault;
 88: PetscErrorCode (*PetscHelpPrintf)(MPI_Comm, const char[], ...) = PetscHelpPrintfDefault;
 89: PetscErrorCode (*PetscVFPrintf)(FILE *, const char[], va_list) = PetscVFPrintfDefault;

 91: /*
 92:    Optional file where all PETSc output from various prints is saved
 93: */
 94: PETSC_INTERN FILE *petsc_history;
 95: FILE              *petsc_history = NULL;

 97: static PetscErrorCode PetscOpenHistoryFile(const char filename[], FILE **fd)
 98: {
 99:   PetscMPIInt rank, size;
100:   char        pfile[PETSC_MAX_PATH_LEN], pname[PETSC_MAX_PATH_LEN], fname[PETSC_MAX_PATH_LEN], date[64];
101:   char        version[256];

103:   PetscFunctionBegin;
104:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
105:   if (rank == 0) {
106:     char arch[10];

108:     PetscCall(PetscGetArchType(arch, 10));
109:     PetscCall(PetscGetDate(date, 64));
110:     PetscCall(PetscGetVersion(version, 256));
111:     PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
112:     if (filename) {
113:       PetscCall(PetscFixFilename(filename, fname));
114:     } else {
115:       PetscCall(PetscGetHomeDirectory(pfile, sizeof(pfile)));
116:       PetscCall(PetscStrlcat(pfile, "/.petschistory", sizeof(pfile)));
117:       PetscCall(PetscFixFilename(pfile, fname));
118:     }

120:     *fd = fopen(fname, "a");
121:     PetscCheck(fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open file: %s", fname);

123:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
124:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "%s %s\n", version, date));
125:     PetscCall(PetscGetProgramName(pname, sizeof(pname)));
126:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "%s on a %s, %d proc. with options:\n", pname, arch, size));
127:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));

129:     PetscCall(PetscFFlush(*fd));
130:   }
131:   PetscFunctionReturn(PETSC_SUCCESS);
132: }

134: PETSC_INTERN PetscErrorCode PetscCloseHistoryFile(FILE **fd)
135: {
136:   PetscMPIInt rank;

138:   PetscFunctionBegin;
139:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
140:   if (rank == 0) {
141:     char date[64];
142:     int  err;

144:     PetscCall(PetscGetDate(date, sizeof(date)));
145:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
146:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "Finished at %s\n", date));
147:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
148:     PetscCall(PetscFFlush(*fd));
149:     err = fclose(*fd);
150:     PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_SYS, "fclose() failed on file");
151:   }
152:   PetscFunctionReturn(PETSC_SUCCESS);
153: }

155: /*
156:    This is ugly and probably belongs somewhere else, but I want to
157:   be able to put a true MPI abort error handler with command line args.

159:     This is so MPI errors in the debugger will leave all the stack
160:   frames. The default MP_Abort() cleans up and exits thus providing no useful information
161:   in the debugger hence we call abort() instead of MPI_Abort().
162: */

164: static void Petsc_MPI_AbortOnError(PETSC_UNUSED MPI_Comm *comm, PetscMPIInt *flag, ...)
165: {
166:   PetscFunctionBegin;
167:   PetscCallContinue((*PetscErrorPrintf)("MPI error %d\n", *flag));
168:   abort();
169: }

171: static void Petsc_MPI_DebuggerOnError(MPI_Comm *comm, PetscMPIInt *flag, ...)
172: {
173:   PetscFunctionBegin;
174:   PetscCallContinue((*PetscErrorPrintf)("MPI error %d\n", *flag));
175:   if (PetscAttachDebugger()) PETSCABORT(*comm, (PetscErrorCode)*flag); /* hopeless so get out */
176: }

178: /*@
179:   PetscEnd - Calls `PetscFinalize()` and then ends the program. This is useful if one
180:   wishes a clean exit somewhere deep in the program.

182:   Collective on `PETSC_COMM_WORLD`

184:   Level: advanced

186:   Note:
187:   See `PetscInitialize()` for more general runtime options.

189: .seealso: `PetscInitialize()`, `PetscOptionsView()`, `PetscMallocDump()`, `PetscMPIDump()`, `PetscFinalize()`
190: @*/
191: PetscErrorCode PetscEnd(void)
192: {
193:   PetscFunctionBegin;
194:   PetscCall(PetscFinalize());
195:   exit(0);
196:   return PETSC_SUCCESS;
197: }

199: PetscBool                   PetscOptionsPublish = PETSC_FALSE;
200: PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void);
201: PETSC_INTERN PetscBool      petscsetmallocvisited;
202: static char                 emacsmachinename[256];

204: PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = NULL;
205: PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = NULL;

207: #include <petscviewer.h>

209: /*@C
210:   PetscSetHelpVersionFunctions - Sets functions that print help and version information
211:   before the PETSc help and version information is printed.

213:   No Fortran Support

215:   Input Parameters:
216: + help    - the help function (may be `NULL`)
217: - version - the version function (may be `NULL`)

219:   Level: developer

221:   Notes:
222:   Must call BEFORE `PetscInitialize()`.

224:   This routine enables a "higher-level" package that uses PETSc to print its messages first and
225:   control how the PETSc help messages are printed.

227: .seealso: `PetscInitialize()`
228: @*/
229: PetscErrorCode PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm), PetscErrorCode (*version)(MPI_Comm))
230: {
231:   PetscFunctionBegin;
232:   PetscExternalHelpFunction    = help;
233:   PetscExternalVersionFunction = version;
234:   PetscFunctionReturn(PETSC_SUCCESS);
235: }

237: PETSC_INTERN PetscBool PetscObjectsLog;

239: PETSC_INTERN PetscErrorCode PetscOptionsCheckInitial_Private(const char help[])
240: {
241:   char        string[64];
242:   MPI_Comm    comm = PETSC_COMM_WORLD;
243:   PetscBool   flg1 = PETSC_FALSE, flg2 = PETSC_FALSE, flag, hasHelp;
244:   PetscBool   checkstack = PETSC_FALSE;
245:   PetscReal   si;
246:   PetscInt    intensity;
247:   int         i;
248:   PetscMPIInt rank;
249:   char        version[256];

251:   PetscFunctionBegin;
252:   PetscCallMPI(MPI_Comm_rank(comm, &rank));

254:   if (PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)) checkstack = PETSC_TRUE;
255:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-checkstack", &checkstack, NULL));
256:   PetscCall(PetscStackSetCheck(checkstack));

258:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-checkfunctionlist", &PetscPrintFunctionList, NULL));

260: #if !defined(PETSC_HAVE_THREADSAFETY)
261:   if (!(PETSC_RUNNING_ON_VALGRIND)) {
262:     /*
263:       Setup the memory management; support for tracing malloc() usage
264:     */
265:     PetscBool mdebug = PETSC_FALSE, eachcall = PETSC_FALSE, initializenan = PETSC_FALSE, mlog = PETSC_FALSE;
266:     PetscBool flg3 = PETSC_FALSE;

268:     if (PetscDefined(USE_DEBUG)) {
269:       mdebug        = PETSC_TRUE;
270:       initializenan = PETSC_TRUE;
271:       PetscCall(PetscOptionsHasName(NULL, NULL, "-malloc_test", &flg1));
272:     } else {
273:       /* don't warn about unused option */
274:       PetscCall(PetscOptionsHasName(NULL, NULL, "-malloc_test", &flg1));
275:       flg1 = PETSC_FALSE;
276:     }
277:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_debug", &flg2, &flg3));
278:     if (flg1 || flg2) {
279:       mdebug        = PETSC_TRUE;
280:       eachcall      = PETSC_TRUE;
281:       initializenan = PETSC_TRUE;
282:     } else if (flg3 && !flg2) {
283:       mdebug        = PETSC_FALSE;
284:       eachcall      = PETSC_FALSE;
285:       initializenan = PETSC_FALSE;
286:     }

288:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_requested_size", &flg1, &flg2));
289:     if (flg2) PetscCall(PetscMallocLogRequestedSizeSet(flg1));

291:     PetscCall(PetscOptionsHasName(NULL, NULL, "-malloc_view", &mlog));
292:     if (mlog) mdebug = PETSC_TRUE;
293:     /* the next line is deprecated */
294:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_dump", &mdebug, NULL));
295:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_view_memory", &mdebug, NULL));
296:     if (mdebug) PetscCall(PetscMallocSetDebug(eachcall, initializenan));
297:     if (mlog) {
298:       PetscReal logthreshold = 0;
299:       PetscCall(PetscOptionsGetReal(NULL, NULL, "-malloc_view_threshold", &logthreshold, NULL));
300:       PetscCall(PetscMallocViewSet(logthreshold));
301:     }
302:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_view_memory", &PetscLogMemory, NULL));
303:   }

305:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_coalesce", &flg1, &flg2));
306:   if (flg2) PetscCall(PetscMallocSetCoalesce(flg1));
307:   flg1 = PETSC_FALSE;
308:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_hbw", &flg1, NULL));
309:   /* ignore this option if malloc is already set */
310:   if (flg1 && !petscsetmallocvisited) PetscCall(PetscSetUseHBWMalloc_Private());

312:   flg1 = PETSC_FALSE;
313:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-memory_view", &flg1, NULL));
314:   if (flg1) PetscCall(PetscMemorySetGetMaximumUsage());
315: #endif

317:   PetscCall(PetscOptionsHasName(NULL, NULL, "-objects_dump", &PetscObjectsLog));

319:   /*
320:       Set the display variable for graphics
321:   */
322:   PetscCall(PetscSetDisplay());

324:   /*
325:      Print main application help message
326:   */
327:   PetscCall(PetscOptionsHasHelp(NULL, &hasHelp));
328:   if (help && hasHelp) {
329:     PetscCall(PetscPrintf(comm, "%s", help));
330:     PetscCall(PetscPrintf(comm, "----------------------------------------\n"));
331:   }

333:   /*
334:       Print the PETSc version information
335:   */
336:   PetscCall(PetscOptionsHasName(NULL, NULL, "-version", &flg1));
337:   if (flg1 || hasHelp) {
338:     /*
339:        Print "higher-level" package version message
340:     */
341:     if (PetscExternalVersionFunction) PetscCall((*PetscExternalVersionFunction)(comm));

343:     PetscCall(PetscGetVersion(version, 256));
344:     if (!PetscCIEnabledPortableErrorOutput) PetscCall((*PetscHelpPrintf)(comm, "%s\n", version));
345:     PetscCall((*PetscHelpPrintf)(comm, "%s", PETSC_AUTHOR_INFO));
346:     PetscCall((*PetscHelpPrintf)(comm, "See https://petsc.org/release/changes for recent updates.\n"));
347:     PetscCall((*PetscHelpPrintf)(comm, "See https://petsc.org/release/faq for problems.\n"));
348:     PetscCall((*PetscHelpPrintf)(comm, "See https://petsc.org/release/manualpages for help.\n"));
349:     if (!PetscCIEnabledPortableErrorOutput) PetscCall((*PetscHelpPrintf)(comm, "Libraries linked from %s\n", PETSC_LIB_DIR));
350:     PetscCall((*PetscHelpPrintf)(comm, "----------------------------------------\n"));
351:   }

353:   /*
354:        Print "higher-level" package help message
355:   */
356:   if (hasHelp) {
357:     PetscBool hasHelpIntro;

359:     if (PetscExternalHelpFunction) PetscCall((*PetscExternalHelpFunction)(comm));
360:     PetscCall(PetscOptionsHasHelpIntro_Internal(NULL, &hasHelpIntro));
361:     if (hasHelpIntro) {
362:       PetscCall(PetscOptionsDestroyDefault());
363:       PetscCall(PetscFreeMPIResources());
364:       PetscCallMPI(MPI_Finalize());
365:       exit(0);
366:     }
367:   }

369:   /*
370:       Setup the error handling
371:   */
372:   flg1 = PETSC_FALSE;
373:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-on_error_abort", &flg1, NULL));
374:   if (flg1) {
375:     PetscCallMPI(MPI_Comm_set_errhandler(comm, MPI_ERRORS_ARE_FATAL));
376:     PetscCall(PetscPushErrorHandler(PetscAbortErrorHandler, NULL));
377:   }
378:   flg1 = PETSC_FALSE;
379:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-on_error_mpiabort", &flg1, NULL));
380:   if (flg1) PetscCall(PetscPushErrorHandler(PetscMPIAbortErrorHandler, NULL));
381:   flg1 = PETSC_FALSE;
382:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-mpi_return_on_error", &flg1, NULL));
383:   if (flg1) PetscCallMPI(MPI_Comm_set_errhandler(comm, MPI_ERRORS_RETURN));
384:   flg1 = PETSC_FALSE;
385:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-no_signal_handler", &flg1, NULL));
386:   if (!flg1) PetscCall(PetscPushSignalHandler(PetscSignalHandlerDefault, NULL));

388:   /*
389:       Setup debugger information
390:   */
391:   PetscCall(PetscSetDefaultDebugger());
392:   PetscCall(PetscOptionsGetString(NULL, NULL, "-on_error_attach_debugger", string, sizeof(string), &flg1));
393:   if (flg1) {
394:     MPI_Errhandler err_handler;

396:     PetscCall(PetscSetDebuggerFromString(string));
397:     PetscCallMPI(MPI_Comm_create_errhandler(Petsc_MPI_DebuggerOnError, &err_handler));
398:     PetscCallMPI(MPI_Comm_set_errhandler(comm, err_handler));
399:     PetscCall(PetscPushErrorHandler(PetscAttachDebuggerErrorHandler, NULL));
400:   }
401:   PetscCall(PetscOptionsGetString(NULL, NULL, "-debug_terminal", string, sizeof(string), &flg1));
402:   if (flg1) PetscCall(PetscSetDebugTerminal(string));
403:   PetscCall(PetscOptionsGetString(NULL, NULL, "-start_in_debugger", string, sizeof(string), &flg1));
404:   PetscCall(PetscOptionsGetString(NULL, NULL, "-stop_for_debugger", string, sizeof(string), &flg2));
405:   if (flg1 || flg2) {
406:     PetscMPIInt    size;
407:     PetscInt       lsize, *ranks;
408:     MPI_Errhandler err_handler;
409:     /*
410:        we have to make sure that all processors have opened
411:        connections to all other processors, otherwise once the
412:        debugger has stated it is likely to receive a SIGUSR1
413:        and kill the program.
414:     */
415:     PetscCallMPI(MPI_Comm_size(comm, &size));
416:     if (size > 2) {
417:       PetscMPIInt dummy = 0;
418:       MPI_Status  status;
419:       for (i = 0; i < size; i++) {
420:         if (rank != i) PetscCallMPI(MPI_Send(&dummy, 1, MPI_INT, i, 109, comm));
421:       }
422:       for (i = 0; i < size; i++) {
423:         if (rank != i) PetscCallMPI(MPI_Recv(&dummy, 1, MPI_INT, i, 109, comm, &status));
424:       }
425:     }
426:     /* check if this processor node should be in debugger */
427:     PetscCall(PetscMalloc1(size, &ranks));
428:     lsize = size;
429:     /* Deprecated in 3.14 */
430:     PetscCall(PetscOptionsGetIntArray(NULL, NULL, "-debugger_nodes", ranks, &lsize, &flag));
431:     if (flag) {
432:       const char *const quietopt = "-options_suppress_deprecated_warnings";
433:       char              msg[4096];
434:       PetscBool         quiet = PETSC_FALSE;

436:       PetscCall(PetscOptionsGetBool(NULL, NULL, quietopt, &quiet, NULL));
437:       if (!quiet) {
438:         PetscCall(PetscStrncpy(msg, "** PETSc DEPRECATION WARNING ** : the option ", sizeof(msg)));
439:         PetscCall(PetscStrlcat(msg, "-debugger_nodes", sizeof(msg)));
440:         PetscCall(PetscStrlcat(msg, " is deprecated as of version ", sizeof(msg)));
441:         PetscCall(PetscStrlcat(msg, "3.14", sizeof(msg)));
442:         PetscCall(PetscStrlcat(msg, " and will be removed in a future release.", sizeof(msg)));
443:         PetscCall(PetscStrlcat(msg, " Please use the option ", sizeof(msg)));
444:         PetscCall(PetscStrlcat(msg, "-debugger_ranks", sizeof(msg)));
445:         PetscCall(PetscStrlcat(msg, " instead.", sizeof(msg)));
446:         PetscCall(PetscStrlcat(msg, " (Silence this warning with ", sizeof(msg)));
447:         PetscCall(PetscStrlcat(msg, quietopt, sizeof(msg)));
448:         PetscCall(PetscStrlcat(msg, ")\n", sizeof(msg)));
449:         PetscCall(PetscPrintf(comm, "%s", msg));
450:       }
451:     } else {
452:       lsize = size;
453:       PetscCall(PetscOptionsGetIntArray(NULL, NULL, "-debugger_ranks", ranks, &lsize, &flag));
454:     }
455:     if (flag) {
456:       for (i = 0; i < lsize; i++) {
457:         if (ranks[i] == rank) {
458:           flag = PETSC_FALSE;
459:           break;
460:         }
461:       }
462:     }
463:     if (!flag) {
464:       PetscCall(PetscSetDebuggerFromString(string));
465:       PetscCall(PetscPushErrorHandler(PetscAbortErrorHandler, NULL));
466:       if (flg1) {
467:         PetscCall(PetscAttachDebugger());
468:       } else {
469:         PetscCall(PetscStopForDebugger());
470:       }
471:       PetscCallMPI(MPI_Comm_create_errhandler(Petsc_MPI_AbortOnError, &err_handler));
472:       PetscCallMPI(MPI_Comm_set_errhandler(comm, err_handler));
473:     } else {
474:       PetscCall(PetscWaitOnError());
475:     }
476:     PetscCall(PetscFree(ranks));
477:   }

479:   PetscCall(PetscOptionsGetString(NULL, NULL, "-on_error_emacs", emacsmachinename, sizeof(emacsmachinename), &flg1));
480:   if (flg1 && rank == 0) PetscCall(PetscPushErrorHandler(PetscEmacsClientErrorHandler, emacsmachinename));

482:   /*
483:         Setup profiling and logging
484:   */
485:   if (PetscDefined(USE_LOG)) { PetscCall(PetscInfoSetFromOptions(NULL)); }
486:   PetscCall(PetscDetermineInitialFPTrap());
487:   flg1 = PETSC_FALSE;
488:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-fp_trap", &flg1, &flag));
489:   if (flag) PetscCall(PetscSetFPTrap(flg1 ? PETSC_FP_TRAP_ON : PETSC_FP_TRAP_OFF));
490:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-check_pointer_intensity", &intensity, &flag));
491:   if (flag) PetscCall(PetscCheckPointerSetIntensity(intensity));
492:   if (PetscDefined(USE_LOG)) {
493:     char              mname[PETSC_MAX_PATH_LEN];
494:     PetscInt          n_max = PETSC_LOG_VIEW_FROM_OPTIONS_MAX;
495:     PetscViewerFormat format[PETSC_LOG_VIEW_FROM_OPTIONS_MAX];
496:     PetscBool         ci_log = PetscCIEnabled;

498:     mname[0] = 0;
499:     PetscCall(PetscOptionsGetString(NULL, NULL, "-history", mname, sizeof(mname), &flg1));
500:     if (flg1) {
501:       if (mname[0]) {
502:         PetscCall(PetscOpenHistoryFile(mname, &petsc_history));
503:       } else {
504:         PetscCall(PetscOpenHistoryFile(NULL, &petsc_history));
505:       }
506:     }

508:     if (ci_log) {
509:       static const char *LogOptions[] = {"-log_view", "-log_mpe", "-log_perfstubs", "-log_nvtx", "-log", "-log_all"};

511:       for (size_t i = 0; i < PETSC_STATIC_ARRAY_LENGTH(LogOptions); i++) {
512:         PetscCall(PetscOptionsHasName(NULL, NULL, LogOptions[i], &flg1));
513:         if (flg1) {
514:           ci_log = PETSC_FALSE;
515:           break;
516:         }
517:       }
518:     }
519:     if (ci_log) PetscLogSyncOn = PETSC_TRUE;
520:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_sync", &PetscLogSyncOn, NULL));

522:     if (PetscDefined(HAVE_MPE)) {
523:       flg1 = PETSC_FALSE;
524:       PetscCall(PetscOptionsHasName(NULL, NULL, "-log_mpe", &flg1));
525:       if (flg1) PetscCall(PetscLogMPEBegin());
526:     }
527:     if (PetscDefined(HAVE_TAU_PERFSTUBS)) {
528:       char     *tau_exec_path       = getenv("TAU_EXEC_PATH");
529:       PetscBool start_log_perfstubs = (tau_exec_path != NULL) ? PETSC_TRUE : PETSC_FALSE;

531:       if (tau_exec_path && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected tau_exec path %s\n", tau_exec_path));
532:       PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_perfstubs", &start_log_perfstubs, NULL));
533:       if (start_log_perfstubs) PetscCall(PetscLogPerfstubsBegin());
534:     }
535:     if (PetscDefined(USE_LOG) && PetscDefined(HAVE_CUDA)) {
536:       char     *nsys_profiling_session_id = getenv("NSYS_PROFILING_SESSION_ID");
537:       char     *nvprof_id                 = getenv("NVPROF_ID");
538:       PetscBool start_log_nvtx            = ((nsys_profiling_session_id != NULL) || (nvprof_id != NULL)) ? PETSC_TRUE : PETSC_FALSE;

540:       if (nsys_profiling_session_id && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected nsys profiling session id %s\n", nsys_profiling_session_id));
541:       if (nvprof_id && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected nvprof session id %s\n", nvprof_id));
542:       PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_nvtx", &start_log_nvtx, NULL));
543:       if (start_log_nvtx) PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERNVTX));
544:     }
545:     flg1 = PETSC_FALSE;
546:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_all", &flg1, NULL));
547:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log", &flg2, NULL));
548:     if (flg1 || flg2 || ci_log) PetscCall(PetscLogDefaultBegin());

550:     PetscCall(PetscOptionsGetString(NULL, NULL, "-log_trace", mname, sizeof(mname), &flg1));
551:     if (flg1) {
552:       char  name[PETSC_MAX_PATH_LEN], fname[PETSC_MAX_PATH_LEN];
553:       FILE *file;
554:       if (mname[0]) {
555:         PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "%s.%d", mname, rank));
556:         PetscCall(PetscFixFilename(name, fname));
557:         file = fopen(fname, "w");
558:         PetscCheck(file, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to open trace file: %s", fname);
559:       } else file = PETSC_STDOUT;
560:       PetscCall(PetscLogTraceBegin(file));
561:     }

563:     PetscCall(PetscOptionsCreateViewers(comm, NULL, NULL, "-log_view", &n_max, NULL, format, NULL));
564:     if (n_max > 0) {
565:       PetscBool any_nested  = PETSC_FALSE;
566:       PetscBool any_default = PETSC_FALSE;

568:       for (PetscInt i = 0; i < n_max; i++) {
569:         if (format[i] == PETSC_VIEWER_ASCII_XML || format[i] == PETSC_VIEWER_ASCII_FLAMEGRAPH) {
570:           any_nested = PETSC_TRUE;
571:         } else {
572:           any_default = PETSC_TRUE;
573:         }
574:       }
575:       if (any_default) { PetscCall(PetscLogDefaultBegin()); }
576:       if (any_nested) {
577:         PetscCall(PetscLogNestedBegin());
578:         PetscReal threshold = PetscRealConstant(0.01);
579:         PetscCall(PetscOptionsGetReal(NULL, NULL, "-log_threshold", &threshold, &flg1));
580:         if (flg1) PetscCall(PetscLogSetThreshold((PetscLogDouble)threshold, NULL));
581:       }
582:     }
583:   }

585:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-saws_options", &PetscOptionsPublish, NULL));
586:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-use_gpu_aware_mpi", &use_gpu_aware_mpi, &flg1));
587:   if (!flg1) PetscCall(PetscOptionsGetBool(NULL, NULL, "-sf_use_gpu_aware_mpi", &use_gpu_aware_mpi, &flg1)); // an alias option

589:   /*
590:        Print basic help message
591:   */
592:   if (hasHelp) {
593:     PetscCall((*PetscHelpPrintf)(comm, "Options for all PETSc programs:\n"));
594:     PetscCall((*PetscHelpPrintf)(comm, " -version: prints PETSc version\n"));
595:     PetscCall((*PetscHelpPrintf)(comm, " -help intro: prints example description and PETSc version, and exits\n"));
596:     PetscCall((*PetscHelpPrintf)(comm, " -help: prints example description, PETSc version, and available options for used routines\n"));
597:     PetscCall((*PetscHelpPrintf)(comm, " -on_error_abort: cause an abort when an error is detected. Useful \n "));
598:     PetscCall((*PetscHelpPrintf)(comm, "       only when run in the debugger\n"));
599:     PetscCall((*PetscHelpPrintf)(comm, " -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n"));
600:     PetscCall((*PetscHelpPrintf)(comm, "       start the debugger in new xterm\n"));
601:     PetscCall((*PetscHelpPrintf)(comm, "       unless noxterm is given\n"));
602:     PetscCall((*PetscHelpPrintf)(comm, " -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n"));
603:     PetscCall((*PetscHelpPrintf)(comm, "       start all processes in the debugger\n"));
604:     PetscCall((*PetscHelpPrintf)(comm, " -on_error_emacs <machinename>\n"));
605:     PetscCall((*PetscHelpPrintf)(comm, "    emacs jumps to error file\n"));
606:     PetscCall((*PetscHelpPrintf)(comm, " -debugger_ranks [n1,n2,..] Ranks to start in debugger\n"));
607:     PetscCall((*PetscHelpPrintf)(comm, " -debugger_pause [m] : delay (in seconds) to attach debugger\n"));
608:     PetscCall((*PetscHelpPrintf)(comm, " -stop_for_debugger : prints message on how to attach debugger manually\n"));
609:     PetscCall((*PetscHelpPrintf)(comm, "                      waits the delay for you to attach\n"));
610:     PetscCall((*PetscHelpPrintf)(comm, " -display display: Location where X window graphics and debuggers are displayed\n"));
611:     PetscCall((*PetscHelpPrintf)(comm, " -no_signal_handler: do not trap error signals\n"));
612:     PetscCall((*PetscHelpPrintf)(comm, " -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n"));
613:     PetscCall((*PetscHelpPrintf)(comm, " -fp_trap: stop on floating point exceptions\n"));
614:     PetscCall((*PetscHelpPrintf)(comm, "           note on IBM RS6000 this slows run greatly\n"));
615:     PetscCall((*PetscHelpPrintf)(comm, " -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n"));
616:     PetscCall((*PetscHelpPrintf)(comm, " -on_error_malloc_dump <optional filename>: dump list of unfreed memory on memory error\n"));
617:     PetscCall((*PetscHelpPrintf)(comm, " -malloc_view <optional filename>: keeps log of all memory allocations, displays in PetscFinalize()\n"));
618:     PetscCall((*PetscHelpPrintf)(comm, " -malloc_debug <true or false>: enables or disables extended checking for memory corruption\n"));
619:     PetscCall((*PetscHelpPrintf)(comm, " -options_view: dump list of options inputted\n"));
620:     PetscCall((*PetscHelpPrintf)(comm, " -options_left: dump list of unused options\n"));
621:     PetscCall((*PetscHelpPrintf)(comm, " -options_left no: don't dump list of unused options\n"));
622:     PetscCall((*PetscHelpPrintf)(comm, " -tmp tmpdir: alternative /tmp directory\n"));
623:     PetscCall((*PetscHelpPrintf)(comm, " -shared_tmp: tmp directory is shared by all processors\n"));
624:     PetscCall((*PetscHelpPrintf)(comm, " -not_shared_tmp: each processor has separate tmp directory\n"));
625:     PetscCall((*PetscHelpPrintf)(comm, " -memory_view: print memory usage at end of run\n"));
626: #if defined(PETSC_USE_LOG)
627:     PetscCall((*PetscHelpPrintf)(comm, " -get_total_flops: total flops over all processors\n"));
628:     PetscCall((*PetscHelpPrintf)(comm, " -log_view [:filename:[format]]: logging objects and events\n"));
629:     PetscCall((*PetscHelpPrintf)(comm, " -log_trace [filename]: prints trace of all PETSc calls\n"));
630:     PetscCall((*PetscHelpPrintf)(comm, " -log_exclude <list,of,classnames>: exclude given classes from logging\n"));
631:   #if defined(PETSC_HAVE_DEVICE)
632:     PetscCall((*PetscHelpPrintf)(comm, " -log_view_gpu_time: log the GPU time for each and event\n"));
633:   #endif
634:   #if defined(PETSC_HAVE_MPE)
635:     PetscCall((*PetscHelpPrintf)(comm, " -log_mpe: Also create logfile viewable through Jumpshot\n"));
636:   #endif
637:   #if PetscDefined(HAVE_CUDA)
638:     PetscCall((*PetscHelpPrintf)(comm, " -log_nvtx: Create nvtx event ranges for Nsight\n"));
639:   #endif
640: #endif
641: #if defined(PETSC_USE_INFO)
642:     PetscCall((*PetscHelpPrintf)(comm, " -info [filename][:[~]<list,of,classnames>[:[~]self]]: print verbose information\n"));
643: #endif
644:     PetscCall((*PetscHelpPrintf)(comm, " -options_file <file>: reads options from file\n"));
645:     PetscCall((*PetscHelpPrintf)(comm, " -options_monitor: monitor options to standard output, including that set previously e.g. in option files\n"));
646:     PetscCall((*PetscHelpPrintf)(comm, " -options_monitor_cancel: cancels all hardwired option monitors\n"));
647:     PetscCall((*PetscHelpPrintf)(comm, " -petsc_sleep n: sleeps n seconds before running program\n"));
648:   }

650: #if defined(PETSC_HAVE_POPEN)
651:   {
652:     char machine[128];
653:     PetscCall(PetscOptionsGetString(NULL, NULL, "-popen_machine", machine, sizeof(machine), &flg1));
654:     if (flg1) PetscCall(PetscPOpenSetMachine(machine));
655:   }
656: #endif

658:   PetscCall(PetscOptionsGetReal(NULL, NULL, "-petsc_sleep", &si, &flg1));
659:   if (flg1) PetscCall(PetscSleep(si));
660:   PetscFunctionReturn(PETSC_SUCCESS);
661: }