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: /*MC
 43:    PETSC_i - the imaginary number i

 45:    Synopsis:
 46: #include <petscsys.h>
 47:    PetscComplex PETSC_i;

 49:    Level: beginner

 51:    Note:
 52:    Complex numbers are automatically available if PETSc located a working complex implementation

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

 78: /*
 79:        Function that is called to display all error messages
 80: */
 81: PetscErrorCode (*PetscErrorPrintf)(const char[], ...)          = PetscErrorPrintfDefault;
 82: PetscErrorCode (*PetscHelpPrintf)(MPI_Comm, const char[], ...) = PetscHelpPrintfDefault;
 83: PetscErrorCode (*PetscVFPrintf)(FILE *, const char[], va_list) = PetscVFPrintfDefault;

 85: /*
 86:    Optional file where all PETSc output from various prints is saved
 87: */
 88: PETSC_INTERN FILE *petsc_history;
 89: FILE              *petsc_history = NULL;

 91: static PetscErrorCode PetscOpenHistoryFile(const char filename[], FILE **fd)
 92: {
 93:   PetscMPIInt rank, size;
 94:   char        pfile[PETSC_MAX_PATH_LEN], pname[PETSC_MAX_PATH_LEN], fname[PETSC_MAX_PATH_LEN], date[64];
 95:   char        version[256];

 97:   PetscFunctionBegin;
 98:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 99:   if (rank == 0) {
100:     char arch[10];

102:     PetscCall(PetscGetArchType(arch, 10));
103:     PetscCall(PetscGetDate(date, 64));
104:     PetscCall(PetscGetVersion(version, 256));
105:     PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
106:     if (filename) {
107:       PetscCall(PetscFixFilename(filename, fname));
108:     } else {
109:       PetscCall(PetscGetHomeDirectory(pfile, sizeof(pfile)));
110:       PetscCall(PetscStrlcat(pfile, "/.petschistory", sizeof(pfile)));
111:       PetscCall(PetscFixFilename(pfile, fname));
112:     }

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

117:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
118:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "%s %s\n", version, date));
119:     PetscCall(PetscGetProgramName(pname, sizeof(pname)));
120:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "%s on a %s, %d proc. with options:\n", pname, arch, size));
121:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));

123:     PetscCall(PetscFFlush(*fd));
124:   }
125:   PetscFunctionReturn(PETSC_SUCCESS);
126: }

128: PETSC_INTERN PetscErrorCode PetscCloseHistoryFile(FILE **fd)
129: {
130:   PetscMPIInt rank;

132:   PetscFunctionBegin;
133:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
134:   if (rank == 0) {
135:     char date[64];
136:     int  err;

138:     PetscCall(PetscGetDate(date, sizeof(date)));
139:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
140:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "Finished at %s\n", date));
141:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
142:     PetscCall(PetscFFlush(*fd));
143:     err = fclose(*fd);
144:     PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_SYS, "fclose() failed on file");
145:   }
146:   PetscFunctionReturn(PETSC_SUCCESS);
147: }

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

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

158: static void Petsc_MPI_AbortOnError(PETSC_UNUSED MPI_Comm *comm, PetscMPIInt *flag, ...)
159: {
160:   PetscFunctionBegin;
161:   PetscCallContinue((*PetscErrorPrintf)("MPI error %d\n", *flag));
162:   abort();
163: }

165: static void Petsc_MPI_DebuggerOnError(MPI_Comm *comm, PetscMPIInt *flag, ...)
166: {
167:   PetscFunctionBegin;
168:   PetscCallContinue((*PetscErrorPrintf)("MPI error %d\n", *flag));
169:   if (PetscAttachDebugger()) PETSCABORT(*comm, (PetscErrorCode)*flag); /* hopeless so get out */
170: }

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

176:   Collective on `PETSC_COMM_WORLD`

178:   Level: advanced

180:   Note:
181:   See `PetscInitialize()` for more general runtime options.

183: .seealso: `PetscInitialize()`, `PetscOptionsView()`, `PetscMallocDump()`, `PetscMPIDump()`, `PetscFinalize()`
184: @*/
185: PetscErrorCode PetscEnd(void)
186: {
187:   PetscFunctionBegin;
188:   PetscCall(PetscFinalize());
189:   exit(0);
190:   return PETSC_SUCCESS;
191: }

193: PetscBool                   PetscOptionsPublish = PETSC_FALSE;
194: PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void);
195: PETSC_INTERN PetscBool      petscsetmallocvisited;
196: static char                 emacsmachinename[256];

198: PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = NULL;
199: PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = NULL;

201: #include <petscviewer.h>

203: /*@C
204:   PetscSetHelpVersionFunctions - Sets functions that print help and version information
205:   before the PETSc help and version information is printed.

207:   No Fortran Support

209:   Input Parameters:
210: + help    - the help function (may be `NULL`)
211: - version - the version function (may be `NULL`)

213:   Level: developer

215:   Notes:
216:   Must call BEFORE `PetscInitialize()`.

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

221: .seealso: `PetscInitialize()`
222: @*/
223: PetscErrorCode PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm), PetscErrorCode (*version)(MPI_Comm))
224: {
225:   PetscFunctionBegin;
226:   PetscExternalHelpFunction    = help;
227:   PetscExternalVersionFunction = version;
228:   PetscFunctionReturn(PETSC_SUCCESS);
229: }

231: PETSC_INTERN PetscBool PetscObjectsLog;

233: PETSC_INTERN PetscErrorCode PetscOptionsCheckInitial_Private(const char help[])
234: {
235:   char        string[64];
236:   MPI_Comm    comm = PETSC_COMM_WORLD;
237:   PetscBool   flg1 = PETSC_FALSE, flg2 = PETSC_FALSE, flag, hasHelp;
238:   PetscBool   checkstack = PETSC_FALSE;
239:   PetscReal   si;
240:   PetscInt    intensity;
241:   int         i;
242:   PetscMPIInt rank;
243:   char        version[256];

245:   PetscFunctionBegin;
246:   PetscCallMPI(MPI_Comm_rank(comm, &rank));

248:   if (PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)) checkstack = PETSC_TRUE;
249:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-checkstack", &checkstack, NULL));
250:   PetscCall(PetscStackSetCheck(checkstack));

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

254: #if !defined(PETSC_HAVE_THREADSAFETY)
255:   if (!(PETSC_RUNNING_ON_VALGRIND)) {
256:     /*
257:       Setup the memory management; support for tracing malloc() usage
258:     */
259:     PetscBool mdebug = PETSC_FALSE, eachcall = PETSC_FALSE, initializenan = PETSC_FALSE, mlog = PETSC_FALSE;
260:     PetscBool flg3 = PETSC_FALSE;

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

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

285:     PetscCall(PetscOptionsHasName(NULL, NULL, "-malloc_view", &mlog));
286:     if (mlog) mdebug = PETSC_TRUE;
287:     /* the next line is deprecated */
288:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_dump", &mdebug, NULL));
289:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_view_memory", &mdebug, NULL));
290:     if (mdebug) PetscCall(PetscMallocSetDebug(eachcall, initializenan));
291:     if (mlog) {
292:       PetscReal logthreshold = 0;
293:       PetscCall(PetscOptionsGetReal(NULL, NULL, "-malloc_view_threshold", &logthreshold, NULL));
294:       PetscCall(PetscMallocViewSet(logthreshold));
295:     }
296:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_view_memory", &PetscLogMemory, NULL));
297:   }

299:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_coalesce", &flg1, &flg2));
300:   if (flg2) PetscCall(PetscMallocSetCoalesce(flg1));
301:   flg1 = PETSC_FALSE;
302:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_hbw", &flg1, NULL));
303:   /* ignore this option if malloc is already set */
304:   if (flg1 && !petscsetmallocvisited) PetscCall(PetscSetUseHBWMalloc_Private());

306:   flg1 = PETSC_FALSE;
307:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-memory_view", &flg1, NULL));
308:   if (flg1) PetscCall(PetscMemorySetGetMaximumUsage());
309: #endif

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

313:   /*
314:       Set the display variable for graphics
315:   */
316:   PetscCall(PetscSetDisplay());

318:   /*
319:      Print main application help message
320:   */
321:   PetscCall(PetscOptionsHasHelp(NULL, &hasHelp));
322:   if (help && hasHelp) {
323:     PetscCall(PetscPrintf(comm, "%s", help));
324:     PetscCall(PetscPrintf(comm, "----------------------------------------\n"));
325:   }

327:   /*
328:       Print the PETSc version information
329:   */
330:   PetscCall(PetscOptionsHasName(NULL, NULL, "-version", &flg1));
331:   if (flg1 || hasHelp) {
332:     /*
333:        Print "higher-level" package version message
334:     */
335:     if (PetscExternalVersionFunction) PetscCall((*PetscExternalVersionFunction)(comm));

337:     PetscCall(PetscGetVersion(version, 256));
338:     if (!PetscCIEnabledPortableErrorOutput) PetscCall((*PetscHelpPrintf)(comm, "%s\n", version));
339:     PetscCall((*PetscHelpPrintf)(comm, "%s", PETSC_AUTHOR_INFO));
340:     PetscCall((*PetscHelpPrintf)(comm, "See https://petsc.org/release/changes for recent updates.\n"));
341:     PetscCall((*PetscHelpPrintf)(comm, "See https://petsc.org/release/faq for problems.\n"));
342:     PetscCall((*PetscHelpPrintf)(comm, "See https://petsc.org/release/manualpages for help.\n"));
343:     if (!PetscCIEnabledPortableErrorOutput) PetscCall((*PetscHelpPrintf)(comm, "Libraries linked from %s\n", PETSC_LIB_DIR));
344:     PetscCall((*PetscHelpPrintf)(comm, "----------------------------------------\n"));
345:   }

347:   /*
348:        Print "higher-level" package help message
349:   */
350:   if (hasHelp) {
351:     PetscBool hasHelpIntro;

353:     if (PetscExternalHelpFunction) PetscCall((*PetscExternalHelpFunction)(comm));
354:     PetscCall(PetscOptionsHasHelpIntro_Internal(NULL, &hasHelpIntro));
355:     if (hasHelpIntro) {
356:       PetscCall(PetscOptionsDestroyDefault());
357:       PetscCall(PetscFreeMPIResources());
358:       PetscCallMPI(MPI_Finalize());
359:       exit(0);
360:     }
361:   }

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

382:   /*
383:       Setup debugger information
384:   */
385:   PetscCall(PetscSetDefaultDebugger());
386:   PetscCall(PetscOptionsGetString(NULL, NULL, "-on_error_attach_debugger", string, sizeof(string), &flg1));
387:   if (flg1) {
388:     MPI_Errhandler err_handler;

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

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

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

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

492:     mname[0] = 0;
493:     PetscCall(PetscOptionsGetString(NULL, NULL, "-history", mname, sizeof(mname), &flg1));
494:     if (flg1) {
495:       if (mname[0]) {
496:         PetscCall(PetscOpenHistoryFile(mname, &petsc_history));
497:       } else {
498:         PetscCall(PetscOpenHistoryFile(NULL, &petsc_history));
499:       }
500:     }

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

505:       for (size_t i = 0; i < PETSC_STATIC_ARRAY_LENGTH(LogOptions); i++) {
506:         PetscCall(PetscOptionsHasName(NULL, NULL, LogOptions[i], &flg1));
507:         if (flg1) {
508:           ci_log = PETSC_FALSE;
509:           break;
510:         }
511:       }
512:     }
513:     if (ci_log) PetscLogSyncOn = PETSC_TRUE;
514:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_sync", &PetscLogSyncOn, NULL));

516:     if (PetscDefined(HAVE_MPE)) {
517:       flg1 = PETSC_FALSE;
518:       PetscCall(PetscOptionsHasName(NULL, NULL, "-log_mpe", &flg1));
519:       if (flg1) PetscCall(PetscLogMPEBegin());
520:     }
521:     if (PetscDefined(HAVE_TAU_PERFSTUBS)) {
522:       char     *tau_exec_path       = getenv("TAU_EXEC_PATH");
523:       PetscBool start_log_perfstubs = (tau_exec_path != NULL) ? PETSC_TRUE : PETSC_FALSE;

525:       if (tau_exec_path && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected tau_exec path %s\n", tau_exec_path));
526:       PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_perfstubs", &start_log_perfstubs, NULL));
527:       if (start_log_perfstubs) PetscCall(PetscLogPerfstubsBegin());
528:     }
529:     if (PetscDefined(USE_LOG) && PetscDefined(HAVE_CUDA)) {
530:       char     *nsys_profiling_session_id = getenv("NSYS_PROFILING_SESSION_ID");
531:       char     *nvprof_id                 = getenv("NVPROF_ID");
532:       PetscBool start_log_nvtx            = ((nsys_profiling_session_id != NULL) || (nvprof_id != NULL)) ? PETSC_TRUE : PETSC_FALSE;

534:       if (nsys_profiling_session_id && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected nsys profiling session id %s\n", nsys_profiling_session_id));
535:       if (nvprof_id && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected nvprof session id %s\n", nvprof_id));
536:       PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_nvtx", &start_log_nvtx, NULL));
537:       if (start_log_nvtx) PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERNVTX));
538:     }
539:     if (PetscDefined(USE_LOG) && PetscDefined(HAVE_ROCTX)) {
540:       char     *rocprof_roctx_trace  = getenv("ROCPROFILER_ROCTX_TRACE");
541:       char     *rocprof_marker_trace = getenv("ROCPROF_MARKER_API_TRACE");
542:       PetscBool start_log_roctx      = ((rocprof_roctx_trace != NULL) || (rocprof_marker_trace != NULL)) ? PETSC_TRUE : PETSC_FALSE;

544:       if (rocprof_roctx_trace && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected rocprofv2 profiling session\n"));
545:       if (rocprof_marker_trace && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected rocprofv3 profiling session\n"));
546:       PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_roctx", &start_log_roctx, NULL));
547:       if (start_log_roctx) PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERROCTX));
548:     }
549:     flg1 = PETSC_FALSE;
550:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_all", &flg1, NULL));
551:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log", &flg2, NULL));
552:     if (flg1 || flg2 || ci_log) PetscCall(PetscLogDefaultBegin());

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

567:     PetscCall(PetscOptionsCreateViewers(comm, NULL, NULL, "-log_view", &n_max, NULL, format, NULL));
568:     if (n_max > 0) {
569:       PetscBool any_nested  = PETSC_FALSE;
570:       PetscBool any_default = PETSC_FALSE;

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

589:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-saws_options", &PetscOptionsPublish, NULL));
590:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-use_gpu_aware_mpi", &use_gpu_aware_mpi, &flg1));
591:   if (!flg1) PetscCall(PetscOptionsGetBool(NULL, NULL, "-sf_use_gpu_aware_mpi", &use_gpu_aware_mpi, &flg1)); // an alias option

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

657: #if defined(PETSC_HAVE_POPEN)
658:   {
659:     char machine[128];
660:     PetscCall(PetscOptionsGetString(NULL, NULL, "-popen_machine", machine, sizeof(machine), &flg1));
661:     if (flg1) PetscCall(PetscPOpenSetMachine(machine));
662:   }
663: #endif

665:   PetscCall(PetscOptionsGetReal(NULL, NULL, "-petsc_sleep", &si, &flg1));
666:   if (flg1) PetscCall(PetscSleep(si));
667:   PetscFunctionReturn(PETSC_SUCCESS);
668: }