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