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