xref: /petsc/src/sys/objects/pinit.c (revision d3f95da61a40ac3d6791ae3f1ea78f97293e41f2)
1 
2 /*
3    This file defines the initialization of PETSc, including PetscInitialize()
4 */
5 
6 #include <petscsys.h>        /*I  "petscsys.h"   I*/
7 
8 #if defined(PETSC_HAVE_CUSP)
9 #include <cublas.h>
10 #endif
11 #if defined(PETSC_HAVE_VALGRIND)
12 #  include <valgrind/valgrind.h>
13 #  define PETSC_RUNNING_ON_VALGRIND RUNNING_ON_VALGRIND
14 #else
15 #  define PETSC_RUNNING_ON_VALGRIND PETSC_FALSE
16 #endif
17 
18 #if defined(PETSC_USE_LOG)
19 extern PetscErrorCode PetscLogBegin_Private(void);
20 #endif
21 extern PetscBool  PetscHMPIWorker;
22 
23 /* -----------------------------------------------------------------------------------------*/
24 
25 extern FILE *petsc_history;
26 
27 extern PetscErrorCode PetscInitialize_DynamicLibraries(void);
28 extern PetscErrorCode PetscFinalize_DynamicLibraries(void);
29 extern PetscErrorCode PetscFListDestroyAll(void);
30 extern PetscErrorCode PetscOpFListDestroyAll(void);
31 extern PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int);
32 extern PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int);
33 extern PetscErrorCode PetscCloseHistoryFile(FILE **);
34 
35 #if defined(PETSC_HAVE_PTHREADCLASSES)
36 # include <../src/sys/objects/pthread/pthreadimpl.h>
37 #endif
38 
39 /* this is used by the _, __, and ___ macros (see include/petscerror.h) */
40 PetscErrorCode __gierr = 0;
41 
42 /* user may set this BEFORE calling PetscInitialize() */
43 MPI_Comm PETSC_COMM_WORLD = MPI_COMM_NULL;
44 
45 PetscMPIInt Petsc_Counter_keyval   = MPI_KEYVAL_INVALID;
46 PetscMPIInt Petsc_InnerComm_keyval = MPI_KEYVAL_INVALID;
47 PetscMPIInt Petsc_OuterComm_keyval = MPI_KEYVAL_INVALID;
48 
49 /*
50      Declare and set all the string names of the PETSc enums
51 */
52 const char *PetscBools[]     = {"FALSE","TRUE","PetscBool","PETSC_",0};
53 const char *PetscCopyModes[] = {"COPY_VALUES","OWN_POINTER","USE_POINTER","PetscCopyMode","PETSC_",0};
54 const char *PetscDataTypes[] = {"INT","DOUBLE","COMPLEX","LONG","SHORT","FLOAT",
55                                 "CHAR","LOGICAL","ENUM","BOOL","LONGDOUBLE","PetscDataType","PETSC_",0};
56 
57 PetscBool  PetscPreLoadingUsed = PETSC_FALSE;
58 PetscBool  PetscPreLoadingOn   = PETSC_FALSE;
59 
60 /*
61        Checks the options database for initializations related to the
62     PETSc components
63 */
64 #undef __FUNCT__
65 #define __FUNCT__ "PetscOptionsCheckInitial_Components"
66 PetscErrorCode  PetscOptionsCheckInitial_Components(void)
67 {
68   PetscBool  flg1;
69   PetscErrorCode ierr;
70 
71   PetscFunctionBegin;
72   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg1);CHKERRQ(ierr);
73   if (flg1) {
74 #if defined (PETSC_USE_LOG)
75     MPI_Comm   comm = PETSC_COMM_WORLD;
76     ierr = (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");CHKERRQ(ierr);
77     ierr = (*PetscHelpPrintf)(comm," -log_summary_exclude: <vec,mat,pc.ksp,snes>\n");CHKERRQ(ierr);
78     ierr = (*PetscHelpPrintf)(comm," -info_exclude: <null,vec,mat,pc,ksp,snes,ts>\n");CHKERRQ(ierr);
79     ierr = (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");CHKERRQ(ierr);
80 #endif
81   }
82   PetscFunctionReturn(0);
83 }
84 
85 #if defined(PETSC_HAVE_MATLAB_ENGINE)
86 extern PetscBool PetscBeganMPI;
87 
88 #undef __FUNCT__
89 #define __FUNCT__ "PetscInitializeMatlab"
90 /*
91       PetscInitializeMatlab - Calls PetscInitialize() from C/C++ without the pointers to argc and args
92 
93    Collective
94 
95    Level: advanced
96 
97     Notes: this is called only by the PETSc MATLAB interface. Even though it might start MPI it sets the flag to
98      indicate that it did NOT start MPI so that the PetscFinalize() does not end MPI, thus allowing PetscInitialize() to
99      be called multiple times from MATLAB without the problem of trying to initialize MPI more than once.
100 
101      Turns off PETSc signal handling because that can interact with MATLAB's signal handling causing random crashes.
102 
103 .seealso: PetscInitialize(), PetscInitializeFortran(), PetscInitializeNoArguments()
104 */
105 PetscErrorCode  PetscInitializeMatlab(int argc,char **args,const char *filename,const char *help)
106 {
107   PetscErrorCode ierr;
108   int            myargc = argc;
109   char           **myargs = args;
110 
111   PetscFunctionBegin;
112   ierr = PetscInitialize(&myargc,&myargs,filename,help);
113   ierr = PetscPopSignalHandler();CHKERRQ(ierr);
114   PetscBeganMPI = PETSC_FALSE;
115   PetscFunctionReturn(ierr);
116 }
117 
118 #undef __FUNCT__
119 #define __FUNCT__ "PetscInitializedMatlab"
120 /*
121       PetscInitializedMatlab - Has PETSc been initialized already?
122 
123    Not Collective
124 
125    Level: advanced
126 
127     Notes: this is called only by the PETSc MATLAB interface.
128 
129 .seealso: PetscInitialize(), PetscInitializeFortran(), PetscInitializeNoArguments()
130 */
131 int  PetscInitializedMatlab(void)
132 {
133   PetscBool flg;
134 
135   PetscInitialized(&flg);
136   if (flg) return 1;
137   else return 0;
138 }
139 
140 #undef __FUNCT__
141 #define __FUNCT__ "PetscGetPETSC_COMM_SELFMatlab"
142 /*
143       Used by MATLAB interface to get communicator
144 */
145 PetscErrorCode  PetscGetPETSC_COMM_SELFMatlab(MPI_Comm *comm)
146 {
147   PetscFunctionBegin;
148   *comm = PETSC_COMM_SELF;
149   PetscFunctionReturn(0);
150 }
151 #endif
152 
153 #undef __FUNCT__
154 #define __FUNCT__ "PetscInitializeNoArguments"
155 /*@C
156       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
157         the command line arguments.
158 
159    Collective
160 
161    Level: advanced
162 
163 .seealso: PetscInitialize(), PetscInitializeFortran()
164 @*/
165 PetscErrorCode  PetscInitializeNoArguments(void)
166 {
167   PetscErrorCode ierr;
168   int            argc = 0;
169   char           **args = 0;
170 
171   PetscFunctionBegin;
172   ierr = PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL);
173   PetscFunctionReturn(ierr);
174 }
175 
176 #undef __FUNCT__
177 #define __FUNCT__ "PetscInitialized"
178 /*@
179       PetscInitialized - Determine whether PETSc is initialized.
180 
181 7   Level: beginner
182 
183 .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
184 @*/
185 PetscErrorCode  PetscInitialized(PetscBool  *isInitialized)
186 {
187   PetscFunctionBegin;
188   PetscValidPointer(isInitialized, 1);
189   *isInitialized = PetscInitializeCalled;
190   PetscFunctionReturn(0);
191 }
192 
193 #undef __FUNCT__
194 #define __FUNCT__ "PetscFinalized"
195 /*@
196       PetscFinalized - Determine whether PetscFinalize() has been called yet
197 
198    Level: developer
199 
200 .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
201 @*/
202 PetscErrorCode  PetscFinalized(PetscBool  *isFinalized)
203 {
204   PetscFunctionBegin;
205   PetscValidPointer(isFinalized, 1);
206   *isFinalized = PetscFinalizeCalled;
207   PetscFunctionReturn(0);
208 }
209 
210 extern PetscErrorCode        PetscOptionsCheckInitial_Private(void);
211 extern PetscBool  PetscBeganMPI;
212 
213 /*
214        This function is the MPI reduction operation used to compute the sum of the
215    first half of the datatype and the max of the second half.
216 */
217 MPI_Op PetscMaxSum_Op = 0;
218 
219 EXTERN_C_BEGIN
220 #undef __FUNCT__
221 #define __FUNCT__ "PetscMaxSum_Local"
222 void  MPIAPI PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
223 {
224   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;
225 
226   PetscFunctionBegin;
227   if (*datatype != MPIU_2INT) {
228     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
229     MPI_Abort(MPI_COMM_WORLD,1);
230   }
231 
232   for (i=0; i<count; i++) {
233     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
234     xout[2*i+1] += xin[2*i+1];
235   }
236   PetscFunctionReturnVoid();
237 }
238 EXTERN_C_END
239 
240 /*
241     Returns the max of the first entry owned by this processor and the
242 sum of the second entry.
243 
244     The reason nprocs[2*i] contains lengths nprocs[2*i+1] contains flag of 1 if length is nonzero
245 is so that the PetscMaxSum_Op() can set TWO values, if we passed in only nprocs[i] with lengths
246 there would be no place to store the both needed results.
247 */
248 #undef __FUNCT__
249 #define __FUNCT__ "PetscMaxSum"
250 PetscErrorCode  PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum)
251 {
252   PetscMPIInt    size,rank;
253   PetscInt       *work;
254   PetscErrorCode ierr;
255 
256   PetscFunctionBegin;
257   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
258   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
259   ierr   = PetscMalloc(2*size*sizeof(PetscInt),&work);CHKERRQ(ierr);
260   ierr   = MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
261   *max   = work[2*rank];
262   *sum   = work[2*rank+1];
263   ierr   = PetscFree(work);CHKERRQ(ierr);
264   PetscFunctionReturn(0);
265 }
266 
267 /* ----------------------------------------------------------------------------*/
268 MPI_Op  PetscADMax_Op = 0;
269 
270 EXTERN_C_BEGIN
271 #undef __FUNCT__
272 #define __FUNCT__ "PetscADMax_Local"
273 void  MPIAPI PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
274 {
275   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
276   PetscInt    i,count = *cnt;
277 
278   PetscFunctionBegin;
279   if (*datatype != MPIU_2SCALAR) {
280     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
281     MPI_Abort(MPI_COMM_WORLD,1);
282   }
283 
284   for (i=0; i<count; i++) {
285     if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) {
286       xout[2*i]   = xin[2*i];
287       xout[2*i+1] = xin[2*i+1];
288     }
289   }
290   PetscFunctionReturnVoid();
291 }
292 EXTERN_C_END
293 
294 MPI_Op  PetscADMin_Op = 0;
295 
296 EXTERN_C_BEGIN
297 #undef __FUNCT__
298 #define __FUNCT__ "PetscADMin_Local"
299 void  MPIAPI PetscADMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
300 {
301   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
302   PetscInt    i,count = *cnt;
303 
304   PetscFunctionBegin;
305   if (*datatype != MPIU_2SCALAR) {
306     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
307     MPI_Abort(MPI_COMM_WORLD,1);
308   }
309 
310   for (i=0; i<count; i++) {
311     if (PetscRealPart(xout[2*i]) > PetscRealPart(xin[2*i])) {
312       xout[2*i]   = xin[2*i];
313       xout[2*i+1] = xin[2*i+1];
314     }
315   }
316   PetscFunctionReturnVoid();
317 }
318 EXTERN_C_END
319 /* ---------------------------------------------------------------------------------------*/
320 
321 #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128)
322 MPI_Op MPIU_SUM = 0;
323 
324 EXTERN_C_BEGIN
325 #undef __FUNCT__
326 #define __FUNCT__ "PetscSum_Local"
327 void  PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
328 {
329   PetscInt    i,count = *cnt;
330 
331   PetscFunctionBegin;
332   if (*datatype == MPIU_SCALAR) {
333     PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
334     for (i=0; i<count; i++) {
335       xout[i] += xin[i];
336     }
337   } else if (*datatype == MPIU_REAL) {
338     PetscReal *xin = (PetscReal *)in,*xout = (PetscReal*)out;
339     for (i=0; i<count; i++) {
340       xout[i] += xin[i];
341     }
342   } else {
343     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_SCALAR data (i.e. double or complex) types");
344     MPI_Abort(MPI_COMM_WORLD,1);
345   }
346   PetscFunctionReturnVoid();
347 }
348 EXTERN_C_END
349 #endif
350 
351 #if defined(PETSC_USE_REAL___FLOAT128)
352 MPI_Op MPIU_MAX = 0;
353 MPI_Op MPIU_MIN = 0;
354 
355 EXTERN_C_BEGIN
356 #undef __FUNCT__
357 #define __FUNCT__ "PetscMax_Local"
358 void  PetscMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
359 {
360   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
361   PetscInt    i,count = *cnt;
362 
363   PetscFunctionBegin;
364   if (*datatype != MPIU_SCALAR) {
365     (*PetscErrorPrintf)("Can only handle MPIU_SCALAR data (i.e. double or complex) types");
366     MPI_Abort(MPI_COMM_WORLD,1);
367   }
368 
369   for (i=0; i<count; i++) {
370     xout[i] = PetscMax(xout[i],xin[i]);
371   }
372   PetscFunctionReturnVoid();
373 }
374 EXTERN_C_END
375 
376 EXTERN_C_BEGIN
377 #undef __FUNCT__
378 #define __FUNCT__ "PetscMin_Local"
379 void  PetscMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
380 {
381   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
382   PetscInt    i,count = *cnt;
383 
384   PetscFunctionBegin;
385   if (*datatype != MPIU_SCALAR) {
386     (*PetscErrorPrintf)("Can only handle MPIU_SCALAR data (i.e. double or complex) types");
387     MPI_Abort(MPI_COMM_WORLD,1);
388   }
389 
390   for (i=0; i<count; i++) {
391     xout[i] = PetscMin(xout[i],xin[i]);
392   }
393   PetscFunctionReturnVoid();
394 }
395 EXTERN_C_END
396 #endif
397 
398 EXTERN_C_BEGIN
399 #undef __FUNCT__
400 #define __FUNCT__ "Petsc_DelCounter"
401 /*
402    Private routine to delete internal tag/name counter storage when a communicator is freed.
403 
404    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.
405 
406    Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
407 
408 */
409 PetscMPIInt  MPIAPI Petsc_DelCounter(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
410 {
411   PetscErrorCode ierr;
412 
413   PetscFunctionBegin;
414   ierr = PetscInfo1(0,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
415   ierr = PetscFree(count_val);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
416   PetscFunctionReturn(MPI_SUCCESS);
417 }
418 EXTERN_C_END
419 
420 EXTERN_C_BEGIN
421 #undef __FUNCT__
422 #define __FUNCT__ "Petsc_DelComm"
423 /*
424   This does not actually free anything, it simply marks when a reference count to an internal or external MPI_Comm reaches zero and the
425   the external MPI_Comm drops its reference to the internal or external MPI_Comm
426 
427   This is called by MPI, not by users. This is called when MPI_Comm_free() is called on the communicator.
428 
429   Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
430 
431 */
432 PetscMPIInt  MPIAPI Petsc_DelComm(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
433 {
434   PetscErrorCode   ierr;
435   PetscMPIInt      flg;
436   MPI_Comm         icomm;
437   void             *ptr;
438 
439   PetscFunctionBegin;
440   ierr  = MPI_Attr_get(comm,Petsc_InnerComm_keyval,&ptr,&flg);CHKERRQ(ierr);
441   if (flg) {
442     /*  Use PetscMemcpy() because casting from pointer to integer of different size is not allowed with some compilers  */
443     ierr = PetscMemcpy(&icomm,&ptr,sizeof(MPI_Comm));CHKERRQ(ierr);
444     ierr = MPI_Attr_get(icomm,Petsc_OuterComm_keyval,&ptr,&flg);CHKERRQ(ierr);
445     if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected reference to outer comm");
446     ierr = MPI_Attr_delete(icomm,Petsc_OuterComm_keyval);CHKERRQ(ierr);
447     ierr = PetscInfo1(0,"User MPI_Comm m %ld is being freed, removing reference from inner PETSc comm to this outer comm\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
448   } else {
449     ierr = PetscInfo1(0,"Removing reference to PETSc communicator imbedded in a user MPI_Comm m %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
450   }
451   PetscFunctionReturn(MPI_SUCCESS);
452 }
453 EXTERN_C_END
454 
455 #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
456 #if !defined(PETSC_WORDS_BIGENDIAN)
457 EXTERN_C_BEGIN
458 extern PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype,MPI_Aint*,void*);
459 extern PetscMPIInt PetscDataRep_read_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
460 extern PetscMPIInt PetscDataRep_write_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
461 EXTERN_C_END
462 #endif
463 #endif
464 
465 int  PetscGlobalArgc   = 0;
466 char **PetscGlobalArgs = 0;
467 
468 #undef __FUNCT__
469 #define __FUNCT__ "PetscGetArgs"
470 /*@C
471    PetscGetArgs - Allows you to access the raw command line arguments anywhere
472      after PetscInitialize() is called but before PetscFinalize().
473 
474    Not Collective
475 
476    Output Parameters:
477 +  argc - count of number of command line arguments
478 -  args - the command line arguments
479 
480    Level: intermediate
481 
482    Notes:
483       This is usually used to pass the command line arguments into other libraries
484    that are called internally deep in PETSc or the application.
485 
486       The first argument contains the program name as is normal for C arguments.
487 
488    Concepts: command line arguments
489 
490 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()
491 
492 @*/
493 PetscErrorCode  PetscGetArgs(int *argc,char ***args)
494 {
495   PetscFunctionBegin;
496   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
497   *argc = PetscGlobalArgc;
498   *args = PetscGlobalArgs;
499   PetscFunctionReturn(0);
500 }
501 
502 #undef __FUNCT__
503 #define __FUNCT__ "PetscGetArguments"
504 /*@C
505    PetscGetArguments - Allows you to access the  command line arguments anywhere
506      after PetscInitialize() is called but before PetscFinalize().
507 
508    Not Collective
509 
510    Output Parameters:
511 .  args - the command line arguments
512 
513    Level: intermediate
514 
515    Notes:
516       This does NOT start with the program name and IS null terminated (final arg is void)
517 
518    Concepts: command line arguments
519 
520 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()
521 
522 @*/
523 PetscErrorCode  PetscGetArguments(char ***args)
524 {
525   PetscInt       i,argc = PetscGlobalArgc;
526   PetscErrorCode ierr;
527 
528   PetscFunctionBegin;
529   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
530   if (!argc) {*args = 0; PetscFunctionReturn(0);}
531   ierr = PetscMalloc(argc*sizeof(char*),args);CHKERRQ(ierr);
532   for (i=0; i<argc-1; i++) {
533     ierr = PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);CHKERRQ(ierr);
534   }
535   (*args)[argc-1] = 0;
536   PetscFunctionReturn(0);
537 }
538 
539 #undef __FUNCT__
540 #define __FUNCT__ "PetscFreeArguments"
541 /*@C
542    PetscFreeArguments - Frees the memory obtained with PetscGetArguments()
543 
544    Not Collective
545 
546    Output Parameters:
547 .  args - the command line arguments
548 
549    Level: intermediate
550 
551    Concepts: command line arguments
552 
553 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()
554 
555 @*/
556 PetscErrorCode  PetscFreeArguments(char **args)
557 {
558   PetscInt       i = 0;
559   PetscErrorCode ierr;
560 
561   PetscFunctionBegin;
562   if (!args) {PetscFunctionReturn(0);}
563   while (args[i]) {
564     ierr = PetscFree(args[i]);CHKERRQ(ierr);
565     i++;
566   }
567   ierr = PetscFree(args);CHKERRQ(ierr);
568   PetscFunctionReturn(0);
569 }
570 
571 #undef __FUNCT__
572 #define __FUNCT__ "PetscInitialize"
573 /*@C
574    PetscInitialize - Initializes the PETSc database and MPI.
575    PetscInitialize() calls MPI_Init() if that has yet to be called,
576    so this routine should always be called near the beginning of
577    your program -- usually the very first line!
578 
579    Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set
580 
581    Input Parameters:
582 +  argc - count of number of command line arguments
583 .  args - the command line arguments
584 .  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL to not check for
585           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
586 -  help - [optional] Help message to print, use PETSC_NULL for no message
587 
588    If you wish PETSc code to run ONLY on a subcommunicator of MPI_COMM_WORLD, create that
589    communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize(). Thus if you are running a
590    four process job and two processes will run PETSc and have PetscInitialize() and PetscFinalize() and two process will not,
591    then do this. If ALL processes in the job are using PetscInitialize() and PetscFinalize() then you don't need to do this, even
592    if different subcommunicators of the job are doing different things with PETSc.
593 
594    Options Database Keys:
595 +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
596 .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
597 .  -on_error_emacs <machinename> causes emacsclient to jump to error file
598 .  -on_error_abort calls abort() when error detected (no traceback)
599 .  -on_error_mpiabort calls MPI_abort() when error detected
600 .  -error_output_stderr prints error messages to stderr instead of the default stdout
601 .  -error_output_none does not print the error messages (but handles errors in the same way as if this was not called)
602 .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
603 .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
604 .  -stop_for_debugger - Print message on how to attach debugger manually to
605                         process and wait (-debugger_pause) seconds for attachment
606 .  -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries)
607 .  -malloc no - Indicates not to use error-checking malloc
608 .  -malloc_debug - check for memory corruption at EVERY malloc or free
609 .  -malloc_test - like -malloc_dump -malloc_debug, but only active for debugging builds
610 .  -fp_trap - Stops on floating point exceptions (Note that on the
611               IBM RS6000 this slows code by at least a factor of 10.)
612 .  -no_signal_handler - Indicates not to trap error signals
613 .  -shared_tmp - indicates /tmp directory is shared by all processors
614 .  -not_shared_tmp - each processor has own /tmp
615 .  -tmp - alternative name of /tmp directory
616 .  -get_total_flops - returns total flops done by all processors
617 .  -memory_info - Print memory usage at end of run
618 -  -server <port> - start PETSc webserver (default port is 8080)
619 
620    Options Database Keys for Profiling:
621    See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details.
622 +  -log_trace [filename] - Print traces of all PETSc calls
623         to the screen (useful to determine where a program
624         hangs without running in the debugger).  See PetscLogTraceBegin().
625 .  -info <optional filename> - Prints verbose information to the screen
626 -  -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages
627 
628    Environmental Variables:
629 +   PETSC_TMP - alternative tmp directory
630 .   PETSC_SHARED_TMP - tmp is shared by all processes
631 .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
632 .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
633 -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to
634 
635 
636    Level: beginner
637 
638    Notes:
639    If for some reason you must call MPI_Init() separately, call
640    it before PetscInitialize().
641 
642    Fortran Version:
643    In Fortran this routine has the format
644 $       call PetscInitialize(file,ierr)
645 
646 +   ierr - error return code
647 -  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL_CHARACTER to not check for
648           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
649 
650    Important Fortran Note:
651    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
652    null character string; you CANNOT just use PETSC_NULL as
653    in the C version. See the <a href="../../docs/manual.pdf">users manual</a> for details.
654 
655    If your main program is C but you call Fortran code that also uses PETSc you need to call PetscInitializeFortran() soon after
656    calling PetscInitialize().
657 
658    Concepts: initializing PETSc
659 
660 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments()
661 
662 @*/
663 PetscErrorCode  PetscInitialize(int *argc,char ***args,const char file[],const char help[])
664 {
665   PetscErrorCode ierr;
666   PetscMPIInt    flag, size;
667   PetscInt       nodesize;
668   PetscBool      flg;
669   char           hostname[256];
670 
671   PetscFunctionBegin;
672   if (PetscInitializeCalled) PetscFunctionReturn(0);
673 
674   /* these must be initialized in a routine, not as a constant declaration*/
675   PETSC_STDOUT = stdout;
676   PETSC_STDERR = stderr;
677 
678   ierr = PetscOptionsCreate();CHKERRQ(ierr);
679 
680   /*
681      We initialize the program name here (before MPI_Init()) because MPICH has a bug in
682      it that it sets args[0] on all processors to be args[0] on the first processor.
683   */
684   if (argc && *argc) {
685     ierr = PetscSetProgramName(**args);CHKERRQ(ierr);
686   } else {
687     ierr = PetscSetProgramName("Unknown Name");CHKERRQ(ierr);
688   }
689 
690   ierr = MPI_Initialized(&flag);CHKERRQ(ierr);
691   if (!flag) {
692     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");
693     ierr          = MPI_Init(argc,args);CHKERRQ(ierr);
694     PetscBeganMPI = PETSC_TRUE;
695   }
696   if (argc && args) {
697     PetscGlobalArgc = *argc;
698     PetscGlobalArgs = *args;
699   }
700   PetscFinalizeCalled   = PETSC_FALSE;
701 
702   if (PETSC_COMM_WORLD == MPI_COMM_NULL) {
703     PETSC_COMM_WORLD = MPI_COMM_WORLD;
704   }
705   ierr = MPI_Errhandler_set(PETSC_COMM_WORLD,MPI_ERRORS_RETURN);CHKERRQ(ierr);
706 
707   /* Done after init due to a bug in MPICH-GM? */
708   ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr);
709 
710   ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);CHKERRQ(ierr);
711   ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);CHKERRQ(ierr);
712 
713 #if defined(PETSC_USE_COMPLEX)
714   /*
715      Initialized the global complex variable; this is because with
716      shared libraries the constructors for global variables
717      are not called; at least on IRIX.
718   */
719   {
720 #if defined(PETSC_CLANGUAGE_CXX)
721     PetscScalar ic(0.0,1.0);
722     PETSC_i = ic;
723 #else
724     PETSC_i = I;
725 #endif
726   }
727 
728 #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
729   ierr = MPI_Type_contiguous(2,MPIU_REAL,&MPIU_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
730   ierr = MPI_Type_commit(&MPIU_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
731   ierr = MPI_Type_contiguous(2,MPI_FLOAT,&MPIU_C_COMPLEX);CHKERRQ(ierr);
732   ierr = MPI_Type_commit(&MPIU_C_COMPLEX);CHKERRQ(ierr);
733   ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);CHKERRQ(ierr);
734 #endif
735 #endif
736 
737   /*
738      Create the PETSc MPI reduction operator that sums of the first
739      half of the entries and maxes the second half.
740   */
741   ierr = MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);CHKERRQ(ierr);
742 
743 #if defined(PETSC_USE_REAL___FLOAT128)
744   ierr = MPI_Type_contiguous(2,MPI_DOUBLE,&MPIU___FLOAT128);CHKERRQ(ierr);
745   ierr = MPI_Type_commit(&MPIU___FLOAT128);CHKERRQ(ierr);
746   ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);CHKERRQ(ierr);
747   ierr = MPI_Op_create(PetscMax_Local,1,&MPIU_MAX);CHKERRQ(ierr);
748   ierr = MPI_Op_create(PetscMin_Local,1,&MPIU_MIN);CHKERRQ(ierr);
749 #endif
750 
751   ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);CHKERRQ(ierr);
752   ierr = MPI_Type_commit(&MPIU_2SCALAR);CHKERRQ(ierr);
753   ierr = MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);CHKERRQ(ierr);
754   ierr = MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);CHKERRQ(ierr);
755 
756   ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);CHKERRQ(ierr);
757   ierr = MPI_Type_commit(&MPIU_2INT);CHKERRQ(ierr);
758 
759   /*
760      Attributes to be set on PETSc communicators
761   */
762   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelCounter,&Petsc_Counter_keyval,(void*)0);CHKERRQ(ierr);
763   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_InnerComm_keyval,(void*)0);CHKERRQ(ierr);
764   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_OuterComm_keyval,(void*)0);CHKERRQ(ierr);
765 
766   /*
767      Build the options database
768   */
769   ierr = PetscOptionsInsert(argc,args,file);CHKERRQ(ierr);
770 
771 
772   /*
773      Print main application help message
774   */
775   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
776   if (help && flg) {
777     ierr = PetscPrintf(PETSC_COMM_WORLD,help);CHKERRQ(ierr);
778   }
779   ierr = PetscOptionsCheckInitial_Private();CHKERRQ(ierr);
780 
781   /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */
782 #if defined(PETSC_USE_LOG)
783   ierr = PetscLogBegin_Private();CHKERRQ(ierr);
784 #endif
785 
786   /*
787      Load the dynamic libraries (on machines that support them), this registers all
788      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
789   */
790   ierr = PetscInitialize_DynamicLibraries();CHKERRQ(ierr);
791 
792   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
793   ierr = PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);CHKERRQ(ierr);
794   ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr);
795   ierr = PetscInfo1(0,"Running on machine: %s\n",hostname);CHKERRQ(ierr);
796 
797   ierr = PetscOptionsCheckInitial_Components();CHKERRQ(ierr);
798   /* Check the options database for options related to the options database itself */
799   ierr = PetscOptionsSetFromOptions();CHKERRQ(ierr);
800 
801 #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
802   /*
803       Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI
804 
805       Currently not used because it is not supported by MPICH.
806   */
807 #if !defined(PETSC_WORDS_BIGENDIAN)
808   ierr = MPI_Register_datarep((char *)"petsc",PetscDataRep_read_conv_fn,PetscDataRep_write_conv_fn,PetscDataRep_extent_fn,PETSC_NULL);CHKERRQ(ierr);
809 #endif
810 #endif
811 
812   ierr = PetscOptionsGetInt(PETSC_NULL,"-hmpi_spawn_size",&nodesize,&flg);CHKERRQ(ierr);
813   if (flg) {
814 #if defined(PETSC_HAVE_MPI_COMM_SPAWN)
815     ierr = PetscHMPISpawn((PetscMPIInt) nodesize);CHKERRQ(ierr); /* worker nodes never return from here; they go directly to PetscEnd() */
816 #else
817     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"PETSc built without MPI 2 (MPI_Comm_spawn) support, use -hmpi_merge_size instead");
818 #endif
819   } else {
820     ierr = PetscOptionsGetInt(PETSC_NULL,"-hmpi_merge_size",&nodesize,&flg);CHKERRQ(ierr);
821     if (flg) {
822       ierr = PetscHMPIMerge((PetscMPIInt) nodesize,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
823       if (PetscHMPIWorker) { /* if worker then never enter user code */
824         PetscInitializeCalled = PETSC_TRUE;
825         ierr = PetscEnd();
826       }
827     }
828   }
829 
830 #if defined(PETSC_HAVE_CUDA)
831   cublasInit();
832 #endif
833 
834 #if defined(PETSC_HAVE_PTHREADCLASSES)
835   ierr = PetscThreadsInitialize(PetscMaxThreads);CHKERRQ(ierr);
836 #endif
837 
838 #if defined(PETSC_HAVE_AMS)
839   ierr = PetscOptionsHasName(PETSC_NULL,"-ams_publish_objects",&flg);CHKERRQ(ierr);
840   if (flg) {
841     PetscAMSPublishAll = PETSC_TRUE;
842   }
843 #endif
844 
845   ierr = PetscOptionsHasName(PETSC_NULL,"-python",&flg);CHKERRQ(ierr);
846   if (flg) {
847     PetscInitializeCalled = PETSC_TRUE;
848     ierr = PetscPythonInitialize(PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
849   }
850 
851   /*
852       Once we are completedly initialized then we can set this variables
853   */
854   PetscInitializeCalled = PETSC_TRUE;
855   PetscFunctionReturn(0);
856 }
857 
858 extern PetscObject *PetscObjects;
859 extern PetscInt    PetscObjectsCounts, PetscObjectsMaxCounts;
860 
861 #undef __FUNCT__
862 #define __FUNCT__ "PetscFinalize"
863 /*@C
864    PetscFinalize - Checks for options to be called at the conclusion
865    of the program. MPI_Finalize() is called only if the user had not
866    called MPI_Init() before calling PetscInitialize().
867 
868    Collective on PETSC_COMM_WORLD
869 
870    Options Database Keys:
871 +  -options_table - Calls PetscOptionsView()
872 .  -options_left - Prints unused options that remain in the database
873 .  -objects_left  - Prints list of all objects that have not been freed
874 .  -mpidump - Calls PetscMPIDump()
875 .  -malloc_dump - Calls PetscMallocDump()
876 .  -malloc_info - Prints total memory usage
877 -  -malloc_log - Prints summary of memory usage
878 
879    Options Database Keys for Profiling:
880    See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details.
881 +  -log_summary [filename] - Prints summary of flop and timing
882         information to screen. If the filename is specified the
883         summary is written to the file.  See PetscLogView().
884 .  -log_summary_python [filename] - Prints data on of flop and timing usage to a file or screen.
885         See PetscLogPrintSViewPython().
886 .  -log_all [filename] - Logs extensive profiling information
887         See PetscLogDump().
888 .  -log [filename] - Logs basic profiline information  See PetscLogDump().
889 .  -log_sync - Log the synchronization in scatters, inner products
890         and norms
891 -  -log_mpe [filename] - Creates a logfile viewable by the
892       utility Upshot/Nupshot (in MPICH distribution)
893 
894    Level: beginner
895 
896    Note:
897    See PetscInitialize() for more general runtime options.
898 
899 .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
900 @*/
901 PetscErrorCode  PetscFinalize(void)
902 {
903   PetscErrorCode ierr;
904   PetscMPIInt    rank;
905   PetscInt       i,nopt;
906   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,objects_left = PETSC_FALSE;
907 #if defined(PETSC_HAVE_AMS)
908   PetscBool      flg = PETSC_FALSE;
909 #endif
910 #if defined(PETSC_USE_LOG)
911   char           mname[PETSC_MAX_PATH_LEN];
912 #endif
913 
914   PetscFunctionBegin;
915 
916   if (!PetscInitializeCalled) {
917     printf("PetscInitialize() must be called before PetscFinalize()\n");
918     PetscFunctionReturn(PETSC_ERR_ARG_WRONGSTATE);
919   }
920   ierr = PetscInfo(PETSC_NULL,"PetscFinalize() called\n");
921 
922 #if defined(PETSC_HAVE_AMS)
923   ierr = PetscOptionsGetBool(PETSC_NULL,"-options_gui",&flg,PETSC_NULL);CHKERRQ(ierr);
924   if (flg) {
925     ierr = PetscOptionsAMSDestroy();CHKERRQ(ierr);
926   }
927 #endif
928 
929   ierr = PetscHMPIFinalize();CHKERRQ(ierr);
930 
931 #if defined(PETSC_HAVE_PTHREADCLASSES)
932   ierr = PetscThreadsFinalize();CHKERRQ(ierr);
933 #endif
934 
935   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
936   ierr = PetscOptionsGetBool(PETSC_NULL,"-malloc_info",&flg2,PETSC_NULL);CHKERRQ(ierr);
937   if (!flg2) {
938     flg2 = PETSC_FALSE;
939     ierr = PetscOptionsGetBool(PETSC_NULL,"-memory_info",&flg2,PETSC_NULL);CHKERRQ(ierr);
940   }
941   if (flg2) {
942     ierr = PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");CHKERRQ(ierr);
943   }
944 
945 #if defined(PETSC_USE_LOG)
946   flg1 = PETSC_FALSE;
947   ierr = PetscOptionsGetBool(PETSC_NULL,"-get_total_flops",&flg1,PETSC_NULL);CHKERRQ(ierr);
948   if (flg1) {
949     PetscLogDouble flops = 0;
950     ierr = MPI_Reduce(&_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
951     ierr = PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);CHKERRQ(ierr);
952   }
953 #endif
954 
955 
956 #if defined(PETSC_USE_LOG)
957 #if defined(PETSC_HAVE_MPE)
958   mname[0] = 0;
959   ierr = PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
960   if (flg1){
961     if (mname[0]) {ierr = PetscLogMPEDump(mname);CHKERRQ(ierr);}
962     else          {ierr = PetscLogMPEDump(0);CHKERRQ(ierr);}
963   }
964 #endif
965   mname[0] = 0;
966   ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
967   if (flg1) {
968     PetscViewer viewer;
969     if (mname[0])  {
970       ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);CHKERRQ(ierr);
971       ierr = PetscLogView(viewer);CHKERRQ(ierr);
972       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
973     } else {
974       viewer = PETSC_VIEWER_STDOUT_WORLD;
975       ierr = PetscLogView(viewer);CHKERRQ(ierr);
976     }
977   }
978 
979   mname[0] = 0;
980   ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary_python",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
981   if (flg1) {
982     PetscViewer viewer;
983     if (mname[0])  {
984       ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);CHKERRQ(ierr);
985       ierr = PetscLogViewPython(viewer);CHKERRQ(ierr);
986       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
987     } else {
988       viewer = PETSC_VIEWER_STDOUT_WORLD;
989       ierr = PetscLogViewPython(viewer);CHKERRQ(ierr);
990     }
991   }
992 
993   ierr = PetscOptionsGetString(PETSC_NULL,"-log_detailed",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
994   if (flg1) {
995     if (mname[0])  {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);}
996     else           {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,0);CHKERRQ(ierr);}
997   }
998 
999   mname[0] = 0;
1000   ierr = PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
1001   ierr = PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);CHKERRQ(ierr);
1002   if (flg1 || flg2){
1003     if (mname[0]) PetscLogDump(mname);
1004     else          PetscLogDump(0);
1005   }
1006 #endif
1007 
1008 #if defined(PETSC_USE_DEBUG) && !defined(PETSC_USE_PTHREAD)
1009   if (PetscStackActive) {
1010     ierr = PetscStackDestroy();CHKERRQ(ierr);
1011   }
1012 #endif
1013 
1014   flg1 = PETSC_FALSE;
1015   ierr = PetscOptionsGetBool(PETSC_NULL,"-no_signal_handler",&flg1,PETSC_NULL);CHKERRQ(ierr);
1016   if (!flg1) { ierr = PetscPopSignalHandler();CHKERRQ(ierr);}
1017   flg1 = PETSC_FALSE;
1018   ierr = PetscOptionsGetBool(PETSC_NULL,"-mpidump",&flg1,PETSC_NULL);CHKERRQ(ierr);
1019   if (flg1) {
1020     ierr = PetscMPIDump(stdout);CHKERRQ(ierr);
1021   }
1022   flg1 = PETSC_FALSE;
1023   flg2 = PETSC_FALSE;
1024   /* preemptive call to avoid listing this option in options table as unused */
1025   ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);CHKERRQ(ierr);
1026   ierr = PetscOptionsGetBool(PETSC_NULL,"-options_table",&flg2,PETSC_NULL);CHKERRQ(ierr);
1027 
1028   if (flg2) {
1029     ierr = PetscOptionsView(PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1030   }
1031 
1032   /* to prevent PETSc -options_left from warning */
1033   ierr = PetscOptionsHasName(PETSC_NULL,"-nox",&flg1);CHKERRQ(ierr);
1034   ierr = PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr);
1035   ierr = PetscOptionsGetBool(PETSC_NULL,"-objects_left",&objects_left,PETSC_NULL);CHKERRQ(ierr);
1036 
1037   if (!PetscHMPIWorker) { /* worker processes skip this because they do not usually process options */
1038     flg3 = PETSC_FALSE; /* default value is required */
1039     ierr = PetscOptionsGetBool(PETSC_NULL,"-options_left",&flg3,&flg1);CHKERRQ(ierr);
1040     ierr = PetscOptionsAllUsed(&nopt);CHKERRQ(ierr);
1041     if (flg3) {
1042       if (!flg2) { /* have not yet printed the options */
1043 	ierr = PetscOptionsView(PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
1044       }
1045       if (!nopt) {
1046 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");CHKERRQ(ierr);
1047       } else if (nopt == 1) {
1048 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");CHKERRQ(ierr);
1049       } else {
1050 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There are %D unused database options. They are:\n",nopt);CHKERRQ(ierr);
1051       }
1052     }
1053 #if defined(PETSC_USE_DEBUG)
1054     if (nopt && !flg3 && !flg1) {
1055       ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");CHKERRQ(ierr);
1056       ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");CHKERRQ(ierr);
1057       ierr = PetscOptionsLeft();CHKERRQ(ierr);
1058     } else if (nopt && flg3) {
1059 #else
1060     if (nopt && flg3) {
1061 #endif
1062       ierr = PetscOptionsLeft();CHKERRQ(ierr);
1063     }
1064   }
1065 
1066   /*
1067      Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_().
1068   */
1069   ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr);
1070 
1071   /*
1072        List all objects the user may have forgot to free
1073   */
1074   if (objects_left && PetscObjectsCounts) {
1075     ierr = PetscPrintf(PETSC_COMM_WORLD,"The following objects %D were never freed\n",PetscObjectsCounts);
1076   }
1077   for (i=0; i<PetscObjectsMaxCounts; i++) {
1078     if (PetscObjects[i]) {
1079       if (objects_left) {
1080         ierr = PetscPrintf(PETSC_COMM_WORLD,"  %s %s %s\n",PetscObjects[i]->class_name,PetscObjects[i]->type_name,PetscObjects[i]->name);CHKERRQ(ierr);
1081       }
1082     }
1083   }
1084   /* cannot actually destroy the left over objects, but destroy the list */
1085   PetscObjectsCounts    = 0;
1086   PetscObjectsMaxCounts = 0;
1087   ierr = PetscFree(PetscObjects);CHKERRQ(ierr);
1088 
1089 
1090 #if defined(PETSC_USE_LOG)
1091   ierr = PetscLogDestroy();CHKERRQ(ierr);
1092 #endif
1093 
1094   /*
1095        Free all the registered create functions, such as KSPList, VecList, SNESList, etc
1096   */
1097   ierr = PetscFListDestroyAll();CHKERRQ(ierr);
1098 
1099   /*
1100        Free all the registered op functions, such as MatOpList, etc
1101   */
1102   ierr = PetscOpFListDestroyAll();CHKERRQ(ierr);
1103 
1104   /*
1105      Destroy any packages that registered a finalize
1106   */
1107   ierr = PetscRegisterFinalizeAll();CHKERRQ(ierr);
1108 
1109   /*
1110      Destroy all the function registration lists created
1111   */
1112   ierr = PetscFinalize_DynamicLibraries();CHKERRQ(ierr);
1113 
1114   if (petsc_history) {
1115     ierr = PetscCloseHistoryFile(&petsc_history);CHKERRQ(ierr);
1116     petsc_history = 0;
1117   }
1118 
1119   ierr = PetscInfoAllow(PETSC_FALSE,PETSC_NULL);CHKERRQ(ierr);
1120 
1121   {
1122     char fname[PETSC_MAX_PATH_LEN];
1123     FILE *fd;
1124     int  err;
1125 
1126     fname[0] = 0;
1127     ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_dump",fname,250,&flg1);CHKERRQ(ierr);
1128     flg2 = PETSC_FALSE;
1129     ierr = PetscOptionsGetBool(PETSC_NULL,"-malloc_test",&flg2,PETSC_NULL);CHKERRQ(ierr);
1130 #if defined(PETSC_USE_DEBUG) && !defined(PETSC_USE_PTHREAD)
1131     if (PETSC_RUNNING_ON_VALGRIND) flg2 = PETSC_FALSE;
1132 #else
1133     flg2 = PETSC_FALSE;         /* Skip reporting for optimized builds regardless of -malloc_test */
1134 #endif
1135     if (flg1 && fname[0]) {
1136       char sname[PETSC_MAX_PATH_LEN];
1137 
1138       sprintf(sname,"%s_%d",fname,rank);
1139       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
1140       ierr = PetscMallocDump(fd);CHKERRQ(ierr);
1141       err = fclose(fd);
1142       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
1143     } else if (flg1 || flg2) {
1144       MPI_Comm local_comm;
1145 
1146       ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr);
1147       ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr);
1148         ierr = PetscMallocDump(stdout);CHKERRQ(ierr);
1149       ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr);
1150       ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr);
1151     }
1152   }
1153   {
1154     char fname[PETSC_MAX_PATH_LEN];
1155     FILE *fd;
1156 
1157     fname[0] = 0;
1158     ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_log",fname,250,&flg1);CHKERRQ(ierr);
1159     if (flg1 && fname[0]) {
1160       char sname[PETSC_MAX_PATH_LEN];
1161       int  err;
1162 
1163       sprintf(sname,"%s_%d",fname,rank);
1164       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
1165       ierr = PetscMallocDumpLog(fd);CHKERRQ(ierr);
1166       err = fclose(fd);
1167       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
1168     } else if (flg1) {
1169       ierr = PetscMallocDumpLog(stdout);CHKERRQ(ierr);
1170     }
1171   }
1172   /* Can be destroyed only after all the options are used */
1173   ierr = PetscOptionsDestroy();CHKERRQ(ierr);
1174 
1175   PetscGlobalArgc = 0;
1176   PetscGlobalArgs = 0;
1177 
1178 #if defined(PETSC_USE_REAL___FLOAT128)
1179   ierr = MPI_Type_free(&MPIU___FLOAT128);CHKERRQ(ierr);
1180   ierr = MPI_Op_free(&MPIU_SUM);CHKERRQ(ierr);
1181   ierr = MPI_Op_free(&MPIU_MAX);CHKERRQ(ierr);
1182   ierr = MPI_Op_free(&MPIU_MIN);CHKERRQ(ierr);
1183 #endif
1184 
1185 #if defined(PETSC_USE_COMPLEX)
1186 #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
1187   ierr = MPI_Op_free(&MPIU_SUM);CHKERRQ(ierr);
1188   ierr = MPI_Type_free(&MPIU_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
1189   ierr = MPI_Type_free(&MPIU_C_COMPLEX);CHKERRQ(ierr);
1190 #endif
1191 #endif
1192   ierr = MPI_Type_free(&MPIU_2SCALAR);CHKERRQ(ierr);
1193   ierr = MPI_Type_free(&MPIU_2INT);CHKERRQ(ierr);
1194   ierr = MPI_Op_free(&PetscMaxSum_Op);CHKERRQ(ierr);
1195   ierr = MPI_Op_free(&PetscADMax_Op);CHKERRQ(ierr);
1196   ierr = MPI_Op_free(&PetscADMin_Op);CHKERRQ(ierr);
1197 
1198   /*
1199      Destroy any known inner MPI_Comm's and attributes pointing to them
1200      Note this will not destroy any new communicators the user has created.
1201 
1202      If all PETSc objects were not destroyed those left over objects will have hanging references to
1203      the MPI_Comms that were freed; but that is ok because those PETSc objects will never be used again
1204  */
1205   {
1206     PetscCommCounter *counter;
1207     PetscMPIInt      flg;
1208     MPI_Comm         icomm;
1209     void             *ptr;
1210     ierr  = MPI_Attr_get(PETSC_COMM_SELF,Petsc_InnerComm_keyval,&ptr,&flg);CHKERRQ(ierr);
1211     if (flg) {
1212       /*  Use PetscMemcpy() because casting from pointer to integer of different size is not allowed with some compilers  */
1213       ierr = PetscMemcpy(&icomm,&ptr,sizeof(MPI_Comm));CHKERRQ(ierr);
1214       ierr = MPI_Attr_get(icomm,Petsc_Counter_keyval,&counter,&flg);CHKERRQ(ierr);
1215       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory");
1216 
1217       ierr = MPI_Attr_delete(PETSC_COMM_SELF,Petsc_InnerComm_keyval);CHKERRQ(ierr);
1218       ierr = MPI_Attr_delete(icomm,Petsc_Counter_keyval);CHKERRQ(ierr);
1219       ierr = MPI_Comm_free(&icomm);CHKERRQ(ierr);
1220     }
1221     ierr  = MPI_Attr_get(PETSC_COMM_WORLD,Petsc_InnerComm_keyval,&ptr,&flg);CHKERRQ(ierr);
1222     if (flg) {
1223       /*  Use PetscMemcpy() because casting from pointer to integer of different size is not allowed with some compilers  */
1224       ierr = PetscMemcpy(&icomm,&ptr,sizeof(MPI_Comm));CHKERRQ(ierr);
1225       ierr = MPI_Attr_get(icomm,Petsc_Counter_keyval,&counter,&flg);CHKERRQ(ierr);
1226       if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory");
1227 
1228       ierr = MPI_Attr_delete(PETSC_COMM_WORLD,Petsc_InnerComm_keyval);CHKERRQ(ierr);
1229       ierr = MPI_Attr_delete(icomm,Petsc_Counter_keyval);CHKERRQ(ierr);
1230       ierr = MPI_Comm_free(&icomm);CHKERRQ(ierr);
1231     }
1232   }
1233 
1234   ierr = MPI_Keyval_free(&Petsc_Counter_keyval);CHKERRQ(ierr);
1235   ierr = MPI_Keyval_free(&Petsc_InnerComm_keyval);CHKERRQ(ierr);
1236   ierr = MPI_Keyval_free(&Petsc_OuterComm_keyval);CHKERRQ(ierr);
1237 
1238   ierr = PetscInfo(0,"PETSc successfully ended!\n");CHKERRQ(ierr);
1239   if (PetscBeganMPI) {
1240 #if defined(PETSC_HAVE_MPI_FINALIZED)
1241     PetscMPIInt flag;
1242     ierr = MPI_Finalized(&flag);CHKERRQ(ierr);
1243     if (flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()");
1244 #endif
1245     ierr = MPI_Finalize();CHKERRQ(ierr);
1246   }
1247 
1248   if (PETSC_ZOPEFD){
1249     if (PETSC_ZOPEFD != PETSC_STDOUT) fprintf(PETSC_ZOPEFD, "<<<end>>>");
1250     else fprintf(PETSC_STDOUT, "<<<end>>>");
1251   }
1252 
1253 #if defined(PETSC_HAVE_CUDA)
1254   cublasShutdown();
1255 #endif
1256 /*
1257 
1258      Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because
1259    the communicator has some outstanding requests on it. Specifically if the
1260    flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See
1261    src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate()
1262    is never freed as it should be. Thus one may obtain messages of the form
1263    [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the
1264    memory was not freed.
1265 
1266 */
1267   ierr = PetscMallocClear();CHKERRQ(ierr);
1268   PetscInitializeCalled = PETSC_FALSE;
1269   PetscFinalizeCalled   = PETSC_TRUE;
1270   PetscFunctionReturn(ierr);
1271 }
1272 
1273 #if defined(PETSC_MISSING_LAPACK_lsame_)
1274 EXTERN_C_BEGIN
1275 int lsame_(char *a,char *b)
1276 {
1277   if (*a == *b) return 1;
1278   if (*a + 32 == *b) return 1;
1279   if (*a - 32 == *b) return 1;
1280   return 0;
1281 }
1282 EXTERN_C_END
1283 #endif
1284 
1285 #if defined(PETSC_MISSING_LAPACK_lsame)
1286 EXTERN_C_BEGIN
1287 int lsame(char *a,char *b)
1288 {
1289   if (*a == *b) return 1;
1290   if (*a + 32 == *b) return 1;
1291   if (*a - 32 == *b) return 1;
1292   return 0;
1293 }
1294 EXTERN_C_END
1295 #endif
1296