xref: /petsc/src/sys/objects/pinit.c (revision 689d2152341de2838b18bf7ec01f5335e95e3d3b)
1 
2 /*
3    This file defines the initialization of PETSc, including PetscInitialize()
4 */
5 #include <petsc/private/petscimpl.h>        /*I  "petscsys.h"   I*/
6 #include <petscvalgrind.h>
7 #include <petscviewer.h>
8 
9 #if defined(PETSC_USE_LOG)
10 PETSC_EXTERN PetscErrorCode PetscLogInitialize(void);
11 PETSC_EXTERN PetscErrorCode PetscLogFinalize(void);
12 #endif
13 
14 #if defined(PETSC_SERIALIZE_FUNCTIONS)
15 PetscFPT PetscFPTData = 0;
16 #endif
17 
18 #if defined(PETSC_HAVE_SAWS)
19 #include <petscviewersaws.h>
20 #endif
21 /* -----------------------------------------------------------------------------------------*/
22 
23 extern FILE *petsc_history;
24 
25 extern PetscErrorCode PetscInitialize_DynamicLibraries(void);
26 extern PetscErrorCode PetscFinalize_DynamicLibraries(void);
27 extern PetscErrorCode PetscFunctionListPrintAll(void);
28 extern PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int);
29 extern PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int);
30 extern PetscErrorCode PetscCloseHistoryFile(FILE**);
31 
32 /* user may set this BEFORE calling PetscInitialize() */
33 MPI_Comm PETSC_COMM_WORLD = MPI_COMM_NULL;
34 
35 PetscMPIInt Petsc_Counter_keyval   = MPI_KEYVAL_INVALID;
36 PetscMPIInt Petsc_InnerComm_keyval = MPI_KEYVAL_INVALID;
37 PetscMPIInt Petsc_OuterComm_keyval = MPI_KEYVAL_INVALID;
38 PetscMPIInt Petsc_Shared_keyval    = MPI_KEYVAL_INVALID;
39 
40 /*
41      Declare and set all the string names of the PETSc enums
42 */
43 const char *const PetscBools[]     = {"FALSE","TRUE","PetscBool","PETSC_",0};
44 const char *const PetscCopyModes[] = {"COPY_VALUES","OWN_POINTER","USE_POINTER","PetscCopyMode","PETSC_",0};
45 const char *const PetscDataTypes[] = {"INT","DOUBLE","COMPLEX","LONG","SHORT","FLOAT",
46                                       "CHAR","LOGICAL","ENUM","BOOL","LONGDOUBLE","OBJECT","FUNCTION","PetscDataType","PETSC_",0};
47 
48 PetscBool PetscPreLoadingUsed = PETSC_FALSE;
49 PetscBool PetscPreLoadingOn   = PETSC_FALSE;
50 
51 PetscInt PetscHotRegionDepth;
52 
53 #if defined(PETSC_HAVE_THREADSAFETY)
54 PetscSpinlock PetscViewerASCIISpinLockOpen;
55 PetscSpinlock PetscViewerASCIISpinLockStdout;
56 PetscSpinlock PetscViewerASCIISpinLockStderr;
57 PetscSpinlock PetscCommSpinLock;
58 #endif
59 
60 /*
61        Checks the options database for initializations related to the
62     PETSc components
63 */
64 PetscErrorCode  PetscOptionsCheckInitial_Components(void)
65 {
66   PetscBool      flg1;
67   PetscErrorCode ierr;
68 
69   PetscFunctionBegin;
70   ierr = PetscOptionsHasName(NULL,NULL,"-help",&flg1);CHKERRQ(ierr);
71   if (flg1) {
72 #if defined(PETSC_USE_LOG)
73     MPI_Comm comm = PETSC_COMM_WORLD;
74     ierr = (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");CHKERRQ(ierr);
75     ierr = (*PetscHelpPrintf)(comm," -log_exclude: <vec,mat,pc,ksp,snes,tao,ts>\n");CHKERRQ(ierr);
76     ierr = (*PetscHelpPrintf)(comm," -info_exclude: <null,vec,mat,pc,ksp,snes,tao,ts>\n");CHKERRQ(ierr);
77     ierr = (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");CHKERRQ(ierr);
78 #endif
79   }
80   PetscFunctionReturn(0);
81 }
82 
83 /*
84       PetscInitializeNoPointers - Calls PetscInitialize() from C/C++ without the pointers to argc and args
85 
86    Collective
87 
88    Level: advanced
89 
90     Notes:
91     this is called only by the PETSc MATLAB and Julia interface. Even though it might start MPI it sets the flag to
92      indicate that it did NOT start MPI so that the PetscFinalize() does not end MPI, thus allowing PetscInitialize() to
93      be called multiple times from MATLAB and Julia without the problem of trying to initialize MPI more than once.
94 
95      Turns off PETSc signal handling because that can interact with MATLAB's signal handling causing random crashes.
96 
97 .seealso: PetscInitialize(), PetscInitializeFortran(), PetscInitializeNoArguments()
98 */
99 PetscErrorCode  PetscInitializeNoPointers(int argc,char **args,const char *filename,const char *help)
100 {
101   PetscErrorCode ierr;
102   int            myargc   = argc;
103   char           **myargs = args;
104 
105   PetscFunctionBegin;
106   ierr = PetscInitialize(&myargc,&myargs,filename,help);CHKERRQ(ierr);
107   ierr = PetscPopSignalHandler();CHKERRQ(ierr);
108   PetscBeganMPI = PETSC_FALSE;
109   PetscFunctionReturn(ierr);
110 }
111 
112 /*
113       Used by MATLAB and Julia interface to get communicator
114 */
115 PetscErrorCode  PetscGetPETSC_COMM_SELF(MPI_Comm *comm)
116 {
117   PetscFunctionBegin;
118   *comm = PETSC_COMM_SELF;
119   PetscFunctionReturn(0);
120 }
121 
122 /*@C
123       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
124         the command line arguments.
125 
126    Collective
127 
128    Level: advanced
129 
130 .seealso: PetscInitialize(), PetscInitializeFortran()
131 @*/
132 PetscErrorCode  PetscInitializeNoArguments(void)
133 {
134   PetscErrorCode ierr;
135   int            argc   = 0;
136   char           **args = 0;
137 
138   PetscFunctionBegin;
139   ierr = PetscInitialize(&argc,&args,NULL,NULL);
140   PetscFunctionReturn(ierr);
141 }
142 
143 /*@
144       PetscInitialized - Determine whether PETSc is initialized.
145 
146    Level: beginner
147 
148 .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
149 @*/
150 PetscErrorCode PetscInitialized(PetscBool  *isInitialized)
151 {
152   *isInitialized = PetscInitializeCalled;
153   return 0;
154 }
155 
156 /*@
157       PetscFinalized - Determine whether PetscFinalize() has been called yet
158 
159    Level: developer
160 
161 .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
162 @*/
163 PetscErrorCode  PetscFinalized(PetscBool  *isFinalized)
164 {
165   *isFinalized = PetscFinalizeCalled;
166   return 0;
167 }
168 
169 extern PetscErrorCode PetscOptionsCheckInitial_Private(void);
170 
171 /*
172        This function is the MPI reduction operation used to compute the sum of the
173    first half of the datatype and the max of the second half.
174 */
175 MPI_Op MPIU_MAXSUM_OP = 0;
176 
177 PETSC_INTERN void MPIAPI MPIU_MaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
178 {
179   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;
180 
181   PetscFunctionBegin;
182   if (*datatype != MPIU_2INT) {
183     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
184     MPI_Abort(MPI_COMM_WORLD,1);
185   }
186 
187   for (i=0; i<count; i++) {
188     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
189     xout[2*i+1] += xin[2*i+1];
190   }
191   PetscFunctionReturnVoid();
192 }
193 
194 /*
195     Returns the max of the first entry owned by this processor and the
196 sum of the second entry.
197 
198     The reason sizes[2*i] contains lengths sizes[2*i+1] contains flag of 1 if length is nonzero
199 is so that the MPIU_MAXSUM_OP() can set TWO values, if we passed in only sizes[i] with lengths
200 there would be no place to store the both needed results.
201 */
202 PetscErrorCode  PetscMaxSum(MPI_Comm comm,const PetscInt sizes[],PetscInt *max,PetscInt *sum)
203 {
204   PetscErrorCode ierr;
205 
206   PetscFunctionBegin;
207 #if defined(PETSC_HAVE_MPI_REDUCE_SCATTER_BLOCK)
208   {
209     struct {PetscInt max,sum;} work;
210     ierr = MPI_Reduce_scatter_block((void*)sizes,&work,1,MPIU_2INT,MPIU_MAXSUM_OP,comm);CHKERRQ(ierr);
211     *max = work.max;
212     *sum = work.sum;
213   }
214 #else
215   {
216     PetscMPIInt    size,rank;
217     struct {PetscInt max,sum;} *work;
218     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
219     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
220     ierr = PetscMalloc1(size,&work);CHKERRQ(ierr);
221     ierr = MPIU_Allreduce((void*)sizes,work,size,MPIU_2INT,MPIU_MAXSUM_OP,comm);CHKERRQ(ierr);
222     *max = work[rank].max;
223     *sum = work[rank].sum;
224     ierr = PetscFree(work);CHKERRQ(ierr);
225   }
226 #endif
227   PetscFunctionReturn(0);
228 }
229 
230 /* ----------------------------------------------------------------------------*/
231 
232 #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
233 MPI_Op MPIU_SUM = 0;
234 
235 PETSC_EXTERN void PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
236 {
237   PetscInt i,count = *cnt;
238 
239   PetscFunctionBegin;
240   if (*datatype == MPIU_REAL) {
241     PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
242     for (i=0; i<count; i++) xout[i] += xin[i];
243   }
244 #if defined(PETSC_HAVE_COMPLEX)
245   else if (*datatype == MPIU_COMPLEX) {
246     PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
247     for (i=0; i<count; i++) xout[i] += xin[i];
248   }
249 #endif
250   else {
251     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types");
252     MPI_Abort(MPI_COMM_WORLD,1);
253   }
254   PetscFunctionReturnVoid();
255 }
256 #endif
257 
258 #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
259 MPI_Op MPIU_MAX = 0;
260 MPI_Op MPIU_MIN = 0;
261 
262 PETSC_EXTERN void PetscMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
263 {
264   PetscInt i,count = *cnt;
265 
266   PetscFunctionBegin;
267   if (*datatype == MPIU_REAL) {
268     PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
269     for (i=0; i<count; i++) xout[i] = PetscMax(xout[i],xin[i]);
270   }
271 #if defined(PETSC_HAVE_COMPLEX)
272   else if (*datatype == MPIU_COMPLEX) {
273     PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
274     for (i=0; i<count; i++) {
275       xout[i] = PetscRealPartComplex(xout[i])<PetscRealPartComplex(xin[i]) ? xin[i] : xout[i];
276     }
277   }
278 #endif
279   else {
280     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types");
281     MPI_Abort(MPI_COMM_WORLD,1);
282   }
283   PetscFunctionReturnVoid();
284 }
285 
286 PETSC_EXTERN void PetscMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
287 {
288   PetscInt    i,count = *cnt;
289 
290   PetscFunctionBegin;
291   if (*datatype == MPIU_REAL) {
292     PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
293     for (i=0; i<count; i++) xout[i] = PetscMin(xout[i],xin[i]);
294   }
295 #if defined(PETSC_HAVE_COMPLEX)
296   else if (*datatype == MPIU_COMPLEX) {
297     PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
298     for (i=0; i<count; i++) {
299       xout[i] = PetscRealPartComplex(xout[i])>PetscRealPartComplex(xin[i]) ? xin[i] : xout[i];
300     }
301   }
302 #endif
303   else {
304     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_SCALAR data (i.e. double or complex) types");
305     MPI_Abort(MPI_COMM_WORLD,1);
306   }
307   PetscFunctionReturnVoid();
308 }
309 #endif
310 
311 /*
312    Private routine to delete internal tag/name counter storage when a communicator is freed.
313 
314    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.
315 
316    Note: this is declared extern "C" because it is passed to MPI_Comm_create_keyval()
317 
318 */
319 PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelCounter(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
320 {
321   PetscErrorCode ierr;
322 
323   PetscFunctionBegin;
324   ierr = PetscInfo1(0,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);CHKERRMPI(ierr);
325   ierr = PetscFree(count_val);CHKERRMPI(ierr);
326   PetscFunctionReturn(MPI_SUCCESS);
327 }
328 
329 /*
330   This is invoked on the outer comm as a result of either PetscCommDestroy() (via MPI_Comm_delete_attr) or when the user
331   calls MPI_Comm_free().
332 
333   This is the only entry point for breaking the links between inner and outer comms.
334 
335   This is called by MPI, not by users. This is called when MPI_Comm_free() is called on the communicator.
336 
337   Note: this is declared extern "C" because it is passed to MPI_Comm_create_keyval()
338 
339 */
340 PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelComm_Outer(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
341 {
342   PetscErrorCode                    ierr;
343   PetscMPIInt                       flg;
344   union {MPI_Comm comm; void *ptr;} icomm,ocomm;
345 
346   PetscFunctionBegin;
347   if (keyval != Petsc_InnerComm_keyval) SETERRMPI(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Unexpected keyval");
348   icomm.ptr = attr_val;
349 
350   ierr = MPI_Comm_get_attr(icomm.comm,Petsc_OuterComm_keyval,&ocomm,&flg);CHKERRMPI(ierr);
351   if (!flg) SETERRMPI(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected reference to outer comm");
352   if (ocomm.comm != comm) SETERRMPI(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm has reference to non-matching outer comm");
353   ierr = MPI_Comm_delete_attr(icomm.comm,Petsc_OuterComm_keyval);CHKERRMPI(ierr);
354   ierr = PetscInfo1(0,"User MPI_Comm %ld is being freed after removing reference from inner PETSc comm to this outer comm\n",(long)comm);CHKERRMPI(ierr);
355   PetscFunctionReturn(MPI_SUCCESS);
356 }
357 
358 /*
359  * This is invoked on the inner comm when Petsc_DelComm_Outer calls MPI_Comm_delete_attr.  It should not be reached any other way.
360  */
361 PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelComm_Inner(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
362 {
363   PetscErrorCode ierr;
364 
365   PetscFunctionBegin;
366   ierr = PetscInfo1(0,"Removing reference to PETSc communicator embedded in a user MPI_Comm %ld\n",(long)comm);CHKERRMPI(ierr);
367   PetscFunctionReturn(MPI_SUCCESS);
368 }
369 
370 PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelShared(MPI_Comm,PetscMPIInt,void *,void *);
371 
372 #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
373 #if !defined(PETSC_WORDS_BIGENDIAN)
374 PETSC_EXTERN PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype,MPI_Aint*,void*);
375 PETSC_EXTERN PetscMPIInt PetscDataRep_read_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
376 PETSC_EXTERN PetscMPIInt PetscDataRep_write_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
377 #endif
378 #endif
379 
380 PetscMPIInt PETSC_MPI_ERROR_CLASS,PETSC_MPI_ERROR_CODE;
381 
382 int  PetscGlobalArgc   = 0;
383 char **PetscGlobalArgs = 0;
384 PetscSegBuffer PetscCitationsList;
385 
386 PetscErrorCode PetscCitationsInitialize(void)
387 {
388   PetscErrorCode ierr;
389 
390   PetscFunctionBegin;
391   ierr = PetscSegBufferCreate(1,10000,&PetscCitationsList);CHKERRQ(ierr);
392   ierr = PetscCitationsRegister("@TechReport{petsc-user-ref,\n  Author = {Satish Balay and Shrirang Abhyankar and Mark F. Adams and Jed Brown and Peter Brune\n            and Kris Buschelman and Lisandro Dalcin and Victor Eijkhout and William D. Gropp\n            and Dinesh Kaushik and Matthew G. Knepley and Dave A. May and Lois Curfman McInnes\n            and Richard Tran Mills and Todd Munson and Karl Rupp and Patrick Sanan\n            and Barry F. Smith and Stefano Zampini and Hong Zhang and Hong Zhang},\n  Title = {{PETS}c Users Manual},\n  Number = {ANL-95/11 - Revision 3.9},\n  Institution = {Argonne National Laboratory},\n  Year = {2018}\n}\n",NULL);CHKERRQ(ierr);
393   ierr = PetscCitationsRegister("@InProceedings{petsc-efficient,\n  Author = {Satish Balay and William D. Gropp and Lois Curfman McInnes and Barry F. Smith},\n  Title = {Efficient Management of Parallelism in Object Oriented Numerical Software Libraries},\n  Booktitle = {Modern Software Tools in Scientific Computing},\n  Editor = {E. Arge and A. M. Bruaset and H. P. Langtangen},\n  Pages = {163--202},\n  Publisher = {Birkh{\\\"{a}}user Press},\n  Year = {1997}\n}\n",NULL);CHKERRQ(ierr);
394   PetscFunctionReturn(0);
395 }
396 
397 /*@C
398    PetscGetArgs - Allows you to access the raw command line arguments anywhere
399      after PetscInitialize() is called but before PetscFinalize().
400 
401    Not Collective
402 
403    Output Parameters:
404 +  argc - count of number of command line arguments
405 -  args - the command line arguments
406 
407    Level: intermediate
408 
409    Notes:
410       This is usually used to pass the command line arguments into other libraries
411    that are called internally deep in PETSc or the application.
412 
413       The first argument contains the program name as is normal for C arguments.
414 
415    Concepts: command line arguments
416 
417 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()
418 
419 @*/
420 PetscErrorCode  PetscGetArgs(int *argc,char ***args)
421 {
422   PetscFunctionBegin;
423   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
424   *argc = PetscGlobalArgc;
425   *args = PetscGlobalArgs;
426   PetscFunctionReturn(0);
427 }
428 
429 /*@C
430    PetscGetArguments - Allows you to access the  command line arguments anywhere
431      after PetscInitialize() is called but before PetscFinalize().
432 
433    Not Collective
434 
435    Output Parameters:
436 .  args - the command line arguments
437 
438    Level: intermediate
439 
440    Notes:
441       This does NOT start with the program name and IS null terminated (final arg is void)
442 
443    Concepts: command line arguments
444 
445 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()
446 
447 @*/
448 PetscErrorCode  PetscGetArguments(char ***args)
449 {
450   PetscInt       i,argc = PetscGlobalArgc;
451   PetscErrorCode ierr;
452 
453   PetscFunctionBegin;
454   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
455   if (!argc) {*args = 0; PetscFunctionReturn(0);}
456   ierr = PetscMalloc1(argc,args);CHKERRQ(ierr);
457   for (i=0; i<argc-1; i++) {
458     ierr = PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);CHKERRQ(ierr);
459   }
460   (*args)[argc-1] = 0;
461   PetscFunctionReturn(0);
462 }
463 
464 /*@C
465    PetscFreeArguments - Frees the memory obtained with PetscGetArguments()
466 
467    Not Collective
468 
469    Output Parameters:
470 .  args - the command line arguments
471 
472    Level: intermediate
473 
474    Concepts: command line arguments
475 
476 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()
477 
478 @*/
479 PetscErrorCode  PetscFreeArguments(char **args)
480 {
481   PetscInt       i = 0;
482   PetscErrorCode ierr;
483 
484   PetscFunctionBegin;
485   if (!args) PetscFunctionReturn(0);
486   while (args[i]) {
487     ierr = PetscFree(args[i]);CHKERRQ(ierr);
488     i++;
489   }
490   ierr = PetscFree(args);CHKERRQ(ierr);
491   PetscFunctionReturn(0);
492 }
493 
494 #if defined(PETSC_HAVE_SAWS)
495 #include <petscconfiginfo.h>
496 
497 PetscErrorCode  PetscInitializeSAWs(const char help[])
498 {
499   if (!PetscGlobalRank) {
500     char           cert[PETSC_MAX_PATH_LEN],root[PETSC_MAX_PATH_LEN],*intro,programname[64],*appline,*options,version[64];
501     int            port;
502     PetscBool      flg,rootlocal = PETSC_FALSE,flg2,selectport = PETSC_FALSE;
503     size_t         applinelen,introlen;
504     PetscErrorCode ierr;
505     char           sawsurl[256];
506 
507     ierr = PetscOptionsHasName(NULL,NULL,"-saws_log",&flg);CHKERRQ(ierr);
508     if (flg) {
509       char  sawslog[PETSC_MAX_PATH_LEN];
510 
511       ierr = PetscOptionsGetString(NULL,NULL,"-saws_log",sawslog,PETSC_MAX_PATH_LEN,NULL);CHKERRQ(ierr);
512       if (sawslog[0]) {
513         PetscStackCallSAWs(SAWs_Set_Use_Logfile,(sawslog));
514       } else {
515         PetscStackCallSAWs(SAWs_Set_Use_Logfile,(NULL));
516       }
517     }
518     ierr = PetscOptionsGetString(NULL,NULL,"-saws_https",cert,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
519     if (flg) {
520       PetscStackCallSAWs(SAWs_Set_Use_HTTPS,(cert));
521     }
522     ierr = PetscOptionsGetBool(NULL,NULL,"-saws_port_auto_select",&selectport,NULL);CHKERRQ(ierr);
523     if (selectport) {
524         PetscStackCallSAWs(SAWs_Get_Available_Port,(&port));
525         PetscStackCallSAWs(SAWs_Set_Port,(port));
526     } else {
527       ierr = PetscOptionsGetInt(NULL,NULL,"-saws_port",&port,&flg);CHKERRQ(ierr);
528       if (flg) {
529         PetscStackCallSAWs(SAWs_Set_Port,(port));
530       }
531     }
532     ierr = PetscOptionsGetString(NULL,NULL,"-saws_root",root,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
533     if (flg) {
534       PetscStackCallSAWs(SAWs_Set_Document_Root,(root));CHKERRQ(ierr);
535       ierr = PetscStrcmp(root,".",&rootlocal);CHKERRQ(ierr);
536     } else {
537       ierr = PetscOptionsHasName(NULL,NULL,"-saws_options",&flg);CHKERRQ(ierr);
538       if (flg) {
539         ierr = PetscStrreplace(PETSC_COMM_WORLD,"${PETSC_DIR}/share/petsc/saws",root,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
540         PetscStackCallSAWs(SAWs_Set_Document_Root,(root));CHKERRQ(ierr);
541       }
542     }
543     ierr = PetscOptionsHasName(NULL,NULL,"-saws_local",&flg2);CHKERRQ(ierr);
544     if (flg2) {
545       char jsdir[PETSC_MAX_PATH_LEN];
546       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"-saws_local option requires -saws_root option");
547       ierr = PetscSNPrintf(jsdir,PETSC_MAX_PATH_LEN,"%s/js",root);CHKERRQ(ierr);
548       ierr = PetscTestDirectory(jsdir,'r',&flg);CHKERRQ(ierr);
549       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"-saws_local option requires js directory in root directory");
550       PetscStackCallSAWs(SAWs_Push_Local_Header,());CHKERRQ(ierr);
551     }
552     ierr = PetscGetProgramName(programname,64);CHKERRQ(ierr);
553     ierr = PetscStrlen(help,&applinelen);CHKERRQ(ierr);
554     introlen   = 4096 + applinelen;
555     applinelen += 1024;
556     ierr = PetscMalloc(applinelen,&appline);CHKERRQ(ierr);
557     ierr = PetscMalloc(introlen,&intro);CHKERRQ(ierr);
558 
559     if (rootlocal) {
560       ierr = PetscSNPrintf(appline,applinelen,"%s.c.html",programname);CHKERRQ(ierr);
561       ierr = PetscTestFile(appline,'r',&rootlocal);CHKERRQ(ierr);
562     }
563     ierr = PetscOptionsGetAll(NULL,&options);CHKERRQ(ierr);
564     if (rootlocal && help) {
565       ierr = 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);
566     } else if (help) {
567       ierr = PetscSNPrintf(appline,applinelen,"<center>Running %s %s</center><br><center><pre>%s</pre></center><br>",programname,options,help);
568     } else {
569       ierr = PetscSNPrintf(appline,applinelen,"<center> Running %s %s</center><br>\n",programname,options);
570     }
571     ierr = PetscFree(options);CHKERRQ(ierr);
572     ierr = PetscGetVersion(version,sizeof(version));CHKERRQ(ierr);
573     ierr = PetscSNPrintf(intro,introlen,"<body>\n"
574                                     "<center><h2> <a href=\"http://www.mcs.anl.gov/petsc\">PETSc</a> Application Web server powered by <a href=\"https://bitbucket.org/saws/saws\">SAWs</a> </h2></center>\n"
575                                     "<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"
576                                     "%s",version,petscconfigureoptions,appline);
577     PetscStackCallSAWs(SAWs_Push_Body,("index.html",0,intro));
578     ierr = PetscFree(intro);CHKERRQ(ierr);
579     ierr = PetscFree(appline);CHKERRQ(ierr);
580     if (selectport) {
581       PetscBool silent;
582 
583       ierr = SAWs_Initialize();
584       /* another process may have grabbed the port so keep trying */
585       while (ierr) {
586         PetscStackCallSAWs(SAWs_Get_Available_Port,(&port));
587         PetscStackCallSAWs(SAWs_Set_Port,(port));
588         ierr = SAWs_Initialize();
589       }
590 
591       ierr = PetscOptionsGetBool(NULL,NULL,"-saws_port_auto_select_silent",&silent,NULL);CHKERRQ(ierr);
592       if (!silent) {
593         PetscStackCallSAWs(SAWs_Get_FullURL,(sizeof(sawsurl),sawsurl));
594         ierr = PetscPrintf(PETSC_COMM_WORLD,"Point your browser to %s for SAWs\n",sawsurl);CHKERRQ(ierr);
595       }
596     } else {
597       PetscStackCallSAWs(SAWs_Initialize,());
598     }
599     ierr = PetscCitationsRegister("@TechReport{ saws,\n"
600                                   "  Author = {Matt Otten and Jed Brown and Barry Smith},\n"
601                                   "  Title  = {Scientific Application Web Server (SAWs) Users Manual},\n"
602                                   "  Institution = {Argonne National Laboratory},\n"
603                                   "  Year   = 2013\n}\n",NULL);CHKERRQ(ierr);
604   }
605   PetscFunctionReturn(0);
606 }
607 #endif
608 
609 /*@C
610    PetscInitialize - Initializes the PETSc database and MPI.
611    PetscInitialize() calls MPI_Init() if that has yet to be called,
612    so this routine should always be called near the beginning of
613    your program -- usually the very first line!
614 
615    Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set
616 
617    Input Parameters:
618 +  argc - count of number of command line arguments
619 .  args - the command line arguments
620 .  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use NULL to not check for
621           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
622 -  help - [optional] Help message to print, use NULL for no message
623 
624    If you wish PETSc code to run ONLY on a subcommunicator of MPI_COMM_WORLD, create that
625    communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize(). Thus if you are running a
626    four process job and two processes will run PETSc and have PetscInitialize() and PetscFinalize() and two process will not,
627    then do this. If ALL processes in the job are using PetscInitialize() and PetscFinalize() then you don't need to do this, even
628    if different subcommunicators of the job are doing different things with PETSc.
629 
630    Options Database Keys:
631 +  -help [intro] - prints help method for each option; if intro is given the program stops after printing the introductory help message
632 .  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
633 .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
634 .  -on_error_emacs <machinename> causes emacsclient to jump to error file
635 .  -on_error_abort calls abort() when error detected (no traceback)
636 .  -on_error_mpiabort calls MPI_abort() when error detected
637 .  -error_output_stderr prints error messages to stderr instead of the default stdout
638 .  -error_output_none does not print the error messages (but handles errors in the same way as if this was not called)
639 .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
640 .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
641 .  -stop_for_debugger - Print message on how to attach debugger manually to
642                         process and wait (-debugger_pause) seconds for attachment
643 .  -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries)
644 .  -malloc no - Indicates not to use error-checking malloc
645 .  -malloc_debug - check for memory corruption at EVERY malloc or free
646 .  -malloc_dump - prints a list of all unfreed memory at the end of the run
647 .  -malloc_test - like -malloc_dump -malloc_debug, but only active for debugging builds
648 .  -fp_trap - Stops on floating point exceptions (Note that on the
649               IBM RS6000 this slows code by at least a factor of 10.)
650 .  -no_signal_handler - Indicates not to trap error signals
651 .  -shared_tmp - indicates /tmp directory is shared by all processors
652 .  -not_shared_tmp - each processor has own /tmp
653 .  -tmp - alternative name of /tmp directory
654 .  -get_total_flops - returns total flops done by all processors
655 -  -memory_view - Print memory usage at end of run
656 
657    Options Database Keys for Profiling:
658    See Users-Manual: ch_profiling for details.
659 +  -info <optional filename> - Prints verbose information to the screen
660 .  -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages
661 .  -log_sync - Enable barrier synchronization for all events. This option is useful to debug imbalance within each event,
662         however it slows things down and gives a distorted view of the overall runtime.
663 .  -log_trace [filename] - Print traces of all PETSc calls to the screen (useful to determine where a program
664         hangs without running in the debugger).  See PetscLogTraceBegin().
665 .  -log_view [:filename:format] - Prints summary of flop and timing information to screen or file, see PetscLogView().
666 .  -log_summary [filename] - (Deprecated, use -log_view) Prints summary of flop and timing information to screen. If the filename is specified the
667         summary is written to the file.  See PetscLogView().
668 .  -log_exclude: <vec,mat,pc,ksp,snes> - excludes subset of object classes from logging
669 .  -log_all [filename] - Logs extensive profiling information  See PetscLogDump().
670 .  -log [filename] - Logs basic profiline information  See PetscLogDump().
671 .  -log_mpe [filename] - Creates a logfile viewable by the utility Jumpshot (in MPICH distribution)
672 -  -check_pointer_intensity 0,1,2 - if pointers are checked for validity (debug version only), using 0 will result in faster code
673 
674     Only one of -log_trace, -log_view, -log_view, -log_all, -log, or -log_mpe may be used at a time
675 
676    Options Database Keys for SAWs:
677 +  -saws_port <portnumber> - port number to publish SAWs data, default is 8080
678 .  -saws_port_auto_select - have SAWs select a new unique port number where it publishes the data, the URL is printed to the screen
679                             this is useful when you are running many jobs that utilize SAWs at the same time
680 .  -saws_log <filename> - save a log of all SAWs communication
681 .  -saws_https <certificate file> - have SAWs use HTTPS instead of HTTP
682 -  -saws_root <directory> - allow SAWs to have access to the given directory to search for requested resources and files
683 
684    Environmental Variables:
685 +   PETSC_TMP - alternative tmp directory
686 .   PETSC_SHARED_TMP - tmp is shared by all processes
687 .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
688 .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
689 -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to
690 
691 
692    Level: beginner
693 
694    Notes:
695    If for some reason you must call MPI_Init() separately, call
696    it before PetscInitialize().
697 
698    Fortran Version:
699    In Fortran this routine has the format
700 $       call PetscInitialize(file,ierr)
701 
702 +   ierr - error return code
703 -  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL_CHARACTER to not check for
704           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
705 
706    Important Fortran Note:
707    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
708    null character string; you CANNOT just use NULL as
709    in the C version. See Users-Manual: ch_fortran for details.
710 
711    If your main program is C but you call Fortran code that also uses PETSc you need to call PetscInitializeFortran() soon after
712    calling PetscInitialize().
713 
714    Concepts: initializing PETSc
715 
716 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments()
717 
718 @*/
719 PetscErrorCode  PetscInitialize(int *argc,char ***args,const char file[],const char help[])
720 {
721   PetscErrorCode ierr;
722   PetscMPIInt    flag, size;
723   PetscBool      flg = PETSC_TRUE;
724   char           hostname[256];
725 #if defined(PETSC_HAVE_HWLOC)
726   PetscViewer    viewer;
727 #endif
728 
729   PetscFunctionBegin;
730   if (PetscInitializeCalled) PetscFunctionReturn(0);
731   /*
732       The checking over compatible runtime libraries is complicated by the MPI ABI initiative
733       https://wiki.mpich.org/mpich/index.php/ABI_Compatibility_Initiative which started with
734         MPICH v3.1 (Released Feburary 2014)
735         IBM MPI v2.1 (December 2014)
736         Intel® MPI Library v5.0 (2014)
737         Cray MPT v7.0.0 (June 2014)
738       As of July 31, 2017 the ABI number still appears to be 12, that is all of the versions
739       listed above and since that time are compatible.
740 
741       Unfortunately the MPI ABI initiative has not defined a way to determine the ABI number
742       at compile time or runtime. Thus we will need to systematically track the allowed versions
743       and how they are represented in the mpi.h and MPI_Get_library_version() output in order
744       to perform the checking.
745 
746       Currently we only check for pre MPI ABI versions (and packages that do not follow the MPI ABI).
747 
748       Questions:
749 
750         Should the checks for ABI incompatibility be only on the major version number below?
751         Presumably the output to stderr will be removed before a release.
752   */
753 
754 #if defined(PETSC_HAVE_MPI_GET_LIBRARY_VERSION)
755   {
756     char        mpilibraryversion[MPI_MAX_LIBRARY_VERSION_STRING];
757     PetscMPIInt mpilibraryversionlength;
758     ierr = MPI_Get_library_version(mpilibraryversion,&mpilibraryversionlength);if (ierr) return ierr;
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       flg = PETSC_FALSE;
765       ierr = PetscStrstr(mpilibraryversion,"MPICH Version:",&ver);if (ierr) return ierr;
766       if (ver) {
767         ierr = PetscStrchr(ver,'\n',&lf);if (ierr) return ierr;
768         if (lf) {
769           *lf = 0;
770           ierr = PetscStrendswith(ver,MPICH_VERSION,&flg);if (ierr) return ierr;
771         }
772       }
773       if (!flg) {
774         fprintf(stderr,"PETSc Error --- MPICH library version \n%s does not match what PETSc was compiled with %s, aborting\n",mpilibraryversion,MPICH_VERSION);
775         return PETSC_ERR_MPI_LIB_INCOMP;
776       }
777     }
778 #endif
779     /* check for OpenMPI version, it is not part of the MPI ABI initiative (is it part of another initiative that needs to be handled?) */
780 #elif defined(OMPI_MAJOR_VERSION)
781     {
782       char *ver,bs[32],*bsf;
783       flg = PETSC_FALSE;
784       ierr = PetscStrstr(mpilibraryversion,"Open MPI",&ver);if (ierr) return ierr;
785       if (ver) {
786         PetscSNPrintf(bs,32,"v%d.%d",OMPI_MAJOR_VERSION,OMPI_MINOR_VERSION);
787         ierr = PetscStrstr(ver,bs,&bsf);if (ierr) return ierr;
788         if (bsf) flg = PETSC_TRUE;
789       }
790       if (!flg) {
791         fprintf(stderr,"PETSc Error --- Open MPI library version \n%s does not match what PETSc was compiled with %d.%d, aborting\n",mpilibraryversion,OMPI_MAJOR_VERSION,OMPI_MINOR_VERSION);
792         return PETSC_ERR_MPI_LIB_INCOMP;
793       }
794     }
795 #endif
796   }
797 #endif
798 
799 
800   /* these must be initialized in a routine, not as a constant declaration*/
801   PETSC_STDOUT = stdout;
802   PETSC_STDERR = stderr;
803 
804   /* on Windows - set printf to default to printing 2 digit exponents */
805 #if defined(PETSC_HAVE__SET_OUTPUT_FORMAT)
806   _set_output_format(_TWO_DIGIT_EXPONENT);
807 #endif
808 
809   ierr = PetscOptionsCreateDefault();CHKERRQ(ierr);
810 
811   /*
812      We initialize the program name here (before MPI_Init()) because MPICH has a bug in
813      it that it sets args[0] on all processors to be args[0] on the first processor.
814   */
815   if (argc && *argc) {
816     ierr = PetscSetProgramName(**args);CHKERRQ(ierr);
817   } else {
818     ierr = PetscSetProgramName("Unknown Name");CHKERRQ(ierr);
819   }
820 
821   ierr = MPI_Initialized(&flag);CHKERRQ(ierr);
822   if (!flag) {
823     if (PETSC_COMM_WORLD != MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"You cannot set PETSC_COMM_WORLD if you have not initialized MPI first");
824 #if defined(PETSC_HAVE_MPI_INIT_THREAD)
825     {
826       PetscMPIInt provided;
827       ierr = MPI_Init_thread(argc,args,MPI_THREAD_FUNNELED,&provided);CHKERRQ(ierr);
828     }
829 #else
830     ierr = MPI_Init(argc,args);CHKERRQ(ierr);
831 #endif
832     PetscBeganMPI = PETSC_TRUE;
833   }
834   if (argc && args) {
835     PetscGlobalArgc = *argc;
836     PetscGlobalArgs = *args;
837   }
838   PetscFinalizeCalled = PETSC_FALSE;
839   ierr = PetscSpinlockCreate(&PetscViewerASCIISpinLockOpen);CHKERRQ(ierr);
840   ierr = PetscSpinlockCreate(&PetscViewerASCIISpinLockStdout);CHKERRQ(ierr);
841   ierr = PetscSpinlockCreate(&PetscViewerASCIISpinLockStderr);CHKERRQ(ierr);
842   ierr = PetscSpinlockCreate(&PetscCommSpinLock);CHKERRQ(ierr);
843 
844   if (PETSC_COMM_WORLD == MPI_COMM_NULL) PETSC_COMM_WORLD = MPI_COMM_WORLD;
845   ierr = MPI_Comm_set_errhandler(PETSC_COMM_WORLD,MPI_ERRORS_RETURN);CHKERRQ(ierr);
846 
847   ierr = MPI_Add_error_class(&PETSC_MPI_ERROR_CLASS);CHKERRQ(ierr);
848   ierr = MPI_Add_error_code(PETSC_MPI_ERROR_CLASS,&PETSC_MPI_ERROR_CODE);CHKERRQ(ierr);
849 
850   /* Done after init due to a bug in MPICH-GM? */
851   ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr);
852 
853   ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);CHKERRQ(ierr);
854   ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);CHKERRQ(ierr);
855 
856   MPIU_BOOL = MPI_INT;
857   MPIU_ENUM = MPI_INT;
858 
859   /*
860      Initialized the global complex variable; this is because with
861      shared libraries the constructors for global variables
862      are not called; at least on IRIX.
863   */
864 #if defined(PETSC_HAVE_COMPLEX)
865   {
866 #if defined(PETSC_CLANGUAGE_CXX) && !defined(PETSC_USE_REAL___FLOAT128)
867     PetscComplex ic(0.0,1.0);
868     PETSC_i = ic;
869 #else
870     PETSC_i = _Complex_I;
871 #endif
872   }
873 
874 #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
875   ierr = MPI_Type_contiguous(2,MPI_DOUBLE,&MPIU_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
876   ierr = MPI_Type_commit(&MPIU_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
877   ierr = MPI_Type_contiguous(2,MPI_FLOAT,&MPIU_C_COMPLEX);CHKERRQ(ierr);
878   ierr = MPI_Type_commit(&MPIU_C_COMPLEX);CHKERRQ(ierr);
879 #endif
880 #endif /* PETSC_HAVE_COMPLEX */
881 
882   /*
883      Create the PETSc MPI reduction operator that sums of the first
884      half of the entries and maxes the second half.
885   */
886   ierr = MPI_Op_create(MPIU_MaxSum_Local,1,&MPIU_MAXSUM_OP);CHKERRQ(ierr);
887 
888 #if defined(PETSC_USE_REAL___FLOAT128)
889   ierr = MPI_Type_contiguous(2,MPI_DOUBLE,&MPIU___FLOAT128);CHKERRQ(ierr);
890   ierr = MPI_Type_commit(&MPIU___FLOAT128);CHKERRQ(ierr);
891 #if defined(PETSC_HAVE_COMPLEX)
892   ierr = MPI_Type_contiguous(4,MPI_DOUBLE,&MPIU___COMPLEX128);CHKERRQ(ierr);
893   ierr = MPI_Type_commit(&MPIU___COMPLEX128);CHKERRQ(ierr);
894 #endif
895   ierr = MPI_Op_create(PetscMax_Local,1,&MPIU_MAX);CHKERRQ(ierr);
896   ierr = MPI_Op_create(PetscMin_Local,1,&MPIU_MIN);CHKERRQ(ierr);
897 #elif defined(PETSC_USE_REAL___FP16)
898   ierr = MPI_Type_contiguous(2,MPI_CHAR,&MPIU___FP16);CHKERRQ(ierr);
899   ierr = MPI_Type_commit(&MPIU___FP16);CHKERRQ(ierr);
900   ierr = MPI_Op_create(PetscMax_Local,1,&MPIU_MAX);CHKERRQ(ierr);
901   ierr = MPI_Op_create(PetscMin_Local,1,&MPIU_MIN);CHKERRQ(ierr);
902 #endif
903 
904 #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
905   ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);CHKERRQ(ierr);
906 #endif
907 
908   ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);CHKERRQ(ierr);
909   ierr = MPI_Type_commit(&MPIU_2SCALAR);CHKERRQ(ierr);
910 
911 #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
912   ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);CHKERRQ(ierr);
913   ierr = MPI_Type_commit(&MPIU_2INT);CHKERRQ(ierr);
914 #endif
915 
916 
917   /*
918      Attributes to be set on PETSc communicators
919   */
920   ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_DelCounter,&Petsc_Counter_keyval,(void*)0);CHKERRQ(ierr);
921   ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_DelComm_Outer,&Petsc_InnerComm_keyval,(void*)0);CHKERRQ(ierr);
922   ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_DelComm_Inner,&Petsc_OuterComm_keyval,(void*)0);CHKERRQ(ierr);
923   ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_DelShared,&Petsc_Shared_keyval,(void*)0);CHKERRQ(ierr);
924 
925   /*
926      Build the options database
927   */
928   ierr = PetscOptionsInsert(NULL,argc,args,file);CHKERRQ(ierr);
929 
930   /* call a second time so it can look in the options database */
931   ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr);
932 
933   /*
934      Print main application help message
935   */
936   ierr = PetscOptionsHasName(NULL,NULL,"-help",&flg);CHKERRQ(ierr);
937   if (help && flg) {
938     ierr = PetscPrintf(PETSC_COMM_WORLD,help);CHKERRQ(ierr);
939   }
940   ierr = PetscOptionsCheckInitial_Private();CHKERRQ(ierr);
941 
942   ierr = PetscCitationsInitialize();CHKERRQ(ierr);
943 
944 #if defined(PETSC_HAVE_SAWS)
945   ierr = PetscInitializeSAWs(help);CHKERRQ(ierr);
946 #endif
947 
948   /* Creates the logging data structures; this is enabled even if logging is not turned on */
949 #if defined(PETSC_USE_LOG)
950   ierr = PetscLogInitialize();CHKERRQ(ierr);
951 #endif
952 
953   /*
954      Load the dynamic libraries (on machines that support them), this registers all
955      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
956   */
957   ierr = PetscInitialize_DynamicLibraries();CHKERRQ(ierr);
958 
959   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
960   ierr = PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);CHKERRQ(ierr);
961   ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr);
962   ierr = PetscInfo1(0,"Running on machine: %s\n",hostname);CHKERRQ(ierr);
963 
964   ierr = PetscOptionsCheckInitial_Components();CHKERRQ(ierr);
965   /* Check the options database for options related to the options database itself */
966   ierr = PetscOptionsSetFromOptions(NULL);CHKERRQ(ierr);
967 
968 #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
969   /*
970       Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI
971 
972       Currently not used because it is not supported by MPICH.
973   */
974 #if !defined(PETSC_WORDS_BIGENDIAN)
975   ierr = MPI_Register_datarep((char*)"petsc",PetscDataRep_read_conv_fn,PetscDataRep_write_conv_fn,PetscDataRep_extent_fn,NULL);CHKERRQ(ierr);
976 #endif
977 #endif
978 
979   ierr = PetscOptionsHasName(NULL,NULL,"-python",&flg);CHKERRQ(ierr);
980   if (flg) {
981     PetscInitializeCalled = PETSC_TRUE;
982     ierr = PetscPythonInitialize(NULL,NULL);CHKERRQ(ierr);
983   }
984 
985   /*
986       Setup building of stack frames for all function calls
987   */
988 #if defined(PETSC_USE_DEBUG) && !defined(PETSC_HAVE_THREADSAFETY)
989   ierr = PetscStackCreate();CHKERRQ(ierr);
990 #endif
991 
992 #if defined(PETSC_SERIALIZE_FUNCTIONS)
993   ierr = PetscFPTCreate(10000);CHKERRQ(ierr);
994 #endif
995 
996 #if defined(PETSC_HAVE_HWLOC)
997   ierr   = PetscOptionsGetViewer(PETSC_COMM_WORLD,NULL,"-process_view",&viewer,NULL,&flg);CHKERRQ(ierr);
998   if (flg) {
999     ierr = PetscProcessPlacementView(viewer);CHKERRQ(ierr);
1000   }
1001 #endif
1002 
1003   /*
1004       Once we are completedly initialized then we can set this variables
1005   */
1006   PetscInitializeCalled = PETSC_TRUE;
1007   PetscFunctionReturn(0);
1008 }
1009 
1010 #if defined(PETSC_USE_LOG)
1011 extern PetscObject *PetscObjects;
1012 extern PetscInt    PetscObjectsCounts, PetscObjectsMaxCounts;
1013 extern PetscBool   PetscObjectsLog;
1014 #endif
1015 
1016 /*
1017     Frees all the MPI types and operations that PETSc may have created
1018 */
1019 PetscErrorCode  PetscFreeMPIResources(void)
1020 {
1021   PetscErrorCode ierr;
1022 
1023   PetscFunctionBegin;
1024 #if defined(PETSC_USE_REAL___FLOAT128)
1025   ierr = MPI_Type_free(&MPIU___FLOAT128);CHKERRQ(ierr);
1026 #if defined(PETSC_HAVE_COMPLEX)
1027   ierr = MPI_Type_free(&MPIU___COMPLEX128);CHKERRQ(ierr);
1028 #endif
1029   ierr = MPI_Op_free(&MPIU_MAX);CHKERRQ(ierr);
1030   ierr = MPI_Op_free(&MPIU_MIN);CHKERRQ(ierr);
1031 #elif defined(PETSC_USE_REAL___FP16)
1032   ierr = MPI_Type_free(&MPIU___FP16);CHKERRQ(ierr);
1033   ierr = MPI_Op_free(&MPIU_MAX);CHKERRQ(ierr);
1034   ierr = MPI_Op_free(&MPIU_MIN);CHKERRQ(ierr);
1035 #endif
1036 
1037 #if defined(PETSC_HAVE_COMPLEX)
1038 #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
1039   ierr = MPI_Type_free(&MPIU_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
1040   ierr = MPI_Type_free(&MPIU_C_COMPLEX);CHKERRQ(ierr);
1041 #endif
1042 #endif
1043 
1044 #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16)
1045   ierr = MPI_Op_free(&MPIU_SUM);CHKERRQ(ierr);
1046 #endif
1047 
1048   ierr = MPI_Type_free(&MPIU_2SCALAR);CHKERRQ(ierr);
1049 #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
1050   ierr = MPI_Type_free(&MPIU_2INT);CHKERRQ(ierr);
1051 #endif
1052   ierr = MPI_Op_free(&MPIU_MAXSUM_OP);CHKERRQ(ierr);
1053   PetscFunctionReturn(0);
1054 }
1055 
1056 /*@C
1057    PetscFinalize - Checks for options to be called at the conclusion
1058    of the program. MPI_Finalize() is called only if the user had not
1059    called MPI_Init() before calling PetscInitialize().
1060 
1061    Collective on PETSC_COMM_WORLD
1062 
1063    Options Database Keys:
1064 +  -options_table - Calls PetscOptionsView()
1065 .  -options_left - Prints unused options that remain in the database
1066 .  -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
1067 .  -mpidump - Calls PetscMPIDump()
1068 .  -malloc_dump - Calls PetscMallocDump()
1069 .  -malloc_info - Prints total memory usage
1070 -  -malloc_log - Prints summary of memory usage
1071 
1072    Level: beginner
1073 
1074    Note:
1075    See PetscInitialize() for more general runtime options.
1076 
1077 .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
1078 @*/
1079 PetscErrorCode  PetscFinalize(void)
1080 {
1081   PetscErrorCode ierr;
1082   PetscMPIInt    rank;
1083   PetscInt       nopt;
1084   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
1085   PetscBool      flg;
1086 #if defined(PETSC_USE_LOG)
1087   char           mname[PETSC_MAX_PATH_LEN];
1088 #endif
1089 
1090   if (!PetscInitializeCalled) {
1091     printf("PetscInitialize() must be called before PetscFinalize()\n");
1092     return(PETSC_ERR_ARG_WRONGSTATE);
1093   }
1094   PetscFunctionBegin;
1095   ierr = PetscInfo(NULL,"PetscFinalize() called\n");CHKERRQ(ierr);
1096 
1097   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
1098 
1099   ierr = PetscOptionsHasName(NULL,NULL,"-citations",&flg);CHKERRQ(ierr);
1100   if (flg) {
1101     char  *cits, filename[PETSC_MAX_PATH_LEN];
1102     FILE  *fd = PETSC_STDOUT;
1103 
1104     ierr = PetscOptionsGetString(NULL,NULL,"-citations",filename,PETSC_MAX_PATH_LEN,NULL);CHKERRQ(ierr);
1105     if (filename[0]) {
1106       ierr = PetscFOpen(PETSC_COMM_WORLD,filename,"w",&fd);CHKERRQ(ierr);
1107     }
1108     ierr = PetscSegBufferGet(PetscCitationsList,1,&cits);CHKERRQ(ierr);
1109     cits[0] = 0;
1110     ierr = PetscSegBufferExtractAlloc(PetscCitationsList,&cits);CHKERRQ(ierr);
1111     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"If you publish results based on this computation please cite the following:\n");CHKERRQ(ierr);
1112     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"===========================================================================\n");CHKERRQ(ierr);
1113     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"%s",cits);CHKERRQ(ierr);
1114     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"===========================================================================\n");CHKERRQ(ierr);
1115     ierr = PetscFClose(PETSC_COMM_WORLD,fd);CHKERRQ(ierr);
1116     ierr = PetscFree(cits);CHKERRQ(ierr);
1117   }
1118   ierr = PetscSegBufferDestroy(&PetscCitationsList);CHKERRQ(ierr);
1119 
1120 #if defined(PETSC_HAVE_SSL) && defined(PETSC_USE_SOCKET_VIEWER)
1121   /* TextBelt is run for testing purposes only, please do not use this feature often */
1122   {
1123     PetscInt nmax = 2;
1124     char     **buffs;
1125     ierr = PetscMalloc1(2,&buffs);CHKERRQ(ierr);
1126     ierr = PetscOptionsGetStringArray(NULL,NULL,"-textbelt",buffs,&nmax,&flg1);CHKERRQ(ierr);
1127     if (flg1) {
1128       if (!nmax) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"-textbelt requires either the phone number or number,\"message\"");
1129       if (nmax == 1) {
1130         ierr = PetscMalloc1(128,&buffs[1]);CHKERRQ(ierr);
1131         ierr = PetscGetProgramName(buffs[1],32);CHKERRQ(ierr);
1132         ierr = PetscStrcat(buffs[1]," has completed");CHKERRQ(ierr);
1133       }
1134       ierr = PetscTextBelt(PETSC_COMM_WORLD,buffs[0],buffs[1],NULL);CHKERRQ(ierr);
1135       ierr = PetscFree(buffs[0]);CHKERRQ(ierr);
1136       ierr = PetscFree(buffs[1]);CHKERRQ(ierr);
1137     }
1138     ierr = PetscFree(buffs);CHKERRQ(ierr);
1139   }
1140   {
1141     PetscInt nmax = 2;
1142     char     **buffs;
1143     ierr = PetscMalloc1(2,&buffs);CHKERRQ(ierr);
1144     ierr = PetscOptionsGetStringArray(NULL,NULL,"-tellmycell",buffs,&nmax,&flg1);CHKERRQ(ierr);
1145     if (flg1) {
1146       if (!nmax) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"-tellmycell requires either the phone number or number,\"message\"");
1147       if (nmax == 1) {
1148         ierr = PetscMalloc1(128,&buffs[1]);CHKERRQ(ierr);
1149         ierr = PetscGetProgramName(buffs[1],32);CHKERRQ(ierr);
1150         ierr = PetscStrcat(buffs[1]," has completed");CHKERRQ(ierr);
1151       }
1152       ierr = PetscTellMyCell(PETSC_COMM_WORLD,buffs[0],buffs[1],NULL);CHKERRQ(ierr);
1153       ierr = PetscFree(buffs[0]);CHKERRQ(ierr);
1154       ierr = PetscFree(buffs[1]);CHKERRQ(ierr);
1155     }
1156     ierr = PetscFree(buffs);CHKERRQ(ierr);
1157   }
1158 #endif
1159   /*
1160     It should be safe to cancel the options monitors, since we don't expect to be setting options
1161     here (at least that are worth monitoring).  Monitors ought to be released so that they release
1162     whatever memory was allocated there before -malloc_dump reports unfreed memory.
1163   */
1164   ierr = PetscOptionsMonitorCancel();CHKERRQ(ierr);
1165 
1166 #if defined(PETSC_SERIALIZE_FUNCTIONS)
1167   ierr = PetscFPTDestroy();CHKERRQ(ierr);
1168 #endif
1169 
1170 
1171 #if defined(PETSC_HAVE_SAWS)
1172   flg = PETSC_FALSE;
1173   ierr = PetscOptionsGetBool(NULL,NULL,"-saw_options",&flg,NULL);CHKERRQ(ierr);
1174   if (flg) {
1175     ierr = PetscOptionsSAWsDestroy();CHKERRQ(ierr);
1176   }
1177 #endif
1178 
1179 #if defined(PETSC_HAVE_X)
1180   flg1 = PETSC_FALSE;
1181   ierr = PetscOptionsGetBool(NULL,NULL,"-x_virtual",&flg1,NULL);CHKERRQ(ierr);
1182   if (flg1) {
1183     /*  this is a crude hack, but better than nothing */
1184     ierr = PetscPOpen(PETSC_COMM_WORLD,NULL,"pkill -9 Xvfb","r",NULL);CHKERRQ(ierr);
1185   }
1186 #endif
1187 
1188 #if !defined(PETSC_HAVE_THREADSAFETY)
1189   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_info",&flg2,NULL);CHKERRQ(ierr);
1190   if (!flg2) {
1191     flg2 = PETSC_FALSE;
1192     ierr = PetscOptionsGetBool(NULL,NULL,"-memory_view",&flg2,NULL);CHKERRQ(ierr);
1193   }
1194   if (flg2) {
1195     ierr = PetscMemoryView(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");CHKERRQ(ierr);
1196   }
1197 #endif
1198 
1199 #if defined(PETSC_USE_LOG)
1200   flg1 = PETSC_FALSE;
1201   ierr = PetscOptionsGetBool(NULL,NULL,"-get_total_flops",&flg1,NULL);CHKERRQ(ierr);
1202   if (flg1) {
1203     PetscLogDouble flops = 0;
1204     ierr = MPI_Reduce(&petsc_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
1205     ierr = PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);CHKERRQ(ierr);
1206   }
1207 #endif
1208 
1209 
1210 #if defined(PETSC_USE_LOG)
1211 #if defined(PETSC_HAVE_MPE)
1212   mname[0] = 0;
1213   ierr = PetscOptionsGetString(NULL,NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
1214   if (flg1) {
1215     if (mname[0]) {ierr = PetscLogMPEDump(mname);CHKERRQ(ierr);}
1216     else          {ierr = PetscLogMPEDump(0);CHKERRQ(ierr);}
1217   }
1218 #endif
1219 #endif
1220 
1221   /*
1222      Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_().
1223   */
1224   ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr);
1225 
1226 #if defined(PETSC_USE_LOG)
1227   ierr = PetscLogViewFromOptions();CHKERRQ(ierr);
1228   mname[0] = 0;
1229   ierr = PetscOptionsGetString(NULL,NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
1230   if (flg1) {
1231     PetscViewer viewer;
1232     ierr = (*PetscHelpPrintf)(PETSC_COMM_WORLD,"\n\n WARNING:   -log_summary is being deprecated; switch to -log_view\n\n\n");CHKERRQ(ierr);
1233     if (mname[0]) {
1234       ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);CHKERRQ(ierr);
1235       ierr = PetscLogView(viewer);CHKERRQ(ierr);
1236       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1237     } else {
1238       viewer = PETSC_VIEWER_STDOUT_WORLD;
1239       ierr   = PetscViewerPushFormat(viewer,PETSC_VIEWER_DEFAULT);CHKERRQ(ierr);
1240       ierr   = PetscLogView(viewer);CHKERRQ(ierr);
1241       ierr   = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
1242     }
1243   }
1244 
1245   /*
1246      Free any objects created by the last block of code.
1247   */
1248   ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr);
1249 
1250   mname[0] = 0;
1251   ierr = PetscOptionsGetString(NULL,NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
1252   ierr = PetscOptionsGetString(NULL,NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);CHKERRQ(ierr);
1253   if (flg1 || flg2) {
1254     if (mname[0]) PetscLogDump(mname);
1255     else          PetscLogDump(0);
1256   }
1257 #endif
1258 
1259   ierr = PetscStackDestroy();CHKERRQ(ierr);
1260 
1261   flg1 = PETSC_FALSE;
1262   ierr = PetscOptionsGetBool(NULL,NULL,"-no_signal_handler",&flg1,NULL);CHKERRQ(ierr);
1263   if (!flg1) { ierr = PetscPopSignalHandler();CHKERRQ(ierr);}
1264   flg1 = PETSC_FALSE;
1265   ierr = PetscOptionsGetBool(NULL,NULL,"-mpidump",&flg1,NULL);CHKERRQ(ierr);
1266   if (flg1) {
1267     ierr = PetscMPIDump(stdout);CHKERRQ(ierr);
1268   }
1269   flg1 = PETSC_FALSE;
1270   flg2 = PETSC_FALSE;
1271   /* preemptive call to avoid listing this option in options table as unused */
1272   ierr = PetscOptionsHasName(NULL,NULL,"-malloc_dump",&flg1);CHKERRQ(ierr);
1273   ierr = PetscOptionsHasName(NULL,NULL,"-objects_dump",&flg1);CHKERRQ(ierr);
1274   ierr = PetscOptionsGetBool(NULL,NULL,"-options_view",&flg2,NULL);CHKERRQ(ierr);
1275 
1276   if (flg2) {
1277     PetscViewer viewer;
1278     ierr = PetscViewerCreate(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr);
1279     ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
1280     ierr = PetscOptionsView(NULL,viewer);CHKERRQ(ierr);
1281     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1282   }
1283 
1284   /* to prevent PETSc -options_left from warning */
1285   ierr = PetscOptionsHasName(NULL,NULL,"-nox",&flg1);CHKERRQ(ierr);
1286   ierr = PetscOptionsHasName(NULL,NULL,"-nox_warning",&flg1);CHKERRQ(ierr);
1287 
1288   flg3 = PETSC_FALSE; /* default value is required */
1289   ierr = PetscOptionsGetBool(NULL,NULL,"-options_left",&flg3,&flg1);CHKERRQ(ierr);
1290   ierr = PetscOptionsAllUsed(NULL,&nopt);CHKERRQ(ierr);
1291   if (flg3) {
1292     if (!flg2) { /* have not yet printed the options */
1293       PetscViewer viewer;
1294       ierr = PetscViewerCreate(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr);
1295       ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
1296       ierr = PetscOptionsView(NULL,viewer);CHKERRQ(ierr);
1297       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1298     }
1299     if (!nopt) {
1300       ierr = PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");CHKERRQ(ierr);
1301     } else if (nopt == 1) {
1302       ierr = PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");CHKERRQ(ierr);
1303     } else {
1304       ierr = PetscPrintf(PETSC_COMM_WORLD,"There are %D unused database options. They are:\n",nopt);CHKERRQ(ierr);
1305     }
1306   }
1307 #if defined(PETSC_USE_DEBUG)
1308   if (nopt && !flg3 && !flg1) {
1309     ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");CHKERRQ(ierr);
1310     ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");CHKERRQ(ierr);
1311     ierr = PetscOptionsLeft(NULL);CHKERRQ(ierr);
1312   } else if (nopt && flg3) {
1313 #else
1314   if (nopt && flg3) {
1315 #endif
1316     ierr = PetscOptionsLeft(NULL);CHKERRQ(ierr);
1317   }
1318 
1319 #if defined(PETSC_HAVE_SAWS)
1320   if (!PetscGlobalRank) {
1321     ierr = PetscStackSAWsViewOff();CHKERRQ(ierr);
1322     PetscStackCallSAWs(SAWs_Finalize,());
1323   }
1324 #endif
1325 
1326 #if defined(PETSC_USE_LOG)
1327   /*
1328        List all objects the user may have forgot to free
1329   */
1330   if (PetscObjectsLog) {
1331     ierr = PetscOptionsHasName(NULL,NULL,"-objects_dump",&flg1);CHKERRQ(ierr);
1332     if (flg1) {
1333       MPI_Comm local_comm;
1334       char     string[64];
1335 
1336       ierr = PetscOptionsGetString(NULL,NULL,"-objects_dump",string,64,NULL);CHKERRQ(ierr);
1337       ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr);
1338       ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr);
1339       ierr = PetscObjectsDump(stdout,(string[0] == 'a') ? PETSC_TRUE : PETSC_FALSE);CHKERRQ(ierr);
1340       ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr);
1341       ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr);
1342     }
1343   }
1344 #endif
1345 
1346 #if defined(PETSC_USE_LOG)
1347   PetscObjectsCounts    = 0;
1348   PetscObjectsMaxCounts = 0;
1349   ierr = PetscFree(PetscObjects);CHKERRQ(ierr);
1350 #endif
1351 
1352   /*
1353      Destroy any packages that registered a finalize
1354   */
1355   ierr = PetscRegisterFinalizeAll();CHKERRQ(ierr);
1356 
1357 #if defined(PETSC_USE_LOG)
1358   ierr = PetscLogFinalize();CHKERRQ(ierr);
1359 #endif
1360 
1361   /*
1362      Print PetscFunctionLists that have not been properly freed
1363 
1364   ierr = PetscFunctionListPrintAll();CHKERRQ(ierr);
1365   */
1366 
1367   if (petsc_history) {
1368     ierr = PetscCloseHistoryFile(&petsc_history);CHKERRQ(ierr);
1369     petsc_history = 0;
1370   }
1371   ierr = PetscOptionsHelpPrintedDestroy(&PetscOptionsHelpPrintedSingleton);CHKERRQ(ierr);
1372 
1373   ierr = PetscInfoAllow(PETSC_FALSE,NULL);CHKERRQ(ierr);
1374 
1375 #if !defined(PETSC_HAVE_THREADSAFETY)
1376   {
1377     char fname[PETSC_MAX_PATH_LEN];
1378     FILE *fd;
1379     int  err;
1380 
1381     fname[0] = 0;
1382 
1383     ierr = PetscOptionsGetString(NULL,NULL,"-malloc_dump",fname,250,&flg1);CHKERRQ(ierr);
1384     flg2 = PETSC_FALSE;
1385     ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_test",&flg2,NULL);CHKERRQ(ierr);
1386 #if defined(PETSC_USE_DEBUG)
1387     if (PETSC_RUNNING_ON_VALGRIND) flg2 = PETSC_FALSE;
1388 #else
1389     flg2 = PETSC_FALSE;         /* Skip reporting for optimized builds regardless of -malloc_test */
1390 #endif
1391     if (flg1 && fname[0]) {
1392       char sname[PETSC_MAX_PATH_LEN];
1393 
1394       PetscSNPrintf(sname,PETSC_MAX_PATH_LEN,"%s_%d",fname,rank);
1395       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
1396       ierr = PetscMallocDump(fd);CHKERRQ(ierr);
1397       err  = fclose(fd);
1398       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
1399     } else if (flg1 || flg2) {
1400       MPI_Comm local_comm;
1401 
1402       ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr);
1403       ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr);
1404       ierr = PetscMallocDump(stdout);CHKERRQ(ierr);
1405       ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr);
1406       ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr);
1407     }
1408   }
1409 
1410   {
1411     char fname[PETSC_MAX_PATH_LEN];
1412     FILE *fd = NULL;
1413 
1414     fname[0] = 0;
1415 
1416     ierr = PetscOptionsGetString(NULL,NULL,"-malloc_log",fname,250,&flg1);CHKERRQ(ierr);
1417     ierr = PetscOptionsHasName(NULL,NULL,"-malloc_log_threshold",&flg2);CHKERRQ(ierr);
1418     if (flg1 && fname[0]) {
1419       int err;
1420 
1421       if (!rank) {
1422         fd = fopen(fname,"w");
1423         if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",fname);
1424       }
1425       ierr = PetscMallocDumpLog(fd);CHKERRQ(ierr);
1426       if (fd) {
1427         err = fclose(fd);
1428         if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
1429       }
1430     } else if (flg1 || flg2) {
1431       ierr = PetscMallocDumpLog(stdout);CHKERRQ(ierr);
1432     }
1433   }
1434 #endif
1435 
1436   /*
1437      Close any open dynamic libraries
1438   */
1439   ierr = PetscFinalize_DynamicLibraries();CHKERRQ(ierr);
1440 
1441   /* Can be destroyed only after all the options are used */
1442   ierr = PetscOptionsDestroyDefault();CHKERRQ(ierr);
1443 
1444   PetscGlobalArgc = 0;
1445   PetscGlobalArgs = 0;
1446 
1447   ierr = PetscFreeMPIResources();CHKERRQ(ierr);
1448 
1449   /*
1450      Destroy any known inner MPI_Comm's and attributes pointing to them
1451      Note this will not destroy any new communicators the user has created.
1452 
1453      If all PETSc objects were not destroyed those left over objects will have hanging references to
1454      the MPI_Comms that were freed; but that is ok because those PETSc objects will never be used again
1455  */
1456   {
1457     PetscCommCounter *counter;
1458     PetscMPIInt      flg;
1459     MPI_Comm         icomm;
1460     union {MPI_Comm comm; void *ptr;} ucomm;
1461     ierr = MPI_Comm_get_attr(PETSC_COMM_SELF,Petsc_InnerComm_keyval,&ucomm,&flg);CHKERRQ(ierr);
1462     if (flg) {
1463       icomm = ucomm.comm;
1464       ierr = MPI_Comm_get_attr(icomm,Petsc_Counter_keyval,&counter,&flg);CHKERRQ(ierr);
1465       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory");
1466 
1467       ierr = MPI_Comm_delete_attr(PETSC_COMM_SELF,Petsc_InnerComm_keyval);CHKERRQ(ierr);
1468       ierr = MPI_Comm_delete_attr(icomm,Petsc_Counter_keyval);CHKERRQ(ierr);
1469       ierr = MPI_Comm_free(&icomm);CHKERRQ(ierr);
1470     }
1471     ierr = MPI_Comm_get_attr(PETSC_COMM_WORLD,Petsc_InnerComm_keyval,&ucomm,&flg);CHKERRQ(ierr);
1472     if (flg) {
1473       icomm = ucomm.comm;
1474       ierr = MPI_Comm_get_attr(icomm,Petsc_Counter_keyval,&counter,&flg);CHKERRQ(ierr);
1475       if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory");
1476 
1477       ierr = MPI_Comm_delete_attr(PETSC_COMM_WORLD,Petsc_InnerComm_keyval);CHKERRQ(ierr);
1478       ierr = MPI_Comm_delete_attr(icomm,Petsc_Counter_keyval);CHKERRQ(ierr);
1479       ierr = MPI_Comm_free(&icomm);CHKERRQ(ierr);
1480     }
1481   }
1482 
1483   ierr = MPI_Comm_free_keyval(&Petsc_Counter_keyval);CHKERRQ(ierr);
1484   ierr = MPI_Comm_free_keyval(&Petsc_InnerComm_keyval);CHKERRQ(ierr);
1485   ierr = MPI_Comm_free_keyval(&Petsc_OuterComm_keyval);CHKERRQ(ierr);
1486   ierr = MPI_Comm_free_keyval(&Petsc_Shared_keyval);CHKERRQ(ierr);
1487 
1488   ierr = PetscSpinlockDestroy(&PetscViewerASCIISpinLockOpen);CHKERRQ(ierr);
1489   ierr = PetscSpinlockDestroy(&PetscViewerASCIISpinLockStdout);CHKERRQ(ierr);
1490   ierr = PetscSpinlockDestroy(&PetscViewerASCIISpinLockStderr);CHKERRQ(ierr);
1491   ierr = PetscSpinlockDestroy(&PetscCommSpinLock);CHKERRQ(ierr);
1492 
1493   if (PetscBeganMPI) {
1494 #if defined(PETSC_HAVE_MPI_FINALIZED)
1495     PetscMPIInt flag;
1496     ierr = MPI_Finalized(&flag);CHKERRQ(ierr);
1497     if (flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()");
1498 #endif
1499     ierr = MPI_Finalize();CHKERRQ(ierr);
1500   }
1501 /*
1502 
1503      Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because
1504    the communicator has some outstanding requests on it. Specifically if the
1505    flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See
1506    src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate()
1507    is never freed as it should be. Thus one may obtain messages of the form
1508    [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the
1509    memory was not freed.
1510 
1511 */
1512   ierr = PetscMallocClear();CHKERRQ(ierr);
1513 
1514   PetscInitializeCalled = PETSC_FALSE;
1515   PetscFinalizeCalled   = PETSC_TRUE;
1516   PetscFunctionReturn(0);
1517 }
1518 
1519 #if defined(PETSC_MISSING_LAPACK_lsame_)
1520 PETSC_EXTERN int lsame_(char *a,char *b)
1521 {
1522   if (*a == *b) return 1;
1523   if (*a + 32 == *b) return 1;
1524   if (*a - 32 == *b) return 1;
1525   return 0;
1526 }
1527 #endif
1528 
1529 #if defined(PETSC_MISSING_LAPACK_lsame)
1530 PETSC_EXTERN int lsame(char *a,char *b)
1531 {
1532   if (*a == *b) return 1;
1533   if (*a + 32 == *b) return 1;
1534   if (*a - 32 == *b) return 1;
1535   return 0;
1536 }
1537 #endif
1538