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