xref: /petsc/src/sys/objects/pinit.c (revision 4b09e917cc256fad21c4a64a4a2b5e20cdd5f9f3)
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};
42e5c89e4eSSatish Balay const char *PetscDataTypes[] = {"INT", "DOUBLE", "COMPLEX",
43e5c89e4eSSatish Balay                                 "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__
75e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitializeNoArguments"
76e5c89e4eSSatish Balay /*@C
77e5c89e4eSSatish Balay       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
78e5c89e4eSSatish Balay         the command line arguments.
79e5c89e4eSSatish Balay 
80e5c89e4eSSatish Balay    Collective
81e5c89e4eSSatish Balay 
82e5c89e4eSSatish Balay    Level: advanced
83e5c89e4eSSatish Balay 
84e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeFortran()
85e5c89e4eSSatish Balay @*/
868738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscInitializeNoArguments(void)
87e5c89e4eSSatish Balay {
88e5c89e4eSSatish Balay   PetscErrorCode ierr;
89e5c89e4eSSatish Balay   int            argc = 0;
90e5c89e4eSSatish Balay   char           **args = 0;
91e5c89e4eSSatish Balay 
92e5c89e4eSSatish Balay   PetscFunctionBegin;
93e5c89e4eSSatish Balay   ierr = PetscInitialize(&argc,&args,PETSC_NULL,PETSC_NULL);
94e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
95e5c89e4eSSatish Balay }
96e5c89e4eSSatish Balay 
97e5c89e4eSSatish Balay #undef __FUNCT__
98e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialized"
99e5c89e4eSSatish Balay /*@
100e5c89e4eSSatish Balay       PetscInitialized - Determine whether PETSc is initialized.
101e5c89e4eSSatish Balay 
1026dc8fec2Sbcordonn 7   Level: beginner
103e5c89e4eSSatish Balay 
104e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
105e5c89e4eSSatish Balay @*/
106ace3abfcSBarry Smith PetscErrorCode PETSCSYS_DLLEXPORT PetscInitialized(PetscBool  *isInitialized)
107e5c89e4eSSatish Balay {
108e5c89e4eSSatish Balay   PetscFunctionBegin;
109e5c89e4eSSatish Balay   PetscValidPointer(isInitialized, 1);
110e5c89e4eSSatish Balay   *isInitialized = PetscInitializeCalled;
111e5c89e4eSSatish Balay   PetscFunctionReturn(0);
112e5c89e4eSSatish Balay }
113e5c89e4eSSatish Balay 
114e5c89e4eSSatish Balay #undef __FUNCT__
115e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalized"
116e5c89e4eSSatish Balay /*@
117e5c89e4eSSatish Balay       PetscFinalized - Determine whether PetscFinalize() has been called yet
118e5c89e4eSSatish Balay 
119e5c89e4eSSatish Balay    Level: developer
120e5c89e4eSSatish Balay 
121e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
122e5c89e4eSSatish Balay @*/
123ace3abfcSBarry Smith PetscErrorCode PETSCSYS_DLLEXPORT PetscFinalized(PetscBool  *isFinalized)
124e5c89e4eSSatish Balay {
125e5c89e4eSSatish Balay   PetscFunctionBegin;
126e5c89e4eSSatish Balay   PetscValidPointer(isFinalized, 1);
127e5c89e4eSSatish Balay   *isFinalized = PetscFinalizeCalled;
128e5c89e4eSSatish Balay   PetscFunctionReturn(0);
129e5c89e4eSSatish Balay }
130e5c89e4eSSatish Balay 
131e5c89e4eSSatish Balay EXTERN PetscErrorCode        PetscOptionsCheckInitial_Private(void);
132ace3abfcSBarry Smith extern PetscBool  PetscBeganMPI;
133e5c89e4eSSatish Balay 
134e5c89e4eSSatish Balay /*
135e5c89e4eSSatish Balay        This function is the MPI reduction operation used to compute the sum of the
136e5c89e4eSSatish Balay    first half of the datatype and the max of the second half.
137e5c89e4eSSatish Balay */
138e5c89e4eSSatish Balay MPI_Op PetscMaxSum_Op = 0;
139e5c89e4eSSatish Balay 
140e5c89e4eSSatish Balay EXTERN_C_BEGIN
141e5c89e4eSSatish Balay #undef __FUNCT__
142e5c89e4eSSatish Balay #define __FUNCT__ "PetscMaxSum_Local"
1438738c821SJed Brown void PETSCSYS_DLLEXPORT MPIAPI PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
144e5c89e4eSSatish Balay {
145e5c89e4eSSatish Balay   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;
146e5c89e4eSSatish Balay 
147e5c89e4eSSatish Balay   PetscFunctionBegin;
148e5c89e4eSSatish Balay   if (*datatype != MPIU_2INT) {
149e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
150e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
151e5c89e4eSSatish Balay   }
152e5c89e4eSSatish Balay 
153e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
154e5c89e4eSSatish Balay     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
155e5c89e4eSSatish Balay     xout[2*i+1] += xin[2*i+1];
156e5c89e4eSSatish Balay   }
157812af9f3SBarry Smith   PetscFunctionReturnVoid();
158e5c89e4eSSatish Balay }
159e5c89e4eSSatish Balay EXTERN_C_END
160e5c89e4eSSatish Balay 
161e5c89e4eSSatish Balay /*
162e5c89e4eSSatish Balay     Returns the max of the first entry owned by this processor and the
163e5c89e4eSSatish Balay sum of the second entry.
164b693b147SBarry Smith 
165b693b147SBarry Smith     The reason nprocs[2*i] contains lengths nprocs[2*i+1] contains flag of 1 if length is nonzero
166b693b147SBarry Smith is so that the PetscMaxSum_Op() can set TWO values, if we passed in only nprocs[i] with lengths
167b693b147SBarry Smith there would be no place to store the both needed results.
168e5c89e4eSSatish Balay */
169e5c89e4eSSatish Balay #undef __FUNCT__
170e5c89e4eSSatish Balay #define __FUNCT__ "PetscMaxSum"
1718738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum)
172e5c89e4eSSatish Balay {
173e5c89e4eSSatish Balay   PetscMPIInt    size,rank;
174e5c89e4eSSatish Balay   PetscInt       *work;
175e5c89e4eSSatish Balay   PetscErrorCode ierr;
176e5c89e4eSSatish Balay 
177e5c89e4eSSatish Balay   PetscFunctionBegin;
178e5c89e4eSSatish Balay   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
179e5c89e4eSSatish Balay   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
180e5c89e4eSSatish Balay   ierr   = PetscMalloc(2*size*sizeof(PetscInt),&work);CHKERRQ(ierr);
181e5c89e4eSSatish Balay   ierr   = MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
182e5c89e4eSSatish Balay   *max   = work[2*rank];
183e5c89e4eSSatish Balay   *sum   = work[2*rank+1];
184e5c89e4eSSatish Balay   ierr   = PetscFree(work);CHKERRQ(ierr);
185e5c89e4eSSatish Balay   PetscFunctionReturn(0);
186e5c89e4eSSatish Balay }
187e5c89e4eSSatish Balay 
188e5c89e4eSSatish Balay /* ----------------------------------------------------------------------------*/
1898738c821SJed Brown MPI_Op PETSCSYS_DLLEXPORT PetscADMax_Op = 0;
190e5c89e4eSSatish Balay 
191e5c89e4eSSatish Balay EXTERN_C_BEGIN
192e5c89e4eSSatish Balay #undef __FUNCT__
193e5c89e4eSSatish Balay #define __FUNCT__ "PetscADMax_Local"
1948738c821SJed Brown void PETSCSYS_DLLEXPORT MPIAPI PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
195e5c89e4eSSatish Balay {
196e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
197e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
198e5c89e4eSSatish Balay 
199e5c89e4eSSatish Balay   PetscFunctionBegin;
200e5c89e4eSSatish Balay   if (*datatype != MPIU_2SCALAR) {
201e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
202e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
203e5c89e4eSSatish Balay   }
204e5c89e4eSSatish Balay 
205e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
206e5c89e4eSSatish Balay     if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) {
207e5c89e4eSSatish Balay       xout[2*i]   = xin[2*i];
208e5c89e4eSSatish Balay       xout[2*i+1] = xin[2*i+1];
209e5c89e4eSSatish Balay     }
210e5c89e4eSSatish Balay   }
211812af9f3SBarry Smith   PetscFunctionReturnVoid();
212e5c89e4eSSatish Balay }
213e5c89e4eSSatish Balay EXTERN_C_END
214e5c89e4eSSatish Balay 
2158738c821SJed Brown MPI_Op PETSCSYS_DLLEXPORT PetscADMin_Op = 0;
216e5c89e4eSSatish Balay 
217e5c89e4eSSatish Balay EXTERN_C_BEGIN
218e5c89e4eSSatish Balay #undef __FUNCT__
219e5c89e4eSSatish Balay #define __FUNCT__ "PetscADMin_Local"
2208738c821SJed Brown void PETSCSYS_DLLEXPORT MPIAPI PetscADMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
221e5c89e4eSSatish Balay {
222e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
223e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
224e5c89e4eSSatish Balay 
225e5c89e4eSSatish Balay   PetscFunctionBegin;
226e5c89e4eSSatish Balay   if (*datatype != MPIU_2SCALAR) {
227e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
228e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
229e5c89e4eSSatish Balay   }
230e5c89e4eSSatish Balay 
231e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
232e5c89e4eSSatish Balay     if (PetscRealPart(xout[2*i]) > PetscRealPart(xin[2*i])) {
233e5c89e4eSSatish Balay       xout[2*i]   = xin[2*i];
234e5c89e4eSSatish Balay       xout[2*i+1] = xin[2*i+1];
235e5c89e4eSSatish Balay     }
236e5c89e4eSSatish Balay   }
237812af9f3SBarry Smith   PetscFunctionReturnVoid();
238e5c89e4eSSatish Balay }
239e5c89e4eSSatish Balay EXTERN_C_END
240e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
241e5c89e4eSSatish Balay 
242e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
2432c876bd9SBarry Smith 
2442c876bd9SBarry Smith /*
2452c876bd9SBarry Smith     This operation is only needed when using complex numbers with older MPI that does not support complex numbers
2462c876bd9SBarry Smith */
2472c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
24806a205a8SBarry Smith MPI_Op MPIU_SUM = 0;
249e5c89e4eSSatish Balay 
250e5c89e4eSSatish Balay EXTERN_C_BEGIN
251e5c89e4eSSatish Balay #undef __FUNCT__
252e5c89e4eSSatish Balay #define __FUNCT__ "PetscSum_Local"
2538738c821SJed Brown void PETSCSYS_DLLEXPORT PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
254e5c89e4eSSatish Balay {
255e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar *)in,*xout = (PetscScalar*)out;
256e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
257e5c89e4eSSatish Balay 
258e5c89e4eSSatish Balay   PetscFunctionBegin;
259e5c89e4eSSatish Balay   if (*datatype != MPIU_SCALAR) {
260e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_SCALAR data (i.e. double or complex) types");
261e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
262e5c89e4eSSatish Balay   }
263e5c89e4eSSatish Balay 
264e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
265e5c89e4eSSatish Balay     xout[i] += xin[i];
266e5c89e4eSSatish Balay   }
267812af9f3SBarry Smith   PetscFunctionReturnVoid();
268e5c89e4eSSatish Balay }
269e5c89e4eSSatish Balay EXTERN_C_END
270e5c89e4eSSatish Balay #endif
2712c876bd9SBarry Smith #endif
272e5c89e4eSSatish Balay 
273480cf27aSJed Brown EXTERN_C_BEGIN
274480cf27aSJed Brown #undef __FUNCT__
275480cf27aSJed Brown #define __FUNCT__ "Petsc_DelCounter"
276480cf27aSJed Brown /*
277480cf27aSJed Brown    Private routine to delete internal tag/name counter storage when a communicator is freed.
278480cf27aSJed Brown 
279480cf27aSJed Brown    This is called by MPI, not by users.
280480cf27aSJed Brown 
281480cf27aSJed Brown    Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
282480cf27aSJed Brown 
283480cf27aSJed Brown */
2848738c821SJed Brown PetscMPIInt PETSCSYS_DLLEXPORT MPIAPI Petsc_DelCounter(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
285480cf27aSJed Brown {
286480cf27aSJed Brown   PetscErrorCode ierr;
287480cf27aSJed Brown 
288480cf27aSJed Brown   PetscFunctionBegin;
289480cf27aSJed Brown   ierr = PetscInfo1(0,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
290480cf27aSJed Brown   ierr = PetscFree(count_val);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
291480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
292480cf27aSJed Brown }
293480cf27aSJed Brown EXTERN_C_END
294480cf27aSJed Brown 
295480cf27aSJed Brown EXTERN_C_BEGIN
296480cf27aSJed Brown #undef __FUNCT__
297480cf27aSJed Brown #define __FUNCT__ "Petsc_DelComm"
298480cf27aSJed Brown /*
299480cf27aSJed Brown   This does not actually free anything, it simply marks when a reference count to an internal MPI_Comm reaches zero and the
300480cf27aSJed Brown   the external MPI_Comm drops its reference to the internal MPI_Comm
301480cf27aSJed Brown 
302480cf27aSJed Brown   This is called by MPI, not by users.
303480cf27aSJed Brown 
304480cf27aSJed Brown   Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
305480cf27aSJed Brown 
306480cf27aSJed Brown */
3078738c821SJed Brown PetscMPIInt PETSCSYS_DLLEXPORT MPIAPI Petsc_DelComm(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
308480cf27aSJed Brown {
309480cf27aSJed Brown   PetscErrorCode ierr;
310480cf27aSJed Brown 
311480cf27aSJed Brown   PetscFunctionBegin;
312480cf27aSJed Brown   ierr = PetscInfo1(0,"Deleting PETSc communicator imbedded in a user MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
313480cf27aSJed Brown   /* actually don't delete anything because we cannot increase the reference count of the communicator anyways */
314480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
315480cf27aSJed Brown }
316480cf27aSJed Brown EXTERN_C_END
317480cf27aSJed Brown 
318951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
319e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
320e39fd77fSBarry Smith EXTERN_C_BEGIN
321e39fd77fSBarry Smith extern PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype,MPI_Aint*,void*);
322e39fd77fSBarry Smith extern PetscMPIInt PetscDataRep_read_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
323e39fd77fSBarry Smith extern PetscMPIInt PetscDataRep_write_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
324e39fd77fSBarry Smith EXTERN_C_END
325e39fd77fSBarry Smith #endif
326951e3c8eSBarry Smith #endif
327e39fd77fSBarry Smith 
3286ae9a8a6SBarry Smith int  PetscGlobalArgc   = 0;
3296ae9a8a6SBarry Smith char **PetscGlobalArgs = 0;
330e5c89e4eSSatish Balay 
331e5c89e4eSSatish Balay #undef __FUNCT__
332e5c89e4eSSatish Balay #define __FUNCT__ "PetscGetArgs"
333e5c89e4eSSatish Balay /*@C
334e5c89e4eSSatish Balay    PetscGetArgs - Allows you to access the raw command line arguments anywhere
335e5c89e4eSSatish Balay      after PetscInitialize() is called but before PetscFinalize().
336e5c89e4eSSatish Balay 
337e5c89e4eSSatish Balay    Not Collective
338e5c89e4eSSatish Balay 
339e5c89e4eSSatish Balay    Output Parameters:
340e5c89e4eSSatish Balay +  argc - count of number of command line arguments
341e5c89e4eSSatish Balay -  args - the command line arguments
342e5c89e4eSSatish Balay 
343e5c89e4eSSatish Balay    Level: intermediate
344e5c89e4eSSatish Balay 
345e5c89e4eSSatish Balay    Notes:
346e5c89e4eSSatish Balay       This is usually used to pass the command line arguments into other libraries
347e5c89e4eSSatish Balay    that are called internally deep in PETSc or the application.
348e5c89e4eSSatish Balay 
349f177e3b1SBarry Smith       The first argument contains the program name as is normal for C arguments.
350f177e3b1SBarry Smith 
351e5c89e4eSSatish Balay    Concepts: command line arguments
352e5c89e4eSSatish Balay 
353793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()
354e5c89e4eSSatish Balay 
355e5c89e4eSSatish Balay @*/
3568738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscGetArgs(int *argc,char ***args)
357e5c89e4eSSatish Balay {
358e5c89e4eSSatish Balay   PetscFunctionBegin;
35917186662SBarry Smith   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
360e5c89e4eSSatish Balay   *argc = PetscGlobalArgc;
361e5c89e4eSSatish Balay   *args = PetscGlobalArgs;
362e5c89e4eSSatish Balay   PetscFunctionReturn(0);
363e5c89e4eSSatish Balay }
364e5c89e4eSSatish Balay 
365e5c89e4eSSatish Balay #undef __FUNCT__
366793721a6SBarry Smith #define __FUNCT__ "PetscGetArguments"
367793721a6SBarry Smith /*@C
368793721a6SBarry Smith    PetscGetArguments - Allows you to access the  command line arguments anywhere
369793721a6SBarry Smith      after PetscInitialize() is called but before PetscFinalize().
370793721a6SBarry Smith 
371793721a6SBarry Smith    Not Collective
372793721a6SBarry Smith 
373793721a6SBarry Smith    Output Parameters:
374793721a6SBarry Smith .  args - the command line arguments
375793721a6SBarry Smith 
376793721a6SBarry Smith    Level: intermediate
377793721a6SBarry Smith 
378793721a6SBarry Smith    Notes:
379793721a6SBarry Smith       This does NOT start with the program name and IS null terminated (final arg is void)
380793721a6SBarry Smith 
381793721a6SBarry Smith    Concepts: command line arguments
382793721a6SBarry Smith 
383793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()
384793721a6SBarry Smith 
385793721a6SBarry Smith @*/
3868738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscGetArguments(char ***args)
387793721a6SBarry Smith {
388793721a6SBarry Smith   PetscInt       i,argc = PetscGlobalArgc;
389793721a6SBarry Smith   PetscErrorCode ierr;
390793721a6SBarry Smith 
391793721a6SBarry Smith   PetscFunctionBegin;
39217186662SBarry Smith   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
393717030eeSLisandro Dalcin   if (!argc) {*args = 0; PetscFunctionReturn(0);}
394793721a6SBarry Smith   ierr = PetscMalloc(argc*sizeof(char*),args);CHKERRQ(ierr);
395793721a6SBarry Smith   for (i=0; i<argc-1; i++) {
396793721a6SBarry Smith     ierr = PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);CHKERRQ(ierr);
397793721a6SBarry Smith   }
398793721a6SBarry Smith   (*args)[argc-1] = 0;
399793721a6SBarry Smith   PetscFunctionReturn(0);
400793721a6SBarry Smith }
401793721a6SBarry Smith 
402793721a6SBarry Smith #undef __FUNCT__
403793721a6SBarry Smith #define __FUNCT__ "PetscFreeArguments"
404793721a6SBarry Smith /*@C
405793721a6SBarry Smith    PetscFreeArguments - Frees the memory obtained with PetscGetArguments()
406793721a6SBarry Smith 
407793721a6SBarry Smith    Not Collective
408793721a6SBarry Smith 
409793721a6SBarry Smith    Output Parameters:
410793721a6SBarry Smith .  args - the command line arguments
411793721a6SBarry Smith 
412793721a6SBarry Smith    Level: intermediate
413793721a6SBarry Smith 
414793721a6SBarry Smith    Concepts: command line arguments
415793721a6SBarry Smith 
416793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()
417793721a6SBarry Smith 
418793721a6SBarry Smith @*/
4198738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscFreeArguments(char **args)
420793721a6SBarry Smith {
421793721a6SBarry Smith   PetscInt       i = 0;
422793721a6SBarry Smith   PetscErrorCode ierr;
423793721a6SBarry Smith 
424793721a6SBarry Smith   PetscFunctionBegin;
425717030eeSLisandro Dalcin   if (!args) {PetscFunctionReturn(0);}
426793721a6SBarry Smith   while (args[i]) {
427793721a6SBarry Smith     ierr = PetscFree(args[i]);CHKERRQ(ierr);
428793721a6SBarry Smith     i++;
429793721a6SBarry Smith   }
430793721a6SBarry Smith   ierr = PetscFree(args);CHKERRQ(ierr);
431793721a6SBarry Smith   PetscFunctionReturn(0);
432793721a6SBarry Smith }
433793721a6SBarry Smith 
434793721a6SBarry Smith #undef __FUNCT__
435e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialize"
436e5c89e4eSSatish Balay /*@C
437e5c89e4eSSatish Balay    PetscInitialize - Initializes the PETSc database and MPI.
438e5c89e4eSSatish Balay    PetscInitialize() calls MPI_Init() if that has yet to be called,
439e5c89e4eSSatish Balay    so this routine should always be called near the beginning of
440e5c89e4eSSatish Balay    your program -- usually the very first line!
441e5c89e4eSSatish Balay 
442e5c89e4eSSatish Balay    Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set
443e5c89e4eSSatish Balay 
444e5c89e4eSSatish Balay    Input Parameters:
445e5c89e4eSSatish Balay +  argc - count of number of command line arguments
446e5c89e4eSSatish Balay .  args - the command line arguments
447fc2bca9aSBarry Smith .  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL to not check for
448fc2bca9aSBarry Smith           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
449e5c89e4eSSatish Balay -  help - [optional] Help message to print, use PETSC_NULL for no message
450e5c89e4eSSatish Balay 
45105827820SBarry Smith    If you wish PETSc code to run ONLY on a subcommunicator of MPI_COMM_WORLD, create that
45205827820SBarry Smith    communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize(). Thus if you are running a
45305827820SBarry Smith    four process job and two processes will run PETSc and have PetscInitialize() and PetscFinalize() and two process will not,
45405827820SBarry Smith    then do this. If ALL processes in the job are using PetscInitialize() and PetscFinalize() then you don't need to do this, even
45505827820SBarry Smith    if different subcommunicators of the job are doing different things with PETSc.
456e5c89e4eSSatish Balay 
457e5c89e4eSSatish Balay    Options Database Keys:
458e5c89e4eSSatish Balay +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
459e5c89e4eSSatish Balay .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
460e5c89e4eSSatish Balay .  -on_error_emacs <machinename> causes emacsclient to jump to error file
461b52f573bSBarry Smith .  -on_error_abort calls abort() when error detected (no traceback)
462e8fb0fc0SBarry Smith .  -on_error_mpiabort calls MPI_abort() when error detected
463e8fb0fc0SBarry Smith .  -error_output_stderr prints error messages to stderr instead of the default stdout
464e8fb0fc0SBarry Smith .  -error_output_none does not print the error messages (but handles errors in the same way as if this was not called)
465e5c89e4eSSatish Balay .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
466e5c89e4eSSatish Balay .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
467e5c89e4eSSatish Balay .  -stop_for_debugger - Print message on how to attach debugger manually to
468e5c89e4eSSatish Balay                         process and wait (-debugger_pause) seconds for attachment
4692fb0ec9aSBarry Smith .  -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries)
470e5c89e4eSSatish Balay .  -malloc no - Indicates not to use error-checking malloc
4712fb0ec9aSBarry Smith .  -malloc_debug - check for memory corruption at EVERY malloc or free
472e5c89e4eSSatish Balay .  -fp_trap - Stops on floating point exceptions (Note that on the
473e5c89e4eSSatish Balay               IBM RS6000 this slows code by at least a factor of 10.)
474e5c89e4eSSatish Balay .  -no_signal_handler - Indicates not to trap error signals
475e5c89e4eSSatish Balay .  -shared_tmp - indicates /tmp directory is shared by all processors
476e5c89e4eSSatish Balay .  -not_shared_tmp - each processor has own /tmp
477e5c89e4eSSatish Balay .  -tmp - alternative name of /tmp directory
478e5c89e4eSSatish Balay .  -get_total_flops - returns total flops done by all processors
479e5c89e4eSSatish Balay -  -memory_info - Print memory usage at end of run
480e5c89e4eSSatish Balay 
481e5c89e4eSSatish Balay    Options Database Keys for Profiling:
4820598bfebSBarry Smith    See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details.
483e5c89e4eSSatish Balay +  -log_trace [filename] - Print traces of all PETSc calls
484e5c89e4eSSatish Balay         to the screen (useful to determine where a program
485e5c89e4eSSatish Balay         hangs without running in the debugger).  See PetscLogTraceBegin().
4866cf91177SBarry Smith .  -info <optional filename> - Prints verbose information to the screen
4876cf91177SBarry Smith -  -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages
488e5c89e4eSSatish Balay 
489e5c89e4eSSatish Balay    Environmental Variables:
490e5c89e4eSSatish Balay +   PETSC_TMP - alternative tmp directory
491e5c89e4eSSatish Balay .   PETSC_SHARED_TMP - tmp is shared by all processes
492e5c89e4eSSatish Balay .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
493e5c89e4eSSatish Balay .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
494e5c89e4eSSatish Balay -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to
495e5c89e4eSSatish Balay 
496e5c89e4eSSatish Balay 
497e5c89e4eSSatish Balay    Level: beginner
498e5c89e4eSSatish Balay 
499e5c89e4eSSatish Balay    Notes:
500e5c89e4eSSatish Balay    If for some reason you must call MPI_Init() separately, call
501e5c89e4eSSatish Balay    it before PetscInitialize().
502e5c89e4eSSatish Balay 
503e5c89e4eSSatish Balay    Fortran Version:
504e5c89e4eSSatish Balay    In Fortran this routine has the format
505e5c89e4eSSatish Balay $       call PetscInitialize(file,ierr)
506e5c89e4eSSatish Balay 
507e5c89e4eSSatish Balay +   ierr - error return code
5083dae0d48SMatthew Knepley -  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use PETSC_NULL_CHARACTER to not check for
509fc2bca9aSBarry Smith           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
510e5c89e4eSSatish Balay 
511e5c89e4eSSatish Balay    Important Fortran Note:
512e5c89e4eSSatish Balay    In Fortran, you MUST use PETSC_NULL_CHARACTER to indicate a
513e5c89e4eSSatish Balay    null character string; you CANNOT just use PETSC_NULL as
5140598bfebSBarry Smith    in the C version. See the <a href="../../docs/manual.pdf">users manual</a> for details.
515e5c89e4eSSatish Balay 
51601cb0274SBarry Smith    If your main program is C but you call Fortran code that also uses PETSc you need to call PetscInitializeFortran() soon after
51701cb0274SBarry Smith    calling PetscInitialize().
518e5c89e4eSSatish Balay 
519e5c89e4eSSatish Balay    Concepts: initializing PETSc
520e5c89e4eSSatish Balay 
52101cb0274SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments()
522e5c89e4eSSatish Balay 
523e5c89e4eSSatish Balay @*/
5248738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscInitialize(int *argc,char ***args,const char file[],const char help[])
525e5c89e4eSSatish Balay {
526e5c89e4eSSatish Balay   PetscErrorCode ierr;
527aa5bb8c0SSatish Balay   PetscMPIInt    flag, size;
528aa5bb8c0SSatish Balay   PetscInt       nodesize;
529ace3abfcSBarry Smith   PetscBool      flg;
530e5c89e4eSSatish Balay   char           hostname[256];
531e5c89e4eSSatish Balay 
532e5c89e4eSSatish Balay   PetscFunctionBegin;
533e5c89e4eSSatish Balay   if (PetscInitializeCalled) PetscFunctionReturn(0);
534e5c89e4eSSatish Balay 
535ae9b4142SLisandro Dalcin   /* these must be initialized in a routine, not as a constant declaration*/
536d89683f4Sbcordonn   PETSC_STDOUT = stdout;
537ae9b4142SLisandro Dalcin   PETSC_STDERR = stderr;
538e5c89e4eSSatish Balay 
539e5c89e4eSSatish Balay   ierr = PetscOptionsCreate();CHKERRQ(ierr);
540e5c89e4eSSatish Balay 
541e5c89e4eSSatish Balay   /*
542e5c89e4eSSatish Balay      We initialize the program name here (before MPI_Init()) because MPICH has a bug in
543e5c89e4eSSatish Balay      it that it sets args[0] on all processors to be args[0] on the first processor.
544e5c89e4eSSatish Balay   */
545e5c89e4eSSatish Balay   if (argc && *argc) {
546e5c89e4eSSatish Balay     ierr = PetscSetProgramName(**args);CHKERRQ(ierr);
547e5c89e4eSSatish Balay   } else {
548e5c89e4eSSatish Balay     ierr = PetscSetProgramName("Unknown Name");CHKERRQ(ierr);
549e5c89e4eSSatish Balay   }
550e5c89e4eSSatish Balay 
551e5c89e4eSSatish Balay   ierr = MPI_Initialized(&flag);CHKERRQ(ierr);
552e5c89e4eSSatish Balay   if (!flag) {
553e32f2f54SBarry 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");
554e5c89e4eSSatish Balay     ierr          = MPI_Init(argc,args);CHKERRQ(ierr);
555e5c89e4eSSatish Balay     PetscBeganMPI = PETSC_TRUE;
556e5c89e4eSSatish Balay   }
557e5c89e4eSSatish Balay   if (argc && args) {
558e5c89e4eSSatish Balay     PetscGlobalArgc = *argc;
559e5c89e4eSSatish Balay     PetscGlobalArgs = *args;
560e5c89e4eSSatish Balay   }
561e5c89e4eSSatish Balay   PetscFinalizeCalled   = PETSC_FALSE;
562e5c89e4eSSatish Balay 
563e8373e55SMatthew Knepley   if (PETSC_COMM_WORLD == MPI_COMM_NULL) {
564e5c89e4eSSatish Balay     PETSC_COMM_WORLD = MPI_COMM_WORLD;
565e5c89e4eSSatish Balay   }
566660746e0SBarry Smith   ierr = MPI_Errhandler_set(PETSC_COMM_WORLD,MPI_ERRORS_RETURN);CHKERRQ(ierr);
567e5c89e4eSSatish Balay 
568e5c89e4eSSatish Balay   /* Done after init due to a bug in MPICH-GM? */
569e5c89e4eSSatish Balay   ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr);
570e5c89e4eSSatish Balay 
571e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);CHKERRQ(ierr);
572e5c89e4eSSatish Balay   ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);CHKERRQ(ierr);
573e5c89e4eSSatish Balay 
574e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
575e5c89e4eSSatish Balay   /*
576e5c89e4eSSatish Balay      Initialized the global complex variable; this is because with
577e5c89e4eSSatish Balay      shared libraries the constructors for global variables
578e5c89e4eSSatish Balay      are not called; at least on IRIX.
579e5c89e4eSSatish Balay   */
580e5c89e4eSSatish Balay   {
581762437b8SSatish Balay #if defined(PETSC_CLANGUAGE_CXX)
582e5c89e4eSSatish Balay     PetscScalar ic(0.0,1.0);
583e5c89e4eSSatish Balay     PETSC_i = ic;
584b7940d39SSatish Balay #else
5853433f298SSatish Balay     PETSC_i = I;
586b7940d39SSatish Balay #endif
587762437b8SSatish Balay   }
588762437b8SSatish Balay 
5892c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
5902c876bd9SBarry Smith   ierr = MPI_Type_contiguous(2,MPIU_REAL,&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
5912c876bd9SBarry Smith   ierr = MPI_Type_commit(&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
592a83b8d76SBarry Smith   ierr = MPI_Type_contiguous(2,MPI_FLOAT,&MPI_C_COMPLEX);CHKERRQ(ierr);
593a83b8d76SBarry Smith   ierr = MPI_Type_commit(&MPI_C_COMPLEX);CHKERRQ(ierr);
59406a205a8SBarry Smith   ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);CHKERRQ(ierr);
595e5c89e4eSSatish Balay #endif
5962c876bd9SBarry Smith #endif
597e5c89e4eSSatish Balay 
598e5c89e4eSSatish Balay   /*
599e5c89e4eSSatish Balay      Create the PETSc MPI reduction operator that sums of the first
600e5c89e4eSSatish Balay      half of the entries and maxes the second half.
601e5c89e4eSSatish Balay   */
602e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);CHKERRQ(ierr);
603e5c89e4eSSatish Balay 
604e5c89e4eSSatish Balay   ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);CHKERRQ(ierr);
605e5c89e4eSSatish Balay   ierr = MPI_Type_commit(&MPIU_2SCALAR);CHKERRQ(ierr);
606e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);CHKERRQ(ierr);
607e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);CHKERRQ(ierr);
608e5c89e4eSSatish Balay 
609e5c89e4eSSatish Balay   ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);CHKERRQ(ierr);
610e5c89e4eSSatish Balay   ierr = MPI_Type_commit(&MPIU_2INT);CHKERRQ(ierr);
611e5c89e4eSSatish Balay 
612e5c89e4eSSatish Balay   /*
613480cf27aSJed Brown      Attributes to be set on PETSc communicators
614480cf27aSJed Brown   */
615480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelCounter,&Petsc_Counter_keyval,(void*)0);CHKERRQ(ierr);
616480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_InnerComm_keyval,(void*)0);CHKERRQ(ierr);
617480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm,&Petsc_OuterComm_keyval,(void*)0);CHKERRQ(ierr);
618480cf27aSJed Brown 
619480cf27aSJed Brown   /*
620e8fb0fc0SBarry Smith      Build the options database
621e5c89e4eSSatish Balay   */
622e5c89e4eSSatish Balay   ierr = PetscOptionsInsert(argc,args,file);CHKERRQ(ierr);
623e5c89e4eSSatish Balay 
6246dc8fec2Sbcordonn 
625e5c89e4eSSatish Balay   /*
626e5c89e4eSSatish Balay      Print main application help message
627e5c89e4eSSatish Balay   */
628e5c89e4eSSatish Balay   ierr = PetscOptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
629e5c89e4eSSatish Balay   if (help && flg) {
630e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,help);CHKERRQ(ierr);
631e5c89e4eSSatish Balay   }
632e5c89e4eSSatish Balay   ierr = PetscOptionsCheckInitial_Private();CHKERRQ(ierr);
633e5c89e4eSSatish Balay 
634e5c89e4eSSatish Balay   /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */
635a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
636e5c89e4eSSatish Balay   ierr = PetscLogBegin_Private();CHKERRQ(ierr);
637a9f03627SSatish Balay #endif
638e5c89e4eSSatish Balay 
639e5c89e4eSSatish Balay   /*
640e5c89e4eSSatish Balay      Load the dynamic libraries (on machines that support them), this registers all
641e5c89e4eSSatish Balay      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
642e5c89e4eSSatish Balay   */
643e5c89e4eSSatish Balay   ierr = PetscInitialize_DynamicLibraries();CHKERRQ(ierr);
644e5c89e4eSSatish Balay 
645e5c89e4eSSatish Balay   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
646ae15b995SBarry Smith   ierr = PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);CHKERRQ(ierr);
647e5c89e4eSSatish Balay   ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr);
648ae15b995SBarry Smith   ierr = PetscInfo1(0,"Running on machine: %s\n",hostname);CHKERRQ(ierr);
649e5c89e4eSSatish Balay 
650e5c89e4eSSatish Balay   ierr = PetscOptionsCheckInitial_Components();CHKERRQ(ierr);
651ef6c6fedSBoyana Norris   /* Check the options database for options related to the options database itself */
652ef6c6fedSBoyana Norris   ierr = PetscOptionsSetFromOptions();CHKERRQ(ierr);
653ef6c6fedSBoyana Norris 
654951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
655e39fd77fSBarry Smith   /*
656e39fd77fSBarry Smith       Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI
657e39fd77fSBarry Smith 
658e39fd77fSBarry Smith       Currently not used because it is not supported by MPICH.
659e39fd77fSBarry Smith   */
660e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
661708350f5SSatish Balay   ierr = MPI_Register_datarep((char *)"petsc",PetscDataRep_read_conv_fn,PetscDataRep_write_conv_fn,PetscDataRep_extent_fn,PETSC_NULL);CHKERRQ(ierr);
662e39fd77fSBarry Smith #endif
663951e3c8eSBarry Smith #endif
664e39fd77fSBarry Smith 
665793721a6SBarry Smith   ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_spawn_size",&nodesize,&flg);CHKERRQ(ierr);
666793721a6SBarry Smith   if (flg) {
66723464e94SBarry Smith #if defined(PETSC_HAVE_MPI_COMM_SPAWN)
6689505b675SBarry Smith     ierr = PetscOpenMPSpawn((PetscMPIInt) nodesize);CHKERRQ(ierr); /* worker nodes never return from here; they go directly to PetscEnd() */
66923464e94SBarry Smith #else
670e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"PETSc built without MPI 2 (MPI_Comm_spawn) support, use -openmp_merge_size instead");
67123464e94SBarry Smith #endif
672793721a6SBarry Smith   } else {
67323464e94SBarry Smith     ierr = PetscOptionsGetInt(PETSC_NULL,"-openmp_merge_size",&nodesize,&flg);CHKERRQ(ierr);
6748002f1cdSBarry Smith     if (flg) {
6759505b675SBarry Smith       ierr = PetscOpenMPMerge((PetscMPIInt) nodesize,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
6769505b675SBarry Smith       if (PetscOpenMPWorker) { /* if worker then never enter user code */
6779505b675SBarry Smith         ierr = PetscEnd();
6789505b675SBarry Smith       }
6798002f1cdSBarry Smith     }
680793721a6SBarry Smith   }
68190d69ab7SBarry Smith   flg  = PETSC_FALSE;
68290d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-python",&flg,PETSC_NULL);CHKERRQ(ierr);
683192daf7cSBarry Smith   if (flg) {ierr = PetscPythonInitialize(PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);}
684e5c89e4eSSatish Balay 
68535d88935SVictor Minden #if defined(PETSC_HAVE_CUDA)
6862f947c57SVictor Minden   cublasInit();
6873e39abd9SVictor Minden #endif
68892e62aa6SBarry Smith 
68992e62aa6SBarry Smith #if defined(PETSC_HAVE_AMS)
690c457296dSBarry Smith   ierr = PetscOptionsHasName(PETSC_NULL,"-ams_publish_objects",&flg);CHKERRQ(ierr);
69192e62aa6SBarry Smith   if (flg) {
69292e62aa6SBarry Smith     PetscAMSPublishAll = PETSC_TRUE;
69392e62aa6SBarry Smith   }
69492e62aa6SBarry Smith #endif
69592e62aa6SBarry Smith 
696301d30feSBarry Smith   /*
697301d30feSBarry Smith       Once we are completedly initialized then we can set this variables
698301d30feSBarry Smith   */
699301d30feSBarry Smith   PetscInitializeCalled = PETSC_TRUE;
700301d30feSBarry Smith   PetscFunctionReturn(0);
701e5c89e4eSSatish Balay }
702e5c89e4eSSatish Balay 
703e5c89e4eSSatish Balay 
704e5c89e4eSSatish Balay #undef __FUNCT__
705e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize"
706e5c89e4eSSatish Balay /*@C
707e5c89e4eSSatish Balay    PetscFinalize - Checks for options to be called at the conclusion
708e5c89e4eSSatish Balay    of the program. MPI_Finalize() is called only if the user had not
709e5c89e4eSSatish Balay    called MPI_Init() before calling PetscInitialize().
710e5c89e4eSSatish Balay 
711e5c89e4eSSatish Balay    Collective on PETSC_COMM_WORLD
712e5c89e4eSSatish Balay 
713e5c89e4eSSatish Balay    Options Database Keys:
714e5c89e4eSSatish Balay +  -options_table - Calls PetscOptionsPrint()
715e5c89e4eSSatish Balay .  -options_left - Prints unused options that remain in the database
716e5c89e4eSSatish Balay .  -options_left no - Does not print unused options that remain in the database
717e5c89e4eSSatish Balay .  -mpidump - Calls PetscMPIDump()
718e5c89e4eSSatish Balay .  -malloc_dump - Calls PetscMallocDump()
719e5c89e4eSSatish Balay .  -malloc_info - Prints total memory usage
720e5c89e4eSSatish Balay -  -malloc_log - Prints summary of memory usage
721e5c89e4eSSatish Balay 
722e5c89e4eSSatish Balay    Options Database Keys for Profiling:
7230598bfebSBarry Smith    See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details.
724e5c89e4eSSatish Balay +  -log_summary [filename] - Prints summary of flop and timing
725e5c89e4eSSatish Balay         information to screen. If the filename is specified the
726ff5bc46bSBarry Smith         summary is written to the file.  See PetscLogPrintSummary().
727ff5bc46bSBarry Smith .  -log_summary_python [filename] - Prints data on of flop and timing usage to a file or screen.
728ff5bc46bSBarry Smith         See PetscLogPrintSummaryPy().
729e5c89e4eSSatish Balay .  -log_all [filename] - Logs extensive profiling information
730ff5bc46bSBarry Smith         See PetscLogDump().
731ff5bc46bSBarry Smith .  -log [filename] - Logs basic profiline information  See PetscLogDump().
732e5c89e4eSSatish Balay .  -log_sync - Log the synchronization in scatters, inner products
733e5c89e4eSSatish Balay         and norms
734e5c89e4eSSatish Balay -  -log_mpe [filename] - Creates a logfile viewable by the
735e5c89e4eSSatish Balay       utility Upshot/Nupshot (in MPICH distribution)
736e5c89e4eSSatish Balay 
737e5c89e4eSSatish Balay    Level: beginner
738e5c89e4eSSatish Balay 
739e5c89e4eSSatish Balay    Note:
740e5c89e4eSSatish Balay    See PetscInitialize() for more general runtime options.
741e5c89e4eSSatish Balay 
742e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
743e5c89e4eSSatish Balay @*/
7448738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscFinalize(void)
745e5c89e4eSSatish Balay {
746e5c89e4eSSatish Balay   PetscErrorCode ierr;
747e5c89e4eSSatish Balay   PetscMPIInt    rank;
748e5c89e4eSSatish Balay   int            nopt;
749ace3abfcSBarry Smith   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
750d5649816SBarry Smith #if defined(PETSC_HAVE_AMS)
751ace3abfcSBarry Smith   PetscBool      flg = PETSC_FALSE;
752d5649816SBarry Smith #endif
753e5c89e4eSSatish Balay 
754e5c89e4eSSatish Balay   PetscFunctionBegin;
755e5c89e4eSSatish Balay 
756e5c89e4eSSatish Balay   if (!PetscInitializeCalled) {
757*4b09e917SBarry Smith     printf("PetscInitialize() must be called before PetscFinalize()\n");
758*4b09e917SBarry Smith     PetscFunctionReturn(PETSC_ERR_ARG_WRONGSTATE);
759e5c89e4eSSatish Balay   }
760b022a5c1SBarry Smith   ierr = PetscInfo(PETSC_NULL,"PetscFinalize() called\n");
761b022a5c1SBarry Smith 
762d5649816SBarry Smith #if defined(PETSC_HAVE_AMS)
763d5649816SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-options_gui",&flg,PETSC_NULL);CHKERRQ(ierr);
764d5649816SBarry Smith   if (flg) {
765d5649816SBarry Smith     ierr = PetscOptionsAMSDestroy();CHKERRQ(ierr);
766d5649816SBarry Smith   }
767d5649816SBarry Smith #endif
768d5649816SBarry Smith 
7698002f1cdSBarry Smith   ierr = PetscOpenMPFinalize();CHKERRQ(ierr);
7708002f1cdSBarry Smith 
77190d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-python",&flg1,PETSC_NULL);CHKERRQ(ierr);
772192daf7cSBarry Smith   if (flg1) {ierr = PetscPythonFinalize();CHKERRQ(ierr);}
773192daf7cSBarry Smith 
774e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
77590d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-malloc_info",&flg2,PETSC_NULL);CHKERRQ(ierr);
776e5c89e4eSSatish Balay   if (!flg2) {
77790d69ab7SBarry Smith     flg2 = PETSC_FALSE;
77890d69ab7SBarry Smith     ierr = PetscOptionsGetTruth(PETSC_NULL,"-memory_info",&flg2,PETSC_NULL);CHKERRQ(ierr);
779e5c89e4eSSatish Balay   }
780e5c89e4eSSatish Balay   if (flg2) {
781e5c89e4eSSatish Balay     ierr = PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");CHKERRQ(ierr);
782e5c89e4eSSatish Balay   }
783e5c89e4eSSatish Balay 
7843fa76a5bSLisandro Dalcin   /*
7853fa76a5bSLisandro Dalcin      Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_().
7863fa76a5bSLisandro Dalcin   */
7873fa76a5bSLisandro Dalcin   ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr);
7883fa76a5bSLisandro Dalcin 
7893fa76a5bSLisandro Dalcin   /*
7903fa76a5bSLisandro Dalcin        Free all the registered create functions, such as KSPList, VecList, SNESList, etc
7913fa76a5bSLisandro Dalcin   */
7923fa76a5bSLisandro Dalcin   ierr = PetscFListDestroyAll();CHKERRQ(ierr);
7933fa76a5bSLisandro Dalcin 
7943fa76a5bSLisandro Dalcin   /*
7953fa76a5bSLisandro Dalcin      Destroy any packages that registered a finalize
7963fa76a5bSLisandro Dalcin   */
797eb8be38cSBarry Smith   ierr = PetscRegisterFinalizeAll();CHKERRQ(ierr);
798e5c89e4eSSatish Balay 
799e5c89e4eSSatish Balay   /*
800e5c89e4eSSatish Balay      Destroy all the function registration lists created
801e5c89e4eSSatish Balay   */
802e5c89e4eSSatish Balay   ierr = PetscFinalize_DynamicLibraries();CHKERRQ(ierr);
803e5c89e4eSSatish Balay 
804e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
80590d69ab7SBarry Smith   flg1 = PETSC_FALSE;
80690d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-get_total_flops",&flg1,PETSC_NULL);CHKERRQ(ierr);
807e5c89e4eSSatish Balay   if (flg1) {
808e5c89e4eSSatish Balay     PetscLogDouble flops = 0;
809e5c89e4eSSatish Balay     ierr = MPI_Reduce(&_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
810e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);CHKERRQ(ierr);
811e5c89e4eSSatish Balay   }
812e5c89e4eSSatish Balay #endif
813e5c89e4eSSatish Balay 
81463d6bff0SBarry Smith #if defined(PETSC_USE_DEBUG) && !defined(PETSC_USE_PTHREAD)
815e5c89e4eSSatish Balay   if (PetscStackActive) {
816e5c89e4eSSatish Balay     ierr = PetscStackDestroy();CHKERRQ(ierr);
817e5c89e4eSSatish Balay   }
818e5c89e4eSSatish Balay #endif
819e5c89e4eSSatish Balay 
820e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
821e5c89e4eSSatish Balay   {
822e5c89e4eSSatish Balay     char mname[PETSC_MAX_PATH_LEN];
823e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE)
824e5c89e4eSSatish Balay     mname[0] = 0;
825e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
826e5c89e4eSSatish Balay     if (flg1){
827e5c89e4eSSatish Balay       if (mname[0]) {ierr = PetscLogMPEDump(mname);CHKERRQ(ierr);}
828e5c89e4eSSatish Balay       else          {ierr = PetscLogMPEDump(0);CHKERRQ(ierr);}
829e5c89e4eSSatish Balay     }
830e5c89e4eSSatish Balay #endif
831e5c89e4eSSatish Balay     mname[0] = 0;
832e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
833e5c89e4eSSatish Balay     if (flg1) {
834e5c89e4eSSatish Balay       if (mname[0])  {ierr = PetscLogPrintSummary(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);}
835e5c89e4eSSatish Balay       else           {ierr = PetscLogPrintSummary(PETSC_COMM_WORLD,0);CHKERRQ(ierr);}
836e5c89e4eSSatish Balay     }
837e5c89e4eSSatish Balay 
838ff5bc46bSBarry Smith     mname[0] = 0;
839ff5bc46bSBarry Smith     ierr = PetscOptionsGetString(PETSC_NULL,"-log_summary_python",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
840ff5bc46bSBarry Smith     if (flg1) {
841ff5bc46bSBarry Smith       PetscViewer viewer;
842ff5bc46bSBarry Smith       if (mname[0])  {
843ff5bc46bSBarry Smith         ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);CHKERRQ(ierr);
844ff5bc46bSBarry Smith       } else {
845ff5bc46bSBarry Smith         viewer = PETSC_VIEWER_STDOUT_WORLD;
846ff5bc46bSBarry Smith       }
847ff5bc46bSBarry Smith       ierr = PetscLogPrintSummaryPython(viewer);CHKERRQ(ierr);
848ff5bc46bSBarry Smith       ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr);
849ff5bc46bSBarry Smith     }
850ff5bc46bSBarry Smith 
85178392ef1SBarry Smith     ierr = PetscOptionsGetString(PETSC_NULL,"-log_detailed",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
85278392ef1SBarry Smith     if (flg1) {
85378392ef1SBarry Smith       if (mname[0])  {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);}
85478392ef1SBarry Smith       else           {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,0);CHKERRQ(ierr);}
85578392ef1SBarry Smith     }
85678392ef1SBarry Smith 
857e5c89e4eSSatish Balay     mname[0] = 0;
858e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
859e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);CHKERRQ(ierr);
860e5c89e4eSSatish Balay     if (flg1 || flg2){
861e5c89e4eSSatish Balay       if (mname[0]) PetscLogDump(mname);
862e5c89e4eSSatish Balay       else          PetscLogDump(0);
863e5c89e4eSSatish Balay     }
864e5c89e4eSSatish Balay     ierr = PetscLogDestroy();CHKERRQ(ierr);
865e5c89e4eSSatish Balay   }
866e5c89e4eSSatish Balay #endif
86790d69ab7SBarry Smith   flg1 = PETSC_FALSE;
86890d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-no_signal_handler",&flg1,PETSC_NULL);CHKERRQ(ierr);
869e5c89e4eSSatish Balay   if (!flg1) { ierr = PetscPopSignalHandler();CHKERRQ(ierr);}
87090d69ab7SBarry Smith   flg1 = PETSC_FALSE;
87190d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-mpidump",&flg1,PETSC_NULL);CHKERRQ(ierr);
872e5c89e4eSSatish Balay   if (flg1) {
873e5c89e4eSSatish Balay     ierr = PetscMPIDump(stdout);CHKERRQ(ierr);
874e5c89e4eSSatish Balay   }
87590d69ab7SBarry Smith   flg1 = PETSC_FALSE;
87690d69ab7SBarry Smith   flg2 = PETSC_FALSE;
87790d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-malloc_dump",&flg1,PETSC_NULL);CHKERRQ(ierr);
87890d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-options_table",&flg2,PETSC_NULL);CHKERRQ(ierr);
879e5c89e4eSSatish Balay   if (flg2) {
880e5c89e4eSSatish Balay     if (!rank) {ierr = PetscOptionsPrint(stdout);CHKERRQ(ierr);}
881e5c89e4eSSatish Balay   }
882e5c89e4eSSatish Balay 
883e5c89e4eSSatish Balay   /* to prevent PETSc -options_left from warning */
884cb9801acSJed Brown   ierr = PetscOptionsHasName(PETSC_NULL,"-nox",&flg1);CHKERRQ(ierr);
885cb9801acSJed Brown   ierr = PetscOptionsHasName(PETSC_NULL,"-nox_warning",&flg1);CHKERRQ(ierr);
886e5c89e4eSSatish Balay 
887f43cc0c9SSatish Balay   if (!PetscOpenMPWorker) { /* worker processes skip this because they do not usually process options */
88833fc4174SSatish Balay     flg3 = PETSC_FALSE; /* default value is required */
889da8b8a77SBarry Smith     ierr = PetscOptionsGetTruth(PETSC_NULL,"-options_left",&flg3,&flg1);CHKERRQ(ierr);
890e5c89e4eSSatish Balay     ierr = PetscOptionsAllUsed(&nopt);CHKERRQ(ierr);
891e5c89e4eSSatish Balay     if (flg3) {
892e5c89e4eSSatish Balay       if (!flg2) { /* have not yet printed the options */
893e5c89e4eSSatish Balay 	ierr = PetscOptionsPrint(stdout);CHKERRQ(ierr);
894e5c89e4eSSatish Balay       }
895e5c89e4eSSatish Balay       if (!nopt) {
896e5c89e4eSSatish Balay 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");CHKERRQ(ierr);
897e5c89e4eSSatish Balay       } else if (nopt == 1) {
898e5c89e4eSSatish Balay 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");CHKERRQ(ierr);
899e5c89e4eSSatish Balay       } else {
900e5c89e4eSSatish Balay 	ierr = PetscPrintf(PETSC_COMM_WORLD,"There are %d unused database options. They are:\n",nopt);CHKERRQ(ierr);
901e5c89e4eSSatish Balay       }
902e5c89e4eSSatish Balay     }
903e5c89e4eSSatish Balay #if defined(PETSC_USE_DEBUG)
904da8b8a77SBarry Smith     if (nopt && !flg3 && !flg1) {
905e5c89e4eSSatish Balay       ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");CHKERRQ(ierr);
906e5c89e4eSSatish Balay       ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");CHKERRQ(ierr);
907e5c89e4eSSatish Balay       ierr = PetscOptionsLeft();CHKERRQ(ierr);
908e5c89e4eSSatish Balay     } else if (nopt && flg3) {
909e5c89e4eSSatish Balay #else
910e5c89e4eSSatish Balay     if (nopt && flg3) {
911e5c89e4eSSatish Balay #endif
912e5c89e4eSSatish Balay       ierr = PetscOptionsLeft();CHKERRQ(ierr);
913e5c89e4eSSatish Balay     }
914931f367cSBarry Smith   }
915e5c89e4eSSatish Balay 
91690d69ab7SBarry Smith   flg1 = PETSC_FALSE;
91790d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-log_history",&flg1,PETSC_NULL);CHKERRQ(ierr);
918e5c89e4eSSatish Balay   if (flg1) {
919e5c89e4eSSatish Balay     ierr = PetscLogCloseHistoryFile(&petsc_history);CHKERRQ(ierr);
920e5c89e4eSSatish Balay     petsc_history = 0;
921e5c89e4eSSatish Balay   }
922e5c89e4eSSatish Balay 
9236cf91177SBarry Smith   ierr = PetscInfoAllow(PETSC_FALSE,PETSC_NULL);CHKERRQ(ierr);
924e5c89e4eSSatish Balay 
92590d69ab7SBarry Smith   flg1 = PETSC_FALSE;
92690d69ab7SBarry Smith   flg3 = PETSC_FALSE;
92790d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-malloc_dump",&flg1,PETSC_NULL);CHKERRQ(ierr);
92890d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(PETSC_NULL,"-malloc_log",&flg3,PETSC_NULL);CHKERRQ(ierr);
929e5c89e4eSSatish Balay   if (flg1) {
930e5c89e4eSSatish Balay     char fname[PETSC_MAX_PATH_LEN];
931e5c89e4eSSatish Balay     FILE *fd;
932ed9cf6e9SBarry Smith     int  err;
933e5c89e4eSSatish Balay 
934e5c89e4eSSatish Balay     fname[0] = 0;
935e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_dump",fname,250,&flg1);CHKERRQ(ierr);
936e5c89e4eSSatish Balay     if (flg1 && fname[0]) {
937e5c89e4eSSatish Balay       char sname[PETSC_MAX_PATH_LEN];
938e5c89e4eSSatish Balay 
939e5c89e4eSSatish Balay       sprintf(sname,"%s_%d",fname,rank);
940e32f2f54SBarry Smith       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
941e5c89e4eSSatish Balay       ierr = PetscMallocDump(fd);CHKERRQ(ierr);
942ed9cf6e9SBarry Smith       err = fclose(fd);
943e32f2f54SBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
944e5c89e4eSSatish Balay     } else {
945e5c89e4eSSatish Balay       MPI_Comm local_comm;
946e5c89e4eSSatish Balay 
947e5c89e4eSSatish Balay       ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr);
948e5c89e4eSSatish Balay       ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr);
949e5c89e4eSSatish Balay         ierr = PetscMallocDump(stdout);CHKERRQ(ierr);
950e5c89e4eSSatish Balay       ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr);
951e5c89e4eSSatish Balay       ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr);
952e5c89e4eSSatish Balay     }
953e5c89e4eSSatish Balay   }
954e5c89e4eSSatish Balay   if (flg3) {
955e5c89e4eSSatish Balay     char fname[PETSC_MAX_PATH_LEN];
956e5c89e4eSSatish Balay     FILE *fd;
957e5c89e4eSSatish Balay 
958e5c89e4eSSatish Balay     fname[0] = 0;
959e5c89e4eSSatish Balay     ierr = PetscOptionsGetString(PETSC_NULL,"-malloc_log",fname,250,&flg1);CHKERRQ(ierr);
960e5c89e4eSSatish Balay     if (flg1 && fname[0]) {
961e5c89e4eSSatish Balay       char sname[PETSC_MAX_PATH_LEN];
962ed9cf6e9SBarry Smith       int  err;
963e5c89e4eSSatish Balay 
964e5c89e4eSSatish Balay       sprintf(sname,"%s_%d",fname,rank);
965e32f2f54SBarry Smith       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
966e5c89e4eSSatish Balay       ierr = PetscMallocDumpLog(fd);CHKERRQ(ierr);
967ed9cf6e9SBarry Smith       err = fclose(fd);
968e32f2f54SBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
969e5c89e4eSSatish Balay     } else {
970e5c89e4eSSatish Balay       ierr = PetscMallocDumpLog(stdout);CHKERRQ(ierr);
971e5c89e4eSSatish Balay     }
972e5c89e4eSSatish Balay   }
973e5c89e4eSSatish Balay   /* Can be destroyed only after all the options are used */
974e5c89e4eSSatish Balay   ierr = PetscOptionsDestroy();CHKERRQ(ierr);
975e5c89e4eSSatish Balay 
976e5c89e4eSSatish Balay   PetscGlobalArgc = 0;
977e5c89e4eSSatish Balay   PetscGlobalArgs = 0;
978e5c89e4eSSatish Balay 
979e5c89e4eSSatish Balay #if defined(PETSC_USE_COMPLEX)
9802c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
98106a205a8SBarry Smith   ierr = MPI_Op_free(&MPIU_SUM);CHKERRQ(ierr);
9822c876bd9SBarry Smith   ierr = MPI_Type_free(&MPI_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
983a83b8d76SBarry Smith   ierr = MPI_Type_free(&MPI_C_COMPLEX);CHKERRQ(ierr);
9842c876bd9SBarry Smith #endif
985e5c89e4eSSatish Balay #endif
986e5c89e4eSSatish Balay   ierr = MPI_Type_free(&MPIU_2SCALAR);CHKERRQ(ierr);
987e5c89e4eSSatish Balay   ierr = MPI_Type_free(&MPIU_2INT);CHKERRQ(ierr);
988e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscMaxSum_Op);CHKERRQ(ierr);
989e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscADMax_Op);CHKERRQ(ierr);
990e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscADMin_Op);CHKERRQ(ierr);
991e5c89e4eSSatish Balay 
992480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_Counter_keyval);CHKERRQ(ierr);
993480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_InnerComm_keyval);CHKERRQ(ierr);
994480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_OuterComm_keyval);CHKERRQ(ierr);
995480cf27aSJed Brown 
996ae15b995SBarry Smith   ierr = PetscInfo(0,"PETSc successfully ended!\n");CHKERRQ(ierr);
997e5c89e4eSSatish Balay   if (PetscBeganMPI) {
99899608316SBarry Smith #if defined(PETSC_HAVE_MPI_FINALIZED)
99999b1327fSBarry Smith     PetscMPIInt flag;
100099b1327fSBarry Smith     ierr = MPI_Finalized(&flag);CHKERRQ(ierr);
1001e32f2f54SBarry Smith     if (flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()");
100299608316SBarry Smith #endif
1003e5c89e4eSSatish Balay     ierr = MPI_Finalize();CHKERRQ(ierr);
1004e5c89e4eSSatish Balay   }
1005e5c89e4eSSatish Balay 
10069c4c166aSBarry Smith   if (PETSC_ZOPEFD){
100722b84c2fSbcordonn     if (PETSC_ZOPEFD != PETSC_STDOUT) fprintf(PETSC_ZOPEFD, "<<<end>>>");
10089c4c166aSBarry Smith     else fprintf(PETSC_STDOUT, "<<<end>>>");
10099c4c166aSBarry Smith   }
101036186564Sbcordonn 
101135d88935SVictor Minden #if defined(PETSC_HAVE_CUDA)
10122f947c57SVictor Minden   cublasShutdown();
1013440a5bbfSVictor Minden #endif
1014e5c89e4eSSatish Balay /*
1015e5c89e4eSSatish Balay 
1016e5c89e4eSSatish Balay      Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because
1017e5c89e4eSSatish Balay    the communicator has some outstanding requests on it. Specifically if the
1018e5c89e4eSSatish Balay    flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See
1019e5c89e4eSSatish Balay    src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate()
1020e5c89e4eSSatish Balay    is never freed as it should be. Thus one may obtain messages of the form
10210e5e90baSSatish Balay    [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the
1022e5c89e4eSSatish Balay    memory was not freed.
1023e5c89e4eSSatish Balay 
1024e5c89e4eSSatish Balay */
10251d1a0024SBarry Smith   ierr = PetscMallocClear();CHKERRQ(ierr);
1026e5c89e4eSSatish Balay   PetscInitializeCalled = PETSC_FALSE;
1027e5c89e4eSSatish Balay   PetscFinalizeCalled   = PETSC_TRUE;
1028e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
1029e5c89e4eSSatish Balay }
1030e5c89e4eSSatish Balay 
1031