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