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