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