xref: /petsc/src/sys/objects/init.c (revision 660278c0612eddb50605b63d23f7384c6f3231af)
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   if (PetscDefined(USE_DEBUG) && !PetscDefined(HAVE_THREADSAFETY)) PetscCall(PetscStackSetCheck(PETSC_TRUE));
261   PetscCall(PetscOptionsGetBool(NULL,NULL,"-checkfunctionlist",&PetscPrintFunctionList,NULL));
262 
263 #if !defined(PETSC_HAVE_THREADSAFETY)
264   if (!(PETSC_RUNNING_ON_VALGRIND)) {
265     /*
266       Setup the memory management; support for tracing malloc() usage
267     */
268     PetscBool mdebug = PETSC_FALSE, eachcall = PETSC_FALSE, initializenan = PETSC_FALSE, mlog = PETSC_FALSE;
269 
270     if (PetscDefined(USE_DEBUG)) {
271       mdebug        = PETSC_TRUE;
272       initializenan = PETSC_TRUE;
273       PetscCall(PetscOptionsHasName(NULL,NULL,"-malloc_test",&flg1));
274     } else {
275       /* don't warn about unused option */
276       PetscCall(PetscOptionsHasName(NULL,NULL,"-malloc_test",&flg1));
277       flg1 = PETSC_FALSE;
278     }
279     PetscCall(PetscOptionsGetBool(NULL,NULL,"-malloc_debug",&flg2,&flg3));
280     if (flg1 || flg2) {
281       mdebug        = PETSC_TRUE;
282       eachcall      = PETSC_TRUE;
283       initializenan = PETSC_TRUE;
284     } else if (flg3 && !flg2) {
285       mdebug        = PETSC_FALSE;
286       eachcall      = PETSC_FALSE;
287       initializenan = PETSC_FALSE;
288     }
289 
290     PetscCall(PetscOptionsGetBool(NULL,NULL,"-malloc_requested_size",&flg1,&flg2));
291     if (flg2) PetscCall(PetscMallocLogRequestedSizeSet(flg1));
292 
293     PetscCall(PetscOptionsHasName(NULL,NULL,"-malloc_view",&mlog));
294     if (mlog) {
295       mdebug = PETSC_TRUE;
296     }
297     /* the next line is deprecated */
298     PetscCall(PetscOptionsGetBool(NULL,NULL,"-malloc",&mdebug,NULL));
299     PetscCall(PetscOptionsGetBool(NULL,NULL,"-malloc_dump",&mdebug,NULL));
300     PetscCall(PetscOptionsGetBool(NULL,NULL,"-log_view_memory",&mdebug,NULL));
301     if (mdebug) PetscCall(PetscMallocSetDebug(eachcall,initializenan));
302     if (mlog) {
303       PetscReal logthreshold = 0;
304       PetscCall(PetscOptionsGetReal(NULL,NULL,"-malloc_view_threshold",&logthreshold,NULL));
305       PetscCall(PetscMallocViewSet(logthreshold));
306     }
307 #if defined(PETSC_USE_LOG)
308     PetscCall(PetscOptionsGetBool(NULL,NULL,"-log_view_memory",&PetscLogMemory,NULL));
309 #endif
310   }
311 
312   PetscCall(PetscOptionsGetBool(NULL,NULL,"-malloc_coalesce",&flg1,&flg2));
313   if (flg2) PetscCall(PetscMallocSetCoalesce(flg1));
314   flg1 = PETSC_FALSE;
315   PetscCall(PetscOptionsGetBool(NULL,NULL,"-malloc_hbw",&flg1,NULL));
316   /* ignore this option if malloc is already set */
317   if (flg1 && !petscsetmallocvisited) PetscCall(PetscSetUseHBWMalloc_Private());
318 
319   flg1 = PETSC_FALSE;
320   PetscCall(PetscOptionsGetBool(NULL,NULL,"-malloc_info",&flg1,NULL));
321   if (!flg1) {
322     flg1 = PETSC_FALSE;
323     PetscCall(PetscOptionsGetBool(NULL,NULL,"-memory_view",&flg1,NULL));
324   }
325   if (flg1) PetscCall(PetscMemorySetGetMaximumUsage());
326 #endif
327 
328 #if defined(PETSC_USE_LOG)
329   PetscCall(PetscOptionsHasName(NULL,NULL,"-objects_dump",&PetscObjectsLog));
330 #endif
331 
332   /*
333       Set the display variable for graphics
334   */
335   PetscCall(PetscSetDisplay());
336 
337   /*
338      Print main application help message
339   */
340   PetscCall(PetscOptionsHasHelp(NULL,&hasHelp));
341   if (help && hasHelp) {
342     PetscCall(PetscPrintf(comm,"%s",help));
343     PetscCall(PetscPrintf(comm,"----------------------------------------\n"));
344   }
345 
346   /*
347       Print the PETSc version information
348   */
349   PetscCall(PetscOptionsHasName(NULL,NULL,"-version",&flg1));
350   if (flg1 || hasHelp) {
351     /*
352        Print "higher-level" package version message
353     */
354     if (PetscExternalVersionFunction) PetscCall((*PetscExternalVersionFunction)(comm));
355 
356     PetscCall(PetscGetVersion(version,256));
357     PetscCall((*PetscHelpPrintf)(comm,"%s\n",version));
358     PetscCall((*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO));
359     PetscCall((*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n"));
360     PetscCall((*PetscHelpPrintf)(comm,"See docs/faq.html for problems.\n"));
361     PetscCall((*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n"));
362     PetscCall((*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR));
363     PetscCall((*PetscHelpPrintf)(comm,"----------------------------------------\n"));
364   }
365 
366   /*
367        Print "higher-level" package help message
368   */
369   if (hasHelp) {
370     PetscBool hasHelpIntro;
371 
372     if (PetscExternalHelpFunction) PetscCall((*PetscExternalHelpFunction)(comm));
373     PetscCall(PetscOptionsHasHelpIntro_Internal(NULL,&hasHelpIntro));
374     if (hasHelpIntro) {
375       PetscCall(PetscOptionsDestroyDefault());
376       PetscCall(PetscFreeMPIResources());
377       PetscCallMPI(MPI_Finalize());
378       exit(0);
379     }
380   }
381 
382   /*
383       Setup the error handling
384   */
385   flg1 = PETSC_FALSE;
386   PetscCall(PetscOptionsGetBool(NULL,NULL,"-on_error_abort",&flg1,NULL));
387   if (flg1) {
388     PetscCallMPI(MPI_Comm_set_errhandler(comm,MPI_ERRORS_ARE_FATAL));
389     PetscCall(PetscPushErrorHandler(PetscAbortErrorHandler,NULL));
390   }
391   flg1 = PETSC_FALSE;
392   PetscCall(PetscOptionsGetBool(NULL,NULL,"-on_error_mpiabort",&flg1,NULL));
393   if (flg1) PetscCall(PetscPushErrorHandler(PetscMPIAbortErrorHandler,NULL));
394   flg1 = PETSC_FALSE;
395   PetscCall(PetscOptionsGetBool(NULL,NULL,"-mpi_return_on_error",&flg1,NULL));
396   if (flg1) {
397     PetscCallMPI(MPI_Comm_set_errhandler(comm,MPI_ERRORS_RETURN));
398   }
399   flg1 = PETSC_FALSE;
400   PetscCall(PetscOptionsGetBool(NULL,NULL,"-no_signal_handler",&flg1,NULL));
401   if (!flg1) PetscCall(PetscPushSignalHandler(PetscSignalHandlerDefault,(void*)0));
402 
403   /*
404       Setup debugger information
405   */
406   PetscCall(PetscSetDefaultDebugger());
407   PetscCall(PetscOptionsGetString(NULL,NULL,"-on_error_attach_debugger",string,sizeof(string),&flg1));
408   if (flg1) {
409     MPI_Errhandler err_handler;
410 
411     PetscCall(PetscSetDebuggerFromString(string));
412     PetscCallMPI(MPI_Comm_create_errhandler(Petsc_MPI_DebuggerOnError,&err_handler));
413     PetscCallMPI(MPI_Comm_set_errhandler(comm,err_handler));
414     PetscCall(PetscPushErrorHandler(PetscAttachDebuggerErrorHandler,NULL));
415   }
416   PetscCall(PetscOptionsGetString(NULL,NULL,"-debug_terminal",string,sizeof(string),&flg1));
417   if (flg1) PetscCall(PetscSetDebugTerminal(string));
418   PetscCall(PetscOptionsGetString(NULL,NULL,"-start_in_debugger",string,sizeof(string),&flg1));
419   PetscCall(PetscOptionsGetString(NULL,NULL,"-stop_for_debugger",string,sizeof(string),&flg2));
420   if (flg1 || flg2) {
421     PetscMPIInt    size;
422     PetscInt       lsize,*ranks;
423     MPI_Errhandler err_handler;
424     /*
425        we have to make sure that all processors have opened
426        connections to all other processors, otherwise once the
427        debugger has stated it is likely to receive a SIGUSR1
428        and kill the program.
429     */
430     PetscCallMPI(MPI_Comm_size(comm,&size));
431     if (size > 2) {
432       PetscMPIInt dummy = 0;
433       MPI_Status  status;
434       for (i=0; i<size; i++) {
435         if (rank != i) {
436           PetscCallMPI(MPI_Send(&dummy,1,MPI_INT,i,109,comm));
437         }
438       }
439       for (i=0; i<size; i++) {
440         if (rank != i) {
441           PetscCallMPI(MPI_Recv(&dummy,1,MPI_INT,i,109,comm,&status));
442         }
443       }
444     }
445     /* check if this processor node should be in debugger */
446     PetscCall(PetscMalloc1(size,&ranks));
447     lsize = size;
448     /* Deprecated in 3.14 */
449     PetscCall(PetscOptionsGetIntArray(NULL,NULL,"-debugger_nodes",ranks,&lsize,&flag));
450     if (flag) {
451       const char * const quietopt="-options_suppress_deprecated_warnings";
452       char               msg[4096];
453       PetscBool          quiet = PETSC_FALSE;
454 
455       PetscCall(PetscOptionsGetBool(NULL,NULL,quietopt,&quiet,NULL));
456       if (!quiet) {
457         PetscCall(PetscStrcpy(msg,"** PETSc DEPRECATION WARNING ** : the option "));
458         PetscCall(PetscStrcat(msg,"-debugger_nodes"));
459         PetscCall(PetscStrcat(msg," is deprecated as of version "));
460         PetscCall(PetscStrcat(msg,"3.14"));
461         PetscCall(PetscStrcat(msg," and will be removed in a future release."));
462         PetscCall(PetscStrcat(msg," Please use the option "));
463         PetscCall(PetscStrcat(msg,"-debugger_ranks"));
464         PetscCall(PetscStrcat(msg," instead."));
465         PetscCall(PetscStrcat(msg," (Silence this warning with "));
466         PetscCall(PetscStrcat(msg,quietopt));
467         PetscCall(PetscStrcat(msg,")\n"));
468         PetscCall(PetscPrintf(comm,"%s",msg));
469       }
470     } else {
471       lsize = size;
472       PetscCall(PetscOptionsGetIntArray(NULL,NULL,"-debugger_ranks",ranks,&lsize,&flag));
473     }
474     if (flag) {
475       for (i=0; i<lsize; i++) {
476         if (ranks[i] == rank) { flag = PETSC_FALSE; break; }
477       }
478     }
479     if (!flag) {
480       PetscCall(PetscSetDebuggerFromString(string));
481       PetscCall(PetscPushErrorHandler(PetscAbortErrorHandler,NULL));
482       if (flg1) {
483         PetscCall(PetscAttachDebugger());
484       } else {
485         PetscCall(PetscStopForDebugger());
486       }
487       PetscCallMPI(MPI_Comm_create_errhandler(Petsc_MPI_AbortOnError,&err_handler));
488       PetscCallMPI(MPI_Comm_set_errhandler(comm,err_handler));
489     } else {
490       PetscCall(PetscWaitOnError());
491     }
492     PetscCall(PetscFree(ranks));
493   }
494 
495   PetscCall(PetscOptionsGetString(NULL,NULL,"-on_error_emacs",emacsmachinename,sizeof(emacsmachinename),&flg1));
496   if (flg1 && rank == 0) PetscCall(PetscPushErrorHandler(PetscEmacsClientErrorHandler,emacsmachinename));
497 
498   /*
499         Setup profiling and logging
500   */
501 #if defined(PETSC_USE_INFO)
502   {
503     PetscCall(PetscInfoSetFromOptions(NULL));
504   }
505 #endif
506   PetscCall(PetscDetermineInitialFPTrap());
507   flg1 = PETSC_FALSE;
508   PetscCall(PetscOptionsGetBool(NULL,NULL,"-fp_trap",&flg1,&flag));
509   if (flag) PetscCall(PetscSetFPTrap((PetscFPTrap)flg1));
510   PetscCall(PetscOptionsGetInt(NULL,NULL,"-check_pointer_intensity",&intensity,&flag));
511   if (flag) PetscCall(PetscCheckPointerSetIntensity(intensity));
512 #if defined(PETSC_USE_LOG)
513   mname[0] = 0;
514   PetscCall(PetscOptionsGetString(NULL,NULL,"-history",mname,sizeof(mname),&flg1));
515   if (flg1) {
516     if (mname[0]) {
517       PetscCall(PetscOpenHistoryFile(mname,&petsc_history));
518     } else {
519       PetscCall(PetscOpenHistoryFile(NULL,&petsc_history));
520     }
521   }
522 
523   PetscCall(PetscOptionsGetBool(NULL,NULL,"-log_sync",&PetscLogSyncOn,NULL));
524 
525 #if defined(PETSC_HAVE_MPE)
526   flg1 = PETSC_FALSE;
527   PetscCall(PetscOptionsHasName(NULL,NULL,"-log_mpe",&flg1));
528   if (flg1) PetscCall(PetscLogMPEBegin());
529 #endif
530   flg1 = PETSC_FALSE;
531   flg3 = PETSC_FALSE;
532   PetscCall(PetscOptionsGetBool(NULL,NULL,"-log_all",&flg1,NULL));
533   PetscCall(PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3));
534   if (flg1)                      PetscCall(PetscLogAllBegin());
535   else if (flg3)                 PetscCall(PetscLogDefaultBegin());
536 
537   PetscCall(PetscOptionsGetString(NULL,NULL,"-log_trace",mname,sizeof(mname),&flg1));
538   if (flg1) {
539     char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN];
540     FILE *file;
541     if (mname[0]) {
542       PetscSNPrintf(name,PETSC_MAX_PATH_LEN,"%s.%d",mname,rank);
543       PetscCall(PetscFixFilename(name,fname));
544       file = fopen(fname,"w");
545       PetscCheck(file,PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname);
546     } else file = PETSC_STDOUT;
547     PetscCall(PetscLogTraceBegin(file));
548   }
549 
550   PetscCall(PetscOptionsGetViewer(comm,NULL,NULL,"-log_view",NULL,&format,&flg4));
551   if (flg4) {
552     if (format == PETSC_VIEWER_ASCII_XML || format == PETSC_VIEWER_ASCII_FLAMEGRAPH) {
553       PetscCall(PetscLogNestedBegin());
554     } else {
555       PetscCall(PetscLogDefaultBegin());
556     }
557   }
558   if (flg4 && (format == PETSC_VIEWER_ASCII_XML || format == PETSC_VIEWER_ASCII_FLAMEGRAPH)) {
559     PetscReal threshold = PetscRealConstant(0.01);
560     PetscCall(PetscOptionsGetReal(NULL,NULL,"-log_threshold",&threshold,&flg1));
561     if (flg1) PetscCall(PetscLogSetThreshold((PetscLogDouble)threshold,NULL));
562   }
563 #endif
564 
565   PetscCall(PetscOptionsGetBool(NULL,NULL,"-saws_options",&PetscOptionsPublish,NULL));
566   PetscCall(PetscOptionsGetBool(NULL,NULL,"-use_gpu_aware_mpi",&use_gpu_aware_mpi,NULL));
567 
568   /*
569        Print basic help message
570   */
571   if (hasHelp) {
572     PetscCall((*PetscHelpPrintf)(comm,"Options for all PETSc programs:\n"));
573     PetscCall((*PetscHelpPrintf)(comm," -version: prints PETSc version\n"));
574     PetscCall((*PetscHelpPrintf)(comm," -help intro: prints example description and PETSc version, and exits\n"));
575     PetscCall((*PetscHelpPrintf)(comm," -help: prints example description, PETSc version, and available options for used routines\n"));
576     PetscCall((*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is detected. Useful \n "));
577     PetscCall((*PetscHelpPrintf)(comm,"       only when run in the debugger\n"));
578     PetscCall((*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n"));
579     PetscCall((*PetscHelpPrintf)(comm,"       start the debugger in new xterm\n"));
580     PetscCall((*PetscHelpPrintf)(comm,"       unless noxterm is given\n"));
581     PetscCall((*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n"));
582     PetscCall((*PetscHelpPrintf)(comm,"       start all processes in the debugger\n"));
583     PetscCall((*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n"));
584     PetscCall((*PetscHelpPrintf)(comm,"    emacs jumps to error file\n"));
585     PetscCall((*PetscHelpPrintf)(comm," -debugger_ranks [n1,n2,..] Ranks to start in debugger\n"));
586     PetscCall((*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n"));
587     PetscCall((*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n"));
588     PetscCall((*PetscHelpPrintf)(comm,"                      waits the delay for you to attach\n"));
589     PetscCall((*PetscHelpPrintf)(comm," -display display: Location where X window graphics and debuggers are displayed\n"));
590     PetscCall((*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n"));
591     PetscCall((*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n"));
592     PetscCall((*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n"));
593     PetscCall((*PetscHelpPrintf)(comm,"           note on IBM RS6000 this slows run greatly\n"));
594     PetscCall((*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n"));
595     PetscCall((*PetscHelpPrintf)(comm," -malloc: use PETSc error checking malloc (deprecated, use -malloc_debug)\n"));
596     PetscCall((*PetscHelpPrintf)(comm," -malloc no: don't use PETSc error checking malloc (deprecated, use -malloc_debug no)\n"));
597     PetscCall((*PetscHelpPrintf)(comm," -malloc_info: prints total memory usage\n"));
598     PetscCall((*PetscHelpPrintf)(comm," -malloc_view <optional filename>: keeps log of all memory allocations, displays in PetscFinalize()\n"));
599     PetscCall((*PetscHelpPrintf)(comm," -malloc_debug <true or false>: enables or disables extended checking for memory corruption\n"));
600     PetscCall((*PetscHelpPrintf)(comm," -options_view: dump list of options inputted\n"));
601     PetscCall((*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n"));
602     PetscCall((*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n"));
603     PetscCall((*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n"));
604     PetscCall((*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n"));
605     PetscCall((*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has separate tmp directory\n"));
606     PetscCall((*PetscHelpPrintf)(comm," -memory_view: print memory usage at end of run\n"));
607 #if defined(PETSC_USE_LOG)
608     PetscCall((*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n"));
609     PetscCall((*PetscHelpPrintf)(comm," -log_view [:filename:[format]]: logging objects and events\n"));
610     PetscCall((*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n"));
611     PetscCall((*PetscHelpPrintf)(comm," -log_exclude <list,of,classnames>: exclude given classes from logging\n"));
612 #if defined(PETSC_HAVE_DEVICE)
613     PetscCall((*PetscHelpPrintf)(comm," -log_view_gpu_time: log the GPU time for each and event\n"));
614 #endif
615 #if defined(PETSC_HAVE_MPE)
616     PetscCall((*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through Jumpshot\n"));
617 #endif
618 #endif
619 #if defined(PETSC_USE_INFO)
620     PetscCall((*PetscHelpPrintf)(comm," -info [filename][:[~]<list,of,classnames>[:[~]self]]: print verbose information\n"));
621 #endif
622     PetscCall((*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n"));
623     PetscCall((*PetscHelpPrintf)(comm," -options_monitor: monitor options to standard output, including that set previously e.g. in option files\n"));
624     PetscCall((*PetscHelpPrintf)(comm," -options_monitor_cancel: cancels all hardwired option monitors\n"));
625     PetscCall((*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n"));
626   }
627 
628 #if defined(PETSC_HAVE_POPEN)
629   {
630   char machine[128];
631   PetscCall(PetscOptionsGetString(NULL,NULL,"-popen_machine",machine,sizeof(machine),&flg1));
632   if (flg1) PetscCall(PetscPOpenSetMachine(machine));
633   }
634 #endif
635 
636   PetscCall(PetscOptionsGetReal(NULL,NULL,"-petsc_sleep",&si,&flg1));
637   if (flg1) PetscCall(PetscSleep(si));
638   PetscFunctionReturn(0);
639 }
640