xref: /petsc/src/sys/objects/pinit.c (revision 2eff7a5159698db79bc55cdeeefa9ddc181a02b7)
1e5c89e4eSSatish Balay #define PETSC_DLL
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay    This file defines the initialization of PETSc, including PetscInitialize()
4e5c89e4eSSatish Balay */
5e5c89e4eSSatish Balay 
6d382aafbSBarry Smith #include "petscsys.h"        /*I  "petscsys.h"   I*/
78101f56cSMatthew Knepley 
87a025f21SVictor Minden #if defined(PETSC_HAVE_CUDA)
92f947c57SVictor Minden #include <cublas.h>
107a025f21SVictor Minden #endif
117a025f21SVictor Minden 
12a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
1309573ac7SBarry Smith extern PetscErrorCode PetscLogBegin_Private(void);
14a9f03627SSatish Balay #endif
15ace3abfcSBarry Smith extern PetscBool  PetscOpenMPWorker;
16e5c89e4eSSatish Balay 
17e5c89e4eSSatish Balay /* -----------------------------------------------------------------------------------------*/
18e5c89e4eSSatish Balay 
19e5c89e4eSSatish Balay extern FILE *petsc_history;
20e5c89e4eSSatish Balay 
2109573ac7SBarry Smith extern PetscErrorCode PetscInitialize_DynamicLibraries(void);
2209573ac7SBarry Smith extern PetscErrorCode PetscFinalize_DynamicLibraries(void);
2309573ac7SBarry Smith extern PetscErrorCode PetscFListDestroyAll(void);
2409573ac7SBarry Smith extern PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int);
2509573ac7SBarry Smith extern PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int);
2609573ac7SBarry Smith extern PetscErrorCode PetscCloseHistoryFile(FILE **);
27e5c89e4eSSatish Balay 
28e5c89e4eSSatish Balay /* this is used by the _, __, and ___ macros (see include/petscerror.h) */
29e5c89e4eSSatish Balay PetscErrorCode __gierr = 0;
30e5c89e4eSSatish Balay 
31e5c89e4eSSatish Balay /* user may set this BEFORE calling PetscInitialize() */
32e8373e55SMatthew Knepley MPI_Comm PETSC_COMM_WORLD = MPI_COMM_NULL;
33e5c89e4eSSatish Balay 
34480cf27aSJed Brown PetscMPIInt Petsc_Counter_keyval   = MPI_KEYVAL_INVALID;
35480cf27aSJed Brown PetscMPIInt Petsc_InnerComm_keyval = MPI_KEYVAL_INVALID;
36480cf27aSJed Brown PetscMPIInt Petsc_OuterComm_keyval = MPI_KEYVAL_INVALID;
37480cf27aSJed Brown 
38e5c89e4eSSatish Balay /*
39e5c89e4eSSatish Balay      Declare and set all the string names of the PETSc enums
40e5c89e4eSSatish Balay */
41ace3abfcSBarry Smith const char *PetscBools[]     = {"FALSE","TRUE","PetscBool","PETSC_",0};
4270b3c8c7SBarry Smith const char *PetscCopyModes[] = {"COPY_VALUES","OWN_POINTER","USE_POINTER","PetscCopyMode","PETSC_",0};
4370b3c8c7SBarry Smith const char *PetscDataTypes[] = {"INT","DOUBLE","COMPLEX","LONG","SHORT","FLOAT",
44ace3abfcSBarry Smith                                 "CHAR","LOGICAL","ENUM","BOOL","LONGDOUBLE","PetscDataType","PETSC_",0};
45e5c89e4eSSatish Balay 
46ace3abfcSBarry Smith PetscBool  PetscPreLoadingUsed = PETSC_FALSE;
47ace3abfcSBarry Smith PetscBool  PetscPreLoadingOn   = PETSC_FALSE;
480f8e0872SSatish Balay 
49e5c89e4eSSatish Balay /*
50e5c89e4eSSatish Balay        Checks the options database for initializations related to the
51e5c89e4eSSatish Balay     PETSc components
52e5c89e4eSSatish Balay */
53e5c89e4eSSatish Balay #undef __FUNCT__
54e5c89e4eSSatish Balay #define __FUNCT__ "PetscOptionsCheckInitial_Components"
557087cfbeSBarry Smith PetscErrorCode  PetscOptionsCheckInitial_Components(void)
56e5c89e4eSSatish Balay {
57ace3abfcSBarry Smith   PetscBool  flg1;
58e5c89e4eSSatish Balay   PetscErrorCode ierr;
59e5c89e4eSSatish Balay 
60e5c89e4eSSatish Balay   PetscFunctionBegin;
61e5c89e4eSSatish Balay   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg1);CHKERRQ(ierr);
62e5c89e4eSSatish Balay   if (flg1) {
63e5c89e4eSSatish Balay #if defined (PETSC_USE_LOG)
64e8e7597cSSatish Balay     MPI_Comm   comm = PETSC_COMM_WORLD;
65e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");CHKERRQ(ierr);
66e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -log_summary_exclude: <vec,mat,pc.ksp,snes>\n");CHKERRQ(ierr);
676cf91177SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -info_exclude: <null,vec,mat,pc,ksp,snes,ts>\n");CHKERRQ(ierr);
68e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");CHKERRQ(ierr);
69e5c89e4eSSatish Balay #endif
70e5c89e4eSSatish Balay   }
71e5c89e4eSSatish Balay   PetscFunctionReturn(0);
72e5c89e4eSSatish Balay }
73e5c89e4eSSatish Balay 
74df413903SBarry Smith extern PetscBool PetscBeganMPI;
75df413903SBarry Smith 
76e5c89e4eSSatish Balay #undef __FUNCT__
7772a42c3cSBarry Smith #define __FUNCT__ "PetscInitializeNonPointers"
7872a42c3cSBarry Smith /*@C
7972a42c3cSBarry Smith       PetscInitializeNonPointers - Calls PetscInitialize() from C/C++ without the pointers to argc and args
8072a42c3cSBarry Smith 
8172a42c3cSBarry Smith    Collective
8272a42c3cSBarry Smith 
8372a42c3cSBarry Smith    Level: advanced
8472a42c3cSBarry Smith 
8572a42c3cSBarry Smith .seealso: PetscInitialize(), PetscInitializeFortran(), PetscInitializeNoArguments()
8672a42c3cSBarry Smith @*/
877087cfbeSBarry Smith PetscErrorCode  PetscInitializeNonPointers(int argc,char **args,const char *filename,const char *help)
8872a42c3cSBarry Smith {
8972a42c3cSBarry Smith   PetscErrorCode ierr;
9072a42c3cSBarry Smith   int            myargc = argc;
9172a42c3cSBarry Smith   char           **myargs = args;
9272a42c3cSBarry Smith 
9372a42c3cSBarry Smith   PetscFunctionBegin;
9472a42c3cSBarry Smith   ierr = PetscInitialize(&myargc,&myargs,filename,help);
95df413903SBarry Smith   PetscBeganMPI = PETSC_FALSE;
9672a42c3cSBarry Smith   PetscFunctionReturn(ierr);
9772a42c3cSBarry Smith }
9872a42c3cSBarry Smith 
9972a42c3cSBarry Smith #undef __FUNCT__
100f0865b08SBarry Smith #define __FUNCT__ "PetscGetPETSC_COMM_SELF"
101f0865b08SBarry Smith /*
102f0865b08SBarry Smith       Used by Matlab interface to get communicator
103f0865b08SBarry Smith */
104f0865b08SBarry Smith PetscErrorCode  PetscGetPETSC_COMM_SELF(MPI_Comm *comm)
105f0865b08SBarry Smith {
106f0865b08SBarry Smith   PetscFunctionBegin;
107f0865b08SBarry Smith   *comm = PETSC_COMM_SELF;
108f0865b08SBarry Smith   PetscFunctionReturn(0);
109f0865b08SBarry Smith }
110f0865b08SBarry Smith 
111f0865b08SBarry Smith #undef __FUNCT__
112e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitializeNoArguments"
113e5c89e4eSSatish Balay /*@C
114e5c89e4eSSatish Balay       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
115e5c89e4eSSatish Balay         the command line arguments.
116e5c89e4eSSatish Balay 
117e5c89e4eSSatish Balay    Collective
118e5c89e4eSSatish Balay 
119e5c89e4eSSatish Balay    Level: advanced
120e5c89e4eSSatish Balay 
121e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeFortran()
122e5c89e4eSSatish Balay @*/
1237087cfbeSBarry Smith PetscErrorCode  PetscInitializeNoArguments(void)
124e5c89e4eSSatish Balay {
125e5c89e4eSSatish Balay   PetscErrorCode ierr;
126e5c89e4eSSatish Balay   int            argc = 0;
127e5c89e4eSSatish Balay   char           **args = 0;
128e5c89e4eSSatish Balay 
129e5c89e4eSSatish Balay   PetscFunctionBegin;
130e5c89e4eSSatish Balay   ierr = PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL);
131e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
132e5c89e4eSSatish Balay }
133e5c89e4eSSatish Balay 
134e5c89e4eSSatish Balay #undef __FUNCT__
135e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialized"
136e5c89e4eSSatish Balay /*@
137e5c89e4eSSatish Balay       PetscInitialized - Determine whether PETSc is initialized.
138e5c89e4eSSatish Balay 
1396dc8fec2Sbcordonn 7   Level: beginner
140e5c89e4eSSatish Balay 
141e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
142e5c89e4eSSatish Balay @*/
1437087cfbeSBarry Smith PetscErrorCode  PetscInitialized(PetscBool  *isInitialized)
144e5c89e4eSSatish Balay {
145e5c89e4eSSatish Balay   PetscFunctionBegin;
146e5c89e4eSSatish Balay   PetscValidPointer(isInitialized, 1);
147e5c89e4eSSatish Balay   *isInitialized = PetscInitializeCalled;
148e5c89e4eSSatish Balay   PetscFunctionReturn(0);
149e5c89e4eSSatish Balay }
150e5c89e4eSSatish Balay 
151e5c89e4eSSatish Balay #undef __FUNCT__
152e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalized"
153e5c89e4eSSatish Balay /*@
154e5c89e4eSSatish Balay       PetscFinalized - Determine whether PetscFinalize() has been called yet
155e5c89e4eSSatish Balay 
156e5c89e4eSSatish Balay    Level: developer
157e5c89e4eSSatish Balay 
158e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
159e5c89e4eSSatish Balay @*/
1607087cfbeSBarry Smith PetscErrorCode  PetscFinalized(PetscBool  *isFinalized)
161e5c89e4eSSatish Balay {
162e5c89e4eSSatish Balay   PetscFunctionBegin;
163e5c89e4eSSatish Balay   PetscValidPointer(isFinalized, 1);
164e5c89e4eSSatish Balay   *isFinalized = PetscFinalizeCalled;
165e5c89e4eSSatish Balay   PetscFunctionReturn(0);
166e5c89e4eSSatish Balay }
167e5c89e4eSSatish Balay 
16809573ac7SBarry Smith extern PetscErrorCode        PetscOptionsCheckInitial_Private(void);
169ace3abfcSBarry Smith extern PetscBool  PetscBeganMPI;
170e5c89e4eSSatish Balay 
171e5c89e4eSSatish Balay /*
172e5c89e4eSSatish Balay        This function is the MPI reduction operation used to compute the sum of the
173e5c89e4eSSatish Balay    first half of the datatype and the max of the second half.
174e5c89e4eSSatish Balay */
175e5c89e4eSSatish Balay MPI_Op PetscMaxSum_Op = 0;
176e5c89e4eSSatish Balay 
177e5c89e4eSSatish Balay EXTERN_C_BEGIN
178e5c89e4eSSatish Balay #undef __FUNCT__
179e5c89e4eSSatish Balay #define __FUNCT__ "PetscMaxSum_Local"
1807087cfbeSBarry Smith void  MPIAPI PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
181e5c89e4eSSatish Balay {
182e5c89e4eSSatish Balay   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;
183e5c89e4eSSatish Balay 
184e5c89e4eSSatish Balay   PetscFunctionBegin;
185e5c89e4eSSatish Balay   if (*datatype != MPIU_2INT) {
186e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
187e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
188e5c89e4eSSatish Balay   }
189e5c89e4eSSatish Balay 
190e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
191e5c89e4eSSatish Balay     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
192e5c89e4eSSatish Balay     xout[2*i+1] += xin[2*i+1];
193e5c89e4eSSatish Balay   }
194812af9f3SBarry Smith   PetscFunctionReturnVoid();
195e5c89e4eSSatish Balay }
196e5c89e4eSSatish Balay EXTERN_C_END
197e5c89e4eSSatish Balay 
198e5c89e4eSSatish Balay /*
199e5c89e4eSSatish Balay     Returns the max of the first entry owned by this processor and the
200e5c89e4eSSatish Balay sum of the second entry.
201b693b147SBarry Smith 
202b693b147SBarry Smith     The reason nprocs[2*i] contains lengths nprocs[2*i+1] contains flag of 1 if length is nonzero
203b693b147SBarry Smith is so that the PetscMaxSum_Op() can set TWO values, if we passed in only nprocs[i] with lengths
204b693b147SBarry Smith there would be no place to store the both needed results.
205e5c89e4eSSatish Balay */
206e5c89e4eSSatish Balay #undef __FUNCT__
207e5c89e4eSSatish Balay #define __FUNCT__ "PetscMaxSum"
2087087cfbeSBarry Smith PetscErrorCode  PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum)
209e5c89e4eSSatish Balay {
210e5c89e4eSSatish Balay   PetscMPIInt    size,rank;
211e5c89e4eSSatish Balay   PetscInt       *work;
212e5c89e4eSSatish Balay   PetscErrorCode ierr;
213e5c89e4eSSatish Balay 
214e5c89e4eSSatish Balay   PetscFunctionBegin;
215e5c89e4eSSatish Balay   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
216e5c89e4eSSatish Balay   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
217e5c89e4eSSatish Balay   ierr   = PetscMalloc(2*size*sizeof(PetscInt),&work);CHKERRQ(ierr);
218e5c89e4eSSatish Balay   ierr   = MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
219e5c89e4eSSatish Balay   *max   = work[2*rank];
220e5c89e4eSSatish Balay   *sum   = work[2*rank+1];
221e5c89e4eSSatish Balay   ierr   = PetscFree(work);CHKERRQ(ierr);
222e5c89e4eSSatish Balay   PetscFunctionReturn(0);
223e5c89e4eSSatish Balay }
224e5c89e4eSSatish Balay 
225e5c89e4eSSatish Balay /* ----------------------------------------------------------------------------*/
2267087cfbeSBarry Smith MPI_Op  PetscADMax_Op = 0;
227e5c89e4eSSatish Balay 
228e5c89e4eSSatish Balay EXTERN_C_BEGIN
229e5c89e4eSSatish Balay #undef __FUNCT__
230e5c89e4eSSatish Balay #define __FUNCT__ "PetscADMax_Local"
2317087cfbeSBarry Smith void  MPIAPI PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
232e5c89e4eSSatish Balay {
233e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
234e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
235e5c89e4eSSatish Balay 
236e5c89e4eSSatish Balay   PetscFunctionBegin;
237e5c89e4eSSatish Balay   if (*datatype != MPIU_2SCALAR) {
238e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
239e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
240e5c89e4eSSatish Balay   }
241e5c89e4eSSatish Balay 
242e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
243e5c89e4eSSatish Balay     if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) {
244e5c89e4eSSatish Balay       xout[2*i]   = xin[2*i];
245e5c89e4eSSatish Balay       xout[2*i+1] = xin[2*i+1];
246e5c89e4eSSatish Balay     }
247e5c89e4eSSatish Balay   }
248812af9f3SBarry Smith   PetscFunctionReturnVoid();
249e5c89e4eSSatish Balay }
250e5c89e4eSSatish Balay EXTERN_C_END
251e5c89e4eSSatish Balay 
2527087cfbeSBarry Smith MPI_Op  PetscADMin_Op = 0;
253e5c89e4eSSatish Balay 
254e5c89e4eSSatish Balay EXTERN_C_BEGIN
255e5c89e4eSSatish Balay #undef __FUNCT__
256e5c89e4eSSatish Balay #define __FUNCT__ "PetscADMin_Local"
2577087cfbeSBarry Smith void  MPIAPI PetscADMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
258e5c89e4eSSatish Balay {
259e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
260e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
261e5c89e4eSSatish Balay 
262e5c89e4eSSatish Balay   PetscFunctionBegin;
263e5c89e4eSSatish Balay   if (*datatype != MPIU_2SCALAR) {
264e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
265e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
266e5c89e4eSSatish Balay   }
267e5c89e4eSSatish Balay 
268e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
269e5c89e4eSSatish Balay     if (PetscRealPart(xout[2*i]) > PetscRealPart(xin[2*i])) {
270e5c89e4eSSatish Balay       xout[2*i]   = xin[2*i];
271e5c89e4eSSatish Balay       xout[2*i+1] = xin[2*i+1];
272e5c89e4eSSatish Balay     }
273e5c89e4eSSatish Balay   }
274812af9f3SBarry Smith   PetscFunctionReturnVoid();
275e5c89e4eSSatish Balay }
276e5c89e4eSSatish Balay EXTERN_C_END
277e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
278e5c89e4eSSatish Balay 
279e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
2802c876bd9SBarry Smith 
2812c876bd9SBarry Smith /*
2822c876bd9SBarry Smith     This operation is only needed when using complex numbers with older MPI that does not support complex numbers
2832c876bd9SBarry Smith */
2842c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
28506a205a8SBarry Smith MPI_Op MPIU_SUM = 0;
286e5c89e4eSSatish Balay 
287e5c89e4eSSatish Balay EXTERN_C_BEGIN
288e5c89e4eSSatish Balay #undef __FUNCT__
289e5c89e4eSSatish Balay #define __FUNCT__ "PetscSum_Local"
2907087cfbeSBarry Smith void  PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
291e5c89e4eSSatish Balay {
292e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
293e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
294e5c89e4eSSatish Balay 
295e5c89e4eSSatish Balay   PetscFunctionBegin;
296e5c89e4eSSatish Balay   if (*datatype != MPIU_SCALAR) {
297e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_SCALAR data (i.e. double or complex) types");
298e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
299e5c89e4eSSatish Balay   }
300e5c89e4eSSatish Balay 
301e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
302e5c89e4eSSatish Balay     xout[i] += xin[i];
303e5c89e4eSSatish Balay   }
304812af9f3SBarry Smith   PetscFunctionReturnVoid();
305e5c89e4eSSatish Balay }
306e5c89e4eSSatish Balay EXTERN_C_END
307e5c89e4eSSatish Balay #endif
3082c876bd9SBarry Smith #endif
309e5c89e4eSSatish Balay 
310480cf27aSJed Brown EXTERN_C_BEGIN
311480cf27aSJed Brown #undef __FUNCT__
312480cf27aSJed Brown #define __FUNCT__ "Petsc_DelCounter"
313480cf27aSJed Brown /*
314480cf27aSJed Brown    Private routine to delete internal tag/name counter storage when a communicator is freed.
315480cf27aSJed Brown 
316480cf27aSJed Brown    This is called by MPI, not by users.
317480cf27aSJed Brown 
318480cf27aSJed Brown    Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
319480cf27aSJed Brown 
320480cf27aSJed Brown */
3217087cfbeSBarry Smith PetscMPIInt  MPIAPI Petsc_DelCounter(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
322480cf27aSJed Brown {
323480cf27aSJed Brown   PetscErrorCode ierr;
324480cf27aSJed Brown 
325480cf27aSJed Brown   PetscFunctionBegin;
326480cf27aSJed Brown   ierr = PetscInfo1(0,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
327480cf27aSJed Brown   ierr = PetscFree(count_val);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
328480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
329480cf27aSJed Brown }
330480cf27aSJed Brown EXTERN_C_END
331480cf27aSJed Brown 
332480cf27aSJed Brown EXTERN_C_BEGIN
333480cf27aSJed Brown #undef __FUNCT__
334480cf27aSJed Brown #define __FUNCT__ "Petsc_DelComm"
335480cf27aSJed Brown /*
336480cf27aSJed Brown   This does not actually free anything, it simply marks when a reference count to an internal MPI_Comm reaches zero and the
337480cf27aSJed Brown   the external MPI_Comm drops its reference to the internal MPI_Comm
338480cf27aSJed Brown 
339480cf27aSJed Brown   This is called by MPI, not by users.
340480cf27aSJed Brown 
341480cf27aSJed Brown   Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
342480cf27aSJed Brown 
343480cf27aSJed Brown */
3447087cfbeSBarry Smith PetscMPIInt  MPIAPI Petsc_DelComm(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
345480cf27aSJed Brown {
346480cf27aSJed Brown   PetscErrorCode ierr;
347480cf27aSJed Brown 
348480cf27aSJed Brown   PetscFunctionBegin;
349480cf27aSJed Brown   ierr = PetscInfo1(0,"Deleting PETSc communicator imbedded in a user MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
350480cf27aSJed Brown   /* actually don't delete anything because we cannot increase the reference count of the communicator anyways */
351480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
352480cf27aSJed Brown }
353480cf27aSJed Brown EXTERN_C_END
354480cf27aSJed Brown 
355951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
356e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
357e39fd77fSBarry Smith EXTERN_C_BEGIN
358e39fd77fSBarry Smith extern PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype,MPI_Aint*,void*);
359e39fd77fSBarry Smith extern PetscMPIInt PetscDataRep_read_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
360e39fd77fSBarry Smith extern PetscMPIInt PetscDataRep_write_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
361e39fd77fSBarry Smith EXTERN_C_END
362e39fd77fSBarry Smith #endif
363951e3c8eSBarry Smith #endif
364e39fd77fSBarry Smith 
3656ae9a8a6SBarry Smith int  PetscGlobalArgc   = 0;
3666ae9a8a6SBarry Smith char **PetscGlobalArgs = 0;
367e5c89e4eSSatish Balay 
368e5c89e4eSSatish Balay #undef __FUNCT__
369e5c89e4eSSatish Balay #define __FUNCT__ "PetscGetArgs"
370e5c89e4eSSatish Balay /*@C
371e5c89e4eSSatish Balay    PetscGetArgs - Allows you to access the raw command line arguments anywhere
372e5c89e4eSSatish Balay      after PetscInitialize() is called but before PetscFinalize().
373e5c89e4eSSatish Balay 
374e5c89e4eSSatish Balay    Not Collective
375e5c89e4eSSatish Balay 
376e5c89e4eSSatish Balay    Output Parameters:
377e5c89e4eSSatish Balay +  argc - count of number of command line arguments
378e5c89e4eSSatish Balay -  args - the command line arguments
379e5c89e4eSSatish Balay 
380e5c89e4eSSatish Balay    Level: intermediate
381e5c89e4eSSatish Balay 
382e5c89e4eSSatish Balay    Notes:
383e5c89e4eSSatish Balay       This is usually used to pass the command line arguments into other libraries
384e5c89e4eSSatish Balay    that are called internally deep in PETSc or the application.
385e5c89e4eSSatish Balay 
386f177e3b1SBarry Smith       The first argument contains the program name as is normal for C arguments.
387f177e3b1SBarry Smith 
388e5c89e4eSSatish Balay    Concepts: command line arguments
389e5c89e4eSSatish Balay 
390793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()
391e5c89e4eSSatish Balay 
392e5c89e4eSSatish Balay @*/
3937087cfbeSBarry Smith PetscErrorCode  PetscGetArgs(int *argc,char ***args)
394e5c89e4eSSatish Balay {
395e5c89e4eSSatish Balay   PetscFunctionBegin;
39617186662SBarry Smith   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
397e5c89e4eSSatish Balay   *argc = PetscGlobalArgc;
398e5c89e4eSSatish Balay   *args = PetscGlobalArgs;
399e5c89e4eSSatish Balay   PetscFunctionReturn(0);
400e5c89e4eSSatish Balay }
401e5c89e4eSSatish Balay 
402e5c89e4eSSatish Balay #undef __FUNCT__
403793721a6SBarry Smith #define __FUNCT__ "PetscGetArguments"
404793721a6SBarry Smith /*@C
405793721a6SBarry Smith    PetscGetArguments - Allows you to access the  command line arguments anywhere
406793721a6SBarry Smith      after PetscInitialize() is called but before PetscFinalize().
407793721a6SBarry Smith 
408793721a6SBarry Smith    Not Collective
409793721a6SBarry Smith 
410793721a6SBarry Smith    Output Parameters:
411793721a6SBarry Smith .  args - the command line arguments
412793721a6SBarry Smith 
413793721a6SBarry Smith    Level: intermediate
414793721a6SBarry Smith 
415793721a6SBarry Smith    Notes:
416793721a6SBarry Smith       This does NOT start with the program name and IS null terminated (final arg is void)
417793721a6SBarry Smith 
418793721a6SBarry Smith    Concepts: command line arguments
419793721a6SBarry Smith 
420793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()
421793721a6SBarry Smith 
422793721a6SBarry Smith @*/
4237087cfbeSBarry Smith PetscErrorCode  PetscGetArguments(char ***args)
424793721a6SBarry Smith {
425793721a6SBarry Smith   PetscInt       i,argc = PetscGlobalArgc;
426793721a6SBarry Smith   PetscErrorCode ierr;
427793721a6SBarry Smith 
428793721a6SBarry Smith   PetscFunctionBegin;
42917186662SBarry Smith   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
430717030eeSLisandro Dalcin   if (!argc) {*args = 0; PetscFunctionReturn(0);}
431793721a6SBarry Smith   ierr = PetscMalloc(argc*sizeof(char*),args);CHKERRQ(ierr);
432793721a6SBarry Smith   for (i=0; i<argc-1; i++) {
433793721a6SBarry Smith     ierr = PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);CHKERRQ(ierr);
434793721a6SBarry Smith   }
435793721a6SBarry Smith   (*args)[argc-1] = 0;
436793721a6SBarry Smith   PetscFunctionReturn(0);
437793721a6SBarry Smith }
438793721a6SBarry Smith 
439793721a6SBarry Smith #undef __FUNCT__
440793721a6SBarry Smith #define __FUNCT__ "PetscFreeArguments"
441793721a6SBarry Smith /*@C
442793721a6SBarry Smith    PetscFreeArguments - Frees the memory obtained with PetscGetArguments()
443793721a6SBarry Smith 
444793721a6SBarry Smith    Not Collective
445793721a6SBarry Smith 
446793721a6SBarry Smith    Output Parameters:
447793721a6SBarry Smith .  args - the command line arguments
448793721a6SBarry Smith 
449793721a6SBarry Smith    Level: intermediate
450793721a6SBarry Smith 
451793721a6SBarry Smith    Concepts: command line arguments
452793721a6SBarry Smith 
453793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()
454793721a6SBarry Smith 
455793721a6SBarry Smith @*/
4567087cfbeSBarry Smith PetscErrorCode  PetscFreeArguments(char **args)
457793721a6SBarry Smith {
458793721a6SBarry Smith   PetscInt       i = 0;
459793721a6SBarry Smith   PetscErrorCode ierr;
460793721a6SBarry Smith 
461793721a6SBarry Smith   PetscFunctionBegin;
462717030eeSLisandro Dalcin   if (!args) {PetscFunctionReturn(0);}
463793721a6SBarry Smith   while (args[i]) {
464793721a6SBarry Smith     ierr = PetscFree(args[i]);CHKERRQ(ierr);
465793721a6SBarry Smith     i++;
466793721a6SBarry Smith   }
467793721a6SBarry Smith   ierr = PetscFree(args);CHKERRQ(ierr);
468793721a6SBarry Smith   PetscFunctionReturn(0);
469793721a6SBarry Smith }
470793721a6SBarry Smith 
471793721a6SBarry Smith #undef __FUNCT__
472e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialize"
473e5c89e4eSSatish Balay /*@C
474e5c89e4eSSatish Balay    PetscInitialize - Initializes the PETSc database and MPI.
475e5c89e4eSSatish Balay    PetscInitialize() calls MPI_Init() if that has yet to be called,
476e5c89e4eSSatish Balay    so this routine should always be called near the beginning of
477e5c89e4eSSatish Balay    your program -- usually the very first line!
478e5c89e4eSSatish Balay 
479e5c89e4eSSatish Balay    Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set
480e5c89e4eSSatish Balay 
481e5c89e4eSSatish Balay    Input Parameters:
482e5c89e4eSSatish Balay +  argc - count of number of command line arguments
483e5c89e4eSSatish Balay .  args - the command line arguments
484fc2bca9aSBarry Smith .  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL to not check for
485fc2bca9aSBarry Smith           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
486e5c89e4eSSatish Balay -  help - [optional] Help message to print, use PETSC_NULL for no message
487e5c89e4eSSatish Balay 
48805827820SBarry Smith    If you wish PETSc code to run ONLY on a subcommunicator of MPI_COMM_WORLD, create that
48905827820SBarry Smith    communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize(). Thus if you are running a
49005827820SBarry Smith    four process job and two processes will run PETSc and have PetscInitialize() and PetscFinalize() and two process will not,
49105827820SBarry Smith    then do this. If ALL processes in the job are using PetscInitialize() and PetscFinalize() then you don't need to do this, even
49205827820SBarry Smith    if different subcommunicators of the job are doing different things with PETSc.
493e5c89e4eSSatish Balay 
494e5c89e4eSSatish Balay    Options Database Keys:
495e5c89e4eSSatish Balay +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
496e5c89e4eSSatish Balay .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
497e5c89e4eSSatish Balay .  -on_error_emacs <machinename> causes emacsclient to jump to error file
498b52f573bSBarry Smith .  -on_error_abort calls abort() when error detected (no traceback)
499e8fb0fc0SBarry Smith .  -on_error_mpiabort calls MPI_abort() when error detected
500e8fb0fc0SBarry Smith .  -error_output_stderr prints error messages to stderr instead of the default stdout
501e8fb0fc0SBarry Smith .  -error_output_none does not print the error messages (but handles errors in the same way as if this was not called)
502e5c89e4eSSatish Balay .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
503e5c89e4eSSatish Balay .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
504e5c89e4eSSatish Balay .  -stop_for_debugger - Print message on how to attach debugger manually to
505e5c89e4eSSatish Balay                         process and wait (-debugger_pause) seconds for attachment
5062fb0ec9aSBarry Smith .  -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries)
507e5c89e4eSSatish Balay .  -malloc no - Indicates not to use error-checking malloc
5082fb0ec9aSBarry Smith .  -malloc_debug - check for memory corruption at EVERY malloc or free
509e5c89e4eSSatish Balay .  -fp_trap - Stops on floating point exceptions (Note that on the
510e5c89e4eSSatish Balay               IBM RS6000 this slows code by at least a factor of 10.)
511e5c89e4eSSatish Balay .  -no_signal_handler - Indicates not to trap error signals
512e5c89e4eSSatish Balay .  -shared_tmp - indicates /tmp directory is shared by all processors
513e5c89e4eSSatish Balay .  -not_shared_tmp - each processor has own /tmp
514e5c89e4eSSatish Balay .  -tmp - alternative name of /tmp directory
515e5c89e4eSSatish Balay .  -get_total_flops - returns total flops done by all processors
516e5c89e4eSSatish Balay -  -memory_info - Print memory usage at end of run
517e5c89e4eSSatish Balay 
518e5c89e4eSSatish Balay    Options Database Keys for Profiling:
5190598bfebSBarry Smith    See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details.
520e5c89e4eSSatish Balay +  -log_trace [filename] - Print traces of all PETSc calls
521e5c89e4eSSatish Balay         to the screen (useful to determine where a program
522e5c89e4eSSatish Balay         hangs without running in the debugger).  See PetscLogTraceBegin().
5236cf91177SBarry Smith .  -info <optional filename> - Prints verbose information to the screen
5246cf91177SBarry Smith -  -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages
525e5c89e4eSSatish Balay 
526e5c89e4eSSatish Balay    Environmental Variables:
527e5c89e4eSSatish Balay +   PETSC_TMP - alternative tmp directory
528e5c89e4eSSatish Balay .   PETSC_SHARED_TMP - tmp is shared by all processes
529e5c89e4eSSatish Balay .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
530e5c89e4eSSatish Balay .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
531e5c89e4eSSatish Balay -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to
532e5c89e4eSSatish Balay 
533e5c89e4eSSatish Balay 
534e5c89e4eSSatish Balay    Level: beginner
535e5c89e4eSSatish Balay 
536e5c89e4eSSatish Balay    Notes:
537e5c89e4eSSatish Balay    If for some reason you must call MPI_Init() separately, call
538e5c89e4eSSatish Balay    it before PetscInitialize().
539e5c89e4eSSatish Balay 
540e5c89e4eSSatish Balay    Fortran Version:
541e5c89e4eSSatish Balay    In Fortran this routine has the format
542e5c89e4eSSatish Balay $       call PetscInitialize(file,ierr)
543e5c89e4eSSatish Balay 
544e5c89e4eSSatish Balay +   ierr - error return code
5453dae0d48SMatthew Knepley -  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL_CHARACTER to not check for
546fc2bca9aSBarry Smith           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
547e5c89e4eSSatish Balay 
548e5c89e4eSSatish Balay    Important Fortran Note:
549e5c89e4eSSatish Balay    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
550e5c89e4eSSatish Balay    null character string; you CANNOT just use PETSC_NULL as
5510598bfebSBarry Smith    in the C version. See the <a href="../../docs/manual.pdf">users manual</a> for details.
552e5c89e4eSSatish Balay 
55301cb0274SBarry Smith    If your main program is C but you call Fortran code that also uses PETSc you need to call PetscInitializeFortran() soon after
55401cb0274SBarry Smith    calling PetscInitialize().
555e5c89e4eSSatish Balay 
556e5c89e4eSSatish Balay    Concepts: initializing PETSc
557e5c89e4eSSatish Balay 
55801cb0274SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments()
559e5c89e4eSSatish Balay 
560e5c89e4eSSatish Balay @*/
5617087cfbeSBarry Smith PetscErrorCode  PetscInitialize(int *argc,char ***args,const char file[],const char help[])
562e5c89e4eSSatish Balay {
563e5c89e4eSSatish Balay   PetscErrorCode ierr;
564aa5bb8c0SSatish Balay   PetscMPIInt    flag, size;
565aa5bb8c0SSatish Balay   PetscInt       nodesize;
566ace3abfcSBarry Smith   PetscBool      flg;
567e5c89e4eSSatish Balay   char           hostname[256];
568e5c89e4eSSatish Balay 
569e5c89e4eSSatish Balay   PetscFunctionBegin;
570e5c89e4eSSatish Balay   if (PetscInitializeCalled) PetscFunctionReturn(0);
571e5c89e4eSSatish Balay 
572ae9b4142SLisandro Dalcin   /* these must be initialized in a routine, not as a constant declaration*/
573d89683f4Sbcordonn   PETSC_STDOUT = stdout;
574ae9b4142SLisandro Dalcin   PETSC_STDERR = stderr;
575e5c89e4eSSatish Balay 
576e5c89e4eSSatish Balay   ierr = PetscOptionsCreate();CHKERRQ(ierr);
577e5c89e4eSSatish Balay 
578e5c89e4eSSatish Balay   /*
579e5c89e4eSSatish Balay      We initialize the program name here (before MPI_Init()) because MPICH has a bug in
580e5c89e4eSSatish Balay      it that it sets args[0] on all processors to be args[0] on the first processor.
581e5c89e4eSSatish Balay   */
582e5c89e4eSSatish Balay   if (argc && *argc) {
583e5c89e4eSSatish Balay     ierr = PetscSetProgramName(**args);CHKERRQ(ierr);
584e5c89e4eSSatish Balay   } else {
585e5c89e4eSSatish Balay     ierr = PetscSetProgramName("Unknown Name");CHKERRQ(ierr);
586e5c89e4eSSatish Balay   }
587e5c89e4eSSatish Balay 
588e5c89e4eSSatish Balay   ierr = MPI_Initialized(&flag);CHKERRQ(ierr);
589e5c89e4eSSatish Balay   if (!flag) {
590e32f2f54SBarry Smith     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");
591e5c89e4eSSatish Balay     ierr          = MPI_Init(argc,args);CHKERRQ(ierr);
592e5c89e4eSSatish Balay     PetscBeganMPI = PETSC_TRUE;
593e5c89e4eSSatish Balay   }
594e5c89e4eSSatish Balay   if (argc && args) {
595e5c89e4eSSatish Balay     PetscGlobalArgc = *argc;
596e5c89e4eSSatish Balay     PetscGlobalArgs = *args;
597e5c89e4eSSatish Balay   }
598e5c89e4eSSatish Balay   PetscFinalizeCalled   = PETSC_FALSE;
599e5c89e4eSSatish Balay 
600e8373e55SMatthew Knepley   if (PETSC_COMM_WORLD == MPI_COMM_NULL) {
601e5c89e4eSSatish Balay     PETSC_COMM_WORLD = MPI_COMM_WORLD;
602e5c89e4eSSatish Balay   }
603660746e0SBarry Smith   ierr = MPI_Errhandler_set(PETSC_COMM_WORLD,MPI_ERRORS_RETURN);CHKERRQ(ierr);
604e5c89e4eSSatish Balay 
605e5c89e4eSSatish Balay   /* Done after init due to a bug in MPICH-GM? */
606e5c89e4eSSatish Balay   ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr);
607e5c89e4eSSatish Balay 
608e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);CHKERRQ(ierr);
609e5c89e4eSSatish Balay   ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);CHKERRQ(ierr);
610e5c89e4eSSatish Balay 
611e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
612e5c89e4eSSatish Balay   /*
613e5c89e4eSSatish Balay      Initialized the global complex variable; this is because with
614e5c89e4eSSatish Balay      shared libraries the constructors for global variables
615e5c89e4eSSatish Balay      are not called; at least on IRIX.
616e5c89e4eSSatish Balay   */
617e5c89e4eSSatish Balay   {
618762437b8SSatish Balay #if defined(PETSC_CLANGUAGE_CXX)
619e5c89e4eSSatish Balay     PetscScalar ic(0.0,1.0);
620e5c89e4eSSatish Balay     PETSC_i = ic;
621b7940d39SSatish Balay #else
6223433f298SSatish Balay     PETSC_i = I;
623b7940d39SSatish Balay #endif
624762437b8SSatish Balay   }
625762437b8SSatish Balay 
6262c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
6272c876bd9SBarry Smith   ierr = MPI_Type_contiguous(2,MPIU_REAL,&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
6282c876bd9SBarry Smith   ierr = MPI_Type_commit(&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
629a83b8d76SBarry Smith   ierr = MPI_Type_contiguous(2,MPI_FLOAT,&MPI_C_COMPLEX);CHKERRQ(ierr);
630a83b8d76SBarry Smith   ierr = MPI_Type_commit(&MPI_C_COMPLEX);CHKERRQ(ierr);
63106a205a8SBarry Smith   ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);CHKERRQ(ierr);
632e5c89e4eSSatish Balay #endif
6332c876bd9SBarry Smith #endif
634e5c89e4eSSatish Balay 
635e5c89e4eSSatish Balay   /*
636e5c89e4eSSatish Balay      Create the PETSc MPI reduction operator that sums of the first
637e5c89e4eSSatish Balay      half of the entries and maxes the second half.
638e5c89e4eSSatish Balay   */
639e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);CHKERRQ(ierr);
640e5c89e4eSSatish Balay 
641e5c89e4eSSatish Balay   ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);CHKERRQ(ierr);
642e5c89e4eSSatish Balay   ierr = MPI_Type_commit(&MPIU_2SCALAR);CHKERRQ(ierr);
643e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);CHKERRQ(ierr);
644e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);CHKERRQ(ierr);
645e5c89e4eSSatish Balay 
646e5c89e4eSSatish Balay   ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);CHKERRQ(ierr);
647e5c89e4eSSatish Balay   ierr = MPI_Type_commit(&MPIU_2INT);CHKERRQ(ierr);
648e5c89e4eSSatish Balay 
649e5c89e4eSSatish Balay   /*
650480cf27aSJed Brown      Attributes to be set on PETSc communicators
651480cf27aSJed Brown   */
652480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelCounter,&Petsc_Counter_keyval,(void*)0);CHKERRQ(ierr);
653480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_InnerComm_keyval,(void*)0);CHKERRQ(ierr);
654480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_OuterComm_keyval,(void*)0);CHKERRQ(ierr);
655480cf27aSJed Brown 
656480cf27aSJed Brown   /*
657e8fb0fc0SBarry Smith      Build the options database
658e5c89e4eSSatish Balay   */
659e5c89e4eSSatish Balay   ierr = PetscOptionsInsert(argc,args,file);CHKERRQ(ierr);
660e5c89e4eSSatish Balay 
6616dc8fec2Sbcordonn 
662e5c89e4eSSatish Balay   /*
663e5c89e4eSSatish Balay      Print main application help message
664e5c89e4eSSatish Balay   */
665e5c89e4eSSatish Balay   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
666e5c89e4eSSatish Balay   if (help && flg) {
667e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,help);CHKERRQ(ierr);
668e5c89e4eSSatish Balay   }
669e5c89e4eSSatish Balay   ierr = PetscOptionsCheckInitial_Private();CHKERRQ(ierr);
670e5c89e4eSSatish Balay 
671e5c89e4eSSatish Balay   /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */
672a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
673e5c89e4eSSatish Balay   ierr = PetscLogBegin_Private();CHKERRQ(ierr);
674a9f03627SSatish Balay #endif
675e5c89e4eSSatish Balay 
676e5c89e4eSSatish Balay   /*
677e5c89e4eSSatish Balay      Load the dynamic libraries (on machines that support them), this registers all
678e5c89e4eSSatish Balay      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
679e5c89e4eSSatish Balay   */
680e5c89e4eSSatish Balay   ierr = PetscInitialize_DynamicLibraries();CHKERRQ(ierr);
681e5c89e4eSSatish Balay 
682e5c89e4eSSatish Balay   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
683ae15b995SBarry Smith   ierr = PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);CHKERRQ(ierr);
684e5c89e4eSSatish Balay   ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr);
685ae15b995SBarry Smith   ierr = PetscInfo1(0,"Running on machine: %s\n",hostname);CHKERRQ(ierr);
686e5c89e4eSSatish Balay 
687e5c89e4eSSatish Balay   ierr = PetscOptionsCheckInitial_Components();CHKERRQ(ierr);
688ef6c6fedSBoyana Norris   /* Check the options database for options related to the options database itself */
689ef6c6fedSBoyana Norris   ierr = PetscOptionsSetFromOptions();CHKERRQ(ierr);
690ef6c6fedSBoyana Norris 
691951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
692e39fd77fSBarry Smith   /*
693e39fd77fSBarry Smith       Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI
694e39fd77fSBarry Smith 
695e39fd77fSBarry Smith       Currently not used because it is not supported by MPICH.
696e39fd77fSBarry Smith   */
697e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
698708350f5SSatish Balay   ierr = MPI_Register_datarep((char *)"petsc",PetscDataRep_read_conv_fn,PetscDataRep_write_conv_fn,PetscDataRep_extent_fn,PETSC_NULL);CHKERRQ(ierr);
699e39fd77fSBarry Smith #endif
700951e3c8eSBarry Smith #endif
701e39fd77fSBarry Smith 
702793721a6SBarry Smith   ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_spawn_size",&nodesize,&flg);CHKERRQ(ierr);
703793721a6SBarry Smith   if (flg) {
70423464e94SBarry Smith #if defined(PETSC_HAVE_MPI_COMM_SPAWN)
7059505b675SBarry Smith     ierr = PetscOpenMPSpawn((PetscMPIInt) nodesize);CHKERRQ(ierr); /* worker nodes never return from here; they go directly to PetscEnd() */
70623464e94SBarry Smith #else
707e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"PETSc built without MPI 2 (MPI_Comm_spawn) support, use -openmp_merge_size instead");
70823464e94SBarry Smith #endif
709793721a6SBarry Smith   } else {
71023464e94SBarry Smith     ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_merge_size",&nodesize,&flg);CHKERRQ(ierr);
7118002f1cdSBarry Smith     if (flg) {
7129505b675SBarry Smith       ierr = PetscOpenMPMerge((PetscMPIInt) nodesize,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7139505b675SBarry Smith       if (PetscOpenMPWorker) { /* if worker then never enter user code */
7149505b675SBarry Smith         ierr = PetscEnd();
7159505b675SBarry Smith       }
7168002f1cdSBarry Smith     }
717793721a6SBarry Smith   }
71890d69ab7SBarry Smith   flg  = PETSC_FALSE;
719acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-python",&flg,PETSC_NULL);CHKERRQ(ierr);
720192daf7cSBarry Smith   if (flg) {ierr = PetscPythonInitialize(PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
721e5c89e4eSSatish Balay 
72235d88935SVictor Minden #if defined(PETSC_HAVE_CUDA)
7232f947c57SVictor Minden   cublasInit();
7243e39abd9SVictor Minden #endif
72592e62aa6SBarry Smith 
72692e62aa6SBarry Smith #if defined(PETSC_HAVE_AMS)
727c457296dSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-ams_publish_objects",&flg);CHKERRQ(ierr);
72892e62aa6SBarry Smith   if (flg) {
72992e62aa6SBarry Smith     PetscAMSPublishAll = PETSC_TRUE;
73092e62aa6SBarry Smith   }
73192e62aa6SBarry Smith #endif
73292e62aa6SBarry Smith 
733301d30feSBarry Smith   /*
734301d30feSBarry Smith       Once we are completedly initialized then we can set this variables
735301d30feSBarry Smith   */
736301d30feSBarry Smith   PetscInitializeCalled = PETSC_TRUE;
737301d30feSBarry Smith   PetscFunctionReturn(0);
738e5c89e4eSSatish Balay }
739e5c89e4eSSatish Balay 
740*2eff7a51SBarry Smith extern PetscObject *PetscObjects;
741*2eff7a51SBarry Smith extern PetscInt    PetscObjectsCounts, PetscObjectsMaxCounts;
742e5c89e4eSSatish Balay 
743e5c89e4eSSatish Balay #undef __FUNCT__
744e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize"
745e5c89e4eSSatish Balay /*@C
746e5c89e4eSSatish Balay    PetscFinalize - Checks for options to be called at the conclusion
747e5c89e4eSSatish Balay    of the program. MPI_Finalize() is called only if the user had not
748e5c89e4eSSatish Balay    called MPI_Init() before calling PetscInitialize().
749e5c89e4eSSatish Balay 
750e5c89e4eSSatish Balay    Collective on PETSC_COMM_WORLD
751e5c89e4eSSatish Balay 
752e5c89e4eSSatish Balay    Options Database Keys:
75388c29154SBarry Smith +  -options_table - Calls PetscOptionsView()
754e5c89e4eSSatish Balay .  -options_left - Prints unused options that remain in the database
755e5c89e4eSSatish Balay .  -options_left no - Does not print unused options that remain in the database
756e5c89e4eSSatish Balay .  -mpidump - Calls PetscMPIDump()
757e5c89e4eSSatish Balay .  -malloc_dump - Calls PetscMallocDump()
758e5c89e4eSSatish Balay .  -malloc_info - Prints total memory usage
759e5c89e4eSSatish Balay -  -malloc_log - Prints summary of memory usage
760e5c89e4eSSatish Balay 
761e5c89e4eSSatish Balay    Options Database Keys for Profiling:
7620598bfebSBarry Smith    See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details.
763e5c89e4eSSatish Balay +  -log_summary [filename] - Prints summary of flop and timing
764e5c89e4eSSatish Balay         information to screen. If the filename is specified the
76591eabc43SBarry Smith         summary is written to the file.  See PetscLogView().
766ff5bc46bSBarry Smith .  -log_summary_python [filename] - Prints data on of flop and timing usage to a file or screen.
76791eabc43SBarry Smith         See PetscLogPrintSViewPython().
768e5c89e4eSSatish Balay .  -log_all [filename] - Logs extensive profiling information
769ff5bc46bSBarry Smith         See PetscLogDump().
770ff5bc46bSBarry Smith .  -log [filename] - Logs basic profiline information  See PetscLogDump().
771e5c89e4eSSatish Balay .  -log_sync - Log the synchronization in scatters, inner products
772e5c89e4eSSatish Balay         and norms
773e5c89e4eSSatish Balay -  -log_mpe [filename] - Creates a logfile viewable by the
774e5c89e4eSSatish Balay       utility Upshot/Nupshot (in MPICH distribution)
775e5c89e4eSSatish Balay 
776e5c89e4eSSatish Balay    Level: beginner
777e5c89e4eSSatish Balay 
778e5c89e4eSSatish Balay    Note:
779e5c89e4eSSatish Balay    See PetscInitialize() for more general runtime options.
780e5c89e4eSSatish Balay 
78188c29154SBarry Smith .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
782e5c89e4eSSatish Balay @*/
7837087cfbeSBarry Smith PetscErrorCode  PetscFinalize(void)
784e5c89e4eSSatish Balay {
785e5c89e4eSSatish Balay   PetscErrorCode ierr;
786e5c89e4eSSatish Balay   PetscMPIInt    rank;
787*2eff7a51SBarry Smith   PetscInt       i,nopt;
788ace3abfcSBarry Smith   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
789d5649816SBarry Smith #if defined(PETSC_HAVE_AMS)
790ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
791d5649816SBarry Smith #endif
79210463e74SBarry Smith #if defined(PETSC_USE_LOG)
79310463e74SBarry Smith   char           mname[PETSC_MAX_PATH_LEN];
79410463e74SBarry Smith #endif
795e5c89e4eSSatish Balay 
796e5c89e4eSSatish Balay   PetscFunctionBegin;
797e5c89e4eSSatish Balay 
798e5c89e4eSSatish Balay   if (!PetscInitializeCalled) {
7994b09e917SBarry Smith     printf("PetscInitialize() must be called before PetscFinalize()\n");
8004b09e917SBarry Smith     PetscFunctionReturn(PETSC_ERR_ARG_WRONGSTATE);
801e5c89e4eSSatish Balay   }
802b022a5c1SBarry Smith   ierr = PetscInfo(PETSC_NULL,"PetscFinalize() called\n");
803b022a5c1SBarry Smith 
804d5649816SBarry Smith #if defined(PETSC_HAVE_AMS)
805acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-options_gui",&flg,PETSC_NULL);CHKERRQ(ierr);
806d5649816SBarry Smith   if (flg) {
807d5649816SBarry Smith     ierr = PetscOptionsAMSDestroy();CHKERRQ(ierr);
808d5649816SBarry Smith   }
809d5649816SBarry Smith #endif
810d5649816SBarry Smith 
8118002f1cdSBarry Smith   ierr = PetscOpenMPFinalize();CHKERRQ(ierr);
8128002f1cdSBarry Smith 
813acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-python",&flg1,PETSC_NULL);CHKERRQ(ierr);
814192daf7cSBarry Smith   if (flg1) {ierr = PetscPythonFinalize();CHKERRQ(ierr);}
815192daf7cSBarry Smith 
816e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
817acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-malloc_info",&flg2,PETSC_NULL);CHKERRQ(ierr);
818e5c89e4eSSatish Balay   if (!flg2) {
81990d69ab7SBarry Smith     flg2 = PETSC_FALSE;
820acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(PETSC_NULL,"-memory_info",&flg2,PETSC_NULL);CHKERRQ(ierr);
821e5c89e4eSSatish Balay   }
822e5c89e4eSSatish Balay   if (flg2) {
823e5c89e4eSSatish Balay     ierr = PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");CHKERRQ(ierr);
824e5c89e4eSSatish Balay   }
825e5c89e4eSSatish Balay 
826e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
82790d69ab7SBarry Smith   flg1 = PETSC_FALSE;
828acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-get_total_flops",&flg1,PETSC_NULL);CHKERRQ(ierr);
829e5c89e4eSSatish Balay   if (flg1) {
830e5c89e4eSSatish Balay     PetscLogDouble flops = 0;
831e5c89e4eSSatish Balay     ierr = MPI_Reduce(&_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
832e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);CHKERRQ(ierr);
833e5c89e4eSSatish Balay   }
834e5c89e4eSSatish Balay #endif
835e5c89e4eSSatish Balay 
836e5c89e4eSSatish Balay 
837e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
838e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE)
839e5c89e4eSSatish Balay   mname[0] = 0;
840e5c89e4eSSatish Balay   ierr = PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
841e5c89e4eSSatish Balay   if (flg1){
842e5c89e4eSSatish Balay     if (mname[0]) {ierr = PetscLogMPEDump(mname);CHKERRQ(ierr);}
843e5c89e4eSSatish Balay     else          {ierr = PetscLogMPEDump(0);CHKERRQ(ierr);}
844e5c89e4eSSatish Balay   }
845e5c89e4eSSatish Balay #endif
846e5c89e4eSSatish Balay   mname[0] = 0;
847e5c89e4eSSatish Balay   ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
848e5c89e4eSSatish Balay   if (flg1) {
84991eabc43SBarry Smith     PetscViewer viewer;
85091eabc43SBarry Smith     if (mname[0])  {
85191eabc43SBarry Smith       ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);CHKERRQ(ierr);
85291eabc43SBarry Smith       ierr = PetscLogView(viewer);CHKERRQ(ierr);
85391eabc43SBarry Smith       ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
85433f85c2fSBarry Smith     } else {
85533f85c2fSBarry Smith       viewer = PETSC_VIEWER_STDOUT_WORLD;
85633f85c2fSBarry Smith       ierr = PetscLogView(viewer);CHKERRQ(ierr);
85733f85c2fSBarry Smith     }
858e5c89e4eSSatish Balay   }
859e5c89e4eSSatish Balay 
860ff5bc46bSBarry Smith   mname[0] = 0;
861ff5bc46bSBarry Smith   ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary_python",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
862ff5bc46bSBarry Smith   if (flg1) {
863ff5bc46bSBarry Smith     PetscViewer viewer;
864ff5bc46bSBarry Smith     if (mname[0])  {
865ff5bc46bSBarry Smith       ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);CHKERRQ(ierr);
86691eabc43SBarry Smith       ierr = PetscLogViewPython(viewer);CHKERRQ(ierr);
867ff5bc46bSBarry Smith       ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
86833f85c2fSBarry Smith     } else {
86933f85c2fSBarry Smith       viewer = PETSC_VIEWER_STDOUT_WORLD;
87033f85c2fSBarry Smith       ierr = PetscLogViewPython(viewer);CHKERRQ(ierr);
87133f85c2fSBarry Smith     }
872ff5bc46bSBarry Smith   }
873ff5bc46bSBarry Smith 
87478392ef1SBarry Smith   ierr = PetscOptionsGetString(PETSC_NULL,"-log_detailed",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
87578392ef1SBarry Smith   if (flg1) {
87678392ef1SBarry Smith     if (mname[0])  {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);}
87778392ef1SBarry Smith     else           {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,0);CHKERRQ(ierr);}
87878392ef1SBarry Smith   }
87978392ef1SBarry Smith 
880e5c89e4eSSatish Balay   mname[0] = 0;
881e5c89e4eSSatish Balay   ierr = PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
882e5c89e4eSSatish Balay   ierr = PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);CHKERRQ(ierr);
883e5c89e4eSSatish Balay   if (flg1 || flg2){
884e5c89e4eSSatish Balay     if (mname[0]) PetscLogDump(mname);
885e5c89e4eSSatish Balay     else          PetscLogDump(0);
886e5c89e4eSSatish Balay   }
887e5c89e4eSSatish Balay #endif
88810463e74SBarry Smith 
88933f85c2fSBarry Smith #if defined(PETSC_USE_DEBUG) && !defined(PETSC_USE_PTHREAD)
89033f85c2fSBarry Smith   if (PetscStackActive) {
89133f85c2fSBarry Smith     ierr = PetscStackDestroy();CHKERRQ(ierr);
89233f85c2fSBarry Smith   }
89333f85c2fSBarry Smith #endif
89410463e74SBarry Smith 
89590d69ab7SBarry Smith   flg1 = PETSC_FALSE;
896acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-no_signal_handler",&flg1,PETSC_NULL);CHKERRQ(ierr);
897e5c89e4eSSatish Balay   if (!flg1) { ierr = PetscPopSignalHandler();CHKERRQ(ierr);}
89890d69ab7SBarry Smith   flg1 = PETSC_FALSE;
899acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-mpidump",&flg1,PETSC_NULL);CHKERRQ(ierr);
900e5c89e4eSSatish Balay   if (flg1) {
901e5c89e4eSSatish Balay     ierr = PetscMPIDump(stdout);CHKERRQ(ierr);
902e5c89e4eSSatish Balay   }
90390d69ab7SBarry Smith   flg1 = PETSC_FALSE;
90490d69ab7SBarry Smith   flg2 = PETSC_FALSE;
9058bb29257SSatish Balay   /* preemptive call to avoid listing this option in options table as unused */
9068bb29257SSatish Balay   ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);CHKERRQ(ierr);
907acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-options_table",&flg2,PETSC_NULL);CHKERRQ(ierr);
908e4c476e2SSatish Balay 
909e5c89e4eSSatish Balay   if (flg2) {
91088c29154SBarry Smith     if (!rank) {ierr = PetscOptionsView(PETSC_NULL);CHKERRQ(ierr);}
911e5c89e4eSSatish Balay   }
912e5c89e4eSSatish Balay 
913e5c89e4eSSatish Balay   /* to prevent PETSc -options_left from warning */
914cb9801acSJed Brown   ierr = PetscOptionsHasName(PETSC_NULL,"-nox",&flg1);CHKERRQ(ierr);
915cb9801acSJed Brown   ierr = PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr);
916e5c89e4eSSatish Balay 
917f43cc0c9SSatish Balay   if (!PetscOpenMPWorker) { /* worker processes skip this because they do not usually process options */
91833fc4174SSatish Balay     flg3 = PETSC_FALSE; /* default value is required */
919acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(PETSC_NULL,"-options_left",&flg3,&flg1);CHKERRQ(ierr);
920e5c89e4eSSatish Balay     ierr = PetscOptionsAllUsed(&nopt);CHKERRQ(ierr);
921e5c89e4eSSatish Balay     if (flg3) {
922e5c89e4eSSatish Balay       if (!flg2) { /* have not yet printed the options */
92333f85c2fSBarry Smith 	ierr = PetscOptionsView(PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);
924e5c89e4eSSatish Balay       }
925e5c89e4eSSatish Balay       if (!nopt) {
926e5c89e4eSSatish Balay 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");CHKERRQ(ierr);
927e5c89e4eSSatish Balay       } else if (nopt == 1) {
928e5c89e4eSSatish Balay 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");CHKERRQ(ierr);
929e5c89e4eSSatish Balay       } else {
930e5c89e4eSSatish Balay 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There are %d unused database options. They are:\n",nopt);CHKERRQ(ierr);
931e5c89e4eSSatish Balay       }
932e5c89e4eSSatish Balay     }
933e5c89e4eSSatish Balay #if defined(PETSC_USE_DEBUG)
934da8b8a77SBarry Smith     if (nopt && !flg3 && !flg1) {
935e5c89e4eSSatish Balay       ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");CHKERRQ(ierr);
936e5c89e4eSSatish Balay       ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");CHKERRQ(ierr);
937e5c89e4eSSatish Balay       ierr = PetscOptionsLeft();CHKERRQ(ierr);
938e5c89e4eSSatish Balay     } else if (nopt && flg3) {
939e5c89e4eSSatish Balay #else
940e5c89e4eSSatish Balay     if (nopt && flg3) {
941e5c89e4eSSatish Balay #endif
942e5c89e4eSSatish Balay       ierr = PetscOptionsLeft();CHKERRQ(ierr);
943e5c89e4eSSatish Balay     }
944931f367cSBarry Smith   }
945e5c89e4eSSatish Balay 
94610463e74SBarry Smith   /*
94733f85c2fSBarry Smith      Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_().
94833f85c2fSBarry Smith   */
94933f85c2fSBarry Smith   ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr);
95033f85c2fSBarry Smith 
951*2eff7a51SBarry Smith   /*
952*2eff7a51SBarry Smith        Free all objects the user forgot to free
953*2eff7a51SBarry Smith    */
954*2eff7a51SBarry Smith   for (i=0; i<PetscObjectsMaxCounts; i++) {
955*2eff7a51SBarry Smith     if (PetscObjects[i]) {
956*2eff7a51SBarry Smith       ierr = PetscObjectDestroy(PetscObjects[i]);CHKERRQ(ierr);
957*2eff7a51SBarry Smith     }
958*2eff7a51SBarry Smith   }
959*2eff7a51SBarry Smith 
960*2eff7a51SBarry Smith 
96133f85c2fSBarry Smith #if defined(PETSC_USE_LOG)
96233f85c2fSBarry Smith   ierr = PetscLogDestroy();CHKERRQ(ierr);
96333f85c2fSBarry Smith #endif
96433f85c2fSBarry Smith 
96533f85c2fSBarry Smith   /*
96633f85c2fSBarry Smith        Free all the registered create functions, such as KSPList, VecList, SNESList, etc
96733f85c2fSBarry Smith   */
96833f85c2fSBarry Smith   ierr = PetscFListDestroyAll();CHKERRQ(ierr);
96933f85c2fSBarry Smith 
97033f85c2fSBarry Smith   /*
97133f85c2fSBarry Smith      Destroy any packages that registered a finalize
97233f85c2fSBarry Smith   */
97333f85c2fSBarry Smith   ierr = PetscRegisterFinalizeAll();CHKERRQ(ierr);
97433f85c2fSBarry Smith 
97533f85c2fSBarry Smith   /*
97610463e74SBarry Smith      Destroy all the function registration lists created
97710463e74SBarry Smith   */
97810463e74SBarry Smith   ierr = PetscFinalize_DynamicLibraries();CHKERRQ(ierr);
97910463e74SBarry Smith 
9804028d114SSatish Balay   if (petsc_history) {
981f3dea69dSBarry Smith     ierr = PetscCloseHistoryFile(&petsc_history);CHKERRQ(ierr);
982e5c89e4eSSatish Balay     petsc_history = 0;
983e5c89e4eSSatish Balay   }
984e5c89e4eSSatish Balay 
9856cf91177SBarry Smith   ierr = PetscInfoAllow(PETSC_FALSE,PETSC_NULL);CHKERRQ(ierr);
986e5c89e4eSSatish Balay 
9878bb29257SSatish Balay   {
988e5c89e4eSSatish Balay     char fname[PETSC_MAX_PATH_LEN];
989e5c89e4eSSatish Balay     FILE *fd;
990ed9cf6e9SBarry Smith     int  err;
991e5c89e4eSSatish Balay 
992e5c89e4eSSatish Balay     fname[0] = 0;
993e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_dump",fname,250,&flg1);CHKERRQ(ierr);
994e5c89e4eSSatish Balay     if (flg1 && fname[0]) {
995e5c89e4eSSatish Balay       char sname[PETSC_MAX_PATH_LEN];
996e5c89e4eSSatish Balay 
997e5c89e4eSSatish Balay       sprintf(sname,"%s_%d",fname,rank);
998e32f2f54SBarry Smith       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
999e5c89e4eSSatish Balay       ierr = PetscMallocDump(fd);CHKERRQ(ierr);
1000ed9cf6e9SBarry Smith       err = fclose(fd);
1001e32f2f54SBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
10028bb29257SSatish Balay     } else if (flg1) {
1003e5c89e4eSSatish Balay       MPI_Comm local_comm;
1004e5c89e4eSSatish Balay 
1005e5c89e4eSSatish Balay       ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr);
1006e5c89e4eSSatish Balay       ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr);
1007e5c89e4eSSatish Balay         ierr = PetscMallocDump(stdout);CHKERRQ(ierr);
1008e5c89e4eSSatish Balay       ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr);
1009e5c89e4eSSatish Balay       ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr);
1010e5c89e4eSSatish Balay     }
1011e5c89e4eSSatish Balay   }
10128bb29257SSatish Balay   {
1013e5c89e4eSSatish Balay     char fname[PETSC_MAX_PATH_LEN];
1014e5c89e4eSSatish Balay     FILE *fd;
1015e5c89e4eSSatish Balay 
1016e5c89e4eSSatish Balay     fname[0] = 0;
1017e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_log",fname,250,&flg1);CHKERRQ(ierr);
1018e5c89e4eSSatish Balay     if (flg1 && fname[0]) {
1019e5c89e4eSSatish Balay       char sname[PETSC_MAX_PATH_LEN];
1020ed9cf6e9SBarry Smith       int  err;
1021e5c89e4eSSatish Balay 
1022e5c89e4eSSatish Balay       sprintf(sname,"%s_%d",fname,rank);
1023e32f2f54SBarry Smith       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
1024e5c89e4eSSatish Balay       ierr = PetscMallocDumpLog(fd);CHKERRQ(ierr);
1025ed9cf6e9SBarry Smith       err = fclose(fd);
1026e32f2f54SBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
10278bb29257SSatish Balay     } else if (flg1) {
1028e5c89e4eSSatish Balay       ierr = PetscMallocDumpLog(stdout);CHKERRQ(ierr);
1029e5c89e4eSSatish Balay     }
1030e5c89e4eSSatish Balay   }
1031e5c89e4eSSatish Balay   /* Can be destroyed only after all the options are used */
1032e5c89e4eSSatish Balay   ierr = PetscOptionsDestroy();CHKERRQ(ierr);
1033e5c89e4eSSatish Balay 
1034e5c89e4eSSatish Balay   PetscGlobalArgc = 0;
1035e5c89e4eSSatish Balay   PetscGlobalArgs = 0;
1036e5c89e4eSSatish Balay 
1037e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
10382c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
103906a205a8SBarry Smith   ierr = MPI_Op_free(&MPIU_SUM);CHKERRQ(ierr);
10402c876bd9SBarry Smith   ierr = MPI_Type_free(&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
1041a83b8d76SBarry Smith   ierr = MPI_Type_free(&MPI_C_COMPLEX);CHKERRQ(ierr);
10422c876bd9SBarry Smith #endif
1043e5c89e4eSSatish Balay #endif
1044e5c89e4eSSatish Balay   ierr = MPI_Type_free(&MPIU_2SCALAR);CHKERRQ(ierr);
1045e5c89e4eSSatish Balay   ierr = MPI_Type_free(&MPIU_2INT);CHKERRQ(ierr);
1046e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscMaxSum_Op);CHKERRQ(ierr);
1047e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscADMax_Op);CHKERRQ(ierr);
1048e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscADMin_Op);CHKERRQ(ierr);
1049e5c89e4eSSatish Balay 
1050480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_Counter_keyval);CHKERRQ(ierr);
1051480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_InnerComm_keyval);CHKERRQ(ierr);
1052480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_OuterComm_keyval);CHKERRQ(ierr);
1053480cf27aSJed Brown 
1054ae15b995SBarry Smith   ierr = PetscInfo(0,"PETSc successfully ended!\n");CHKERRQ(ierr);
1055e5c89e4eSSatish Balay   if (PetscBeganMPI) {
105699608316SBarry Smith #if defined(PETSC_HAVE_MPI_FINALIZED)
105799b1327fSBarry Smith     PetscMPIInt flag;
105899b1327fSBarry Smith     ierr = MPI_Finalized(&flag);CHKERRQ(ierr);
1059e32f2f54SBarry Smith     if (flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()");
106099608316SBarry Smith #endif
1061e5c89e4eSSatish Balay     ierr = MPI_Finalize();CHKERRQ(ierr);
1062e5c89e4eSSatish Balay   }
1063e5c89e4eSSatish Balay 
10649c4c166aSBarry Smith   if (PETSC_ZOPEFD){
106522b84c2fSbcordonn     if (PETSC_ZOPEFD != PETSC_STDOUT) fprintf(PETSC_ZOPEFD, "<<<end>>>");
10669c4c166aSBarry Smith     else fprintf(PETSC_STDOUT, "<<<end>>>");
10679c4c166aSBarry Smith   }
106836186564Sbcordonn 
106935d88935SVictor Minden #if defined(PETSC_HAVE_CUDA)
10702f947c57SVictor Minden   cublasShutdown();
1071440a5bbfSVictor Minden #endif
1072e5c89e4eSSatish Balay /*
1073e5c89e4eSSatish Balay 
1074e5c89e4eSSatish Balay      Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because
1075e5c89e4eSSatish Balay    the communicator has some outstanding requests on it. Specifically if the
1076e5c89e4eSSatish Balay    flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See
1077e5c89e4eSSatish Balay    src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate()
1078e5c89e4eSSatish Balay    is never freed as it should be. Thus one may obtain messages of the form
10790e5e90baSSatish Balay    [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the
1080e5c89e4eSSatish Balay    memory was not freed.
1081e5c89e4eSSatish Balay 
1082e5c89e4eSSatish Balay */
10831d1a0024SBarry Smith   ierr = PetscMallocClear();CHKERRQ(ierr);
1084e5c89e4eSSatish Balay   PetscInitializeCalled = PETSC_FALSE;
1085e5c89e4eSSatish Balay   PetscFinalizeCalled   = PETSC_TRUE;
1086e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
1087e5c89e4eSSatish Balay }
1088e5c89e4eSSatish Balay 
1089