xref: /petsc/src/sys/objects/init.c (revision 649db6d204056c1c29154091fa8bd5dd207d7dae)
1 /*
2 
3    This file defines part of the initialization of PETSc
4 
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> /*I  "petscsys.h"   I*/
9 #include <petsc/private/logimpl.h>
10 
11 #if defined(PETSC_HAVE_UNISTD_H)
12   #include <unistd.h>
13 #endif
14 
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;
24 
25 PetscMPIInt PetscGlobalRank = -1;
26 PetscMPIInt PetscGlobalSize = -1;
27 
28 #if defined(PETSC_HAVE_KOKKOS)
29 PetscBool PetscBeganKokkos = PETSC_FALSE;
30 #endif
31 
32 #if defined(PETSC_HAVE_NVSHMEM)
33 PetscBool PetscBeganNvshmem       = PETSC_FALSE;
34 PetscBool PetscNvshmemInitialized = PETSC_FALSE;
35 #endif
36 
37 PetscBool use_gpu_aware_mpi = PetscDefined(HAVE_MPIUNI) ? PETSC_FALSE : PETSC_TRUE;
38 
39 PetscBool PetscPrintFunctionList = PETSC_FALSE;
40 
41 #if defined(PETSC_HAVE_COMPLEX)
42 /*MC
43    PETSC_i - the imaginary number i
44 
45    Synopsis:
46    #include <petscsys.h>
47    PetscComplex PETSC_i;
48 
49    Level: beginner
50 
51    Note:
52    Complex numbers are automatically available if PETSc located a working complex implementation
53 
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;
77 
78 const char *const PetscPrecisionTypes[] = {"INVALID", "BFLOAT16", "__FP16", "SINGLE", "DOUBLE", "__FLOAT128", "PetscPrecisionTypes", "PETSC_PRECISION_", NULL};
79 
80 /*
81        Function that is called to display all error messages
82 */
83 PetscErrorCode (*PetscErrorPrintf)(const char[], ...)          = PetscErrorPrintfDefault;
84 PetscErrorCode (*PetscHelpPrintf)(MPI_Comm, const char[], ...) = PetscHelpPrintfDefault;
85 PetscErrorCode (*PetscVFPrintf)(FILE *, const char[], va_list) = PetscVFPrintfDefault;
86 
87 /*
88    Optional file where all PETSc output from various prints is saved
89 */
90 PETSC_INTERN FILE *petsc_history;
91 FILE              *petsc_history = NULL;
92 
93 static PetscErrorCode PetscOpenHistoryFile(const char filename[], FILE **fd)
94 {
95   PetscMPIInt rank, size;
96   char        pfile[PETSC_MAX_PATH_LEN], pname[PETSC_MAX_PATH_LEN], fname[PETSC_MAX_PATH_LEN], date[64];
97   char        version[256];
98 
99   PetscFunctionBegin;
100   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
101   if (rank == 0) {
102     char arch[10];
103 
104     PetscCall(PetscGetArchType(arch, 10));
105     PetscCall(PetscGetDate(date, 64));
106     PetscCall(PetscGetVersion(version, 256));
107     PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
108     if (filename) {
109       PetscCall(PetscFixFilename(filename, fname));
110     } else {
111       PetscCall(PetscGetHomeDirectory(pfile, sizeof(pfile)));
112       PetscCall(PetscStrlcat(pfile, "/.petschistory", sizeof(pfile)));
113       PetscCall(PetscFixFilename(pfile, fname));
114     }
115 
116     *fd = fopen(fname, "a");
117     PetscCheck(fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open file: %s", fname);
118 
119     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
120     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "%s %s\n", version, date));
121     PetscCall(PetscGetProgramName(pname, sizeof(pname)));
122     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "%s on a %s, %d proc. with options:\n", pname, arch, size));
123     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
124 
125     PetscCall(PetscFFlush(*fd));
126   }
127   PetscFunctionReturn(PETSC_SUCCESS);
128 }
129 
130 PETSC_INTERN PetscErrorCode PetscCloseHistoryFile(FILE **fd)
131 {
132   PetscMPIInt rank;
133 
134   PetscFunctionBegin;
135   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
136   if (rank == 0) {
137     char date[64];
138     int  err;
139 
140     PetscCall(PetscGetDate(date, sizeof(date)));
141     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
142     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "Finished at %s\n", date));
143     PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
144     PetscCall(PetscFFlush(*fd));
145     err = fclose(*fd);
146     PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_SYS, "fclose() failed on file");
147   }
148   PetscFunctionReturn(PETSC_SUCCESS);
149 }
150 
151 /*
152    This is ugly and probably belongs somewhere else, but I want to
153   be able to put a true MPI abort error handler with command line args.
154 
155     This is so MPI errors in the debugger will leave all the stack
156   frames. The default MP_Abort() cleans up and exits thus providing no useful information
157   in the debugger hence we call abort() instead of MPI_Abort().
158 */
159 
160 static void Petsc_MPI_AbortOnError(PETSC_UNUSED MPI_Comm *comm, PetscMPIInt *flag, ...)
161 {
162   PetscFunctionBegin;
163   PetscCallContinue((*PetscErrorPrintf)("MPI error %d\n", *flag));
164   abort();
165 }
166 
167 static void Petsc_MPI_DebuggerOnError(MPI_Comm *comm, PetscMPIInt *flag, ...)
168 {
169   PetscFunctionBegin;
170   PetscCallContinue((*PetscErrorPrintf)("MPI error %d\n", *flag));
171   if (PetscAttachDebugger()) PETSCABORT(*comm, (PetscErrorCode)*flag); /* hopeless so get out */
172 }
173 
174 /*@
175   PetscEnd - Calls `PetscFinalize()` and then ends the program. This is useful if one
176   wishes a clean exit somewhere deep in the program.
177 
178   Collective on `PETSC_COMM_WORLD`
179 
180   Level: advanced
181 
182   Note:
183   See `PetscInitialize()` for more general runtime options.
184 
185 .seealso: `PetscInitialize()`, `PetscOptionsView()`, `PetscMallocDump()`, `PetscMPIDump()`, `PetscFinalize()`
186 @*/
187 PetscErrorCode PetscEnd(void)
188 {
189   PetscFunctionBegin;
190   PetscCall(PetscFinalize());
191   exit(0);
192   return PETSC_SUCCESS;
193 }
194 
195 PetscBool                   PetscOptionsPublish = PETSC_FALSE;
196 PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void);
197 PETSC_INTERN PetscBool      petscsetmallocvisited;
198 static char                 emacsmachinename[256];
199 
200 PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = NULL;
201 PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = NULL;
202 
203 #include <petscviewer.h>
204 
205 /*@C
206   PetscSetHelpVersionFunctions - Sets functions that print help and version information
207   before the PETSc help and version information is printed.
208 
209   No Fortran Support
210 
211   Input Parameters:
212 + help    - the help function (may be `NULL`)
213 - version - the version function (may be `NULL`)
214 
215   Level: developer
216 
217   Notes:
218   Must call BEFORE `PetscInitialize()`.
219 
220   This routine enables a "higher-level" package that uses PETSc to print its messages first and
221   control how the PETSc help messages are printed.
222 
223 .seealso: `PetscInitialize()`
224 @*/
225 PetscErrorCode PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm), PetscErrorCode (*version)(MPI_Comm))
226 {
227   PetscFunctionBegin;
228   PetscExternalHelpFunction    = help;
229   PetscExternalVersionFunction = version;
230   PetscFunctionReturn(PETSC_SUCCESS);
231 }
232 
233 PETSC_INTERN PetscBool PetscObjectsLog;
234 
235 PETSC_INTERN PetscErrorCode PetscOptionsCheckInitial_Private(const char help[])
236 {
237   char        string[64];
238   MPI_Comm    comm = PETSC_COMM_WORLD;
239   PetscBool   flg1 = PETSC_FALSE, flg2 = PETSC_FALSE, flag, hasHelp;
240   PetscBool   checkstack = PETSC_FALSE;
241   PetscReal   si;
242   PetscInt    intensity;
243   int         i;
244   PetscMPIInt rank;
245   char        version[256];
246 
247   PetscFunctionBegin;
248   PetscCallMPI(MPI_Comm_rank(comm, &rank));
249 
250   if (PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)) checkstack = PETSC_TRUE;
251   PetscCall(PetscOptionsGetBool(NULL, NULL, "-checkstack", &checkstack, NULL));
252   PetscCall(PetscStackSetCheck(checkstack));
253 
254   PetscCall(PetscOptionsGetBool(NULL, NULL, "-checkfunctionlist", &PetscPrintFunctionList, NULL));
255 
256 #if !defined(PETSC_HAVE_THREADSAFETY)
257   if (!(PETSC_RUNNING_ON_VALGRIND)) {
258     /*
259       Setup the memory management; support for tracing malloc() usage
260     */
261     PetscBool mdebug = PETSC_FALSE, eachcall = PETSC_FALSE, initializenan = PETSC_FALSE, mlog = PETSC_FALSE;
262     PetscBool flg3 = PETSC_FALSE;
263 
264     if (PetscDefined(USE_DEBUG)) {
265       mdebug        = PETSC_TRUE;
266       initializenan = PETSC_TRUE;
267       PetscCall(PetscOptionsHasName(NULL, NULL, "-malloc_test", &flg1));
268     } else {
269       /* don't warn about unused option */
270       PetscCall(PetscOptionsHasName(NULL, NULL, "-malloc_test", &flg1));
271       flg1 = PETSC_FALSE;
272     }
273     PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_debug", &flg2, &flg3));
274     if (flg1 || flg2) {
275       mdebug        = PETSC_TRUE;
276       eachcall      = PETSC_TRUE;
277       initializenan = PETSC_TRUE;
278     } else if (flg3 && !flg2) {
279       mdebug        = PETSC_FALSE;
280       eachcall      = PETSC_FALSE;
281       initializenan = PETSC_FALSE;
282     }
283 
284     PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_requested_size", &flg1, &flg2));
285     if (flg2) PetscCall(PetscMallocLogRequestedSizeSet(flg1));
286 
287     PetscCall(PetscOptionsHasName(NULL, NULL, "-malloc_view", &mlog));
288     if (mlog) mdebug = PETSC_TRUE;
289     /* the next line is deprecated */
290     PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_dump", &mdebug, NULL));
291     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_view_memory", &mdebug, NULL));
292     if (mdebug) PetscCall(PetscMallocSetDebug(eachcall, initializenan));
293     if (mlog) {
294       PetscReal logthreshold = 0;
295       PetscCall(PetscOptionsGetReal(NULL, NULL, "-malloc_view_threshold", &logthreshold, NULL));
296       PetscCall(PetscMallocViewSet(logthreshold));
297     }
298     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_view_memory", &PetscLogMemory, NULL));
299   }
300 
301   PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_coalesce", &flg1, &flg2));
302   if (flg2) PetscCall(PetscMallocSetCoalesce(flg1));
303   flg1 = PETSC_FALSE;
304   PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_hbw", &flg1, NULL));
305   /* ignore this option if malloc is already set */
306   if (flg1 && !petscsetmallocvisited) PetscCall(PetscSetUseHBWMalloc_Private());
307 
308   flg1 = PETSC_FALSE;
309   PetscCall(PetscOptionsGetBool(NULL, NULL, "-memory_view", &flg1, NULL));
310   if (flg1) PetscCall(PetscMemorySetGetMaximumUsage());
311 #endif
312 
313   PetscCall(PetscOptionsHasName(NULL, NULL, "-objects_dump", &PetscObjectsLog));
314 
315   /*
316       Set the display variable for graphics
317   */
318   PetscCall(PetscSetDisplay());
319 
320   /*
321      Print main application help message
322   */
323   PetscCall(PetscOptionsHasHelp(NULL, &hasHelp));
324   if (help && hasHelp) {
325     PetscCall(PetscPrintf(comm, "%s", help));
326     PetscCall(PetscPrintf(comm, "----------------------------------------\n"));
327   }
328 
329   /*
330       Print the PETSc version information
331   */
332   PetscCall(PetscOptionsHasName(NULL, NULL, "-version", &flg1));
333   if (flg1 || hasHelp) {
334     /*
335        Print "higher-level" package version message
336     */
337     if (PetscExternalVersionFunction) PetscCall((*PetscExternalVersionFunction)(comm));
338 
339     PetscCall(PetscGetVersion(version, 256));
340     if (!PetscCIEnabledPortableErrorOutput) PetscCall((*PetscHelpPrintf)(comm, "%s\n", version));
341     PetscCall((*PetscHelpPrintf)(comm, "%s", PETSC_AUTHOR_INFO));
342     PetscCall((*PetscHelpPrintf)(comm, "See https://petsc.org/release/changes for recent updates.\n"));
343     PetscCall((*PetscHelpPrintf)(comm, "See https://petsc.org/release/faq for problems.\n"));
344     PetscCall((*PetscHelpPrintf)(comm, "See https://petsc.org/release/manualpages for help.\n"));
345     if (!PetscCIEnabledPortableErrorOutput) PetscCall((*PetscHelpPrintf)(comm, "Libraries linked from %s\n", PETSC_LIB_DIR));
346     PetscCall((*PetscHelpPrintf)(comm, "----------------------------------------\n"));
347   }
348 
349   /*
350        Print "higher-level" package help message
351   */
352   if (hasHelp) {
353     PetscBool hasHelpIntro;
354 
355     if (PetscExternalHelpFunction) PetscCall((*PetscExternalHelpFunction)(comm));
356     PetscCall(PetscOptionsHasHelpIntro_Internal(NULL, &hasHelpIntro));
357     if (hasHelpIntro) {
358       PetscCall(PetscOptionsDestroyDefault());
359       PetscCall(PetscFreeMPIResources());
360       PetscCallMPI(MPI_Finalize());
361       exit(0);
362     }
363   }
364 
365   /*
366       Setup the error handling
367   */
368   flg1 = PETSC_FALSE;
369   PetscCall(PetscOptionsGetBool(NULL, NULL, "-on_error_abort", &flg1, NULL));
370   if (flg1) {
371     PetscCallMPI(MPI_Comm_set_errhandler(comm, MPI_ERRORS_ARE_FATAL));
372     PetscCall(PetscPushErrorHandler(PetscAbortErrorHandler, NULL));
373   }
374   flg1 = PETSC_FALSE;
375   PetscCall(PetscOptionsGetBool(NULL, NULL, "-on_error_mpiabort", &flg1, NULL));
376   if (flg1) PetscCall(PetscPushErrorHandler(PetscMPIAbortErrorHandler, NULL));
377   flg1 = PETSC_FALSE;
378   PetscCall(PetscOptionsGetBool(NULL, NULL, "-mpi_return_on_error", &flg1, NULL));
379   if (flg1) PetscCallMPI(MPI_Comm_set_errhandler(comm, MPI_ERRORS_RETURN));
380   flg1 = PETSC_FALSE;
381   PetscCall(PetscOptionsGetBool(NULL, NULL, "-no_signal_handler", &flg1, NULL));
382   if (!flg1) PetscCall(PetscPushSignalHandler(PetscSignalHandlerDefault, NULL));
383 
384   /*
385       Setup debugger information
386   */
387   PetscCall(PetscSetDefaultDebugger());
388   PetscCall(PetscOptionsGetString(NULL, NULL, "-on_error_attach_debugger", string, sizeof(string), &flg1));
389   if (flg1) {
390     MPI_Errhandler err_handler;
391 
392     PetscCall(PetscSetDebuggerFromString(string));
393     PetscCallMPI(MPI_Comm_create_errhandler(Petsc_MPI_DebuggerOnError, &err_handler));
394     PetscCallMPI(MPI_Comm_set_errhandler(comm, err_handler));
395     PetscCall(PetscPushErrorHandler(PetscAttachDebuggerErrorHandler, NULL));
396   }
397   PetscCall(PetscOptionsGetString(NULL, NULL, "-debug_terminal", string, sizeof(string), &flg1));
398   if (flg1) PetscCall(PetscSetDebugTerminal(string));
399   PetscCall(PetscOptionsGetString(NULL, NULL, "-start_in_debugger", string, sizeof(string), &flg1));
400   PetscCall(PetscOptionsGetString(NULL, NULL, "-stop_for_debugger", string, sizeof(string), &flg2));
401   if (flg1 || flg2) {
402     PetscMPIInt    size;
403     PetscInt       lsize, *ranks;
404     MPI_Errhandler err_handler;
405     /*
406        we have to make sure that all processors have opened
407        connections to all other processors, otherwise once the
408        debugger has stated it is likely to receive a SIGUSR1
409        and kill the program.
410     */
411     PetscCallMPI(MPI_Comm_size(comm, &size));
412     if (size > 2) {
413       PetscMPIInt dummy = 0;
414       MPI_Status  status;
415       for (i = 0; i < size; i++) {
416         if (rank != i) PetscCallMPI(MPI_Send(&dummy, 1, MPI_INT, i, 109, comm));
417       }
418       for (i = 0; i < size; i++) {
419         if (rank != i) PetscCallMPI(MPI_Recv(&dummy, 1, MPI_INT, i, 109, comm, &status));
420       }
421     }
422     /* check if this processor node should be in debugger */
423     PetscCall(PetscMalloc1(size, &ranks));
424     lsize = size;
425     /* Deprecated in 3.14 */
426     PetscCall(PetscOptionsGetIntArray(NULL, NULL, "-debugger_nodes", ranks, &lsize, &flag));
427     if (flag) {
428       const char *const quietopt = "-options_suppress_deprecated_warnings";
429       char              msg[4096];
430       PetscBool         quiet = PETSC_FALSE;
431 
432       PetscCall(PetscOptionsGetBool(NULL, NULL, quietopt, &quiet, NULL));
433       if (!quiet) {
434         PetscCall(PetscStrncpy(msg, "** PETSc DEPRECATION WARNING ** : the option ", sizeof(msg)));
435         PetscCall(PetscStrlcat(msg, "-debugger_nodes", sizeof(msg)));
436         PetscCall(PetscStrlcat(msg, " is deprecated as of version ", sizeof(msg)));
437         PetscCall(PetscStrlcat(msg, "3.14", sizeof(msg)));
438         PetscCall(PetscStrlcat(msg, " and will be removed in a future release.", sizeof(msg)));
439         PetscCall(PetscStrlcat(msg, " Please use the option ", sizeof(msg)));
440         PetscCall(PetscStrlcat(msg, "-debugger_ranks", sizeof(msg)));
441         PetscCall(PetscStrlcat(msg, " instead.", sizeof(msg)));
442         PetscCall(PetscStrlcat(msg, " (Silence this warning with ", sizeof(msg)));
443         PetscCall(PetscStrlcat(msg, quietopt, sizeof(msg)));
444         PetscCall(PetscStrlcat(msg, ")\n", sizeof(msg)));
445         PetscCall(PetscPrintf(comm, "%s", msg));
446       }
447     } else {
448       lsize = size;
449       PetscCall(PetscOptionsGetIntArray(NULL, NULL, "-debugger_ranks", ranks, &lsize, &flag));
450     }
451     if (flag) {
452       for (i = 0; i < lsize; i++) {
453         if (ranks[i] == rank) {
454           flag = PETSC_FALSE;
455           break;
456         }
457       }
458     }
459     if (!flag) {
460       PetscCall(PetscSetDebuggerFromString(string));
461       PetscCall(PetscPushErrorHandler(PetscAbortErrorHandler, NULL));
462       if (flg1) {
463         PetscCall(PetscAttachDebugger());
464       } else {
465         PetscCall(PetscStopForDebugger());
466       }
467       PetscCallMPI(MPI_Comm_create_errhandler(Petsc_MPI_AbortOnError, &err_handler));
468       PetscCallMPI(MPI_Comm_set_errhandler(comm, err_handler));
469     } else {
470       PetscCall(PetscWaitOnError());
471     }
472     PetscCall(PetscFree(ranks));
473   }
474 
475   PetscCall(PetscOptionsGetString(NULL, NULL, "-on_error_emacs", emacsmachinename, sizeof(emacsmachinename), &flg1));
476   if (flg1 && rank == 0) PetscCall(PetscPushErrorHandler(PetscEmacsClientErrorHandler, emacsmachinename));
477 
478   /*
479         Setup profiling and logging
480   */
481   if (PetscDefined(USE_LOG)) PetscCall(PetscInfoSetFromOptions(NULL));
482   PetscCall(PetscDetermineInitialFPTrap());
483   flg1 = PETSC_FALSE;
484   PetscCall(PetscOptionsGetBool(NULL, NULL, "-fp_trap", &flg1, &flag));
485   if (flag) PetscCall(PetscSetFPTrap(flg1 ? PETSC_FP_TRAP_ON : PETSC_FP_TRAP_OFF));
486   PetscCall(PetscOptionsGetInt(NULL, NULL, "-check_pointer_intensity", &intensity, &flag));
487   if (flag) PetscCall(PetscCheckPointerSetIntensity(intensity));
488   if (PetscDefined(USE_LOG)) {
489     char              mname[PETSC_MAX_PATH_LEN];
490     PetscInt          n_max = PETSC_LOG_VIEW_FROM_OPTIONS_MAX;
491     PetscViewerFormat format[PETSC_LOG_VIEW_FROM_OPTIONS_MAX];
492     PetscBool         ci_log = PetscCIEnabled;
493 
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     }
503 
504     if (ci_log) {
505       static const char *LogOptions[] = {"-log_view", "-log_mpe", "-log_perfstubs", "-log_nvtx", "-log_roctx", "-log", "-log_all"};
506 
507       for (size_t i = 0; i < PETSC_STATIC_ARRAY_LENGTH(LogOptions); i++) {
508         PetscCall(PetscOptionsHasName(NULL, NULL, LogOptions[i], &flg1));
509         if (flg1) {
510           ci_log = PETSC_FALSE;
511           break;
512         }
513       }
514     }
515     if (ci_log) PetscLogSyncOn = PETSC_TRUE;
516     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_sync", &PetscLogSyncOn, NULL));
517 
518     if (PetscDefined(HAVE_MPE)) {
519       flg1 = PETSC_FALSE;
520       PetscCall(PetscOptionsHasName(NULL, NULL, "-log_mpe", &flg1));
521       if (flg1) PetscCall(PetscLogMPEBegin());
522     }
523     if (PetscDefined(HAVE_TAU_PERFSTUBS)) {
524       char     *tau_exec_path       = getenv("TAU_EXEC_PATH");
525       PetscBool start_log_perfstubs = (tau_exec_path != NULL) ? PETSC_TRUE : PETSC_FALSE;
526 
527       if (tau_exec_path && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected tau_exec path %s\n", tau_exec_path));
528       PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_perfstubs", &start_log_perfstubs, NULL));
529       if (start_log_perfstubs) PetscCall(PetscLogPerfstubsBegin());
530     }
531     if (PetscDefined(USE_LOG) && PetscDefined(HAVE_CUDA)) {
532       char     *nsys_profiling_session_id = getenv("NSYS_PROFILING_SESSION_ID");
533       char     *nvprof_id                 = getenv("NVPROF_ID");
534       PetscBool start_log_nvtx            = ((nsys_profiling_session_id != NULL) || (nvprof_id != NULL)) ? PETSC_TRUE : PETSC_FALSE;
535 
536       if (nsys_profiling_session_id && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected nsys profiling session id %s\n", nsys_profiling_session_id));
537       if (nvprof_id && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected nvprof session id %s\n", nvprof_id));
538       PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_nvtx", &start_log_nvtx, NULL));
539       if (start_log_nvtx) PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERNVTX));
540     }
541     if (PetscDefined(USE_LOG) && PetscDefined(HAVE_ROCTX)) {
542       char     *rocprof_roctx_trace  = getenv("ROCPROFILER_ROCTX_TRACE");
543       char     *rocprof_marker_trace = getenv("ROCPROF_MARKER_API_TRACE");
544       PetscBool start_log_roctx      = ((rocprof_roctx_trace != NULL) || (rocprof_marker_trace != NULL)) ? PETSC_TRUE : PETSC_FALSE;
545 
546       if (rocprof_roctx_trace && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected rocprofv2 profiling session\n"));
547       if (rocprof_marker_trace && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected rocprofv3 profiling session\n"));
548       PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_roctx", &start_log_roctx, NULL));
549       if (start_log_roctx) PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERROCTX));
550     }
551     flg1 = PETSC_FALSE;
552     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_all", &flg1, NULL));
553     PetscCall(PetscOptionsGetBool(NULL, NULL, "-log", &flg2, NULL));
554     if (flg1 || flg2 || ci_log) PetscCall(PetscLogDefaultBegin());
555 
556     PetscCall(PetscOptionsGetString(NULL, NULL, "-log_trace", mname, sizeof(mname), &flg1));
557     if (flg1) {
558       char  name[PETSC_MAX_PATH_LEN], fname[PETSC_MAX_PATH_LEN];
559       FILE *file;
560       if (mname[0]) {
561         PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "%s.%d", mname, rank));
562         PetscCall(PetscFixFilename(name, fname));
563         file = fopen(fname, "w");
564         PetscCheck(file, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to open trace file: %s", fname);
565       } else file = PETSC_STDOUT;
566       PetscCall(PetscLogTraceBegin(file));
567     }
568 
569     PetscCall(PetscOptionsCreateViewers(comm, NULL, NULL, "-log_view", &n_max, NULL, format, NULL));
570     if (n_max > 0) {
571       PetscBool any_nested  = PETSC_FALSE;
572       PetscBool any_default = PETSC_FALSE;
573 
574       for (PetscInt i = 0; i < n_max; i++) {
575         if (format[i] == PETSC_VIEWER_ASCII_XML || format[i] == PETSC_VIEWER_ASCII_FLAMEGRAPH) {
576           any_nested = PETSC_TRUE;
577         } else {
578           any_default = PETSC_TRUE;
579         }
580       }
581       if (any_default) PetscCall(PetscLogDefaultBegin());
582       if (any_nested) {
583         PetscCall(PetscLogNestedBegin());
584         PetscReal threshold = PetscRealConstant(0.01);
585         PetscCall(PetscOptionsGetReal(NULL, NULL, "-log_threshold", &threshold, &flg1));
586         if (flg1) PetscCall(PetscLogSetThreshold((PetscLogDouble)threshold, NULL));
587       }
588     }
589   }
590 
591   PetscCall(PetscOptionsGetBool(NULL, NULL, "-saws_options", &PetscOptionsPublish, NULL));
592   PetscCall(PetscOptionsGetBool(NULL, NULL, "-use_gpu_aware_mpi", &use_gpu_aware_mpi, &flg1));
593   if (!flg1) PetscCall(PetscOptionsGetBool(NULL, NULL, "-sf_use_gpu_aware_mpi", &use_gpu_aware_mpi, &flg1)); // an alias option
594 
595   /*
596        Print basic help message
597   */
598   if (hasHelp) {
599     PetscCall((*PetscHelpPrintf)(comm, "Options for all PETSc programs:\n"));
600     PetscCall((*PetscHelpPrintf)(comm, " -version: prints PETSc version\n"));
601     PetscCall((*PetscHelpPrintf)(comm, " -help intro: prints example description and PETSc version, and exits\n"));
602     PetscCall((*PetscHelpPrintf)(comm, " -help: prints example description, PETSc version, and available options for used routines\n"));
603     PetscCall((*PetscHelpPrintf)(comm, " -on_error_abort: cause an abort when an error is detected. Useful \n "));
604     PetscCall((*PetscHelpPrintf)(comm, "       only when run in the debugger\n"));
605     PetscCall((*PetscHelpPrintf)(comm, " -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n"));
606     PetscCall((*PetscHelpPrintf)(comm, "       start the debugger in new xterm\n"));
607     PetscCall((*PetscHelpPrintf)(comm, "       unless noxterm is given\n"));
608     PetscCall((*PetscHelpPrintf)(comm, " -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n"));
609     PetscCall((*PetscHelpPrintf)(comm, "       start all processes in the debugger\n"));
610     PetscCall((*PetscHelpPrintf)(comm, " -on_error_emacs <machinename>\n"));
611     PetscCall((*PetscHelpPrintf)(comm, "    emacs jumps to error file\n"));
612     PetscCall((*PetscHelpPrintf)(comm, " -debugger_ranks [n1,n2,..] Ranks to start in debugger\n"));
613     PetscCall((*PetscHelpPrintf)(comm, " -debugger_pause [m] : delay (in seconds) to attach debugger\n"));
614     PetscCall((*PetscHelpPrintf)(comm, " -stop_for_debugger : prints message on how to attach debugger manually\n"));
615     PetscCall((*PetscHelpPrintf)(comm, "                      waits the delay for you to attach\n"));
616     PetscCall((*PetscHelpPrintf)(comm, " -display display: Location where X window graphics and debuggers are displayed\n"));
617     PetscCall((*PetscHelpPrintf)(comm, " -no_signal_handler: do not trap error signals\n"));
618     PetscCall((*PetscHelpPrintf)(comm, " -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n"));
619     PetscCall((*PetscHelpPrintf)(comm, " -fp_trap: stop on floating point exceptions\n"));
620     PetscCall((*PetscHelpPrintf)(comm, "           note on IBM RS6000 this slows run greatly\n"));
621     PetscCall((*PetscHelpPrintf)(comm, " -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n"));
622     PetscCall((*PetscHelpPrintf)(comm, " -on_error_malloc_dump <optional filename>: dump list of unfreed memory on memory error\n"));
623     PetscCall((*PetscHelpPrintf)(comm, " -malloc_view <optional filename>: keeps log of all memory allocations, displays in PetscFinalize()\n"));
624     PetscCall((*PetscHelpPrintf)(comm, " -malloc_debug <true or false>: enables or disables extended checking for memory corruption\n"));
625     PetscCall((*PetscHelpPrintf)(comm, " -options_view: dump list of options inputted\n"));
626     PetscCall((*PetscHelpPrintf)(comm, " -options_left: dump list of unused options\n"));
627     PetscCall((*PetscHelpPrintf)(comm, " -options_left no: don't dump list of unused options\n"));
628     PetscCall((*PetscHelpPrintf)(comm, " -tmp tmpdir: alternative /tmp directory\n"));
629     PetscCall((*PetscHelpPrintf)(comm, " -shared_tmp: tmp directory is shared by all processors\n"));
630     PetscCall((*PetscHelpPrintf)(comm, " -not_shared_tmp: each processor has separate tmp directory\n"));
631     PetscCall((*PetscHelpPrintf)(comm, " -memory_view: print memory usage at end of run\n"));
632 #if defined(PETSC_USE_LOG)
633     PetscCall((*PetscHelpPrintf)(comm, " -get_total_flops: total flops over all processors\n"));
634     PetscCall((*PetscHelpPrintf)(comm, " -log_view [:filename:[format]]: logging objects and events\n"));
635     PetscCall((*PetscHelpPrintf)(comm, " -log_trace [filename]: prints trace of all PETSc calls\n"));
636     PetscCall((*PetscHelpPrintf)(comm, " -log_exclude <list,of,classnames>: exclude given classes from logging\n"));
637   #if defined(PETSC_HAVE_DEVICE)
638     PetscCall((*PetscHelpPrintf)(comm, " -log_view_gpu_time: log the GPU time for each event\n"));
639     PetscCall((*PetscHelpPrintf)(comm, " -log_view_gpu_energy: log the GPU energy (estimated) for each event\n"));
640     PetscCall((*PetscHelpPrintf)(comm, " -log_view_gpu_energy_meter: log the GPU energy (readings from meters) for each event\n"));
641   #endif
642   #if defined(PETSC_HAVE_MPE)
643     PetscCall((*PetscHelpPrintf)(comm, " -log_mpe: Also create logfile viewable through Jumpshot\n"));
644   #endif
645   #if PetscDefined(HAVE_CUDA)
646     PetscCall((*PetscHelpPrintf)(comm, " -log_nvtx: Create nvtx event ranges for Nsight\n"));
647   #endif
648   #if PetscDefined(HAVE_HIP)
649     PetscCall((*PetscHelpPrintf)(comm, " -log_roctx: Create roctx event ranges for rocprof\n"));
650   #endif
651 #endif
652 #if defined(PETSC_USE_INFO)
653     PetscCall((*PetscHelpPrintf)(comm, " -info [filename][:[~]<list,of,classnames>[:[~]self]]: print verbose information\n"));
654 #endif
655     PetscCall((*PetscHelpPrintf)(comm, " -options_file <file>: reads options from file\n"));
656     PetscCall((*PetscHelpPrintf)(comm, " -options_monitor: monitor options to standard output, including that set previously e.g. in option files\n"));
657     PetscCall((*PetscHelpPrintf)(comm, " -options_monitor_cancel: cancels all hardwired option monitors\n"));
658     PetscCall((*PetscHelpPrintf)(comm, " -petsc_sleep n: sleeps n seconds before running program\n"));
659   }
660 
661 #if defined(PETSC_HAVE_POPEN)
662   {
663     char machine[128];
664     PetscCall(PetscOptionsGetString(NULL, NULL, "-popen_machine", machine, sizeof(machine), &flg1));
665     if (flg1) PetscCall(PetscPOpenSetMachine(machine));
666   }
667 #endif
668 
669   PetscCall(PetscOptionsGetReal(NULL, NULL, "-petsc_sleep", &si, &flg1));
670   if (flg1) PetscCall(PetscSleep(si));
671   PetscFunctionReturn(PETSC_SUCCESS);
672 }
673