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