Actual source code: pinit.c
1: #define PETSC_DESIRE_FEATURE_TEST_MACROS
2: /*
3: This file defines the initialization of PETSc, including PetscInitialize()
4: */
5: #include <petsc/private/petscimpl.h>
6: #include <petscviewer.h>
7: #include <petsc/private/garbagecollector.h>
9: #if !defined(PETSC_HAVE_WINDOWS_COMPILERS)
10: #include <petsc/private/valgrind/valgrind.h>
11: #endif
13: #if defined(PETSC_HAVE_FORTRAN)
14: #include <petsc/private/fortranimpl.h>
15: #endif
17: #if PetscDefined(USE_COVERAGE)
18: EXTERN_C_BEGIN
19: #if defined(PETSC_HAVE___GCOV_DUMP)
21: #endif
22: void __gcov_flush(void);
23: EXTERN_C_END
24: #endif
26: #if defined(PETSC_SERIALIZE_FUNCTIONS)
27: PETSC_INTERN PetscFPT PetscFPTData;
28: PetscFPT PetscFPTData = 0;
29: #endif
31: #if PetscDefined(HAVE_SAWS)
32: #include <petscviewersaws.h>
33: #endif
35: PETSC_INTERN FILE *petsc_history;
37: PETSC_INTERN PetscErrorCode PetscInitialize_DynamicLibraries(void);
38: PETSC_INTERN PetscErrorCode PetscFinalize_DynamicLibraries(void);
39: PETSC_INTERN PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm, int);
40: PETSC_INTERN PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm, int);
41: PETSC_INTERN PetscErrorCode PetscCloseHistoryFile(FILE **);
43: /* user may set these BEFORE calling PetscInitialize() */
44: MPI_Comm PETSC_COMM_WORLD = MPI_COMM_NULL;
45: #if PetscDefined(HAVE_MPI_INIT_THREAD)
46: PetscMPIInt PETSC_MPI_THREAD_REQUIRED = MPI_THREAD_FUNNELED;
47: #else
48: PetscMPIInt PETSC_MPI_THREAD_REQUIRED = 0;
49: #endif
51: PetscMPIInt Petsc_Counter_keyval = MPI_KEYVAL_INVALID;
52: PetscMPIInt Petsc_InnerComm_keyval = MPI_KEYVAL_INVALID;
53: PetscMPIInt Petsc_OuterComm_keyval = MPI_KEYVAL_INVALID;
54: PetscMPIInt Petsc_ShmComm_keyval = MPI_KEYVAL_INVALID;
55: PetscMPIInt Petsc_CreationIdx_keyval = MPI_KEYVAL_INVALID;
56: PetscMPIInt Petsc_Garbage_HMap_keyval = MPI_KEYVAL_INVALID;
58: PetscMPIInt Petsc_SharedWD_keyval = MPI_KEYVAL_INVALID;
59: PetscMPIInt Petsc_SharedTmp_keyval = MPI_KEYVAL_INVALID;
61: /*
62: Declare and set all the string names of the PETSc enums
63: */
64: const char *const PetscBools[] = {"FALSE", "TRUE", "PetscBool", "PETSC_", NULL};
65: const char *const PetscCopyModes[] = {"COPY_VALUES", "OWN_POINTER", "USE_POINTER", "PetscCopyMode", "PETSC_", NULL};
67: PetscBool PetscPreLoadingUsed = PETSC_FALSE;
68: PetscBool PetscPreLoadingOn = PETSC_FALSE;
70: PetscInt PetscHotRegionDepth;
72: PetscBool PETSC_RUNNING_ON_VALGRIND = PETSC_FALSE;
74: #if defined(PETSC_HAVE_THREADSAFETY)
75: PetscSpinlock PetscViewerASCIISpinLockOpen;
76: PetscSpinlock PetscViewerASCIISpinLockStdout;
77: PetscSpinlock PetscViewerASCIISpinLockStderr;
78: PetscSpinlock PetscCommSpinLock;
79: #endif
81: /*
82: PetscInitializeNoPointers - Calls PetscInitialize() from C/C++ without the pointers to argc and args
84: Collective
86: Level: advanced
88: Notes:
89: this is called only by the PETSc Julia interface. Even though it might start MPI it sets the flag to
90: indicate that it did NOT start MPI so that the PetscFinalize() does not end MPI, thus allowing PetscInitialize() to
91: be called multiple times from Julia without the problem of trying to initialize MPI more than once.
93: Developer Note: Turns off PETSc signal handling to allow Julia to manage signals
95: .seealso: `PetscInitialize()`, `PetscInitializeFortran()`, `PetscInitializeNoArguments()`
96: */
97: PetscErrorCode PetscInitializeNoPointers(int argc, char **args, const char *filename, const char *help)
98: {
99: int myargc = argc;
100: char **myargs = args;
102: PetscFunctionBegin;
103: PetscCall(PetscInitialize(&myargc, &myargs, filename, help));
104: PetscCall(PetscPopSignalHandler());
105: PetscBeganMPI = PETSC_FALSE;
106: PetscFunctionReturn(PETSC_SUCCESS);
107: }
109: /*
110: Used by Julia interface to get communicator
111: */
112: PetscErrorCode PetscGetPETSC_COMM_SELF(MPI_Comm *comm)
113: {
114: PetscFunctionBegin;
116: *comm = PETSC_COMM_SELF;
117: PetscFunctionReturn(PETSC_SUCCESS);
118: }
120: /*@C
121: PetscInitializeNoArguments - Calls `PetscInitialize()` from C/C++ without
122: the command line arguments.
124: Collective
126: Level: advanced
128: .seealso: `PetscInitialize()`, `PetscInitializeFortran()`
129: @*/
130: PetscErrorCode PetscInitializeNoArguments(void)
131: {
132: int argc = 0;
133: char **args = NULL;
135: PetscFunctionBegin;
136: PetscCall(PetscInitialize(&argc, &args, NULL, NULL));
137: PetscFunctionReturn(PETSC_SUCCESS);
138: }
140: /*@
141: PetscInitialized - Determine whether PETSc is initialized.
143: Level: beginner
145: .seealso: `PetscInitialize()`, `PetscInitializeNoArguments()`, `PetscInitializeFortran()`
146: @*/
147: PetscErrorCode PetscInitialized(PetscBool *isInitialized)
148: {
149: PetscFunctionBegin;
151: *isInitialized = PetscInitializeCalled;
152: PetscFunctionReturn(PETSC_SUCCESS);
153: }
155: /*@
156: PetscFinalized - Determine whether `PetscFinalize()` has been called yet
158: Level: developer
160: .seealso: `PetscInitialize()`, `PetscInitializeNoArguments()`, `PetscInitializeFortran()`
161: @*/
162: PetscErrorCode PetscFinalized(PetscBool *isFinalized)
163: {
164: PetscFunctionBegin;
166: *isFinalized = PetscFinalizeCalled;
167: PetscFunctionReturn(PETSC_SUCCESS);
168: }
170: PETSC_INTERN PetscErrorCode PetscOptionsCheckInitial_Private(const char[]);
172: /*
173: This function is the MPI reduction operation used to compute the sum of the
174: first half of the datatype and the max of the second half.
175: */
176: MPI_Op MPIU_MAXSUM_OP = 0;
177: MPI_Op Petsc_Garbage_SetIntersectOp = 0;
179: PETSC_INTERN void MPIAPI MPIU_MaxSum_Local(void *in, void *out, int *cnt, MPI_Datatype *datatype)
180: {
181: PetscInt *xin = (PetscInt *)in, *xout = (PetscInt *)out, i, count = *cnt;
183: PetscFunctionBegin;
184: if (*datatype != MPIU_2INT) {
185: PetscErrorCode ierr = (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
186: (void)ierr;
187: PETSCABORT(MPI_COMM_SELF, PETSC_ERR_ARG_WRONG);
188: }
190: for (i = 0; i < count; i++) {
191: xout[2 * i] = PetscMax(xout[2 * i], xin[2 * i]);
192: xout[2 * i + 1] += xin[2 * i + 1];
193: }
194: PetscFunctionReturnVoid();
195: }
197: /*
198: Returns the max of the first entry owned by this processor and the
199: sum of the second entry.
201: The reason sizes[2*i] contains lengths sizes[2*i+1] contains flag of 1 if length is nonzero
202: is so that the MPIU_MAXSUM_OP() can set TWO values, if we passed in only sizes[i] with lengths
203: there would be no place to store the both needed results.
204: */
205: PetscErrorCode PetscMaxSum(MPI_Comm comm, const PetscInt sizes[], PetscInt *max, PetscInt *sum)
206: {
207: PetscFunctionBegin;
208: #if defined(PETSC_HAVE_MPI_REDUCE_SCATTER_BLOCK)
209: {
210: struct {
211: PetscInt max, sum;
212: } work;
213: PetscCallMPI(MPI_Reduce_scatter_block((void *)sizes, &work, 1, MPIU_2INT, MPIU_MAXSUM_OP, comm));
214: *max = work.max;
215: *sum = work.sum;
216: }
217: #else
218: {
219: PetscMPIInt size, rank;
220: struct {
221: PetscInt max, sum;
222: } *work;
223: PetscCallMPI(MPI_Comm_size(comm, &size));
224: PetscCallMPI(MPI_Comm_rank(comm, &rank));
225: PetscCall(PetscMalloc1(size, &work));
226: PetscCall(MPIU_Allreduce((void *)sizes, work, size, MPIU_2INT, MPIU_MAXSUM_OP, comm));
227: *max = work[rank].max;
228: *sum = work[rank].sum;
229: PetscCall(PetscFree(work));
230: }
231: #endif
232: PetscFunctionReturn(PETSC_SUCCESS);
233: }
235: #if defined(PETSC_HAVE_REAL___FLOAT128) || defined(PETSC_HAVE_REAL___FP16)
236: #if defined(PETSC_HAVE_REAL___FLOAT128)
237: #include <quadmath.h>
238: #endif
239: MPI_Op MPIU_SUM___FP16___FLOAT128 = 0;
240: #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
241: MPI_Op MPIU_SUM = 0;
242: #endif
244: PETSC_EXTERN void MPIAPI PetscSum_Local(void *in, void *out, PetscMPIInt *cnt, MPI_Datatype *datatype)
245: {
246: PetscInt i, count = *cnt;
248: PetscFunctionBegin;
249: if (*datatype == MPIU_REAL) {
250: PetscReal *xin = (PetscReal *)in, *xout = (PetscReal *)out;
251: for (i = 0; i < count; i++) xout[i] += xin[i];
252: }
253: #if defined(PETSC_HAVE_COMPLEX)
254: else if (*datatype == MPIU_COMPLEX) {
255: PetscComplex *xin = (PetscComplex *)in, *xout = (PetscComplex *)out;
256: for (i = 0; i < count; i++) xout[i] += xin[i];
257: }
258: #endif
259: #if defined(PETSC_HAVE_REAL___FLOAT128)
260: else if (*datatype == MPIU___FLOAT128) {
261: __float128 *xin = (__float128 *)in, *xout = (__float128 *)out;
262: for (i = 0; i < count; i++) xout[i] += xin[i];
263: #if defined(PETSC_HAVE_COMPLEX)
264: } else if (*datatype == MPIU___COMPLEX128) {
265: __complex128 *xin = (__complex128 *)in, *xout = (__complex128 *)out;
266: for (i = 0; i < count; i++) xout[i] += xin[i];
267: #endif
268: }
269: #endif
270: #if defined(PETSC_HAVE_REAL___FP16)
271: else if (*datatype == MPIU___FP16) {
272: __fp16 *xin = (__fp16 *)in, *xout = (__fp16 *)out;
273: for (i = 0; i < count; i++) xout[i] += xin[i];
274: }
275: #endif
276: else {
277: #if !defined(PETSC_HAVE_REAL___FLOAT128) && !defined(PETSC_HAVE_REAL___FP16)
278: PetscCallAbort(MPI_COMM_SElF, (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types"));
279: #elif !defined(PETSC_HAVE_REAL___FP16)
280: PetscCallAbort(MPI_COMM_SELF, (*PetscErrorPrintf)("Can only handle MPIU_REAL, MPIU_COMPLEX, MPIU___FLOAT128, or MPIU___COMPLEX128 data types"));
281: #elif !defined(PETSC_HAVE_REAL___FLOAT128)
282: PetscCallAbort(MPI_COMM_SELF, (*PetscErrorPrintf)("Can only handle MPIU_REAL, MPIU_COMPLEX, or MPIU___FP16 data types"));
283: #else
284: PetscCallAbort(MPI_COMM_SELF, (*PetscErrorPrintf)("Can only handle MPIU_REAL, MPIU_COMPLEX, MPIU___FLOAT128, MPIU___COMPLEX128, or MPIU___FP16 data types"));
285: #endif
286: PETSCABORT(MPI_COMM_SELF, PETSC_ERR_ARG_WRONG);
287: }
288: PetscFunctionReturnVoid();
289: }
290: #endif
292: #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
293: MPI_Op MPIU_MAX = 0;
294: MPI_Op MPIU_MIN = 0;
296: PETSC_EXTERN void MPIAPI PetscMax_Local(void *in, void *out, PetscMPIInt *cnt, MPI_Datatype *datatype)
297: {
298: PetscInt i, count = *cnt;
300: PetscFunctionBegin;
301: if (*datatype == MPIU_REAL) {
302: PetscReal *xin = (PetscReal *)in, *xout = (PetscReal *)out;
303: for (i = 0; i < count; i++) xout[i] = PetscMax(xout[i], xin[i]);
304: }
305: #if defined(PETSC_HAVE_COMPLEX)
306: else if (*datatype == MPIU_COMPLEX) {
307: PetscComplex *xin = (PetscComplex *)in, *xout = (PetscComplex *)out;
308: for (i = 0; i < count; i++) xout[i] = PetscRealPartComplex(xout[i]) < PetscRealPartComplex(xin[i]) ? xin[i] : xout[i];
309: }
310: #endif
311: else {
312: PetscCallAbort(MPI_COMM_SELF, (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types"));
313: PETSCABORT(MPI_COMM_SELF, PETSC_ERR_ARG_WRONG);
314: }
315: PetscFunctionReturnVoid();
316: }
318: PETSC_EXTERN void MPIAPI PetscMin_Local(void *in, void *out, PetscMPIInt *cnt, MPI_Datatype *datatype)
319: {
320: PetscInt i, count = *cnt;
322: PetscFunctionBegin;
323: if (*datatype == MPIU_REAL) {
324: PetscReal *xin = (PetscReal *)in, *xout = (PetscReal *)out;
325: for (i = 0; i < count; i++) xout[i] = PetscMin(xout[i], xin[i]);
326: }
327: #if defined(PETSC_HAVE_COMPLEX)
328: else if (*datatype == MPIU_COMPLEX) {
329: PetscComplex *xin = (PetscComplex *)in, *xout = (PetscComplex *)out;
330: for (i = 0; i < count; i++) xout[i] = PetscRealPartComplex(xout[i]) > PetscRealPartComplex(xin[i]) ? xin[i] : xout[i];
331: }
332: #endif
333: else {
334: PetscCallAbort(MPI_COMM_SELF, (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_SCALAR data (i.e. double or complex) types"));
335: PETSCABORT(MPI_COMM_SELF, PETSC_ERR_ARG_WRONG);
336: }
337: PetscFunctionReturnVoid();
338: }
339: #endif
341: /*
342: Private routine to delete internal tag/name counter storage when a communicator is freed.
344: This is called by MPI, not by users. This is called by MPI_Comm_free() when the communicator that has this data as an attribute is freed.
346: Note: this is declared extern "C" because it is passed to MPI_Comm_create_keyval()
348: */
349: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_Counter_Attr_Delete_Fn(MPI_Comm comm, PetscMPIInt keyval, void *count_val, void *extra_state)
350: {
351: PetscCommCounter *counter = (PetscCommCounter *)count_val;
352: struct PetscCommStash *comms = counter->comms, *pcomm;
354: PetscFunctionBegin;
355: PetscCallMPI(PetscInfo(NULL, "Deleting counter data in an MPI_Comm %ld\n", (long)comm));
356: PetscCallMPI(PetscFree(counter->iflags));
357: while (comms) {
358: PetscCallMPI(MPI_Comm_free(&comms->comm));
359: pcomm = comms;
360: comms = comms->next;
361: PetscCall(PetscFree(pcomm));
362: }
363: PetscCallMPI(PetscFree(counter));
364: PetscFunctionReturn(MPI_SUCCESS);
365: }
367: /*
368: This is invoked on the outer comm as a result of either PetscCommDestroy() (via MPI_Comm_delete_attr) or when the user
369: calls MPI_Comm_free().
371: This is the only entry point for breaking the links between inner and outer comms.
373: This is called by MPI, not by users. This is called when MPI_Comm_free() is called on the communicator.
375: Note: this is declared extern "C" because it is passed to MPI_Comm_create_keyval()
377: */
378: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_InnerComm_Attr_Delete_Fn(MPI_Comm comm, PetscMPIInt keyval, void *attr_val, void *extra_state)
379: {
380: union
381: {
382: MPI_Comm comm;
383: void *ptr;
384: } icomm;
386: PetscFunctionBegin;
387: if (keyval != Petsc_InnerComm_keyval) SETERRMPI(PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Unexpected keyval");
388: icomm.ptr = attr_val;
389: if (PetscDefined(USE_DEBUG)) {
390: /* Error out if the inner/outer comms are not correctly linked through their Outer/InnterComm attributes */
391: PetscMPIInt flg;
392: union
393: {
394: MPI_Comm comm;
395: void *ptr;
396: } ocomm;
397: PetscCallMPI(MPI_Comm_get_attr(icomm.comm, Petsc_OuterComm_keyval, &ocomm, &flg));
398: if (!flg) SETERRMPI(PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Inner comm does not have OuterComm attribute");
399: if (ocomm.comm != comm) SETERRMPI(PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Inner comm's OuterComm attribute does not point to outer PETSc comm");
400: }
401: PetscCallMPI(MPI_Comm_delete_attr(icomm.comm, Petsc_OuterComm_keyval));
402: PetscCallMPI(PetscInfo(NULL, "User MPI_Comm %ld is being unlinked from inner PETSc comm %ld\n", (long)comm, (long)icomm.comm));
403: PetscFunctionReturn(MPI_SUCCESS);
404: }
406: /*
407: * This is invoked on the inner comm when Petsc_InnerComm_Attr_Delete_Fn calls MPI_Comm_delete_attr(). It should not be reached any other way.
408: */
409: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_OuterComm_Attr_Delete_Fn(MPI_Comm comm, PetscMPIInt keyval, void *attr_val, void *extra_state)
410: {
411: PetscFunctionBegin;
412: PetscCallMPI(PetscInfo(NULL, "Removing reference to PETSc communicator embedded in a user MPI_Comm %ld\n", (long)comm));
413: PetscFunctionReturn(MPI_SUCCESS);
414: }
416: PETSC_EXTERN PetscMPIInt MPIAPI Petsc_ShmComm_Attr_Delete_Fn(MPI_Comm, PetscMPIInt, void *, void *);
418: #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
419: PETSC_EXTERN PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype, MPI_Aint *, void *);
420: PETSC_EXTERN PetscMPIInt PetscDataRep_read_conv_fn(void *, MPI_Datatype, PetscMPIInt, void *, MPI_Offset, void *);
421: PETSC_EXTERN PetscMPIInt PetscDataRep_write_conv_fn(void *, MPI_Datatype, PetscMPIInt, void *, MPI_Offset, void *);
422: #endif
424: PetscMPIInt PETSC_MPI_ERROR_CLASS = MPI_ERR_LASTCODE, PETSC_MPI_ERROR_CODE;
426: PETSC_INTERN int PetscGlobalArgc;
427: PETSC_INTERN char **PetscGlobalArgs;
428: int PetscGlobalArgc = 0;
429: char **PetscGlobalArgs = NULL;
430: PetscSegBuffer PetscCitationsList;
432: PetscErrorCode PetscCitationsInitialize(void)
433: {
434: PetscFunctionBegin;
435: PetscCall(PetscSegBufferCreate(1, 10000, &PetscCitationsList));
437: PetscCall(PetscCitationsRegister("@TechReport{petsc-user-ref,\n\
438: Author = {Satish Balay and Shrirang Abhyankar and Mark~F. Adams and Steven Benson and Jed Brown\n\
439: and Peter Brune and Kris Buschelman and Emil Constantinescu and Lisandro Dalcin and Alp Dener\n\
440: and Victor Eijkhout and Jacob Faibussowitsch and William~D. Gropp and V\'{a}clav Hapla and Tobin Isaac and Pierre Jolivet\n\
441: and Dmitry Karpeev and Dinesh Kaushik and Matthew~G. Knepley and Fande Kong and Scott Kruger\n\
442: and Dave~A. May and Lois Curfman McInnes and Richard Tran Mills and Lawrence Mitchell and Todd Munson\n\
443: and Jose~E. Roman and Karl Rupp and Patrick Sanan and Jason Sarich and Barry~F. Smith\n\
444: and Stefano Zampini and Hong Zhang and Hong Zhang and Junchao Zhang},\n\
445: Title = {{PETSc/TAO} Users Manual},\n\
446: Number = {ANL-21/39 - Revision 3.19},\n\
447: Institution = {Argonne National Laboratory},\n\
448: Year = {2023}\n}\n",
449: NULL));
451: PetscCall(PetscCitationsRegister("@InProceedings{petsc-efficient,\n\
452: Author = {Satish Balay and William D. Gropp and Lois Curfman McInnes and Barry F. Smith},\n\
453: Title = {Efficient Management of Parallelism in Object Oriented Numerical Software Libraries},\n\
454: Booktitle = {Modern Software Tools in Scientific Computing},\n\
455: Editor = {E. Arge and A. M. Bruaset and H. P. Langtangen},\n\
456: Pages = {163--202},\n\
457: Publisher = {Birkh{\\\"{a}}user Press},\n\
458: Year = {1997}\n}\n",
459: NULL));
461: PetscFunctionReturn(PETSC_SUCCESS);
462: }
464: static char programname[PETSC_MAX_PATH_LEN] = ""; /* HP includes entire path in name */
466: PetscErrorCode PetscSetProgramName(const char name[])
467: {
468: PetscFunctionBegin;
469: PetscCall(PetscStrncpy(programname, name, sizeof(programname)));
470: PetscFunctionReturn(PETSC_SUCCESS);
471: }
473: /*@C
474: PetscGetProgramName - Gets the name of the running program.
476: Not Collective
478: Input Parameter:
479: . len - length of the string name
481: Output Parameter:
482: . name - the name of the running program, provide a string of length `PETSC_MAX_PATH_LEN`
484: Level: advanced
486: .seealso: `PetscFinalize()`, `PetscInitializeFortran()`, `PetscGetArguments()`, `PetscInitialize()`
487: @*/
488: PetscErrorCode PetscGetProgramName(char name[], size_t len)
489: {
490: PetscFunctionBegin;
491: PetscCall(PetscStrncpy(name, programname, len));
492: PetscFunctionReturn(PETSC_SUCCESS);
493: }
495: /*@C
496: PetscGetArgs - Allows you to access the raw command line arguments anywhere
497: after PetscInitialize() is called but before `PetscFinalize()`.
499: Not Collective
501: Output Parameters:
502: + argc - count of number of command line arguments
503: - args - the command line arguments
505: Level: intermediate
507: Notes:
508: This is usually used to pass the command line arguments into other libraries
509: that are called internally deep in PETSc or the application.
511: The first argument contains the program name as is normal for C arguments.
513: .seealso: `PetscFinalize()`, `PetscInitializeFortran()`, `PetscGetArguments()`, `PetscInitialize()`
514: @*/
515: PetscErrorCode PetscGetArgs(int *argc, char ***args)
516: {
517: PetscFunctionBegin;
518: PetscCheck(PetscInitializeCalled || !PetscFinalizeCalled, PETSC_COMM_SELF, PETSC_ERR_ORDER, "You must call after PetscInitialize() but before PetscFinalize()");
519: *argc = PetscGlobalArgc;
520: *args = PetscGlobalArgs;
521: PetscFunctionReturn(PETSC_SUCCESS);
522: }
524: /*@C
525: PetscGetArguments - Allows you to access the command line arguments anywhere
526: after `PetscInitialize()` is called but before `PetscFinalize()`.
528: Not Collective
530: Output Parameter:
531: . args - the command line arguments
533: Level: intermediate
535: Note:
536: This does NOT start with the program name and IS `NULL` terminated (final arg is void)
538: .seealso: `PetscFinalize()`, `PetscInitializeFortran()`, `PetscGetArgs()`, `PetscFreeArguments()`, `PetscInitialize()`
539: @*/
540: PetscErrorCode PetscGetArguments(char ***args)
541: {
542: PetscInt i, argc = PetscGlobalArgc;
544: PetscFunctionBegin;
545: PetscCheck(PetscInitializeCalled || !PetscFinalizeCalled, PETSC_COMM_SELF, PETSC_ERR_ORDER, "You must call after PetscInitialize() but before PetscFinalize()");
546: if (!argc) {
547: *args = NULL;
548: PetscFunctionReturn(PETSC_SUCCESS);
549: }
550: PetscCall(PetscMalloc1(argc, args));
551: for (i = 0; i < argc - 1; i++) PetscCall(PetscStrallocpy(PetscGlobalArgs[i + 1], &(*args)[i]));
552: (*args)[argc - 1] = NULL;
553: PetscFunctionReturn(PETSC_SUCCESS);
554: }
556: /*@C
557: PetscFreeArguments - Frees the memory obtained with `PetscGetArguments()`
559: Not Collective
561: Output Parameter:
562: . args - the command line arguments
564: Level: intermediate
566: .seealso: `PetscFinalize()`, `PetscInitializeFortran()`, `PetscGetArgs()`, `PetscGetArguments()`
567: @*/
568: PetscErrorCode PetscFreeArguments(char **args)
569: {
570: PetscFunctionBegin;
571: if (args) {
572: PetscInt i = 0;
574: while (args[i]) PetscCall(PetscFree(args[i++]));
575: PetscCall(PetscFree(args));
576: }
577: PetscFunctionReturn(PETSC_SUCCESS);
578: }
580: #if PetscDefined(HAVE_SAWS)
581: #include <petscconfiginfo.h>
583: PETSC_INTERN PetscErrorCode PetscInitializeSAWs(const char help[])
584: {
585: PetscFunctionBegin;
586: if (!PetscGlobalRank) {
587: char cert[PETSC_MAX_PATH_LEN], root[PETSC_MAX_PATH_LEN], *intro, programname[64], *appline, *options, version[64];
588: int port;
589: PetscBool flg, rootlocal = PETSC_FALSE, flg2, selectport = PETSC_FALSE;
590: size_t applinelen, introlen;
591: char sawsurl[256];
593: PetscCall(PetscOptionsHasName(NULL, NULL, "-saws_log", &flg));
594: if (flg) {
595: char sawslog[PETSC_MAX_PATH_LEN];
597: PetscCall(PetscOptionsGetString(NULL, NULL, "-saws_log", sawslog, sizeof(sawslog), NULL));
598: if (sawslog[0]) {
599: PetscCallSAWs(SAWs_Set_Use_Logfile, (sawslog));
600: } else {
601: PetscCallSAWs(SAWs_Set_Use_Logfile, (NULL));
602: }
603: }
604: PetscCall(PetscOptionsGetString(NULL, NULL, "-saws_https", cert, sizeof(cert), &flg));
605: if (flg) PetscCallSAWs(SAWs_Set_Use_HTTPS, (cert));
606: PetscCall(PetscOptionsGetBool(NULL, NULL, "-saws_port_auto_select", &selectport, NULL));
607: if (selectport) {
608: PetscCallSAWs(SAWs_Get_Available_Port, (&port));
609: PetscCallSAWs(SAWs_Set_Port, (port));
610: } else {
611: PetscCall(PetscOptionsGetInt(NULL, NULL, "-saws_port", &port, &flg));
612: if (flg) PetscCallSAWs(SAWs_Set_Port, (port));
613: }
614: PetscCall(PetscOptionsGetString(NULL, NULL, "-saws_root", root, sizeof(root), &flg));
615: if (flg) {
616: PetscCallSAWs(SAWs_Set_Document_Root, (root));
617: PetscCall(PetscStrcmp(root, ".", &rootlocal));
618: } else {
619: PetscCall(PetscOptionsHasName(NULL, NULL, "-saws_options", &flg));
620: if (flg) {
621: PetscCall(PetscStrreplace(PETSC_COMM_WORLD, "${PETSC_DIR}/share/petsc/saws", root, sizeof(root)));
622: PetscCallSAWs(SAWs_Set_Document_Root, (root));
623: }
624: }
625: PetscCall(PetscOptionsHasName(NULL, NULL, "-saws_local", &flg2));
626: if (flg2) {
627: char jsdir[PETSC_MAX_PATH_LEN];
628: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_SUP, "-saws_local option requires -saws_root option");
629: PetscCall(PetscSNPrintf(jsdir, sizeof(jsdir), "%s/js", root));
630: PetscCall(PetscTestDirectory(jsdir, 'r', &flg));
631: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "-saws_local option requires js directory in root directory");
632: PetscCallSAWs(SAWs_Push_Local_Header, ());
633: }
634: PetscCall(PetscGetProgramName(programname, sizeof(programname)));
635: PetscCall(PetscStrlen(help, &applinelen));
636: introlen = 4096 + applinelen;
637: applinelen += 1024;
638: PetscCall(PetscMalloc(applinelen, &appline));
639: PetscCall(PetscMalloc(introlen, &intro));
641: if (rootlocal) {
642: PetscCall(PetscSNPrintf(appline, applinelen, "%s.c.html", programname));
643: PetscCall(PetscTestFile(appline, 'r', &rootlocal));
644: }
645: PetscCall(PetscOptionsGetAll(NULL, &options));
646: if (rootlocal && help) {
647: PetscCall(PetscSNPrintf(appline, applinelen, "<center> Running <a href=\"%s.c.html\">%s</a> %s</center><br><center><pre>%s</pre></center><br>\n", programname, programname, options, help));
648: } else if (help) {
649: PetscCall(PetscSNPrintf(appline, applinelen, "<center>Running %s %s</center><br><center><pre>%s</pre></center><br>", programname, options, help));
650: } else {
651: PetscCall(PetscSNPrintf(appline, applinelen, "<center> Running %s %s</center><br>\n", programname, options));
652: }
653: PetscCall(PetscFree(options));
654: PetscCall(PetscGetVersion(version, sizeof(version)));
655: PetscCall(PetscSNPrintf(intro, introlen,
656: "<body>\n"
657: "<center><h2> <a href=\"https://petsc.org/\">PETSc</a> Application Web server powered by <a href=\"https://bitbucket.org/saws/saws\">SAWs</a> </h2></center>\n"
658: "<center>This is the default PETSc application dashboard, from it you can access any published PETSc objects or logging data</center><br><center>%s configured with %s</center><br>\n"
659: "%s",
660: version, petscconfigureoptions, appline));
661: PetscCallSAWs(SAWs_Push_Body, ("index.html", 0, intro));
662: PetscCall(PetscFree(intro));
663: PetscCall(PetscFree(appline));
664: if (selectport) {
665: PetscBool silent;
667: /* another process may have grabbed the port so keep trying */
668: while (SAWs_Initialize()) {
669: PetscCallSAWs(SAWs_Get_Available_Port, (&port));
670: PetscCallSAWs(SAWs_Set_Port, (port));
671: }
673: PetscCall(PetscOptionsGetBool(NULL, NULL, "-saws_port_auto_select_silent", &silent, NULL));
674: if (!silent) {
675: PetscCallSAWs(SAWs_Get_FullURL, (sizeof(sawsurl), sawsurl));
676: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Point your browser to %s for SAWs\n", sawsurl));
677: }
678: } else {
679: PetscCallSAWs(SAWs_Initialize, ());
680: }
681: PetscCall(PetscCitationsRegister("@TechReport{ saws,\n"
682: " Author = {Matt Otten and Jed Brown and Barry Smith},\n"
683: " Title = {Scientific Application Web Server (SAWs) Users Manual},\n"
684: " Institution = {Argonne National Laboratory},\n"
685: " Year = 2013\n}\n",
686: NULL));
687: }
688: PetscFunctionReturn(PETSC_SUCCESS);
689: }
690: #endif
692: /* Things must be done before MPI_Init() when MPI is not yet initialized, and can be shared between C init and Fortran init */
693: PETSC_INTERN PetscErrorCode PetscPreMPIInit_Private(void)
694: {
695: PetscFunctionBegin;
696: #if defined(PETSC_HAVE_HWLOC_SOLARIS_BUG)
697: /* see MPI.py for details on this bug */
698: (void)setenv("HWLOC_COMPONENTS", "-x86", 1);
699: #endif
700: PetscFunctionReturn(PETSC_SUCCESS);
701: }
703: #if PetscDefined(HAVE_ADIOS)
704: #include <adios.h>
705: #include <adios_read.h>
706: int64_t Petsc_adios_group;
707: #endif
708: #if PetscDefined(HAVE_OPENMP)
709: #include <omp.h>
710: PetscInt PetscNumOMPThreads;
711: #endif
713: #include <petsc/private/deviceimpl.h>
714: #if PetscDefined(HAVE_CUDA)
715: #include <petscdevice_cuda.h>
716: // REMOVE ME
717: cudaStream_t PetscDefaultCudaStream = NULL;
718: #endif
719: #if PetscDefined(HAVE_HIP)
720: #include <petscdevice_hip.h>
721: // REMOVE ME
722: hipStream_t PetscDefaultHipStream = NULL;
723: #endif
725: #if PetscDefined(HAVE_DLFCN_H)
726: #include <dlfcn.h>
727: #endif
728: #if PetscDefined(USE_LOG)
729: PETSC_INTERN PetscErrorCode PetscLogInitialize(void);
730: #endif
731: #if PetscDefined(HAVE_VIENNACL)
732: PETSC_EXTERN PetscErrorCode PetscViennaCLInit(void);
733: PetscBool PetscViennaCLSynchronize = PETSC_FALSE;
734: #endif
736: PetscBool PetscCIEnabled = PETSC_FALSE, PetscCIEnabledPortableErrorOutput = PETSC_FALSE;
738: /*
739: PetscInitialize_Common - shared code between C and Fortran initialization
741: prog: program name
742: file: optional PETSc database file name. Might be in Fortran string format when 'ftn' is true
743: help: program help message
744: ftn: is it called from Fortran initialization (petscinitializef_)?
745: readarguments,len: used when fortran is true
746: */
747: PETSC_INTERN PetscErrorCode PetscInitialize_Common(const char *prog, const char *file, const char *help, PetscBool ftn, PetscBool readarguments, PetscInt len)
748: {
749: PetscMPIInt size;
750: PetscBool flg = PETSC_TRUE;
751: char hostname[256];
753: PetscFunctionBegin;
754: if (PetscInitializeCalled) PetscFunctionReturn(PETSC_SUCCESS);
755: /* these must be initialized in a routine, not as a constant declaration */
756: PETSC_STDOUT = stdout;
757: PETSC_STDERR = stderr;
759: /* PetscCall can be used from now */
760: PetscErrorHandlingInitialized = PETSC_TRUE;
762: /*
763: The checking over compatible runtime libraries is complicated by the MPI ABI initiative
764: https://wiki.mpich.org/mpich/index.php/ABI_Compatibility_Initiative which started with
765: MPICH v3.1 (Released February 2014)
766: IBM MPI v2.1 (December 2014)
767: Intel MPI Library v5.0 (2014)
768: Cray MPT v7.0.0 (June 2014)
769: As of July 31, 2017 the ABI number still appears to be 12, that is all of the versions
770: listed above and since that time are compatible.
772: Unfortunately the MPI ABI initiative has not defined a way to determine the ABI number
773: at compile time or runtime. Thus we will need to systematically track the allowed versions
774: and how they are represented in the mpi.h and MPI_Get_library_version() output in order
775: to perform the checking.
777: Currently we only check for pre MPI ABI versions (and packages that do not follow the MPI ABI).
779: Questions:
781: Should the checks for ABI incompatibility be only on the major version number below?
782: Presumably the output to stderr will be removed before a release.
783: */
785: #if defined(PETSC_HAVE_MPI_GET_LIBRARY_VERSION)
786: {
787: char mpilibraryversion[MPI_MAX_LIBRARY_VERSION_STRING];
788: PetscMPIInt mpilibraryversionlength;
790: PetscCallMPI(MPI_Get_library_version(mpilibraryversion, &mpilibraryversionlength));
791: /* check for MPICH versions before MPI ABI initiative */
792: #if defined(MPICH_VERSION)
793: #if MPICH_NUMVERSION < 30100000
794: {
795: char *ver, *lf;
796: PetscBool flg = PETSC_FALSE;
798: PetscCall(PetscStrstr(mpilibraryversion, "MPICH Version:", &ver));
799: if (ver) {
800: PetscCall(PetscStrchr(ver, '\n', &lf));
801: if (lf) {
802: *lf = 0;
803: PetscCall(PetscStrendswith(ver, MPICH_VERSION, &flg));
804: }
805: }
806: if (!flg) {
807: PetscCall(PetscInfo(NULL, "PETSc warning --- MPICH library version \n%s does not match what PETSc was compiled with %s.\n", mpilibraryversion, MPICH_VERSION));
808: flg = PETSC_TRUE;
809: }
810: }
811: #endif
812: /* check for OpenMPI version, it is not part of the MPI ABI initiative (is it part of another initiative that needs to be handled?) */
813: #elif defined(OMPI_MAJOR_VERSION)
814: {
815: char *ver, bs[MPI_MAX_LIBRARY_VERSION_STRING], *bsf;
816: PetscBool flg = PETSC_FALSE;
817: #define PSTRSZ 2
818: char ompistr1[PSTRSZ][MPI_MAX_LIBRARY_VERSION_STRING] = {"Open MPI", "FUJITSU MPI"};
819: char ompistr2[PSTRSZ][MPI_MAX_LIBRARY_VERSION_STRING] = {"v", "Library "};
820: int i;
821: for (i = 0; i < PSTRSZ; i++) {
822: PetscCall(PetscStrstr(mpilibraryversion, ompistr1[i], &ver));
823: if (ver) {
824: PetscCall(PetscSNPrintf(bs, MPI_MAX_LIBRARY_VERSION_STRING, "%s%d.%d", ompistr2[i], OMPI_MAJOR_VERSION, OMPI_MINOR_VERSION));
825: PetscCall(PetscStrstr(ver, bs, &bsf));
826: if (bsf) flg = PETSC_TRUE;
827: break;
828: }
829: }
830: if (!flg) {
831: PetscCall(PetscInfo(NULL, "PETSc warning --- Open MPI library version \n%s does not match what PETSc was compiled with %d.%d.\n", mpilibraryversion, OMPI_MAJOR_VERSION, OMPI_MINOR_VERSION));
832: flg = PETSC_TRUE;
833: }
834: }
835: #endif
836: }
837: #endif
839: #if defined(PETSC_HAVE_DLADDR) && !(defined(__cray__) && defined(__clang__))
840: /* These symbols are currently in the OpenMPI and MPICH libraries; they may not always be, in that case the test will simply not detect the problem */
841: PetscCheck(!dlsym(RTLD_DEFAULT, "ompi_mpi_init") || !dlsym(RTLD_DEFAULT, "MPID_Abort"), PETSC_COMM_SELF, PETSC_ERR_MPI_LIB_INCOMP, "Application was linked against both OpenMPI and MPICH based MPI libraries and will not run correctly");
842: #endif
844: /* on Windows - set printf to default to printing 2 digit exponents */
845: #if defined(PETSC_HAVE__SET_OUTPUT_FORMAT)
846: _set_output_format(_TWO_DIGIT_EXPONENT);
847: #endif
849: PetscCall(PetscOptionsCreateDefault());
851: PetscFinalizeCalled = PETSC_FALSE;
853: PetscCall(PetscSetProgramName(prog));
854: PetscCall(PetscSpinlockCreate(&PetscViewerASCIISpinLockOpen));
855: PetscCall(PetscSpinlockCreate(&PetscViewerASCIISpinLockStdout));
856: PetscCall(PetscSpinlockCreate(&PetscViewerASCIISpinLockStderr));
857: PetscCall(PetscSpinlockCreate(&PetscCommSpinLock));
859: if (PETSC_COMM_WORLD == MPI_COMM_NULL) PETSC_COMM_WORLD = MPI_COMM_WORLD;
860: PetscCallMPI(MPI_Comm_set_errhandler(PETSC_COMM_WORLD, MPI_ERRORS_RETURN));
862: if (PETSC_MPI_ERROR_CLASS == MPI_ERR_LASTCODE) {
863: PetscCallMPI(MPI_Add_error_class(&PETSC_MPI_ERROR_CLASS));
864: PetscCallMPI(MPI_Add_error_code(PETSC_MPI_ERROR_CLASS, &PETSC_MPI_ERROR_CODE));
865: }
867: /* Done after init due to a bug in MPICH-GM? */
868: PetscCall(PetscErrorPrintfInitialize());
870: PetscCallMPI(MPI_Comm_rank(MPI_COMM_WORLD, &PetscGlobalRank));
871: PetscCallMPI(MPI_Comm_size(MPI_COMM_WORLD, &PetscGlobalSize));
873: MPIU_BOOL = MPI_INT;
874: MPIU_ENUM = MPI_INT;
875: MPIU_FORTRANADDR = (sizeof(void *) == sizeof(int)) ? MPI_INT : MPIU_INT64;
876: if (sizeof(size_t) == sizeof(unsigned)) MPIU_SIZE_T = MPI_UNSIGNED;
877: else if (sizeof(size_t) == sizeof(unsigned long)) MPIU_SIZE_T = MPI_UNSIGNED_LONG;
878: #if defined(PETSC_SIZEOF_LONG_LONG)
879: else if (sizeof(size_t) == sizeof(unsigned long long)) MPIU_SIZE_T = MPI_UNSIGNED_LONG_LONG;
880: #endif
881: else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "Could not find MPI type for size_t");
883: /*
884: Initialized the global complex variable; this is because with
885: shared libraries the constructors for global variables
886: are not called; at least on IRIX.
887: */
888: #if defined(PETSC_HAVE_COMPLEX)
889: {
890: #if defined(PETSC_CLANGUAGE_CXX) && !defined(PETSC_USE_REAL___FLOAT128)
891: PetscComplex ic(0.0, 1.0);
892: PETSC_i = ic;
893: #else
894: PETSC_i = _Complex_I;
895: #endif
896: }
897: #endif /* PETSC_HAVE_COMPLEX */
899: /*
900: Create the PETSc MPI reduction operator that sums of the first
901: half of the entries and maxes the second half.
902: */
903: PetscCallMPI(MPI_Op_create(MPIU_MaxSum_Local, 1, &MPIU_MAXSUM_OP));
905: #if defined(PETSC_HAVE_REAL___FLOAT128)
906: PetscCallMPI(MPI_Type_contiguous(2, MPI_DOUBLE, &MPIU___FLOAT128));
907: PetscCallMPI(MPI_Type_commit(&MPIU___FLOAT128));
908: #if defined(PETSC_HAVE_COMPLEX)
909: PetscCallMPI(MPI_Type_contiguous(4, MPI_DOUBLE, &MPIU___COMPLEX128));
910: PetscCallMPI(MPI_Type_commit(&MPIU___COMPLEX128));
911: #endif
912: #endif
913: #if defined(PETSC_HAVE_REAL___FP16)
914: PetscCallMPI(MPI_Type_contiguous(2, MPI_CHAR, &MPIU___FP16));
915: PetscCallMPI(MPI_Type_commit(&MPIU___FP16));
916: #endif
918: #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
919: PetscCallMPI(MPI_Op_create(PetscSum_Local, 1, &MPIU_SUM));
920: PetscCallMPI(MPI_Op_create(PetscMax_Local, 1, &MPIU_MAX));
921: PetscCallMPI(MPI_Op_create(PetscMin_Local, 1, &MPIU_MIN));
922: #elif defined(PETSC_HAVE_REAL___FLOAT128) || defined(PETSC_HAVE_REAL___FP16)
923: PetscCallMPI(MPI_Op_create(PetscSum_Local, 1, &MPIU_SUM___FP16___FLOAT128));
924: #endif
926: PetscCallMPI(MPI_Type_contiguous(2, MPIU_SCALAR, &MPIU_2SCALAR));
927: PetscCallMPI(MPI_Op_create(PetscGarbageKeySortedIntersect, 1, &Petsc_Garbage_SetIntersectOp));
928: PetscCallMPI(MPI_Type_commit(&MPIU_2SCALAR));
930: /* create datatypes used by MPIU_MAXLOC, MPIU_MINLOC and PetscSplitReduction_Op */
931: #if !defined(PETSC_HAVE_MPIUNI)
932: {
933: PetscMPIInt blockSizes[2] = {1, 1};
934: MPI_Aint blockOffsets[2] = {offsetof(struct petsc_mpiu_real_int, v), offsetof(struct petsc_mpiu_real_int, i)};
935: MPI_Datatype blockTypes[2] = {MPIU_REAL, MPIU_INT}, tmpStruct;
937: PetscCallMPI(MPI_Type_create_struct(2, blockSizes, blockOffsets, blockTypes, &tmpStruct));
938: PetscCallMPI(MPI_Type_create_resized(tmpStruct, 0, sizeof(struct petsc_mpiu_real_int), &MPIU_REAL_INT));
939: PetscCallMPI(MPI_Type_free(&tmpStruct));
940: PetscCallMPI(MPI_Type_commit(&MPIU_REAL_INT));
941: }
942: {
943: PetscMPIInt blockSizes[2] = {1, 1};
944: MPI_Aint blockOffsets[2] = {offsetof(struct petsc_mpiu_scalar_int, v), offsetof(struct petsc_mpiu_scalar_int, i)};
945: MPI_Datatype blockTypes[2] = {MPIU_SCALAR, MPIU_INT}, tmpStruct;
947: PetscCallMPI(MPI_Type_create_struct(2, blockSizes, blockOffsets, blockTypes, &tmpStruct));
948: PetscCallMPI(MPI_Type_create_resized(tmpStruct, 0, sizeof(struct petsc_mpiu_scalar_int), &MPIU_SCALAR_INT));
949: PetscCallMPI(MPI_Type_free(&tmpStruct));
950: PetscCallMPI(MPI_Type_commit(&MPIU_SCALAR_INT));
951: }
952: #endif
954: #if defined(PETSC_USE_64BIT_INDICES)
955: PetscCallMPI(MPI_Type_contiguous(2, MPIU_INT, &MPIU_2INT));
956: PetscCallMPI(MPI_Type_commit(&MPIU_2INT));
957: #endif
958: PetscCallMPI(MPI_Type_contiguous(4, MPI_INT, &MPI_4INT));
959: PetscCallMPI(MPI_Type_commit(&MPI_4INT));
960: PetscCallMPI(MPI_Type_contiguous(4, MPIU_INT, &MPIU_4INT));
961: PetscCallMPI(MPI_Type_commit(&MPIU_4INT));
963: /*
964: Attributes to be set on PETSc communicators
965: */
966: PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, Petsc_Counter_Attr_Delete_Fn, &Petsc_Counter_keyval, (void *)0));
967: PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, Petsc_InnerComm_Attr_Delete_Fn, &Petsc_InnerComm_keyval, (void *)0));
968: PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, Petsc_OuterComm_Attr_Delete_Fn, &Petsc_OuterComm_keyval, (void *)0));
969: PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, Petsc_ShmComm_Attr_Delete_Fn, &Petsc_ShmComm_keyval, (void *)0));
970: PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_CreationIdx_keyval, (void *)0));
971: PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Garbage_HMap_keyval, (void *)0));
973: #if defined(PETSC_HAVE_FORTRAN)
974: if (ftn) PetscCall(PetscInitFortran_Private(readarguments, file, len));
975: else
976: #endif
977: PetscCall(PetscOptionsInsert(NULL, &PetscGlobalArgc, &PetscGlobalArgs, file));
979: /* call a second time so it can look in the options database */
980: PetscCall(PetscErrorPrintfInitialize());
982: /*
983: Check system options and print help
984: */
985: PetscCall(PetscOptionsCheckInitial_Private(help));
987: /*
988: Creates the logging data structures; this is enabled even if logging is not turned on
989: This is the last thing we do before returning to the user code to prevent having the
990: logging numbers contaminated by any startup time associated with MPI
991: */
992: #if defined(PETSC_USE_LOG)
993: PetscCall(PetscLogInitialize());
994: #endif
996: /*
997: Initialize PetscDevice and PetscDeviceContext
999: Note to any future devs thinking of moving this, proper initialization requires:
1000: 1. MPI initialized
1001: 2. Options DB initialized
1002: 3. Petsc error handling initialized, specifically signal handlers. This expects to set up
1003: its own SIGSEV handler via the push/pop interface.
1004: 4. Logging initialized
1005: */
1006: PetscCall(PetscDeviceInitializeFromOptions_Internal(PETSC_COMM_WORLD));
1008: #if PetscDefined(HAVE_VIENNACL)
1009: flg = PETSC_FALSE;
1010: PetscCall(PetscOptionsHasName(NULL, NULL, "-log_summary", &flg));
1011: if (!flg) PetscCall(PetscOptionsHasName(NULL, NULL, "-log_view", &flg));
1012: if (!flg) PetscCall(PetscOptionsGetBool(NULL, NULL, "-viennacl_synchronize", &flg, NULL));
1013: PetscViennaCLSynchronize = flg;
1014: PetscCall(PetscViennaCLInit());
1015: #endif
1017: PetscCall(PetscCitationsInitialize());
1019: #if defined(PETSC_HAVE_SAWS)
1020: PetscCall(PetscInitializeSAWs(ftn ? NULL : help));
1021: flg = PETSC_FALSE;
1022: PetscCall(PetscOptionsHasName(NULL, NULL, "-stack_view", &flg));
1023: if (flg) PetscCall(PetscStackViewSAWs());
1024: #endif
1026: /*
1027: Load the dynamic libraries (on machines that support them), this registers all
1028: the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
1029: */
1030: PetscCall(PetscInitialize_DynamicLibraries());
1032: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
1033: PetscCall(PetscInfo(NULL, "PETSc successfully started: number of processors = %d\n", size));
1034: PetscCall(PetscGetHostName(hostname, sizeof(hostname)));
1035: PetscCall(PetscInfo(NULL, "Running on machine: %s\n", hostname));
1036: #if defined(PETSC_HAVE_OPENMP)
1037: {
1038: PetscBool omp_view_flag;
1039: char *threads = getenv("OMP_NUM_THREADS");
1041: if (threads) {
1042: PetscCall(PetscInfo(NULL, "Number of OpenMP threads %s (as given by OMP_NUM_THREADS)\n", threads));
1043: (void)sscanf(threads, "%" PetscInt_FMT, &PetscNumOMPThreads);
1044: } else {
1045: PetscNumOMPThreads = (PetscInt)omp_get_max_threads();
1046: PetscCall(PetscInfo(NULL, "Number of OpenMP threads %" PetscInt_FMT " (as given by omp_get_max_threads())\n", PetscNumOMPThreads));
1047: }
1048: PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "OpenMP options", "Sys");
1049: PetscCall(PetscOptionsInt("-omp_num_threads", "Number of OpenMP threads to use (can also use environmental variable OMP_NUM_THREADS", "None", PetscNumOMPThreads, &PetscNumOMPThreads, &flg));
1050: PetscCall(PetscOptionsName("-omp_view", "Display OpenMP number of threads", NULL, &omp_view_flag));
1051: PetscOptionsEnd();
1052: if (flg) {
1053: PetscCall(PetscInfo(NULL, "Number of OpenMP theads %" PetscInt_FMT " (given by -omp_num_threads)\n", PetscNumOMPThreads));
1054: omp_set_num_threads((int)PetscNumOMPThreads);
1055: }
1056: if (omp_view_flag) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "OpenMP: number of threads %" PetscInt_FMT "\n", PetscNumOMPThreads));
1057: }
1058: #endif
1060: #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
1061: /*
1062: Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI
1064: Currently not used because it is not supported by MPICH.
1065: */
1066: if (!PetscBinaryBigEndian()) PetscCallMPI(MPI_Register_datarep((char *)"petsc", PetscDataRep_read_conv_fn, PetscDataRep_write_conv_fn, PetscDataRep_extent_fn, NULL));
1067: #endif
1069: #if defined(PETSC_SERIALIZE_FUNCTIONS)
1070: PetscCall(PetscFPTCreate(10000));
1071: #endif
1073: #if defined(PETSC_HAVE_HWLOC)
1074: {
1075: PetscViewer viewer;
1076: PetscCall(PetscOptionsGetViewer(PETSC_COMM_WORLD, NULL, NULL, "-process_view", &viewer, NULL, &flg));
1077: if (flg) {
1078: PetscCall(PetscProcessPlacementView(viewer));
1079: PetscCall(PetscViewerDestroy(&viewer));
1080: }
1081: }
1082: #endif
1084: flg = PETSC_TRUE;
1085: PetscCall(PetscOptionsGetBool(NULL, NULL, "-viewfromoptions", &flg, NULL));
1086: if (!flg) PetscCall(PetscOptionsPushGetViewerOff(PETSC_TRUE));
1088: #if defined(PETSC_HAVE_ADIOS)
1089: PetscCallExternal(adios_init_noxml, PETSC_COMM_WORLD);
1090: PetscCallExternal(adios_declare_group, &Petsc_adios_group, "PETSc", "", adios_stat_default);
1091: PetscCallExternal(adios_select_method, Petsc_adios_group, "MPI", "", "");
1092: PetscCallExternal(adios_read_init_method, ADIOS_READ_METHOD_BP, PETSC_COMM_WORLD, "");
1093: #endif
1095: #if defined(__VALGRIND_H)
1096: PETSC_RUNNING_ON_VALGRIND = RUNNING_ON_VALGRIND ? PETSC_TRUE : PETSC_FALSE;
1097: #if defined(PETSC_USING_DARWIN) && defined(PETSC_BLASLAPACK_SDOT_RETURNS_DOUBLE)
1098: if (PETSC_RUNNING_ON_VALGRIND) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "WARNING: Running valgrind with the MacOS native BLAS and LAPACK can fail. If it fails suggest configuring with --download-fblaslapack or --download-f2cblaslapack"));
1099: #endif
1100: #endif
1101: /*
1102: Set flag that we are completely initialized
1103: */
1104: PetscInitializeCalled = PETSC_TRUE;
1106: PetscCall(PetscOptionsHasName(NULL, NULL, "-python", &flg));
1107: if (flg) PetscCall(PetscPythonInitialize(NULL, NULL));
1109: PetscCall(PetscOptionsHasName(NULL, NULL, "-mpi_linear_solver_server", &flg));
1110: if (PetscDefined(USE_SINGLE_LIBRARY) && flg) PetscCall(PCMPIServerBegin());
1111: else PetscCheck(!flg, PETSC_COMM_WORLD, PETSC_ERR_SUP, "PETSc configured using -with-single-library=0; -mpi_linear_solver_server not supported in that case");
1112: PetscFunctionReturn(PETSC_SUCCESS);
1113: }
1115: /*@C
1116: PetscInitialize - Initializes the PETSc database and MPI.
1117: `PetscInitialize()` calls MPI_Init() if that has yet to be called,
1118: so this routine should always be called near the beginning of
1119: your program -- usually the very first line!
1121: Collective on `MPI_COMM_WORLD` or `PETSC_COMM_WORLD` if it has been set
1123: Input Parameters:
1124: + argc - count of number of command line arguments
1125: . args - the command line arguments
1126: . file - [optional] PETSc database file, append ":yaml" to filename to specify YAML options format.
1127: Use NULL or empty string to not check for code specific file.
1128: Also checks ~/.petscrc, .petscrc and petscrc.
1129: Use -skip_petscrc in the code specific file (or command line) to skip ~/.petscrc, .petscrc and petscrc files.
1130: - help - [optional] Help message to print, use NULL for no message
1132: If you wish PETSc code to run ONLY on a subcommunicator of `MPI_COMM_WORLD`, create that
1133: communicator first and assign it to `PETSC_COMM_WORLD` BEFORE calling `PetscInitialize()`. Thus if you are running a
1134: four process job and two processes will run PETSc and have `PetscInitialize()` and PetscFinalize() and two process will not,
1135: then do this. If ALL processes in the job are using `PetscInitialize()` and `PetscFinalize()` then you don't need to do this, even
1136: if different subcommunicators of the job are doing different things with PETSc.
1138: Options Database Keys:
1139: + -help [intro] - prints help method for each option; if intro is given the program stops after printing the introductory help message
1140: . -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
1141: . -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
1142: . -on_error_emacs <machinename> - causes emacsclient to jump to error file
1143: . -on_error_abort - calls `abort()` when error detected (no traceback)
1144: . -on_error_mpiabort - calls `MPI_abort()` when error detected
1145: . -error_output_stdout - prints PETSc error messages to stdout instead of the default stderr
1146: . -error_output_none - does not print the error messages (but handles errors in the same way as if this was not called)
1147: . -debugger_ranks [rank1,rank2,...] - Indicates ranks to start in debugger
1148: . -debugger_pause [sleeptime] (in seconds) - Pauses debugger
1149: . -stop_for_debugger - Print message on how to attach debugger manually to
1150: process and wait (-debugger_pause) seconds for attachment
1151: . -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries) (deprecated, use -malloc_debug)
1152: . -malloc no - Indicates not to use error-checking malloc (deprecated, use -malloc_debug no)
1153: . -malloc_debug - check for memory corruption at EVERY malloc or free, see `PetscMallocSetDebug()`
1154: . -malloc_dump - prints a list of all unfreed memory at the end of the run
1155: . -malloc_test - like -malloc_dump -malloc_debug, but only active for debugging builds, ignored in optimized build. May want to set in PETSC_OPTIONS environmental variable
1156: . -malloc_view - show a list of all allocated memory during `PetscFinalize()`
1157: . -malloc_view_threshold <t> - only list memory allocations of size greater than t with -malloc_view
1158: . -malloc_requested_size - malloc logging will record the requested size rather than size after alignment
1159: . -fp_trap - Stops on floating point exceptions
1160: . -no_signal_handler - Indicates not to trap error signals
1161: . -shared_tmp - indicates /tmp directory is shared by all processors
1162: . -not_shared_tmp - each processor has own /tmp
1163: . -tmp - alternative name of /tmp directory
1164: . -get_total_flops - returns total flops done by all processors
1165: - -memory_view - Print memory usage at end of run
1167: Options Database Keys for Option Database:
1168: + -skip_petscrc - skip the default option files ~/.petscrc, .petscrc, petscrc
1169: . -options_monitor - monitor all set options to standard output for the whole program run
1170: - -options_monitor_cancel - cancel options monitoring hard-wired using `PetscOptionsMonitorSet()`
1172: Options -options_monitor_{all,cancel} are
1173: position-independent and apply to all options set since the PETSc start.
1174: They can be used also in option files.
1176: See `PetscOptionsMonitorSet()` to do monitoring programmatically.
1178: Options Database Keys for Profiling:
1179: See Users-Manual: ch_profiling for details.
1180: + -info [filename][:[~]<list,of,classnames>[:[~]self]] - Prints verbose information. See `PetscInfo()`.
1181: . -log_sync - Enable barrier synchronization for all events. This option is useful to debug imbalance within each event,
1182: however it slows things down and gives a distorted view of the overall runtime.
1183: . -log_trace [filename] - Print traces of all PETSc calls to the screen (useful to determine where a program
1184: hangs without running in the debugger). See `PetscLogTraceBegin()`.
1185: . -log_view [:filename:format] - Prints summary of flop and timing information to screen or file, see `PetscLogView()`.
1186: . -log_view_memory - Includes in the summary from -log_view the memory used in each event, see `PetscLogView()`.
1187: . -log_view_gpu_time - Includes in the summary from -log_view the time used in each GPU kernel, see `PetscLogView().
1188: . -log_summary [filename] - (Deprecated, use -log_view) Prints summary of flop and timing information to screen. If the filename is specified the
1189: summary is written to the file. See PetscLogView().
1190: . -log_exclude: <vec,mat,pc,ksp,snes> - excludes subset of object classes from logging
1191: . -log_all [filename] - Logs extensive profiling information See `PetscLogDump()`.
1192: . -log [filename] - Logs basic profiline information See `PetscLogDump()`.
1193: . -log_mpe [filename] - Creates a logfile viewable by the utility Jumpshot (in MPICH distribution)
1194: . -viewfromoptions on,off - Enable or disable `XXXSetFromOptions()` calls, for applications with many small solves turn this off
1195: - -check_pointer_intensity 0,1,2 - if pointers are checked for validity (debug version only), using 0 will result in faster code
1197: Only one of -log_trace, -log_view, -log_all, -log, or -log_mpe may be used at a time
1199: Options Database Keys for SAWs:
1200: + -saws_port <portnumber> - port number to publish SAWs data, default is 8080
1201: . -saws_port_auto_select - have SAWs select a new unique port number where it publishes the data, the URL is printed to the screen
1202: this is useful when you are running many jobs that utilize SAWs at the same time
1203: . -saws_log <filename> - save a log of all SAWs communication
1204: . -saws_https <certificate file> - have SAWs use HTTPS instead of HTTP
1205: - -saws_root <directory> - allow SAWs to have access to the given directory to search for requested resources and files
1207: Environmental Variables:
1208: + `PETSC_TMP` - alternative tmp directory
1209: . `PETSC_SHARED_TMP` - tmp is shared by all processes
1210: . `PETSC_NOT_SHARED_TMP` - each process has its own private tmp
1211: . `PETSC_OPTIONS` - a string containing additional options for petsc in the form of command line "-key value" pairs
1212: . `PETSC_OPTIONS_YAML` - (requires configuring PETSc to use libyaml) a string containing additional options for petsc in the form of a YAML document
1213: . `PETSC_VIEWER_SOCKET_PORT` - socket number to use for socket viewer
1214: - `PETSC_VIEWER_SOCKET_MACHINE` - machine to use for socket viewer to connect to
1216: Level: beginner
1218: Note:
1219: If for some reason you must call `MPI_Init()` separately, call
1220: it before `PetscInitialize()`.
1222: Fortran Notes:
1223: In Fortran this routine can be called with
1224: .vb
1225: call PetscInitialize(ierr)
1226: call PetscInitialize(file,ierr) or
1227: call PetscInitialize(file,help,ierr)
1228: .ve
1230: If your main program is C but you call Fortran code that also uses PETSc you need to call `PetscInitializeFortran()` soon after
1231: calling `PetscInitialize()`.
1233: Options Database Key for Developers:
1234: . -checkfunctionlist - automatically checks that function lists associated with objects are correctly cleaned up. Produces messages of the form:
1235: "function name: MatInodeGetInodeSizes_C" if they are not cleaned up. This flag is always set for the test harness (in framework.py)
1237: .seealso: `PetscFinalize()`, `PetscInitializeFortran()`, `PetscGetArgs()`, `PetscInitializeNoArguments()`, `PetscLogGpuTime()`
1238: @*/
1239: PetscErrorCode PetscInitialize(int *argc, char ***args, const char file[], const char help[])
1240: {
1241: PetscMPIInt flag;
1242: const char *prog = "Unknown Name", *mpienv;
1244: PetscFunctionBegin;
1245: if (PetscInitializeCalled) PetscFunctionReturn(PETSC_SUCCESS);
1246: PetscCallMPI(MPI_Initialized(&flag));
1247: if (!flag) {
1248: PetscCheck(PETSC_COMM_WORLD == MPI_COMM_NULL, PETSC_COMM_SELF, PETSC_ERR_SUP, "You cannot set PETSC_COMM_WORLD if you have not initialized MPI first");
1249: PetscCall(PetscPreMPIInit_Private());
1250: #if defined(PETSC_HAVE_MPI_INIT_THREAD)
1251: {
1252: PetscMPIInt PETSC_UNUSED provided;
1253: PetscCallMPI(MPI_Init_thread(argc, args, PETSC_MPI_THREAD_REQUIRED, &provided));
1254: }
1255: #else
1256: PetscCallMPI(MPI_Init(argc, args));
1257: #endif
1258: if (PetscDefined(HAVE_MPIUNI)) {
1259: mpienv = getenv("PMI_SIZE");
1260: if (!mpienv) mpienv = getenv("OMPI_COMM_WORLD_SIZE");
1261: if (mpienv) {
1262: PetscInt isize;
1263: PetscCall(PetscOptionsStringToInt(mpienv, &isize));
1264: if (isize != 1) printf("You are using an MPI-uni (sequential) install of PETSc but trying to launch parallel jobs; you need full MPI version of PETSc\n");
1265: PetscCheck(isize == 1, MPI_COMM_SELF, PETSC_ERR_MPI, "You are using an MPI-uni (sequential) install of PETSc but trying to launch parallel jobs; you need full MPI version of PETSc");
1266: }
1267: }
1268: PetscBeganMPI = PETSC_TRUE;
1269: }
1271: if (argc && *argc) prog = **args;
1272: if (argc && args) {
1273: PetscGlobalArgc = *argc;
1274: PetscGlobalArgs = *args;
1275: }
1276: PetscCall(PetscInitialize_Common(prog, file, help, PETSC_FALSE, PETSC_FALSE, 0));
1277: PetscFunctionReturn(PETSC_SUCCESS);
1278: }
1280: #if PetscDefined(USE_LOG)
1281: PETSC_INTERN PetscObject *PetscObjects;
1282: PETSC_INTERN PetscInt PetscObjectsCounts;
1283: PETSC_INTERN PetscInt PetscObjectsMaxCounts;
1284: PETSC_INTERN PetscBool PetscObjectsLog;
1285: #endif
1287: /*
1288: Frees all the MPI types and operations that PETSc may have created
1289: */
1290: PetscErrorCode PetscFreeMPIResources(void)
1291: {
1292: PetscFunctionBegin;
1293: #if defined(PETSC_HAVE_REAL___FLOAT128)
1294: PetscCallMPI(MPI_Type_free(&MPIU___FLOAT128));
1295: #if defined(PETSC_HAVE_COMPLEX)
1296: PetscCallMPI(MPI_Type_free(&MPIU___COMPLEX128));
1297: #endif
1298: #endif
1299: #if defined(PETSC_HAVE_REAL___FP16)
1300: PetscCallMPI(MPI_Type_free(&MPIU___FP16));
1301: #endif
1303: #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
1304: PetscCallMPI(MPI_Op_free(&MPIU_SUM));
1305: PetscCallMPI(MPI_Op_free(&MPIU_MAX));
1306: PetscCallMPI(MPI_Op_free(&MPIU_MIN));
1307: #elif defined(PETSC_HAVE_REAL___FLOAT128) || defined(PETSC_HAVE_REAL___FP16)
1308: PetscCallMPI(MPI_Op_free(&MPIU_SUM___FP16___FLOAT128));
1309: #endif
1311: PetscCallMPI(MPI_Type_free(&MPIU_2SCALAR));
1312: PetscCallMPI(MPI_Type_free(&MPIU_REAL_INT));
1313: PetscCallMPI(MPI_Type_free(&MPIU_SCALAR_INT));
1314: #if defined(PETSC_USE_64BIT_INDICES)
1315: PetscCallMPI(MPI_Type_free(&MPIU_2INT));
1316: #endif
1317: PetscCallMPI(MPI_Type_free(&MPI_4INT));
1318: PetscCallMPI(MPI_Type_free(&MPIU_4INT));
1319: PetscCallMPI(MPI_Op_free(&MPIU_MAXSUM_OP));
1320: PetscCallMPI(MPI_Op_free(&Petsc_Garbage_SetIntersectOp));
1321: PetscFunctionReturn(PETSC_SUCCESS);
1322: }
1324: #if PetscDefined(USE_LOG)
1325: PETSC_INTERN PetscErrorCode PetscLogFinalize(void);
1326: #endif
1328: /*@C
1329: PetscFinalize - Checks for options to be called at the conclusion
1330: of the program. `MPI_Finalize()` is called only if the user had not
1331: called `MPI_Init()` before calling `PetscInitialize()`.
1333: Collective on `PETSC_COMM_WORLD`
1335: Options Database Keys:
1336: + -options_view - Calls `PetscOptionsView()`
1337: . -options_left - Prints unused options that remain in the database
1338: . -objects_dump [all] - Prints list of objects allocated by the user that have not been freed, the option all cause all outstanding objects to be listed
1339: . -mpidump - Calls PetscMPIDump()
1340: . -malloc_dump <optional filename> - Calls `PetscMallocDump()`, displays all memory allocated that has not been freed
1341: . -malloc_info - Prints total memory usage
1342: - -malloc_view <optional filename> - Prints list of all memory allocated and where
1344: Level: beginner
1346: Note:
1347: See `PetscInitialize()` for other runtime options.
1349: .seealso: `PetscInitialize()`, `PetscOptionsView()`, `PetscMallocDump()`, `PetscMPIDump()`, `PetscEnd()`
1350: @*/
1351: PetscErrorCode PetscFinalize(void)
1352: {
1353: PetscMPIInt rank;
1354: PetscInt nopt;
1355: PetscBool flg1 = PETSC_FALSE, flg2 = PETSC_FALSE, flg3 = PETSC_FALSE;
1356: PetscBool flg;
1357: #if defined(PETSC_USE_LOG)
1358: char mname[PETSC_MAX_PATH_LEN];
1359: #endif
1361: PetscFunctionBegin;
1362: PetscCheck(PetscInitializeCalled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "PetscInitialize() must be called before PetscFinalize()");
1363: PetscCall(PetscInfo(NULL, "PetscFinalize() called\n"));
1365: PetscCall(PetscOptionsHasName(NULL, NULL, "-mpi_linear_solver_server", &flg));
1366: if (PetscDefined(USE_SINGLE_LIBRARY) && flg) PetscCall(PCMPIServerEnd());
1368: /* Clean up Garbage automatically on COMM_SELF and COMM_WORLD at finalize */
1369: {
1370: union
1371: {
1372: MPI_Comm comm;
1373: void *ptr;
1374: } ucomm;
1375: PetscMPIInt flg;
1376: void *tmp;
1378: PetscCallMPI(MPI_Comm_get_attr(PETSC_COMM_SELF, Petsc_InnerComm_keyval, &ucomm, &flg));
1379: if (flg) PetscCallMPI(MPI_Comm_get_attr(ucomm.comm, Petsc_Garbage_HMap_keyval, &tmp, &flg));
1380: if (flg) PetscCall(PetscGarbageCleanup(PETSC_COMM_SELF));
1381: PetscCallMPI(MPI_Comm_get_attr(PETSC_COMM_WORLD, Petsc_InnerComm_keyval, &ucomm, &flg));
1382: if (flg) PetscCallMPI(MPI_Comm_get_attr(ucomm.comm, Petsc_Garbage_HMap_keyval, &tmp, &flg));
1383: if (flg) PetscCall(PetscGarbageCleanup(PETSC_COMM_WORLD));
1384: }
1386: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
1387: #if defined(PETSC_HAVE_ADIOS)
1388: PetscCallExternal(adios_read_finalize_method, ADIOS_READ_METHOD_BP_AGGREGATE);
1389: PetscCallExternal(adios_finalize, rank);
1390: #endif
1391: PetscCall(PetscOptionsHasName(NULL, NULL, "-citations", &flg));
1392: if (flg) {
1393: char *cits, filename[PETSC_MAX_PATH_LEN];
1394: FILE *fd = PETSC_STDOUT;
1396: PetscCall(PetscOptionsGetString(NULL, NULL, "-citations", filename, sizeof(filename), NULL));
1397: if (filename[0]) PetscCall(PetscFOpen(PETSC_COMM_WORLD, filename, "w", &fd));
1398: PetscCall(PetscSegBufferGet(PetscCitationsList, 1, &cits));
1399: cits[0] = 0;
1400: PetscCall(PetscSegBufferExtractAlloc(PetscCitationsList, &cits));
1401: PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "If you publish results based on this computation please cite the following:\n"));
1402: PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "===========================================================================\n"));
1403: PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "%s", cits));
1404: PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "===========================================================================\n"));
1405: PetscCall(PetscFClose(PETSC_COMM_WORLD, fd));
1406: PetscCall(PetscFree(cits));
1407: }
1408: PetscCall(PetscSegBufferDestroy(&PetscCitationsList));
1410: #if defined(PETSC_HAVE_SSL) && defined(PETSC_USE_SOCKET_VIEWER)
1411: /* TextBelt is run for testing purposes only, please do not use this feature often */
1412: {
1413: PetscInt nmax = 2;
1414: char **buffs;
1415: PetscCall(PetscMalloc1(2, &buffs));
1416: PetscCall(PetscOptionsGetStringArray(NULL, NULL, "-textbelt", buffs, &nmax, &flg1));
1417: if (flg1) {
1418: PetscCheck(nmax, PETSC_COMM_WORLD, PETSC_ERR_USER, "-textbelt requires either the phone number or number,\"message\"");
1419: if (nmax == 1) {
1420: size_t len = 128;
1421: PetscCall(PetscMalloc1(len, &buffs[1]));
1422: PetscCall(PetscGetProgramName(buffs[1], 32));
1423: PetscCall(PetscStrlcat(buffs[1], " has completed", len));
1424: }
1425: PetscCall(PetscTextBelt(PETSC_COMM_WORLD, buffs[0], buffs[1], NULL));
1426: PetscCall(PetscFree(buffs[0]));
1427: PetscCall(PetscFree(buffs[1]));
1428: }
1429: PetscCall(PetscFree(buffs));
1430: }
1431: {
1432: PetscInt nmax = 2;
1433: char **buffs;
1434: PetscCall(PetscMalloc1(2, &buffs));
1435: PetscCall(PetscOptionsGetStringArray(NULL, NULL, "-tellmycell", buffs, &nmax, &flg1));
1436: if (flg1) {
1437: PetscCheck(nmax, PETSC_COMM_WORLD, PETSC_ERR_USER, "-tellmycell requires either the phone number or number,\"message\"");
1438: if (nmax == 1) {
1439: size_t len = 128;
1440: PetscCall(PetscMalloc1(len, &buffs[1]));
1441: PetscCall(PetscGetProgramName(buffs[1], 32));
1442: PetscCall(PetscStrlcat(buffs[1], " has completed", len));
1443: }
1444: PetscCall(PetscTellMyCell(PETSC_COMM_WORLD, buffs[0], buffs[1], NULL));
1445: PetscCall(PetscFree(buffs[0]));
1446: PetscCall(PetscFree(buffs[1]));
1447: }
1448: PetscCall(PetscFree(buffs));
1449: }
1450: #endif
1452: #if defined(PETSC_SERIALIZE_FUNCTIONS)
1453: PetscCall(PetscFPTDestroy());
1454: #endif
1456: #if defined(PETSC_HAVE_SAWS)
1457: flg = PETSC_FALSE;
1458: PetscCall(PetscOptionsGetBool(NULL, NULL, "-saw_options", &flg, NULL));
1459: if (flg) PetscCall(PetscOptionsSAWsDestroy());
1460: #endif
1462: #if defined(PETSC_HAVE_X)
1463: flg1 = PETSC_FALSE;
1464: PetscCall(PetscOptionsGetBool(NULL, NULL, "-x_virtual", &flg1, NULL));
1465: if (flg1) {
1466: /* this is a crude hack, but better than nothing */
1467: PetscCall(PetscPOpen(PETSC_COMM_WORLD, NULL, "pkill -9 Xvfb", "r", NULL));
1468: }
1469: #endif
1471: #if !defined(PETSC_HAVE_THREADSAFETY)
1472: PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_info", &flg2, NULL));
1473: if (!flg2) {
1474: flg2 = PETSC_FALSE;
1475: PetscCall(PetscOptionsGetBool(NULL, NULL, "-memory_view", &flg2, NULL));
1476: }
1477: if (flg2) PetscCall(PetscMemoryView(PETSC_VIEWER_STDOUT_WORLD, "Summary of Memory Usage in PETSc\n"));
1478: #endif
1480: #if defined(PETSC_USE_LOG)
1481: flg1 = PETSC_FALSE;
1482: PetscCall(PetscOptionsGetBool(NULL, NULL, "-get_total_flops", &flg1, NULL));
1483: if (flg1) {
1484: PetscLogDouble flops = 0;
1485: PetscCallMPI(MPI_Reduce(&petsc_TotalFlops, &flops, 1, MPI_DOUBLE, MPI_SUM, 0, PETSC_COMM_WORLD));
1486: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Total flops over all processors %g\n", flops));
1487: }
1488: #endif
1490: #if defined(PETSC_USE_LOG)
1491: #if defined(PETSC_HAVE_MPE)
1492: mname[0] = 0;
1493: PetscCall(PetscOptionsGetString(NULL, NULL, "-log_mpe", mname, sizeof(mname), &flg1));
1494: if (flg1) {
1495: if (mname[0]) PetscCall(PetscLogMPEDump(mname));
1496: else PetscCall(PetscLogMPEDump(0));
1497: }
1498: #endif
1499: #endif
1501: /*
1502: Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_().
1503: */
1504: PetscCall(PetscObjectRegisterDestroyAll());
1506: #if defined(PETSC_USE_LOG)
1507: PetscCall(PetscOptionsPushGetViewerOff(PETSC_FALSE));
1508: PetscCall(PetscLogViewFromOptions());
1509: PetscCall(PetscOptionsPopGetViewerOff());
1511: mname[0] = 0;
1512: PetscCall(PetscOptionsGetString(NULL, NULL, "-log_summary", mname, sizeof(mname), &flg1));
1513: if (flg1) {
1514: PetscViewer viewer;
1515: PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, "\n\n WARNING: -log_summary is being deprecated; switch to -log_view\n\n\n"));
1516: if (mname[0]) {
1517: PetscCall(PetscViewerASCIIOpen(PETSC_COMM_WORLD, mname, &viewer));
1518: PetscCall(PetscLogView(viewer));
1519: PetscCall(PetscViewerDestroy(&viewer));
1520: } else {
1521: viewer = PETSC_VIEWER_STDOUT_WORLD;
1522: PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_DEFAULT));
1523: PetscCall(PetscLogView(viewer));
1524: PetscCall(PetscViewerPopFormat(viewer));
1525: }
1526: }
1528: /*
1529: Free any objects created by the last block of code.
1530: */
1531: PetscCall(PetscObjectRegisterDestroyAll());
1533: mname[0] = 0;
1534: PetscCall(PetscOptionsGetString(NULL, NULL, "-log_all", mname, sizeof(mname), &flg1));
1535: PetscCall(PetscOptionsGetString(NULL, NULL, "-log", mname, sizeof(mname), &flg2));
1536: if (flg1 || flg2) PetscCall(PetscLogDump(mname));
1537: #endif
1539: flg1 = PETSC_FALSE;
1540: PetscCall(PetscOptionsGetBool(NULL, NULL, "-no_signal_handler", &flg1, NULL));
1541: if (!flg1) PetscCall(PetscPopSignalHandler());
1542: flg1 = PETSC_FALSE;
1543: PetscCall(PetscOptionsGetBool(NULL, NULL, "-mpidump", &flg1, NULL));
1544: if (flg1) PetscCall(PetscMPIDump(stdout));
1545: flg1 = PETSC_FALSE;
1546: flg2 = PETSC_FALSE;
1547: /* preemptive call to avoid listing this option in options table as unused */
1548: PetscCall(PetscOptionsHasName(NULL, NULL, "-malloc_dump", &flg1));
1549: PetscCall(PetscOptionsHasName(NULL, NULL, "-objects_dump", &flg1));
1550: PetscCall(PetscOptionsGetBool(NULL, NULL, "-options_view", &flg2, NULL));
1552: if (flg2) {
1553: PetscViewer viewer;
1554: PetscCall(PetscViewerCreate(PETSC_COMM_WORLD, &viewer));
1555: PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
1556: PetscCall(PetscOptionsView(NULL, viewer));
1557: PetscCall(PetscViewerDestroy(&viewer));
1558: }
1560: /* to prevent PETSc -options_left from warning */
1561: PetscCall(PetscOptionsHasName(NULL, NULL, "-nox", &flg1));
1562: PetscCall(PetscOptionsHasName(NULL, NULL, "-nox_warning", &flg1));
1564: flg3 = PETSC_FALSE; /* default value is required */
1565: PetscCall(PetscOptionsGetBool(NULL, NULL, "-options_left", &flg3, &flg1));
1566: if (PetscUnlikelyDebug(!flg1)) flg3 = PETSC_TRUE;
1567: if (flg3) {
1568: if (!flg2 && flg1) { /* have not yet printed the options */
1569: PetscViewer viewer;
1570: PetscCall(PetscViewerCreate(PETSC_COMM_WORLD, &viewer));
1571: PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
1572: PetscCall(PetscOptionsView(NULL, viewer));
1573: PetscCall(PetscViewerDestroy(&viewer));
1574: }
1575: PetscCall(PetscOptionsAllUsed(NULL, &nopt));
1576: if (nopt) {
1577: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "WARNING! There are options you set that were not used!\n"));
1578: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "WARNING! could be spelling mistake, etc!\n"));
1579: if (nopt == 1) {
1580: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "There is one unused database option. It is:\n"));
1581: } else {
1582: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "There are %" PetscInt_FMT " unused database options. They are:\n", nopt));
1583: }
1584: } else if (flg3 && flg1) {
1585: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "There are no unused options.\n"));
1586: }
1587: PetscCall(PetscOptionsLeft(NULL));
1588: }
1590: #if defined(PETSC_HAVE_SAWS)
1591: if (!PetscGlobalRank) {
1592: PetscCall(PetscStackSAWsViewOff());
1593: PetscCallSAWs(SAWs_Finalize, ());
1594: }
1595: #endif
1597: #if defined(PETSC_USE_LOG)
1598: /*
1599: List all objects the user may have forgot to free
1600: */
1601: if (PetscObjectsLog) {
1602: PetscCall(PetscOptionsHasName(NULL, NULL, "-objects_dump", &flg1));
1603: if (flg1) {
1604: MPI_Comm local_comm;
1605: char string[64];
1607: PetscCall(PetscOptionsGetString(NULL, NULL, "-objects_dump", string, sizeof(string), NULL));
1608: PetscCallMPI(MPI_Comm_dup(PETSC_COMM_WORLD, &local_comm));
1609: PetscCall(PetscSequentialPhaseBegin_Private(local_comm, 1));
1610: PetscCall(PetscObjectsDump(stdout, (string[0] == 'a') ? PETSC_TRUE : PETSC_FALSE));
1611: PetscCall(PetscSequentialPhaseEnd_Private(local_comm, 1));
1612: PetscCallMPI(MPI_Comm_free(&local_comm));
1613: }
1614: }
1615: #endif
1617: #if defined(PETSC_USE_LOG)
1618: PetscObjectsCounts = 0;
1619: PetscObjectsMaxCounts = 0;
1620: PetscCall(PetscFree(PetscObjects));
1621: #endif
1623: /*
1624: Destroy any packages that registered a finalize
1625: */
1626: PetscCall(PetscRegisterFinalizeAll());
1628: #if defined(PETSC_USE_LOG)
1629: PetscCall(PetscLogFinalize());
1630: #endif
1632: /*
1633: Print PetscFunctionLists that have not been properly freed
1634: */
1635: if (PetscPrintFunctionList) PetscCall(PetscFunctionListPrintAll());
1637: if (petsc_history) {
1638: PetscCall(PetscCloseHistoryFile(&petsc_history));
1639: petsc_history = NULL;
1640: }
1641: PetscCall(PetscOptionsHelpPrintedDestroy(&PetscOptionsHelpPrintedSingleton));
1642: PetscCall(PetscInfoDestroy());
1644: #if !defined(PETSC_HAVE_THREADSAFETY)
1645: if (!(PETSC_RUNNING_ON_VALGRIND)) {
1646: char fname[PETSC_MAX_PATH_LEN];
1647: char sname[PETSC_MAX_PATH_LEN];
1648: FILE *fd;
1649: int err;
1651: flg2 = PETSC_FALSE;
1652: flg3 = PETSC_FALSE;
1653: if (PetscDefined(USE_DEBUG)) PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_test", &flg2, NULL));
1654: PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_debug", &flg3, NULL));
1655: fname[0] = 0;
1656: PetscCall(PetscOptionsGetString(NULL, NULL, "-malloc_dump", fname, sizeof(fname), &flg1));
1657: if (flg1 && fname[0]) {
1658: PetscCall(PetscSNPrintf(sname, sizeof(sname), "%s_%d", fname, rank));
1659: fd = fopen(sname, "w");
1660: PetscCheck(fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open log file: %s", sname);
1661: PetscCall(PetscMallocDump(fd));
1662: err = fclose(fd);
1663: PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_SYS, "fclose() failed on file");
1664: } else if (flg1 || flg2 || flg3) {
1665: MPI_Comm local_comm;
1667: PetscCallMPI(MPI_Comm_dup(PETSC_COMM_WORLD, &local_comm));
1668: PetscCall(PetscSequentialPhaseBegin_Private(local_comm, 1));
1669: PetscCall(PetscMallocDump(stdout));
1670: PetscCall(PetscSequentialPhaseEnd_Private(local_comm, 1));
1671: PetscCallMPI(MPI_Comm_free(&local_comm));
1672: }
1673: fname[0] = 0;
1674: PetscCall(PetscOptionsGetString(NULL, NULL, "-malloc_view", fname, sizeof(fname), &flg1));
1675: if (flg1 && fname[0]) {
1676: PetscCall(PetscSNPrintf(sname, sizeof(sname), "%s_%d", fname, rank));
1677: fd = fopen(sname, "w");
1678: PetscCheck(fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open log file: %s", sname);
1679: PetscCall(PetscMallocView(fd));
1680: err = fclose(fd);
1681: PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_SYS, "fclose() failed on file");
1682: } else if (flg1) {
1683: MPI_Comm local_comm;
1685: PetscCallMPI(MPI_Comm_dup(PETSC_COMM_WORLD, &local_comm));
1686: PetscCall(PetscSequentialPhaseBegin_Private(local_comm, 1));
1687: PetscCall(PetscMallocView(stdout));
1688: PetscCall(PetscSequentialPhaseEnd_Private(local_comm, 1));
1689: PetscCallMPI(MPI_Comm_free(&local_comm));
1690: }
1691: }
1692: #endif
1694: /*
1695: Close any open dynamic libraries
1696: */
1697: PetscCall(PetscFinalize_DynamicLibraries());
1699: /* Can be destroyed only after all the options are used */
1700: PetscCall(PetscOptionsDestroyDefault());
1702: PetscGlobalArgc = 0;
1703: PetscGlobalArgs = NULL;
1705: #if defined(PETSC_HAVE_KOKKOS)
1706: if (PetscBeganKokkos) {
1707: PetscCall(PetscKokkosFinalize_Private());
1708: PetscBeganKokkos = PETSC_FALSE;
1709: PetscKokkosInitialized = PETSC_FALSE;
1710: }
1711: #endif
1713: #if defined(PETSC_HAVE_NVSHMEM)
1714: if (PetscBeganNvshmem) {
1715: PetscCall(PetscNvshmemFinalize());
1716: PetscBeganNvshmem = PETSC_FALSE;
1717: }
1718: #endif
1720: PetscCall(PetscFreeMPIResources());
1722: /*
1723: Destroy any known inner MPI_Comm's and attributes pointing to them
1724: Note this will not destroy any new communicators the user has created.
1726: If all PETSc objects were not destroyed those left over objects will have hanging references to
1727: the MPI_Comms that were freed; but that is ok because those PETSc objects will never be used again
1728: */
1729: {
1730: PetscCommCounter *counter;
1731: PetscMPIInt flg;
1732: MPI_Comm icomm;
1733: union
1734: {
1735: MPI_Comm comm;
1736: void *ptr;
1737: } ucomm;
1738: PetscCallMPI(MPI_Comm_get_attr(PETSC_COMM_SELF, Petsc_InnerComm_keyval, &ucomm, &flg));
1739: if (flg) {
1740: icomm = ucomm.comm;
1741: PetscCallMPI(MPI_Comm_get_attr(icomm, Petsc_Counter_keyval, &counter, &flg));
1742: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory");
1744: PetscCallMPI(MPI_Comm_delete_attr(PETSC_COMM_SELF, Petsc_InnerComm_keyval));
1745: PetscCallMPI(MPI_Comm_delete_attr(icomm, Petsc_Counter_keyval));
1746: PetscCallMPI(MPI_Comm_free(&icomm));
1747: }
1748: PetscCallMPI(MPI_Comm_get_attr(PETSC_COMM_WORLD, Petsc_InnerComm_keyval, &ucomm, &flg));
1749: if (flg) {
1750: icomm = ucomm.comm;
1751: PetscCallMPI(MPI_Comm_get_attr(icomm, Petsc_Counter_keyval, &counter, &flg));
1752: PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_ARG_CORRUPT, "Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory");
1754: PetscCallMPI(MPI_Comm_delete_attr(PETSC_COMM_WORLD, Petsc_InnerComm_keyval));
1755: PetscCallMPI(MPI_Comm_delete_attr(icomm, Petsc_Counter_keyval));
1756: PetscCallMPI(MPI_Comm_free(&icomm));
1757: }
1758: }
1760: PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Counter_keyval));
1761: PetscCallMPI(MPI_Comm_free_keyval(&Petsc_InnerComm_keyval));
1762: PetscCallMPI(MPI_Comm_free_keyval(&Petsc_OuterComm_keyval));
1763: PetscCallMPI(MPI_Comm_free_keyval(&Petsc_ShmComm_keyval));
1764: PetscCallMPI(MPI_Comm_free_keyval(&Petsc_CreationIdx_keyval));
1765: PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Garbage_HMap_keyval));
1767: // Free keyvals which may be silently created by some routines
1768: if (Petsc_SharedWD_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_SharedWD_keyval));
1769: if (Petsc_SharedTmp_keyval != MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_free_keyval(&Petsc_SharedTmp_keyval));
1771: PetscCall(PetscSpinlockDestroy(&PetscViewerASCIISpinLockOpen));
1772: PetscCall(PetscSpinlockDestroy(&PetscViewerASCIISpinLockStdout));
1773: PetscCall(PetscSpinlockDestroy(&PetscViewerASCIISpinLockStderr));
1774: PetscCall(PetscSpinlockDestroy(&PetscCommSpinLock));
1776: if (PetscBeganMPI) {
1777: PetscMPIInt flag;
1778: PetscCallMPI(MPI_Finalized(&flag));
1779: PetscCheck(!flag, PETSC_COMM_SELF, PETSC_ERR_LIB, "MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()");
1780: /* wait until the very last moment to disable error handling */
1781: PetscErrorHandlingInitialized = PETSC_FALSE;
1782: PetscCallMPI(MPI_Finalize());
1783: } else PetscErrorHandlingInitialized = PETSC_FALSE;
1785: /*
1787: Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because
1788: the communicator has some outstanding requests on it. Specifically if the
1789: flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See
1790: src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate()
1791: is never freed as it should be. Thus one may obtain messages of the form
1792: [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the
1793: memory was not freed.
1795: */
1796: PetscCall(PetscMallocClear());
1797: PetscCall(PetscStackReset());
1799: PetscInitializeCalled = PETSC_FALSE;
1800: PetscFinalizeCalled = PETSC_TRUE;
1801: #if defined(PETSC_USE_COVERAGE)
1802: /*
1803: flush gcov, otherwise during CI the flushing continues into the next pipeline resulting in git not being able to delete directories since the
1804: gcov files are still being added to the directories as git tries to remove the directories.
1805: */
1806: __gcov_flush();
1807: #endif
1808: /* To match PetscFunctionBegin() at the beginning of this function */
1809: PetscStackClearTop;
1810: return PETSC_SUCCESS;
1811: }
1813: #if defined(PETSC_MISSING_LAPACK_lsame_)
1814: PETSC_EXTERN int lsame_(char *a, char *b)
1815: {
1816: if (*a == *b) return 1;
1817: if (*a + 32 == *b) return 1;
1818: if (*a - 32 == *b) return 1;
1819: return 0;
1820: }
1821: #endif
1823: #if defined(PETSC_MISSING_LAPACK_lsame)
1824: PETSC_EXTERN int lsame(char *a, char *b)
1825: {
1826: if (*a == *b) return 1;
1827: if (*a + 32 == *b) return 1;
1828: if (*a - 32 == *b) return 1;
1829: return 0;
1830: }
1831: #endif