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: #endif
 76: MPI_Datatype MPI_4INT  = 0;
 77: MPI_Datatype MPIU_4INT = 0;
 78: MPI_Datatype MPIU_BOOL;
 79: MPI_Datatype MPIU_ENUM;
 80: MPI_Datatype MPIU_FORTRANADDR;
 81: MPI_Datatype MPIU_SIZE_T;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

181:   Collective on `PETSC_COMM_WORLD`

183:   Level: advanced

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

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

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

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

206: #include <petscviewer.h>

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

212:   Input Parameters:
213: + help    - the help function (may be `NULL`)
214: - version - the version function (may be `NULL`)

216:   Level: developer

218:   Notes:
219:   Must call BEFORE `PetscInitialize()`.

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

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

234: PETSC_INTERN PetscBool PetscObjectsLog;

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

248:   PetscFunctionBegin;
249:   PetscCallMPI(MPI_Comm_rank(comm, &rank));

251:   if (PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)) checkstack = PETSC_TRUE;
252:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-checkstack", &checkstack, NULL));
253:   PetscCall(PetscStackSetCheck(checkstack));

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

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

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

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

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

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

309:   flg1 = PETSC_FALSE;
310:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-memory_view", &flg1, NULL));
311:   if (flg1) PetscCall(PetscMemorySetGetMaximumUsage());
312: #endif

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

316:   /*
317:       Set the display variable for graphics
318:   */
319:   PetscCall(PetscSetDisplay());

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

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

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

350:   /*
351:        Print "higher-level" package help message
352:   */
353:   if (hasHelp) {
354:     PetscBool hasHelpIntro;

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

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

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

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

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

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

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

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

504:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_sync", &PetscLogSyncOn, NULL));

506:     if (PetscDefined(HAVE_MPE)) {
507:       flg1 = PETSC_FALSE;
508:       PetscCall(PetscOptionsHasName(NULL, NULL, "-log_mpe", &flg1));
509:       if (flg1) PetscCall(PetscLogMPEBegin());
510:     }
511:     if (PetscDefined(HAVE_TAU_PERFSTUBS)) {
512:       flg1 = PETSC_FALSE;
513:       PetscCall(PetscOptionsHasName(NULL, NULL, "-log_perfstubs", &flg1));
514:       if (flg1) PetscCall(PetscLogPerfstubsBegin());
515:     }
516:     if (PetscDefined(USE_LOG) && PetscDefined(HAVE_CUDA)) {
517:       char     *nsys_profiling_session_id = getenv("NSYS_PROFILING_SESSION_ID");
518:       char     *nvprof_id                 = getenv("NVPROF_ID");
519:       PetscBool start_log_nvtx            = ((nsys_profiling_session_id != NULL) || (nvprof_id != NULL)) ? PETSC_TRUE : PETSC_FALSE;

521:       if (nsys_profiling_session_id && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected nsys profiling session id %s\n", nsys_profiling_session_id));
522:       if (nvprof_id && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected nvprof session id %s\n", nvprof_id));
523:       PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_nvtx", &start_log_nvtx, NULL));
524:       if (start_log_nvtx) PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERNVTX));
525:     }
526:     flg1 = PETSC_FALSE;
527:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_all", &flg1, NULL));
528:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log", &flg2, NULL));
529:     if (flg1 || flg2) PetscCall(PetscLogDefaultBegin());

531:     PetscCall(PetscOptionsGetString(NULL, NULL, "-log_trace", mname, sizeof(mname), &flg1));
532:     if (flg1) {
533:       char  name[PETSC_MAX_PATH_LEN], fname[PETSC_MAX_PATH_LEN];
534:       FILE *file;
535:       if (mname[0]) {
536:         PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "%s.%d", mname, rank));
537:         PetscCall(PetscFixFilename(name, fname));
538:         file = fopen(fname, "w");
539:         PetscCheck(file, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to open trace file: %s", fname);
540:       } else file = PETSC_STDOUT;
541:       PetscCall(PetscLogTraceBegin(file));
542:     }

544:     PetscCall(PetscOptionsGetViewers(comm, NULL, NULL, "-log_view", &n_max, NULL, format, NULL));
545:     if (n_max > 0) {
546:       PetscBool any_nested  = PETSC_FALSE;
547:       PetscBool any_default = PETSC_FALSE;

549:       for (PetscInt i = 0; i < n_max; i++) {
550:         if (format[i] == PETSC_VIEWER_ASCII_XML || format[i] == PETSC_VIEWER_ASCII_FLAMEGRAPH) {
551:           any_nested = PETSC_TRUE;
552:         } else {
553:           any_default = PETSC_TRUE;
554:         }
555:       }
556:       if (any_default) { PetscCall(PetscLogDefaultBegin()); }
557:       if (any_nested) {
558:         PetscCall(PetscLogNestedBegin());
559:         PetscReal threshold = PetscRealConstant(0.01);
560:         PetscCall(PetscOptionsGetReal(NULL, NULL, "-log_threshold", &threshold, &flg1));
561:         if (flg1) PetscCall(PetscLogSetThreshold((PetscLogDouble)threshold, NULL));
562:       }
563:     }
564:   }

566:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-saws_options", &PetscOptionsPublish, NULL));
567:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-use_gpu_aware_mpi", &use_gpu_aware_mpi, &flg1));
568:   if (!flg1) PetscCall(PetscOptionsGetBool(NULL, NULL, "-sf_use_gpu_aware_mpi", &use_gpu_aware_mpi, &flg1)); // an alias option

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

631: #if defined(PETSC_HAVE_POPEN)
632:   {
633:     char machine[128];
634:     PetscCall(PetscOptionsGetString(NULL, NULL, "-popen_machine", machine, sizeof(machine), &flg1));
635:     if (flg1) PetscCall(PetscPOpenSetMachine(machine));
636:   }
637: #endif

639:   PetscCall(PetscOptionsGetReal(NULL, NULL, "-petsc_sleep", &si, &flg1));
640:   if (flg1) PetscCall(PetscSleep(si));
641:   PetscFunctionReturn(PETSC_SUCCESS);
642: }