xref: /petsc/src/sys/objects/pinit.c (revision 6d95c8af2390204478363dca9b8e61374b1c9218)
17d0a6c19SBarry Smith 
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay    This file defines the initialization of PETSc, including PetscInitialize()
4e5c89e4eSSatish Balay */
5afcb2eb5SJed Brown #include <petsc-private/petscimpl.h>        /*I  "petscsys.h"   I*/
6022afb99SBarry Smith #include <petscvalgrind.h>
7665c2dedSJed Brown #include <petscviewer.h>
88101f56cSMatthew Knepley 
9663ba86aSMatthew G Knepley #if defined(PETSC_HAVE_CUDA)
102f947c57SVictor Minden #include <cublas.h>
117a025f21SVictor Minden #endif
127a025f21SVictor Minden 
13bd8b14e7SShri Abhyankar #include <petscthreadcomm.h>
14bd8b14e7SShri Abhyankar 
15a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
1609573ac7SBarry Smith extern PetscErrorCode PetscLogBegin_Private(void);
17a9f03627SSatish Balay #endif
18f2d66bcaSShri Abhyankar 
192d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
202d53ad75SBarry Smith PetscFPT PetscFPTData = 0;
212d53ad75SBarry Smith #endif
222d53ad75SBarry Smith 
23a6790183SBarry Smith #if defined(PETSC_HAVE_SAWS)
2416ad0300SBarry Smith #include <petscviewersaws.h>
25a6790183SBarry Smith #endif
26e5c89e4eSSatish Balay /* -----------------------------------------------------------------------------------------*/
27e5c89e4eSSatish Balay 
28e5c89e4eSSatish Balay extern FILE *petsc_history;
29e5c89e4eSSatish Balay 
3009573ac7SBarry Smith extern PetscErrorCode PetscInitialize_DynamicLibraries(void);
3109573ac7SBarry Smith extern PetscErrorCode PetscFinalize_DynamicLibraries(void);
3237e93019SBarry Smith extern PetscErrorCode PetscFunctionListPrintAll(void);
3309573ac7SBarry Smith extern PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int);
3409573ac7SBarry Smith extern PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int);
3509573ac7SBarry Smith extern PetscErrorCode PetscCloseHistoryFile(FILE**);
360069ddf5SShri Abhyankar 
37e5c89e4eSSatish Balay /* user may set this BEFORE calling PetscInitialize() */
38e8373e55SMatthew Knepley MPI_Comm PETSC_COMM_WORLD = MPI_COMM_NULL;
39e5c89e4eSSatish Balay 
40480cf27aSJed Brown PetscMPIInt Petsc_Counter_keyval   = MPI_KEYVAL_INVALID;
41480cf27aSJed Brown PetscMPIInt Petsc_InnerComm_keyval = MPI_KEYVAL_INVALID;
42480cf27aSJed Brown PetscMPIInt Petsc_OuterComm_keyval = MPI_KEYVAL_INVALID;
43480cf27aSJed Brown 
44e5c89e4eSSatish Balay /*
45e5c89e4eSSatish Balay      Declare and set all the string names of the PETSc enums
46e5c89e4eSSatish Balay */
476a6fc655SJed Brown const char *const PetscBools[]     = {"FALSE","TRUE","PetscBool","PETSC_",0};
486a6fc655SJed Brown const char *const PetscCopyModes[] = {"COPY_VALUES","OWN_POINTER","USE_POINTER","PetscCopyMode","PETSC_",0};
496a6fc655SJed Brown const char *const PetscDataTypes[] = {"INT","DOUBLE","COMPLEX","LONG","SHORT","FLOAT",
502d53ad75SBarry Smith                                       "CHAR","LOGICAL","ENUM","BOOL","LONGDOUBLE","OBJECT","FUNCTION","PetscDataType","PETSC_",0};
51e5c89e4eSSatish Balay 
52ace3abfcSBarry Smith PetscBool PetscPreLoadingUsed = PETSC_FALSE;
53ace3abfcSBarry Smith PetscBool PetscPreLoadingOn   = PETSC_FALSE;
540f8e0872SSatish Balay 
55a2f94806SJed Brown PetscInt PetscHotRegionDepth;
56a2f94806SJed Brown 
57e5c89e4eSSatish Balay /*
58e5c89e4eSSatish Balay        Checks the options database for initializations related to the
59e5c89e4eSSatish Balay     PETSc components
60e5c89e4eSSatish Balay */
61e5c89e4eSSatish Balay #undef __FUNCT__
62e5c89e4eSSatish Balay #define __FUNCT__ "PetscOptionsCheckInitial_Components"
637087cfbeSBarry Smith PetscErrorCode  PetscOptionsCheckInitial_Components(void)
64e5c89e4eSSatish Balay {
65ace3abfcSBarry Smith   PetscBool      flg1;
66e5c89e4eSSatish Balay   PetscErrorCode ierr;
67e5c89e4eSSatish Balay 
68e5c89e4eSSatish Balay   PetscFunctionBegin;
690298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-help",&flg1);CHKERRQ(ierr);
70e5c89e4eSSatish Balay   if (flg1) {
71e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
72e8e7597cSSatish Balay     MPI_Comm comm = PETSC_COMM_WORLD;
73e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");CHKERRQ(ierr);
74e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -log_summary_exclude: <vec,mat,pc.ksp,snes>\n");CHKERRQ(ierr);
756cf91177SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -info_exclude: <null,vec,mat,pc,ksp,snes,ts>\n");CHKERRQ(ierr);
76e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");CHKERRQ(ierr);
77e5c89e4eSSatish Balay #endif
78e5c89e4eSSatish Balay   }
79e5c89e4eSSatish Balay   PetscFunctionReturn(0);
80e5c89e4eSSatish Balay }
81e5c89e4eSSatish Balay 
82e5c89e4eSSatish Balay #undef __FUNCT__
83945d1669SBarry Smith #define __FUNCT__ "PetscInitializeNoPointers"
840f11a792SBarry Smith /*
85945d1669SBarry Smith       PetscInitializeNoPointers - Calls PetscInitialize() from C/C++ without the pointers to argc and args
8672a42c3cSBarry Smith 
8772a42c3cSBarry Smith    Collective
8872a42c3cSBarry Smith 
8972a42c3cSBarry Smith    Level: advanced
9072a42c3cSBarry Smith 
91945d1669SBarry Smith     Notes: this is called only by the PETSc MATLAB and Julia interface. Even though it might start MPI it sets the flag to
920f11a792SBarry Smith      indicate that it did NOT start MPI so that the PetscFinalize() does not end MPI, thus allowing PetscInitialize() to
93945d1669SBarry Smith      be called multiple times from MATLAB and Julia without the problem of trying to initialize MPI more than once.
940f11a792SBarry Smith 
951ea5a559SBarry Smith      Turns off PETSc signal handling because that can interact with MATLAB's signal handling causing random crashes.
961ea5a559SBarry Smith 
9772a42c3cSBarry Smith .seealso: PetscInitialize(), PetscInitializeFortran(), PetscInitializeNoArguments()
980f11a792SBarry Smith */
99945d1669SBarry Smith PetscErrorCode  PetscInitializeNoPointers(int argc,char **args,const char *filename,const char *help)
10072a42c3cSBarry Smith {
10172a42c3cSBarry Smith   PetscErrorCode ierr;
10272a42c3cSBarry Smith   int            myargc   = argc;
10372a42c3cSBarry Smith   char           **myargs = args;
10472a42c3cSBarry Smith 
10572a42c3cSBarry Smith   PetscFunctionBegin;
1063bf036e2SBarry Smith   ierr = PetscInitialize(&myargc,&myargs,filename,help);CHKERRQ(ierr);
1071ea5a559SBarry Smith   ierr = PetscPopSignalHandler();CHKERRQ(ierr);
108df413903SBarry Smith   PetscBeganMPI = PETSC_FALSE;
10972a42c3cSBarry Smith   PetscFunctionReturn(ierr);
11072a42c3cSBarry Smith }
11172a42c3cSBarry Smith 
11272a42c3cSBarry Smith #undef __FUNCT__
113945d1669SBarry Smith #define __FUNCT__ "PetscGetPETSC_COMM_SELF"
114f0865b08SBarry Smith /*
115945d1669SBarry Smith       Used by MATLAB and Julia interface to get communicator
116f0865b08SBarry Smith */
117945d1669SBarry Smith PetscErrorCode  PetscGetPETSC_COMM_SELF(MPI_Comm *comm)
118f0865b08SBarry Smith {
119f0865b08SBarry Smith   PetscFunctionBegin;
120f0865b08SBarry Smith   *comm = PETSC_COMM_SELF;
121f0865b08SBarry Smith   PetscFunctionReturn(0);
122f0865b08SBarry Smith }
123f0865b08SBarry Smith 
124f0865b08SBarry Smith #undef __FUNCT__
125e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitializeNoArguments"
126e5c89e4eSSatish Balay /*@C
127e5c89e4eSSatish Balay       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
128e5c89e4eSSatish Balay         the command line arguments.
129e5c89e4eSSatish Balay 
130e5c89e4eSSatish Balay    Collective
131e5c89e4eSSatish Balay 
132e5c89e4eSSatish Balay    Level: advanced
133e5c89e4eSSatish Balay 
134e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeFortran()
135e5c89e4eSSatish Balay @*/
1367087cfbeSBarry Smith PetscErrorCode  PetscInitializeNoArguments(void)
137e5c89e4eSSatish Balay {
138e5c89e4eSSatish Balay   PetscErrorCode ierr;
139e5c89e4eSSatish Balay   int            argc   = 0;
140e5c89e4eSSatish Balay   char           **args = 0;
141e5c89e4eSSatish Balay 
142e5c89e4eSSatish Balay   PetscFunctionBegin;
1430298fd71SBarry Smith   ierr = PetscInitialize(&argc,&args,NULL,NULL);
144e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
145e5c89e4eSSatish Balay }
146e5c89e4eSSatish Balay 
147e5c89e4eSSatish Balay #undef __FUNCT__
148e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialized"
149e5c89e4eSSatish Balay /*@
150e5c89e4eSSatish Balay       PetscInitialized - Determine whether PETSc is initialized.
151e5c89e4eSSatish Balay 
15293b6d2d1SJed Brown    Level: beginner
153e5c89e4eSSatish Balay 
154e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
155e5c89e4eSSatish Balay @*/
1567087cfbeSBarry Smith PetscErrorCode PetscInitialized(PetscBool  *isInitialized)
157e5c89e4eSSatish Balay {
158e5c89e4eSSatish Balay   *isInitialized = PetscInitializeCalled;
15993b6d2d1SJed Brown   return 0;
160e5c89e4eSSatish Balay }
161e5c89e4eSSatish Balay 
162e5c89e4eSSatish Balay #undef __FUNCT__
163e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalized"
164e5c89e4eSSatish Balay /*@
165e5c89e4eSSatish Balay       PetscFinalized - Determine whether PetscFinalize() has been called yet
166e5c89e4eSSatish Balay 
167e5c89e4eSSatish Balay    Level: developer
168e5c89e4eSSatish Balay 
169e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
170e5c89e4eSSatish Balay @*/
1717087cfbeSBarry Smith PetscErrorCode  PetscFinalized(PetscBool  *isFinalized)
172e5c89e4eSSatish Balay {
173e5c89e4eSSatish Balay   *isFinalized = PetscFinalizeCalled;
17493b6d2d1SJed Brown   return 0;
175e5c89e4eSSatish Balay }
176e5c89e4eSSatish Balay 
17709573ac7SBarry Smith extern PetscErrorCode PetscOptionsCheckInitial_Private(void);
178e5c89e4eSSatish Balay 
179e5c89e4eSSatish Balay /*
180e5c89e4eSSatish Balay        This function is the MPI reduction operation used to compute the sum of the
181e5c89e4eSSatish Balay    first half of the datatype and the max of the second half.
182e5c89e4eSSatish Balay */
183e5c89e4eSSatish Balay MPI_Op PetscMaxSum_Op = 0;
184e5c89e4eSSatish Balay 
185e5c89e4eSSatish Balay #undef __FUNCT__
186e5c89e4eSSatish Balay #define __FUNCT__ "PetscMaxSum_Local"
1878cc058d9SJed Brown PETSC_EXTERN void MPIAPI PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
188e5c89e4eSSatish Balay {
189e5c89e4eSSatish Balay   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;
190e5c89e4eSSatish Balay 
191e5c89e4eSSatish Balay   PetscFunctionBegin;
192e5c89e4eSSatish Balay   if (*datatype != MPIU_2INT) {
193e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
194e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
195e5c89e4eSSatish Balay   }
196e5c89e4eSSatish Balay 
197e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
198e5c89e4eSSatish Balay     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
199e5c89e4eSSatish Balay     xout[2*i+1] += xin[2*i+1];
200e5c89e4eSSatish Balay   }
201812af9f3SBarry Smith   PetscFunctionReturnVoid();
202e5c89e4eSSatish Balay }
203e5c89e4eSSatish Balay 
204e5c89e4eSSatish Balay /*
205e5c89e4eSSatish Balay     Returns the max of the first entry owned by this processor and the
206e5c89e4eSSatish Balay sum of the second entry.
207b693b147SBarry Smith 
208b693b147SBarry Smith     The reason nprocs[2*i] contains lengths nprocs[2*i+1] contains flag of 1 if length is nonzero
209b693b147SBarry Smith is so that the PetscMaxSum_Op() can set TWO values, if we passed in only nprocs[i] with lengths
210b693b147SBarry Smith there would be no place to store the both needed results.
211e5c89e4eSSatish Balay */
212e5c89e4eSSatish Balay #undef __FUNCT__
213e5c89e4eSSatish Balay #define __FUNCT__ "PetscMaxSum"
21476ec1555SBarry Smith PetscErrorCode  PetscMaxSum(MPI_Comm comm,const PetscInt sizes[],PetscInt *max,PetscInt *sum)
215e5c89e4eSSatish Balay {
216e5c89e4eSSatish Balay   PetscMPIInt    size,rank;
2176ac3741eSJed Brown   struct {PetscInt max,sum;} *work;
218e5c89e4eSSatish Balay   PetscErrorCode ierr;
219e5c89e4eSSatish Balay 
220e5c89e4eSSatish Balay   PetscFunctionBegin;
221e5c89e4eSSatish Balay   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
222e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
223785e854fSJed Brown   ierr = PetscMalloc1(size,&work);CHKERRQ(ierr);
22476ec1555SBarry Smith   ierr = MPI_Allreduce((void*)sizes,work,size,MPIU_2INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
2256ac3741eSJed Brown   *max = work[rank].max;
2266ac3741eSJed Brown   *sum = work[rank].sum;
227e5c89e4eSSatish Balay   ierr = PetscFree(work);CHKERRQ(ierr);
228e5c89e4eSSatish Balay   PetscFunctionReturn(0);
229e5c89e4eSSatish Balay }
230e5c89e4eSSatish Balay 
231e5c89e4eSSatish Balay /* ----------------------------------------------------------------------------*/
232e5c89e4eSSatish Balay 
2337c2de775SJed Brown #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128)
23406a205a8SBarry Smith MPI_Op MPIU_SUM = 0;
235e5c89e4eSSatish Balay 
236e5c89e4eSSatish Balay #undef __FUNCT__
237e5c89e4eSSatish Balay #define __FUNCT__ "PetscSum_Local"
2388cc058d9SJed Brown PETSC_EXTERN void PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
239e5c89e4eSSatish Balay {
240e5c89e4eSSatish Balay   PetscInt i,count = *cnt;
241e5c89e4eSSatish Balay 
242e5c89e4eSSatish Balay   PetscFunctionBegin;
2437c2de775SJed Brown   if (*datatype == MPIU_REAL) {
244e2e03761SBarry Smith     PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
245a297a907SKarl Rupp     for (i=0; i<count; i++) xout[i] += xin[i];
2467c2de775SJed Brown   }
2477c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
2487c2de775SJed Brown   else if (*datatype == MPIU_COMPLEX) {
2497c2de775SJed Brown     PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
250a297a907SKarl Rupp     for (i=0; i<count; i++) xout[i] += xin[i];
2517c2de775SJed Brown   }
2527c2de775SJed Brown #endif
2537c2de775SJed Brown   else {
2547c2de775SJed Brown     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types");
255e2e03761SBarry Smith     MPI_Abort(MPI_COMM_WORLD,1);
256e2e03761SBarry Smith   }
257812af9f3SBarry Smith   PetscFunctionReturnVoid();
258e5c89e4eSSatish Balay }
259e5c89e4eSSatish Balay #endif
260e5c89e4eSSatish Balay 
261ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
262d9822059SBarry Smith MPI_Op MPIU_MAX = 0;
263d9822059SBarry Smith MPI_Op MPIU_MIN = 0;
264d9822059SBarry Smith 
265d9822059SBarry Smith #undef __FUNCT__
266d9822059SBarry Smith #define __FUNCT__ "PetscMax_Local"
2678cc058d9SJed Brown PETSC_EXTERN void PetscMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
268d9822059SBarry Smith {
269d9822059SBarry Smith   PetscInt i,count = *cnt;
270d9822059SBarry Smith 
271d9822059SBarry Smith   PetscFunctionBegin;
2727c2de775SJed Brown   if (*datatype == MPIU_REAL) {
2738c764dc5SJose Roman     PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
274a297a907SKarl Rupp     for (i=0; i<count; i++) xout[i] = PetscMax(xout[i],xin[i]);
2757c2de775SJed Brown   }
2767c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
2777c2de775SJed Brown   else if (*datatype == MPIU_COMPLEX) {
2787c2de775SJed Brown     PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
2797c2de775SJed Brown     for (i=0; i<count; i++) {
2807c2de775SJed Brown       xout[i] = PetscRealPartComplex(xout[i])<PetscRealPartComplex(xin[i]) ? xin[i] : xout[i];
2817c2de775SJed Brown     }
2827c2de775SJed Brown   }
2837c2de775SJed Brown #endif
2847c2de775SJed Brown   else {
2857c2de775SJed Brown     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types");
2868c764dc5SJose Roman     MPI_Abort(MPI_COMM_WORLD,1);
2878c764dc5SJose Roman   }
288d9822059SBarry Smith   PetscFunctionReturnVoid();
289d9822059SBarry Smith }
290d9822059SBarry Smith 
291d9822059SBarry Smith #undef __FUNCT__
292d9822059SBarry Smith #define __FUNCT__ "PetscMin_Local"
2938cc058d9SJed Brown PETSC_EXTERN void PetscMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
294d9822059SBarry Smith {
295d9822059SBarry Smith   PetscInt    i,count = *cnt;
296d9822059SBarry Smith 
297d9822059SBarry Smith   PetscFunctionBegin;
2987c2de775SJed Brown   if (*datatype == MPIU_REAL) {
2998c764dc5SJose Roman     PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
300a297a907SKarl Rupp     for (i=0; i<count; i++) xout[i] = PetscMin(xout[i],xin[i]);
3017c2de775SJed Brown   }
3027c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
3037c2de775SJed Brown   else if (*datatype == MPIU_COMPLEX) {
3047c2de775SJed Brown     PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
3057c2de775SJed Brown     for (i=0; i<count; i++) {
3067c2de775SJed Brown       xout[i] = PetscRealPartComplex(xout[i])>PetscRealPartComplex(xin[i]) ? xin[i] : xout[i];
3077c2de775SJed Brown     }
3087c2de775SJed Brown   }
3097c2de775SJed Brown #endif
3107c2de775SJed Brown   else {
3118c764dc5SJose Roman     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_SCALAR data (i.e. double or complex) types");
3128c764dc5SJose Roman     MPI_Abort(MPI_COMM_WORLD,1);
3138c764dc5SJose Roman   }
314d9822059SBarry Smith   PetscFunctionReturnVoid();
315d9822059SBarry Smith }
316d9822059SBarry Smith #endif
317d9822059SBarry Smith 
318480cf27aSJed Brown #undef __FUNCT__
319480cf27aSJed Brown #define __FUNCT__ "Petsc_DelCounter"
320480cf27aSJed Brown /*
321480cf27aSJed Brown    Private routine to delete internal tag/name counter storage when a communicator is freed.
322480cf27aSJed Brown 
323ff0e51ddSBarry Smith    This is called by MPI, not by users. This is called by MPI_Comm_free() when the communicator that has this  data as an attribute is freed.
324480cf27aSJed Brown 
325480cf27aSJed Brown    Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
326480cf27aSJed Brown 
327480cf27aSJed Brown */
3288cc058d9SJed Brown PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelCounter(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
329480cf27aSJed Brown {
330480cf27aSJed Brown   PetscErrorCode ierr;
331480cf27aSJed Brown 
332480cf27aSJed Brown   PetscFunctionBegin;
333480cf27aSJed Brown   ierr = PetscInfo1(0,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
334480cf27aSJed Brown   ierr = PetscFree(count_val);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
335480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
336480cf27aSJed Brown }
337480cf27aSJed Brown 
338480cf27aSJed Brown #undef __FUNCT__
339da3039f7SJed Brown #define __FUNCT__ "Petsc_DelComm_Outer"
340480cf27aSJed Brown /*
341da3039f7SJed Brown   This is invoked on the outer comm as a result of either PetscCommDestroy() (via MPI_Attr_delete) or when the user
342da3039f7SJed Brown   calls MPI_Comm_free().
343da3039f7SJed Brown 
344da3039f7SJed Brown   This is the only entry point for breaking the links between inner and outer comms.
345480cf27aSJed Brown 
346ff0e51ddSBarry Smith   This is called by MPI, not by users. This is called when MPI_Comm_free() is called on the communicator.
347480cf27aSJed Brown 
348480cf27aSJed Brown   Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
349480cf27aSJed Brown 
350480cf27aSJed Brown */
351da3039f7SJed Brown PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelComm_Outer(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
352480cf27aSJed Brown {
353480cf27aSJed Brown   PetscErrorCode ierr;
354b89831e5SBarry Smith   PetscMPIInt    flg;
355265f3f35SJed Brown   union {MPI_Comm comm; void *ptr;} icomm,ocomm;
356480cf27aSJed Brown 
357480cf27aSJed Brown   PetscFunctionBegin;
358da3039f7SJed Brown   if (keyval != Petsc_InnerComm_keyval) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Unexpected keyval");
359ec4fadc2SJed Brown   icomm.ptr = attr_val;
360da3039f7SJed Brown 
361265f3f35SJed Brown   ierr = MPI_Attr_get(icomm.comm,Petsc_OuterComm_keyval,&ocomm,&flg);CHKERRQ(ierr);
362b3ef52cdSBarry Smith   if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected reference to outer comm");
363da3039f7SJed Brown   if (ocomm.comm != comm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm has reference to non-matching outer comm");
364da3039f7SJed Brown   ierr = MPI_Attr_delete(icomm.comm,Petsc_OuterComm_keyval);CHKERRQ(ierr); /* Calls Petsc_DelComm_Inner */
365da3039f7SJed Brown   ierr = PetscInfo1(0,"User MPI_Comm %ld is being freed after removing reference from inner PETSc comm to this outer comm\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
366da3039f7SJed Brown   PetscFunctionReturn(MPI_SUCCESS);
367b89831e5SBarry Smith }
368da3039f7SJed Brown 
369da3039f7SJed Brown #undef __FUNCT__
370da3039f7SJed Brown #define __FUNCT__ "Petsc_DelComm_Inner"
371da3039f7SJed Brown /*
372da3039f7SJed Brown  * This is invoked on the inner comm when Petsc_DelComm_Outer calls MPI_Attr_delete.  It should not be reached any other way.
373da3039f7SJed Brown  */
374da3039f7SJed Brown PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelComm_Inner(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
375da3039f7SJed Brown {
376da3039f7SJed Brown   PetscErrorCode ierr;
377da3039f7SJed Brown 
378da3039f7SJed Brown   PetscFunctionBegin;
379da3039f7SJed Brown   ierr = PetscInfo1(0,"Removing reference to PETSc communicator embedded in a user MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
380480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
381480cf27aSJed Brown }
382480cf27aSJed Brown 
383951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
384e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
3858cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype,MPI_Aint*,void*);
3868cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_read_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
3878cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_write_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
388e39fd77fSBarry Smith #endif
389951e3c8eSBarry Smith #endif
390e39fd77fSBarry Smith 
3916ae9a8a6SBarry Smith int  PetscGlobalArgc   = 0;
3926ae9a8a6SBarry Smith char **PetscGlobalArgs = 0;
393dff31646SBarry Smith PetscSegBuffer PetscCitationsList;
394e5c89e4eSSatish Balay 
395e5c89e4eSSatish Balay #undef __FUNCT__
396051e4cf2SJed Brown #define __FUNCT__ "PetscCitationsInitialize"
397051e4cf2SJed Brown PetscErrorCode PetscCitationsInitialize()
398051e4cf2SJed Brown {
399051e4cf2SJed Brown   PetscErrorCode ierr;
400051e4cf2SJed Brown 
401051e4cf2SJed Brown   PetscFunctionBegin;
402051e4cf2SJed Brown   ierr = PetscSegBufferCreate(1,10000,&PetscCitationsList);CHKERRQ(ierr);
403fa9c09a1SSatish Balay   ierr = PetscCitationsRegister("@TechReport{petsc-user-ref,\n  Author = {Satish Balay and Shrirang Abhyankar and Mark F. Adams and Jed Brown and Peter Brune\n            and Kris Buschelman and Victor Eijkhout and William D. Gropp\n            and Dinesh Kaushik and Matthew G. Knepley\n            and Lois Curfman McInnes and Karl Rupp and Barry F. Smith\n            and Hong Zhang},\n  Title = {{PETS}c Users Manual},\n  Number = {ANL-95/11 - Revision 3.5},\n  Institution = {Argonne National Laboratory},\n  Year = {2014}\n}\n",NULL);CHKERRQ(ierr);
404051e4cf2SJed Brown   ierr = PetscCitationsRegister("@InProceedings{petsc-efficient,\n  Author = {Satish Balay and William D. Gropp and Lois Curfman McInnes and Barry F. Smith},\n  Title = {Efficient Management of Parallelism in Object Oriented Numerical Software Libraries},\n  Booktitle = {Modern Software Tools in Scientific Computing},\n  Editor = {E. Arge and A. M. Bruaset and H. P. Langtangen},\n  Pages = {163--202},\n  Publisher = {Birkh{\\\"{a}}user Press},\n  Year = {1997}\n}\n",NULL);CHKERRQ(ierr);
405051e4cf2SJed Brown   PetscFunctionReturn(0);
406051e4cf2SJed Brown }
407e5c89e4eSSatish Balay 
408e5c89e4eSSatish Balay #undef __FUNCT__
409e5c89e4eSSatish Balay #define __FUNCT__ "PetscGetArgs"
410e5c89e4eSSatish Balay /*@C
411e5c89e4eSSatish Balay    PetscGetArgs - Allows you to access the raw command line arguments anywhere
412e5c89e4eSSatish Balay      after PetscInitialize() is called but before PetscFinalize().
413e5c89e4eSSatish Balay 
414e5c89e4eSSatish Balay    Not Collective
415e5c89e4eSSatish Balay 
416e5c89e4eSSatish Balay    Output Parameters:
417e5c89e4eSSatish Balay +  argc - count of number of command line arguments
418e5c89e4eSSatish Balay -  args - the command line arguments
419e5c89e4eSSatish Balay 
420e5c89e4eSSatish Balay    Level: intermediate
421e5c89e4eSSatish Balay 
422e5c89e4eSSatish Balay    Notes:
423e5c89e4eSSatish Balay       This is usually used to pass the command line arguments into other libraries
424e5c89e4eSSatish Balay    that are called internally deep in PETSc or the application.
425e5c89e4eSSatish Balay 
426f177e3b1SBarry Smith       The first argument contains the program name as is normal for C arguments.
427f177e3b1SBarry Smith 
428e5c89e4eSSatish Balay    Concepts: command line arguments
429e5c89e4eSSatish Balay 
430793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()
431e5c89e4eSSatish Balay 
432e5c89e4eSSatish Balay @*/
4337087cfbeSBarry Smith PetscErrorCode  PetscGetArgs(int *argc,char ***args)
434e5c89e4eSSatish Balay {
435e5c89e4eSSatish Balay   PetscFunctionBegin;
43617186662SBarry Smith   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
437e5c89e4eSSatish Balay   *argc = PetscGlobalArgc;
438e5c89e4eSSatish Balay   *args = PetscGlobalArgs;
439e5c89e4eSSatish Balay   PetscFunctionReturn(0);
440e5c89e4eSSatish Balay }
441e5c89e4eSSatish Balay 
442e5c89e4eSSatish Balay #undef __FUNCT__
443793721a6SBarry Smith #define __FUNCT__ "PetscGetArguments"
444793721a6SBarry Smith /*@C
445793721a6SBarry Smith    PetscGetArguments - Allows you to access the  command line arguments anywhere
446793721a6SBarry Smith      after PetscInitialize() is called but before PetscFinalize().
447793721a6SBarry Smith 
448793721a6SBarry Smith    Not Collective
449793721a6SBarry Smith 
450793721a6SBarry Smith    Output Parameters:
451793721a6SBarry Smith .  args - the command line arguments
452793721a6SBarry Smith 
453793721a6SBarry Smith    Level: intermediate
454793721a6SBarry Smith 
455793721a6SBarry Smith    Notes:
456793721a6SBarry Smith       This does NOT start with the program name and IS null terminated (final arg is void)
457793721a6SBarry Smith 
458793721a6SBarry Smith    Concepts: command line arguments
459793721a6SBarry Smith 
460793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()
461793721a6SBarry Smith 
462793721a6SBarry Smith @*/
4637087cfbeSBarry Smith PetscErrorCode  PetscGetArguments(char ***args)
464793721a6SBarry Smith {
465793721a6SBarry Smith   PetscInt       i,argc = PetscGlobalArgc;
466793721a6SBarry Smith   PetscErrorCode ierr;
467793721a6SBarry Smith 
468793721a6SBarry Smith   PetscFunctionBegin;
46917186662SBarry Smith   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
470717030eeSLisandro Dalcin   if (!argc) {*args = 0; PetscFunctionReturn(0);}
471785e854fSJed Brown   ierr = PetscMalloc1(argc,args);CHKERRQ(ierr);
472793721a6SBarry Smith   for (i=0; i<argc-1; i++) {
473793721a6SBarry Smith     ierr = PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);CHKERRQ(ierr);
474793721a6SBarry Smith   }
475793721a6SBarry Smith   (*args)[argc-1] = 0;
476793721a6SBarry Smith   PetscFunctionReturn(0);
477793721a6SBarry Smith }
478793721a6SBarry Smith 
479793721a6SBarry Smith #undef __FUNCT__
480793721a6SBarry Smith #define __FUNCT__ "PetscFreeArguments"
481793721a6SBarry Smith /*@C
482793721a6SBarry Smith    PetscFreeArguments - Frees the memory obtained with PetscGetArguments()
483793721a6SBarry Smith 
484793721a6SBarry Smith    Not Collective
485793721a6SBarry Smith 
486793721a6SBarry Smith    Output Parameters:
487793721a6SBarry Smith .  args - the command line arguments
488793721a6SBarry Smith 
489793721a6SBarry Smith    Level: intermediate
490793721a6SBarry Smith 
491793721a6SBarry Smith    Concepts: command line arguments
492793721a6SBarry Smith 
493793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()
494793721a6SBarry Smith 
495793721a6SBarry Smith @*/
4967087cfbeSBarry Smith PetscErrorCode  PetscFreeArguments(char **args)
497793721a6SBarry Smith {
498793721a6SBarry Smith   PetscInt       i = 0;
499793721a6SBarry Smith   PetscErrorCode ierr;
500793721a6SBarry Smith 
501793721a6SBarry Smith   PetscFunctionBegin;
502a297a907SKarl Rupp   if (!args) PetscFunctionReturn(0);
503793721a6SBarry Smith   while (args[i]) {
504793721a6SBarry Smith     ierr = PetscFree(args[i]);CHKERRQ(ierr);
505793721a6SBarry Smith     i++;
506793721a6SBarry Smith   }
507793721a6SBarry Smith   ierr = PetscFree(args);CHKERRQ(ierr);
508793721a6SBarry Smith   PetscFunctionReturn(0);
509793721a6SBarry Smith }
510793721a6SBarry Smith 
51111525c0dSBarry Smith #if defined(PETSC_HAVE_SAWS)
51230befbd2SBarry Smith #include <petscconfiginfo.h>
51330befbd2SBarry Smith 
51411525c0dSBarry Smith #undef __FUNCT__
51511525c0dSBarry Smith #define __FUNCT__ "PetscInitializeSAWs"
51611525c0dSBarry Smith PetscErrorCode  PetscInitializeSAWs(const char help[])
51711525c0dSBarry Smith {
51811525c0dSBarry Smith   if (!PetscGlobalRank) {
51930befbd2SBarry Smith     char           cert[PETSC_MAX_PATH_LEN],root[PETSC_MAX_PATH_LEN],*intro,programname[64],*appline,*options,version[64];
52011525c0dSBarry Smith     int            port;
52111525c0dSBarry Smith     PetscBool      flg,rootlocal = PETSC_FALSE,flg2;
52211525c0dSBarry Smith     size_t         applinelen,introlen;
52311525c0dSBarry Smith     PetscErrorCode ierr;
524*6d95c8afSBarry Smith     /* char           sawsurl[256]; */
52511525c0dSBarry Smith 
52611525c0dSBarry Smith     ierr = PetscOptionsHasName(NULL,"-saws_log",&flg);CHKERRQ(ierr);
52711525c0dSBarry Smith     if (flg) {
52811525c0dSBarry Smith       char  sawslog[PETSC_MAX_PATH_LEN];
52911525c0dSBarry Smith 
53011525c0dSBarry Smith       ierr = PetscOptionsGetString(NULL,"-saws_log",sawslog,PETSC_MAX_PATH_LEN,NULL);CHKERRQ(ierr);
53111525c0dSBarry Smith       if (sawslog[0]) {
53211525c0dSBarry Smith         PetscStackCallSAWs(SAWs_Set_Use_Logfile,(sawslog));
53311525c0dSBarry Smith       } else {
53411525c0dSBarry Smith         PetscStackCallSAWs(SAWs_Set_Use_Logfile,(NULL));
53511525c0dSBarry Smith       }
53611525c0dSBarry Smith     }
53711525c0dSBarry Smith     ierr = PetscOptionsGetString(NULL,"-saws_https",cert,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
53811525c0dSBarry Smith     if (flg) {
53911525c0dSBarry Smith       PetscStackCallSAWs(SAWs_Set_Use_HTTPS,(cert));
54011525c0dSBarry Smith     }
54111525c0dSBarry Smith     ierr = PetscOptionsGetInt(NULL,"-saws_port",&port,&flg);CHKERRQ(ierr);
54211525c0dSBarry Smith     if (flg) {
54311525c0dSBarry Smith       PetscStackCallSAWs(SAWs_Set_Port,(port));
54411525c0dSBarry Smith     }
54511525c0dSBarry Smith     ierr = PetscOptionsGetString(NULL,"-saws_root",root,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
54611525c0dSBarry Smith     if (flg) {
54711525c0dSBarry Smith       PetscStackCallSAWs(SAWs_Set_Document_Root,(root));CHKERRQ(ierr);
54811525c0dSBarry Smith       ierr = PetscStrcmp(root,".",&rootlocal);CHKERRQ(ierr);
5499c1e0ce8SBarry Smith     } else {
5509c1e0ce8SBarry Smith       ierr = PetscOptionsHasName(NULL,"-saws_options",&flg);CHKERRQ(ierr);
5519c1e0ce8SBarry Smith       if (flg) {
5523c01dfcfSBarry Smith         ierr = PetscStrreplace(PETSC_COMM_WORLD,"${PETSC_DIR}/share/petsc/saws",root,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
5539c1e0ce8SBarry Smith         PetscStackCallSAWs(SAWs_Set_Document_Root,(root));CHKERRQ(ierr);
5549c1e0ce8SBarry Smith       }
55511525c0dSBarry Smith     }
55611525c0dSBarry Smith     ierr = PetscOptionsHasName(NULL,"-saws_local",&flg2);CHKERRQ(ierr);
55711525c0dSBarry Smith     if (flg2) {
55811525c0dSBarry Smith       char jsdir[PETSC_MAX_PATH_LEN];
55911525c0dSBarry Smith       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"-saws_local option requires -saws_root option");
56011525c0dSBarry Smith       ierr = PetscSNPrintf(jsdir,PETSC_MAX_PATH_LEN,"%s/js",root);CHKERRQ(ierr);
56111525c0dSBarry Smith       ierr = PetscTestDirectory(jsdir,'r',&flg);CHKERRQ(ierr);
56211525c0dSBarry Smith       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"-saws_local option requires js directory in root directory");
56343da4ab2SBarry Smith       PetscStackCallSAWs(SAWs_Push_Local_Header,());CHKERRQ(ierr);
56411525c0dSBarry Smith     }
56511525c0dSBarry Smith     ierr = PetscGetProgramName(programname,64);CHKERRQ(ierr);
56611525c0dSBarry Smith     ierr = PetscStrlen(help,&applinelen);CHKERRQ(ierr);
56711525c0dSBarry Smith     introlen   = 4096 + applinelen;
56830a8c9c0SSurtai Han     applinelen += 1024;
56911525c0dSBarry Smith     ierr = PetscMalloc(applinelen,&appline);CHKERRQ(ierr);
57011525c0dSBarry Smith     ierr = PetscMalloc(introlen,&intro);CHKERRQ(ierr);
57111525c0dSBarry Smith 
57211525c0dSBarry Smith     if (rootlocal) {
57311525c0dSBarry Smith       ierr = PetscSNPrintf(appline,applinelen,"%s.c.html",programname);CHKERRQ(ierr);
57411525c0dSBarry Smith       ierr = PetscTestFile(appline,'r',&rootlocal);CHKERRQ(ierr);
57511525c0dSBarry Smith     }
57630befbd2SBarry Smith     ierr = PetscOptionsGetAll(&options);CHKERRQ(ierr);
57711525c0dSBarry Smith     if (rootlocal && help) {
57830befbd2SBarry Smith       ierr = PetscSNPrintf(appline,applinelen,"<center> Running <a href=\"%s.c.html\">%s</a> %s</center><br><center><pre>%s</pre></center><br>\n",programname,programname,options,help);
57911525c0dSBarry Smith     } else if (help) {
58030a8c9c0SSurtai Han       ierr = PetscSNPrintf(appline,applinelen,"<center>Running %s %s</center><br><center><pre>%s</pre></center><br>",programname,options,help);
58111525c0dSBarry Smith     } else {
58230befbd2SBarry Smith       ierr = PetscSNPrintf(appline,applinelen,"<center> Running %s %s</center><br>\n",programname,options);
58311525c0dSBarry Smith     }
584b0bb5815SBarry Smith     ierr = PetscFree(options);CHKERRQ(ierr);
58530befbd2SBarry Smith     ierr = PetscGetVersion(version,sizeof(version));CHKERRQ(ierr);
58611525c0dSBarry Smith     ierr = PetscSNPrintf(intro,introlen,"<body>\n"
58711525c0dSBarry Smith                                     "<center><h2> <a href=\"http://www.mcs.anl.gov/petsc\">PETSc</a> Application Web server powered by <a href=\"https://bitbucket.org/saws/saws\">SAWs</a> </h2></center>\n"
588df62c222SBarry Smith                                     "<center>This is the default PETSc application dashboard, from it you can access any published PETSc objects or logging data</center><br><center>%s configured with %s</center><br>\n"
589df62c222SBarry Smith                                     "%s",version,petscconfigureoptions,appline);
59043da4ab2SBarry Smith     PetscStackCallSAWs(SAWs_Push_Body,("index.html",0,intro));
59111525c0dSBarry Smith     ierr = PetscFree(intro);CHKERRQ(ierr);
59211525c0dSBarry Smith     ierr = PetscFree(appline);CHKERRQ(ierr);
59311525c0dSBarry Smith     PetscStackCallSAWs(SAWs_Initialize,());
594*6d95c8afSBarry Smith     /* PetscStackCallSAWs(SAWs_Get_FullURL,(sizeof(sawsurl),sawsurl));
595*6d95c8afSBarry Smith      ierr = PetscPrintf(PETSC_COMM_WORLD,"Point your browser to %s for SAWs\n",sawsurl);CHKERRQ(ierr); */
5960af79b04SBarry Smith     ierr = PetscCitationsRegister("@TechReport{ saws,\n"
5970af79b04SBarry Smith                                   "  Author = {Matt Otten and Jed Brown and Barry Smith},\n"
5980af79b04SBarry Smith                                   "  Title  = {Scientific Application Web Server (SAWs) Users Manual},\n"
5990af79b04SBarry Smith                                   "  Institution = {Argonne National Laboratory},\n"
6000af79b04SBarry Smith                                   "  Year   = 2013\n}\n",NULL);CHKERRQ(ierr);
60111525c0dSBarry Smith   }
60211525c0dSBarry Smith   PetscFunctionReturn(0);
60311525c0dSBarry Smith }
60411525c0dSBarry Smith #endif
60511525c0dSBarry Smith 
606793721a6SBarry Smith #undef __FUNCT__
607e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialize"
608e5c89e4eSSatish Balay /*@C
609e5c89e4eSSatish Balay    PetscInitialize - Initializes the PETSc database and MPI.
610e5c89e4eSSatish Balay    PetscInitialize() calls MPI_Init() if that has yet to be called,
611e5c89e4eSSatish Balay    so this routine should always be called near the beginning of
612e5c89e4eSSatish Balay    your program -- usually the very first line!
613e5c89e4eSSatish Balay 
614e5c89e4eSSatish Balay    Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set
615e5c89e4eSSatish Balay 
616e5c89e4eSSatish Balay    Input Parameters:
617e5c89e4eSSatish Balay +  argc - count of number of command line arguments
618e5c89e4eSSatish Balay .  args - the command line arguments
6190298fd71SBarry Smith .  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use NULL to not check for
620fc2bca9aSBarry Smith           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
6210298fd71SBarry Smith -  help - [optional] Help message to print, use NULL for no message
622e5c89e4eSSatish Balay 
62305827820SBarry Smith    If you wish PETSc code to run ONLY on a subcommunicator of MPI_COMM_WORLD, create that
62405827820SBarry Smith    communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize(). Thus if you are running a
62505827820SBarry Smith    four process job and two processes will run PETSc and have PetscInitialize() and PetscFinalize() and two process will not,
62605827820SBarry Smith    then do this. If ALL processes in the job are using PetscInitialize() and PetscFinalize() then you don't need to do this, even
62705827820SBarry Smith    if different subcommunicators of the job are doing different things with PETSc.
628e5c89e4eSSatish Balay 
629e5c89e4eSSatish Balay    Options Database Keys:
630e5c89e4eSSatish Balay +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
631e5c89e4eSSatish Balay .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
632e5c89e4eSSatish Balay .  -on_error_emacs <machinename> causes emacsclient to jump to error file
633b52f573bSBarry Smith .  -on_error_abort calls abort() when error detected (no traceback)
634e8fb0fc0SBarry Smith .  -on_error_mpiabort calls MPI_abort() when error detected
635e8fb0fc0SBarry Smith .  -error_output_stderr prints error messages to stderr instead of the default stdout
636e8fb0fc0SBarry Smith .  -error_output_none does not print the error messages (but handles errors in the same way as if this was not called)
637e5c89e4eSSatish Balay .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
638e5c89e4eSSatish Balay .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
639e5c89e4eSSatish Balay .  -stop_for_debugger - Print message on how to attach debugger manually to
640e5c89e4eSSatish Balay                         process and wait (-debugger_pause) seconds for attachment
6412fb0ec9aSBarry Smith .  -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries)
642e5c89e4eSSatish Balay .  -malloc no - Indicates not to use error-checking malloc
6432fb0ec9aSBarry Smith .  -malloc_debug - check for memory corruption at EVERY malloc or free
644aee23540SBarry Smith .  -malloc_dump - prints a list of all unfreed memory at the end of the run
645dc92acbaSJed Brown .  -malloc_test - like -malloc_dump -malloc_debug, but only active for debugging builds
646e5c89e4eSSatish Balay .  -fp_trap - Stops on floating point exceptions (Note that on the
647e5c89e4eSSatish Balay               IBM RS6000 this slows code by at least a factor of 10.)
648e5c89e4eSSatish Balay .  -no_signal_handler - Indicates not to trap error signals
649e5c89e4eSSatish Balay .  -shared_tmp - indicates /tmp directory is shared by all processors
650e5c89e4eSSatish Balay .  -not_shared_tmp - each processor has own /tmp
651e5c89e4eSSatish Balay .  -tmp - alternative name of /tmp directory
652e5c89e4eSSatish Balay .  -get_total_flops - returns total flops done by all processors
653142c234bSBarry Smith -  -memory_info - Print memory usage at end of run
654e5c89e4eSSatish Balay 
655e5c89e4eSSatish Balay    Options Database Keys for Profiling:
656a7f22e61SSatish Balay    See Users-Manual: ch_profiling for details.
657495fc317SBarry Smith +  -info <optional filename> - Prints verbose information to the screen
658495fc317SBarry Smith .  -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages
659495fc317SBarry Smith .  -log_sync - Log the synchronization in scatters, inner products and norms
660495fc317SBarry Smith .  -log_trace [filename] - Print traces of all PETSc calls to the screen (useful to determine where a program
661e5c89e4eSSatish Balay         hangs without running in the debugger).  See PetscLogTraceBegin().
662495fc317SBarry Smith .  -log_summary [filename] - Prints summary of flop and timing information to screen. If the filename is specified the
663495fc317SBarry Smith         summary is written to the file.  See PetscLogView().
664495fc317SBarry Smith .  -log_all [filename] - Logs extensive profiling information  See PetscLogDump().
665495fc317SBarry Smith .  -log [filename] - Logs basic profiline information  See PetscLogDump().
666495fc317SBarry Smith -  -log_mpe [filename] - Creates a logfile viewable by the utility Jumpshot (in MPICH distribution)
667495fc317SBarry Smith 
668495fc317SBarry Smith     Only one of -log_trace, -log_summary, -log_all, -log, or -log_mpe may be used at a time
669e5c89e4eSSatish Balay 
670e5c89e4eSSatish Balay    Environmental Variables:
671e5c89e4eSSatish Balay +   PETSC_TMP - alternative tmp directory
672e5c89e4eSSatish Balay .   PETSC_SHARED_TMP - tmp is shared by all processes
673e5c89e4eSSatish Balay .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
674e5c89e4eSSatish Balay .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
675e5c89e4eSSatish Balay -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to
676e5c89e4eSSatish Balay 
677e5c89e4eSSatish Balay 
678e5c89e4eSSatish Balay    Level: beginner
679e5c89e4eSSatish Balay 
680e5c89e4eSSatish Balay    Notes:
681e5c89e4eSSatish Balay    If for some reason you must call MPI_Init() separately, call
682e5c89e4eSSatish Balay    it before PetscInitialize().
683e5c89e4eSSatish Balay 
684e5c89e4eSSatish Balay    Fortran Version:
685e5c89e4eSSatish Balay    In Fortran this routine has the format
686e5c89e4eSSatish Balay $       call PetscInitialize(file,ierr)
687e5c89e4eSSatish Balay 
688e5c89e4eSSatish Balay +   ierr - error return code
6890298fd71SBarry Smith -  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use NULL_CHARACTER to not check for
690fc2bca9aSBarry Smith           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
691e5c89e4eSSatish Balay 
692e5c89e4eSSatish Balay    Important Fortran Note:
6930298fd71SBarry Smith    In Fortran, you MUST use NULL_CHARACTER to indicate a
6940298fd71SBarry Smith    null character string; you CANNOT just use NULL as
695a7f22e61SSatish Balay    in the C version. See Users-Manual: ch_fortran for details.
696e5c89e4eSSatish Balay 
69701cb0274SBarry Smith    If your main program is C but you call Fortran code that also uses PETSc you need to call PetscInitializeFortran() soon after
69801cb0274SBarry Smith    calling PetscInitialize().
699e5c89e4eSSatish Balay 
700e5c89e4eSSatish Balay    Concepts: initializing PETSc
701e5c89e4eSSatish Balay 
70201cb0274SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments()
703e5c89e4eSSatish Balay 
704e5c89e4eSSatish Balay @*/
7057087cfbeSBarry Smith PetscErrorCode  PetscInitialize(int *argc,char ***args,const char file[],const char help[])
706e5c89e4eSSatish Balay {
707e5c89e4eSSatish Balay   PetscErrorCode ierr;
7084bb5149bSJed Brown   PetscMPIInt    flag, size;
709ace3abfcSBarry Smith   PetscBool      flg;
710e5c89e4eSSatish Balay   char           hostname[256];
711e5c89e4eSSatish Balay 
712e5c89e4eSSatish Balay   PetscFunctionBegin;
713e5c89e4eSSatish Balay   if (PetscInitializeCalled) PetscFunctionReturn(0);
714e5c89e4eSSatish Balay 
715ae9b4142SLisandro Dalcin   /* these must be initialized in a routine, not as a constant declaration*/
716d89683f4Sbcordonn   PETSC_STDOUT = stdout;
717ae9b4142SLisandro Dalcin   PETSC_STDERR = stderr;
718e5c89e4eSSatish Balay 
7190c30907bSSatish Balay   /* on Windows - set printf to default to printing 2 digit exponents */
7200c30907bSSatish Balay #if defined(PETSC_HAVE__SET_OUTPUT_FORMAT)
7210c30907bSSatish Balay   _set_output_format(_TWO_DIGIT_EXPONENT);
7220c30907bSSatish Balay #endif
7230c30907bSSatish Balay 
724e5c89e4eSSatish Balay   ierr = PetscOptionsCreate();CHKERRQ(ierr);
725e5c89e4eSSatish Balay 
726e5c89e4eSSatish Balay   /*
727e5c89e4eSSatish Balay      We initialize the program name here (before MPI_Init()) because MPICH has a bug in
728e5c89e4eSSatish Balay      it that it sets args[0] on all processors to be args[0] on the first processor.
729e5c89e4eSSatish Balay   */
730e5c89e4eSSatish Balay   if (argc && *argc) {
731e5c89e4eSSatish Balay     ierr = PetscSetProgramName(**args);CHKERRQ(ierr);
732e5c89e4eSSatish Balay   } else {
733e5c89e4eSSatish Balay     ierr = PetscSetProgramName("Unknown Name");CHKERRQ(ierr);
734e5c89e4eSSatish Balay   }
735e5c89e4eSSatish Balay 
736e5c89e4eSSatish Balay   ierr = MPI_Initialized(&flag);CHKERRQ(ierr);
737e5c89e4eSSatish Balay   if (!flag) {
738e32f2f54SBarry 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");
7395e765c61SJed Brown #if defined(PETSC_HAVE_MPI_INIT_THREAD)
7405e765c61SJed Brown     {
7415e765c61SJed Brown       PetscMPIInt provided;
7425e765c61SJed Brown       ierr = MPI_Init_thread(argc,args,MPI_THREAD_FUNNELED,&provided);CHKERRQ(ierr);
7435e765c61SJed Brown     }
7445e765c61SJed Brown #else
745e5c89e4eSSatish Balay     ierr = MPI_Init(argc,args);CHKERRQ(ierr);
7465e765c61SJed Brown #endif
747e5c89e4eSSatish Balay     PetscBeganMPI = PETSC_TRUE;
748e5c89e4eSSatish Balay   }
749e5c89e4eSSatish Balay   if (argc && args) {
750e5c89e4eSSatish Balay     PetscGlobalArgc = *argc;
751e5c89e4eSSatish Balay     PetscGlobalArgs = *args;
752e5c89e4eSSatish Balay   }
753e5c89e4eSSatish Balay   PetscFinalizeCalled = PETSC_FALSE;
754e5c89e4eSSatish Balay 
755a297a907SKarl Rupp   if (PETSC_COMM_WORLD == MPI_COMM_NULL) PETSC_COMM_WORLD = MPI_COMM_WORLD;
756d54338ecSKarl Rupp   ierr = MPI_Comm_set_errhandler(PETSC_COMM_WORLD,MPI_ERRORS_RETURN);CHKERRQ(ierr);
757e5c89e4eSSatish Balay 
758e5c89e4eSSatish Balay   /* Done after init due to a bug in MPICH-GM? */
759e5c89e4eSSatish Balay   ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr);
760e5c89e4eSSatish Balay 
761e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);CHKERRQ(ierr);
762e5c89e4eSSatish Balay   ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);CHKERRQ(ierr);
763e5c89e4eSSatish Balay 
7648ad47952SJed Brown   MPIU_BOOL = MPI_INT;
7658ad47952SJed Brown   MPIU_ENUM = MPI_INT;
7668ad47952SJed Brown 
767e5c89e4eSSatish Balay   /*
768e5c89e4eSSatish Balay      Initialized the global complex variable; this is because with
769e5c89e4eSSatish Balay      shared libraries the constructors for global variables
770e5c89e4eSSatish Balay      are not called; at least on IRIX.
771e5c89e4eSSatish Balay   */
772886cfec0SSatish Balay #if defined(PETSC_HAVE_COMPLEX)
773e5c89e4eSSatish Balay   {
774a24ee22aSSatish Balay #if defined(PETSC_CLANGUAGE_CXX)
77550f81f78SJed Brown     PetscComplex ic(0.0,1.0);
776e5c89e4eSSatish Balay     PETSC_i = ic;
777a24ee22aSSatish Balay #elif defined(PETSC_CLANGUAGE_C)
77850f81f78SJed Brown     PETSC_i = _Complex_I;
779b7940d39SSatish Balay #endif
780762437b8SSatish Balay   }
781762437b8SSatish Balay 
7822c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
783e69cd0e6SSatish Balay   ierr = MPI_Type_contiguous(2,MPI_DOUBLE,&MPIU_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
784500d8756SSatish Balay   ierr = MPI_Type_commit(&MPIU_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
785500d8756SSatish Balay   ierr = MPI_Type_contiguous(2,MPI_FLOAT,&MPIU_C_COMPLEX);CHKERRQ(ierr);
786500d8756SSatish Balay   ierr = MPI_Type_commit(&MPIU_C_COMPLEX);CHKERRQ(ierr);
7872c876bd9SBarry Smith #endif
788886cfec0SSatish Balay #endif /* PETSC_HAVE_COMPLEX */
789e5c89e4eSSatish Balay 
790e5c89e4eSSatish Balay   /*
791e5c89e4eSSatish Balay      Create the PETSc MPI reduction operator that sums of the first
792e5c89e4eSSatish Balay      half of the entries and maxes the second half.
793e5c89e4eSSatish Balay   */
794e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);CHKERRQ(ierr);
795e5c89e4eSSatish Balay 
796ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
797c90a1750SBarry Smith   ierr = MPI_Type_contiguous(2,MPI_DOUBLE,&MPIU___FLOAT128);CHKERRQ(ierr);
798c90a1750SBarry Smith   ierr = MPI_Type_commit(&MPIU___FLOAT128);CHKERRQ(ierr);
7997c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
8008c764dc5SJose Roman   ierr = MPI_Type_contiguous(4,MPI_DOUBLE,&MPIU___COMPLEX128);CHKERRQ(ierr);
8018c764dc5SJose Roman   ierr = MPI_Type_commit(&MPIU___COMPLEX128);CHKERRQ(ierr);
8028c764dc5SJose Roman #endif
803d9822059SBarry Smith   ierr = MPI_Op_create(PetscMax_Local,1,&MPIU_MAX);CHKERRQ(ierr);
804d9822059SBarry Smith   ierr = MPI_Op_create(PetscMin_Local,1,&MPIU_MIN);CHKERRQ(ierr);
805c90a1750SBarry Smith #endif
806c90a1750SBarry Smith 
8077c2de775SJed Brown #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128)
808cca4cb22SSatish Balay   ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);CHKERRQ(ierr);
809cca4cb22SSatish Balay #endif
810cca4cb22SSatish Balay 
811e5c89e4eSSatish Balay   ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);CHKERRQ(ierr);
812e5c89e4eSSatish Balay   ierr = MPI_Type_commit(&MPIU_2SCALAR);CHKERRQ(ierr);
813e5c89e4eSSatish Balay 
81444041f26SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
815e5c89e4eSSatish Balay   ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);CHKERRQ(ierr);
816e5c89e4eSSatish Balay   ierr = MPI_Type_commit(&MPIU_2INT);CHKERRQ(ierr);
81744041f26SJed Brown #endif
818e5c89e4eSSatish Balay 
819ec957eceSBarry Smith 
820e5c89e4eSSatish Balay   /*
821480cf27aSJed Brown      Attributes to be set on PETSc communicators
822480cf27aSJed Brown   */
823480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelCounter,&Petsc_Counter_keyval,(void*)0);CHKERRQ(ierr);
824da3039f7SJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm_Outer,&Petsc_InnerComm_keyval,(void*)0);CHKERRQ(ierr);
825da3039f7SJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm_Inner,&Petsc_OuterComm_keyval,(void*)0);CHKERRQ(ierr);
826480cf27aSJed Brown 
827480cf27aSJed Brown   /*
828e8fb0fc0SBarry Smith      Build the options database
829e5c89e4eSSatish Balay   */
830e5c89e4eSSatish Balay   ierr = PetscOptionsInsert(argc,args,file);CHKERRQ(ierr);
831e5c89e4eSSatish Balay 
8326dc8fec2Sbcordonn 
833e5c89e4eSSatish Balay   /*
834e5c89e4eSSatish Balay      Print main application help message
835e5c89e4eSSatish Balay   */
8360298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-help",&flg);CHKERRQ(ierr);
837e5c89e4eSSatish Balay   if (help && flg) {
838e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,help);CHKERRQ(ierr);
839e5c89e4eSSatish Balay   }
840e5c89e4eSSatish Balay   ierr = PetscOptionsCheckInitial_Private();CHKERRQ(ierr);
841e5c89e4eSSatish Balay 
842d45a07a7SBarry Smith   ierr = PetscCitationsInitialize();CHKERRQ(ierr);
843d45a07a7SBarry Smith 
844e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
84511525c0dSBarry Smith   ierr = PetscInitializeSAWs(help);CHKERRQ(ierr);
846f4202a44SBarry Smith #endif
847f4202a44SBarry Smith 
848e5c89e4eSSatish Balay   /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */
849a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
850e5c89e4eSSatish Balay   ierr = PetscLogBegin_Private();CHKERRQ(ierr);
851a9f03627SSatish Balay #endif
852e5c89e4eSSatish Balay 
853e5c89e4eSSatish Balay   /*
854e5c89e4eSSatish Balay      Load the dynamic libraries (on machines that support them), this registers all
855e5c89e4eSSatish Balay      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
856e5c89e4eSSatish Balay   */
857e5c89e4eSSatish Balay   ierr = PetscInitialize_DynamicLibraries();CHKERRQ(ierr);
858e5c89e4eSSatish Balay 
859e5c89e4eSSatish Balay   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
860ae15b995SBarry Smith   ierr = PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);CHKERRQ(ierr);
861e5c89e4eSSatish Balay   ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr);
862ae15b995SBarry Smith   ierr = PetscInfo1(0,"Running on machine: %s\n",hostname);CHKERRQ(ierr);
863e5c89e4eSSatish Balay 
86467234432SDmitry Karpeev   /* Ensure that threadcomm-related keyval exists, so that PetscOptionsSetFromOptions can use PetscCommDuplicate. */
86567234432SDmitry Karpeev   ierr = PetscThreadCommInitializePackage();CHKERRQ(ierr);
86667234432SDmitry Karpeev 
867e5c89e4eSSatish Balay   ierr = PetscOptionsCheckInitial_Components();CHKERRQ(ierr);
868ef6c6fedSBoyana Norris   /* Check the options database for options related to the options database itself */
869ef6c6fedSBoyana Norris   ierr = PetscOptionsSetFromOptions();CHKERRQ(ierr);
870ef6c6fedSBoyana Norris 
871951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
872e39fd77fSBarry Smith   /*
873e39fd77fSBarry Smith       Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI
874e39fd77fSBarry Smith 
875e39fd77fSBarry Smith       Currently not used because it is not supported by MPICH.
876e39fd77fSBarry Smith   */
877e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
8780298fd71SBarry Smith   ierr = MPI_Register_datarep((char*)"petsc",PetscDataRep_read_conv_fn,PetscDataRep_write_conv_fn,PetscDataRep_extent_fn,NULL);CHKERRQ(ierr);
879e39fd77fSBarry Smith #endif
880951e3c8eSBarry Smith #endif
881e39fd77fSBarry Smith 
88235d88935SVictor Minden #if defined(PETSC_HAVE_CUDA)
88320e2c332SMatthew G. Knepley   flg  = PETSC_TRUE;
88420e2c332SMatthew G. Knepley   ierr = PetscOptionsGetBool(NULL,"-cublas",&flg,NULL);CHKERRQ(ierr);
88520e2c332SMatthew G. Knepley   if (flg) {
8864bb5149bSJed Brown     PetscMPIInt p;
887663ba86aSMatthew G Knepley     for (p = 0; p < PetscGlobalSize; ++p) {
888a297a907SKarl Rupp       if (p == PetscGlobalRank) cublasInit();
889663ba86aSMatthew G Knepley       ierr = MPI_Barrier(PETSC_COMM_WORLD);CHKERRQ(ierr);
890663ba86aSMatthew G Knepley     }
8914bb5149bSJed Brown   }
8923e39abd9SVictor Minden #endif
89392e62aa6SBarry Smith 
8940298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-python",&flg);CHKERRQ(ierr);
8959ac80d5eSLisandro Dalcin   if (flg) {
8969ac80d5eSLisandro Dalcin     PetscInitializeCalled = PETSC_TRUE;
8970298fd71SBarry Smith     ierr = PetscPythonInitialize(NULL,NULL);CHKERRQ(ierr);
8989ac80d5eSLisandro Dalcin   }
8999ac80d5eSLisandro Dalcin 
90041c0b4b3SShri Abhyankar   /*
90141c0b4b3SShri Abhyankar       Setup building of stack frames for all function calls
90241c0b4b3SShri Abhyankar   */
90376386721SLisandro Dalcin   PetscThreadLocalRegister((PetscThreadKey*)&petscstack); /* Creates pthread_key */
904e1167bb9SShri Abhyankar #if defined(PETSC_USE_DEBUG)
905e1167bb9SShri Abhyankar   ierr = PetscStackCreate();CHKERRQ(ierr);
906e1167bb9SShri Abhyankar #endif
907e1167bb9SShri Abhyankar 
9082d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
9092d53ad75SBarry Smith   ierr = PetscFPTCreate(10000);CHKERRQ(ierr);
9102d53ad75SBarry Smith #endif
9112d53ad75SBarry Smith 
912dff31646SBarry Smith 
913301d30feSBarry Smith   /*
914301d30feSBarry Smith       Once we are completedly initialized then we can set this variables
915301d30feSBarry Smith   */
916301d30feSBarry Smith   PetscInitializeCalled = PETSC_TRUE;
917301d30feSBarry Smith   PetscFunctionReturn(0);
918e5c89e4eSSatish Balay }
919e5c89e4eSSatish Balay 
9204097062eSBarry Smith #if defined(PETSC_USE_LOG)
9212eff7a51SBarry Smith extern PetscObject *PetscObjects;
9222eff7a51SBarry Smith extern PetscInt    PetscObjectsCounts, PetscObjectsMaxCounts;
9234097062eSBarry Smith #endif
924e5c89e4eSSatish Balay 
925e5c89e4eSSatish Balay #undef __FUNCT__
926e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize"
927e5c89e4eSSatish Balay /*@C
928e5c89e4eSSatish Balay    PetscFinalize - Checks for options to be called at the conclusion
929e5c89e4eSSatish Balay    of the program. MPI_Finalize() is called only if the user had not
930e5c89e4eSSatish Balay    called MPI_Init() before calling PetscInitialize().
931e5c89e4eSSatish Balay 
932e5c89e4eSSatish Balay    Collective on PETSC_COMM_WORLD
933e5c89e4eSSatish Balay 
934e5c89e4eSSatish Balay    Options Database Keys:
93588c29154SBarry Smith +  -options_table - Calls PetscOptionsView()
936e5c89e4eSSatish Balay .  -options_left - Prints unused options that remain in the database
9377eb1d149SBarry Smith .  -objects_dump [all] - Prints list of objects allocated by the user that have not been freed, the option all cause all outstanding objects to be listed
938e5c89e4eSSatish Balay .  -mpidump - Calls PetscMPIDump()
939e5c89e4eSSatish Balay .  -malloc_dump - Calls PetscMallocDump()
940e5c89e4eSSatish Balay .  -malloc_info - Prints total memory usage
941e5c89e4eSSatish Balay -  -malloc_log - Prints summary of memory usage
942e5c89e4eSSatish Balay 
943e5c89e4eSSatish Balay    Level: beginner
944e5c89e4eSSatish Balay 
945e5c89e4eSSatish Balay    Note:
946e5c89e4eSSatish Balay    See PetscInitialize() for more general runtime options.
947e5c89e4eSSatish Balay 
94888c29154SBarry Smith .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
949e5c89e4eSSatish Balay @*/
9507087cfbeSBarry Smith PetscErrorCode  PetscFinalize(void)
951e5c89e4eSSatish Balay {
952e5c89e4eSSatish Balay   PetscErrorCode ierr;
9534bb5149bSJed Brown   PetscMPIInt    rank;
954a8d2bbe5SBarry Smith   PetscInt       nopt;
9552bf49c77SBarry Smith   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
956dff31646SBarry Smith   PetscBool      flg;
95710463e74SBarry Smith #if defined(PETSC_USE_LOG)
95810463e74SBarry Smith   char           mname[PETSC_MAX_PATH_LEN];
95910463e74SBarry Smith #endif
960e5c89e4eSSatish Balay 
961e5c89e4eSSatish Balay   PetscFunctionBegin;
962e5c89e4eSSatish Balay   if (!PetscInitializeCalled) {
9634b09e917SBarry Smith     printf("PetscInitialize() must be called before PetscFinalize()\n");
9644b09e917SBarry Smith     PetscFunctionReturn(PETSC_ERR_ARG_WRONGSTATE);
965e5c89e4eSSatish Balay   }
9660298fd71SBarry Smith   ierr = PetscInfo(NULL,"PetscFinalize() called\n");CHKERRQ(ierr);
967b022a5c1SBarry Smith 
9681f817a21SBarry Smith   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
9691f817a21SBarry Smith 
9701f817a21SBarry Smith   ierr = PetscOptionsHasName(NULL,"-citations",&flg);CHKERRQ(ierr);
971dff31646SBarry Smith   if (flg) {
9721f817a21SBarry Smith     char  *cits, filename[PETSC_MAX_PATH_LEN];
9731f817a21SBarry Smith     FILE  *fd = PETSC_STDOUT;
9741f817a21SBarry Smith 
9751f817a21SBarry Smith     ierr = PetscOptionsGetString(NULL,"-citations",filename,PETSC_MAX_PATH_LEN,NULL);CHKERRQ(ierr);
9761f817a21SBarry Smith     if (filename[0]) {
9771f817a21SBarry Smith       ierr = PetscFOpen(PETSC_COMM_WORLD,filename,"w",&fd);CHKERRQ(ierr);
9781f817a21SBarry Smith     }
979dff31646SBarry Smith     ierr = PetscSegBufferGet(PetscCitationsList,1,&cits);CHKERRQ(ierr);
980dff31646SBarry Smith     cits[0] = 0;
981dff31646SBarry Smith     ierr = PetscSegBufferExtractAlloc(PetscCitationsList,&cits);CHKERRQ(ierr);
9821f817a21SBarry Smith     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"If you publish results based on this computation please cite the following:\n");CHKERRQ(ierr);
9831f817a21SBarry Smith     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"===========================================================================\n");CHKERRQ(ierr);
9841f817a21SBarry Smith     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"%s",cits);CHKERRQ(ierr);
9851f817a21SBarry Smith     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"===========================================================================\n");CHKERRQ(ierr);
9861f817a21SBarry Smith     ierr = PetscFClose(PETSC_COMM_WORLD,fd);CHKERRQ(ierr);
987dff31646SBarry Smith     ierr = PetscFree(cits);CHKERRQ(ierr);
988dff31646SBarry Smith   }
989dff31646SBarry Smith   ierr = PetscSegBufferDestroy(&PetscCitationsList);CHKERRQ(ierr);
990dff31646SBarry Smith 
991c2a97968SBarry Smith #if defined(PETSC_HAVE_SSL) && defined(PETSC_USE_SOCKET_VIEWER)
99204102261SBarry Smith   /* TextBelt is run for testing purposes only, please do not use this feature often */
99304102261SBarry Smith   {
99404102261SBarry Smith     PetscInt nmax = 2;
99504102261SBarry Smith     char     **buffs;
99604102261SBarry Smith     ierr = PetscMalloc1(2,&buffs);CHKERRQ(ierr);
99704102261SBarry Smith     ierr = PetscOptionsGetStringArray(NULL,"-textbelt",buffs,&nmax,&flg1);CHKERRQ(ierr);
99804102261SBarry Smith     if (flg1) {
99904102261SBarry Smith       if (!nmax) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_USER,"-textbelt requires either the phone number or number,\"message\"");
100004102261SBarry Smith       if (nmax == 1) {
100104102261SBarry Smith         ierr = PetscMalloc1(128,&buffs[1]);CHKERRQ(ierr);
100204102261SBarry Smith         ierr = PetscGetProgramName(buffs[1],32);CHKERRQ(ierr);
100304102261SBarry Smith         ierr = PetscStrcat(buffs[1]," has completed");CHKERRQ(ierr);
100404102261SBarry Smith       }
100504102261SBarry Smith       ierr = PetscTextBelt(PETSC_COMM_WORLD,buffs[0],buffs[1],NULL);CHKERRQ(ierr);
100604102261SBarry Smith       ierr = PetscFree(buffs[0]);CHKERRQ(ierr);
100704102261SBarry Smith       ierr = PetscFree(buffs[1]);CHKERRQ(ierr);
100804102261SBarry Smith     }
100904102261SBarry Smith     ierr = PetscFree(buffs);CHKERRQ(ierr);
101004102261SBarry Smith   }
101104102261SBarry Smith #endif
101267234432SDmitry Karpeev   /*
101367234432SDmitry Karpeev     It should be safe to cancel the options monitors, since we don't expect to be setting options
101467234432SDmitry Karpeev     here (at least that are worth monitoring).  Monitors ought to be released so that they release
101567234432SDmitry Karpeev     whatever memory was allocated there before -malloc_dump reports unfreed memory.
101667234432SDmitry Karpeev   */
101767234432SDmitry Karpeev   ierr = PetscOptionsMonitorCancel();CHKERRQ(ierr);
101804102261SBarry Smith 
10192d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
10202d53ad75SBarry Smith   ierr = PetscFPTDestroy();CHKERRQ(ierr);
10212d53ad75SBarry Smith #endif
10222d53ad75SBarry Smith 
10232d53ad75SBarry Smith 
1024e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1025dff31646SBarry Smith   flg = PETSC_FALSE;
1026e78c4b8cSBarry Smith   ierr = PetscOptionsGetBool(NULL,"-saw_options",&flg,NULL);CHKERRQ(ierr);
1027d5649816SBarry Smith   if (flg) {
1028e04113cfSBarry Smith     ierr = PetscOptionsSAWsDestroy();CHKERRQ(ierr);
1029d5649816SBarry Smith   }
1030d5649816SBarry Smith #endif
1031d5649816SBarry Smith 
1032681455b2SBarry Smith #if defined(PETSC_HAVE_X)
1033681455b2SBarry Smith   flg1 = PETSC_FALSE;
1034681455b2SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-x_virtual",&flg1,NULL);CHKERRQ(ierr);
1035681455b2SBarry Smith   if (flg1) {
1036681455b2SBarry Smith     /*  this is a crude hack, but better than nothing */
1037681455b2SBarry Smith     ierr = PetscPOpen(PETSC_COMM_WORLD,NULL,"pkill -9 Xvfb","r",NULL);CHKERRQ(ierr);
1038681455b2SBarry Smith   }
1039681455b2SBarry Smith #endif
1040681455b2SBarry Smith 
10410298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-malloc_info",&flg2,NULL);CHKERRQ(ierr);
1042e5c89e4eSSatish Balay   if (!flg2) {
104390d69ab7SBarry Smith     flg2 = PETSC_FALSE;
10440298fd71SBarry Smith     ierr = PetscOptionsGetBool(NULL,"-memory_info",&flg2,NULL);CHKERRQ(ierr);
1045e5c89e4eSSatish Balay   }
1046e5c89e4eSSatish Balay   if (flg2) {
1047e5c89e4eSSatish Balay     ierr = PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");CHKERRQ(ierr);
1048e5c89e4eSSatish Balay   }
1049e5c89e4eSSatish Balay 
1050e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
105190d69ab7SBarry Smith   flg1 = PETSC_FALSE;
10520298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-get_total_flops",&flg1,NULL);CHKERRQ(ierr);
1053e5c89e4eSSatish Balay   if (flg1) {
1054e5c89e4eSSatish Balay     PetscLogDouble flops = 0;
1055205a32c2SJed Brown     ierr = MPI_Reduce(&petsc_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
1056e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);CHKERRQ(ierr);
1057e5c89e4eSSatish Balay   }
1058e5c89e4eSSatish Balay #endif
1059e5c89e4eSSatish Balay 
1060e5c89e4eSSatish Balay 
1061e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
1062e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE)
1063e5c89e4eSSatish Balay   mname[0] = 0;
1064a297a907SKarl Rupp 
10650298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
1066e5c89e4eSSatish Balay   if (flg1) {
1067e5c89e4eSSatish Balay     if (mname[0]) {ierr = PetscLogMPEDump(mname);CHKERRQ(ierr);}
1068e5c89e4eSSatish Balay     else          {ierr = PetscLogMPEDump(0);CHKERRQ(ierr);}
1069e5c89e4eSSatish Balay   }
1070e5c89e4eSSatish Balay #endif
1071e5c89e4eSSatish Balay   mname[0] = 0;
1072a297a907SKarl Rupp 
1073f14045dbSBarry Smith   ierr = PetscLogViewFromOptions();CHKERRQ(ierr);
10740298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
1075e5c89e4eSSatish Balay   if (flg1) {
107691eabc43SBarry Smith     PetscViewer viewer;
107791eabc43SBarry Smith     if (mname[0]) {
107891eabc43SBarry Smith       ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);CHKERRQ(ierr);
107991eabc43SBarry Smith       ierr = PetscLogView(viewer);CHKERRQ(ierr);
10806bf464f9SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
108133f85c2fSBarry Smith     } else {
108233f85c2fSBarry Smith       viewer = PETSC_VIEWER_STDOUT_WORLD;
108333f85c2fSBarry Smith       ierr   = PetscLogView(viewer);CHKERRQ(ierr);
108433f85c2fSBarry Smith     }
1085e5c89e4eSSatish Balay   }
1086e5c89e4eSSatish Balay   mname[0] = 0;
1087a297a907SKarl Rupp 
10880298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
10890298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);CHKERRQ(ierr);
1090e5c89e4eSSatish Balay   if (flg1 || flg2) {
1091e5c89e4eSSatish Balay     if (mname[0]) PetscLogDump(mname);
1092e5c89e4eSSatish Balay     else          PetscLogDump(0);
1093e5c89e4eSSatish Balay   }
1094e5c89e4eSSatish Balay #endif
109510463e74SBarry Smith 
1096b58ca069SBarry Smith   /*
1097b58ca069SBarry Smith      Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_().
1098b58ca069SBarry Smith   */
1099b58ca069SBarry Smith   ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr);
1100b58ca069SBarry Smith 
110133f85c2fSBarry Smith   ierr = PetscStackDestroy();CHKERRQ(ierr);
110276386721SLisandro Dalcin   PetscThreadLocalDestroy((PetscThreadKey)petscstack); /* Deletes pthread_key */
110310463e74SBarry Smith 
110490d69ab7SBarry Smith   flg1 = PETSC_FALSE;
11050298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-no_signal_handler",&flg1,NULL);CHKERRQ(ierr);
1106e5c89e4eSSatish Balay   if (!flg1) { ierr = PetscPopSignalHandler();CHKERRQ(ierr);}
110790d69ab7SBarry Smith   flg1 = PETSC_FALSE;
11080298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-mpidump",&flg1,NULL);CHKERRQ(ierr);
1109e5c89e4eSSatish Balay   if (flg1) {
1110e5c89e4eSSatish Balay     ierr = PetscMPIDump(stdout);CHKERRQ(ierr);
1111e5c89e4eSSatish Balay   }
111290d69ab7SBarry Smith   flg1 = PETSC_FALSE;
111390d69ab7SBarry Smith   flg2 = PETSC_FALSE;
11148bb29257SSatish Balay   /* preemptive call to avoid listing this option in options table as unused */
11150298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-malloc_dump",&flg1);CHKERRQ(ierr);
11160298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-objects_dump",&flg1);CHKERRQ(ierr);
1117662a7babSBarry Smith   ierr = PetscOptionsGetBool(NULL,"-options_view",&flg2,NULL);CHKERRQ(ierr);
1118e4c476e2SSatish Balay 
1119e5c89e4eSSatish Balay   if (flg2) {
1120be56827dSJed Brown     PetscViewer viewer;
112102ba9f54SBarry Smith     ierr = PetscViewerCreate(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr);
112202ba9f54SBarry Smith     ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
1123be56827dSJed Brown     ierr = PetscOptionsView(viewer);CHKERRQ(ierr);
1124be56827dSJed Brown     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1125e5c89e4eSSatish Balay   }
1126e5c89e4eSSatish Balay 
1127e5c89e4eSSatish Balay   /* to prevent PETSc -options_left from warning */
11280298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-nox",&flg1);CHKERRQ(ierr);
11290298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-nox_warning",&flg1);CHKERRQ(ierr);
1130e5c89e4eSSatish Balay 
113133fc4174SSatish Balay   flg3 = PETSC_FALSE; /* default value is required */
11320298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-options_left",&flg3,&flg1);CHKERRQ(ierr);
1133e5c89e4eSSatish Balay   ierr = PetscOptionsAllUsed(&nopt);CHKERRQ(ierr);
1134e5c89e4eSSatish Balay   if (flg3) {
1135e5c89e4eSSatish Balay     if (!flg2) { /* have not yet printed the options */
1136be56827dSJed Brown       PetscViewer viewer;
113702ba9f54SBarry Smith       ierr = PetscViewerCreate(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr);
113802ba9f54SBarry Smith       ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
1139be56827dSJed Brown       ierr = PetscOptionsView(viewer);CHKERRQ(ierr);
1140be56827dSJed Brown       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1141e5c89e4eSSatish Balay     }
1142e5c89e4eSSatish Balay     if (!nopt) {
1143e5c89e4eSSatish Balay       ierr = PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");CHKERRQ(ierr);
1144e5c89e4eSSatish Balay     } else if (nopt == 1) {
1145e5c89e4eSSatish Balay       ierr = PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");CHKERRQ(ierr);
1146e5c89e4eSSatish Balay     } else {
11477582186dSLisandro Dalcin       ierr = PetscPrintf(PETSC_COMM_WORLD,"There are %D unused database options. They are:\n",nopt);CHKERRQ(ierr);
1148e5c89e4eSSatish Balay     }
1149df12ba86SBarry Smith   }
1150e5c89e4eSSatish Balay #if defined(PETSC_USE_DEBUG)
1151da8b8a77SBarry Smith   if (nopt && !flg3 && !flg1) {
1152e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");CHKERRQ(ierr);
1153e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");CHKERRQ(ierr);
1154e5c89e4eSSatish Balay     ierr = PetscOptionsLeft();CHKERRQ(ierr);
1155e5c89e4eSSatish Balay   } else if (nopt && flg3) {
1156e5c89e4eSSatish Balay #else
1157e5c89e4eSSatish Balay   if (nopt && flg3) {
1158e5c89e4eSSatish Balay #endif
1159e5c89e4eSSatish Balay     ierr = PetscOptionsLeft();CHKERRQ(ierr);
1160e5c89e4eSSatish Balay   }
1161e5c89e4eSSatish Balay 
1162e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1163d45a07a7SBarry Smith   if (!PetscGlobalRank) {
116487f587eeSBarry Smith     ierr = PetscStackSAWsViewOff();CHKERRQ(ierr);
116516ad0300SBarry Smith     PetscStackCallSAWs(SAWs_Finalize,());
1166d45a07a7SBarry Smith   }
1167ec957eceSBarry Smith #endif
1168ec957eceSBarry Smith 
1169eea2bdeeSJed Brown   {
1170eea2bdeeSJed Brown     PetscThreadComm tcomm_world;
1171eea2bdeeSJed Brown     ierr = PetscGetThreadCommWorld(&tcomm_world);CHKERRQ(ierr);
1172eea2bdeeSJed Brown     /* Free global thread communicator */
1173eea2bdeeSJed Brown     ierr = PetscThreadCommDestroy(&tcomm_world);CHKERRQ(ierr);
1174eea2bdeeSJed Brown   }
1175eea2bdeeSJed Brown 
11764097062eSBarry Smith #if defined(PETSC_USE_LOG)
117710463e74SBarry Smith   /*
1178dbc8283eSBarry Smith        List all objects the user may have forgot to free
11792eff7a51SBarry Smith   */
11800298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-objects_dump",&flg1);CHKERRQ(ierr);
1181a64a8e02SBarry Smith   if (flg1) {
1182a64a8e02SBarry Smith     MPI_Comm local_comm;
11837eb1d149SBarry Smith     char     string[64];
1184a64a8e02SBarry Smith 
11850298fd71SBarry Smith     ierr = PetscOptionsGetString(NULL,"-objects_dump",string,64,NULL);CHKERRQ(ierr);
1186a64a8e02SBarry Smith     ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr);
1187a64a8e02SBarry Smith     ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr);
11887eb1d149SBarry Smith     ierr = PetscObjectsDump(stdout,(string[0] == 'a') ? PETSC_TRUE : PETSC_FALSE);CHKERRQ(ierr);
1189a64a8e02SBarry Smith     ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr);
1190a64a8e02SBarry Smith     ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr);
11910a1571b3SBarry Smith   }
11924097062eSBarry Smith #endif
11934097062eSBarry Smith 
11944097062eSBarry Smith #if defined(PETSC_USE_LOG)
1195dbc8283eSBarry Smith   PetscObjectsCounts    = 0;
1196dbc8283eSBarry Smith   PetscObjectsMaxCounts = 0;
1197a297a907SKarl Rupp   ierr = PetscFree(PetscObjects);CHKERRQ(ierr);
11984097062eSBarry Smith #endif
11992eff7a51SBarry Smith 
120033f85c2fSBarry Smith #if defined(PETSC_USE_LOG)
120133f85c2fSBarry Smith   ierr = PetscLogDestroy();CHKERRQ(ierr);
120233f85c2fSBarry Smith #endif
120333f85c2fSBarry Smith 
120433f85c2fSBarry Smith   /*
120522b6d1caSBarry Smith      Close any open dynamic libraries
120622b6d1caSBarry Smith   */
120722b6d1caSBarry Smith   ierr = PetscFinalize_DynamicLibraries();CHKERRQ(ierr);
120822b6d1caSBarry Smith 
120922b6d1caSBarry Smith   /*
121033f85c2fSBarry Smith      Destroy any packages that registered a finalize
121133f85c2fSBarry Smith   */
121233f85c2fSBarry Smith   ierr = PetscRegisterFinalizeAll();CHKERRQ(ierr);
121333f85c2fSBarry Smith 
121433f85c2fSBarry Smith   /*
121548dd1dffSBarry Smith      Print PetscFunctionLists that have not been properly freed
121648dd1dffSBarry Smith 
121737e93019SBarry Smith   ierr = PetscFunctionListPrintAll();CHKERRQ(ierr);
121848dd1dffSBarry Smith   */
121937e93019SBarry Smith 
12204028d114SSatish Balay   if (petsc_history) {
1221f3dea69dSBarry Smith     ierr = PetscCloseHistoryFile(&petsc_history);CHKERRQ(ierr);
1222e5c89e4eSSatish Balay     petsc_history = 0;
1223e5c89e4eSSatish Balay   }
1224e5c89e4eSSatish Balay 
12250298fd71SBarry Smith   ierr = PetscInfoAllow(PETSC_FALSE,NULL);CHKERRQ(ierr);
1226e5c89e4eSSatish Balay 
12278bb29257SSatish Balay   {
1228e5c89e4eSSatish Balay     char fname[PETSC_MAX_PATH_LEN];
1229e5c89e4eSSatish Balay     FILE *fd;
1230ed9cf6e9SBarry Smith     int  err;
1231e5c89e4eSSatish Balay 
1232e5c89e4eSSatish Balay     fname[0] = 0;
1233a297a907SKarl Rupp 
12340298fd71SBarry Smith     ierr = PetscOptionsGetString(NULL,"-malloc_dump",fname,250,&flg1);CHKERRQ(ierr);
1235dc92acbaSJed Brown     flg2 = PETSC_FALSE;
12360298fd71SBarry Smith     ierr = PetscOptionsGetBool(NULL,"-malloc_test",&flg2,NULL);CHKERRQ(ierr);
12378bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG)
1238dc92acbaSJed Brown     if (PETSC_RUNNING_ON_VALGRIND) flg2 = PETSC_FALSE;
1239dc92acbaSJed Brown #else
1240dc92acbaSJed Brown     flg2 = PETSC_FALSE;         /* Skip reporting for optimized builds regardless of -malloc_test */
1241dc92acbaSJed Brown #endif
1242e5c89e4eSSatish Balay     if (flg1 && fname[0]) {
1243e5c89e4eSSatish Balay       char sname[PETSC_MAX_PATH_LEN];
1244e5c89e4eSSatish Balay 
1245e5c89e4eSSatish Balay       sprintf(sname,"%s_%d",fname,rank);
1246e32f2f54SBarry Smith       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
1247e5c89e4eSSatish Balay       ierr = PetscMallocDump(fd);CHKERRQ(ierr);
1248ed9cf6e9SBarry Smith       err  = fclose(fd);
1249e32f2f54SBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
1250dc92acbaSJed Brown     } else if (flg1 || flg2) {
1251e5c89e4eSSatish Balay       MPI_Comm local_comm;
1252e5c89e4eSSatish Balay 
1253e5c89e4eSSatish Balay       ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr);
1254e5c89e4eSSatish Balay       ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr);
1255e5c89e4eSSatish Balay       ierr = PetscMallocDump(stdout);CHKERRQ(ierr);
1256e5c89e4eSSatish Balay       ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr);
1257e5c89e4eSSatish Balay       ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr);
1258e5c89e4eSSatish Balay     }
1259e5c89e4eSSatish Balay   }
1260a64a8e02SBarry Smith 
12618bb29257SSatish Balay   {
1262e5c89e4eSSatish Balay     char fname[PETSC_MAX_PATH_LEN];
12630298fd71SBarry Smith     FILE *fd = NULL;
1264e5c89e4eSSatish Balay 
1265e5c89e4eSSatish Balay     fname[0] = 0;
1266a297a907SKarl Rupp 
12670298fd71SBarry Smith     ierr = PetscOptionsGetString(NULL,"-malloc_log",fname,250,&flg1);CHKERRQ(ierr);
12680298fd71SBarry Smith     ierr = PetscOptionsHasName(NULL,"-malloc_log_threshold",&flg2);CHKERRQ(ierr);
1269e5c89e4eSSatish Balay     if (flg1 && fname[0]) {
1270ed9cf6e9SBarry Smith       int err;
1271e5c89e4eSSatish Balay 
1272574034a9SJed Brown       if (!rank) {
1273574034a9SJed Brown         fd = fopen(fname,"w");
1274574034a9SJed Brown         if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",fname);
1275574034a9SJed Brown       }
1276e5c89e4eSSatish Balay       ierr = PetscMallocDumpLog(fd);CHKERRQ(ierr);
1277574034a9SJed Brown       if (fd) {
1278ed9cf6e9SBarry Smith         err = fclose(fd);
1279e32f2f54SBarry Smith         if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
1280574034a9SJed Brown       }
1281574034a9SJed Brown     } else if (flg1 || flg2) {
1282e5c89e4eSSatish Balay       ierr = PetscMallocDumpLog(stdout);CHKERRQ(ierr);
1283e5c89e4eSSatish Balay     }
1284e5c89e4eSSatish Balay   }
128520e2c332SMatthew G. Knepley 
128620e2c332SMatthew G. Knepley #if defined(PETSC_HAVE_CUDA)
128720e2c332SMatthew G. Knepley   flg  = PETSC_TRUE;
128820e2c332SMatthew G. Knepley   ierr = PetscOptionsGetBool(NULL,"-cublas",&flg,NULL);CHKERRQ(ierr);
128920e2c332SMatthew G. Knepley   if (flg) {
129020e2c332SMatthew G. Knepley     PetscInt p;
129120e2c332SMatthew G. Knepley     for (p = 0; p < PetscGlobalSize; ++p) {
129220e2c332SMatthew G. Knepley       if (p == PetscGlobalRank) cublasShutdown();
129320e2c332SMatthew G. Knepley       ierr = MPI_Barrier(PETSC_COMM_WORLD);CHKERRQ(ierr);
129420e2c332SMatthew G. Knepley     }
129520e2c332SMatthew G. Knepley   }
129620e2c332SMatthew G. Knepley #endif
129720e2c332SMatthew G. Knepley 
1298e5c89e4eSSatish Balay   /* Can be destroyed only after all the options are used */
1299e5c89e4eSSatish Balay   ierr = PetscOptionsDestroy();CHKERRQ(ierr);
1300e5c89e4eSSatish Balay 
1301e5c89e4eSSatish Balay   PetscGlobalArgc = 0;
1302e5c89e4eSSatish Balay   PetscGlobalArgs = 0;
1303e5c89e4eSSatish Balay 
1304ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
1305c90a1750SBarry Smith   ierr = MPI_Type_free(&MPIU___FLOAT128);CHKERRQ(ierr);
13067c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
13078c764dc5SJose Roman   ierr = MPI_Type_free(&MPIU___COMPLEX128);CHKERRQ(ierr);
13088c764dc5SJose Roman #endif
1309d9822059SBarry Smith   ierr = MPI_Op_free(&MPIU_MAX);CHKERRQ(ierr);
1310d9822059SBarry Smith   ierr = MPI_Op_free(&MPIU_MIN);CHKERRQ(ierr);
1311c90a1750SBarry Smith #endif
1312c90a1750SBarry Smith 
13137c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
13142c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
1315500d8756SSatish Balay   ierr = MPI_Type_free(&MPIU_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
1316500d8756SSatish Balay   ierr = MPI_Type_free(&MPIU_C_COMPLEX);CHKERRQ(ierr);
13172c876bd9SBarry Smith #endif
1318e5c89e4eSSatish Balay #endif
1319cca4cb22SSatish Balay 
13207c2de775SJed Brown #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128)
1321cca4cb22SSatish Balay   ierr = MPI_Op_free(&MPIU_SUM);CHKERRQ(ierr);
1322cca4cb22SSatish Balay #endif
1323cca4cb22SSatish Balay 
1324e5c89e4eSSatish Balay   ierr = MPI_Type_free(&MPIU_2SCALAR);CHKERRQ(ierr);
132544041f26SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
1326e5c89e4eSSatish Balay   ierr = MPI_Type_free(&MPIU_2INT);CHKERRQ(ierr);
132744041f26SJed Brown #endif
1328e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscMaxSum_Op);CHKERRQ(ierr);
1329e5c89e4eSSatish Balay 
1330dbc8283eSBarry Smith   /*
1331efb80d3cSBarry Smith      Destroy any known inner MPI_Comm's and attributes pointing to them
1332efb80d3cSBarry Smith      Note this will not destroy any new communicators the user has created.
1333efb80d3cSBarry Smith 
1334efb80d3cSBarry Smith      If all PETSc objects were not destroyed those left over objects will have hanging references to
1335efb80d3cSBarry Smith      the MPI_Comms that were freed; but that is ok because those PETSc objects will never be used again
1336dbc8283eSBarry Smith  */
1337b770b1f6SSatish Balay   {
1338dbc8283eSBarry Smith     PetscCommCounter *counter;
1339dbc8283eSBarry Smith     PetscMPIInt      flg;
1340dbc8283eSBarry Smith     MPI_Comm         icomm;
1341265f3f35SJed Brown     union {MPI_Comm comm; void *ptr;} ucomm;
1342265f3f35SJed Brown     ierr = MPI_Attr_get(PETSC_COMM_SELF,Petsc_InnerComm_keyval,&ucomm,&flg);CHKERRQ(ierr);
1343dbc8283eSBarry Smith     if (flg) {
1344265f3f35SJed Brown       icomm = ucomm.comm;
1345dbc8283eSBarry Smith       ierr = MPI_Attr_get(icomm,Petsc_Counter_keyval,&counter,&flg);CHKERRQ(ierr);
1346dbc8283eSBarry Smith       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory");
1347dbc8283eSBarry Smith 
1348dbc8283eSBarry Smith       ierr = MPI_Attr_delete(PETSC_COMM_SELF,Petsc_InnerComm_keyval);CHKERRQ(ierr);
1349efb80d3cSBarry Smith       ierr = MPI_Attr_delete(icomm,Petsc_Counter_keyval);CHKERRQ(ierr);
1350efb80d3cSBarry Smith       ierr = MPI_Comm_free(&icomm);CHKERRQ(ierr);
1351dbc8283eSBarry Smith     }
1352265f3f35SJed Brown     ierr = MPI_Attr_get(PETSC_COMM_WORLD,Petsc_InnerComm_keyval,&ucomm,&flg);CHKERRQ(ierr);
1353dbc8283eSBarry Smith     if (flg) {
1354265f3f35SJed Brown       icomm = ucomm.comm;
1355dbc8283eSBarry Smith       ierr = MPI_Attr_get(icomm,Petsc_Counter_keyval,&counter,&flg);CHKERRQ(ierr);
1356dbc8283eSBarry Smith       if (!flg) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory");
1357dbc8283eSBarry Smith 
1358dbc8283eSBarry Smith       ierr = MPI_Attr_delete(PETSC_COMM_WORLD,Petsc_InnerComm_keyval);CHKERRQ(ierr);
1359efb80d3cSBarry Smith       ierr = MPI_Attr_delete(icomm,Petsc_Counter_keyval);CHKERRQ(ierr);
1360efb80d3cSBarry Smith       ierr = MPI_Comm_free(&icomm);CHKERRQ(ierr);
1361dbc8283eSBarry Smith     }
1362b770b1f6SSatish Balay   }
1363dbc8283eSBarry Smith 
1364480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_Counter_keyval);CHKERRQ(ierr);
1365480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_InnerComm_keyval);CHKERRQ(ierr);
1366480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_OuterComm_keyval);CHKERRQ(ierr);
1367480cf27aSJed Brown 
1368e5c89e4eSSatish Balay   if (PetscBeganMPI) {
136999608316SBarry Smith #if defined(PETSC_HAVE_MPI_FINALIZED)
137099b1327fSBarry Smith     PetscMPIInt flag;
137199b1327fSBarry Smith     ierr = MPI_Finalized(&flag);CHKERRQ(ierr);
1372e32f2f54SBarry Smith     if (flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()");
137399608316SBarry Smith #endif
1374e5c89e4eSSatish Balay     ierr = MPI_Finalize();CHKERRQ(ierr);
1375e5c89e4eSSatish Balay   }
1376e5c89e4eSSatish Balay /*
1377e5c89e4eSSatish Balay 
1378e5c89e4eSSatish Balay      Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because
1379e5c89e4eSSatish Balay    the communicator has some outstanding requests on it. Specifically if the
1380e5c89e4eSSatish Balay    flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See
1381e5c89e4eSSatish Balay    src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate()
1382e5c89e4eSSatish Balay    is never freed as it should be. Thus one may obtain messages of the form
13830e5e90baSSatish Balay    [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the
1384e5c89e4eSSatish Balay    memory was not freed.
1385e5c89e4eSSatish Balay 
1386e5c89e4eSSatish Balay */
13871d1a0024SBarry Smith   ierr = PetscMallocClear();CHKERRQ(ierr);
1388a297a907SKarl Rupp 
1389e5c89e4eSSatish Balay   PetscInitializeCalled = PETSC_FALSE;
1390e5c89e4eSSatish Balay   PetscFinalizeCalled   = PETSC_TRUE;
1391e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
1392e5c89e4eSSatish Balay }
1393e5c89e4eSSatish Balay 
139443db4dbbSBarry Smith #if defined(PETSC_MISSING_LAPACK_lsame_)
13958cc058d9SJed Brown PETSC_EXTERN int lsame_(char *a,char *b)
139643db4dbbSBarry Smith {
139743db4dbbSBarry Smith   if (*a == *b) return 1;
139843db4dbbSBarry Smith   if (*a + 32 == *b) return 1;
139943db4dbbSBarry Smith   if (*a - 32 == *b) return 1;
140043db4dbbSBarry Smith   return 0;
140143db4dbbSBarry Smith }
1402a70650f6SBarry Smith #endif
140343db4dbbSBarry Smith 
140443db4dbbSBarry Smith #if defined(PETSC_MISSING_LAPACK_lsame)
14058cc058d9SJed Brown PETSC_EXTERN int lsame(char *a,char *b)
140643db4dbbSBarry Smith {
140743db4dbbSBarry Smith   if (*a == *b) return 1;
140843db4dbbSBarry Smith   if (*a + 32 == *b) return 1;
140943db4dbbSBarry Smith   if (*a - 32 == *b) return 1;
141043db4dbbSBarry Smith   return 0;
141143db4dbbSBarry Smith }
141243db4dbbSBarry Smith #endif
1413