Actual source code: init.c
1: /*
2: This file defines part of the initialization of PETSc
4: This file uses regular malloc and free because it cannot be known
5: what malloc is being used until it has already processed the input.
6: */
7: #include <petsc/private/petscimpl.h>
8: #include <petsc/private/logimpl.h>
10: #if PetscDefined(HAVE_UNISTD_H)
11: #include <unistd.h>
12: #endif
14: /* ------------------------Nasty global variables -------------------------------*/
15: /*
16: Indicates if PETSc started up MPI, or it was
17: already started before PETSc was initialized.
18: */
19: PetscBool PetscBeganMPI = PETSC_FALSE;
20: PetscBool PetscErrorHandlingInitialized = PETSC_FALSE;
21: PetscBool PetscInitializeCalled = PETSC_FALSE;
22: PetscBool PetscFinalizeCalled = PETSC_FALSE;
24: PetscMPIInt PetscGlobalRank = -1;
25: PetscMPIInt PetscGlobalSize = -1;
27: #if PetscDefined(HAVE_KOKKOS)
28: PetscBool PetscBeganKokkos = PETSC_FALSE;
29: #endif
31: #if PetscDefined(HAVE_NVSHMEM)
32: PetscBool PetscBeganNvshmem = PETSC_FALSE;
33: PetscBool PetscNvshmemInitialized = PETSC_FALSE;
34: #endif
36: PetscBool use_gpu_aware_mpi = PetscDefined(HAVE_MPIUNI) ? PETSC_FALSE : PETSC_TRUE;
38: PetscBool PetscPrintFunctionList = PETSC_FALSE;
40: #if PetscDefined(HAVE_COMPLEX)
41: /*MC
42: PETSC_i - the imaginary number i
44: Synopsis:
45: #include <petscsys.h>
46: PetscComplex PETSC_i;
48: Level: beginner
50: Note:
51: Complex numbers are automatically available if PETSc located a working complex implementation
53: .seealso: `PetscRealPart()`, `PetscImaginaryPart()`, `PetscRealPartComplex()`, `PetscImaginaryPartComplex()`
54: M*/
55: PetscComplex PETSC_i;
56: MPI_Datatype MPIU___COMPLEX128 = 0;
57: #endif /* PETSC_HAVE_COMPLEX */
58: #if PetscDefined(HAVE_REAL___FLOAT128)
59: MPI_Datatype MPIU___FLOAT128 = 0;
60: #endif
61: #if PetscDefined(HAVE_REAL___FP16)
62: MPI_Datatype MPIU___FP16 = 0;
63: #endif
64: MPI_Datatype MPIU_2SCALAR = 0;
65: MPI_Datatype MPIU_REAL_INT = 0;
66: MPI_Datatype MPIU_SCALAR_INT = 0;
67: #if PetscDefined(USE_64BIT_INDICES)
68: MPI_Datatype MPIU_2INT = 0;
69: MPI_Datatype MPIU_INT_MPIINT = 0;
70: #endif
71: MPI_Datatype MPI_4INT = 0;
72: MPI_Datatype MPIU_4INT = 0;
73: MPI_Datatype MPIU_ENUM;
74: MPI_Datatype MPIU_FORTRANADDR;
75: MPI_Datatype MPIU_SIZE_T;
77: const char *const PetscPrecisionTypes[] = {"INVALID", "BFLOAT16", "__FP16", "SINGLE", "DOUBLE", "__FLOAT128", "PetscPrecisionTypes", "PETSC_PRECISION_", NULL};
79: /*
80: Function that is called to display all error messages
81: */
82: PetscErrorCode (*PetscErrorPrintf)(const char[], ...) = PetscErrorPrintfDefault;
83: PetscErrorCode (*PetscHelpPrintf)(MPI_Comm, const char[], ...) = PetscHelpPrintfDefault;
84: PetscErrorCode (*PetscVFPrintf)(FILE *, const char[], va_list) = PetscVFPrintfDefault;
86: /*
87: Optional file where all PETSc output from various prints is saved
88: */
89: PETSC_INTERN FILE *petsc_history;
90: FILE *petsc_history = NULL;
92: static PetscErrorCode PetscOpenHistoryFile(const char filename[], FILE **fd)
93: {
94: PetscMPIInt rank, size;
95: char pfile[PETSC_MAX_PATH_LEN], pname[PETSC_MAX_PATH_LEN], fname[PETSC_MAX_PATH_LEN], date[64];
96: char version[256];
98: PetscFunctionBegin;
99: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
100: if (rank == 0) {
101: char arch[10];
103: PetscCall(PetscGetArchType(arch, 10));
104: PetscCall(PetscGetDate(date, 64));
105: PetscCall(PetscGetVersion(version, 256));
106: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
107: if (filename) PetscCall(PetscFixFilename(filename, fname));
108: else {
109: PetscCall(PetscGetHomeDirectory(pfile, sizeof(pfile)));
110: PetscCall(PetscStrlcat(pfile, "/.petschistory", sizeof(pfile)));
111: PetscCall(PetscFixFilename(pfile, fname));
112: }
114: *fd = fopen(fname, "a");
115: PetscCheck(fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open file: %s", fname);
117: PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
118: PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "%s %s\n", version, date));
119: PetscCall(PetscGetProgramName(pname, sizeof(pname)));
120: PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "%s on a %s, %d proc. with options:\n", pname, arch, size));
121: PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
123: PetscCall(PetscFFlush(*fd));
124: }
125: PetscFunctionReturn(PETSC_SUCCESS);
126: }
128: PETSC_INTERN PetscErrorCode PetscCloseHistoryFile(FILE **fd)
129: {
130: PetscMPIInt rank;
132: PetscFunctionBegin;
133: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
134: if (rank == 0) {
135: char date[64];
136: int err;
138: PetscCall(PetscGetDate(date, sizeof(date)));
139: PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
140: PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "Finished at %s\n", date));
141: PetscCall(PetscFPrintf(PETSC_COMM_SELF, *fd, "----------------------------------------\n"));
142: PetscCall(PetscFFlush(*fd));
143: err = fclose(*fd);
144: PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_SYS, "fclose() failed on file");
145: }
146: PetscFunctionReturn(PETSC_SUCCESS);
147: }
149: /*
150: This is ugly and probably belongs somewhere else, but I want to
151: be able to put a true MPI abort error handler with command line args.
153: This is so MPI errors in the debugger will leave all the stack
154: frames. The default MP_Abort() cleans up and exits thus providing no useful information
155: in the debugger hence we call abort() instead of MPI_Abort().
156: */
158: static void Petsc_MPI_AbortOnError(PETSC_UNUSED MPI_Comm *comm, PetscMPIInt *flag, ...)
159: {
160: PetscFunctionBegin;
161: PetscCallContinue((*PetscErrorPrintf)("MPI error %d\n", *flag));
162: abort();
163: }
165: static void Petsc_MPI_DebuggerOnError(MPI_Comm *comm, PetscMPIInt *flag, ...)
166: {
167: PetscFunctionBegin;
168: PetscCallContinue((*PetscErrorPrintf)("MPI error %d\n", *flag));
169: if (PetscAttachDebugger()) PETSCABORT(*comm, (PetscErrorCode)*flag); /* hopeless so get out */
170: }
172: /*@
173: PetscEnd - Calls `PetscFinalize()` and then ends the program. This is useful if one
174: wishes a clean exit somewhere deep in the program.
176: Collective on `PETSC_COMM_WORLD`
178: Level: advanced
180: Note:
181: See `PetscInitialize()` for more general runtime options.
183: .seealso: `PetscInitialize()`, `PetscOptionsView()`, `PetscMallocDump()`, `PetscMPIDump()`, `PetscFinalize()`
184: @*/
185: PetscErrorCode PetscEnd(void)
186: {
187: PetscFunctionBegin;
188: PetscCall(PetscFinalize());
189: exit(0);
190: return PETSC_SUCCESS;
191: }
193: PetscBool PetscOptionsPublish = PETSC_FALSE;
194: PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void);
195: PETSC_INTERN PetscBool petscsetmallocvisited;
196: static char emacsmachinename[256];
198: PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = NULL;
199: PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm) = NULL;
201: /*@
202: PetscSetHelpVersionFunctions - Sets functions that print help and version information
203: before the PETSc help and version information is printed.
205: No Fortran Support
207: Input Parameters:
208: + help - the help function (may be `NULL`)
209: - version - the version function (may be `NULL`)
211: Level: developer
213: Notes:
214: Must call BEFORE `PetscInitialize()`.
216: This routine enables a "higher-level" package that uses PETSc to print its messages first and
217: control how the PETSc help messages are printed.
219: .seealso: `PetscInitialize()`
220: @*/
221: PetscErrorCode PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm), PetscErrorCode (*version)(MPI_Comm))
222: {
223: PetscFunctionBegin;
224: PetscExternalHelpFunction = help;
225: PetscExternalVersionFunction = version;
226: PetscFunctionReturn(PETSC_SUCCESS);
227: }
229: PETSC_INTERN PetscBool PetscObjectsLog;
231: PETSC_INTERN PetscErrorCode PetscOptionsCheckInitial_Private(const char help[])
232: {
233: char string[64];
234: PetscBool flg1 = PETSC_FALSE, flg2 = PETSC_FALSE, flag, hasHelp;
235: PetscBool checkstack = PETSC_FALSE;
236: PetscReal si;
237: PetscInt intensity;
238: PetscInt nHelpManSecs = 0;
239: int i;
240: PetscMPIInt rank;
241: char version[256];
243: PetscFunctionBegin;
244: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
246: if (PetscDefined(USE_DEBUG)) checkstack = PETSC_TRUE;
247: PetscCall(PetscOptionsGetBool(NULL, NULL, "-checkstack", &checkstack, NULL));
248: #if PetscDefined(USE_DEBUG)
249: petscstackcheck = checkstack;
250: #endif
251: PetscCall(PetscStackSetCheck(checkstack));
253: PetscCall(PetscOptionsGetBool(NULL, NULL, "-checkfunctionlist", &PetscPrintFunctionList, NULL));
255: #if !PetscDefined(HAVE_THREADSAFETY)
256: if (!PETSC_RUNNING_ON_VALGRIND) {
257: /*
258: Setup the memory management; support for tracing malloc() usage
259: */
260: PetscBool mdebug = PETSC_FALSE, eachcall = PETSC_FALSE, initializenan = PETSC_FALSE, mlog = PETSC_FALSE;
261: PetscBool flg3 = PETSC_FALSE;
263: if (PetscDefined(USE_DEBUG)) {
264: mdebug = PETSC_TRUE;
265: initializenan = PETSC_TRUE;
266: PetscCall(PetscOptionsHasName(NULL, NULL, "-malloc_test", &flg1));
267: } else {
268: /* don't warn about unused option */
269: PetscCall(PetscOptionsHasName(NULL, NULL, "-malloc_test", &flg1));
270: flg1 = PETSC_FALSE;
271: }
272: PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_debug", &flg2, &flg3));
273: if (flg1 || flg2) {
274: mdebug = PETSC_TRUE;
275: eachcall = PETSC_TRUE;
276: initializenan = PETSC_TRUE;
277: } else if (flg3 && !flg2) {
278: mdebug = PETSC_FALSE;
279: eachcall = PETSC_FALSE;
280: initializenan = PETSC_FALSE;
281: }
283: PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_requested_size", &flg1, &flg2));
284: if (flg2) PetscCall(PetscMallocLogRequestedSizeSet(flg1));
286: PetscCall(PetscOptionsHasName(NULL, NULL, "-malloc_view", &mlog));
287: if (mlog) mdebug = PETSC_TRUE;
288: /* the next line is deprecated */
289: PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_dump", &mdebug, NULL));
290: PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_view_memory", &mdebug, NULL));
291: if (mdebug) PetscCall(PetscMallocSetDebug(eachcall, initializenan));
292: if (mlog) {
293: PetscReal logthreshold = 0;
294: PetscCall(PetscOptionsGetReal(NULL, NULL, "-malloc_view_threshold", &logthreshold, NULL));
295: PetscCall(PetscMallocViewSet(logthreshold));
296: }
297: PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_view_memory", &PetscLogMemory, NULL));
298: }
300: PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_coalesce", &flg1, &flg2));
301: if (flg2) PetscCall(PetscMallocSetCoalesce(flg1));
302: flg1 = PETSC_FALSE;
303: PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_hbw", &flg1, NULL));
304: /* ignore this option if malloc is already set */
305: if (flg1 && !petscsetmallocvisited) PetscCall(PetscSetUseHBWMalloc_Private());
307: flg1 = PETSC_FALSE;
308: PetscCall(PetscOptionsGetBool(NULL, NULL, "-memory_view", &flg1, NULL));
309: if (flg1) PetscCall(PetscMemorySetGetMaximumUsage());
310: #endif
312: PetscCall(PetscOptionsHasName(NULL, NULL, "-objects_dump", &PetscObjectsLog));
314: /*
315: Set the display variable for graphics
316: */
317: PetscCall(PetscSetDisplay());
319: /*
320: Print main application help message
321: */
322: PetscCall(PetscOptionsHasHelp(NULL, &hasHelp));
323: /* in "-help mansec" mode the list of options common to all PETSc programs, and the separator that
324: precedes it, are suppressed; the example description and the version banner are still printed */
325: PetscCall(PetscOptionsHelpManSecs_Internal(NULL, &nHelpManSecs, NULL));
326: if (help && hasHelp) {
327: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%s", help));
328: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "----------------------------------------\n"));
329: }
331: /*
332: Print the PETSc version information
333: */
334: PetscCall(PetscOptionsHasName(NULL, NULL, "-version", &flg1));
335: if (flg1 || hasHelp) {
336: /*
337: Print "higher-level" package version message
338: */
339: if (PetscExternalVersionFunction) PetscCall((*PetscExternalVersionFunction)(PETSC_COMM_WORLD));
341: PetscCall(PetscGetVersion(version, 256));
342: if (!PetscCIEnabledPortableErrorOutput) PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, "%s\n", version));
343: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, "%s", PETSC_AUTHOR_INFO));
344: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, "See https://petsc.org/release/changes for recent updates.\n"));
345: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, "See https://petsc.org/release/faq for problems.\n"));
346: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, "See https://petsc.org/release/manualpages for help.\n"));
347: if (!PetscCIEnabledPortableErrorOutput) PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, "Libraries linked from %s\n", PETSC_LIB_DIR));
348: /* the closing separator and program-wide options list below are dropped in "-help mansec" mode so each matching block prints its own header */
349: if (!nHelpManSecs) PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, "----------------------------------------\n"));
350: }
352: /*
353: Print "higher-level" package help message
354: */
355: if (hasHelp) {
356: PetscBool hasHelpIntro;
358: if (PetscExternalHelpFunction) PetscCall((*PetscExternalHelpFunction)(PETSC_COMM_WORLD));
359: PetscCall(PetscOptionsHasHelpIntro_Internal(NULL, &hasHelpIntro));
360: if (hasHelpIntro) {
361: PetscCall(PetscOptionsDestroyDefault());
362: PetscCall(PetscFreeMPIResources());
363: PetscCallMPI(MPI_Finalize());
364: exit(0);
365: }
366: }
368: /*
369: Setup the error handling
370: */
371: flg1 = PETSC_FALSE;
372: PetscCall(PetscOptionsGetBool(NULL, NULL, "-on_error_abort", &flg1, NULL));
373: if (flg1) {
374: PetscCallMPI(MPI_Comm_set_errhandler(PETSC_COMM_WORLD, MPI_ERRORS_ARE_FATAL));
375: PetscCall(PetscPushErrorHandler(PetscAbortErrorHandler, NULL));
376: }
377: flg1 = PETSC_FALSE;
378: PetscCall(PetscOptionsGetBool(NULL, NULL, "-on_error_mpiabort", &flg1, NULL));
379: if (flg1) PetscCall(PetscPushErrorHandler(PetscMPIAbortErrorHandler, NULL));
380: flg1 = PETSC_FALSE;
381: PetscCall(PetscOptionsGetBool(NULL, NULL, "-mpi_return_on_error", &flg1, NULL));
382: if (flg1) PetscCallMPI(MPI_Comm_set_errhandler(PETSC_COMM_WORLD, MPI_ERRORS_RETURN));
383: flg1 = PETSC_FALSE;
384: PetscCall(PetscOptionsGetBool(NULL, NULL, "-no_signal_handler", &flg1, NULL));
385: if (!flg1) PetscCall(PetscPushSignalHandler(PetscSignalHandlerDefault, NULL));
387: /*
388: Setup debugger information
389: */
390: PetscCall(PetscSetDefaultDebugger());
391: PetscCall(PetscOptionsGetString(NULL, NULL, "-on_error_attach_debugger", string, sizeof(string), &flg1));
392: if (flg1) {
393: MPI_Errhandler err_handler;
395: PetscCall(PetscSetDebuggerFromString(string));
396: PetscCallMPI(MPI_Comm_create_errhandler(Petsc_MPI_DebuggerOnError, &err_handler));
397: PetscCallMPI(MPI_Comm_set_errhandler(PETSC_COMM_WORLD, err_handler));
398: PetscCall(PetscPushErrorHandler(PetscAttachDebuggerErrorHandler, NULL));
399: }
400: PetscCall(PetscOptionsGetString(NULL, NULL, "-debug_terminal", string, sizeof(string), &flg1));
401: if (flg1) PetscCall(PetscSetDebugTerminal(string));
402: PetscCall(PetscOptionsGetString(NULL, NULL, "-start_in_debugger", string, sizeof(string), &flg1));
403: PetscCall(PetscOptionsGetString(NULL, NULL, "-stop_for_debugger", string, sizeof(string), &flg2));
404: if (flg1 || flg2) {
405: PetscMPIInt size;
406: PetscInt lsize, *ranks;
407: MPI_Errhandler err_handler;
408: /*
409: we have to make sure that all processors have opened
410: connections to all other processors, otherwise once the
411: debugger has stated it is likely to receive a SIGUSR1
412: and kill the program.
413: */
414: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
415: if (size > 2) {
416: PetscMPIInt dummy = 0;
417: MPI_Status status;
418: for (i = 0; i < size; i++) {
419: if (rank != i) PetscCallMPI(MPI_Send(&dummy, 1, MPI_INT, i, 109, PETSC_COMM_WORLD));
420: }
421: for (i = 0; i < size; i++) {
422: if (rank != i) PetscCallMPI(MPI_Recv(&dummy, 1, MPI_INT, i, 109, PETSC_COMM_WORLD, &status));
423: }
424: }
425: /* check if this processor node should be in debugger */
426: PetscCall(PetscMalloc1(size, &ranks));
427: lsize = size;
428: /* Deprecated in 3.14 */
429: PetscCall(PetscOptionsGetIntArray(NULL, NULL, "-debugger_nodes", ranks, &lsize, &flag));
430: if (flag) {
431: const char *const quietopt = "-options_suppress_deprecated_warnings";
432: char msg[4096];
433: PetscBool quiet = PETSC_FALSE;
435: PetscCall(PetscOptionsGetBool(NULL, NULL, quietopt, &quiet, NULL));
436: if (!quiet) {
437: PetscCall(PetscStrncpy(msg, "** PETSc DEPRECATION WARNING ** : the option ", sizeof(msg)));
438: PetscCall(PetscStrlcat(msg, "-debugger_nodes", sizeof(msg)));
439: PetscCall(PetscStrlcat(msg, " is deprecated as of version ", sizeof(msg)));
440: PetscCall(PetscStrlcat(msg, "3.14", sizeof(msg)));
441: PetscCall(PetscStrlcat(msg, " and will be removed in a future release.", sizeof(msg)));
442: PetscCall(PetscStrlcat(msg, " Please use the option ", sizeof(msg)));
443: PetscCall(PetscStrlcat(msg, "-debugger_ranks", sizeof(msg)));
444: PetscCall(PetscStrlcat(msg, " instead.", sizeof(msg)));
445: PetscCall(PetscStrlcat(msg, " (Silence this warning with ", sizeof(msg)));
446: PetscCall(PetscStrlcat(msg, quietopt, sizeof(msg)));
447: PetscCall(PetscStrlcat(msg, ")\n", sizeof(msg)));
448: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%s", msg));
449: }
450: } else {
451: lsize = size;
452: PetscCall(PetscOptionsGetIntArray(NULL, NULL, "-debugger_ranks", ranks, &lsize, &flag));
453: }
454: if (flag) {
455: for (i = 0; i < lsize; i++) {
456: if (ranks[i] == rank) {
457: flag = PETSC_FALSE;
458: break;
459: }
460: }
461: }
462: if (!flag) {
463: PetscCall(PetscSetDebuggerFromString(string));
464: PetscCall(PetscPushErrorHandler(PetscAbortErrorHandler, NULL));
465: if (flg1) {
466: PetscCall(PetscAttachDebugger());
467: } else {
468: PetscCall(PetscStopForDebugger());
469: }
470: PetscCallMPI(MPI_Comm_create_errhandler(Petsc_MPI_AbortOnError, &err_handler));
471: PetscCallMPI(MPI_Comm_set_errhandler(PETSC_COMM_WORLD, err_handler));
472: } else {
473: PetscCall(PetscWaitOnError());
474: }
475: PetscCall(PetscFree(ranks));
476: }
478: PetscCall(PetscOptionsGetString(NULL, NULL, "-on_error_emacs", emacsmachinename, sizeof(emacsmachinename), &flg1));
479: if (flg1 && rank == 0) PetscCall(PetscPushErrorHandler(PetscEmacsClientErrorHandler, emacsmachinename));
481: /*
482: Setup profiling and logging
483: */
484: if (PetscDefined(USE_LOG)) PetscCall(PetscInfoSetFromOptions(NULL));
485: PetscCall(PetscDetermineInitialFPTrap());
486: flg1 = PETSC_FALSE;
487: PetscCall(PetscOptionsGetBool(NULL, NULL, "-fp_trap", &flg1, &flag));
488: if (flag) PetscCall(PetscSetFPTrap(flg1 ? PETSC_FP_TRAP_ON : PETSC_FP_TRAP_OFF));
489: PetscCall(PetscOptionsGetInt(NULL, NULL, "-check_pointer_intensity", &intensity, &flag));
490: if (flag) PetscCall(PetscCheckPointerSetIntensity(intensity));
491: if (PetscDefined(USE_LOG)) {
492: char mname[PETSC_MAX_PATH_LEN];
493: PetscInt n_max = PETSC_LOG_VIEW_FROM_OPTIONS_MAX;
494: PetscViewerFormat format[PETSC_LOG_VIEW_FROM_OPTIONS_MAX];
495: PetscBool ci_log = PetscCIEnabled;
497: mname[0] = 0;
498: PetscCall(PetscOptionsGetString(NULL, NULL, "-history", mname, sizeof(mname), &flg1));
499: if (flg1) {
500: if (mname[0]) {
501: PetscCall(PetscOpenHistoryFile(mname, &petsc_history));
502: } else {
503: PetscCall(PetscOpenHistoryFile(NULL, &petsc_history));
504: }
505: }
507: if (ci_log) {
508: static const char *LogOptions[] = {"-log_view", "-log_mpe", "-log_perfstubs", "-log_nvtx", "-log_roctx", "-log", "-log_all"};
510: for (size_t i = 0; i < PETSC_STATIC_ARRAY_LENGTH(LogOptions); i++) {
511: PetscCall(PetscOptionsHasName(NULL, NULL, LogOptions[i], &flg1));
512: if (flg1) {
513: ci_log = PETSC_FALSE;
514: break;
515: }
516: }
517: }
518: if (ci_log) PetscLogSyncOn = PETSC_TRUE;
519: PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_sync", &PetscLogSyncOn, NULL));
521: if (PetscDefined(HAVE_MPE)) {
522: flg1 = PETSC_FALSE;
523: PetscCall(PetscOptionsHasName(NULL, NULL, "-log_mpe", &flg1));
524: if (flg1) PetscCall(PetscLogMPEBegin());
525: }
526: if (PetscDefined(HAVE_TAU_PERFSTUBS)) {
527: char *tau_exec_path = getenv("TAU_EXEC_PATH");
528: PetscBool start_log_perfstubs = (tau_exec_path != NULL) ? PETSC_TRUE : PETSC_FALSE;
530: if (tau_exec_path && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected tau_exec path %s\n", tau_exec_path));
531: PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_perfstubs", &start_log_perfstubs, NULL));
532: if (start_log_perfstubs) PetscCall(PetscLogPerfstubsBegin());
533: }
534: if (PetscDefined(USE_LOG) && PetscDefined(HAVE_CUDA)) {
535: char *nsys_profiling_session_id = getenv("NSYS_PROFILING_SESSION_ID");
536: char *nvprof_id = getenv("NVPROF_ID");
537: PetscBool start_log_nvtx = ((nsys_profiling_session_id != NULL) || (nvprof_id != NULL)) ? PETSC_TRUE : PETSC_FALSE;
539: if (nsys_profiling_session_id && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected nsys profiling session id %s\n", nsys_profiling_session_id));
540: if (nvprof_id && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected nvprof session id %s\n", nvprof_id));
541: PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_nvtx", &start_log_nvtx, NULL));
542: if (start_log_nvtx) PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERNVTX));
543: }
544: if (PetscDefined(USE_LOG) && PetscDefined(HAVE_ROCTX)) {
545: char *rocprof_roctx_trace = getenv("ROCPROFILER_ROCTX_TRACE");
546: char *rocprof_marker_trace = getenv("ROCPROF_MARKER_API_TRACE");
547: PetscBool start_log_roctx = ((rocprof_roctx_trace != NULL) || (rocprof_marker_trace != NULL)) ? PETSC_TRUE : PETSC_FALSE;
549: if (rocprof_roctx_trace && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected rocprofv2 profiling session\n"));
550: if (rocprof_marker_trace && !PetscGlobalRank) PetscCall(PetscInfo(NULL, "Detected rocprofv3 profiling session\n"));
551: PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_roctx", &start_log_roctx, NULL));
552: if (start_log_roctx) PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERROCTX));
553: }
554: PetscCall(PetscOptionsHasName(NULL, NULL, "-log_all", &flg1));
555: PetscCall(PetscOptionsHasName(NULL, NULL, "-log", &flg2));
556: if (flg1 || flg2 || ci_log) PetscCall(PetscLogDefaultBegin());
558: PetscCall(PetscOptionsGetString(NULL, NULL, "-log_trace", mname, sizeof(mname), &flg1));
559: if (flg1) {
560: char name[PETSC_MAX_PATH_LEN], fname[PETSC_MAX_PATH_LEN];
561: FILE *file;
562: if (mname[0]) {
563: PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "%s.%d", mname, rank));
564: PetscCall(PetscFixFilename(name, fname));
565: file = fopen(fname, "w");
566: PetscCheck(file, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to open trace file: %s", fname);
567: } else file = PETSC_STDOUT;
568: PetscCall(PetscLogTraceBegin(file));
569: }
571: PetscCall(PetscOptionsCreateViewers(PETSC_COMM_WORLD, NULL, NULL, "-log_view", &n_max, NULL, format, NULL));
572: if (n_max > 0) {
573: PetscBool any_nested = PETSC_FALSE;
574: PetscBool any_default = PETSC_FALSE;
576: for (PetscInt i = 0; i < n_max; i++) {
577: if (format[i] == PETSC_VIEWER_ASCII_XML || format[i] == PETSC_VIEWER_ASCII_FLAMEGRAPH) {
578: any_nested = PETSC_TRUE;
579: } else {
580: any_default = PETSC_TRUE;
581: }
582: }
583: if (any_default) PetscCall(PetscLogDefaultBegin());
584: if (any_nested) {
585: PetscCall(PetscLogNestedBegin());
586: PetscReal threshold = PetscRealConstant(0.01);
587: PetscCall(PetscOptionsGetReal(NULL, NULL, "-log_threshold", &threshold, &flg1));
588: if (flg1) PetscCall(PetscLogSetThreshold((PetscLogDouble)threshold, NULL));
589: }
590: }
591: }
593: PetscCall(PetscOptionsGetBool(NULL, NULL, "-saws_options", &PetscOptionsPublish, NULL));
594: PetscCall(PetscOptionsGetBool(NULL, NULL, "-use_gpu_aware_mpi", &use_gpu_aware_mpi, &flg1));
595: if (!flg1) PetscCall(PetscOptionsGetBool(NULL, NULL, "-sf_use_gpu_aware_mpi", &use_gpu_aware_mpi, &flg1)); // an alias option
597: /*
598: Print basic help message
599: */
600: if (hasHelp && !nHelpManSecs) {
601: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, "Options for all PETSc programs:\n"));
602: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -version: prints PETSc version\n"));
603: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -help intro: prints example description and PETSc version, and exits\n"));
604: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -help: prints example description, PETSc version, and available options for used routines\n"));
605: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -help mansec: prints only the options in the given manual section(s), for example -help ksp,snes\n"));
606: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -on_error_abort: cause an abort when an error is detected. Useful \n "));
607: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " only when run in the debugger\n"));
608: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -on_error_attach_debugger [(noxterm)],[(gdb|lldb|...)]\n"));
609: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " start the debugger in new xterm\n"));
610: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " unless noxterm is given\n"));
611: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -start_in_debugger [(noxterm)],[(gdb|lldb|...)]\n"));
612: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " start all processes in the debugger\n"));
613: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -on_error_emacs machinename\n"));
614: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " emacs jumps to error file\n"));
615: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -debugger_ranks n1,n2,.. Ranks to start in debugger\n"));
616: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -debugger_pause secs : delay (in seconds) to attach debugger\n"));
617: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -stop_for_debugger : prints message on how to attach debugger manually\n"));
618: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " waits the delay for you to attach\n"));
619: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -display display: Location where X window graphics and debuggers are displayed\n"));
620: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -no_signal_handler: do not trap error signals\n"));
621: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n"));
622: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -fp_trap: stop on floating point exceptions\n"));
623: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " note on IBM RS6000 this slows run greatly\n"));
624: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -malloc_dump [filename]: dump list of unfreed memory at conclusion\n"));
625: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -on_error_malloc_dump [filename]: dump list of unfreed memory on memory error\n"));
626: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -malloc_view [filename]: keeps log of all memory allocations, displays in PetscFinalize()\n"));
627: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -malloc_debug (true|false): enables or disables extended checking for memory corruption\n"));
628: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -options_view: dump list of options inputted\n"));
629: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -options_left: dump list of unused options\n"));
630: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -options_left no: don't dump list of unused options\n"));
631: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -memory_view: print memory usage at end of run\n"));
632: if (PetscDefined(USE_LOG)) {
633: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -get_total_flops: total flops over all processors\n"));
634: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -log_view [:filename:[format]]: logging objects and events\n"));
635: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -log_trace [filename]: prints trace of all PETSc calls\n"));
636: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -log_exclude classname1,classname2,...: exclude given classes from logging\n"));
637: if (PetscDefined(HAVE_DEVICE)) {
638: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -log_view_gpu_time: log the GPU time for each event\n"));
639: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -log_view_gpu_energy: log the GPU energy (estimated) for each event\n"));
640: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -log_view_gpu_energy_meter: log the GPU energy (readings from meters) for each event\n"));
641: }
642: if (PetscDefined(HAVE_MPE)) PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -log_mpe: Also create logfile viewable through Jumpshot\n"));
643: if (PetscDefined(HAVE_CUDA)) PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -log_nvtx: Create nvtx event ranges for Nsight\n"));
644: if (PetscDefined(HAVE_HIP)) PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -log_roctx: Create roctx event ranges for rocprof\n"));
645: }
646: if (PetscDefined(USE_INFO)) PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -info [filename][:[~]c1,c2,...[:[~]self]]: print verbose information. c1 and c2 are class names\n"));
647: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -options_file filename: reads options from file\n"));
648: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -options_monitor: monitor options to standard output, including that set previously e.g. in option files\n"));
649: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -options_monitor_cancel: cancels all hardwired option monitors\n"));
650: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, " -petsc_sleep n: sleeps n seconds before running program\n"));
651: }
653: #if PetscDefined(HAVE_POPEN)
654: {
655: char machine[128];
656: PetscCall(PetscOptionsGetString(NULL, NULL, "-popen_machine", machine, sizeof(machine), &flg1));
657: if (flg1) PetscCall(PetscPOpenSetMachine(machine));
658: }
659: #endif
661: PetscCall(PetscOptionsGetReal(NULL, NULL, "-petsc_sleep", &si, &flg1));
662: if (flg1) PetscCall(PetscSleep(si));
663: PetscFunctionReturn(PETSC_SUCCESS);
664: }