xref: /petsc/src/sys/objects/pinit.c (revision f0865b086e8f4c34d750388762e5421e85fbf8f1)
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 
74e5c89e4eSSatish Balay #undef __FUNCT__
7572a42c3cSBarry Smith #define __FUNCT__ "PetscInitializeNonPointers"
7672a42c3cSBarry Smith /*@C
7772a42c3cSBarry Smith       PetscInitializeNonPointers - Calls PetscInitialize() from C/C++ without the pointers to argc and args
7872a42c3cSBarry Smith 
7972a42c3cSBarry Smith    Collective
8072a42c3cSBarry Smith 
8172a42c3cSBarry Smith    Level: advanced
8272a42c3cSBarry Smith 
8372a42c3cSBarry Smith .seealso: PetscInitialize(), PetscInitializeFortran(), PetscInitializeNoArguments()
8472a42c3cSBarry Smith @*/
857087cfbeSBarry Smith PetscErrorCode  PetscInitializeNonPointers(int argc,char **args,const char *filename,const char *help)
8672a42c3cSBarry Smith {
8772a42c3cSBarry Smith   PetscErrorCode ierr;
8872a42c3cSBarry Smith   int            myargc = argc;
8972a42c3cSBarry Smith   char           **myargs = args;
9072a42c3cSBarry Smith 
9172a42c3cSBarry Smith   PetscFunctionBegin;
9272a42c3cSBarry Smith   ierr = PetscInitialize(&myargc,&myargs,filename,help);
9372a42c3cSBarry Smith   PetscFunctionReturn(ierr);
9472a42c3cSBarry Smith }
9572a42c3cSBarry Smith 
9672a42c3cSBarry Smith #undef __FUNCT__
97*f0865b08SBarry Smith #define __FUNCT__ "PetscGetPETSC_COMM_SELF"
98*f0865b08SBarry Smith /*
99*f0865b08SBarry Smith       Used by Matlab interface to get communicator
100*f0865b08SBarry Smith */
101*f0865b08SBarry Smith PetscErrorCode  PetscGetPETSC_COMM_SELF(MPI_Comm *comm)
102*f0865b08SBarry Smith {
103*f0865b08SBarry Smith   PetscFunctionBegin;
104*f0865b08SBarry Smith   *comm = PETSC_COMM_SELF;
105*f0865b08SBarry Smith   PetscFunctionReturn(0);
106*f0865b08SBarry Smith }
107*f0865b08SBarry Smith 
108*f0865b08SBarry Smith #undef __FUNCT__
109e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitializeNoArguments"
110e5c89e4eSSatish Balay /*@C
111e5c89e4eSSatish Balay       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
112e5c89e4eSSatish Balay         the command line arguments.
113e5c89e4eSSatish Balay 
114e5c89e4eSSatish Balay    Collective
115e5c89e4eSSatish Balay 
116e5c89e4eSSatish Balay    Level: advanced
117e5c89e4eSSatish Balay 
118e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeFortran()
119e5c89e4eSSatish Balay @*/
1207087cfbeSBarry Smith PetscErrorCode  PetscInitializeNoArguments(void)
121e5c89e4eSSatish Balay {
122e5c89e4eSSatish Balay   PetscErrorCode ierr;
123e5c89e4eSSatish Balay   int            argc = 0;
124e5c89e4eSSatish Balay   char           **args = 0;
125e5c89e4eSSatish Balay 
126e5c89e4eSSatish Balay   PetscFunctionBegin;
127e5c89e4eSSatish Balay   ierr = PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL);
128e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
129e5c89e4eSSatish Balay }
130e5c89e4eSSatish Balay 
131e5c89e4eSSatish Balay #undef __FUNCT__
132e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialized"
133e5c89e4eSSatish Balay /*@
134e5c89e4eSSatish Balay       PetscInitialized - Determine whether PETSc is initialized.
135e5c89e4eSSatish Balay 
1366dc8fec2Sbcordonn 7   Level: beginner
137e5c89e4eSSatish Balay 
138e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
139e5c89e4eSSatish Balay @*/
1407087cfbeSBarry Smith PetscErrorCode  PetscInitialized(PetscBool  *isInitialized)
141e5c89e4eSSatish Balay {
142e5c89e4eSSatish Balay   PetscFunctionBegin;
143e5c89e4eSSatish Balay   PetscValidPointer(isInitialized, 1);
144e5c89e4eSSatish Balay   *isInitialized = PetscInitializeCalled;
145e5c89e4eSSatish Balay   PetscFunctionReturn(0);
146e5c89e4eSSatish Balay }
147e5c89e4eSSatish Balay 
148e5c89e4eSSatish Balay #undef __FUNCT__
149e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalized"
150e5c89e4eSSatish Balay /*@
151e5c89e4eSSatish Balay       PetscFinalized - Determine whether PetscFinalize() has been called yet
152e5c89e4eSSatish Balay 
153e5c89e4eSSatish Balay    Level: developer
154e5c89e4eSSatish Balay 
155e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
156e5c89e4eSSatish Balay @*/
1577087cfbeSBarry Smith PetscErrorCode  PetscFinalized(PetscBool  *isFinalized)
158e5c89e4eSSatish Balay {
159e5c89e4eSSatish Balay   PetscFunctionBegin;
160e5c89e4eSSatish Balay   PetscValidPointer(isFinalized, 1);
161e5c89e4eSSatish Balay   *isFinalized = PetscFinalizeCalled;
162e5c89e4eSSatish Balay   PetscFunctionReturn(0);
163e5c89e4eSSatish Balay }
164e5c89e4eSSatish Balay 
16509573ac7SBarry Smith extern PetscErrorCode        PetscOptionsCheckInitial_Private(void);
166ace3abfcSBarry Smith extern PetscBool  PetscBeganMPI;
167e5c89e4eSSatish Balay 
168e5c89e4eSSatish Balay /*
169e5c89e4eSSatish Balay        This function is the MPI reduction operation used to compute the sum of the
170e5c89e4eSSatish Balay    first half of the datatype and the max of the second half.
171e5c89e4eSSatish Balay */
172e5c89e4eSSatish Balay MPI_Op PetscMaxSum_Op = 0;
173e5c89e4eSSatish Balay 
174e5c89e4eSSatish Balay EXTERN_C_BEGIN
175e5c89e4eSSatish Balay #undef __FUNCT__
176e5c89e4eSSatish Balay #define __FUNCT__ "PetscMaxSum_Local"
1777087cfbeSBarry Smith void  MPIAPI PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
178e5c89e4eSSatish Balay {
179e5c89e4eSSatish Balay   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;
180e5c89e4eSSatish Balay 
181e5c89e4eSSatish Balay   PetscFunctionBegin;
182e5c89e4eSSatish Balay   if (*datatype != MPIU_2INT) {
183e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
184e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
185e5c89e4eSSatish Balay   }
186e5c89e4eSSatish Balay 
187e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
188e5c89e4eSSatish Balay     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
189e5c89e4eSSatish Balay     xout[2*i+1] += xin[2*i+1];
190e5c89e4eSSatish Balay   }
191812af9f3SBarry Smith   PetscFunctionReturnVoid();
192e5c89e4eSSatish Balay }
193e5c89e4eSSatish Balay EXTERN_C_END
194e5c89e4eSSatish Balay 
195e5c89e4eSSatish Balay /*
196e5c89e4eSSatish Balay     Returns the max of the first entry owned by this processor and the
197e5c89e4eSSatish Balay sum of the second entry.
198b693b147SBarry Smith 
199b693b147SBarry Smith     The reason nprocs[2*i] contains lengths nprocs[2*i+1] contains flag of 1 if length is nonzero
200b693b147SBarry Smith is so that the PetscMaxSum_Op() can set TWO values, if we passed in only nprocs[i] with lengths
201b693b147SBarry Smith there would be no place to store the both needed results.
202e5c89e4eSSatish Balay */
203e5c89e4eSSatish Balay #undef __FUNCT__
204e5c89e4eSSatish Balay #define __FUNCT__ "PetscMaxSum"
2057087cfbeSBarry Smith PetscErrorCode  PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum)
206e5c89e4eSSatish Balay {
207e5c89e4eSSatish Balay   PetscMPIInt    size,rank;
208e5c89e4eSSatish Balay   PetscInt       *work;
209e5c89e4eSSatish Balay   PetscErrorCode ierr;
210e5c89e4eSSatish Balay 
211e5c89e4eSSatish Balay   PetscFunctionBegin;
212e5c89e4eSSatish Balay   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
213e5c89e4eSSatish Balay   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
214e5c89e4eSSatish Balay   ierr   = PetscMalloc(2*size*sizeof(PetscInt),&work);CHKERRQ(ierr);
215e5c89e4eSSatish Balay   ierr   = MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
216e5c89e4eSSatish Balay   *max   = work[2*rank];
217e5c89e4eSSatish Balay   *sum   = work[2*rank+1];
218e5c89e4eSSatish Balay   ierr   = PetscFree(work);CHKERRQ(ierr);
219e5c89e4eSSatish Balay   PetscFunctionReturn(0);
220e5c89e4eSSatish Balay }
221e5c89e4eSSatish Balay 
222e5c89e4eSSatish Balay /* ----------------------------------------------------------------------------*/
2237087cfbeSBarry Smith MPI_Op  PetscADMax_Op = 0;
224e5c89e4eSSatish Balay 
225e5c89e4eSSatish Balay EXTERN_C_BEGIN
226e5c89e4eSSatish Balay #undef __FUNCT__
227e5c89e4eSSatish Balay #define __FUNCT__ "PetscADMax_Local"
2287087cfbeSBarry Smith void  MPIAPI PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
229e5c89e4eSSatish Balay {
230e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
231e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
232e5c89e4eSSatish Balay 
233e5c89e4eSSatish Balay   PetscFunctionBegin;
234e5c89e4eSSatish Balay   if (*datatype != MPIU_2SCALAR) {
235e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
236e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
237e5c89e4eSSatish Balay   }
238e5c89e4eSSatish Balay 
239e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
240e5c89e4eSSatish Balay     if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) {
241e5c89e4eSSatish Balay       xout[2*i]   = xin[2*i];
242e5c89e4eSSatish Balay       xout[2*i+1] = xin[2*i+1];
243e5c89e4eSSatish Balay     }
244e5c89e4eSSatish Balay   }
245812af9f3SBarry Smith   PetscFunctionReturnVoid();
246e5c89e4eSSatish Balay }
247e5c89e4eSSatish Balay EXTERN_C_END
248e5c89e4eSSatish Balay 
2497087cfbeSBarry Smith MPI_Op  PetscADMin_Op = 0;
250e5c89e4eSSatish Balay 
251e5c89e4eSSatish Balay EXTERN_C_BEGIN
252e5c89e4eSSatish Balay #undef __FUNCT__
253e5c89e4eSSatish Balay #define __FUNCT__ "PetscADMin_Local"
2547087cfbeSBarry Smith void  MPIAPI PetscADMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
255e5c89e4eSSatish Balay {
256e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
257e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
258e5c89e4eSSatish Balay 
259e5c89e4eSSatish Balay   PetscFunctionBegin;
260e5c89e4eSSatish Balay   if (*datatype != MPIU_2SCALAR) {
261e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
262e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
263e5c89e4eSSatish Balay   }
264e5c89e4eSSatish Balay 
265e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
266e5c89e4eSSatish Balay     if (PetscRealPart(xout[2*i]) > PetscRealPart(xin[2*i])) {
267e5c89e4eSSatish Balay       xout[2*i]   = xin[2*i];
268e5c89e4eSSatish Balay       xout[2*i+1] = xin[2*i+1];
269e5c89e4eSSatish Balay     }
270e5c89e4eSSatish Balay   }
271812af9f3SBarry Smith   PetscFunctionReturnVoid();
272e5c89e4eSSatish Balay }
273e5c89e4eSSatish Balay EXTERN_C_END
274e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
275e5c89e4eSSatish Balay 
276e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
2772c876bd9SBarry Smith 
2782c876bd9SBarry Smith /*
2792c876bd9SBarry Smith     This operation is only needed when using complex numbers with older MPI that does not support complex numbers
2802c876bd9SBarry Smith */
2812c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
28206a205a8SBarry Smith MPI_Op MPIU_SUM = 0;
283e5c89e4eSSatish Balay 
284e5c89e4eSSatish Balay EXTERN_C_BEGIN
285e5c89e4eSSatish Balay #undef __FUNCT__
286e5c89e4eSSatish Balay #define __FUNCT__ "PetscSum_Local"
2877087cfbeSBarry Smith void  PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
288e5c89e4eSSatish Balay {
289e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
290e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
291e5c89e4eSSatish Balay 
292e5c89e4eSSatish Balay   PetscFunctionBegin;
293e5c89e4eSSatish Balay   if (*datatype != MPIU_SCALAR) {
294e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_SCALAR data (i.e. double or complex) types");
295e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
296e5c89e4eSSatish Balay   }
297e5c89e4eSSatish Balay 
298e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
299e5c89e4eSSatish Balay     xout[i] += xin[i];
300e5c89e4eSSatish Balay   }
301812af9f3SBarry Smith   PetscFunctionReturnVoid();
302e5c89e4eSSatish Balay }
303e5c89e4eSSatish Balay EXTERN_C_END
304e5c89e4eSSatish Balay #endif
3052c876bd9SBarry Smith #endif
306e5c89e4eSSatish Balay 
307480cf27aSJed Brown EXTERN_C_BEGIN
308480cf27aSJed Brown #undef __FUNCT__
309480cf27aSJed Brown #define __FUNCT__ "Petsc_DelCounter"
310480cf27aSJed Brown /*
311480cf27aSJed Brown    Private routine to delete internal tag/name counter storage when a communicator is freed.
312480cf27aSJed Brown 
313480cf27aSJed Brown    This is called by MPI, not by users.
314480cf27aSJed Brown 
315480cf27aSJed Brown    Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
316480cf27aSJed Brown 
317480cf27aSJed Brown */
3187087cfbeSBarry Smith PetscMPIInt  MPIAPI Petsc_DelCounter(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
319480cf27aSJed Brown {
320480cf27aSJed Brown   PetscErrorCode ierr;
321480cf27aSJed Brown 
322480cf27aSJed Brown   PetscFunctionBegin;
323480cf27aSJed Brown   ierr = PetscInfo1(0,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
324480cf27aSJed Brown   ierr = PetscFree(count_val);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
325480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
326480cf27aSJed Brown }
327480cf27aSJed Brown EXTERN_C_END
328480cf27aSJed Brown 
329480cf27aSJed Brown EXTERN_C_BEGIN
330480cf27aSJed Brown #undef __FUNCT__
331480cf27aSJed Brown #define __FUNCT__ "Petsc_DelComm"
332480cf27aSJed Brown /*
333480cf27aSJed Brown   This does not actually free anything, it simply marks when a reference count to an internal MPI_Comm reaches zero and the
334480cf27aSJed Brown   the external MPI_Comm drops its reference to the internal MPI_Comm
335480cf27aSJed Brown 
336480cf27aSJed Brown   This is called by MPI, not by users.
337480cf27aSJed Brown 
338480cf27aSJed Brown   Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
339480cf27aSJed Brown 
340480cf27aSJed Brown */
3417087cfbeSBarry Smith PetscMPIInt  MPIAPI Petsc_DelComm(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
342480cf27aSJed Brown {
343480cf27aSJed Brown   PetscErrorCode ierr;
344480cf27aSJed Brown 
345480cf27aSJed Brown   PetscFunctionBegin;
346480cf27aSJed Brown   ierr = PetscInfo1(0,"Deleting PETSc communicator imbedded in a user MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
347480cf27aSJed Brown   /* actually don't delete anything because we cannot increase the reference count of the communicator anyways */
348480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
349480cf27aSJed Brown }
350480cf27aSJed Brown EXTERN_C_END
351480cf27aSJed Brown 
352951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
353e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
354e39fd77fSBarry Smith EXTERN_C_BEGIN
355e39fd77fSBarry Smith extern PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype,MPI_Aint*,void*);
356e39fd77fSBarry Smith extern PetscMPIInt PetscDataRep_read_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
357e39fd77fSBarry Smith extern PetscMPIInt PetscDataRep_write_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
358e39fd77fSBarry Smith EXTERN_C_END
359e39fd77fSBarry Smith #endif
360951e3c8eSBarry Smith #endif
361e39fd77fSBarry Smith 
3626ae9a8a6SBarry Smith int  PetscGlobalArgc   = 0;
3636ae9a8a6SBarry Smith char **PetscGlobalArgs = 0;
364e5c89e4eSSatish Balay 
365e5c89e4eSSatish Balay #undef __FUNCT__
366e5c89e4eSSatish Balay #define __FUNCT__ "PetscGetArgs"
367e5c89e4eSSatish Balay /*@C
368e5c89e4eSSatish Balay    PetscGetArgs - Allows you to access the raw command line arguments anywhere
369e5c89e4eSSatish Balay      after PetscInitialize() is called but before PetscFinalize().
370e5c89e4eSSatish Balay 
371e5c89e4eSSatish Balay    Not Collective
372e5c89e4eSSatish Balay 
373e5c89e4eSSatish Balay    Output Parameters:
374e5c89e4eSSatish Balay +  argc - count of number of command line arguments
375e5c89e4eSSatish Balay -  args - the command line arguments
376e5c89e4eSSatish Balay 
377e5c89e4eSSatish Balay    Level: intermediate
378e5c89e4eSSatish Balay 
379e5c89e4eSSatish Balay    Notes:
380e5c89e4eSSatish Balay       This is usually used to pass the command line arguments into other libraries
381e5c89e4eSSatish Balay    that are called internally deep in PETSc or the application.
382e5c89e4eSSatish Balay 
383f177e3b1SBarry Smith       The first argument contains the program name as is normal for C arguments.
384f177e3b1SBarry Smith 
385e5c89e4eSSatish Balay    Concepts: command line arguments
386e5c89e4eSSatish Balay 
387793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()
388e5c89e4eSSatish Balay 
389e5c89e4eSSatish Balay @*/
3907087cfbeSBarry Smith PetscErrorCode  PetscGetArgs(int *argc,char ***args)
391e5c89e4eSSatish Balay {
392e5c89e4eSSatish Balay   PetscFunctionBegin;
39317186662SBarry Smith   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
394e5c89e4eSSatish Balay   *argc = PetscGlobalArgc;
395e5c89e4eSSatish Balay   *args = PetscGlobalArgs;
396e5c89e4eSSatish Balay   PetscFunctionReturn(0);
397e5c89e4eSSatish Balay }
398e5c89e4eSSatish Balay 
399e5c89e4eSSatish Balay #undef __FUNCT__
400793721a6SBarry Smith #define __FUNCT__ "PetscGetArguments"
401793721a6SBarry Smith /*@C
402793721a6SBarry Smith    PetscGetArguments - Allows you to access the  command line arguments anywhere
403793721a6SBarry Smith      after PetscInitialize() is called but before PetscFinalize().
404793721a6SBarry Smith 
405793721a6SBarry Smith    Not Collective
406793721a6SBarry Smith 
407793721a6SBarry Smith    Output Parameters:
408793721a6SBarry Smith .  args - the command line arguments
409793721a6SBarry Smith 
410793721a6SBarry Smith    Level: intermediate
411793721a6SBarry Smith 
412793721a6SBarry Smith    Notes:
413793721a6SBarry Smith       This does NOT start with the program name and IS null terminated (final arg is void)
414793721a6SBarry Smith 
415793721a6SBarry Smith    Concepts: command line arguments
416793721a6SBarry Smith 
417793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()
418793721a6SBarry Smith 
419793721a6SBarry Smith @*/
4207087cfbeSBarry Smith PetscErrorCode  PetscGetArguments(char ***args)
421793721a6SBarry Smith {
422793721a6SBarry Smith   PetscInt       i,argc = PetscGlobalArgc;
423793721a6SBarry Smith   PetscErrorCode ierr;
424793721a6SBarry Smith 
425793721a6SBarry Smith   PetscFunctionBegin;
42617186662SBarry Smith   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
427717030eeSLisandro Dalcin   if (!argc) {*args = 0; PetscFunctionReturn(0);}
428793721a6SBarry Smith   ierr = PetscMalloc(argc*sizeof(char*),args);CHKERRQ(ierr);
429793721a6SBarry Smith   for (i=0; i<argc-1; i++) {
430793721a6SBarry Smith     ierr = PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);CHKERRQ(ierr);
431793721a6SBarry Smith   }
432793721a6SBarry Smith   (*args)[argc-1] = 0;
433793721a6SBarry Smith   PetscFunctionReturn(0);
434793721a6SBarry Smith }
435793721a6SBarry Smith 
436793721a6SBarry Smith #undef __FUNCT__
437793721a6SBarry Smith #define __FUNCT__ "PetscFreeArguments"
438793721a6SBarry Smith /*@C
439793721a6SBarry Smith    PetscFreeArguments - Frees the memory obtained with PetscGetArguments()
440793721a6SBarry Smith 
441793721a6SBarry Smith    Not Collective
442793721a6SBarry Smith 
443793721a6SBarry Smith    Output Parameters:
444793721a6SBarry Smith .  args - the command line arguments
445793721a6SBarry Smith 
446793721a6SBarry Smith    Level: intermediate
447793721a6SBarry Smith 
448793721a6SBarry Smith    Concepts: command line arguments
449793721a6SBarry Smith 
450793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()
451793721a6SBarry Smith 
452793721a6SBarry Smith @*/
4537087cfbeSBarry Smith PetscErrorCode  PetscFreeArguments(char **args)
454793721a6SBarry Smith {
455793721a6SBarry Smith   PetscInt       i = 0;
456793721a6SBarry Smith   PetscErrorCode ierr;
457793721a6SBarry Smith 
458793721a6SBarry Smith   PetscFunctionBegin;
459717030eeSLisandro Dalcin   if (!args) {PetscFunctionReturn(0);}
460793721a6SBarry Smith   while (args[i]) {
461793721a6SBarry Smith     ierr = PetscFree(args[i]);CHKERRQ(ierr);
462793721a6SBarry Smith     i++;
463793721a6SBarry Smith   }
464793721a6SBarry Smith   ierr = PetscFree(args);CHKERRQ(ierr);
465793721a6SBarry Smith   PetscFunctionReturn(0);
466793721a6SBarry Smith }
467793721a6SBarry Smith 
468793721a6SBarry Smith #undef __FUNCT__
469e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialize"
470e5c89e4eSSatish Balay /*@C
471e5c89e4eSSatish Balay    PetscInitialize - Initializes the PETSc database and MPI.
472e5c89e4eSSatish Balay    PetscInitialize() calls MPI_Init() if that has yet to be called,
473e5c89e4eSSatish Balay    so this routine should always be called near the beginning of
474e5c89e4eSSatish Balay    your program -- usually the very first line!
475e5c89e4eSSatish Balay 
476e5c89e4eSSatish Balay    Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set
477e5c89e4eSSatish Balay 
478e5c89e4eSSatish Balay    Input Parameters:
479e5c89e4eSSatish Balay +  argc - count of number of command line arguments
480e5c89e4eSSatish Balay .  args - the command line arguments
481fc2bca9aSBarry Smith .  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL to not check for
482fc2bca9aSBarry Smith           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
483e5c89e4eSSatish Balay -  help - [optional] Help message to print, use PETSC_NULL for no message
484e5c89e4eSSatish Balay 
48505827820SBarry Smith    If you wish PETSc code to run ONLY on a subcommunicator of MPI_COMM_WORLD, create that
48605827820SBarry Smith    communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize(). Thus if you are running a
48705827820SBarry Smith    four process job and two processes will run PETSc and have PetscInitialize() and PetscFinalize() and two process will not,
48805827820SBarry Smith    then do this. If ALL processes in the job are using PetscInitialize() and PetscFinalize() then you don't need to do this, even
48905827820SBarry Smith    if different subcommunicators of the job are doing different things with PETSc.
490e5c89e4eSSatish Balay 
491e5c89e4eSSatish Balay    Options Database Keys:
492e5c89e4eSSatish Balay +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
493e5c89e4eSSatish Balay .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
494e5c89e4eSSatish Balay .  -on_error_emacs <machinename> causes emacsclient to jump to error file
495b52f573bSBarry Smith .  -on_error_abort calls abort() when error detected (no traceback)
496e8fb0fc0SBarry Smith .  -on_error_mpiabort calls MPI_abort() when error detected
497e8fb0fc0SBarry Smith .  -error_output_stderr prints error messages to stderr instead of the default stdout
498e8fb0fc0SBarry Smith .  -error_output_none does not print the error messages (but handles errors in the same way as if this was not called)
499e5c89e4eSSatish Balay .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
500e5c89e4eSSatish Balay .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
501e5c89e4eSSatish Balay .  -stop_for_debugger - Print message on how to attach debugger manually to
502e5c89e4eSSatish Balay                         process and wait (-debugger_pause) seconds for attachment
5032fb0ec9aSBarry Smith .  -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries)
504e5c89e4eSSatish Balay .  -malloc no - Indicates not to use error-checking malloc
5052fb0ec9aSBarry Smith .  -malloc_debug - check for memory corruption at EVERY malloc or free
506e5c89e4eSSatish Balay .  -fp_trap - Stops on floating point exceptions (Note that on the
507e5c89e4eSSatish Balay               IBM RS6000 this slows code by at least a factor of 10.)
508e5c89e4eSSatish Balay .  -no_signal_handler - Indicates not to trap error signals
509e5c89e4eSSatish Balay .  -shared_tmp - indicates /tmp directory is shared by all processors
510e5c89e4eSSatish Balay .  -not_shared_tmp - each processor has own /tmp
511e5c89e4eSSatish Balay .  -tmp - alternative name of /tmp directory
512e5c89e4eSSatish Balay .  -get_total_flops - returns total flops done by all processors
513e5c89e4eSSatish Balay -  -memory_info - Print memory usage at end of run
514e5c89e4eSSatish Balay 
515e5c89e4eSSatish Balay    Options Database Keys for Profiling:
5160598bfebSBarry Smith    See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details.
517e5c89e4eSSatish Balay +  -log_trace [filename] - Print traces of all PETSc calls
518e5c89e4eSSatish Balay         to the screen (useful to determine where a program
519e5c89e4eSSatish Balay         hangs without running in the debugger).  See PetscLogTraceBegin().
5206cf91177SBarry Smith .  -info <optional filename> - Prints verbose information to the screen
5216cf91177SBarry Smith -  -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages
522e5c89e4eSSatish Balay 
523e5c89e4eSSatish Balay    Environmental Variables:
524e5c89e4eSSatish Balay +   PETSC_TMP - alternative tmp directory
525e5c89e4eSSatish Balay .   PETSC_SHARED_TMP - tmp is shared by all processes
526e5c89e4eSSatish Balay .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
527e5c89e4eSSatish Balay .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
528e5c89e4eSSatish Balay -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to
529e5c89e4eSSatish Balay 
530e5c89e4eSSatish Balay 
531e5c89e4eSSatish Balay    Level: beginner
532e5c89e4eSSatish Balay 
533e5c89e4eSSatish Balay    Notes:
534e5c89e4eSSatish Balay    If for some reason you must call MPI_Init() separately, call
535e5c89e4eSSatish Balay    it before PetscInitialize().
536e5c89e4eSSatish Balay 
537e5c89e4eSSatish Balay    Fortran Version:
538e5c89e4eSSatish Balay    In Fortran this routine has the format
539e5c89e4eSSatish Balay $       call PetscInitialize(file,ierr)
540e5c89e4eSSatish Balay 
541e5c89e4eSSatish Balay +   ierr - error return code
5423dae0d48SMatthew Knepley -  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL_CHARACTER to not check for
543fc2bca9aSBarry Smith           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
544e5c89e4eSSatish Balay 
545e5c89e4eSSatish Balay    Important Fortran Note:
546e5c89e4eSSatish Balay    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
547e5c89e4eSSatish Balay    null character string; you CANNOT just use PETSC_NULL as
5480598bfebSBarry Smith    in the C version. See the <a href="../../docs/manual.pdf">users manual</a> for details.
549e5c89e4eSSatish Balay 
55001cb0274SBarry Smith    If your main program is C but you call Fortran code that also uses PETSc you need to call PetscInitializeFortran() soon after
55101cb0274SBarry Smith    calling PetscInitialize().
552e5c89e4eSSatish Balay 
553e5c89e4eSSatish Balay    Concepts: initializing PETSc
554e5c89e4eSSatish Balay 
55501cb0274SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments()
556e5c89e4eSSatish Balay 
557e5c89e4eSSatish Balay @*/
5587087cfbeSBarry Smith PetscErrorCode  PetscInitialize(int *argc,char ***args,const char file[],const char help[])
559e5c89e4eSSatish Balay {
560e5c89e4eSSatish Balay   PetscErrorCode ierr;
561aa5bb8c0SSatish Balay   PetscMPIInt    flag, size;
562aa5bb8c0SSatish Balay   PetscInt       nodesize;
563ace3abfcSBarry Smith   PetscBool      flg;
564e5c89e4eSSatish Balay   char           hostname[256];
565e5c89e4eSSatish Balay 
566e5c89e4eSSatish Balay   PetscFunctionBegin;
567e5c89e4eSSatish Balay   if (PetscInitializeCalled) PetscFunctionReturn(0);
568e5c89e4eSSatish Balay 
569ae9b4142SLisandro Dalcin   /* these must be initialized in a routine, not as a constant declaration*/
570d89683f4Sbcordonn   PETSC_STDOUT = stdout;
571ae9b4142SLisandro Dalcin   PETSC_STDERR = stderr;
572e5c89e4eSSatish Balay 
573e5c89e4eSSatish Balay   ierr = PetscOptionsCreate();CHKERRQ(ierr);
574e5c89e4eSSatish Balay 
575e5c89e4eSSatish Balay   /*
576e5c89e4eSSatish Balay      We initialize the program name here (before MPI_Init()) because MPICH has a bug in
577e5c89e4eSSatish Balay      it that it sets args[0] on all processors to be args[0] on the first processor.
578e5c89e4eSSatish Balay   */
579e5c89e4eSSatish Balay   if (argc && *argc) {
580e5c89e4eSSatish Balay     ierr = PetscSetProgramName(**args);CHKERRQ(ierr);
581e5c89e4eSSatish Balay   } else {
582e5c89e4eSSatish Balay     ierr = PetscSetProgramName("Unknown Name");CHKERRQ(ierr);
583e5c89e4eSSatish Balay   }
584e5c89e4eSSatish Balay 
585e5c89e4eSSatish Balay   ierr = MPI_Initialized(&flag);CHKERRQ(ierr);
586e5c89e4eSSatish Balay   if (!flag) {
587e32f2f54SBarry 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");
588e5c89e4eSSatish Balay     ierr          = MPI_Init(argc,args);CHKERRQ(ierr);
589e5c89e4eSSatish Balay     PetscBeganMPI = PETSC_TRUE;
590e5c89e4eSSatish Balay   }
591e5c89e4eSSatish Balay   if (argc && args) {
592e5c89e4eSSatish Balay     PetscGlobalArgc = *argc;
593e5c89e4eSSatish Balay     PetscGlobalArgs = *args;
594e5c89e4eSSatish Balay   }
595e5c89e4eSSatish Balay   PetscFinalizeCalled   = PETSC_FALSE;
596e5c89e4eSSatish Balay 
597e8373e55SMatthew Knepley   if (PETSC_COMM_WORLD == MPI_COMM_NULL) {
598e5c89e4eSSatish Balay     PETSC_COMM_WORLD = MPI_COMM_WORLD;
599e5c89e4eSSatish Balay   }
600660746e0SBarry Smith   ierr = MPI_Errhandler_set(PETSC_COMM_WORLD,MPI_ERRORS_RETURN);CHKERRQ(ierr);
601e5c89e4eSSatish Balay 
602e5c89e4eSSatish Balay   /* Done after init due to a bug in MPICH-GM? */
603e5c89e4eSSatish Balay   ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr);
604e5c89e4eSSatish Balay 
605e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);CHKERRQ(ierr);
606e5c89e4eSSatish Balay   ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);CHKERRQ(ierr);
607e5c89e4eSSatish Balay 
608e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
609e5c89e4eSSatish Balay   /*
610e5c89e4eSSatish Balay      Initialized the global complex variable; this is because with
611e5c89e4eSSatish Balay      shared libraries the constructors for global variables
612e5c89e4eSSatish Balay      are not called; at least on IRIX.
613e5c89e4eSSatish Balay   */
614e5c89e4eSSatish Balay   {
615762437b8SSatish Balay #if defined(PETSC_CLANGUAGE_CXX)
616e5c89e4eSSatish Balay     PetscScalar ic(0.0,1.0);
617e5c89e4eSSatish Balay     PETSC_i = ic;
618b7940d39SSatish Balay #else
6193433f298SSatish Balay     PETSC_i = I;
620b7940d39SSatish Balay #endif
621762437b8SSatish Balay   }
622762437b8SSatish Balay 
6232c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
6242c876bd9SBarry Smith   ierr = MPI_Type_contiguous(2,MPIU_REAL,&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
6252c876bd9SBarry Smith   ierr = MPI_Type_commit(&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
626a83b8d76SBarry Smith   ierr = MPI_Type_contiguous(2,MPI_FLOAT,&MPI_C_COMPLEX);CHKERRQ(ierr);
627a83b8d76SBarry Smith   ierr = MPI_Type_commit(&MPI_C_COMPLEX);CHKERRQ(ierr);
62806a205a8SBarry Smith   ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);CHKERRQ(ierr);
629e5c89e4eSSatish Balay #endif
6302c876bd9SBarry Smith #endif
631e5c89e4eSSatish Balay 
632e5c89e4eSSatish Balay   /*
633e5c89e4eSSatish Balay      Create the PETSc MPI reduction operator that sums of the first
634e5c89e4eSSatish Balay      half of the entries and maxes the second half.
635e5c89e4eSSatish Balay   */
636e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);CHKERRQ(ierr);
637e5c89e4eSSatish Balay 
638e5c89e4eSSatish Balay   ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);CHKERRQ(ierr);
639e5c89e4eSSatish Balay   ierr = MPI_Type_commit(&MPIU_2SCALAR);CHKERRQ(ierr);
640e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);CHKERRQ(ierr);
641e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);CHKERRQ(ierr);
642e5c89e4eSSatish Balay 
643e5c89e4eSSatish Balay   ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);CHKERRQ(ierr);
644e5c89e4eSSatish Balay   ierr = MPI_Type_commit(&MPIU_2INT);CHKERRQ(ierr);
645e5c89e4eSSatish Balay 
646e5c89e4eSSatish Balay   /*
647480cf27aSJed Brown      Attributes to be set on PETSc communicators
648480cf27aSJed Brown   */
649480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelCounter,&Petsc_Counter_keyval,(void*)0);CHKERRQ(ierr);
650480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_InnerComm_keyval,(void*)0);CHKERRQ(ierr);
651480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_OuterComm_keyval,(void*)0);CHKERRQ(ierr);
652480cf27aSJed Brown 
653480cf27aSJed Brown   /*
654e8fb0fc0SBarry Smith      Build the options database
655e5c89e4eSSatish Balay   */
656e5c89e4eSSatish Balay   ierr = PetscOptionsInsert(argc,args,file);CHKERRQ(ierr);
657e5c89e4eSSatish Balay 
6586dc8fec2Sbcordonn 
659e5c89e4eSSatish Balay   /*
660e5c89e4eSSatish Balay      Print main application help message
661e5c89e4eSSatish Balay   */
662e5c89e4eSSatish Balay   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
663e5c89e4eSSatish Balay   if (help && flg) {
664e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,help);CHKERRQ(ierr);
665e5c89e4eSSatish Balay   }
666e5c89e4eSSatish Balay   ierr = PetscOptionsCheckInitial_Private();CHKERRQ(ierr);
667e5c89e4eSSatish Balay 
668e5c89e4eSSatish Balay   /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */
669a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
670e5c89e4eSSatish Balay   ierr = PetscLogBegin_Private();CHKERRQ(ierr);
671a9f03627SSatish Balay #endif
672e5c89e4eSSatish Balay 
673e5c89e4eSSatish Balay   /*
674e5c89e4eSSatish Balay      Load the dynamic libraries (on machines that support them), this registers all
675e5c89e4eSSatish Balay      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
676e5c89e4eSSatish Balay   */
677e5c89e4eSSatish Balay   ierr = PetscInitialize_DynamicLibraries();CHKERRQ(ierr);
678e5c89e4eSSatish Balay 
679e5c89e4eSSatish Balay   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
680ae15b995SBarry Smith   ierr = PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);CHKERRQ(ierr);
681e5c89e4eSSatish Balay   ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr);
682ae15b995SBarry Smith   ierr = PetscInfo1(0,"Running on machine: %s\n",hostname);CHKERRQ(ierr);
683e5c89e4eSSatish Balay 
684e5c89e4eSSatish Balay   ierr = PetscOptionsCheckInitial_Components();CHKERRQ(ierr);
685ef6c6fedSBoyana Norris   /* Check the options database for options related to the options database itself */
686ef6c6fedSBoyana Norris   ierr = PetscOptionsSetFromOptions();CHKERRQ(ierr);
687ef6c6fedSBoyana Norris 
688951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
689e39fd77fSBarry Smith   /*
690e39fd77fSBarry Smith       Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI
691e39fd77fSBarry Smith 
692e39fd77fSBarry Smith       Currently not used because it is not supported by MPICH.
693e39fd77fSBarry Smith   */
694e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
695708350f5SSatish Balay   ierr = MPI_Register_datarep((char *)"petsc",PetscDataRep_read_conv_fn,PetscDataRep_write_conv_fn,PetscDataRep_extent_fn,PETSC_NULL);CHKERRQ(ierr);
696e39fd77fSBarry Smith #endif
697951e3c8eSBarry Smith #endif
698e39fd77fSBarry Smith 
699793721a6SBarry Smith   ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_spawn_size",&nodesize,&flg);CHKERRQ(ierr);
700793721a6SBarry Smith   if (flg) {
70123464e94SBarry Smith #if defined(PETSC_HAVE_MPI_COMM_SPAWN)
7029505b675SBarry Smith     ierr = PetscOpenMPSpawn((PetscMPIInt) nodesize);CHKERRQ(ierr); /* worker nodes never return from here; they go directly to PetscEnd() */
70323464e94SBarry Smith #else
704e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"PETSc built without MPI 2 (MPI_Comm_spawn) support, use -openmp_merge_size instead");
70523464e94SBarry Smith #endif
706793721a6SBarry Smith   } else {
70723464e94SBarry Smith     ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_merge_size",&nodesize,&flg);CHKERRQ(ierr);
7088002f1cdSBarry Smith     if (flg) {
7099505b675SBarry Smith       ierr = PetscOpenMPMerge((PetscMPIInt) nodesize,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
7109505b675SBarry Smith       if (PetscOpenMPWorker) { /* if worker then never enter user code */
7119505b675SBarry Smith         ierr = PetscEnd();
7129505b675SBarry Smith       }
7138002f1cdSBarry Smith     }
714793721a6SBarry Smith   }
71590d69ab7SBarry Smith   flg  = PETSC_FALSE;
716acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-python",&flg,PETSC_NULL);CHKERRQ(ierr);
717192daf7cSBarry Smith   if (flg) {ierr = PetscPythonInitialize(PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
718e5c89e4eSSatish Balay 
71935d88935SVictor Minden #if defined(PETSC_HAVE_CUDA)
7202f947c57SVictor Minden   cublasInit();
7213e39abd9SVictor Minden #endif
72292e62aa6SBarry Smith 
72392e62aa6SBarry Smith #if defined(PETSC_HAVE_AMS)
724c457296dSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-ams_publish_objects",&flg);CHKERRQ(ierr);
72592e62aa6SBarry Smith   if (flg) {
72692e62aa6SBarry Smith     PetscAMSPublishAll = PETSC_TRUE;
72792e62aa6SBarry Smith   }
72892e62aa6SBarry Smith #endif
72992e62aa6SBarry Smith 
730301d30feSBarry Smith   /*
731301d30feSBarry Smith       Once we are completedly initialized then we can set this variables
732301d30feSBarry Smith   */
733301d30feSBarry Smith   PetscInitializeCalled = PETSC_TRUE;
734301d30feSBarry Smith   PetscFunctionReturn(0);
735e5c89e4eSSatish Balay }
736e5c89e4eSSatish Balay 
737e5c89e4eSSatish Balay 
738e5c89e4eSSatish Balay #undef __FUNCT__
739e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize"
740e5c89e4eSSatish Balay /*@C
741e5c89e4eSSatish Balay    PetscFinalize - Checks for options to be called at the conclusion
742e5c89e4eSSatish Balay    of the program. MPI_Finalize() is called only if the user had not
743e5c89e4eSSatish Balay    called MPI_Init() before calling PetscInitialize().
744e5c89e4eSSatish Balay 
745e5c89e4eSSatish Balay    Collective on PETSC_COMM_WORLD
746e5c89e4eSSatish Balay 
747e5c89e4eSSatish Balay    Options Database Keys:
748e5c89e4eSSatish Balay +  -options_table - Calls PetscOptionsPrint()
749e5c89e4eSSatish Balay .  -options_left - Prints unused options that remain in the database
750e5c89e4eSSatish Balay .  -options_left no - Does not print unused options that remain in the database
751e5c89e4eSSatish Balay .  -mpidump - Calls PetscMPIDump()
752e5c89e4eSSatish Balay .  -malloc_dump - Calls PetscMallocDump()
753e5c89e4eSSatish Balay .  -malloc_info - Prints total memory usage
754e5c89e4eSSatish Balay -  -malloc_log - Prints summary of memory usage
755e5c89e4eSSatish Balay 
756e5c89e4eSSatish Balay    Options Database Keys for Profiling:
7570598bfebSBarry Smith    See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details.
758e5c89e4eSSatish Balay +  -log_summary [filename] - Prints summary of flop and timing
759e5c89e4eSSatish Balay         information to screen. If the filename is specified the
760ff5bc46bSBarry Smith         summary is written to the file.  See PetscLogPrintSummary().
761ff5bc46bSBarry Smith .  -log_summary_python [filename] - Prints data on of flop and timing usage to a file or screen.
762ff5bc46bSBarry Smith         See PetscLogPrintSummaryPy().
763e5c89e4eSSatish Balay .  -log_all [filename] - Logs extensive profiling information
764ff5bc46bSBarry Smith         See PetscLogDump().
765ff5bc46bSBarry Smith .  -log [filename] - Logs basic profiline information  See PetscLogDump().
766e5c89e4eSSatish Balay .  -log_sync - Log the synchronization in scatters, inner products
767e5c89e4eSSatish Balay         and norms
768e5c89e4eSSatish Balay -  -log_mpe [filename] - Creates a logfile viewable by the
769e5c89e4eSSatish Balay       utility Upshot/Nupshot (in MPICH distribution)
770e5c89e4eSSatish Balay 
771e5c89e4eSSatish Balay    Level: beginner
772e5c89e4eSSatish Balay 
773e5c89e4eSSatish Balay    Note:
774e5c89e4eSSatish Balay    See PetscInitialize() for more general runtime options.
775e5c89e4eSSatish Balay 
776e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
777e5c89e4eSSatish Balay @*/
7787087cfbeSBarry Smith PetscErrorCode  PetscFinalize(void)
779e5c89e4eSSatish Balay {
780e5c89e4eSSatish Balay   PetscErrorCode ierr;
781e5c89e4eSSatish Balay   PetscMPIInt    rank;
782e5c89e4eSSatish Balay   int            nopt;
783ace3abfcSBarry Smith   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
784d5649816SBarry Smith #if defined(PETSC_HAVE_AMS)
785ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
786d5649816SBarry Smith #endif
787e5c89e4eSSatish Balay 
788e5c89e4eSSatish Balay   PetscFunctionBegin;
789e5c89e4eSSatish Balay 
790e5c89e4eSSatish Balay   if (!PetscInitializeCalled) {
7914b09e917SBarry Smith     printf("PetscInitialize() must be called before PetscFinalize()\n");
7924b09e917SBarry Smith     PetscFunctionReturn(PETSC_ERR_ARG_WRONGSTATE);
793e5c89e4eSSatish Balay   }
794b022a5c1SBarry Smith   ierr = PetscInfo(PETSC_NULL,"PetscFinalize() called\n");
795b022a5c1SBarry Smith 
796d5649816SBarry Smith #if defined(PETSC_HAVE_AMS)
797acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-options_gui",&flg,PETSC_NULL);CHKERRQ(ierr);
798d5649816SBarry Smith   if (flg) {
799d5649816SBarry Smith     ierr = PetscOptionsAMSDestroy();CHKERRQ(ierr);
800d5649816SBarry Smith   }
801d5649816SBarry Smith #endif
802d5649816SBarry Smith 
8038002f1cdSBarry Smith   ierr = PetscOpenMPFinalize();CHKERRQ(ierr);
8048002f1cdSBarry Smith 
805acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-python",&flg1,PETSC_NULL);CHKERRQ(ierr);
806192daf7cSBarry Smith   if (flg1) {ierr = PetscPythonFinalize();CHKERRQ(ierr);}
807192daf7cSBarry Smith 
808e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
809acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-malloc_info",&flg2,PETSC_NULL);CHKERRQ(ierr);
810e5c89e4eSSatish Balay   if (!flg2) {
81190d69ab7SBarry Smith     flg2 = PETSC_FALSE;
812acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(PETSC_NULL,"-memory_info",&flg2,PETSC_NULL);CHKERRQ(ierr);
813e5c89e4eSSatish Balay   }
814e5c89e4eSSatish Balay   if (flg2) {
815e5c89e4eSSatish Balay     ierr = PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");CHKERRQ(ierr);
816e5c89e4eSSatish Balay   }
817e5c89e4eSSatish Balay 
8183fa76a5bSLisandro Dalcin   /*
8193fa76a5bSLisandro Dalcin      Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_().
8203fa76a5bSLisandro Dalcin   */
8213fa76a5bSLisandro Dalcin   ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr);
8223fa76a5bSLisandro Dalcin 
8233fa76a5bSLisandro Dalcin   /*
8243fa76a5bSLisandro Dalcin        Free all the registered create functions, such as KSPList, VecList, SNESList, etc
8253fa76a5bSLisandro Dalcin   */
8263fa76a5bSLisandro Dalcin   ierr = PetscFListDestroyAll();CHKERRQ(ierr);
8273fa76a5bSLisandro Dalcin 
8283fa76a5bSLisandro Dalcin   /*
8293fa76a5bSLisandro Dalcin      Destroy any packages that registered a finalize
8303fa76a5bSLisandro Dalcin   */
831eb8be38cSBarry Smith   ierr = PetscRegisterFinalizeAll();CHKERRQ(ierr);
832e5c89e4eSSatish Balay 
833e5c89e4eSSatish Balay   /*
834e5c89e4eSSatish Balay      Destroy all the function registration lists created
835e5c89e4eSSatish Balay   */
836e5c89e4eSSatish Balay   ierr = PetscFinalize_DynamicLibraries();CHKERRQ(ierr);
837e5c89e4eSSatish Balay 
838e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
83990d69ab7SBarry Smith   flg1 = PETSC_FALSE;
840acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-get_total_flops",&flg1,PETSC_NULL);CHKERRQ(ierr);
841e5c89e4eSSatish Balay   if (flg1) {
842e5c89e4eSSatish Balay     PetscLogDouble flops = 0;
843e5c89e4eSSatish Balay     ierr = MPI_Reduce(&_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
844e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);CHKERRQ(ierr);
845e5c89e4eSSatish Balay   }
846e5c89e4eSSatish Balay #endif
847e5c89e4eSSatish Balay 
84863d6bff0SBarry Smith #if defined(PETSC_USE_DEBUG) && !defined(PETSC_USE_PTHREAD)
849e5c89e4eSSatish Balay   if (PetscStackActive) {
850e5c89e4eSSatish Balay     ierr = PetscStackDestroy();CHKERRQ(ierr);
851e5c89e4eSSatish Balay   }
852e5c89e4eSSatish Balay #endif
853e5c89e4eSSatish Balay 
854e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
855e5c89e4eSSatish Balay   {
856e5c89e4eSSatish Balay     char mname[PETSC_MAX_PATH_LEN];
857e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE)
858e5c89e4eSSatish Balay     mname[0] = 0;
859e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
860e5c89e4eSSatish Balay     if (flg1){
861e5c89e4eSSatish Balay       if (mname[0]) {ierr = PetscLogMPEDump(mname);CHKERRQ(ierr);}
862e5c89e4eSSatish Balay       else          {ierr = PetscLogMPEDump(0);CHKERRQ(ierr);}
863e5c89e4eSSatish Balay     }
864e5c89e4eSSatish Balay #endif
865e5c89e4eSSatish Balay     mname[0] = 0;
866e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
867e5c89e4eSSatish Balay     if (flg1) {
868e5c89e4eSSatish Balay       if (mname[0])  {ierr = PetscLogPrintSummary(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);}
869e5c89e4eSSatish Balay       else           {ierr = PetscLogPrintSummary(PETSC_COMM_WORLD,0);CHKERRQ(ierr);}
870e5c89e4eSSatish Balay     }
871e5c89e4eSSatish Balay 
872ff5bc46bSBarry Smith     mname[0] = 0;
873ff5bc46bSBarry Smith     ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary_python",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
874ff5bc46bSBarry Smith     if (flg1) {
875ff5bc46bSBarry Smith       PetscViewer viewer;
876ff5bc46bSBarry Smith       if (mname[0])  {
877ff5bc46bSBarry Smith         ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);CHKERRQ(ierr);
878ff5bc46bSBarry Smith       } else {
879ff5bc46bSBarry Smith         viewer = PETSC_VIEWER_STDOUT_WORLD;
880ff5bc46bSBarry Smith       }
881ff5bc46bSBarry Smith       ierr = PetscLogPrintSummaryPython(viewer);CHKERRQ(ierr);
882ff5bc46bSBarry Smith       ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
883ff5bc46bSBarry Smith     }
884ff5bc46bSBarry Smith 
88578392ef1SBarry Smith     ierr = PetscOptionsGetString(PETSC_NULL,"-log_detailed",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
88678392ef1SBarry Smith     if (flg1) {
88778392ef1SBarry Smith       if (mname[0])  {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);}
88878392ef1SBarry Smith       else           {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,0);CHKERRQ(ierr);}
88978392ef1SBarry Smith     }
89078392ef1SBarry Smith 
891e5c89e4eSSatish Balay     mname[0] = 0;
892e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
893e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);CHKERRQ(ierr);
894e5c89e4eSSatish Balay     if (flg1 || flg2){
895e5c89e4eSSatish Balay       if (mname[0]) PetscLogDump(mname);
896e5c89e4eSSatish Balay       else          PetscLogDump(0);
897e5c89e4eSSatish Balay     }
898e5c89e4eSSatish Balay     ierr = PetscLogDestroy();CHKERRQ(ierr);
899e5c89e4eSSatish Balay   }
900e5c89e4eSSatish Balay #endif
90190d69ab7SBarry Smith   flg1 = PETSC_FALSE;
902acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-no_signal_handler",&flg1,PETSC_NULL);CHKERRQ(ierr);
903e5c89e4eSSatish Balay   if (!flg1) { ierr = PetscPopSignalHandler();CHKERRQ(ierr);}
90490d69ab7SBarry Smith   flg1 = PETSC_FALSE;
905acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-mpidump",&flg1,PETSC_NULL);CHKERRQ(ierr);
906e5c89e4eSSatish Balay   if (flg1) {
907e5c89e4eSSatish Balay     ierr = PetscMPIDump(stdout);CHKERRQ(ierr);
908e5c89e4eSSatish Balay   }
90990d69ab7SBarry Smith   flg1 = PETSC_FALSE;
91090d69ab7SBarry Smith   flg2 = PETSC_FALSE;
9118bb29257SSatish Balay   /* preemptive call to avoid listing this option in options table as unused */
9128bb29257SSatish Balay   ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);CHKERRQ(ierr);
913acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-options_table",&flg2,PETSC_NULL);CHKERRQ(ierr);
914e4c476e2SSatish Balay 
915e5c89e4eSSatish Balay   if (flg2) {
916e5c89e4eSSatish Balay     if (!rank) {ierr = PetscOptionsPrint(stdout);CHKERRQ(ierr);}
917e5c89e4eSSatish Balay   }
918e5c89e4eSSatish Balay 
919e5c89e4eSSatish Balay   /* to prevent PETSc -options_left from warning */
920cb9801acSJed Brown   ierr = PetscOptionsHasName(PETSC_NULL,"-nox",&flg1);CHKERRQ(ierr);
921cb9801acSJed Brown   ierr = PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr);
922e5c89e4eSSatish Balay 
923f43cc0c9SSatish Balay   if (!PetscOpenMPWorker) { /* worker processes skip this because they do not usually process options */
92433fc4174SSatish Balay     flg3 = PETSC_FALSE; /* default value is required */
925acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(PETSC_NULL,"-options_left",&flg3,&flg1);CHKERRQ(ierr);
926e5c89e4eSSatish Balay     ierr = PetscOptionsAllUsed(&nopt);CHKERRQ(ierr);
927e5c89e4eSSatish Balay     if (flg3) {
928e5c89e4eSSatish Balay       if (!flg2) { /* have not yet printed the options */
929e5c89e4eSSatish Balay 	ierr = PetscOptionsPrint(stdout);CHKERRQ(ierr);
930e5c89e4eSSatish Balay       }
931e5c89e4eSSatish Balay       if (!nopt) {
932e5c89e4eSSatish Balay 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");CHKERRQ(ierr);
933e5c89e4eSSatish Balay       } else if (nopt == 1) {
934e5c89e4eSSatish Balay 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");CHKERRQ(ierr);
935e5c89e4eSSatish Balay       } else {
936e5c89e4eSSatish Balay 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There are %d unused database options. They are:\n",nopt);CHKERRQ(ierr);
937e5c89e4eSSatish Balay       }
938e5c89e4eSSatish Balay     }
939e5c89e4eSSatish Balay #if defined(PETSC_USE_DEBUG)
940da8b8a77SBarry Smith     if (nopt && !flg3 && !flg1) {
941e5c89e4eSSatish Balay       ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");CHKERRQ(ierr);
942e5c89e4eSSatish Balay       ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");CHKERRQ(ierr);
943e5c89e4eSSatish Balay       ierr = PetscOptionsLeft();CHKERRQ(ierr);
944e5c89e4eSSatish Balay     } else if (nopt && flg3) {
945e5c89e4eSSatish Balay #else
946e5c89e4eSSatish Balay     if (nopt && flg3) {
947e5c89e4eSSatish Balay #endif
948e5c89e4eSSatish Balay       ierr = PetscOptionsLeft();CHKERRQ(ierr);
949e5c89e4eSSatish Balay     }
950931f367cSBarry Smith   }
951e5c89e4eSSatish Balay 
9524028d114SSatish Balay   if (petsc_history) {
953f3dea69dSBarry Smith     ierr = PetscCloseHistoryFile(&petsc_history);CHKERRQ(ierr);
954e5c89e4eSSatish Balay     petsc_history = 0;
955e5c89e4eSSatish Balay   }
956e5c89e4eSSatish Balay 
9576cf91177SBarry Smith   ierr = PetscInfoAllow(PETSC_FALSE,PETSC_NULL);CHKERRQ(ierr);
958e5c89e4eSSatish Balay 
9598bb29257SSatish Balay   {
960e5c89e4eSSatish Balay     char fname[PETSC_MAX_PATH_LEN];
961e5c89e4eSSatish Balay     FILE *fd;
962ed9cf6e9SBarry Smith     int  err;
963e5c89e4eSSatish Balay 
964e5c89e4eSSatish Balay     fname[0] = 0;
965e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_dump",fname,250,&flg1);CHKERRQ(ierr);
966e5c89e4eSSatish Balay     if (flg1 && fname[0]) {
967e5c89e4eSSatish Balay       char sname[PETSC_MAX_PATH_LEN];
968e5c89e4eSSatish Balay 
969e5c89e4eSSatish Balay       sprintf(sname,"%s_%d",fname,rank);
970e32f2f54SBarry Smith       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
971e5c89e4eSSatish Balay       ierr = PetscMallocDump(fd);CHKERRQ(ierr);
972ed9cf6e9SBarry Smith       err = fclose(fd);
973e32f2f54SBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
9748bb29257SSatish Balay     } else if (flg1) {
975e5c89e4eSSatish Balay       MPI_Comm local_comm;
976e5c89e4eSSatish Balay 
977e5c89e4eSSatish Balay       ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr);
978e5c89e4eSSatish Balay       ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr);
979e5c89e4eSSatish Balay         ierr = PetscMallocDump(stdout);CHKERRQ(ierr);
980e5c89e4eSSatish Balay       ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr);
981e5c89e4eSSatish Balay       ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr);
982e5c89e4eSSatish Balay     }
983e5c89e4eSSatish Balay   }
9848bb29257SSatish Balay   {
985e5c89e4eSSatish Balay     char fname[PETSC_MAX_PATH_LEN];
986e5c89e4eSSatish Balay     FILE *fd;
987e5c89e4eSSatish Balay 
988e5c89e4eSSatish Balay     fname[0] = 0;
989e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_log",fname,250,&flg1);CHKERRQ(ierr);
990e5c89e4eSSatish Balay     if (flg1 && fname[0]) {
991e5c89e4eSSatish Balay       char sname[PETSC_MAX_PATH_LEN];
992ed9cf6e9SBarry Smith       int  err;
993e5c89e4eSSatish Balay 
994e5c89e4eSSatish Balay       sprintf(sname,"%s_%d",fname,rank);
995e32f2f54SBarry Smith       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
996e5c89e4eSSatish Balay       ierr = PetscMallocDumpLog(fd);CHKERRQ(ierr);
997ed9cf6e9SBarry Smith       err = fclose(fd);
998e32f2f54SBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
9998bb29257SSatish Balay     } else if (flg1) {
1000e5c89e4eSSatish Balay       ierr = PetscMallocDumpLog(stdout);CHKERRQ(ierr);
1001e5c89e4eSSatish Balay     }
1002e5c89e4eSSatish Balay   }
1003e5c89e4eSSatish Balay   /* Can be destroyed only after all the options are used */
1004e5c89e4eSSatish Balay   ierr = PetscOptionsDestroy();CHKERRQ(ierr);
1005e5c89e4eSSatish Balay 
1006e5c89e4eSSatish Balay   PetscGlobalArgc = 0;
1007e5c89e4eSSatish Balay   PetscGlobalArgs = 0;
1008e5c89e4eSSatish Balay 
1009e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
10102c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
101106a205a8SBarry Smith   ierr = MPI_Op_free(&MPIU_SUM);CHKERRQ(ierr);
10122c876bd9SBarry Smith   ierr = MPI_Type_free(&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
1013a83b8d76SBarry Smith   ierr = MPI_Type_free(&MPI_C_COMPLEX);CHKERRQ(ierr);
10142c876bd9SBarry Smith #endif
1015e5c89e4eSSatish Balay #endif
1016e5c89e4eSSatish Balay   ierr = MPI_Type_free(&MPIU_2SCALAR);CHKERRQ(ierr);
1017e5c89e4eSSatish Balay   ierr = MPI_Type_free(&MPIU_2INT);CHKERRQ(ierr);
1018e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscMaxSum_Op);CHKERRQ(ierr);
1019e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscADMax_Op);CHKERRQ(ierr);
1020e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscADMin_Op);CHKERRQ(ierr);
1021e5c89e4eSSatish Balay 
1022480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_Counter_keyval);CHKERRQ(ierr);
1023480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_InnerComm_keyval);CHKERRQ(ierr);
1024480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_OuterComm_keyval);CHKERRQ(ierr);
1025480cf27aSJed Brown 
1026ae15b995SBarry Smith   ierr = PetscInfo(0,"PETSc successfully ended!\n");CHKERRQ(ierr);
1027e5c89e4eSSatish Balay   if (PetscBeganMPI) {
102899608316SBarry Smith #if defined(PETSC_HAVE_MPI_FINALIZED)
102999b1327fSBarry Smith     PetscMPIInt flag;
103099b1327fSBarry Smith     ierr = MPI_Finalized(&flag);CHKERRQ(ierr);
1031e32f2f54SBarry Smith     if (flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()");
103299608316SBarry Smith #endif
1033e5c89e4eSSatish Balay     ierr = MPI_Finalize();CHKERRQ(ierr);
1034e5c89e4eSSatish Balay   }
1035e5c89e4eSSatish Balay 
10369c4c166aSBarry Smith   if (PETSC_ZOPEFD){
103722b84c2fSbcordonn     if (PETSC_ZOPEFD != PETSC_STDOUT) fprintf(PETSC_ZOPEFD, "<<<end>>>");
10389c4c166aSBarry Smith     else fprintf(PETSC_STDOUT, "<<<end>>>");
10399c4c166aSBarry Smith   }
104036186564Sbcordonn 
104135d88935SVictor Minden #if defined(PETSC_HAVE_CUDA)
10422f947c57SVictor Minden   cublasShutdown();
1043440a5bbfSVictor Minden #endif
1044e5c89e4eSSatish Balay /*
1045e5c89e4eSSatish Balay 
1046e5c89e4eSSatish Balay      Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because
1047e5c89e4eSSatish Balay    the communicator has some outstanding requests on it. Specifically if the
1048e5c89e4eSSatish Balay    flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See
1049e5c89e4eSSatish Balay    src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate()
1050e5c89e4eSSatish Balay    is never freed as it should be. Thus one may obtain messages of the form
10510e5e90baSSatish Balay    [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the
1052e5c89e4eSSatish Balay    memory was not freed.
1053e5c89e4eSSatish Balay 
1054e5c89e4eSSatish Balay */
10551d1a0024SBarry Smith   ierr = PetscMallocClear();CHKERRQ(ierr);
1056e5c89e4eSSatish Balay   PetscInitializeCalled = PETSC_FALSE;
1057e5c89e4eSSatish Balay   PetscFinalizeCalled   = PETSC_TRUE;
1058e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
1059e5c89e4eSSatish Balay }
1060e5c89e4eSSatish Balay 
1061