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