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