xref: /petsc/src/sys/objects/pinit.c (revision 11525c0d8e626c522c766a3408f7e8671b0dac87)
17d0a6c19SBarry Smith 
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay    This file defines the initialization of PETSc, including PetscInitialize()
4e5c89e4eSSatish Balay */
550f81f78SJed Brown #define PETSC_DESIRE_COMPLEX
6afcb2eb5SJed Brown #include <petsc-private/petscimpl.h>        /*I  "petscsys.h"   I*/
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
1885afcc9aSBarry Smith extern PetscBool PetscHMPIWorker;
19f2d66bcaSShri Abhyankar 
202d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
212d53ad75SBarry Smith PetscFPT PetscFPTData = 0;
222d53ad75SBarry Smith #endif
232d53ad75SBarry Smith 
24a6790183SBarry Smith #if defined(PETSC_HAVE_SAWS)
2516ad0300SBarry Smith #include <petscviewersaws.h>
26a6790183SBarry Smith #endif
27e5c89e4eSSatish Balay /* -----------------------------------------------------------------------------------------*/
28e5c89e4eSSatish Balay 
29e5c89e4eSSatish Balay extern FILE *petsc_history;
30e5c89e4eSSatish Balay 
3109573ac7SBarry Smith extern PetscErrorCode PetscInitialize_DynamicLibraries(void);
3209573ac7SBarry Smith extern PetscErrorCode PetscFinalize_DynamicLibraries(void);
3337e93019SBarry Smith extern PetscErrorCode PetscFunctionListPrintAll(void);
3409573ac7SBarry Smith extern PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm,int);
3509573ac7SBarry Smith extern PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm,int);
3609573ac7SBarry Smith extern PetscErrorCode PetscCloseHistoryFile(FILE**);
370069ddf5SShri Abhyankar 
38e5c89e4eSSatish Balay /* user may set this BEFORE calling PetscInitialize() */
39e8373e55SMatthew Knepley MPI_Comm PETSC_COMM_WORLD = MPI_COMM_NULL;
40e5c89e4eSSatish Balay 
41480cf27aSJed Brown PetscMPIInt Petsc_Counter_keyval   = MPI_KEYVAL_INVALID;
42480cf27aSJed Brown PetscMPIInt Petsc_InnerComm_keyval = MPI_KEYVAL_INVALID;
43480cf27aSJed Brown PetscMPIInt Petsc_OuterComm_keyval = MPI_KEYVAL_INVALID;
44480cf27aSJed Brown 
45e5c89e4eSSatish Balay /*
46e5c89e4eSSatish Balay      Declare and set all the string names of the PETSc enums
47e5c89e4eSSatish Balay */
486a6fc655SJed Brown const char *const PetscBools[]     = {"FALSE","TRUE","PetscBool","PETSC_",0};
496a6fc655SJed Brown const char *const PetscCopyModes[] = {"COPY_VALUES","OWN_POINTER","USE_POINTER","PetscCopyMode","PETSC_",0};
506a6fc655SJed Brown const char *const PetscDataTypes[] = {"INT","DOUBLE","COMPLEX","LONG","SHORT","FLOAT",
512d53ad75SBarry Smith                                       "CHAR","LOGICAL","ENUM","BOOL","LONGDOUBLE","OBJECT","FUNCTION","PetscDataType","PETSC_",0};
52e5c89e4eSSatish Balay 
53ace3abfcSBarry Smith PetscBool PetscPreLoadingUsed = PETSC_FALSE;
54ace3abfcSBarry Smith PetscBool PetscPreLoadingOn   = PETSC_FALSE;
550f8e0872SSatish Balay 
56e5c89e4eSSatish Balay /*
57e5c89e4eSSatish Balay        Checks the options database for initializations related to the
58e5c89e4eSSatish Balay     PETSc components
59e5c89e4eSSatish Balay */
60e5c89e4eSSatish Balay #undef __FUNCT__
61e5c89e4eSSatish Balay #define __FUNCT__ "PetscOptionsCheckInitial_Components"
627087cfbeSBarry Smith PetscErrorCode  PetscOptionsCheckInitial_Components(void)
63e5c89e4eSSatish Balay {
64ace3abfcSBarry Smith   PetscBool      flg1;
65e5c89e4eSSatish Balay   PetscErrorCode ierr;
66e5c89e4eSSatish Balay 
67e5c89e4eSSatish Balay   PetscFunctionBegin;
680298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-help",&flg1);CHKERRQ(ierr);
69e5c89e4eSSatish Balay   if (flg1) {
70e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
71e8e7597cSSatish Balay     MPI_Comm comm = PETSC_COMM_WORLD;
72e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"------Additional PETSc component options--------\n");CHKERRQ(ierr);
73e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -log_summary_exclude: <vec,mat,pc.ksp,snes>\n");CHKERRQ(ierr);
746cf91177SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -info_exclude: <null,vec,mat,pc,ksp,snes,ts>\n");CHKERRQ(ierr);
75e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");CHKERRQ(ierr);
76e5c89e4eSSatish Balay #endif
77e5c89e4eSSatish Balay   }
78e5c89e4eSSatish Balay   PetscFunctionReturn(0);
79e5c89e4eSSatish Balay }
80e5c89e4eSSatish Balay 
81e5c89e4eSSatish Balay #undef __FUNCT__
82945d1669SBarry Smith #define __FUNCT__ "PetscInitializeNoPointers"
830f11a792SBarry Smith /*
84945d1669SBarry Smith       PetscInitializeNoPointers - Calls PetscInitialize() from C/C++ without the pointers to argc and args
8572a42c3cSBarry Smith 
8672a42c3cSBarry Smith    Collective
8772a42c3cSBarry Smith 
8872a42c3cSBarry Smith    Level: advanced
8972a42c3cSBarry Smith 
90945d1669SBarry Smith     Notes: this is called only by the PETSc MATLAB and Julia interface. Even though it might start MPI it sets the flag to
910f11a792SBarry Smith      indicate that it did NOT start MPI so that the PetscFinalize() does not end MPI, thus allowing PetscInitialize() to
92945d1669SBarry Smith      be called multiple times from MATLAB and Julia without the problem of trying to initialize MPI more than once.
930f11a792SBarry Smith 
941ea5a559SBarry Smith      Turns off PETSc signal handling because that can interact with MATLAB's signal handling causing random crashes.
951ea5a559SBarry Smith 
9672a42c3cSBarry Smith .seealso: PetscInitialize(), PetscInitializeFortran(), PetscInitializeNoArguments()
970f11a792SBarry Smith */
98945d1669SBarry Smith PetscErrorCode  PetscInitializeNoPointers(int argc,char **args,const char *filename,const char *help)
9972a42c3cSBarry Smith {
10072a42c3cSBarry Smith   PetscErrorCode ierr;
10172a42c3cSBarry Smith   int            myargc   = argc;
10272a42c3cSBarry Smith   char           **myargs = args;
10372a42c3cSBarry Smith 
10472a42c3cSBarry Smith   PetscFunctionBegin;
1053bf036e2SBarry Smith   ierr = PetscInitialize(&myargc,&myargs,filename,help);CHKERRQ(ierr);
1061ea5a559SBarry Smith   ierr = PetscPopSignalHandler();CHKERRQ(ierr);
107df413903SBarry Smith   PetscBeganMPI = PETSC_FALSE;
10872a42c3cSBarry Smith   PetscFunctionReturn(ierr);
10972a42c3cSBarry Smith }
11072a42c3cSBarry Smith 
11172a42c3cSBarry Smith #undef __FUNCT__
112945d1669SBarry Smith #define __FUNCT__ "PetscGetPETSC_COMM_SELF"
113f0865b08SBarry Smith /*
114945d1669SBarry Smith       Used by MATLAB and Julia interface to get communicator
115f0865b08SBarry Smith */
116945d1669SBarry Smith PetscErrorCode  PetscGetPETSC_COMM_SELF(MPI_Comm *comm)
117f0865b08SBarry Smith {
118f0865b08SBarry Smith   PetscFunctionBegin;
119f0865b08SBarry Smith   *comm = PETSC_COMM_SELF;
120f0865b08SBarry Smith   PetscFunctionReturn(0);
121f0865b08SBarry Smith }
122f0865b08SBarry Smith 
123f0865b08SBarry Smith #undef __FUNCT__
124e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitializeNoArguments"
125e5c89e4eSSatish Balay /*@C
126e5c89e4eSSatish Balay       PetscInitializeNoArguments - Calls PetscInitialize() from C/C++ without
127e5c89e4eSSatish Balay         the command line arguments.
128e5c89e4eSSatish Balay 
129e5c89e4eSSatish Balay    Collective
130e5c89e4eSSatish Balay 
131e5c89e4eSSatish Balay    Level: advanced
132e5c89e4eSSatish Balay 
133e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeFortran()
134e5c89e4eSSatish Balay @*/
1357087cfbeSBarry Smith PetscErrorCode  PetscInitializeNoArguments(void)
136e5c89e4eSSatish Balay {
137e5c89e4eSSatish Balay   PetscErrorCode ierr;
138e5c89e4eSSatish Balay   int            argc   = 0;
139e5c89e4eSSatish Balay   char           **args = 0;
140e5c89e4eSSatish Balay 
141e5c89e4eSSatish Balay   PetscFunctionBegin;
1420298fd71SBarry Smith   ierr = PetscInitialize(&argc,&args,NULL,NULL);
143e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
144e5c89e4eSSatish Balay }
145e5c89e4eSSatish Balay 
146e5c89e4eSSatish Balay #undef __FUNCT__
147e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialized"
148e5c89e4eSSatish Balay /*@
149e5c89e4eSSatish Balay       PetscInitialized - Determine whether PETSc is initialized.
150e5c89e4eSSatish Balay 
15193b6d2d1SJed Brown    Level: beginner
152e5c89e4eSSatish Balay 
153e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
154e5c89e4eSSatish Balay @*/
1557087cfbeSBarry Smith PetscErrorCode PetscInitialized(PetscBool  *isInitialized)
156e5c89e4eSSatish Balay {
157e5c89e4eSSatish Balay   *isInitialized = PetscInitializeCalled;
15893b6d2d1SJed Brown   return 0;
159e5c89e4eSSatish Balay }
160e5c89e4eSSatish Balay 
161e5c89e4eSSatish Balay #undef __FUNCT__
162e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalized"
163e5c89e4eSSatish Balay /*@
164e5c89e4eSSatish Balay       PetscFinalized - Determine whether PetscFinalize() has been called yet
165e5c89e4eSSatish Balay 
166e5c89e4eSSatish Balay    Level: developer
167e5c89e4eSSatish Balay 
168e5c89e4eSSatish Balay .seealso: PetscInitialize(), PetscInitializeNoArguments(), PetscInitializeFortran()
169e5c89e4eSSatish Balay @*/
1707087cfbeSBarry Smith PetscErrorCode  PetscFinalized(PetscBool  *isFinalized)
171e5c89e4eSSatish Balay {
172e5c89e4eSSatish Balay   *isFinalized = PetscFinalizeCalled;
17393b6d2d1SJed Brown   return 0;
174e5c89e4eSSatish Balay }
175e5c89e4eSSatish Balay 
17609573ac7SBarry Smith extern PetscErrorCode PetscOptionsCheckInitial_Private(void);
177e5c89e4eSSatish Balay 
178e5c89e4eSSatish Balay /*
179e5c89e4eSSatish Balay        This function is the MPI reduction operation used to compute the sum of the
180e5c89e4eSSatish Balay    first half of the datatype and the max of the second half.
181e5c89e4eSSatish Balay */
182e5c89e4eSSatish Balay MPI_Op PetscMaxSum_Op = 0;
183e5c89e4eSSatish Balay 
184e5c89e4eSSatish Balay #undef __FUNCT__
185e5c89e4eSSatish Balay #define __FUNCT__ "PetscMaxSum_Local"
1868cc058d9SJed Brown PETSC_EXTERN void MPIAPI PetscMaxSum_Local(void *in,void *out,int *cnt,MPI_Datatype *datatype)
187e5c89e4eSSatish Balay {
188e5c89e4eSSatish Balay   PetscInt *xin = (PetscInt*)in,*xout = (PetscInt*)out,i,count = *cnt;
189e5c89e4eSSatish Balay 
190e5c89e4eSSatish Balay   PetscFunctionBegin;
191e5c89e4eSSatish Balay   if (*datatype != MPIU_2INT) {
192e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2INT data types");
193e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
194e5c89e4eSSatish Balay   }
195e5c89e4eSSatish Balay 
196e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
197e5c89e4eSSatish Balay     xout[2*i]    = PetscMax(xout[2*i],xin[2*i]);
198e5c89e4eSSatish Balay     xout[2*i+1] += xin[2*i+1];
199e5c89e4eSSatish Balay   }
200812af9f3SBarry Smith   PetscFunctionReturnVoid();
201e5c89e4eSSatish Balay }
202e5c89e4eSSatish Balay 
203e5c89e4eSSatish Balay /*
204e5c89e4eSSatish Balay     Returns the max of the first entry owned by this processor and the
205e5c89e4eSSatish Balay sum of the second entry.
206b693b147SBarry Smith 
207b693b147SBarry Smith     The reason nprocs[2*i] contains lengths nprocs[2*i+1] contains flag of 1 if length is nonzero
208b693b147SBarry Smith is so that the PetscMaxSum_Op() can set TWO values, if we passed in only nprocs[i] with lengths
209b693b147SBarry Smith there would be no place to store the both needed results.
210e5c89e4eSSatish Balay */
211e5c89e4eSSatish Balay #undef __FUNCT__
212e5c89e4eSSatish Balay #define __FUNCT__ "PetscMaxSum"
2137087cfbeSBarry Smith PetscErrorCode  PetscMaxSum(MPI_Comm comm,const PetscInt nprocs[],PetscInt *max,PetscInt *sum)
214e5c89e4eSSatish Balay {
215e5c89e4eSSatish Balay   PetscMPIInt    size,rank;
2166ac3741eSJed Brown   struct {PetscInt max,sum;} *work;
217e5c89e4eSSatish Balay   PetscErrorCode ierr;
218e5c89e4eSSatish Balay 
219e5c89e4eSSatish Balay   PetscFunctionBegin;
220e5c89e4eSSatish Balay   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
221e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
2226ac3741eSJed Brown   ierr = PetscMalloc(size*sizeof(*work),&work);CHKERRQ(ierr);
223e5c89e4eSSatish Balay   ierr = MPI_Allreduce((void*)nprocs,work,size,MPIU_2INT,PetscMaxSum_Op,comm);CHKERRQ(ierr);
2246ac3741eSJed Brown   *max = work[rank].max;
2256ac3741eSJed Brown   *sum = work[rank].sum;
226e5c89e4eSSatish Balay   ierr = PetscFree(work);CHKERRQ(ierr);
227e5c89e4eSSatish Balay   PetscFunctionReturn(0);
228e5c89e4eSSatish Balay }
229e5c89e4eSSatish Balay 
230e5c89e4eSSatish Balay /* ----------------------------------------------------------------------------*/
2317087cfbeSBarry Smith MPI_Op  PetscADMax_Op = 0;
232e5c89e4eSSatish Balay 
233e5c89e4eSSatish Balay #undef __FUNCT__
234e5c89e4eSSatish Balay #define __FUNCT__ "PetscADMax_Local"
2358cc058d9SJed Brown PETSC_EXTERN void MPIAPI PetscADMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
236e5c89e4eSSatish Balay {
237e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar*)in,*xout = (PetscScalar*)out;
238e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
239e5c89e4eSSatish Balay 
240e5c89e4eSSatish Balay   PetscFunctionBegin;
241e5c89e4eSSatish Balay   if (*datatype != MPIU_2SCALAR) {
242e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
243e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
244e5c89e4eSSatish Balay   }
245e5c89e4eSSatish Balay 
246e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
247e5c89e4eSSatish Balay     if (PetscRealPart(xout[2*i]) < PetscRealPart(xin[2*i])) {
248e5c89e4eSSatish Balay       xout[2*i]   = xin[2*i];
249e5c89e4eSSatish Balay       xout[2*i+1] = xin[2*i+1];
250e5c89e4eSSatish Balay     }
251e5c89e4eSSatish Balay   }
252812af9f3SBarry Smith   PetscFunctionReturnVoid();
253e5c89e4eSSatish Balay }
254e5c89e4eSSatish Balay 
2557087cfbeSBarry Smith MPI_Op PetscADMin_Op = 0;
256e5c89e4eSSatish Balay 
257e5c89e4eSSatish Balay #undef __FUNCT__
258e5c89e4eSSatish Balay #define __FUNCT__ "PetscADMin_Local"
2598cc058d9SJed Brown PETSC_EXTERN void MPIAPI PetscADMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
260e5c89e4eSSatish Balay {
261e5c89e4eSSatish Balay   PetscScalar *xin = (PetscScalar*)in,*xout = (PetscScalar*)out;
262e5c89e4eSSatish Balay   PetscInt    i,count = *cnt;
263e5c89e4eSSatish Balay 
264e5c89e4eSSatish Balay   PetscFunctionBegin;
265e5c89e4eSSatish Balay   if (*datatype != MPIU_2SCALAR) {
266e5c89e4eSSatish Balay     (*PetscErrorPrintf)("Can only handle MPIU_2SCALAR data (i.e. double or complex) types");
267e5c89e4eSSatish Balay     MPI_Abort(MPI_COMM_WORLD,1);
268e5c89e4eSSatish Balay   }
269e5c89e4eSSatish Balay 
270e5c89e4eSSatish Balay   for (i=0; i<count; i++) {
271e5c89e4eSSatish Balay     if (PetscRealPart(xout[2*i]) > PetscRealPart(xin[2*i])) {
272e5c89e4eSSatish Balay       xout[2*i]   = xin[2*i];
273e5c89e4eSSatish Balay       xout[2*i+1] = xin[2*i+1];
274e5c89e4eSSatish Balay     }
275e5c89e4eSSatish Balay   }
276812af9f3SBarry Smith   PetscFunctionReturnVoid();
277e5c89e4eSSatish Balay }
278e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
279e5c89e4eSSatish Balay 
2807c2de775SJed Brown #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128)
28106a205a8SBarry Smith MPI_Op MPIU_SUM = 0;
282e5c89e4eSSatish Balay 
283e5c89e4eSSatish Balay #undef __FUNCT__
284e5c89e4eSSatish Balay #define __FUNCT__ "PetscSum_Local"
2858cc058d9SJed Brown PETSC_EXTERN void PetscSum_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
286e5c89e4eSSatish Balay {
287e5c89e4eSSatish Balay   PetscInt i,count = *cnt;
288e5c89e4eSSatish Balay 
289e5c89e4eSSatish Balay   PetscFunctionBegin;
2907c2de775SJed Brown   if (*datatype == MPIU_REAL) {
291e2e03761SBarry Smith     PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
292a297a907SKarl Rupp     for (i=0; i<count; i++) xout[i] += xin[i];
2937c2de775SJed Brown   }
2947c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
2957c2de775SJed Brown   else if (*datatype == MPIU_COMPLEX) {
2967c2de775SJed Brown     PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
297a297a907SKarl Rupp     for (i=0; i<count; i++) xout[i] += xin[i];
2987c2de775SJed Brown   }
2997c2de775SJed Brown #endif
3007c2de775SJed Brown   else {
3017c2de775SJed Brown     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types");
302e2e03761SBarry Smith     MPI_Abort(MPI_COMM_WORLD,1);
303e2e03761SBarry Smith   }
304812af9f3SBarry Smith   PetscFunctionReturnVoid();
305e5c89e4eSSatish Balay }
306e5c89e4eSSatish Balay #endif
307e5c89e4eSSatish Balay 
308ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
309d9822059SBarry Smith MPI_Op MPIU_MAX = 0;
310d9822059SBarry Smith MPI_Op MPIU_MIN = 0;
311d9822059SBarry Smith 
312d9822059SBarry Smith #undef __FUNCT__
313d9822059SBarry Smith #define __FUNCT__ "PetscMax_Local"
3148cc058d9SJed Brown PETSC_EXTERN void PetscMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
315d9822059SBarry Smith {
316d9822059SBarry Smith   PetscInt i,count = *cnt;
317d9822059SBarry Smith 
318d9822059SBarry Smith   PetscFunctionBegin;
3197c2de775SJed Brown   if (*datatype == MPIU_REAL) {
3208c764dc5SJose Roman     PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
321a297a907SKarl Rupp     for (i=0; i<count; i++) xout[i] = PetscMax(xout[i],xin[i]);
3227c2de775SJed Brown   }
3237c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
3247c2de775SJed Brown   else if (*datatype == MPIU_COMPLEX) {
3257c2de775SJed Brown     PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
3267c2de775SJed Brown     for (i=0; i<count; i++) {
3277c2de775SJed Brown       xout[i] = PetscRealPartComplex(xout[i])<PetscRealPartComplex(xin[i]) ? xin[i] : xout[i];
3287c2de775SJed Brown     }
3297c2de775SJed Brown   }
3307c2de775SJed Brown #endif
3317c2de775SJed Brown   else {
3327c2de775SJed Brown     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types");
3338c764dc5SJose Roman     MPI_Abort(MPI_COMM_WORLD,1);
3348c764dc5SJose Roman   }
335d9822059SBarry Smith   PetscFunctionReturnVoid();
336d9822059SBarry Smith }
337d9822059SBarry Smith 
338d9822059SBarry Smith #undef __FUNCT__
339d9822059SBarry Smith #define __FUNCT__ "PetscMin_Local"
3408cc058d9SJed Brown PETSC_EXTERN void PetscMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
341d9822059SBarry Smith {
342d9822059SBarry Smith   PetscInt    i,count = *cnt;
343d9822059SBarry Smith 
344d9822059SBarry Smith   PetscFunctionBegin;
3457c2de775SJed Brown   if (*datatype == MPIU_REAL) {
3468c764dc5SJose Roman     PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out;
347a297a907SKarl Rupp     for (i=0; i<count; i++) xout[i] = PetscMin(xout[i],xin[i]);
3487c2de775SJed Brown   }
3497c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
3507c2de775SJed Brown   else if (*datatype == MPIU_COMPLEX) {
3517c2de775SJed Brown     PetscComplex *xin = (PetscComplex*)in,*xout = (PetscComplex*)out;
3527c2de775SJed Brown     for (i=0; i<count; i++) {
3537c2de775SJed Brown       xout[i] = PetscRealPartComplex(xout[i])>PetscRealPartComplex(xin[i]) ? xin[i] : xout[i];
3547c2de775SJed Brown     }
3557c2de775SJed Brown   }
3567c2de775SJed Brown #endif
3577c2de775SJed Brown   else {
3588c764dc5SJose Roman     (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_SCALAR data (i.e. double or complex) types");
3598c764dc5SJose Roman     MPI_Abort(MPI_COMM_WORLD,1);
3608c764dc5SJose Roman   }
361d9822059SBarry Smith   PetscFunctionReturnVoid();
362d9822059SBarry Smith }
363d9822059SBarry Smith #endif
364d9822059SBarry Smith 
365480cf27aSJed Brown #undef __FUNCT__
366480cf27aSJed Brown #define __FUNCT__ "Petsc_DelCounter"
367480cf27aSJed Brown /*
368480cf27aSJed Brown    Private routine to delete internal tag/name counter storage when a communicator is freed.
369480cf27aSJed Brown 
370ff0e51ddSBarry 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.
371480cf27aSJed Brown 
372480cf27aSJed Brown    Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
373480cf27aSJed Brown 
374480cf27aSJed Brown */
3758cc058d9SJed Brown PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelCounter(MPI_Comm comm,PetscMPIInt keyval,void *count_val,void *extra_state)
376480cf27aSJed Brown {
377480cf27aSJed Brown   PetscErrorCode ierr;
378480cf27aSJed Brown 
379480cf27aSJed Brown   PetscFunctionBegin;
380480cf27aSJed Brown   ierr = PetscInfo1(0,"Deleting counter data in an MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
381480cf27aSJed Brown   ierr = PetscFree(count_val);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
382480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
383480cf27aSJed Brown }
384480cf27aSJed Brown 
385480cf27aSJed Brown #undef __FUNCT__
386da3039f7SJed Brown #define __FUNCT__ "Petsc_DelComm_Outer"
387480cf27aSJed Brown /*
388da3039f7SJed Brown   This is invoked on the outer comm as a result of either PetscCommDestroy() (via MPI_Attr_delete) or when the user
389da3039f7SJed Brown   calls MPI_Comm_free().
390da3039f7SJed Brown 
391da3039f7SJed Brown   This is the only entry point for breaking the links between inner and outer comms.
392480cf27aSJed Brown 
393ff0e51ddSBarry Smith   This is called by MPI, not by users. This is called when MPI_Comm_free() is called on the communicator.
394480cf27aSJed Brown 
395480cf27aSJed Brown   Note: this is declared extern "C" because it is passed to MPI_Keyval_create()
396480cf27aSJed Brown 
397480cf27aSJed Brown */
398da3039f7SJed Brown PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelComm_Outer(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
399480cf27aSJed Brown {
400480cf27aSJed Brown   PetscErrorCode ierr;
401b89831e5SBarry Smith   PetscMPIInt    flg;
402265f3f35SJed Brown   union {MPI_Comm comm; void *ptr;} icomm,ocomm;
403480cf27aSJed Brown 
404480cf27aSJed Brown   PetscFunctionBegin;
405da3039f7SJed Brown   if (keyval != Petsc_InnerComm_keyval) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Unexpected keyval");
406ec4fadc2SJed Brown   icomm.ptr = attr_val;
407da3039f7SJed Brown 
408265f3f35SJed Brown   ierr = MPI_Attr_get(icomm.comm,Petsc_OuterComm_keyval,&ocomm,&flg);CHKERRQ(ierr);
409b3ef52cdSBarry Smith   if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm does not have expected reference to outer comm");
410da3039f7SJed Brown   if (ocomm.comm != comm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_CORRUPT,"Inner MPI_Comm has reference to non-matching outer comm");
411da3039f7SJed Brown   ierr = MPI_Attr_delete(icomm.comm,Petsc_OuterComm_keyval);CHKERRQ(ierr); /* Calls Petsc_DelComm_Inner */
412da3039f7SJed 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);
413da3039f7SJed Brown   PetscFunctionReturn(MPI_SUCCESS);
414b89831e5SBarry Smith }
415da3039f7SJed Brown 
416da3039f7SJed Brown #undef __FUNCT__
417da3039f7SJed Brown #define __FUNCT__ "Petsc_DelComm_Inner"
418da3039f7SJed Brown /*
419da3039f7SJed Brown  * This is invoked on the inner comm when Petsc_DelComm_Outer calls MPI_Attr_delete.  It should not be reached any other way.
420da3039f7SJed Brown  */
421da3039f7SJed Brown PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelComm_Inner(MPI_Comm comm,PetscMPIInt keyval,void *attr_val,void *extra_state)
422da3039f7SJed Brown {
423da3039f7SJed Brown   PetscErrorCode ierr;
424da3039f7SJed Brown 
425da3039f7SJed Brown   PetscFunctionBegin;
426da3039f7SJed Brown   ierr = PetscInfo1(0,"Removing reference to PETSc communicator embedded in a user MPI_Comm %ld\n",(long)comm);if (ierr) PetscFunctionReturn((PetscMPIInt)ierr);
427480cf27aSJed Brown   PetscFunctionReturn(MPI_SUCCESS);
428480cf27aSJed Brown }
429480cf27aSJed Brown 
430951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
431e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
4328cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype,MPI_Aint*,void*);
4338cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_read_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
4348cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_write_conv_fn(void*, MPI_Datatype,PetscMPIInt,void*,MPI_Offset,void*);
435e39fd77fSBarry Smith #endif
436951e3c8eSBarry Smith #endif
437e39fd77fSBarry Smith 
4386ae9a8a6SBarry Smith int  PetscGlobalArgc   = 0;
4396ae9a8a6SBarry Smith char **PetscGlobalArgs = 0;
440dff31646SBarry Smith PetscSegBuffer PetscCitationsList;
441e5c89e4eSSatish Balay 
442e5c89e4eSSatish Balay #undef __FUNCT__
443051e4cf2SJed Brown #define __FUNCT__ "PetscCitationsInitialize"
444051e4cf2SJed Brown PetscErrorCode PetscCitationsInitialize()
445051e4cf2SJed Brown {
446051e4cf2SJed Brown   PetscErrorCode ierr;
447051e4cf2SJed Brown 
448051e4cf2SJed Brown   PetscFunctionBegin;
449051e4cf2SJed Brown   ierr = PetscSegBufferCreate(1,10000,&PetscCitationsList);CHKERRQ(ierr);
450051e4cf2SJed Brown   ierr = PetscCitationsRegister("@TechReport{petsc-user-ref,\n  Author = {Satish Balay and Jed Brown and and Kris Buschelman and Victor Eijkhout\n            and William D.  Gropp and Dinesh Kaushik and Matthew G. Knepley\n            and Lois Curfman McInnes and Barry F. Smith and Hong Zhang},\n  Title = {{PETS}c Users Manual},\n  Number = {ANL-95/11 - Revision 3.4},\n  Institution = {Argonne National Laboratory},\n  Year = {2013}\n}\n",NULL);CHKERRQ(ierr);
451051e4cf2SJed 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);
452051e4cf2SJed Brown   PetscFunctionReturn(0);
453051e4cf2SJed Brown }
454e5c89e4eSSatish Balay 
455e5c89e4eSSatish Balay #undef __FUNCT__
456e5c89e4eSSatish Balay #define __FUNCT__ "PetscGetArgs"
457e5c89e4eSSatish Balay /*@C
458e5c89e4eSSatish Balay    PetscGetArgs - Allows you to access the raw command line arguments anywhere
459e5c89e4eSSatish Balay      after PetscInitialize() is called but before PetscFinalize().
460e5c89e4eSSatish Balay 
461e5c89e4eSSatish Balay    Not Collective
462e5c89e4eSSatish Balay 
463e5c89e4eSSatish Balay    Output Parameters:
464e5c89e4eSSatish Balay +  argc - count of number of command line arguments
465e5c89e4eSSatish Balay -  args - the command line arguments
466e5c89e4eSSatish Balay 
467e5c89e4eSSatish Balay    Level: intermediate
468e5c89e4eSSatish Balay 
469e5c89e4eSSatish Balay    Notes:
470e5c89e4eSSatish Balay       This is usually used to pass the command line arguments into other libraries
471e5c89e4eSSatish Balay    that are called internally deep in PETSc or the application.
472e5c89e4eSSatish Balay 
473f177e3b1SBarry Smith       The first argument contains the program name as is normal for C arguments.
474f177e3b1SBarry Smith 
475e5c89e4eSSatish Balay    Concepts: command line arguments
476e5c89e4eSSatish Balay 
477793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArguments()
478e5c89e4eSSatish Balay 
479e5c89e4eSSatish Balay @*/
4807087cfbeSBarry Smith PetscErrorCode  PetscGetArgs(int *argc,char ***args)
481e5c89e4eSSatish Balay {
482e5c89e4eSSatish Balay   PetscFunctionBegin;
48317186662SBarry Smith   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
484e5c89e4eSSatish Balay   *argc = PetscGlobalArgc;
485e5c89e4eSSatish Balay   *args = PetscGlobalArgs;
486e5c89e4eSSatish Balay   PetscFunctionReturn(0);
487e5c89e4eSSatish Balay }
488e5c89e4eSSatish Balay 
489e5c89e4eSSatish Balay #undef __FUNCT__
490793721a6SBarry Smith #define __FUNCT__ "PetscGetArguments"
491793721a6SBarry Smith /*@C
492793721a6SBarry Smith    PetscGetArguments - Allows you to access the  command line arguments anywhere
493793721a6SBarry Smith      after PetscInitialize() is called but before PetscFinalize().
494793721a6SBarry Smith 
495793721a6SBarry Smith    Not Collective
496793721a6SBarry Smith 
497793721a6SBarry Smith    Output Parameters:
498793721a6SBarry Smith .  args - the command line arguments
499793721a6SBarry Smith 
500793721a6SBarry Smith    Level: intermediate
501793721a6SBarry Smith 
502793721a6SBarry Smith    Notes:
503793721a6SBarry Smith       This does NOT start with the program name and IS null terminated (final arg is void)
504793721a6SBarry Smith 
505793721a6SBarry Smith    Concepts: command line arguments
506793721a6SBarry Smith 
507793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscFreeArguments()
508793721a6SBarry Smith 
509793721a6SBarry Smith @*/
5107087cfbeSBarry Smith PetscErrorCode  PetscGetArguments(char ***args)
511793721a6SBarry Smith {
512793721a6SBarry Smith   PetscInt       i,argc = PetscGlobalArgc;
513793721a6SBarry Smith   PetscErrorCode ierr;
514793721a6SBarry Smith 
515793721a6SBarry Smith   PetscFunctionBegin;
51617186662SBarry Smith   if (!PetscInitializeCalled && PetscFinalizeCalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"You must call after PetscInitialize() but before PetscFinalize()");
517717030eeSLisandro Dalcin   if (!argc) {*args = 0; PetscFunctionReturn(0);}
518793721a6SBarry Smith   ierr = PetscMalloc(argc*sizeof(char*),args);CHKERRQ(ierr);
519793721a6SBarry Smith   for (i=0; i<argc-1; i++) {
520793721a6SBarry Smith     ierr = PetscStrallocpy(PetscGlobalArgs[i+1],&(*args)[i]);CHKERRQ(ierr);
521793721a6SBarry Smith   }
522793721a6SBarry Smith   (*args)[argc-1] = 0;
523793721a6SBarry Smith   PetscFunctionReturn(0);
524793721a6SBarry Smith }
525793721a6SBarry Smith 
526793721a6SBarry Smith #undef __FUNCT__
527793721a6SBarry Smith #define __FUNCT__ "PetscFreeArguments"
528793721a6SBarry Smith /*@C
529793721a6SBarry Smith    PetscFreeArguments - Frees the memory obtained with PetscGetArguments()
530793721a6SBarry Smith 
531793721a6SBarry Smith    Not Collective
532793721a6SBarry Smith 
533793721a6SBarry Smith    Output Parameters:
534793721a6SBarry Smith .  args - the command line arguments
535793721a6SBarry Smith 
536793721a6SBarry Smith    Level: intermediate
537793721a6SBarry Smith 
538793721a6SBarry Smith    Concepts: command line arguments
539793721a6SBarry Smith 
540793721a6SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscGetArguments()
541793721a6SBarry Smith 
542793721a6SBarry Smith @*/
5437087cfbeSBarry Smith PetscErrorCode  PetscFreeArguments(char **args)
544793721a6SBarry Smith {
545793721a6SBarry Smith   PetscInt       i = 0;
546793721a6SBarry Smith   PetscErrorCode ierr;
547793721a6SBarry Smith 
548793721a6SBarry Smith   PetscFunctionBegin;
549a297a907SKarl Rupp   if (!args) PetscFunctionReturn(0);
550793721a6SBarry Smith   while (args[i]) {
551793721a6SBarry Smith     ierr = PetscFree(args[i]);CHKERRQ(ierr);
552793721a6SBarry Smith     i++;
553793721a6SBarry Smith   }
554793721a6SBarry Smith   ierr = PetscFree(args);CHKERRQ(ierr);
555793721a6SBarry Smith   PetscFunctionReturn(0);
556793721a6SBarry Smith }
557793721a6SBarry Smith 
558*11525c0dSBarry Smith #if defined(PETSC_HAVE_SAWS)
559*11525c0dSBarry Smith #undef __FUNCT__
560*11525c0dSBarry Smith #define __FUNCT__ "PetscInitializeSAWs"
561*11525c0dSBarry Smith PetscErrorCode  PetscInitializeSAWs(const char help[])
562*11525c0dSBarry Smith {
563*11525c0dSBarry Smith   if (!PetscGlobalRank) {
564*11525c0dSBarry Smith     char           cert[PETSC_MAX_PATH_LEN],root[PETSC_MAX_PATH_LEN],*intro,programname[64],*appline;
565*11525c0dSBarry Smith     int            port;
566*11525c0dSBarry Smith     PetscBool      flg,rootlocal = PETSC_FALSE,flg2;
567*11525c0dSBarry Smith     size_t         applinelen,introlen;
568*11525c0dSBarry Smith     PetscErrorCode ierr;
569*11525c0dSBarry Smith 
570*11525c0dSBarry Smith     ierr = PetscOptionsHasName(NULL,"-saws_log",&flg);CHKERRQ(ierr);
571*11525c0dSBarry Smith     if (flg) {
572*11525c0dSBarry Smith       char  sawslog[PETSC_MAX_PATH_LEN];
573*11525c0dSBarry Smith 
574*11525c0dSBarry Smith       ierr = PetscOptionsGetString(NULL,"-saws_log",sawslog,PETSC_MAX_PATH_LEN,NULL);CHKERRQ(ierr);
575*11525c0dSBarry Smith       if (sawslog[0]) {
576*11525c0dSBarry Smith         PetscStackCallSAWs(SAWs_Set_Use_Logfile,(sawslog));
577*11525c0dSBarry Smith       } else {
578*11525c0dSBarry Smith         PetscStackCallSAWs(SAWs_Set_Use_Logfile,(NULL));
579*11525c0dSBarry Smith       }
580*11525c0dSBarry Smith     }
581*11525c0dSBarry Smith     ierr = PetscOptionsGetString(NULL,"-saws_https",cert,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
582*11525c0dSBarry Smith     if (flg) {
583*11525c0dSBarry Smith       PetscStackCallSAWs(SAWs_Set_Use_HTTPS,(cert));
584*11525c0dSBarry Smith     }
585*11525c0dSBarry Smith     ierr = PetscOptionsGetInt(NULL,"-saws_port",&port,&flg);CHKERRQ(ierr);
586*11525c0dSBarry Smith     if (flg) {
587*11525c0dSBarry Smith       PetscStackCallSAWs(SAWs_Set_Port,(port));
588*11525c0dSBarry Smith     }
589*11525c0dSBarry Smith     ierr = PetscOptionsGetString(NULL,"-saws_root",root,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr);
590*11525c0dSBarry Smith     if (flg) {
591*11525c0dSBarry Smith       PetscStackCallSAWs(SAWs_Set_Document_Root,(root));CHKERRQ(ierr);
592*11525c0dSBarry Smith       ierr = PetscStrcmp(root,".",&rootlocal);CHKERRQ(ierr);
593*11525c0dSBarry Smith     }
594*11525c0dSBarry Smith     ierr = PetscOptionsHasName(NULL,"-saws_local",&flg2);CHKERRQ(ierr);
595*11525c0dSBarry Smith     if (flg2) {
596*11525c0dSBarry Smith       char jsdir[PETSC_MAX_PATH_LEN];
597*11525c0dSBarry Smith       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"-saws_local option requires -saws_root option");
598*11525c0dSBarry Smith       ierr = PetscSNPrintf(jsdir,PETSC_MAX_PATH_LEN,"%s/js",root);CHKERRQ(ierr);
599*11525c0dSBarry Smith       ierr = PetscTestDirectory(jsdir,'r',&flg);CHKERRQ(ierr);
600*11525c0dSBarry Smith       if (!flg) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"-saws_local option requires js directory in root directory");
601*11525c0dSBarry Smith       PetscStackCallSAWs(SAWs_Set_Local_JSHeader,());CHKERRQ(ierr);
602*11525c0dSBarry Smith     }
603*11525c0dSBarry Smith     ierr = PetscGetProgramName(programname,64);CHKERRQ(ierr);
604*11525c0dSBarry Smith     ierr = PetscStrlen(help,&applinelen);CHKERRQ(ierr);
605*11525c0dSBarry Smith     introlen   = 4096 + applinelen;
606*11525c0dSBarry Smith     applinelen += 256;
607*11525c0dSBarry Smith     ierr = PetscMalloc(applinelen,&appline);CHKERRQ(ierr);
608*11525c0dSBarry Smith     ierr = PetscMalloc(introlen,&intro);CHKERRQ(ierr);
609*11525c0dSBarry Smith 
610*11525c0dSBarry Smith     if (rootlocal) {
611*11525c0dSBarry Smith       ierr = PetscSNPrintf(appline,applinelen,"%s.c.html",programname);CHKERRQ(ierr);
612*11525c0dSBarry Smith       ierr = PetscTestFile(appline,'r',&rootlocal);CHKERRQ(ierr);
613*11525c0dSBarry Smith     }
614*11525c0dSBarry Smith     if (rootlocal && help) {
615*11525c0dSBarry Smith       ierr = PetscSNPrintf(appline,applinelen,"<center> Running <a href=\"%s.c.html\">%s</a></center><br><pre>%s</pre><br>\n",programname,programname,help);
616*11525c0dSBarry Smith     } else if (help) {
617*11525c0dSBarry Smith       ierr = PetscSNPrintf(appline,applinelen,"<center>Running %s </center><br><pre>%s</pre><br>\n",programname,help);
618*11525c0dSBarry Smith     } else {
619*11525c0dSBarry Smith       ierr = PetscSNPrintf(appline,applinelen,"<center> Running %s</center><br>\n",programname);
620*11525c0dSBarry Smith     }
621*11525c0dSBarry Smith 
622*11525c0dSBarry Smith     ierr = PetscSNPrintf(intro,introlen,"<body>\n"
623*11525c0dSBarry 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"
624*11525c0dSBarry Smith                                     "<center>This is the default PETSc application dashboard, from it you can access any published PETSc objects or logging data</center><br>\n"
625*11525c0dSBarry Smith                                     "%s",appline);
626*11525c0dSBarry Smith     PetscStackCallSAWs(SAWs_Set_Body,("index.html",0,intro));
627*11525c0dSBarry Smith     ierr = PetscFree(intro);CHKERRQ(ierr);
628*11525c0dSBarry Smith     ierr = PetscFree(appline);CHKERRQ(ierr);
629*11525c0dSBarry Smith     PetscStackCallSAWs(SAWs_Initialize,());
630*11525c0dSBarry Smith     ierr = PetscCitationsRegister("@TechReport{ saws,"
631*11525c0dSBarry Smith                                   "Author = {Matt Otten and Jed Brown and Barry Smith},"
632*11525c0dSBarry Smith                                   "Title  = {Scientific Application Web Server (SAWs) Users Manual},"
633*11525c0dSBarry Smith                                   "Institution = {Argonne National Laboratory},"
634*11525c0dSBarry Smith                                   "Year   = 2013}",NULL);CHKERRQ(ierr);
635*11525c0dSBarry Smith   }
636*11525c0dSBarry Smith   PetscFunctionReturn(0);
637*11525c0dSBarry Smith }
638*11525c0dSBarry Smith #endif
639*11525c0dSBarry Smith 
640793721a6SBarry Smith #undef __FUNCT__
641e5c89e4eSSatish Balay #define __FUNCT__ "PetscInitialize"
642e5c89e4eSSatish Balay /*@C
643e5c89e4eSSatish Balay    PetscInitialize - Initializes the PETSc database and MPI.
644e5c89e4eSSatish Balay    PetscInitialize() calls MPI_Init() if that has yet to be called,
645e5c89e4eSSatish Balay    so this routine should always be called near the beginning of
646e5c89e4eSSatish Balay    your program -- usually the very first line!
647e5c89e4eSSatish Balay 
648e5c89e4eSSatish Balay    Collective on MPI_COMM_WORLD or PETSC_COMM_WORLD if it has been set
649e5c89e4eSSatish Balay 
650e5c89e4eSSatish Balay    Input Parameters:
651e5c89e4eSSatish Balay +  argc - count of number of command line arguments
652e5c89e4eSSatish Balay .  args - the command line arguments
6530298fd71SBarry Smith .  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use NULL to not check for
654fc2bca9aSBarry Smith           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
6550298fd71SBarry Smith -  help - [optional] Help message to print, use NULL for no message
656e5c89e4eSSatish Balay 
65705827820SBarry Smith    If you wish PETSc code to run ONLY on a subcommunicator of MPI_COMM_WORLD, create that
65805827820SBarry Smith    communicator first and assign it to PETSC_COMM_WORLD BEFORE calling PetscInitialize(). Thus if you are running a
65905827820SBarry Smith    four process job and two processes will run PETSc and have PetscInitialize() and PetscFinalize() and two process will not,
66005827820SBarry Smith    then do this. If ALL processes in the job are using PetscInitialize() and PetscFinalize() then you don't need to do this, even
66105827820SBarry Smith    if different subcommunicators of the job are doing different things with PETSc.
662e5c89e4eSSatish Balay 
663e5c89e4eSSatish Balay    Options Database Keys:
664e5c89e4eSSatish Balay +  -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger
665e5c89e4eSSatish Balay .  -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected
666e5c89e4eSSatish Balay .  -on_error_emacs <machinename> causes emacsclient to jump to error file
667b52f573bSBarry Smith .  -on_error_abort calls abort() when error detected (no traceback)
668e8fb0fc0SBarry Smith .  -on_error_mpiabort calls MPI_abort() when error detected
669e8fb0fc0SBarry Smith .  -error_output_stderr prints error messages to stderr instead of the default stdout
670e8fb0fc0SBarry Smith .  -error_output_none does not print the error messages (but handles errors in the same way as if this was not called)
671e5c89e4eSSatish Balay .  -debugger_nodes [node1,node2,...] - Indicates nodes to start in debugger
672e5c89e4eSSatish Balay .  -debugger_pause [sleeptime] (in seconds) - Pauses debugger
673e5c89e4eSSatish Balay .  -stop_for_debugger - Print message on how to attach debugger manually to
674e5c89e4eSSatish Balay                         process and wait (-debugger_pause) seconds for attachment
6752fb0ec9aSBarry Smith .  -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries)
676e5c89e4eSSatish Balay .  -malloc no - Indicates not to use error-checking malloc
6772fb0ec9aSBarry Smith .  -malloc_debug - check for memory corruption at EVERY malloc or free
678aee23540SBarry Smith .  -malloc_dump - prints a list of all unfreed memory at the end of the run
679dc92acbaSJed Brown .  -malloc_test - like -malloc_dump -malloc_debug, but only active for debugging builds
680e5c89e4eSSatish Balay .  -fp_trap - Stops on floating point exceptions (Note that on the
681e5c89e4eSSatish Balay               IBM RS6000 this slows code by at least a factor of 10.)
682e5c89e4eSSatish Balay .  -no_signal_handler - Indicates not to trap error signals
683e5c89e4eSSatish Balay .  -shared_tmp - indicates /tmp directory is shared by all processors
684e5c89e4eSSatish Balay .  -not_shared_tmp - each processor has own /tmp
685e5c89e4eSSatish Balay .  -tmp - alternative name of /tmp directory
686e5c89e4eSSatish Balay .  -get_total_flops - returns total flops done by all processors
68740ab9619SBarry Smith .  -memory_info - Print memory usage at end of run
68840ab9619SBarry Smith -  -server <port> - start PETSc webserver (default port is 8080)
689e5c89e4eSSatish Balay 
690e5c89e4eSSatish Balay    Options Database Keys for Profiling:
6910598bfebSBarry Smith    See the <a href="../../docs/manual.pdf#nameddest=ch_profiling">profiling chapter of the users manual</a> for details.
692495fc317SBarry Smith +  -info <optional filename> - Prints verbose information to the screen
693495fc317SBarry Smith .  -info_exclude <null,vec,mat,pc,ksp,snes,ts> - Excludes some of the verbose messages
694495fc317SBarry Smith .  -log_sync - Log the synchronization in scatters, inner products and norms
695495fc317SBarry Smith .  -log_trace [filename] - Print traces of all PETSc calls to the screen (useful to determine where a program
696e5c89e4eSSatish Balay         hangs without running in the debugger).  See PetscLogTraceBegin().
697495fc317SBarry Smith .  -log_summary [filename] - Prints summary of flop and timing information to screen. If the filename is specified the
698495fc317SBarry Smith         summary is written to the file.  See PetscLogView().
699495fc317SBarry Smith .  -log_summary_python [filename] - Prints data on of flop and timing usage to a file or screen. See PetscLogPrintSViewPython().
700495fc317SBarry Smith .  -log_all [filename] - Logs extensive profiling information  See PetscLogDump().
701495fc317SBarry Smith .  -log [filename] - Logs basic profiline information  See PetscLogDump().
702495fc317SBarry Smith -  -log_mpe [filename] - Creates a logfile viewable by the utility Jumpshot (in MPICH distribution)
703495fc317SBarry Smith 
704495fc317SBarry Smith     Only one of -log_trace, -log_summary, -log_all, -log, or -log_mpe may be used at a time
705e5c89e4eSSatish Balay 
706e5c89e4eSSatish Balay    Environmental Variables:
707e5c89e4eSSatish Balay +   PETSC_TMP - alternative tmp directory
708e5c89e4eSSatish Balay .   PETSC_SHARED_TMP - tmp is shared by all processes
709e5c89e4eSSatish Balay .   PETSC_NOT_SHARED_TMP - each process has its own private tmp
710e5c89e4eSSatish Balay .   PETSC_VIEWER_SOCKET_PORT - socket number to use for socket viewer
711e5c89e4eSSatish Balay -   PETSC_VIEWER_SOCKET_MACHINE - machine to use for socket viewer to connect to
712e5c89e4eSSatish Balay 
713e5c89e4eSSatish Balay 
714e5c89e4eSSatish Balay    Level: beginner
715e5c89e4eSSatish Balay 
716e5c89e4eSSatish Balay    Notes:
717e5c89e4eSSatish Balay    If for some reason you must call MPI_Init() separately, call
718e5c89e4eSSatish Balay    it before PetscInitialize().
719e5c89e4eSSatish Balay 
720e5c89e4eSSatish Balay    Fortran Version:
721e5c89e4eSSatish Balay    In Fortran this routine has the format
722e5c89e4eSSatish Balay $       call PetscInitialize(file,ierr)
723e5c89e4eSSatish Balay 
724e5c89e4eSSatish Balay +   ierr - error return code
7250298fd71SBarry Smith -  file - [optional] PETSc database file, also checks ~username/.petscrc and .petscrc use NULL_CHARACTER to not check for
726fc2bca9aSBarry Smith           code specific file. Use -skip_petscrc in the code specific file to skip the .petscrc files
727e5c89e4eSSatish Balay 
728e5c89e4eSSatish Balay    Important Fortran Note:
7290298fd71SBarry Smith    In Fortran, you MUST use NULL_CHARACTER to indicate a
7300298fd71SBarry Smith    null character string; you CANNOT just use NULL as
7310598bfebSBarry Smith    in the C version. See the <a href="../../docs/manual.pdf">users manual</a> for details.
732e5c89e4eSSatish Balay 
73301cb0274SBarry Smith    If your main program is C but you call Fortran code that also uses PETSc you need to call PetscInitializeFortran() soon after
73401cb0274SBarry Smith    calling PetscInitialize().
735e5c89e4eSSatish Balay 
736e5c89e4eSSatish Balay    Concepts: initializing PETSc
737e5c89e4eSSatish Balay 
73801cb0274SBarry Smith .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments()
739e5c89e4eSSatish Balay 
740e5c89e4eSSatish Balay @*/
7417087cfbeSBarry Smith PetscErrorCode  PetscInitialize(int *argc,char ***args,const char file[],const char help[])
742e5c89e4eSSatish Balay {
743e5c89e4eSSatish Balay   PetscErrorCode ierr;
7444bb5149bSJed Brown   PetscMPIInt    flag, size;
745aa5bb8c0SSatish Balay   PetscInt       nodesize;
746ace3abfcSBarry Smith   PetscBool      flg;
747e5c89e4eSSatish Balay   char           hostname[256];
748e5c89e4eSSatish Balay 
749e5c89e4eSSatish Balay   PetscFunctionBegin;
750e5c89e4eSSatish Balay   if (PetscInitializeCalled) PetscFunctionReturn(0);
751e5c89e4eSSatish Balay 
752ae9b4142SLisandro Dalcin   /* these must be initialized in a routine, not as a constant declaration*/
753d89683f4Sbcordonn   PETSC_STDOUT = stdout;
754ae9b4142SLisandro Dalcin   PETSC_STDERR = stderr;
755e5c89e4eSSatish Balay 
756e5c89e4eSSatish Balay   ierr = PetscOptionsCreate();CHKERRQ(ierr);
757e5c89e4eSSatish Balay 
758e5c89e4eSSatish Balay   /*
759e5c89e4eSSatish Balay      We initialize the program name here (before MPI_Init()) because MPICH has a bug in
760e5c89e4eSSatish Balay      it that it sets args[0] on all processors to be args[0] on the first processor.
761e5c89e4eSSatish Balay   */
762e5c89e4eSSatish Balay   if (argc && *argc) {
763e5c89e4eSSatish Balay     ierr = PetscSetProgramName(**args);CHKERRQ(ierr);
764e5c89e4eSSatish Balay   } else {
765e5c89e4eSSatish Balay     ierr = PetscSetProgramName("Unknown Name");CHKERRQ(ierr);
766e5c89e4eSSatish Balay   }
767e5c89e4eSSatish Balay 
768e5c89e4eSSatish Balay   ierr = MPI_Initialized(&flag);CHKERRQ(ierr);
769e5c89e4eSSatish Balay   if (!flag) {
770e32f2f54SBarry 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");
7715e765c61SJed Brown #if defined(PETSC_HAVE_MPI_INIT_THREAD)
7725e765c61SJed Brown     {
7735e765c61SJed Brown       PetscMPIInt provided;
7745e765c61SJed Brown       ierr = MPI_Init_thread(argc,args,MPI_THREAD_FUNNELED,&provided);CHKERRQ(ierr);
7755e765c61SJed Brown     }
7765e765c61SJed Brown #else
777e5c89e4eSSatish Balay     ierr = MPI_Init(argc,args);CHKERRQ(ierr);
7785e765c61SJed Brown #endif
779e5c89e4eSSatish Balay     PetscBeganMPI = PETSC_TRUE;
780e5c89e4eSSatish Balay   }
781e5c89e4eSSatish Balay   if (argc && args) {
782e5c89e4eSSatish Balay     PetscGlobalArgc = *argc;
783e5c89e4eSSatish Balay     PetscGlobalArgs = *args;
784e5c89e4eSSatish Balay   }
785e5c89e4eSSatish Balay   PetscFinalizeCalled = PETSC_FALSE;
786e5c89e4eSSatish Balay 
787a297a907SKarl Rupp   if (PETSC_COMM_WORLD == MPI_COMM_NULL) PETSC_COMM_WORLD = MPI_COMM_WORLD;
788d54338ecSKarl Rupp   ierr = MPI_Comm_set_errhandler(PETSC_COMM_WORLD,MPI_ERRORS_RETURN);CHKERRQ(ierr);
789e5c89e4eSSatish Balay 
790e5c89e4eSSatish Balay   /* Done after init due to a bug in MPICH-GM? */
791e5c89e4eSSatish Balay   ierr = PetscErrorPrintfInitialize();CHKERRQ(ierr);
792e5c89e4eSSatish Balay 
793e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(MPI_COMM_WORLD,&PetscGlobalRank);CHKERRQ(ierr);
794e5c89e4eSSatish Balay   ierr = MPI_Comm_size(MPI_COMM_WORLD,&PetscGlobalSize);CHKERRQ(ierr);
795e5c89e4eSSatish Balay 
7968ad47952SJed Brown   MPIU_BOOL = MPI_INT;
7978ad47952SJed Brown   MPIU_ENUM = MPI_INT;
7988ad47952SJed Brown 
799e5c89e4eSSatish Balay   /*
800e5c89e4eSSatish Balay      Initialized the global complex variable; this is because with
801e5c89e4eSSatish Balay      shared libraries the constructors for global variables
802e5c89e4eSSatish Balay      are not called; at least on IRIX.
803e5c89e4eSSatish Balay   */
804886cfec0SSatish Balay #if defined(PETSC_HAVE_COMPLEX)
805e5c89e4eSSatish Balay   {
806a24ee22aSSatish Balay #if defined(PETSC_CLANGUAGE_CXX)
80750f81f78SJed Brown     PetscComplex ic(0.0,1.0);
808e5c89e4eSSatish Balay     PETSC_i = ic;
809a24ee22aSSatish Balay #elif defined(PETSC_CLANGUAGE_C)
81050f81f78SJed Brown     PETSC_i = _Complex_I;
811b7940d39SSatish Balay #endif
812762437b8SSatish Balay   }
813762437b8SSatish Balay 
8142c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
815e69cd0e6SSatish Balay   ierr = MPI_Type_contiguous(2,MPI_DOUBLE,&MPIU_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
816500d8756SSatish Balay   ierr = MPI_Type_commit(&MPIU_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
817500d8756SSatish Balay   ierr = MPI_Type_contiguous(2,MPI_FLOAT,&MPIU_C_COMPLEX);CHKERRQ(ierr);
818500d8756SSatish Balay   ierr = MPI_Type_commit(&MPIU_C_COMPLEX);CHKERRQ(ierr);
8192c876bd9SBarry Smith #endif
820886cfec0SSatish Balay #endif /* PETSC_HAVE_COMPLEX */
821e5c89e4eSSatish Balay 
822e5c89e4eSSatish Balay   /*
823e5c89e4eSSatish Balay      Create the PETSc MPI reduction operator that sums of the first
824e5c89e4eSSatish Balay      half of the entries and maxes the second half.
825e5c89e4eSSatish Balay   */
826e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscMaxSum_Local,1,&PetscMaxSum_Op);CHKERRQ(ierr);
827e5c89e4eSSatish Balay 
828ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
829c90a1750SBarry Smith   ierr = MPI_Type_contiguous(2,MPI_DOUBLE,&MPIU___FLOAT128);CHKERRQ(ierr);
830c90a1750SBarry Smith   ierr = MPI_Type_commit(&MPIU___FLOAT128);CHKERRQ(ierr);
8317c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
8328c764dc5SJose Roman   ierr = MPI_Type_contiguous(4,MPI_DOUBLE,&MPIU___COMPLEX128);CHKERRQ(ierr);
8338c764dc5SJose Roman   ierr = MPI_Type_commit(&MPIU___COMPLEX128);CHKERRQ(ierr);
8348c764dc5SJose Roman #endif
835d9822059SBarry Smith   ierr = MPI_Op_create(PetscMax_Local,1,&MPIU_MAX);CHKERRQ(ierr);
836d9822059SBarry Smith   ierr = MPI_Op_create(PetscMin_Local,1,&MPIU_MIN);CHKERRQ(ierr);
837c90a1750SBarry Smith #endif
838c90a1750SBarry Smith 
8397c2de775SJed Brown #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128)
840cca4cb22SSatish Balay   ierr = MPI_Op_create(PetscSum_Local,1,&MPIU_SUM);CHKERRQ(ierr);
841cca4cb22SSatish Balay #endif
842cca4cb22SSatish Balay 
843e5c89e4eSSatish Balay   ierr = MPI_Type_contiguous(2,MPIU_SCALAR,&MPIU_2SCALAR);CHKERRQ(ierr);
844e5c89e4eSSatish Balay   ierr = MPI_Type_commit(&MPIU_2SCALAR);CHKERRQ(ierr);
845e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscADMax_Local,1,&PetscADMax_Op);CHKERRQ(ierr);
846e5c89e4eSSatish Balay   ierr = MPI_Op_create(PetscADMin_Local,1,&PetscADMin_Op);CHKERRQ(ierr);
847e5c89e4eSSatish Balay 
84844041f26SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
849e5c89e4eSSatish Balay   ierr = MPI_Type_contiguous(2,MPIU_INT,&MPIU_2INT);CHKERRQ(ierr);
850e5c89e4eSSatish Balay   ierr = MPI_Type_commit(&MPIU_2INT);CHKERRQ(ierr);
85144041f26SJed Brown #endif
852e5c89e4eSSatish Balay 
853ec957eceSBarry Smith 
854e5c89e4eSSatish Balay   /*
855480cf27aSJed Brown      Attributes to be set on PETSc communicators
856480cf27aSJed Brown   */
857480cf27aSJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelCounter,&Petsc_Counter_keyval,(void*)0);CHKERRQ(ierr);
858da3039f7SJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm_Outer,&Petsc_InnerComm_keyval,(void*)0);CHKERRQ(ierr);
859da3039f7SJed Brown   ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelComm_Inner,&Petsc_OuterComm_keyval,(void*)0);CHKERRQ(ierr);
860480cf27aSJed Brown 
861480cf27aSJed Brown   /*
862e8fb0fc0SBarry Smith      Build the options database
863e5c89e4eSSatish Balay   */
864e5c89e4eSSatish Balay   ierr = PetscOptionsInsert(argc,args,file);CHKERRQ(ierr);
865e5c89e4eSSatish Balay 
8666dc8fec2Sbcordonn 
867e5c89e4eSSatish Balay   /*
868e5c89e4eSSatish Balay      Print main application help message
869e5c89e4eSSatish Balay   */
8700298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-help",&flg);CHKERRQ(ierr);
871e5c89e4eSSatish Balay   if (help && flg) {
872e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,help);CHKERRQ(ierr);
873e5c89e4eSSatish Balay   }
874e5c89e4eSSatish Balay   ierr = PetscOptionsCheckInitial_Private();CHKERRQ(ierr);
875e5c89e4eSSatish Balay 
876d45a07a7SBarry Smith   ierr = PetscCitationsInitialize();CHKERRQ(ierr);
877d45a07a7SBarry Smith 
878e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
879*11525c0dSBarry Smith   ierr = PetscInitializeSAWs(help);CHKERRQ(ierr);
880f4202a44SBarry Smith #endif
881f4202a44SBarry Smith 
882e5c89e4eSSatish Balay   /* SHOULD PUT IN GUARDS: Make sure logging is initialized, even if we do not print it out */
883a9f03627SSatish Balay #if defined(PETSC_USE_LOG)
884e5c89e4eSSatish Balay   ierr = PetscLogBegin_Private();CHKERRQ(ierr);
885a9f03627SSatish Balay #endif
886e5c89e4eSSatish Balay 
887e5c89e4eSSatish Balay   /*
888e5c89e4eSSatish Balay      Load the dynamic libraries (on machines that support them), this registers all
889e5c89e4eSSatish Balay      the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes)
890e5c89e4eSSatish Balay   */
891e5c89e4eSSatish Balay   ierr = PetscInitialize_DynamicLibraries();CHKERRQ(ierr);
892e5c89e4eSSatish Balay 
893e5c89e4eSSatish Balay   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
894ae15b995SBarry Smith   ierr = PetscInfo1(0,"PETSc successfully started: number of processors = %d\n",size);CHKERRQ(ierr);
895e5c89e4eSSatish Balay   ierr = PetscGetHostName(hostname,256);CHKERRQ(ierr);
896ae15b995SBarry Smith   ierr = PetscInfo1(0,"Running on machine: %s\n",hostname);CHKERRQ(ierr);
897e5c89e4eSSatish Balay 
898e5c89e4eSSatish Balay   ierr = PetscOptionsCheckInitial_Components();CHKERRQ(ierr);
899ef6c6fedSBoyana Norris   /* Check the options database for options related to the options database itself */
900ef6c6fedSBoyana Norris   ierr = PetscOptionsSetFromOptions();CHKERRQ(ierr);
901ef6c6fedSBoyana Norris 
902951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32)
903e39fd77fSBarry Smith   /*
904e39fd77fSBarry Smith       Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI
905e39fd77fSBarry Smith 
906e39fd77fSBarry Smith       Currently not used because it is not supported by MPICH.
907e39fd77fSBarry Smith   */
908e39fd77fSBarry Smith #if !defined(PETSC_WORDS_BIGENDIAN)
9090298fd71SBarry Smith   ierr = MPI_Register_datarep((char*)"petsc",PetscDataRep_read_conv_fn,PetscDataRep_write_conv_fn,PetscDataRep_extent_fn,NULL);CHKERRQ(ierr);
910e39fd77fSBarry Smith #endif
911951e3c8eSBarry Smith #endif
912e39fd77fSBarry Smith 
9130298fd71SBarry Smith   ierr = PetscOptionsGetInt(NULL,"-hmpi_spawn_size",&nodesize,&flg);CHKERRQ(ierr);
914793721a6SBarry Smith   if (flg) {
91523464e94SBarry Smith #if defined(PETSC_HAVE_MPI_COMM_SPAWN)
91685afcc9aSBarry Smith     ierr = PetscHMPISpawn((PetscMPIInt) nodesize);CHKERRQ(ierr); /* worker nodes never return from here; they go directly to PetscEnd() */
91723464e94SBarry Smith #else
91885afcc9aSBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"PETSc built without MPI 2 (MPI_Comm_spawn) support, use -hmpi_merge_size instead");
91923464e94SBarry Smith #endif
920793721a6SBarry Smith   } else {
9210298fd71SBarry Smith     ierr = PetscOptionsGetInt(NULL,"-hmpi_merge_size",&nodesize,&flg);CHKERRQ(ierr);
9228002f1cdSBarry Smith     if (flg) {
9230298fd71SBarry Smith       ierr = PetscHMPIMerge((PetscMPIInt) nodesize,NULL,NULL);CHKERRQ(ierr);
92485afcc9aSBarry Smith       if (PetscHMPIWorker) { /* if worker then never enter user code */
925bad7cb1dSBarry Smith         PetscInitializeCalled = PETSC_TRUE;
9263bf036e2SBarry Smith         PetscEnd();
9279505b675SBarry Smith       }
9288002f1cdSBarry Smith     }
929793721a6SBarry Smith   }
930e5c89e4eSSatish Balay 
93135d88935SVictor Minden #if defined(PETSC_HAVE_CUDA)
9324bb5149bSJed Brown   {
9334bb5149bSJed Brown     PetscMPIInt p;
934663ba86aSMatthew G Knepley     for (p = 0; p < PetscGlobalSize; ++p) {
935a297a907SKarl Rupp       if (p == PetscGlobalRank) cublasInit();
936663ba86aSMatthew G Knepley       ierr = MPI_Barrier(PETSC_COMM_WORLD);CHKERRQ(ierr);
937663ba86aSMatthew G Knepley     }
9384bb5149bSJed Brown   }
9393e39abd9SVictor Minden #endif
94092e62aa6SBarry Smith 
9410298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-python",&flg);CHKERRQ(ierr);
9429ac80d5eSLisandro Dalcin   if (flg) {
9439ac80d5eSLisandro Dalcin     PetscInitializeCalled = PETSC_TRUE;
9440298fd71SBarry Smith     ierr = PetscPythonInitialize(NULL,NULL);CHKERRQ(ierr);
9459ac80d5eSLisandro Dalcin   }
9469ac80d5eSLisandro Dalcin 
947607a6623SBarry Smith   ierr = PetscThreadCommInitializePackage();CHKERRQ(ierr);
948bd8b14e7SShri Abhyankar 
94941c0b4b3SShri Abhyankar   /*
95041c0b4b3SShri Abhyankar       Setup building of stack frames for all function calls
95141c0b4b3SShri Abhyankar   */
95276386721SLisandro Dalcin   PetscThreadLocalRegister((PetscThreadKey*)&petscstack); /* Creates pthread_key */
953e1167bb9SShri Abhyankar #if defined(PETSC_USE_DEBUG)
954e1167bb9SShri Abhyankar   ierr = PetscStackCreate();CHKERRQ(ierr);
955e1167bb9SShri Abhyankar #endif
956e1167bb9SShri Abhyankar 
9572d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
9582d53ad75SBarry Smith   ierr = PetscFPTCreate(10000);CHKERRQ(ierr);
9592d53ad75SBarry Smith #endif
9602d53ad75SBarry Smith 
961dff31646SBarry Smith 
962301d30feSBarry Smith   /*
963301d30feSBarry Smith       Once we are completedly initialized then we can set this variables
964301d30feSBarry Smith   */
965301d30feSBarry Smith   PetscInitializeCalled = PETSC_TRUE;
966301d30feSBarry Smith   PetscFunctionReturn(0);
967e5c89e4eSSatish Balay }
968e5c89e4eSSatish Balay 
9692eff7a51SBarry Smith extern PetscObject *PetscObjects;
9702eff7a51SBarry Smith extern PetscInt    PetscObjectsCounts, PetscObjectsMaxCounts;
971e5c89e4eSSatish Balay 
972e5c89e4eSSatish Balay #undef __FUNCT__
973e5c89e4eSSatish Balay #define __FUNCT__ "PetscFinalize"
974e5c89e4eSSatish Balay /*@C
975e5c89e4eSSatish Balay    PetscFinalize - Checks for options to be called at the conclusion
976e5c89e4eSSatish Balay    of the program. MPI_Finalize() is called only if the user had not
977e5c89e4eSSatish Balay    called MPI_Init() before calling PetscInitialize().
978e5c89e4eSSatish Balay 
979e5c89e4eSSatish Balay    Collective on PETSC_COMM_WORLD
980e5c89e4eSSatish Balay 
981e5c89e4eSSatish Balay    Options Database Keys:
98288c29154SBarry Smith +  -options_table - Calls PetscOptionsView()
983e5c89e4eSSatish Balay .  -options_left - Prints unused options that remain in the database
9847eb1d149SBarry 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
985e5c89e4eSSatish Balay .  -mpidump - Calls PetscMPIDump()
986e5c89e4eSSatish Balay .  -malloc_dump - Calls PetscMallocDump()
987e5c89e4eSSatish Balay .  -malloc_info - Prints total memory usage
988e5c89e4eSSatish Balay -  -malloc_log - Prints summary of memory usage
989e5c89e4eSSatish Balay 
990e5c89e4eSSatish Balay    Level: beginner
991e5c89e4eSSatish Balay 
992e5c89e4eSSatish Balay    Note:
993e5c89e4eSSatish Balay    See PetscInitialize() for more general runtime options.
994e5c89e4eSSatish Balay 
99588c29154SBarry Smith .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscEnd()
996e5c89e4eSSatish Balay @*/
9977087cfbeSBarry Smith PetscErrorCode  PetscFinalize(void)
998e5c89e4eSSatish Balay {
999e5c89e4eSSatish Balay   PetscErrorCode ierr;
10004bb5149bSJed Brown   PetscMPIInt    rank;
1001a8d2bbe5SBarry Smith   PetscInt       nopt;
10022bf49c77SBarry Smith   PetscBool      flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE;
1003dff31646SBarry Smith   PetscBool      flg;
100410463e74SBarry Smith #if defined(PETSC_USE_LOG)
100510463e74SBarry Smith   char           mname[PETSC_MAX_PATH_LEN];
100610463e74SBarry Smith #endif
1007e5c89e4eSSatish Balay 
1008e5c89e4eSSatish Balay   PetscFunctionBegin;
1009e5c89e4eSSatish Balay   if (!PetscInitializeCalled) {
10104b09e917SBarry Smith     printf("PetscInitialize() must be called before PetscFinalize()\n");
10114b09e917SBarry Smith     PetscFunctionReturn(PETSC_ERR_ARG_WRONGSTATE);
1012e5c89e4eSSatish Balay   }
10130298fd71SBarry Smith   ierr = PetscInfo(NULL,"PetscFinalize() called\n");CHKERRQ(ierr);
1014b022a5c1SBarry Smith 
10151f817a21SBarry Smith   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
10161f817a21SBarry Smith 
10171f817a21SBarry Smith   ierr = PetscOptionsHasName(NULL,"-citations",&flg);CHKERRQ(ierr);
1018dff31646SBarry Smith   if (flg) {
10191f817a21SBarry Smith     char  *cits, filename[PETSC_MAX_PATH_LEN];
10201f817a21SBarry Smith     FILE  *fd = PETSC_STDOUT;
10211f817a21SBarry Smith 
10221f817a21SBarry Smith     ierr = PetscOptionsGetString(NULL,"-citations",filename,PETSC_MAX_PATH_LEN,NULL);CHKERRQ(ierr);
10231f817a21SBarry Smith     if (filename[0]) {
10241f817a21SBarry Smith       ierr = PetscFOpen(PETSC_COMM_WORLD,filename,"w",&fd);CHKERRQ(ierr);
10251f817a21SBarry Smith     }
1026dff31646SBarry Smith     ierr = PetscSegBufferGet(PetscCitationsList,1,&cits);CHKERRQ(ierr);
1027dff31646SBarry Smith     cits[0] = 0;
1028dff31646SBarry Smith     ierr = PetscSegBufferExtractAlloc(PetscCitationsList,&cits);CHKERRQ(ierr);
10291f817a21SBarry Smith     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"If you publish results based on this computation please cite the following:\n");CHKERRQ(ierr);
10301f817a21SBarry Smith     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"===========================================================================\n");CHKERRQ(ierr);
10311f817a21SBarry Smith     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"%s",cits);CHKERRQ(ierr);
10321f817a21SBarry Smith     ierr = PetscFPrintf(PETSC_COMM_WORLD,fd,"===========================================================================\n");CHKERRQ(ierr);
10331f817a21SBarry Smith     ierr = PetscFClose(PETSC_COMM_WORLD,fd);CHKERRQ(ierr);
1034dff31646SBarry Smith     ierr = PetscFree(cits);CHKERRQ(ierr);
1035dff31646SBarry Smith   }
1036dff31646SBarry Smith   ierr = PetscSegBufferDestroy(&PetscCitationsList);CHKERRQ(ierr);
1037dff31646SBarry Smith 
10382d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
10392d53ad75SBarry Smith   ierr = PetscFPTDestroy();CHKERRQ(ierr);
10402d53ad75SBarry Smith #endif
10412d53ad75SBarry Smith 
10422d53ad75SBarry Smith 
1043e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1044dff31646SBarry Smith   flg = PETSC_FALSE;
1045e78c4b8cSBarry Smith   ierr = PetscOptionsGetBool(NULL,"-saw_options",&flg,NULL);CHKERRQ(ierr);
1046d5649816SBarry Smith   if (flg) {
1047e04113cfSBarry Smith     ierr = PetscOptionsSAWsDestroy();CHKERRQ(ierr);
1048d5649816SBarry Smith   }
1049d5649816SBarry Smith #endif
1050d5649816SBarry Smith 
10516d065ec1SBarry Smith #if defined(PETSC_HAVE_SERVER)
10522d139d8fSBarry Smith   flg1 = PETSC_FALSE;
10532d139d8fSBarry Smith   ierr = PetscOptionsGetBool(NULL,"-server",&flg1,NULL);CHKERRQ(ierr);
10542d139d8fSBarry Smith   if (flg1) {
10556d065ec1SBarry Smith     /*  this is a crude hack, but better than nothing */
10562d139d8fSBarry Smith     ierr = PetscPOpen(PETSC_COMM_WORLD,NULL,"pkill -9 petscwebserver","r",NULL);CHKERRQ(ierr);
10572d139d8fSBarry Smith   }
10586d065ec1SBarry Smith #endif
10592d139d8fSBarry Smith 
1060681455b2SBarry Smith #if defined(PETSC_HAVE_X)
1061681455b2SBarry Smith   flg1 = PETSC_FALSE;
1062681455b2SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-x_virtual",&flg1,NULL);CHKERRQ(ierr);
1063681455b2SBarry Smith   if (flg1) {
1064681455b2SBarry Smith     /*  this is a crude hack, but better than nothing */
1065681455b2SBarry Smith     ierr = PetscPOpen(PETSC_COMM_WORLD,NULL,"pkill -9 Xvfb","r",NULL);CHKERRQ(ierr);
1066681455b2SBarry Smith   }
1067681455b2SBarry Smith #endif
1068681455b2SBarry Smith 
106985afcc9aSBarry Smith   ierr = PetscHMPIFinalize();CHKERRQ(ierr);
1070d3f95da6SShri Abhyankar 
10710298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-malloc_info",&flg2,NULL);CHKERRQ(ierr);
1072e5c89e4eSSatish Balay   if (!flg2) {
107390d69ab7SBarry Smith     flg2 = PETSC_FALSE;
10740298fd71SBarry Smith     ierr = PetscOptionsGetBool(NULL,"-memory_info",&flg2,NULL);CHKERRQ(ierr);
1075e5c89e4eSSatish Balay   }
1076e5c89e4eSSatish Balay   if (flg2) {
1077e5c89e4eSSatish Balay     ierr = PetscMemoryShowUsage(PETSC_VIEWER_STDOUT_WORLD,"Summary of Memory Usage in PETSc\n");CHKERRQ(ierr);
1078e5c89e4eSSatish Balay   }
1079e5c89e4eSSatish Balay 
1080e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
108190d69ab7SBarry Smith   flg1 = PETSC_FALSE;
10820298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-get_total_flops",&flg1,NULL);CHKERRQ(ierr);
1083e5c89e4eSSatish Balay   if (flg1) {
1084e5c89e4eSSatish Balay     PetscLogDouble flops = 0;
1085205a32c2SJed Brown     ierr = MPI_Reduce(&petsc_TotalFlops,&flops,1,MPI_DOUBLE,MPI_SUM,0,PETSC_COMM_WORLD);CHKERRQ(ierr);
1086e5c89e4eSSatish Balay     ierr = PetscPrintf(PETSC_COMM_WORLD,"Total flops over all processors %g\n",flops);CHKERRQ(ierr);
1087e5c89e4eSSatish Balay   }
1088e5c89e4eSSatish Balay #endif
1089e5c89e4eSSatish Balay 
1090e5c89e4eSSatish Balay 
1091e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
1092e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE)
1093e5c89e4eSSatish Balay   mname[0] = 0;
1094a297a907SKarl Rupp 
10950298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL,"-log_mpe",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
1096e5c89e4eSSatish Balay   if (flg1) {
1097e5c89e4eSSatish Balay     if (mname[0]) {ierr = PetscLogMPEDump(mname);CHKERRQ(ierr);}
1098e5c89e4eSSatish Balay     else          {ierr = PetscLogMPEDump(0);CHKERRQ(ierr);}
1099e5c89e4eSSatish Balay   }
1100e5c89e4eSSatish Balay #endif
1101e5c89e4eSSatish Balay   mname[0] = 0;
1102a297a907SKarl Rupp 
11030298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL,"-log_summary",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
1104e5c89e4eSSatish Balay   if (flg1) {
110591eabc43SBarry Smith     PetscViewer viewer;
110691eabc43SBarry Smith     if (mname[0]) {
110791eabc43SBarry Smith       ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);CHKERRQ(ierr);
110891eabc43SBarry Smith       ierr = PetscLogView(viewer);CHKERRQ(ierr);
11096bf464f9SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
111033f85c2fSBarry Smith     } else {
111133f85c2fSBarry Smith       viewer = PETSC_VIEWER_STDOUT_WORLD;
111233f85c2fSBarry Smith       ierr   = PetscLogView(viewer);CHKERRQ(ierr);
111333f85c2fSBarry Smith     }
1114e5c89e4eSSatish Balay   }
1115e5c89e4eSSatish Balay 
1116ff5bc46bSBarry Smith   mname[0] = 0;
1117a297a907SKarl Rupp 
11180298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL,"-log_summary_python",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
1119ff5bc46bSBarry Smith   if (flg1) {
1120ff5bc46bSBarry Smith     PetscViewer viewer;
1121ff5bc46bSBarry Smith     if (mname[0]) {
1122ff5bc46bSBarry Smith       ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,mname,&viewer);CHKERRQ(ierr);
112391eabc43SBarry Smith       ierr = PetscLogViewPython(viewer);CHKERRQ(ierr);
11246bf464f9SBarry Smith       ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
112533f85c2fSBarry Smith     } else {
112633f85c2fSBarry Smith       viewer = PETSC_VIEWER_STDOUT_WORLD;
112733f85c2fSBarry Smith       ierr   = PetscLogViewPython(viewer);CHKERRQ(ierr);
112833f85c2fSBarry Smith     }
1129ff5bc46bSBarry Smith   }
1130ff5bc46bSBarry Smith 
11310298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL,"-log_detailed",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
113278392ef1SBarry Smith   if (flg1) {
113378392ef1SBarry Smith     if (mname[0])  {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,mname);CHKERRQ(ierr);}
113478392ef1SBarry Smith     else           {ierr = PetscLogPrintDetailed(PETSC_COMM_WORLD,0);CHKERRQ(ierr);}
113578392ef1SBarry Smith   }
113678392ef1SBarry Smith 
1137e5c89e4eSSatish Balay   mname[0] = 0;
1138a297a907SKarl Rupp 
11390298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL,"-log_all",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
11400298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL,"-log",mname,PETSC_MAX_PATH_LEN,&flg2);CHKERRQ(ierr);
1141e5c89e4eSSatish Balay   if (flg1 || flg2) {
1142e5c89e4eSSatish Balay     if (mname[0]) PetscLogDump(mname);
1143e5c89e4eSSatish Balay     else          PetscLogDump(0);
1144e5c89e4eSSatish Balay   }
1145e5c89e4eSSatish Balay #endif
114610463e74SBarry Smith 
1147b58ca069SBarry Smith   /*
1148b58ca069SBarry Smith      Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_().
1149b58ca069SBarry Smith   */
1150b58ca069SBarry Smith   ierr = PetscObjectRegisterDestroyAll();CHKERRQ(ierr);
1151b58ca069SBarry Smith 
115233f85c2fSBarry Smith   ierr = PetscStackDestroy();CHKERRQ(ierr);
115376386721SLisandro Dalcin   PetscThreadLocalDestroy((PetscThreadKey)petscstack); /* Deletes pthread_key */
115410463e74SBarry Smith 
115590d69ab7SBarry Smith   flg1 = PETSC_FALSE;
11560298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-no_signal_handler",&flg1,NULL);CHKERRQ(ierr);
1157e5c89e4eSSatish Balay   if (!flg1) { ierr = PetscPopSignalHandler();CHKERRQ(ierr);}
115890d69ab7SBarry Smith   flg1 = PETSC_FALSE;
11590298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-mpidump",&flg1,NULL);CHKERRQ(ierr);
1160e5c89e4eSSatish Balay   if (flg1) {
1161e5c89e4eSSatish Balay     ierr = PetscMPIDump(stdout);CHKERRQ(ierr);
1162e5c89e4eSSatish Balay   }
116390d69ab7SBarry Smith   flg1 = PETSC_FALSE;
116490d69ab7SBarry Smith   flg2 = PETSC_FALSE;
11658bb29257SSatish Balay   /* preemptive call to avoid listing this option in options table as unused */
11660298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-malloc_dump",&flg1);CHKERRQ(ierr);
11670298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-objects_dump",&flg1);CHKERRQ(ierr);
11680298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-options_table",&flg2,NULL);CHKERRQ(ierr);
1169e4c476e2SSatish Balay 
1170e5c89e4eSSatish Balay   if (flg2) {
1171be56827dSJed Brown     PetscViewer viewer;
1172be56827dSJed Brown     ierr = PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr);
1173be56827dSJed Brown     ierr = PetscOptionsView(viewer);CHKERRQ(ierr);
1174be56827dSJed Brown     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1175e5c89e4eSSatish Balay   }
1176e5c89e4eSSatish Balay 
1177e5c89e4eSSatish Balay   /* to prevent PETSc -options_left from warning */
11780298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-nox",&flg1);CHKERRQ(ierr);
11790298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-nox_warning",&flg1);CHKERRQ(ierr);
1180e5c89e4eSSatish Balay 
118185afcc9aSBarry Smith   if (!PetscHMPIWorker) { /* worker processes skip this because they do not usually process options */
118233fc4174SSatish Balay     flg3 = PETSC_FALSE; /* default value is required */
11830298fd71SBarry Smith     ierr = PetscOptionsGetBool(NULL,"-options_left",&flg3,&flg1);CHKERRQ(ierr);
1184e5c89e4eSSatish Balay     ierr = PetscOptionsAllUsed(&nopt);CHKERRQ(ierr);
1185e5c89e4eSSatish Balay     if (flg3) {
1186e5c89e4eSSatish Balay       if (!flg2) { /* have not yet printed the options */
1187be56827dSJed Brown         PetscViewer viewer;
1188be56827dSJed Brown         ierr = PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr);
1189be56827dSJed Brown         ierr = PetscOptionsView(viewer);CHKERRQ(ierr);
1190be56827dSJed Brown         ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1191e5c89e4eSSatish Balay       }
1192e5c89e4eSSatish Balay       if (!nopt) {
1193e5c89e4eSSatish Balay         ierr = PetscPrintf(PETSC_COMM_WORLD,"There are no unused options.\n");CHKERRQ(ierr);
1194e5c89e4eSSatish Balay       } else if (nopt == 1) {
1195e5c89e4eSSatish Balay         ierr = PetscPrintf(PETSC_COMM_WORLD,"There is one unused database option. It is:\n");CHKERRQ(ierr);
1196e5c89e4eSSatish Balay       } else {
11977582186dSLisandro Dalcin         ierr = PetscPrintf(PETSC_COMM_WORLD,"There are %D unused database options. They are:\n",nopt);CHKERRQ(ierr);
1198e5c89e4eSSatish Balay       }
1199e5c89e4eSSatish Balay     }
1200e5c89e4eSSatish Balay #if defined(PETSC_USE_DEBUG)
1201da8b8a77SBarry Smith     if (nopt && !flg3 && !flg1) {
1202e5c89e4eSSatish Balay       ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! There are options you set that were not used!\n");CHKERRQ(ierr);
1203e5c89e4eSSatish Balay       ierr = PetscPrintf(PETSC_COMM_WORLD,"WARNING! could be spelling mistake, etc!\n");CHKERRQ(ierr);
1204e5c89e4eSSatish Balay       ierr = PetscOptionsLeft();CHKERRQ(ierr);
1205e5c89e4eSSatish Balay     } else if (nopt && flg3) {
1206e5c89e4eSSatish Balay #else
1207e5c89e4eSSatish Balay     if (nopt && flg3) {
1208e5c89e4eSSatish Balay #endif
1209e5c89e4eSSatish Balay       ierr = PetscOptionsLeft();CHKERRQ(ierr);
1210e5c89e4eSSatish Balay     }
1211931f367cSBarry Smith   }
1212e5c89e4eSSatish Balay 
1213e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
1214d45a07a7SBarry Smith   if (!PetscGlobalRank) {
121587f587eeSBarry Smith     ierr = PetscStackSAWsViewOff();CHKERRQ(ierr);
121616ad0300SBarry Smith     PetscStackCallSAWs(SAWs_Finalize,());
1217d45a07a7SBarry Smith   }
1218ec957eceSBarry Smith #endif
1219ec957eceSBarry Smith 
1220eea2bdeeSJed Brown   {
1221eea2bdeeSJed Brown     PetscThreadComm tcomm_world;
1222eea2bdeeSJed Brown     ierr = PetscGetThreadCommWorld(&tcomm_world);CHKERRQ(ierr);
1223eea2bdeeSJed Brown     /* Free global thread communicator */
1224eea2bdeeSJed Brown     ierr = PetscThreadCommDestroy(&tcomm_world);CHKERRQ(ierr);
1225eea2bdeeSJed Brown   }
1226eea2bdeeSJed Brown 
122710463e74SBarry Smith   /*
1228dbc8283eSBarry Smith        List all objects the user may have forgot to free
12292eff7a51SBarry Smith   */
12300298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-objects_dump",&flg1);CHKERRQ(ierr);
1231a64a8e02SBarry Smith   if (flg1) {
1232a64a8e02SBarry Smith     MPI_Comm local_comm;
12337eb1d149SBarry Smith     char     string[64];
1234a64a8e02SBarry Smith 
12350298fd71SBarry Smith     ierr = PetscOptionsGetString(NULL,"-objects_dump",string,64,NULL);CHKERRQ(ierr);
1236a64a8e02SBarry Smith     ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr);
1237a64a8e02SBarry Smith     ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr);
12387eb1d149SBarry Smith     ierr = PetscObjectsDump(stdout,(string[0] == 'a') ? PETSC_TRUE : PETSC_FALSE);CHKERRQ(ierr);
1239a64a8e02SBarry Smith     ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr);
1240a64a8e02SBarry Smith     ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr);
12410a1571b3SBarry Smith   }
1242dbc8283eSBarry Smith   PetscObjectsCounts    = 0;
1243dbc8283eSBarry Smith   PetscObjectsMaxCounts = 0;
12442eff7a51SBarry Smith 
1245a297a907SKarl Rupp   ierr = PetscFree(PetscObjects);CHKERRQ(ierr);
12462eff7a51SBarry Smith 
124733f85c2fSBarry Smith #if defined(PETSC_USE_LOG)
124833f85c2fSBarry Smith   ierr = PetscLogDestroy();CHKERRQ(ierr);
124933f85c2fSBarry Smith #endif
125033f85c2fSBarry Smith 
125133f85c2fSBarry Smith   /*
125233f85c2fSBarry Smith      Destroy any packages that registered a finalize
125333f85c2fSBarry Smith   */
125433f85c2fSBarry Smith   ierr = PetscRegisterFinalizeAll();CHKERRQ(ierr);
125533f85c2fSBarry Smith 
125633f85c2fSBarry Smith   /*
125710463e74SBarry Smith      Destroy all the function registration lists created
125810463e74SBarry Smith   */
125910463e74SBarry Smith   ierr = PetscFinalize_DynamicLibraries();CHKERRQ(ierr);
126010463e74SBarry Smith 
126148dd1dffSBarry Smith   /*
126248dd1dffSBarry Smith      Print PetscFunctionLists that have not been properly freed
126348dd1dffSBarry Smith 
126437e93019SBarry Smith   ierr = PetscFunctionListPrintAll();CHKERRQ(ierr);
126548dd1dffSBarry Smith   */
126637e93019SBarry Smith 
12674028d114SSatish Balay   if (petsc_history) {
1268f3dea69dSBarry Smith     ierr = PetscCloseHistoryFile(&petsc_history);CHKERRQ(ierr);
1269e5c89e4eSSatish Balay     petsc_history = 0;
1270e5c89e4eSSatish Balay   }
1271e5c89e4eSSatish Balay 
12720298fd71SBarry Smith   ierr = PetscInfoAllow(PETSC_FALSE,NULL);CHKERRQ(ierr);
1273e5c89e4eSSatish Balay 
12748bb29257SSatish Balay   {
1275e5c89e4eSSatish Balay     char fname[PETSC_MAX_PATH_LEN];
1276e5c89e4eSSatish Balay     FILE *fd;
1277ed9cf6e9SBarry Smith     int  err;
1278e5c89e4eSSatish Balay 
1279e5c89e4eSSatish Balay     fname[0] = 0;
1280a297a907SKarl Rupp 
12810298fd71SBarry Smith     ierr = PetscOptionsGetString(NULL,"-malloc_dump",fname,250,&flg1);CHKERRQ(ierr);
1282dc92acbaSJed Brown     flg2 = PETSC_FALSE;
12830298fd71SBarry Smith     ierr = PetscOptionsGetBool(NULL,"-malloc_test",&flg2,NULL);CHKERRQ(ierr);
12848bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG)
1285dc92acbaSJed Brown     if (PETSC_RUNNING_ON_VALGRIND) flg2 = PETSC_FALSE;
1286dc92acbaSJed Brown #else
1287dc92acbaSJed Brown     flg2 = PETSC_FALSE;         /* Skip reporting for optimized builds regardless of -malloc_test */
1288dc92acbaSJed Brown #endif
1289e5c89e4eSSatish Balay     if (flg1 && fname[0]) {
1290e5c89e4eSSatish Balay       char sname[PETSC_MAX_PATH_LEN];
1291e5c89e4eSSatish Balay 
1292e5c89e4eSSatish Balay       sprintf(sname,"%s_%d",fname,rank);
1293e32f2f54SBarry Smith       fd   = fopen(sname,"w"); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",sname);
1294e5c89e4eSSatish Balay       ierr = PetscMallocDump(fd);CHKERRQ(ierr);
1295ed9cf6e9SBarry Smith       err  = fclose(fd);
1296e32f2f54SBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
1297dc92acbaSJed Brown     } else if (flg1 || flg2) {
1298e5c89e4eSSatish Balay       MPI_Comm local_comm;
1299e5c89e4eSSatish Balay 
1300e5c89e4eSSatish Balay       ierr = MPI_Comm_dup(MPI_COMM_WORLD,&local_comm);CHKERRQ(ierr);
1301e5c89e4eSSatish Balay       ierr = PetscSequentialPhaseBegin_Private(local_comm,1);CHKERRQ(ierr);
1302e5c89e4eSSatish Balay       ierr = PetscMallocDump(stdout);CHKERRQ(ierr);
1303e5c89e4eSSatish Balay       ierr = PetscSequentialPhaseEnd_Private(local_comm,1);CHKERRQ(ierr);
1304e5c89e4eSSatish Balay       ierr = MPI_Comm_free(&local_comm);CHKERRQ(ierr);
1305e5c89e4eSSatish Balay     }
1306e5c89e4eSSatish Balay   }
1307a64a8e02SBarry Smith 
13088bb29257SSatish Balay   {
1309e5c89e4eSSatish Balay     char fname[PETSC_MAX_PATH_LEN];
13100298fd71SBarry Smith     FILE *fd = NULL;
1311e5c89e4eSSatish Balay 
1312e5c89e4eSSatish Balay     fname[0] = 0;
1313a297a907SKarl Rupp 
13140298fd71SBarry Smith     ierr = PetscOptionsGetString(NULL,"-malloc_log",fname,250,&flg1);CHKERRQ(ierr);
13150298fd71SBarry Smith     ierr = PetscOptionsHasName(NULL,"-malloc_log_threshold",&flg2);CHKERRQ(ierr);
1316e5c89e4eSSatish Balay     if (flg1 && fname[0]) {
1317ed9cf6e9SBarry Smith       int err;
1318e5c89e4eSSatish Balay 
1319574034a9SJed Brown       if (!rank) {
1320574034a9SJed Brown         fd = fopen(fname,"w");
1321574034a9SJed Brown         if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open log file: %s",fname);
1322574034a9SJed Brown       }
1323e5c89e4eSSatish Balay       ierr = PetscMallocDumpLog(fd);CHKERRQ(ierr);
1324574034a9SJed Brown       if (fd) {
1325ed9cf6e9SBarry Smith         err = fclose(fd);
1326e32f2f54SBarry Smith         if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
1327574034a9SJed Brown       }
1328574034a9SJed Brown     } else if (flg1 || flg2) {
1329e5c89e4eSSatish Balay       ierr = PetscMallocDumpLog(stdout);CHKERRQ(ierr);
1330e5c89e4eSSatish Balay     }
1331e5c89e4eSSatish Balay   }
1332e5c89e4eSSatish Balay   /* Can be destroyed only after all the options are used */
1333e5c89e4eSSatish Balay   ierr = PetscOptionsDestroy();CHKERRQ(ierr);
1334e5c89e4eSSatish Balay 
1335e5c89e4eSSatish Balay   PetscGlobalArgc = 0;
1336e5c89e4eSSatish Balay   PetscGlobalArgs = 0;
1337e5c89e4eSSatish Balay 
1338ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
1339c90a1750SBarry Smith   ierr = MPI_Type_free(&MPIU___FLOAT128);CHKERRQ(ierr);
13407c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
13418c764dc5SJose Roman   ierr = MPI_Type_free(&MPIU___COMPLEX128);CHKERRQ(ierr);
13428c764dc5SJose Roman #endif
1343d9822059SBarry Smith   ierr = MPI_Op_free(&MPIU_MAX);CHKERRQ(ierr);
1344d9822059SBarry Smith   ierr = MPI_Op_free(&MPIU_MIN);CHKERRQ(ierr);
1345c90a1750SBarry Smith #endif
1346c90a1750SBarry Smith 
13477c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
13482c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
1349500d8756SSatish Balay   ierr = MPI_Type_free(&MPIU_C_DOUBLE_COMPLEX);CHKERRQ(ierr);
1350500d8756SSatish Balay   ierr = MPI_Type_free(&MPIU_C_COMPLEX);CHKERRQ(ierr);
13512c876bd9SBarry Smith #endif
1352e5c89e4eSSatish Balay #endif
1353cca4cb22SSatish Balay 
13547c2de775SJed Brown #if (defined(PETSC_HAVE_COMPLEX) && !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)) || defined(PETSC_USE_REAL___FLOAT128)
1355cca4cb22SSatish Balay   ierr = MPI_Op_free(&MPIU_SUM);CHKERRQ(ierr);
1356cca4cb22SSatish Balay #endif
1357cca4cb22SSatish Balay 
1358e5c89e4eSSatish Balay   ierr = MPI_Type_free(&MPIU_2SCALAR);CHKERRQ(ierr);
135944041f26SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
1360e5c89e4eSSatish Balay   ierr = MPI_Type_free(&MPIU_2INT);CHKERRQ(ierr);
136144041f26SJed Brown #endif
1362e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscMaxSum_Op);CHKERRQ(ierr);
1363e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscADMax_Op);CHKERRQ(ierr);
1364e5c89e4eSSatish Balay   ierr = MPI_Op_free(&PetscADMin_Op);CHKERRQ(ierr);
1365e5c89e4eSSatish Balay 
1366dbc8283eSBarry Smith   /*
1367efb80d3cSBarry Smith      Destroy any known inner MPI_Comm's and attributes pointing to them
1368efb80d3cSBarry Smith      Note this will not destroy any new communicators the user has created.
1369efb80d3cSBarry Smith 
1370efb80d3cSBarry Smith      If all PETSc objects were not destroyed those left over objects will have hanging references to
1371efb80d3cSBarry Smith      the MPI_Comms that were freed; but that is ok because those PETSc objects will never be used again
1372dbc8283eSBarry Smith  */
1373b770b1f6SSatish Balay   {
1374dbc8283eSBarry Smith     PetscCommCounter *counter;
1375dbc8283eSBarry Smith     PetscMPIInt      flg;
1376dbc8283eSBarry Smith     MPI_Comm         icomm;
1377265f3f35SJed Brown     union {MPI_Comm comm; void *ptr;} ucomm;
1378265f3f35SJed Brown     ierr = MPI_Attr_get(PETSC_COMM_SELF,Petsc_InnerComm_keyval,&ucomm,&flg);CHKERRQ(ierr);
1379dbc8283eSBarry Smith     if (flg) {
1380265f3f35SJed Brown       icomm = ucomm.comm;
1381dbc8283eSBarry Smith       ierr = MPI_Attr_get(icomm,Petsc_Counter_keyval,&counter,&flg);CHKERRQ(ierr);
1382dbc8283eSBarry 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");
1383dbc8283eSBarry Smith 
1384dbc8283eSBarry Smith       ierr = MPI_Attr_delete(PETSC_COMM_SELF,Petsc_InnerComm_keyval);CHKERRQ(ierr);
1385efb80d3cSBarry Smith       ierr = MPI_Attr_delete(icomm,Petsc_Counter_keyval);CHKERRQ(ierr);
1386efb80d3cSBarry Smith       ierr = MPI_Comm_free(&icomm);CHKERRQ(ierr);
1387dbc8283eSBarry Smith     }
1388265f3f35SJed Brown     ierr = MPI_Attr_get(PETSC_COMM_WORLD,Petsc_InnerComm_keyval,&ucomm,&flg);CHKERRQ(ierr);
1389dbc8283eSBarry Smith     if (flg) {
1390265f3f35SJed Brown       icomm = ucomm.comm;
1391dbc8283eSBarry Smith       ierr = MPI_Attr_get(icomm,Petsc_Counter_keyval,&counter,&flg);CHKERRQ(ierr);
1392dbc8283eSBarry 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");
1393dbc8283eSBarry Smith 
1394dbc8283eSBarry Smith       ierr = MPI_Attr_delete(PETSC_COMM_WORLD,Petsc_InnerComm_keyval);CHKERRQ(ierr);
1395efb80d3cSBarry Smith       ierr = MPI_Attr_delete(icomm,Petsc_Counter_keyval);CHKERRQ(ierr);
1396efb80d3cSBarry Smith       ierr = MPI_Comm_free(&icomm);CHKERRQ(ierr);
1397dbc8283eSBarry Smith     }
1398b770b1f6SSatish Balay   }
1399dbc8283eSBarry Smith 
1400480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_Counter_keyval);CHKERRQ(ierr);
1401480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_InnerComm_keyval);CHKERRQ(ierr);
1402480cf27aSJed Brown   ierr = MPI_Keyval_free(&Petsc_OuterComm_keyval);CHKERRQ(ierr);
1403480cf27aSJed Brown 
1404663ba86aSMatthew G Knepley #if defined(PETSC_HAVE_CUDA)
14054bb5149bSJed Brown   {
14064bb5149bSJed Brown     PetscInt p;
1407663ba86aSMatthew G Knepley     for (p = 0; p < PetscGlobalSize; ++p) {
1408a297a907SKarl Rupp       if (p == PetscGlobalRank) cublasShutdown();
1409663ba86aSMatthew G Knepley       ierr = MPI_Barrier(PETSC_COMM_WORLD);CHKERRQ(ierr);
1410663ba86aSMatthew G Knepley     }
14114bb5149bSJed Brown   }
1412663ba86aSMatthew G Knepley #endif
1413663ba86aSMatthew G Knepley 
1414e5c89e4eSSatish Balay   if (PetscBeganMPI) {
141599608316SBarry Smith #if defined(PETSC_HAVE_MPI_FINALIZED)
141699b1327fSBarry Smith     PetscMPIInt flag;
141799b1327fSBarry Smith     ierr = MPI_Finalized(&flag);CHKERRQ(ierr);
1418e32f2f54SBarry Smith     if (flag) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()");
141999608316SBarry Smith #endif
1420e5c89e4eSSatish Balay     ierr = MPI_Finalize();CHKERRQ(ierr);
1421e5c89e4eSSatish Balay   }
1422e5c89e4eSSatish Balay /*
1423e5c89e4eSSatish Balay 
1424e5c89e4eSSatish Balay      Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because
1425e5c89e4eSSatish Balay    the communicator has some outstanding requests on it. Specifically if the
1426e5c89e4eSSatish Balay    flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See
1427e5c89e4eSSatish Balay    src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate()
1428e5c89e4eSSatish Balay    is never freed as it should be. Thus one may obtain messages of the form
14290e5e90baSSatish Balay    [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the
1430e5c89e4eSSatish Balay    memory was not freed.
1431e5c89e4eSSatish Balay 
1432e5c89e4eSSatish Balay */
14331d1a0024SBarry Smith   ierr = PetscMallocClear();CHKERRQ(ierr);
1434a297a907SKarl Rupp 
1435e5c89e4eSSatish Balay   PetscInitializeCalled = PETSC_FALSE;
1436e5c89e4eSSatish Balay   PetscFinalizeCalled   = PETSC_TRUE;
1437e5c89e4eSSatish Balay   PetscFunctionReturn(ierr);
1438e5c89e4eSSatish Balay }
1439e5c89e4eSSatish Balay 
144043db4dbbSBarry Smith #if defined(PETSC_MISSING_LAPACK_lsame_)
14418cc058d9SJed Brown PETSC_EXTERN int lsame_(char *a,char *b)
144243db4dbbSBarry Smith {
144343db4dbbSBarry Smith   if (*a == *b) return 1;
144443db4dbbSBarry Smith   if (*a + 32 == *b) return 1;
144543db4dbbSBarry Smith   if (*a - 32 == *b) return 1;
144643db4dbbSBarry Smith   return 0;
144743db4dbbSBarry Smith }
1448a70650f6SBarry Smith #endif
144943db4dbbSBarry Smith 
145043db4dbbSBarry Smith #if defined(PETSC_MISSING_LAPACK_lsame)
14518cc058d9SJed Brown PETSC_EXTERN int lsame(char *a,char *b)
145243db4dbbSBarry Smith {
145343db4dbbSBarry Smith   if (*a == *b) return 1;
145443db4dbbSBarry Smith   if (*a + 32 == *b) return 1;
145543db4dbbSBarry Smith   if (*a - 32 == *b) return 1;
145643db4dbbSBarry Smith   return 0;
145743db4dbbSBarry Smith }
145843db4dbbSBarry Smith #endif
1459