xref: /petsc/src/sys/objects/pinit.c (revision e4c476e2c7168ebdecf8eb81d2eb3cf049d1c626)
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)
13e5c89e4eSSatish Balay 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 
21e5c89e4eSSatish Balay EXTERN PetscErrorCode PetscInitialize_DynamicLibraries(void);
22e5c89e4eSSatish Balay EXTERN PetscErrorCode PetscFinalize_DynamicLibraries(void);
23e5c89e4eSSatish Balay EXTERN PetscErrorCode PetscFListDestroyAll(void);
24e5c89e4eSSatish Balay EXTERN PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int);
25e5c89e4eSSatish Balay EXTERN PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int);
26e5c89e4eSSatish Balay EXTERN PetscErrorCode PetscLogCloseHistoryFile(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"
558738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT 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 @*/
8572a42c3cSBarry Smith PetscErrorCode PETSCSYS_DLLEXPORT 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__
97e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitializeNoArguments"
98e5c89e4eSSatish Balay /*@C
99e5c89e4eSSatish Balay       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
100e5c89e4eSSatish Balay         the command line arguments.
101e5c89e4eSSatish Balay 
102e5c89e4eSSatish Balay    Collective
103e5c89e4eSSatish Balay 
104e5c89e4eSSatish Balay    Level: advanced
105e5c89e4eSSatish Balay 
106e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeFortran()
107e5c89e4eSSatish Balay @*/
1088738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscInitializeNoArguments(void)
109e5c89e4eSSatish Balay {
110e5c89e4eSSatish Balay   PetscErrorCode ierr;
111e5c89e4eSSatish Balay   int            argc = 0;
112e5c89e4eSSatish Balay   char           **args = 0;
113e5c89e4eSSatish Balay 
114e5c89e4eSSatish Balay   PetscFunctionBegin;
115e5c89e4eSSatish Balay   ierr = PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL);
116e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
117e5c89e4eSSatish Balay }
118e5c89e4eSSatish Balay 
119e5c89e4eSSatish Balay #undef __FUNCT__
120e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialized"
121e5c89e4eSSatish Balay /*@
122e5c89e4eSSatish Balay       PetscInitialized - Determine whether PETSc is initialized.
123e5c89e4eSSatish Balay 
1246dc8fec2Sbcordonn 7   Level: beginner
125e5c89e4eSSatish Balay 
126e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
127e5c89e4eSSatish Balay @*/
128ace3abfcSBarry Smith PetscErrorCode PETSCSYS_DLLEXPORT PetscInitialized(PetscBool  *isInitialized)
129e5c89e4eSSatish Balay {
130e5c89e4eSSatish Balay   PetscFunctionBegin;
131e5c89e4eSSatish Balay   PetscValidPointer(isInitialized, 1);
132e5c89e4eSSatish Balay   *isInitialized = PetscInitializeCalled;
133e5c89e4eSSatish Balay   PetscFunctionReturn(0);
134e5c89e4eSSatish Balay }
135e5c89e4eSSatish Balay 
136e5c89e4eSSatish Balay #undef __FUNCT__
137e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalized"
138e5c89e4eSSatish Balay /*@
139e5c89e4eSSatish Balay       PetscFinalized - Determine whether PetscFinalize() has been called yet
140e5c89e4eSSatish Balay 
141e5c89e4eSSatish Balay    Level: developer
142e5c89e4eSSatish Balay 
143e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
144e5c89e4eSSatish Balay @*/
145ace3abfcSBarry Smith PetscErrorCode PETSCSYS_DLLEXPORT PetscFinalized(PetscBool  *isFinalized)
146e5c89e4eSSatish Balay {
147e5c89e4eSSatish Balay   PetscFunctionBegin;
148e5c89e4eSSatish Balay   PetscValidPointer(isFinalized, 1);
149e5c89e4eSSatish Balay   *isFinalized = PetscFinalizeCalled;
150e5c89e4eSSatish Balay   PetscFunctionReturn(0);
151e5c89e4eSSatish Balay }
152e5c89e4eSSatish Balay 
153e5c89e4eSSatish Balay EXTERN PetscErrorCode        PetscOptionsCheckInitial_Private(void);
154ace3abfcSBarry Smith extern PetscBool  PetscBeganMPI;
155e5c89e4eSSatish Balay 
156e5c89e4eSSatish Balay /*
157e5c89e4eSSatish Balay        This function is the MPI reduction operation used to compute the sum of the
158e5c89e4eSSatish Balay    first half of the datatype and the max of the second half.
159e5c89e4eSSatish Balay */
160e5c89e4eSSatish Balay MPI_Op PetscMaxSum_Op = 0;
161e5c89e4eSSatish Balay 
162e5c89e4eSSatish Balay EXTERN_C_BEGIN
163e5c89e4eSSatish Balay #undef __FUNCT__
164e5c89e4eSSatish Balay #define __FUNCT__ "PetscMaxSum_Local"
1658738c821SJed Brown void PETSCSYS_DLLEXPORT MPIAPI PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
166e5c89e4eSSatish Balay {
167e5c89e4eSSatish Balay   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;
168e5c89e4eSSatish Balay 
169e5c89e4eSSatish Balay   PetscFunctionBegin;
170e5c89e4eSSatish Balay   if (*datatype != MPIU_2INT) {
171e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
172e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
173e5c89e4eSSatish Balay   }
174e5c89e4eSSatish Balay 
175e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
176e5c89e4eSSatish Balay     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
177e5c89e4eSSatish Balay     xout[2*i+1] += xin[2*i+1];
178e5c89e4eSSatish Balay   }
179812af9f3SBarry Smith   PetscFunctionReturnVoid();
180e5c89e4eSSatish Balay }
181e5c89e4eSSatish Balay EXTERN_C_END
182e5c89e4eSSatish Balay 
183e5c89e4eSSatish Balay /*
184e5c89e4eSSatish Balay     Returns the max of the first entry owned by this processor and the
185e5c89e4eSSatish Balay sum of the second entry.
186b693b147SBarry Smith 
187b693b147SBarry Smith     The reason nprocs[2*i] contains lengths nprocs[2*i+1] contains flag of 1 if length is nonzero
188b693b147SBarry Smith is so that the PetscMaxSum_Op() can set TWO values, if we passed in only nprocs[i] with lengths
189b693b147SBarry Smith there would be no place to store the both needed results.
190e5c89e4eSSatish Balay */
191e5c89e4eSSatish Balay #undef __FUNCT__
192e5c89e4eSSatish Balay #define __FUNCT__ "PetscMaxSum"
1938738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum)
194e5c89e4eSSatish Balay {
195e5c89e4eSSatish Balay   PetscMPIInt    size,rank;
196e5c89e4eSSatish Balay   PetscInt       *work;
197e5c89e4eSSatish Balay   PetscErrorCode ierr;
198e5c89e4eSSatish Balay 
199e5c89e4eSSatish Balay   PetscFunctionBegin;
200e5c89e4eSSatish Balay   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
201e5c89e4eSSatish Balay   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
202e5c89e4eSSatish Balay   ierr   = PetscMalloc(2*size*sizeof(PetscInt),&work);CHKERRQ(ierr);
203e5c89e4eSSatish Balay   ierr   = MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
204e5c89e4eSSatish Balay   *max   = work[2*rank];
205e5c89e4eSSatish Balay   *sum   = work[2*rank+1];
206e5c89e4eSSatish Balay   ierr   = PetscFree(work);CHKERRQ(ierr);
207e5c89e4eSSatish Balay   PetscFunctionReturn(0);
208e5c89e4eSSatish Balay }
209e5c89e4eSSatish Balay 
210e5c89e4eSSatish Balay /* ----------------------------------------------------------------------------*/
2118738c821SJed Brown MPI_Op PETSCSYS_DLLEXPORT PetscADMax_Op = 0;
212e5c89e4eSSatish Balay 
213e5c89e4eSSatish Balay EXTERN_C_BEGIN
214e5c89e4eSSatish Balay #undef __FUNCT__
215e5c89e4eSSatish Balay #define __FUNCT__ "PetscADMax_Local"
2168738c821SJed Brown void PETSCSYS_DLLEXPORT MPIAPI PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
217e5c89e4eSSatish Balay {
218e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
219e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
220e5c89e4eSSatish Balay 
221e5c89e4eSSatish Balay   PetscFunctionBegin;
222e5c89e4eSSatish Balay   if (*datatype != MPIU_2SCALAR) {
223e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
224e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
225e5c89e4eSSatish Balay   }
226e5c89e4eSSatish Balay 
227e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
228e5c89e4eSSatish Balay     if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) {
229e5c89e4eSSatish Balay       xout[2*i]   = xin[2*i];
230e5c89e4eSSatish Balay       xout[2*i+1] = xin[2*i+1];
231e5c89e4eSSatish Balay     }
232e5c89e4eSSatish Balay   }
233812af9f3SBarry Smith   PetscFunctionReturnVoid();
234e5c89e4eSSatish Balay }
235e5c89e4eSSatish Balay EXTERN_C_END
236e5c89e4eSSatish Balay 
2378738c821SJed Brown MPI_Op PETSCSYS_DLLEXPORT PetscADMin_Op = 0;
238e5c89e4eSSatish Balay 
239e5c89e4eSSatish Balay EXTERN_C_BEGIN
240e5c89e4eSSatish Balay #undef __FUNCT__
241e5c89e4eSSatish Balay #define __FUNCT__ "PetscADMin_Local"
2428738c821SJed Brown void PETSCSYS_DLLEXPORT MPIAPI PetscADMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
243e5c89e4eSSatish Balay {
244e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
245e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
246e5c89e4eSSatish Balay 
247e5c89e4eSSatish Balay   PetscFunctionBegin;
248e5c89e4eSSatish Balay   if (*datatype != MPIU_2SCALAR) {
249e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
250e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
251e5c89e4eSSatish Balay   }
252e5c89e4eSSatish Balay 
253e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
254e5c89e4eSSatish Balay     if (PetscRealPart(xout[2*i]) > PetscRealPart(xin[2*i])) {
255e5c89e4eSSatish Balay       xout[2*i]   = xin[2*i];
256e5c89e4eSSatish Balay       xout[2*i+1] = xin[2*i+1];
257e5c89e4eSSatish Balay     }
258e5c89e4eSSatish Balay   }
259812af9f3SBarry Smith   PetscFunctionReturnVoid();
260e5c89e4eSSatish Balay }
261e5c89e4eSSatish Balay EXTERN_C_END
262e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
263e5c89e4eSSatish Balay 
264e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
2652c876bd9SBarry Smith 
2662c876bd9SBarry Smith /*
2672c876bd9SBarry Smith     This operation is only needed when using complex numbers with older MPI that does not support complex numbers
2682c876bd9SBarry Smith */
2692c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
27006a205a8SBarry Smith MPI_Op MPIU_SUM = 0;
271e5c89e4eSSatish Balay 
272e5c89e4eSSatish Balay EXTERN_C_BEGIN
273e5c89e4eSSatish Balay #undef __FUNCT__
274e5c89e4eSSatish Balay #define __FUNCT__ "PetscSum_Local"
2758738c821SJed Brown void PETSCSYS_DLLEXPORT PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
276e5c89e4eSSatish Balay {
277e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
278e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
279e5c89e4eSSatish Balay 
280e5c89e4eSSatish Balay   PetscFunctionBegin;
281e5c89e4eSSatish Balay   if (*datatype != MPIU_SCALAR) {
282e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_SCALAR data (i.e. double or complex) types");
283e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
284e5c89e4eSSatish Balay   }
285e5c89e4eSSatish Balay 
286e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
287e5c89e4eSSatish Balay     xout[i] += xin[i];
288e5c89e4eSSatish Balay   }
289812af9f3SBarry Smith   PetscFunctionReturnVoid();
290e5c89e4eSSatish Balay }
291e5c89e4eSSatish Balay EXTERN_C_END
292e5c89e4eSSatish Balay #endif
2932c876bd9SBarry Smith #endif
294e5c89e4eSSatish Balay 
295480cf27aSJed Brown EXTERN_C_BEGIN
296480cf27aSJed Brown #undef __FUNCT__
297480cf27aSJed Brown #define __FUNCT__ "Petsc_DelCounter"
298480cf27aSJed Brown /*
299480cf27aSJed Brown    Private routine to delete internal tag/name counter storage when a communicator is freed.
300480cf27aSJed Brown 
301480cf27aSJed Brown    This is called by MPI, not by users.
302480cf27aSJed Brown 
303480cf27aSJed Brown    Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
304480cf27aSJed Brown 
305480cf27aSJed Brown */
3068738c821SJed Brown PetscMPIInt PETSCSYS_DLLEXPORT MPIAPI Petsc_DelCounter(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
307480cf27aSJed Brown {
308480cf27aSJed Brown   PetscErrorCode ierr;
309480cf27aSJed Brown 
310480cf27aSJed Brown   PetscFunctionBegin;
311480cf27aSJed Brown   ierr = PetscInfo1(0,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
312480cf27aSJed Brown   ierr = PetscFree(count_val);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
313480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
314480cf27aSJed Brown }
315480cf27aSJed Brown EXTERN_C_END
316480cf27aSJed Brown 
317480cf27aSJed Brown EXTERN_C_BEGIN
318480cf27aSJed Brown #undef __FUNCT__
319480cf27aSJed Brown #define __FUNCT__ "Petsc_DelComm"
320480cf27aSJed Brown /*
321480cf27aSJed Brown   This does not actually free anything, it simply marks when a reference count to an internal MPI_Comm reaches zero and the
322480cf27aSJed Brown   the external MPI_Comm drops its reference to the internal MPI_Comm
323480cf27aSJed Brown 
324480cf27aSJed Brown   This is called by MPI, not by users.
325480cf27aSJed Brown 
326480cf27aSJed Brown   Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
327480cf27aSJed Brown 
328480cf27aSJed Brown */
3298738c821SJed Brown PetscMPIInt PETSCSYS_DLLEXPORT MPIAPI Petsc_DelComm(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
330480cf27aSJed Brown {
331480cf27aSJed Brown   PetscErrorCode ierr;
332480cf27aSJed Brown 
333480cf27aSJed Brown   PetscFunctionBegin;
334480cf27aSJed Brown   ierr = PetscInfo1(0,"Deleting PETSc communicator imbedded in a user MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
335480cf27aSJed Brown   /* actually don't delete anything because we cannot increase the reference count of the communicator anyways */
336480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
337480cf27aSJed Brown }
338480cf27aSJed Brown EXTERN_C_END
339480cf27aSJed Brown 
340951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
341e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
342e39fd77fSBarry Smith EXTERN_C_BEGIN
343e39fd77fSBarry Smith extern PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype,MPI_Aint*,void*);
344e39fd77fSBarry Smith extern PetscMPIInt PetscDataRep_read_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
345e39fd77fSBarry Smith extern PetscMPIInt PetscDataRep_write_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
346e39fd77fSBarry Smith EXTERN_C_END
347e39fd77fSBarry Smith #endif
348951e3c8eSBarry Smith #endif
349e39fd77fSBarry Smith 
3506ae9a8a6SBarry Smith int  PetscGlobalArgc   = 0;
3516ae9a8a6SBarry Smith char **PetscGlobalArgs = 0;
352e5c89e4eSSatish Balay 
353e5c89e4eSSatish Balay #undef __FUNCT__
354e5c89e4eSSatish Balay #define __FUNCT__ "PetscGetArgs"
355e5c89e4eSSatish Balay /*@C
356e5c89e4eSSatish Balay    PetscGetArgs - Allows you to access the raw command line arguments anywhere
357e5c89e4eSSatish Balay      after PetscInitialize() is called but before PetscFinalize().
358e5c89e4eSSatish Balay 
359e5c89e4eSSatish Balay    Not Collective
360e5c89e4eSSatish Balay 
361e5c89e4eSSatish Balay    Output Parameters:
362e5c89e4eSSatish Balay +  argc - count of number of command line arguments
363e5c89e4eSSatish Balay -  args - the command line arguments
364e5c89e4eSSatish Balay 
365e5c89e4eSSatish Balay    Level: intermediate
366e5c89e4eSSatish Balay 
367e5c89e4eSSatish Balay    Notes:
368e5c89e4eSSatish Balay       This is usually used to pass the command line arguments into other libraries
369e5c89e4eSSatish Balay    that are called internally deep in PETSc or the application.
370e5c89e4eSSatish Balay 
371f177e3b1SBarry Smith       The first argument contains the program name as is normal for C arguments.
372f177e3b1SBarry Smith 
373e5c89e4eSSatish Balay    Concepts: command line arguments
374e5c89e4eSSatish Balay 
375793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()
376e5c89e4eSSatish Balay 
377e5c89e4eSSatish Balay @*/
3788738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscGetArgs(int *argc,char ***args)
379e5c89e4eSSatish Balay {
380e5c89e4eSSatish Balay   PetscFunctionBegin;
38117186662SBarry Smith   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
382e5c89e4eSSatish Balay   *argc = PetscGlobalArgc;
383e5c89e4eSSatish Balay   *args = PetscGlobalArgs;
384e5c89e4eSSatish Balay   PetscFunctionReturn(0);
385e5c89e4eSSatish Balay }
386e5c89e4eSSatish Balay 
387e5c89e4eSSatish Balay #undef __FUNCT__
388793721a6SBarry Smith #define __FUNCT__ "PetscGetArguments"
389793721a6SBarry Smith /*@C
390793721a6SBarry Smith    PetscGetArguments - Allows you to access the  command line arguments anywhere
391793721a6SBarry Smith      after PetscInitialize() is called but before PetscFinalize().
392793721a6SBarry Smith 
393793721a6SBarry Smith    Not Collective
394793721a6SBarry Smith 
395793721a6SBarry Smith    Output Parameters:
396793721a6SBarry Smith .  args - the command line arguments
397793721a6SBarry Smith 
398793721a6SBarry Smith    Level: intermediate
399793721a6SBarry Smith 
400793721a6SBarry Smith    Notes:
401793721a6SBarry Smith       This does NOT start with the program name and IS null terminated (final arg is void)
402793721a6SBarry Smith 
403793721a6SBarry Smith    Concepts: command line arguments
404793721a6SBarry Smith 
405793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()
406793721a6SBarry Smith 
407793721a6SBarry Smith @*/
4088738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscGetArguments(char ***args)
409793721a6SBarry Smith {
410793721a6SBarry Smith   PetscInt       i,argc = PetscGlobalArgc;
411793721a6SBarry Smith   PetscErrorCode ierr;
412793721a6SBarry Smith 
413793721a6SBarry Smith   PetscFunctionBegin;
41417186662SBarry Smith   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
415717030eeSLisandro Dalcin   if (!argc) {*args = 0; PetscFunctionReturn(0);}
416793721a6SBarry Smith   ierr = PetscMalloc(argc*sizeof(char*),args);CHKERRQ(ierr);
417793721a6SBarry Smith   for (i=0; i<argc-1; i++) {
418793721a6SBarry Smith     ierr = PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);CHKERRQ(ierr);
419793721a6SBarry Smith   }
420793721a6SBarry Smith   (*args)[argc-1] = 0;
421793721a6SBarry Smith   PetscFunctionReturn(0);
422793721a6SBarry Smith }
423793721a6SBarry Smith 
424793721a6SBarry Smith #undef __FUNCT__
425793721a6SBarry Smith #define __FUNCT__ "PetscFreeArguments"
426793721a6SBarry Smith /*@C
427793721a6SBarry Smith    PetscFreeArguments - Frees the memory obtained with PetscGetArguments()
428793721a6SBarry Smith 
429793721a6SBarry Smith    Not Collective
430793721a6SBarry Smith 
431793721a6SBarry Smith    Output Parameters:
432793721a6SBarry Smith .  args - the command line arguments
433793721a6SBarry Smith 
434793721a6SBarry Smith    Level: intermediate
435793721a6SBarry Smith 
436793721a6SBarry Smith    Concepts: command line arguments
437793721a6SBarry Smith 
438793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()
439793721a6SBarry Smith 
440793721a6SBarry Smith @*/
4418738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscFreeArguments(char **args)
442793721a6SBarry Smith {
443793721a6SBarry Smith   PetscInt       i = 0;
444793721a6SBarry Smith   PetscErrorCode ierr;
445793721a6SBarry Smith 
446793721a6SBarry Smith   PetscFunctionBegin;
447717030eeSLisandro Dalcin   if (!args) {PetscFunctionReturn(0);}
448793721a6SBarry Smith   while (args[i]) {
449793721a6SBarry Smith     ierr = PetscFree(args[i]);CHKERRQ(ierr);
450793721a6SBarry Smith     i++;
451793721a6SBarry Smith   }
452793721a6SBarry Smith   ierr = PetscFree(args);CHKERRQ(ierr);
453793721a6SBarry Smith   PetscFunctionReturn(0);
454793721a6SBarry Smith }
455793721a6SBarry Smith 
456793721a6SBarry Smith #undef __FUNCT__
457e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialize"
458e5c89e4eSSatish Balay /*@C
459e5c89e4eSSatish Balay    PetscInitialize - Initializes the PETSc database and MPI.
460e5c89e4eSSatish Balay    PetscInitialize() calls MPI_Init() if that has yet to be called,
461e5c89e4eSSatish Balay    so this routine should always be called near the beginning of
462e5c89e4eSSatish Balay    your program -- usually the very first line!
463e5c89e4eSSatish Balay 
464e5c89e4eSSatish Balay    Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set
465e5c89e4eSSatish Balay 
466e5c89e4eSSatish Balay    Input Parameters:
467e5c89e4eSSatish Balay +  argc - count of number of command line arguments
468e5c89e4eSSatish Balay .  args - the command line arguments
469fc2bca9aSBarry Smith .  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL to not check for
470fc2bca9aSBarry Smith           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
471e5c89e4eSSatish Balay -  help - [optional] Help message to print, use PETSC_NULL for no message
472e5c89e4eSSatish Balay 
47305827820SBarry Smith    If you wish PETSc code to run ONLY on a subcommunicator of MPI_COMM_WORLD, create that
47405827820SBarry Smith    communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize(). Thus if you are running a
47505827820SBarry Smith    four process job and two processes will run PETSc and have PetscInitialize() and PetscFinalize() and two process will not,
47605827820SBarry Smith    then do this. If ALL processes in the job are using PetscInitialize() and PetscFinalize() then you don't need to do this, even
47705827820SBarry Smith    if different subcommunicators of the job are doing different things with PETSc.
478e5c89e4eSSatish Balay 
479e5c89e4eSSatish Balay    Options Database Keys:
480e5c89e4eSSatish Balay +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
481e5c89e4eSSatish Balay .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
482e5c89e4eSSatish Balay .  -on_error_emacs <machinename> causes emacsclient to jump to error file
483b52f573bSBarry Smith .  -on_error_abort calls abort() when error detected (no traceback)
484e8fb0fc0SBarry Smith .  -on_error_mpiabort calls MPI_abort() when error detected
485e8fb0fc0SBarry Smith .  -error_output_stderr prints error messages to stderr instead of the default stdout
486e8fb0fc0SBarry Smith .  -error_output_none does not print the error messages (but handles errors in the same way as if this was not called)
487e5c89e4eSSatish Balay .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
488e5c89e4eSSatish Balay .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
489e5c89e4eSSatish Balay .  -stop_for_debugger - Print message on how to attach debugger manually to
490e5c89e4eSSatish Balay                         process and wait (-debugger_pause) seconds for attachment
4912fb0ec9aSBarry Smith .  -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries)
492e5c89e4eSSatish Balay .  -malloc no - Indicates not to use error-checking malloc
4932fb0ec9aSBarry Smith .  -malloc_debug - check for memory corruption at EVERY malloc or free
494e5c89e4eSSatish Balay .  -fp_trap - Stops on floating point exceptions (Note that on the
495e5c89e4eSSatish Balay               IBM RS6000 this slows code by at least a factor of 10.)
496e5c89e4eSSatish Balay .  -no_signal_handler - Indicates not to trap error signals
497e5c89e4eSSatish Balay .  -shared_tmp - indicates /tmp directory is shared by all processors
498e5c89e4eSSatish Balay .  -not_shared_tmp - each processor has own /tmp
499e5c89e4eSSatish Balay .  -tmp - alternative name of /tmp directory
500e5c89e4eSSatish Balay .  -get_total_flops - returns total flops done by all processors
501e5c89e4eSSatish Balay -  -memory_info - Print memory usage at end of run
502e5c89e4eSSatish Balay 
503e5c89e4eSSatish Balay    Options Database Keys for Profiling:
5040598bfebSBarry Smith    See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details.
505e5c89e4eSSatish Balay +  -log_trace [filename] - Print traces of all PETSc calls
506e5c89e4eSSatish Balay         to the screen (useful to determine where a program
507e5c89e4eSSatish Balay         hangs without running in the debugger).  See PetscLogTraceBegin().
5086cf91177SBarry Smith .  -info <optional filename> - Prints verbose information to the screen
5096cf91177SBarry Smith -  -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages
510e5c89e4eSSatish Balay 
511e5c89e4eSSatish Balay    Environmental Variables:
512e5c89e4eSSatish Balay +   PETSC_TMP - alternative tmp directory
513e5c89e4eSSatish Balay .   PETSC_SHARED_TMP - tmp is shared by all processes
514e5c89e4eSSatish Balay .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
515e5c89e4eSSatish Balay .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
516e5c89e4eSSatish Balay -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to
517e5c89e4eSSatish Balay 
518e5c89e4eSSatish Balay 
519e5c89e4eSSatish Balay    Level: beginner
520e5c89e4eSSatish Balay 
521e5c89e4eSSatish Balay    Notes:
522e5c89e4eSSatish Balay    If for some reason you must call MPI_Init() separately, call
523e5c89e4eSSatish Balay    it before PetscInitialize().
524e5c89e4eSSatish Balay 
525e5c89e4eSSatish Balay    Fortran Version:
526e5c89e4eSSatish Balay    In Fortran this routine has the format
527e5c89e4eSSatish Balay $       call PetscInitialize(file,ierr)
528e5c89e4eSSatish Balay 
529e5c89e4eSSatish Balay +   ierr - error return code
5303dae0d48SMatthew Knepley -  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL_CHARACTER to not check for
531fc2bca9aSBarry Smith           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
532e5c89e4eSSatish Balay 
533e5c89e4eSSatish Balay    Important Fortran Note:
534e5c89e4eSSatish Balay    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
535e5c89e4eSSatish Balay    null character string; you CANNOT just use PETSC_NULL as
5360598bfebSBarry Smith    in the C version. See the <a href="../../docs/manual.pdf">users manual</a> for details.
537e5c89e4eSSatish Balay 
53801cb0274SBarry Smith    If your main program is C but you call Fortran code that also uses PETSc you need to call PetscInitializeFortran() soon after
53901cb0274SBarry Smith    calling PetscInitialize().
540e5c89e4eSSatish Balay 
541e5c89e4eSSatish Balay    Concepts: initializing PETSc
542e5c89e4eSSatish Balay 
54301cb0274SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments()
544e5c89e4eSSatish Balay 
545e5c89e4eSSatish Balay @*/
5468738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscInitialize(int *argc,char ***args,const char file[],const char help[])
547e5c89e4eSSatish Balay {
548e5c89e4eSSatish Balay   PetscErrorCode ierr;
549aa5bb8c0SSatish Balay   PetscMPIInt    flag, size;
550aa5bb8c0SSatish Balay   PetscInt       nodesize;
551ace3abfcSBarry Smith   PetscBool      flg;
552e5c89e4eSSatish Balay   char           hostname[256];
553e5c89e4eSSatish Balay 
554e5c89e4eSSatish Balay   PetscFunctionBegin;
555e5c89e4eSSatish Balay   if (PetscInitializeCalled) PetscFunctionReturn(0);
556e5c89e4eSSatish Balay 
557ae9b4142SLisandro Dalcin   /* these must be initialized in a routine, not as a constant declaration*/
558d89683f4Sbcordonn   PETSC_STDOUT = stdout;
559ae9b4142SLisandro Dalcin   PETSC_STDERR = stderr;
560e5c89e4eSSatish Balay 
561e5c89e4eSSatish Balay   ierr = PetscOptionsCreate();CHKERRQ(ierr);
562e5c89e4eSSatish Balay 
563e5c89e4eSSatish Balay   /*
564e5c89e4eSSatish Balay      We initialize the program name here (before MPI_Init()) because MPICH has a bug in
565e5c89e4eSSatish Balay      it that it sets args[0] on all processors to be args[0] on the first processor.
566e5c89e4eSSatish Balay   */
567e5c89e4eSSatish Balay   if (argc && *argc) {
568e5c89e4eSSatish Balay     ierr = PetscSetProgramName(**args);CHKERRQ(ierr);
569e5c89e4eSSatish Balay   } else {
570e5c89e4eSSatish Balay     ierr = PetscSetProgramName("Unknown Name");CHKERRQ(ierr);
571e5c89e4eSSatish Balay   }
572e5c89e4eSSatish Balay 
573e5c89e4eSSatish Balay   ierr = MPI_Initialized(&flag);CHKERRQ(ierr);
574e5c89e4eSSatish Balay   if (!flag) {
575e32f2f54SBarry 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");
576e5c89e4eSSatish Balay     ierr          = MPI_Init(argc,args);CHKERRQ(ierr);
577e5c89e4eSSatish Balay     PetscBeganMPI = PETSC_TRUE;
578e5c89e4eSSatish Balay   }
579e5c89e4eSSatish Balay   if (argc && args) {
580e5c89e4eSSatish Balay     PetscGlobalArgc = *argc;
581e5c89e4eSSatish Balay     PetscGlobalArgs = *args;
582e5c89e4eSSatish Balay   }
583e5c89e4eSSatish Balay   PetscFinalizeCalled   = PETSC_FALSE;
584e5c89e4eSSatish Balay 
585e8373e55SMatthew Knepley   if (PETSC_COMM_WORLD == MPI_COMM_NULL) {
586e5c89e4eSSatish Balay     PETSC_COMM_WORLD = MPI_COMM_WORLD;
587e5c89e4eSSatish Balay   }
588660746e0SBarry Smith   ierr = MPI_Errhandler_set(PETSC_COMM_WORLD,MPI_ERRORS_RETURN);CHKERRQ(ierr);
589e5c89e4eSSatish Balay 
590e5c89e4eSSatish Balay   /* Done after init due to a bug in MPICH-GM? */
591e5c89e4eSSatish Balay   ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr);
592e5c89e4eSSatish Balay 
593e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);CHKERRQ(ierr);
594e5c89e4eSSatish Balay   ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);CHKERRQ(ierr);
595e5c89e4eSSatish Balay 
596e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
597e5c89e4eSSatish Balay   /*
598e5c89e4eSSatish Balay      Initialized the global complex variable; this is because with
599e5c89e4eSSatish Balay      shared libraries the constructors for global variables
600e5c89e4eSSatish Balay      are not called; at least on IRIX.
601e5c89e4eSSatish Balay   */
602e5c89e4eSSatish Balay   {
603762437b8SSatish Balay #if defined(PETSC_CLANGUAGE_CXX)
604e5c89e4eSSatish Balay     PetscScalar ic(0.0,1.0);
605e5c89e4eSSatish Balay     PETSC_i = ic;
606b7940d39SSatish Balay #else
6073433f298SSatish Balay     PETSC_i = I;
608b7940d39SSatish Balay #endif
609762437b8SSatish Balay   }
610762437b8SSatish Balay 
6112c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
6122c876bd9SBarry Smith   ierr = MPI_Type_contiguous(2,MPIU_REAL,&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
6132c876bd9SBarry Smith   ierr = MPI_Type_commit(&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
614a83b8d76SBarry Smith   ierr = MPI_Type_contiguous(2,MPI_FLOAT,&MPI_C_COMPLEX);CHKERRQ(ierr);
615a83b8d76SBarry Smith   ierr = MPI_Type_commit(&MPI_C_COMPLEX);CHKERRQ(ierr);
61606a205a8SBarry Smith   ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);CHKERRQ(ierr);
617e5c89e4eSSatish Balay #endif
6182c876bd9SBarry Smith #endif
619e5c89e4eSSatish Balay 
620e5c89e4eSSatish Balay   /*
621e5c89e4eSSatish Balay      Create the PETSc MPI reduction operator that sums of the first
622e5c89e4eSSatish Balay      half of the entries and maxes the second half.
623e5c89e4eSSatish Balay   */
624e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);CHKERRQ(ierr);
625e5c89e4eSSatish Balay 
626e5c89e4eSSatish Balay   ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);CHKERRQ(ierr);
627e5c89e4eSSatish Balay   ierr = MPI_Type_commit(&MPIU_2SCALAR);CHKERRQ(ierr);
628e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);CHKERRQ(ierr);
629e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);CHKERRQ(ierr);
630e5c89e4eSSatish Balay 
631e5c89e4eSSatish Balay   ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);CHKERRQ(ierr);
632e5c89e4eSSatish Balay   ierr = MPI_Type_commit(&MPIU_2INT);CHKERRQ(ierr);
633e5c89e4eSSatish Balay 
634e5c89e4eSSatish Balay   /*
635480cf27aSJed Brown      Attributes to be set on PETSc communicators
636480cf27aSJed Brown   */
637480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelCounter,&Petsc_Counter_keyval,(void*)0);CHKERRQ(ierr);
638480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_InnerComm_keyval,(void*)0);CHKERRQ(ierr);
639480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_OuterComm_keyval,(void*)0);CHKERRQ(ierr);
640480cf27aSJed Brown 
641480cf27aSJed Brown   /*
642e8fb0fc0SBarry Smith      Build the options database
643e5c89e4eSSatish Balay   */
644e5c89e4eSSatish Balay   ierr = PetscOptionsInsert(argc,args,file);CHKERRQ(ierr);
645e5c89e4eSSatish Balay 
6466dc8fec2Sbcordonn 
647e5c89e4eSSatish Balay   /*
648e5c89e4eSSatish Balay      Print main application help message
649e5c89e4eSSatish Balay   */
650e5c89e4eSSatish Balay   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
651e5c89e4eSSatish Balay   if (help && flg) {
652e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,help);CHKERRQ(ierr);
653e5c89e4eSSatish Balay   }
654e5c89e4eSSatish Balay   ierr = PetscOptionsCheckInitial_Private();CHKERRQ(ierr);
655e5c89e4eSSatish Balay 
656e5c89e4eSSatish Balay   /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */
657a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
658e5c89e4eSSatish Balay   ierr = PetscLogBegin_Private();CHKERRQ(ierr);
659a9f03627SSatish Balay #endif
660e5c89e4eSSatish Balay 
661e5c89e4eSSatish Balay   /*
662e5c89e4eSSatish Balay      Load the dynamic libraries (on machines that support them), this registers all
663e5c89e4eSSatish Balay      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
664e5c89e4eSSatish Balay   */
665e5c89e4eSSatish Balay   ierr = PetscInitialize_DynamicLibraries();CHKERRQ(ierr);
666e5c89e4eSSatish Balay 
667e5c89e4eSSatish Balay   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
668ae15b995SBarry Smith   ierr = PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);CHKERRQ(ierr);
669e5c89e4eSSatish Balay   ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr);
670ae15b995SBarry Smith   ierr = PetscInfo1(0,"Running on machine: %s\n",hostname);CHKERRQ(ierr);
671e5c89e4eSSatish Balay 
672e5c89e4eSSatish Balay   ierr = PetscOptionsCheckInitial_Components();CHKERRQ(ierr);
673ef6c6fedSBoyana Norris   /* Check the options database for options related to the options database itself */
674ef6c6fedSBoyana Norris   ierr = PetscOptionsSetFromOptions();CHKERRQ(ierr);
675ef6c6fedSBoyana Norris 
676951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
677e39fd77fSBarry Smith   /*
678e39fd77fSBarry Smith       Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI
679e39fd77fSBarry Smith 
680e39fd77fSBarry Smith       Currently not used because it is not supported by MPICH.
681e39fd77fSBarry Smith   */
682e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
683708350f5SSatish Balay   ierr = MPI_Register_datarep((char *)"petsc",PetscDataRep_read_conv_fn,PetscDataRep_write_conv_fn,PetscDataRep_extent_fn,PETSC_NULL);CHKERRQ(ierr);
684e39fd77fSBarry Smith #endif
685951e3c8eSBarry Smith #endif
686e39fd77fSBarry Smith 
687793721a6SBarry Smith   ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_spawn_size",&nodesize,&flg);CHKERRQ(ierr);
688793721a6SBarry Smith   if (flg) {
68923464e94SBarry Smith #if defined(PETSC_HAVE_MPI_COMM_SPAWN)
6909505b675SBarry Smith     ierr = PetscOpenMPSpawn((PetscMPIInt) nodesize);CHKERRQ(ierr); /* worker nodes never return from here; they go directly to PetscEnd() */
69123464e94SBarry Smith #else
692e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"PETSc built without MPI 2 (MPI_Comm_spawn) support, use -openmp_merge_size instead");
69323464e94SBarry Smith #endif
694793721a6SBarry Smith   } else {
69523464e94SBarry Smith     ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_merge_size",&nodesize,&flg);CHKERRQ(ierr);
6968002f1cdSBarry Smith     if (flg) {
6979505b675SBarry Smith       ierr = PetscOpenMPMerge((PetscMPIInt) nodesize,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
6989505b675SBarry Smith       if (PetscOpenMPWorker) { /* if worker then never enter user code */
6999505b675SBarry Smith         ierr = PetscEnd();
7009505b675SBarry Smith       }
7018002f1cdSBarry Smith     }
702793721a6SBarry Smith   }
70390d69ab7SBarry Smith   flg  = PETSC_FALSE;
704acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-python",&flg,PETSC_NULL);CHKERRQ(ierr);
705192daf7cSBarry Smith   if (flg) {ierr = PetscPythonInitialize(PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
706e5c89e4eSSatish Balay 
70735d88935SVictor Minden #if defined(PETSC_HAVE_CUDA)
7082f947c57SVictor Minden   cublasInit();
7093e39abd9SVictor Minden #endif
71092e62aa6SBarry Smith 
71192e62aa6SBarry Smith #if defined(PETSC_HAVE_AMS)
712c457296dSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-ams_publish_objects",&flg);CHKERRQ(ierr);
71392e62aa6SBarry Smith   if (flg) {
71492e62aa6SBarry Smith     PetscAMSPublishAll = PETSC_TRUE;
71592e62aa6SBarry Smith   }
71692e62aa6SBarry Smith #endif
71792e62aa6SBarry Smith 
718301d30feSBarry Smith   /*
719301d30feSBarry Smith       Once we are completedly initialized then we can set this variables
720301d30feSBarry Smith   */
721301d30feSBarry Smith   PetscInitializeCalled = PETSC_TRUE;
722301d30feSBarry Smith   PetscFunctionReturn(0);
723e5c89e4eSSatish Balay }
724e5c89e4eSSatish Balay 
725e5c89e4eSSatish Balay 
726e5c89e4eSSatish Balay #undef __FUNCT__
727e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize"
728e5c89e4eSSatish Balay /*@C
729e5c89e4eSSatish Balay    PetscFinalize - Checks for options to be called at the conclusion
730e5c89e4eSSatish Balay    of the program. MPI_Finalize() is called only if the user had not
731e5c89e4eSSatish Balay    called MPI_Init() before calling PetscInitialize().
732e5c89e4eSSatish Balay 
733e5c89e4eSSatish Balay    Collective on PETSC_COMM_WORLD
734e5c89e4eSSatish Balay 
735e5c89e4eSSatish Balay    Options Database Keys:
736e5c89e4eSSatish Balay +  -options_table - Calls PetscOptionsPrint()
737e5c89e4eSSatish Balay .  -options_left - Prints unused options that remain in the database
738e5c89e4eSSatish Balay .  -options_left no - Does not print unused options that remain in the database
739e5c89e4eSSatish Balay .  -mpidump - Calls PetscMPIDump()
740e5c89e4eSSatish Balay .  -malloc_dump - Calls PetscMallocDump()
741e5c89e4eSSatish Balay .  -malloc_info - Prints total memory usage
742e5c89e4eSSatish Balay -  -malloc_log - Prints summary of memory usage
743e5c89e4eSSatish Balay 
744e5c89e4eSSatish Balay    Options Database Keys for Profiling:
7450598bfebSBarry Smith    See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details.
746e5c89e4eSSatish Balay +  -log_summary [filename] - Prints summary of flop and timing
747e5c89e4eSSatish Balay         information to screen. If the filename is specified the
748ff5bc46bSBarry Smith         summary is written to the file.  See PetscLogPrintSummary().
749ff5bc46bSBarry Smith .  -log_summary_python [filename] - Prints data on of flop and timing usage to a file or screen.
750ff5bc46bSBarry Smith         See PetscLogPrintSummaryPy().
751e5c89e4eSSatish Balay .  -log_all [filename] - Logs extensive profiling information
752ff5bc46bSBarry Smith         See PetscLogDump().
753ff5bc46bSBarry Smith .  -log [filename] - Logs basic profiline information  See PetscLogDump().
754e5c89e4eSSatish Balay .  -log_sync - Log the synchronization in scatters, inner products
755e5c89e4eSSatish Balay         and norms
756e5c89e4eSSatish Balay -  -log_mpe [filename] - Creates a logfile viewable by the
757e5c89e4eSSatish Balay       utility Upshot/Nupshot (in MPICH distribution)
758e5c89e4eSSatish Balay 
759e5c89e4eSSatish Balay    Level: beginner
760e5c89e4eSSatish Balay 
761e5c89e4eSSatish Balay    Note:
762e5c89e4eSSatish Balay    See PetscInitialize() for more general runtime options.
763e5c89e4eSSatish Balay 
764e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
765e5c89e4eSSatish Balay @*/
7668738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscFinalize(void)
767e5c89e4eSSatish Balay {
768e5c89e4eSSatish Balay   PetscErrorCode ierr;
769e5c89e4eSSatish Balay   PetscMPIInt    rank;
770e5c89e4eSSatish Balay   int            nopt;
771ace3abfcSBarry Smith   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
772d5649816SBarry Smith #if defined(PETSC_HAVE_AMS)
773ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
774d5649816SBarry Smith #endif
775e5c89e4eSSatish Balay 
776e5c89e4eSSatish Balay   PetscFunctionBegin;
777e5c89e4eSSatish Balay 
778e5c89e4eSSatish Balay   if (!PetscInitializeCalled) {
7794b09e917SBarry Smith     printf("PetscInitialize() must be called before PetscFinalize()\n");
7804b09e917SBarry Smith     PetscFunctionReturn(PETSC_ERR_ARG_WRONGSTATE);
781e5c89e4eSSatish Balay   }
782b022a5c1SBarry Smith   ierr = PetscInfo(PETSC_NULL,"PetscFinalize() called\n");
783b022a5c1SBarry Smith 
784d5649816SBarry Smith #if defined(PETSC_HAVE_AMS)
785acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-options_gui",&flg,PETSC_NULL);CHKERRQ(ierr);
786d5649816SBarry Smith   if (flg) {
787d5649816SBarry Smith     ierr = PetscOptionsAMSDestroy();CHKERRQ(ierr);
788d5649816SBarry Smith   }
789d5649816SBarry Smith #endif
790d5649816SBarry Smith 
7918002f1cdSBarry Smith   ierr = PetscOpenMPFinalize();CHKERRQ(ierr);
7928002f1cdSBarry Smith 
793acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-python",&flg1,PETSC_NULL);CHKERRQ(ierr);
794192daf7cSBarry Smith   if (flg1) {ierr = PetscPythonFinalize();CHKERRQ(ierr);}
795192daf7cSBarry Smith 
796e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
797acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-malloc_info",&flg2,PETSC_NULL);CHKERRQ(ierr);
798e5c89e4eSSatish Balay   if (!flg2) {
79990d69ab7SBarry Smith     flg2 = PETSC_FALSE;
800acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(PETSC_NULL,"-memory_info",&flg2,PETSC_NULL);CHKERRQ(ierr);
801e5c89e4eSSatish Balay   }
802e5c89e4eSSatish Balay   if (flg2) {
803e5c89e4eSSatish Balay     ierr = PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");CHKERRQ(ierr);
804e5c89e4eSSatish Balay   }
805e5c89e4eSSatish Balay 
8063fa76a5bSLisandro Dalcin   /*
8073fa76a5bSLisandro Dalcin      Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_().
8083fa76a5bSLisandro Dalcin   */
8093fa76a5bSLisandro Dalcin   ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr);
8103fa76a5bSLisandro Dalcin 
8113fa76a5bSLisandro Dalcin   /*
8123fa76a5bSLisandro Dalcin        Free all the registered create functions, such as KSPList, VecList, SNESList, etc
8133fa76a5bSLisandro Dalcin   */
8143fa76a5bSLisandro Dalcin   ierr = PetscFListDestroyAll();CHKERRQ(ierr);
8153fa76a5bSLisandro Dalcin 
8163fa76a5bSLisandro Dalcin   /*
8173fa76a5bSLisandro Dalcin      Destroy any packages that registered a finalize
8183fa76a5bSLisandro Dalcin   */
819eb8be38cSBarry Smith   ierr = PetscRegisterFinalizeAll();CHKERRQ(ierr);
820e5c89e4eSSatish Balay 
821e5c89e4eSSatish Balay   /*
822e5c89e4eSSatish Balay      Destroy all the function registration lists created
823e5c89e4eSSatish Balay   */
824e5c89e4eSSatish Balay   ierr = PetscFinalize_DynamicLibraries();CHKERRQ(ierr);
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 
83663d6bff0SBarry Smith #if defined(PETSC_USE_DEBUG) && !defined(PETSC_USE_PTHREAD)
837e5c89e4eSSatish Balay   if (PetscStackActive) {
838e5c89e4eSSatish Balay     ierr = PetscStackDestroy();CHKERRQ(ierr);
839e5c89e4eSSatish Balay   }
840e5c89e4eSSatish Balay #endif
841e5c89e4eSSatish Balay 
842e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
843e5c89e4eSSatish Balay   {
844e5c89e4eSSatish Balay     char mname[PETSC_MAX_PATH_LEN];
845e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE)
846e5c89e4eSSatish Balay     mname[0] = 0;
847e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
848e5c89e4eSSatish Balay     if (flg1){
849e5c89e4eSSatish Balay       if (mname[0]) {ierr = PetscLogMPEDump(mname);CHKERRQ(ierr);}
850e5c89e4eSSatish Balay       else          {ierr = PetscLogMPEDump(0);CHKERRQ(ierr);}
851e5c89e4eSSatish Balay     }
852e5c89e4eSSatish Balay #endif
853e5c89e4eSSatish Balay     mname[0] = 0;
854e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
855e5c89e4eSSatish Balay     if (flg1) {
856e5c89e4eSSatish Balay       if (mname[0])  {ierr = PetscLogPrintSummary(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);}
857e5c89e4eSSatish Balay       else           {ierr = PetscLogPrintSummary(PETSC_COMM_WORLD,0);CHKERRQ(ierr);}
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);
866ff5bc46bSBarry Smith       } else {
867ff5bc46bSBarry Smith         viewer = PETSC_VIEWER_STDOUT_WORLD;
868ff5bc46bSBarry Smith       }
869ff5bc46bSBarry Smith       ierr = PetscLogPrintSummaryPython(viewer);CHKERRQ(ierr);
870ff5bc46bSBarry Smith       ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
871ff5bc46bSBarry Smith     }
872ff5bc46bSBarry Smith 
87378392ef1SBarry Smith     ierr = PetscOptionsGetString(PETSC_NULL,"-log_detailed",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
87478392ef1SBarry Smith     if (flg1) {
87578392ef1SBarry Smith       if (mname[0])  {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);}
87678392ef1SBarry Smith       else           {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,0);CHKERRQ(ierr);}
87778392ef1SBarry Smith     }
87878392ef1SBarry Smith 
879e5c89e4eSSatish Balay     mname[0] = 0;
880e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
881e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);CHKERRQ(ierr);
882e5c89e4eSSatish Balay     if (flg1 || flg2){
883e5c89e4eSSatish Balay       if (mname[0]) PetscLogDump(mname);
884e5c89e4eSSatish Balay       else          PetscLogDump(0);
885e5c89e4eSSatish Balay     }
886e5c89e4eSSatish Balay     ierr = PetscLogDestroy();CHKERRQ(ierr);
887e5c89e4eSSatish Balay   }
888e5c89e4eSSatish Balay #endif
88990d69ab7SBarry Smith   flg1 = PETSC_FALSE;
890acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-no_signal_handler",&flg1,PETSC_NULL);CHKERRQ(ierr);
891e5c89e4eSSatish Balay   if (!flg1) { ierr = PetscPopSignalHandler();CHKERRQ(ierr);}
89290d69ab7SBarry Smith   flg1 = PETSC_FALSE;
893acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-mpidump",&flg1,PETSC_NULL);CHKERRQ(ierr);
894e5c89e4eSSatish Balay   if (flg1) {
895e5c89e4eSSatish Balay     ierr = PetscMPIDump(stdout);CHKERRQ(ierr);
896e5c89e4eSSatish Balay   }
89790d69ab7SBarry Smith   flg1 = PETSC_FALSE;
89890d69ab7SBarry Smith   flg2 = PETSC_FALSE;
8998bb29257SSatish Balay   /* preemptive call to avoid listing this option in options table as unused */
9008bb29257SSatish Balay   ierr = PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);CHKERRQ(ierr);
901acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-options_table",&flg2,PETSC_NULL);CHKERRQ(ierr);
902*e4c476e2SSatish Balay 
903e5c89e4eSSatish Balay   if (flg2) {
904e5c89e4eSSatish Balay     if (!rank) {ierr = PetscOptionsPrint(stdout);CHKERRQ(ierr);}
905e5c89e4eSSatish Balay   }
906e5c89e4eSSatish Balay 
907e5c89e4eSSatish Balay   /* to prevent PETSc -options_left from warning */
908cb9801acSJed Brown   ierr = PetscOptionsHasName(PETSC_NULL,"-nox",&flg1);CHKERRQ(ierr);
909cb9801acSJed Brown   ierr = PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr);
910e5c89e4eSSatish Balay 
911f43cc0c9SSatish Balay   if (!PetscOpenMPWorker) { /* worker processes skip this because they do not usually process options */
91233fc4174SSatish Balay     flg3 = PETSC_FALSE; /* default value is required */
913acfcf0e5SJed Brown     ierr = PetscOptionsGetBool(PETSC_NULL,"-options_left",&flg3,&flg1);CHKERRQ(ierr);
914e5c89e4eSSatish Balay     ierr = PetscOptionsAllUsed(&nopt);CHKERRQ(ierr);
915e5c89e4eSSatish Balay     if (flg3) {
916e5c89e4eSSatish Balay       if (!flg2) { /* have not yet printed the options */
917e5c89e4eSSatish Balay 	ierr = PetscOptionsPrint(stdout);CHKERRQ(ierr);
918e5c89e4eSSatish Balay       }
919e5c89e4eSSatish Balay       if (!nopt) {
920e5c89e4eSSatish Balay 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");CHKERRQ(ierr);
921e5c89e4eSSatish Balay       } else if (nopt == 1) {
922e5c89e4eSSatish Balay 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");CHKERRQ(ierr);
923e5c89e4eSSatish Balay       } else {
924e5c89e4eSSatish Balay 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There are %d unused database options. They are:\n",nopt);CHKERRQ(ierr);
925e5c89e4eSSatish Balay       }
926e5c89e4eSSatish Balay     }
927e5c89e4eSSatish Balay #if defined(PETSC_USE_DEBUG)
928da8b8a77SBarry Smith     if (nopt && !flg3 && !flg1) {
929e5c89e4eSSatish Balay       ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");CHKERRQ(ierr);
930e5c89e4eSSatish Balay       ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");CHKERRQ(ierr);
931e5c89e4eSSatish Balay       ierr = PetscOptionsLeft();CHKERRQ(ierr);
932e5c89e4eSSatish Balay     } else if (nopt && flg3) {
933e5c89e4eSSatish Balay #else
934e5c89e4eSSatish Balay     if (nopt && flg3) {
935e5c89e4eSSatish Balay #endif
936e5c89e4eSSatish Balay       ierr = PetscOptionsLeft();CHKERRQ(ierr);
937e5c89e4eSSatish Balay     }
938931f367cSBarry Smith   }
939e5c89e4eSSatish Balay 
9404028d114SSatish Balay   if (petsc_history) {
941e5c89e4eSSatish Balay     ierr = PetscLogCloseHistoryFile(&petsc_history);CHKERRQ(ierr);
942e5c89e4eSSatish Balay     petsc_history = 0;
943e5c89e4eSSatish Balay   }
944e5c89e4eSSatish Balay 
9456cf91177SBarry Smith   ierr = PetscInfoAllow(PETSC_FALSE,PETSC_NULL);CHKERRQ(ierr);
946e5c89e4eSSatish Balay 
9478bb29257SSatish Balay   {
948e5c89e4eSSatish Balay     char fname[PETSC_MAX_PATH_LEN];
949e5c89e4eSSatish Balay     FILE *fd;
950ed9cf6e9SBarry Smith     int  err;
951e5c89e4eSSatish Balay 
952e5c89e4eSSatish Balay     fname[0] = 0;
953e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_dump",fname,250,&flg1);CHKERRQ(ierr);
954e5c89e4eSSatish Balay     if (flg1 && fname[0]) {
955e5c89e4eSSatish Balay       char sname[PETSC_MAX_PATH_LEN];
956e5c89e4eSSatish Balay 
957e5c89e4eSSatish Balay       sprintf(sname,"%s_%d",fname,rank);
958e32f2f54SBarry Smith       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
959e5c89e4eSSatish Balay       ierr = PetscMallocDump(fd);CHKERRQ(ierr);
960ed9cf6e9SBarry Smith       err = fclose(fd);
961e32f2f54SBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
9628bb29257SSatish Balay     } else if (flg1) {
963e5c89e4eSSatish Balay       MPI_Comm local_comm;
964e5c89e4eSSatish Balay 
965e5c89e4eSSatish Balay       ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr);
966e5c89e4eSSatish Balay       ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr);
967e5c89e4eSSatish Balay         ierr = PetscMallocDump(stdout);CHKERRQ(ierr);
968e5c89e4eSSatish Balay       ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr);
969e5c89e4eSSatish Balay       ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr);
970e5c89e4eSSatish Balay     }
971e5c89e4eSSatish Balay   }
9728bb29257SSatish Balay   {
973e5c89e4eSSatish Balay     char fname[PETSC_MAX_PATH_LEN];
974e5c89e4eSSatish Balay     FILE *fd;
975e5c89e4eSSatish Balay 
976e5c89e4eSSatish Balay     fname[0] = 0;
977e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_log",fname,250,&flg1);CHKERRQ(ierr);
978e5c89e4eSSatish Balay     if (flg1 && fname[0]) {
979e5c89e4eSSatish Balay       char sname[PETSC_MAX_PATH_LEN];
980ed9cf6e9SBarry Smith       int  err;
981e5c89e4eSSatish Balay 
982e5c89e4eSSatish Balay       sprintf(sname,"%s_%d",fname,rank);
983e32f2f54SBarry Smith       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
984e5c89e4eSSatish Balay       ierr = PetscMallocDumpLog(fd);CHKERRQ(ierr);
985ed9cf6e9SBarry Smith       err = fclose(fd);
986e32f2f54SBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
9878bb29257SSatish Balay     } else if (flg1) {
988e5c89e4eSSatish Balay       ierr = PetscMallocDumpLog(stdout);CHKERRQ(ierr);
989e5c89e4eSSatish Balay     }
990e5c89e4eSSatish Balay   }
991e5c89e4eSSatish Balay   /* Can be destroyed only after all the options are used */
992e5c89e4eSSatish Balay   ierr = PetscOptionsDestroy();CHKERRQ(ierr);
993e5c89e4eSSatish Balay 
994e5c89e4eSSatish Balay   PetscGlobalArgc = 0;
995e5c89e4eSSatish Balay   PetscGlobalArgs = 0;
996e5c89e4eSSatish Balay 
997e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
9982c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
99906a205a8SBarry Smith   ierr = MPI_Op_free(&MPIU_SUM);CHKERRQ(ierr);
10002c876bd9SBarry Smith   ierr = MPI_Type_free(&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
1001a83b8d76SBarry Smith   ierr = MPI_Type_free(&MPI_C_COMPLEX);CHKERRQ(ierr);
10022c876bd9SBarry Smith #endif
1003e5c89e4eSSatish Balay #endif
1004e5c89e4eSSatish Balay   ierr = MPI_Type_free(&MPIU_2SCALAR);CHKERRQ(ierr);
1005e5c89e4eSSatish Balay   ierr = MPI_Type_free(&MPIU_2INT);CHKERRQ(ierr);
1006e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscMaxSum_Op);CHKERRQ(ierr);
1007e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscADMax_Op);CHKERRQ(ierr);
1008e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscADMin_Op);CHKERRQ(ierr);
1009e5c89e4eSSatish Balay 
1010480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_Counter_keyval);CHKERRQ(ierr);
1011480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_InnerComm_keyval);CHKERRQ(ierr);
1012480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_OuterComm_keyval);CHKERRQ(ierr);
1013480cf27aSJed Brown 
1014ae15b995SBarry Smith   ierr = PetscInfo(0,"PETSc successfully ended!\n");CHKERRQ(ierr);
1015e5c89e4eSSatish Balay   if (PetscBeganMPI) {
101699608316SBarry Smith #if defined(PETSC_HAVE_MPI_FINALIZED)
101799b1327fSBarry Smith     PetscMPIInt flag;
101899b1327fSBarry Smith     ierr = MPI_Finalized(&flag);CHKERRQ(ierr);
1019e32f2f54SBarry Smith     if (flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()");
102099608316SBarry Smith #endif
1021e5c89e4eSSatish Balay     ierr = MPI_Finalize();CHKERRQ(ierr);
1022e5c89e4eSSatish Balay   }
1023e5c89e4eSSatish Balay 
10249c4c166aSBarry Smith   if (PETSC_ZOPEFD){
102522b84c2fSbcordonn     if (PETSC_ZOPEFD != PETSC_STDOUT) fprintf(PETSC_ZOPEFD, "<<<end>>>");
10269c4c166aSBarry Smith     else fprintf(PETSC_STDOUT, "<<<end>>>");
10279c4c166aSBarry Smith   }
102836186564Sbcordonn 
102935d88935SVictor Minden #if defined(PETSC_HAVE_CUDA)
10302f947c57SVictor Minden   cublasShutdown();
1031440a5bbfSVictor Minden #endif
1032e5c89e4eSSatish Balay /*
1033e5c89e4eSSatish Balay 
1034e5c89e4eSSatish Balay      Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because
1035e5c89e4eSSatish Balay    the communicator has some outstanding requests on it. Specifically if the
1036e5c89e4eSSatish Balay    flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See
1037e5c89e4eSSatish Balay    src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate()
1038e5c89e4eSSatish Balay    is never freed as it should be. Thus one may obtain messages of the form
10390e5e90baSSatish Balay    [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the
1040e5c89e4eSSatish Balay    memory was not freed.
1041e5c89e4eSSatish Balay 
1042e5c89e4eSSatish Balay */
10431d1a0024SBarry Smith   ierr = PetscMallocClear();CHKERRQ(ierr);
1044e5c89e4eSSatish Balay   PetscInitializeCalled = PETSC_FALSE;
1045e5c89e4eSSatish Balay   PetscFinalizeCalled   = PETSC_TRUE;
1046e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
1047e5c89e4eSSatish Balay }
1048e5c89e4eSSatish Balay 
1049