1bc8a24ecSBarry Smith #define PETSC_DESIRE_FEATURE_TEST_MACROS 2e5c89e4eSSatish Balay /* 3e5c89e4eSSatish Balay This file defines the initialization of PETSc, including PetscInitialize() 4e5c89e4eSSatish Balay */ 5af0996ceSBarry Smith #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/ 6665c2dedSJed Brown #include <petscviewer.h> 762e5d2d2SJDBetteridge #include <petsc/private/garbagecollector.h> 8a0e72f99SJunchao Zhang 96edef35eSSatish Balay #if !defined(PETSC_HAVE_WINDOWS_COMPILERS) 106edef35eSSatish Balay #include <petsc/private/valgrind/valgrind.h> 116edef35eSSatish Balay #endif 126edef35eSSatish Balay 1385649d77SJunchao Zhang #if defined(PETSC_HAVE_FORTRAN) 1485649d77SJunchao Zhang #include <petsc/private/fortranimpl.h> 1585649d77SJunchao Zhang #endif 1685649d77SJunchao Zhang 177ce81a4bSJacob Faibussowitsch #if PetscDefined(USE_COVERAGE) 1856883f60SBarry Smith EXTERN_C_BEGIN 19aaf3846cSSatish Balay #if defined(PETSC_HAVE___GCOV_DUMP) 20aaf3846cSSatish Balay #define __gcov_flush(x) __gcov_dump(x) 21aaf3846cSSatish Balay #endif 2256883f60SBarry Smith void __gcov_flush(void); 2356883f60SBarry Smith EXTERN_C_END 2456883f60SBarry Smith #endif 258101f56cSMatthew Knepley 262d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS) 2795c0884eSLisandro Dalcin PETSC_INTERN PetscFPT PetscFPTData; 282d53ad75SBarry Smith PetscFPT PetscFPTData = 0; 292d53ad75SBarry Smith #endif 302d53ad75SBarry Smith 3127104ee2SJacob Faibussowitsch #if PetscDefined(HAVE_SAWS) 3216ad0300SBarry Smith #include <petscviewersaws.h> 33a6790183SBarry Smith #endif 34eb02082bSJunchao Zhang 35e5c89e4eSSatish Balay /* -----------------------------------------------------------------------------------------*/ 36e5c89e4eSSatish Balay 3795c0884eSLisandro Dalcin PETSC_INTERN FILE *petsc_history; 38e5c89e4eSSatish Balay 3995c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscInitialize_DynamicLibraries(void); 4095c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscFinalize_DynamicLibraries(void); 4195c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscSequentialPhaseBegin_Private(MPI_Comm, int); 4295c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscSequentialPhaseEnd_Private(MPI_Comm, int); 4395c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscCloseHistoryFile(FILE **); 440069ddf5SShri Abhyankar 456de5d289SStefano Zampini /* user may set these BEFORE calling PetscInitialize() */ 46e8373e55SMatthew Knepley MPI_Comm PETSC_COMM_WORLD = MPI_COMM_NULL; 4727104ee2SJacob Faibussowitsch #if PetscDefined(HAVE_MPI_INIT_THREAD) 486de5d289SStefano Zampini PetscMPIInt PETSC_MPI_THREAD_REQUIRED = MPI_THREAD_FUNNELED; 496de5d289SStefano Zampini #else 506de5d289SStefano Zampini PetscMPIInt PETSC_MPI_THREAD_REQUIRED = 0; 516de5d289SStefano Zampini #endif 52e5c89e4eSSatish Balay 53480cf27aSJed Brown PetscMPIInt Petsc_Counter_keyval = MPI_KEYVAL_INVALID; 54480cf27aSJed Brown PetscMPIInt Petsc_InnerComm_keyval = MPI_KEYVAL_INVALID; 55480cf27aSJed Brown PetscMPIInt Petsc_OuterComm_keyval = MPI_KEYVAL_INVALID; 565f7487a0SJunchao Zhang PetscMPIInt Petsc_ShmComm_keyval = MPI_KEYVAL_INVALID; 5762e5d2d2SJDBetteridge PetscMPIInt Petsc_CreationIdx_keyval = MPI_KEYVAL_INVALID; 5862e5d2d2SJDBetteridge PetscMPIInt Petsc_Garbage_HMap_keyval = MPI_KEYVAL_INVALID; 59480cf27aSJed Brown 60e5c89e4eSSatish Balay /* 61e5c89e4eSSatish Balay Declare and set all the string names of the PETSc enums 62e5c89e4eSSatish Balay */ 6302c9f0b5SLisandro Dalcin const char *const PetscBools[] = {"FALSE", "TRUE", "PetscBool", "PETSC_", NULL}; 6402c9f0b5SLisandro Dalcin const char *const PetscCopyModes[] = {"COPY_VALUES", "OWN_POINTER", "USE_POINTER", "PetscCopyMode", "PETSC_", NULL}; 65e5c89e4eSSatish Balay 66ace3abfcSBarry Smith PetscBool PetscPreLoadingUsed = PETSC_FALSE; 67ace3abfcSBarry Smith PetscBool PetscPreLoadingOn = PETSC_FALSE; 680f8e0872SSatish Balay 69a2f94806SJed Brown PetscInt PetscHotRegionDepth; 70a2f94806SJed Brown 716edef35eSSatish Balay PetscBool PETSC_RUNNING_ON_VALGRIND = PETSC_FALSE; 726edef35eSSatish Balay 73b22622e2STadeu Manoel #if defined(PETSC_HAVE_THREADSAFETY) 74b22622e2STadeu Manoel PetscSpinlock PetscViewerASCIISpinLockOpen; 75b22622e2STadeu Manoel PetscSpinlock PetscViewerASCIISpinLockStdout; 76b22622e2STadeu Manoel PetscSpinlock PetscViewerASCIISpinLockStderr; 77b22622e2STadeu Manoel PetscSpinlock PetscCommSpinLock; 78b22622e2STadeu Manoel #endif 79b22622e2STadeu Manoel 80e5c89e4eSSatish Balay /* 81945d1669SBarry Smith PetscInitializeNoPointers - Calls PetscInitialize() from C/C++ without the pointers to argc and args 8272a42c3cSBarry Smith 8372a42c3cSBarry Smith Collective 8472a42c3cSBarry Smith 8572a42c3cSBarry Smith Level: advanced 8672a42c3cSBarry Smith 8795452b02SPatrick Sanan Notes: 88a56f64adSBarry Smith this is called only by the PETSc Julia interface. Even though it might start MPI it sets the flag to 890f11a792SBarry Smith indicate that it did NOT start MPI so that the PetscFinalize() does not end MPI, thus allowing PetscInitialize() to 90a56f64adSBarry Smith be called multiple times from Julia without the problem of trying to initialize MPI more than once. 910f11a792SBarry Smith 92a56f64adSBarry Smith Developer Note: Turns off PETSc signal handling to allow Julia to manage signals 931ea5a559SBarry Smith 94db781477SPatrick Sanan .seealso: `PetscInitialize()`, `PetscInitializeFortran()`, `PetscInitializeNoArguments()` 950f11a792SBarry Smith */ 96d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscInitializeNoPointers(int argc, char **args, const char *filename, const char *help) 97d71ae5a4SJacob Faibussowitsch { 9872a42c3cSBarry Smith int myargc = argc; 9972a42c3cSBarry Smith char **myargs = args; 10072a42c3cSBarry Smith 10172a42c3cSBarry Smith PetscFunctionBegin; 1029566063dSJacob Faibussowitsch PetscCall(PetscInitialize(&myargc, &myargs, filename, help)); 1039566063dSJacob Faibussowitsch PetscCall(PetscPopSignalHandler()); 104df413903SBarry Smith PetscBeganMPI = PETSC_FALSE; 105*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 10672a42c3cSBarry Smith } 10772a42c3cSBarry Smith 108f0865b08SBarry Smith /* 109a56f64adSBarry Smith Used by Julia interface to get communicator 110f0865b08SBarry Smith */ 111d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGetPETSC_COMM_SELF(MPI_Comm *comm) 112d71ae5a4SJacob Faibussowitsch { 113f0865b08SBarry Smith PetscFunctionBegin; 11427104ee2SJacob Faibussowitsch if (PetscInitializeCalled) PetscValidPointer(comm, 1); 115f0865b08SBarry Smith *comm = PETSC_COMM_SELF; 116*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 117f0865b08SBarry Smith } 118f0865b08SBarry Smith 119e5c89e4eSSatish Balay /*@C 120811af0c4SBarry Smith PetscInitializeNoArguments - Calls `PetscInitialize()` from C/C++ without 121e5c89e4eSSatish Balay the command line arguments. 122e5c89e4eSSatish Balay 123e5c89e4eSSatish Balay Collective 124e5c89e4eSSatish Balay 125e5c89e4eSSatish Balay Level: advanced 126e5c89e4eSSatish Balay 127db781477SPatrick Sanan .seealso: `PetscInitialize()`, `PetscInitializeFortran()` 128e5c89e4eSSatish Balay @*/ 129d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscInitializeNoArguments(void) 130d71ae5a4SJacob Faibussowitsch { 131e5c89e4eSSatish Balay int argc = 0; 13202c9f0b5SLisandro Dalcin char **args = NULL; 133e5c89e4eSSatish Balay 134e5c89e4eSSatish Balay PetscFunctionBegin; 1359566063dSJacob Faibussowitsch PetscCall(PetscInitialize(&argc, &args, NULL, NULL)); 136*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 137e5c89e4eSSatish Balay } 138e5c89e4eSSatish Balay 139e5c89e4eSSatish Balay /*@ 140e5c89e4eSSatish Balay PetscInitialized - Determine whether PETSc is initialized. 141e5c89e4eSSatish Balay 14293b6d2d1SJed Brown Level: beginner 143e5c89e4eSSatish Balay 144db781477SPatrick Sanan .seealso: `PetscInitialize()`, `PetscInitializeNoArguments()`, `PetscInitializeFortran()` 145e5c89e4eSSatish Balay @*/ 146d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscInitialized(PetscBool *isInitialized) 147d71ae5a4SJacob Faibussowitsch { 14827104ee2SJacob Faibussowitsch PetscFunctionBegin; 14927104ee2SJacob Faibussowitsch if (PetscInitializeCalled) PetscValidBoolPointer(isInitialized, 1); 150e5c89e4eSSatish Balay *isInitialized = PetscInitializeCalled; 151*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 152e5c89e4eSSatish Balay } 153e5c89e4eSSatish Balay 154e5c89e4eSSatish Balay /*@ 155811af0c4SBarry Smith PetscFinalized - Determine whether `PetscFinalize()` has been called yet 156e5c89e4eSSatish Balay 157e5c89e4eSSatish Balay Level: developer 158e5c89e4eSSatish Balay 159db781477SPatrick Sanan .seealso: `PetscInitialize()`, `PetscInitializeNoArguments()`, `PetscInitializeFortran()` 160e5c89e4eSSatish Balay @*/ 161d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFinalized(PetscBool *isFinalized) 162d71ae5a4SJacob Faibussowitsch { 16327104ee2SJacob Faibussowitsch PetscFunctionBegin; 16427104ee2SJacob Faibussowitsch if (!PetscFinalizeCalled) PetscValidBoolPointer(isFinalized, 1); 165e5c89e4eSSatish Balay *isFinalized = PetscFinalizeCalled; 166*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 167e5c89e4eSSatish Balay } 168e5c89e4eSSatish Balay 16957171095SVaclav Hapla PETSC_INTERN PetscErrorCode PetscOptionsCheckInitial_Private(const char[]); 170e5c89e4eSSatish Balay 171e5c89e4eSSatish Balay /* 172e5c89e4eSSatish Balay This function is the MPI reduction operation used to compute the sum of the 173e5c89e4eSSatish Balay first half of the datatype and the max of the second half. 174e5c89e4eSSatish Balay */ 175367daffbSBarry Smith MPI_Op MPIU_MAXSUM_OP = 0; 17662e5d2d2SJDBetteridge MPI_Op Petsc_Garbage_SetIntersectOp = 0; 177e5c89e4eSSatish Balay 178d71ae5a4SJacob Faibussowitsch PETSC_INTERN void MPIAPI MPIU_MaxSum_Local(void *in, void *out, int *cnt, MPI_Datatype *datatype) 179d71ae5a4SJacob Faibussowitsch { 180e5c89e4eSSatish Balay PetscInt *xin = (PetscInt *)in, *xout = (PetscInt *)out, i, count = *cnt; 181e5c89e4eSSatish Balay 182e5c89e4eSSatish Balay PetscFunctionBegin; 183e5c89e4eSSatish Balay if (*datatype != MPIU_2INT) { 184*3ba16761SJacob Faibussowitsch PetscErrorCode ierr = (*PetscErrorPrintf)("Can only handle MPIU_2INT data types"); 185*3ba16761SJacob Faibussowitsch (void)ierr; 18641e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_SELF, PETSC_ERR_ARG_WRONG); 187e5c89e4eSSatish Balay } 188e5c89e4eSSatish Balay 189e5c89e4eSSatish Balay for (i = 0; i < count; i++) { 190e5c89e4eSSatish Balay xout[2 * i] = PetscMax(xout[2 * i], xin[2 * i]); 191e5c89e4eSSatish Balay xout[2 * i + 1] += xin[2 * i + 1]; 192e5c89e4eSSatish Balay } 193812af9f3SBarry Smith PetscFunctionReturnVoid(); 194e5c89e4eSSatish Balay } 195e5c89e4eSSatish Balay 196e5c89e4eSSatish Balay /* 197e5c89e4eSSatish Balay Returns the max of the first entry owned by this processor and the 198e5c89e4eSSatish Balay sum of the second entry. 199b693b147SBarry Smith 20076f543a4SJed Brown The reason sizes[2*i] contains lengths sizes[2*i+1] contains flag of 1 if length is nonzero 201367daffbSBarry Smith is so that the MPIU_MAXSUM_OP() can set TWO values, if we passed in only sizes[i] with lengths 202b693b147SBarry Smith there would be no place to store the both needed results. 203e5c89e4eSSatish Balay */ 204d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscMaxSum(MPI_Comm comm, const PetscInt sizes[], PetscInt *max, PetscInt *sum) 205d71ae5a4SJacob Faibussowitsch { 206e5c89e4eSSatish Balay PetscFunctionBegin; 207d6e4c47cSJed Brown #if defined(PETSC_HAVE_MPI_REDUCE_SCATTER_BLOCK) 208d6e4c47cSJed Brown { 2099371c9d4SSatish Balay struct { 2109371c9d4SSatish Balay PetscInt max, sum; 2119371c9d4SSatish Balay } work; 2129566063dSJacob Faibussowitsch PetscCallMPI(MPI_Reduce_scatter_block((void *)sizes, &work, 1, MPIU_2INT, MPIU_MAXSUM_OP, comm)); 213d6e4c47cSJed Brown *max = work.max; 214d6e4c47cSJed Brown *sum = work.sum; 215d6e4c47cSJed Brown } 216d6e4c47cSJed Brown #else 217d6e4c47cSJed Brown { 218d6e4c47cSJed Brown PetscMPIInt size, rank; 2199371c9d4SSatish Balay struct { 2209371c9d4SSatish Balay PetscInt max, sum; 2219371c9d4SSatish Balay } *work; 2229566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 2239566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 2249566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(size, &work)); 2251c2dc1cbSBarry Smith PetscCall(MPIU_Allreduce((void *)sizes, work, size, MPIU_2INT, MPIU_MAXSUM_OP, comm)); 2266ac3741eSJed Brown *max = work[rank].max; 2276ac3741eSJed Brown *sum = work[rank].sum; 2289566063dSJacob Faibussowitsch PetscCall(PetscFree(work)); 229d6e4c47cSJed Brown } 230d6e4c47cSJed Brown #endif 231*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 232e5c89e4eSSatish Balay } 233e5c89e4eSSatish Balay 234e5c89e4eSSatish Balay /* ----------------------------------------------------------------------------*/ 235e5c89e4eSSatish Balay 2369e517322SPierre Jolivet #if defined(PETSC_HAVE_REAL___FLOAT128) || defined(PETSC_HAVE_REAL___FP16) 237613bf2b2SPierre Jolivet #if defined(PETSC_HAVE_REAL___FLOAT128) 238613bf2b2SPierre Jolivet #include <quadmath.h> 239613bf2b2SPierre Jolivet #endif 2409e517322SPierre Jolivet MPI_Op MPIU_SUM___FP16___FLOAT128 = 0; 241de272c7aSSatish Balay #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16) 24206a205a8SBarry Smith MPI_Op MPIU_SUM = 0; 243613bf2b2SPierre Jolivet #endif 244e5c89e4eSSatish Balay 245d71ae5a4SJacob Faibussowitsch PETSC_EXTERN void MPIAPI PetscSum_Local(void *in, void *out, PetscMPIInt *cnt, MPI_Datatype *datatype) 246d71ae5a4SJacob Faibussowitsch { 247e5c89e4eSSatish Balay PetscInt i, count = *cnt; 248e5c89e4eSSatish Balay 249e5c89e4eSSatish Balay PetscFunctionBegin; 2507c2de775SJed Brown if (*datatype == MPIU_REAL) { 251e2e03761SBarry Smith PetscReal *xin = (PetscReal *)in, *xout = (PetscReal *)out; 252a297a907SKarl Rupp for (i = 0; i < count; i++) xout[i] += xin[i]; 2537c2de775SJed Brown } 2547c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX) 255c5481aeeSJose E. Roman else if (*datatype == MPIU_COMPLEX) { 2567c2de775SJed Brown PetscComplex *xin = (PetscComplex *)in, *xout = (PetscComplex *)out; 257a297a907SKarl Rupp for (i = 0; i < count; i++) xout[i] += xin[i]; 2587c2de775SJed Brown } 2597c2de775SJed Brown #endif 260613bf2b2SPierre Jolivet #if defined(PETSC_HAVE_REAL___FLOAT128) 261613bf2b2SPierre Jolivet else if (*datatype == MPIU___FLOAT128) { 262613bf2b2SPierre Jolivet __float128 *xin = (__float128 *)in, *xout = (__float128 *)out; 263613bf2b2SPierre Jolivet for (i = 0; i < count; i++) xout[i] += xin[i]; 2649371c9d4SSatish Balay } else if (*datatype == MPIU___COMPLEX128) { 265613bf2b2SPierre Jolivet __complex128 *xin = (__complex128 *)in, *xout = (__complex128 *)out; 266613bf2b2SPierre Jolivet for (i = 0; i < count; i++) xout[i] += xin[i]; 267613bf2b2SPierre Jolivet } 268613bf2b2SPierre Jolivet #endif 2699e517322SPierre Jolivet #if defined(PETSC_HAVE_REAL___FP16) 2709e517322SPierre Jolivet else if (*datatype == MPIU___FP16) { 2719e517322SPierre Jolivet __fp16 *xin = (__fp16 *)in, *xout = (__fp16 *)out; 2729e517322SPierre Jolivet for (i = 0; i < count; i++) xout[i] += xin[i]; 2739e517322SPierre Jolivet } 2749e517322SPierre Jolivet #endif 2757c2de775SJed Brown else { 2769e517322SPierre Jolivet #if !defined(PETSC_HAVE_REAL___FLOAT128) && !defined(PETSC_HAVE_REAL___FP16) 277*3ba16761SJacob Faibussowitsch PetscCallAbort(MPI_COMM_SElF, (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types")); 2789e517322SPierre Jolivet #elif !defined(PETSC_HAVE_REAL___FP16) 279*3ba16761SJacob Faibussowitsch PetscCallAbort(MPI_COMM_SELF, (*PetscErrorPrintf)("Can only handle MPIU_REAL, MPIU_COMPLEX, MPIU___FLOAT128, or MPIU___COMPLEX128 data types")); 2809e517322SPierre Jolivet #elif !defined(PETSC_HAVE_REAL___FLOAT128) 281*3ba16761SJacob Faibussowitsch PetscCallAbort(MPI_COMM_SELF, (*PetscErrorPrintf)("Can only handle MPIU_REAL, MPIU_COMPLEX, or MPIU___FP16 data types")); 2829e517322SPierre Jolivet #else 283*3ba16761SJacob Faibussowitsch PetscCallAbort(MPI_COMM_SELF, (*PetscErrorPrintf)("Can only handle MPIU_REAL, MPIU_COMPLEX, MPIU___FLOAT128, MPIU___COMPLEX128, or MPIU___FP16 data types")); 284613bf2b2SPierre Jolivet #endif 28541e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_SELF, PETSC_ERR_ARG_WRONG); 286e2e03761SBarry Smith } 287812af9f3SBarry Smith PetscFunctionReturnVoid(); 288e5c89e4eSSatish Balay } 289e5c89e4eSSatish Balay #endif 290e5c89e4eSSatish Balay 291570b7f6dSBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16) 292d9822059SBarry Smith MPI_Op MPIU_MAX = 0; 293d9822059SBarry Smith MPI_Op MPIU_MIN = 0; 294d9822059SBarry Smith 295d71ae5a4SJacob Faibussowitsch PETSC_EXTERN void MPIAPI PetscMax_Local(void *in, void *out, PetscMPIInt *cnt, MPI_Datatype *datatype) 296d71ae5a4SJacob Faibussowitsch { 297d9822059SBarry Smith PetscInt i, count = *cnt; 298d9822059SBarry Smith 299d9822059SBarry Smith PetscFunctionBegin; 3007c2de775SJed Brown if (*datatype == MPIU_REAL) { 3018c764dc5SJose Roman PetscReal *xin = (PetscReal *)in, *xout = (PetscReal *)out; 302a297a907SKarl Rupp for (i = 0; i < count; i++) xout[i] = PetscMax(xout[i], xin[i]); 3037c2de775SJed Brown } 3047c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX) 3057c2de775SJed Brown else if (*datatype == MPIU_COMPLEX) { 3067c2de775SJed Brown PetscComplex *xin = (PetscComplex *)in, *xout = (PetscComplex *)out; 307ad540459SPierre Jolivet for (i = 0; i < count; i++) xout[i] = PetscRealPartComplex(xout[i]) < PetscRealPartComplex(xin[i]) ? xin[i] : xout[i]; 3087c2de775SJed Brown } 3097c2de775SJed Brown #endif 3107c2de775SJed Brown else { 311*3ba16761SJacob Faibussowitsch PetscCallAbort(MPI_COMM_SELF, (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_COMPLEX data types")); 31241e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_SELF, PETSC_ERR_ARG_WRONG); 3138c764dc5SJose Roman } 314d9822059SBarry Smith PetscFunctionReturnVoid(); 315d9822059SBarry Smith } 316d9822059SBarry Smith 317d71ae5a4SJacob Faibussowitsch PETSC_EXTERN void MPIAPI PetscMin_Local(void *in, void *out, PetscMPIInt *cnt, MPI_Datatype *datatype) 318d71ae5a4SJacob Faibussowitsch { 319d9822059SBarry Smith PetscInt i, count = *cnt; 320d9822059SBarry Smith 321d9822059SBarry Smith PetscFunctionBegin; 3227c2de775SJed Brown if (*datatype == MPIU_REAL) { 3238c764dc5SJose Roman PetscReal *xin = (PetscReal *)in, *xout = (PetscReal *)out; 324a297a907SKarl Rupp for (i = 0; i < count; i++) xout[i] = PetscMin(xout[i], xin[i]); 3257c2de775SJed Brown } 3267c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX) 3277c2de775SJed Brown else if (*datatype == MPIU_COMPLEX) { 3287c2de775SJed Brown PetscComplex *xin = (PetscComplex *)in, *xout = (PetscComplex *)out; 329ad540459SPierre Jolivet for (i = 0; i < count; i++) xout[i] = PetscRealPartComplex(xout[i]) > PetscRealPartComplex(xin[i]) ? xin[i] : xout[i]; 3307c2de775SJed Brown } 3317c2de775SJed Brown #endif 3327c2de775SJed Brown else { 333*3ba16761SJacob Faibussowitsch PetscCallAbort(MPI_COMM_SELF, (*PetscErrorPrintf)("Can only handle MPIU_REAL or MPIU_SCALAR data (i.e. double or complex) types")); 33441e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_SELF, PETSC_ERR_ARG_WRONG); 3358c764dc5SJose Roman } 336d9822059SBarry Smith PetscFunctionReturnVoid(); 337d9822059SBarry Smith } 338d9822059SBarry Smith #endif 339d9822059SBarry Smith 340480cf27aSJed Brown /* 341480cf27aSJed Brown Private routine to delete internal tag/name counter storage when a communicator is freed. 342480cf27aSJed Brown 343ff0e51ddSBarry 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. 344480cf27aSJed Brown 34512801b39SBarry Smith Note: this is declared extern "C" because it is passed to MPI_Comm_create_keyval() 346480cf27aSJed Brown 347480cf27aSJed Brown */ 348d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscMPIInt MPIAPI Petsc_Counter_Attr_Delete_Fn(MPI_Comm comm, PetscMPIInt keyval, void *count_val, void *extra_state) 349d71ae5a4SJacob Faibussowitsch { 35005342407SJunchao Zhang PetscCommCounter *counter = (PetscCommCounter *)count_val; 35157f21012SBarry Smith struct PetscCommStash *comms = counter->comms, *pcomm; 352480cf27aSJed Brown 353480cf27aSJed Brown PetscFunctionBegin; 3549566063dSJacob Faibussowitsch PetscCallMPI(PetscInfo(NULL, "Deleting counter data in an MPI_Comm %ld\n", (long)comm)); 3559566063dSJacob Faibussowitsch PetscCallMPI(PetscFree(counter->iflags)); 35657f21012SBarry Smith while (comms) { 3579566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_free(&comms->comm)); 35857f21012SBarry Smith pcomm = comms; 35957f21012SBarry Smith comms = comms->next; 3609566063dSJacob Faibussowitsch PetscCall(PetscFree(pcomm)); 36157f21012SBarry Smith } 3629566063dSJacob Faibussowitsch PetscCallMPI(PetscFree(counter)); 363480cf27aSJed Brown PetscFunctionReturn(MPI_SUCCESS); 364480cf27aSJed Brown } 365480cf27aSJed Brown 366480cf27aSJed Brown /* 36747435625SJed Brown This is invoked on the outer comm as a result of either PetscCommDestroy() (via MPI_Comm_delete_attr) or when the user 368da3039f7SJed Brown calls MPI_Comm_free(). 369da3039f7SJed Brown 370da3039f7SJed Brown This is the only entry point for breaking the links between inner and outer comms. 371480cf27aSJed Brown 372ff0e51ddSBarry Smith This is called by MPI, not by users. This is called when MPI_Comm_free() is called on the communicator. 373480cf27aSJed Brown 37412801b39SBarry Smith Note: this is declared extern "C" because it is passed to MPI_Comm_create_keyval() 375480cf27aSJed Brown 376480cf27aSJed Brown */ 377d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscMPIInt MPIAPI Petsc_InnerComm_Attr_Delete_Fn(MPI_Comm comm, PetscMPIInt keyval, void *attr_val, void *extra_state) 378d71ae5a4SJacob Faibussowitsch { 3799371c9d4SSatish Balay union 380480cf27aSJed Brown { 3819371c9d4SSatish Balay MPI_Comm comm; 3829371c9d4SSatish Balay void *ptr; 3839371c9d4SSatish Balay } icomm; 384480cf27aSJed Brown 385480cf27aSJed Brown PetscFunctionBegin; 38612801b39SBarry Smith if (keyval != Petsc_InnerComm_keyval) SETERRMPI(PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Unexpected keyval"); 387ec4fadc2SJed Brown icomm.ptr = attr_val; 38876bd3646SJed Brown if (PetscDefined(USE_DEBUG)) { 38933779a13SJunchao Zhang /* Error out if the inner/outer comms are not correctly linked through their Outer/InnterComm attributes */ 39033779a13SJunchao Zhang PetscMPIInt flg; 3919371c9d4SSatish Balay union 3929371c9d4SSatish Balay { 3939371c9d4SSatish Balay MPI_Comm comm; 3949371c9d4SSatish Balay void *ptr; 3959371c9d4SSatish Balay } ocomm; 3969566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_get_attr(icomm.comm, Petsc_OuterComm_keyval, &ocomm, &flg)); 39733779a13SJunchao Zhang if (!flg) SETERRMPI(PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Inner comm does not have OuterComm attribute"); 39833779a13SJunchao Zhang if (ocomm.comm != comm) SETERRMPI(PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Inner comm's OuterComm attribute does not point to outer PETSc comm"); 39933779a13SJunchao Zhang } 4009566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_delete_attr(icomm.comm, Petsc_OuterComm_keyval)); 4019566063dSJacob Faibussowitsch PetscCallMPI(PetscInfo(NULL, "User MPI_Comm %ld is being unlinked from inner PETSc comm %ld\n", (long)comm, (long)icomm.comm)); 402da3039f7SJed Brown PetscFunctionReturn(MPI_SUCCESS); 403b89831e5SBarry Smith } 404da3039f7SJed Brown 405da3039f7SJed Brown /* 40633779a13SJunchao Zhang * This is invoked on the inner comm when Petsc_InnerComm_Attr_Delete_Fn calls MPI_Comm_delete_attr(). It should not be reached any other way. 407da3039f7SJed Brown */ 408d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscMPIInt MPIAPI Petsc_OuterComm_Attr_Delete_Fn(MPI_Comm comm, PetscMPIInt keyval, void *attr_val, void *extra_state) 409d71ae5a4SJacob Faibussowitsch { 410da3039f7SJed Brown PetscFunctionBegin; 4119566063dSJacob Faibussowitsch PetscCallMPI(PetscInfo(NULL, "Removing reference to PETSc communicator embedded in a user MPI_Comm %ld\n", (long)comm)); 412480cf27aSJed Brown PetscFunctionReturn(MPI_SUCCESS); 413480cf27aSJed Brown } 414480cf27aSJed Brown 41533779a13SJunchao Zhang PETSC_EXTERN PetscMPIInt MPIAPI Petsc_ShmComm_Attr_Delete_Fn(MPI_Comm, PetscMPIInt, void *, void *); 41642218b76SBarry Smith 417951e3c8eSBarry Smith #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32) 4188cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_extent_fn(MPI_Datatype, MPI_Aint *, void *); 4198cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_read_conv_fn(void *, MPI_Datatype, PetscMPIInt, void *, MPI_Offset, void *); 4208cc058d9SJed Brown PETSC_EXTERN PetscMPIInt PetscDataRep_write_conv_fn(void *, MPI_Datatype, PetscMPIInt, void *, MPI_Offset, void *); 421e39fd77fSBarry Smith #endif 422e39fd77fSBarry Smith 4230f9be574SPeter Hill PetscMPIInt PETSC_MPI_ERROR_CLASS = MPI_ERR_LASTCODE, PETSC_MPI_ERROR_CODE; 42412801b39SBarry Smith 425eb27c7c8SSatish Balay PETSC_INTERN int PetscGlobalArgc; 426eb27c7c8SSatish Balay PETSC_INTERN char **PetscGlobalArgs; 4276ae9a8a6SBarry Smith int PetscGlobalArgc = 0; 42802c9f0b5SLisandro Dalcin char **PetscGlobalArgs = NULL; 429dff31646SBarry Smith PetscSegBuffer PetscCitationsList; 430e5c89e4eSSatish Balay 431d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscCitationsInitialize(void) 432d71ae5a4SJacob Faibussowitsch { 433051e4cf2SJed Brown PetscFunctionBegin; 4349566063dSJacob Faibussowitsch PetscCall(PetscSegBufferCreate(1, 10000, &PetscCitationsList)); 43530c35bf2SSatish Balay 43630c35bf2SSatish Balay PetscCall(PetscCitationsRegister("@TechReport{petsc-user-ref,\n\ 43730c35bf2SSatish Balay Author = {Satish Balay and Shrirang Abhyankar and Mark~F. Adams and Steven Benson and Jed Brown\n\ 43830c35bf2SSatish Balay and Peter Brune and Kris Buschelman and Emil Constantinescu and Lisandro Dalcin and Alp Dener\n\ 4393f81df79SBarry Smith and Victor Eijkhout and Jacob Faibussowitsch and William~D. Gropp and V\'{a}clav Hapla and Tobin Isaac and Pierre Jolivet\n\ 44030c35bf2SSatish Balay and Dmitry Karpeev and Dinesh Kaushik and Matthew~G. Knepley and Fande Kong and Scott Kruger\n\ 44130c35bf2SSatish Balay and Dave~A. May and Lois Curfman McInnes and Richard Tran Mills and Lawrence Mitchell and Todd Munson\n\ 44230c35bf2SSatish Balay and Jose~E. Roman and Karl Rupp and Patrick Sanan and Jason Sarich and Barry~F. Smith\n\ 44330c35bf2SSatish Balay and Stefano Zampini and Hong Zhang and Hong Zhang and Junchao Zhang},\n\ 44430c35bf2SSatish Balay Title = {{PETSc/TAO} Users Manual},\n\ 4458f23a7d0SSatish Balay Number = {ANL-21/39 - Revision 3.18},\n\ 44630c35bf2SSatish Balay Institution = {Argonne National Laboratory},\n\ 4479371c9d4SSatish Balay Year = {2022}\n}\n", 4489371c9d4SSatish Balay NULL)); 44930c35bf2SSatish Balay 45030c35bf2SSatish Balay PetscCall(PetscCitationsRegister("@InProceedings{petsc-efficient,\n\ 45130c35bf2SSatish Balay Author = {Satish Balay and William D. Gropp and Lois Curfman McInnes and Barry F. Smith},\n\ 45230c35bf2SSatish Balay Title = {Efficient Management of Parallelism in Object Oriented Numerical Software Libraries},\n\ 45330c35bf2SSatish Balay Booktitle = {Modern Software Tools in Scientific Computing},\n\ 45430c35bf2SSatish Balay Editor = {E. Arge and A. M. Bruaset and H. P. Langtangen},\n\ 45530c35bf2SSatish Balay Pages = {163--202},\n\ 45630c35bf2SSatish Balay Publisher = {Birkh{\\\"{a}}user Press},\n\ 4579371c9d4SSatish Balay Year = {1997}\n}\n", 4589371c9d4SSatish Balay NULL)); 45930c35bf2SSatish Balay 460*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 461051e4cf2SJed Brown } 462e5c89e4eSSatish Balay 4632d747510SLisandro Dalcin static char programname[PETSC_MAX_PATH_LEN] = ""; /* HP includes entire path in name */ 4642d747510SLisandro Dalcin 465d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscSetProgramName(const char name[]) 466d71ae5a4SJacob Faibussowitsch { 4672d747510SLisandro Dalcin PetscFunctionBegin; 4689566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(programname, name, sizeof(programname))); 469*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4702d747510SLisandro Dalcin } 4712d747510SLisandro Dalcin 4722d747510SLisandro Dalcin /*@C 4732d747510SLisandro Dalcin PetscGetProgramName - Gets the name of the running program. 4742d747510SLisandro Dalcin 4752d747510SLisandro Dalcin Not Collective 4762d747510SLisandro Dalcin 4772d747510SLisandro Dalcin Input Parameter: 4782d747510SLisandro Dalcin . len - length of the string name 4792d747510SLisandro Dalcin 4802d747510SLisandro Dalcin Output Parameter: 481811af0c4SBarry Smith . name - the name of the running program, provide a string of length `PETSC_MAX_PATH_LEN` 4822d747510SLisandro Dalcin 4832d747510SLisandro Dalcin Level: advanced 4842d747510SLisandro Dalcin 4852d747510SLisandro Dalcin @*/ 486d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGetProgramName(char name[], size_t len) 487d71ae5a4SJacob Faibussowitsch { 4882d747510SLisandro Dalcin PetscFunctionBegin; 4899566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(name, programname, len)); 490*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4912d747510SLisandro Dalcin } 4922d747510SLisandro Dalcin 493e5c89e4eSSatish Balay /*@C 494e5c89e4eSSatish Balay PetscGetArgs - Allows you to access the raw command line arguments anywhere 495811af0c4SBarry Smith after PetscInitialize() is called but before `PetscFinalize()`. 496e5c89e4eSSatish Balay 497e5c89e4eSSatish Balay Not Collective 498e5c89e4eSSatish Balay 499e5c89e4eSSatish Balay Output Parameters: 500e5c89e4eSSatish Balay + argc - count of number of command line arguments 501e5c89e4eSSatish Balay - args - the command line arguments 502e5c89e4eSSatish Balay 503e5c89e4eSSatish Balay Level: intermediate 504e5c89e4eSSatish Balay 505e5c89e4eSSatish Balay Notes: 506e5c89e4eSSatish Balay This is usually used to pass the command line arguments into other libraries 507e5c89e4eSSatish Balay that are called internally deep in PETSc or the application. 508e5c89e4eSSatish Balay 509f177e3b1SBarry Smith The first argument contains the program name as is normal for C arguments. 510f177e3b1SBarry Smith 511db781477SPatrick Sanan .seealso: `PetscFinalize()`, `PetscInitializeFortran()`, `PetscGetArguments()` 512e5c89e4eSSatish Balay @*/ 513d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGetArgs(int *argc, char ***args) 514d71ae5a4SJacob Faibussowitsch { 515e5c89e4eSSatish Balay PetscFunctionBegin; 51639a651e2SJacob Faibussowitsch PetscCheck(PetscInitializeCalled || !PetscFinalizeCalled, PETSC_COMM_SELF, PETSC_ERR_ORDER, "You must call after PetscInitialize() but before PetscFinalize()"); 517e5c89e4eSSatish Balay *argc = PetscGlobalArgc; 518e5c89e4eSSatish Balay *args = PetscGlobalArgs; 519*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 520e5c89e4eSSatish Balay } 521e5c89e4eSSatish Balay 522793721a6SBarry Smith /*@C 523793721a6SBarry Smith PetscGetArguments - Allows you to access the command line arguments anywhere 524811af0c4SBarry Smith after `PetscInitialize()` is called but before `PetscFinalize()`. 525793721a6SBarry Smith 526793721a6SBarry Smith Not Collective 527793721a6SBarry Smith 528793721a6SBarry Smith Output Parameters: 529793721a6SBarry Smith . args - the command line arguments 530793721a6SBarry Smith 531793721a6SBarry Smith Level: intermediate 532793721a6SBarry Smith 533793721a6SBarry Smith Notes: 534793721a6SBarry Smith This does NOT start with the program name and IS null terminated (final arg is void) 535793721a6SBarry Smith 536db781477SPatrick Sanan .seealso: `PetscFinalize()`, `PetscInitializeFortran()`, `PetscGetArgs()`, `PetscFreeArguments()` 537793721a6SBarry Smith @*/ 538d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGetArguments(char ***args) 539d71ae5a4SJacob Faibussowitsch { 540793721a6SBarry Smith PetscInt i, argc = PetscGlobalArgc; 541793721a6SBarry Smith 542793721a6SBarry Smith PetscFunctionBegin; 54339a651e2SJacob Faibussowitsch PetscCheck(PetscInitializeCalled || !PetscFinalizeCalled, PETSC_COMM_SELF, PETSC_ERR_ORDER, "You must call after PetscInitialize() but before PetscFinalize()"); 5449371c9d4SSatish Balay if (!argc) { 5459371c9d4SSatish Balay *args = NULL; 546*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5479371c9d4SSatish Balay } 5489566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(argc, args)); 5499566063dSJacob Faibussowitsch for (i = 0; i < argc - 1; i++) PetscCall(PetscStrallocpy(PetscGlobalArgs[i + 1], &(*args)[i])); 5502d747510SLisandro Dalcin (*args)[argc - 1] = NULL; 551*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 552793721a6SBarry Smith } 553793721a6SBarry Smith 554793721a6SBarry Smith /*@C 555811af0c4SBarry Smith PetscFreeArguments - Frees the memory obtained with `PetscGetArguments()` 556793721a6SBarry Smith 557793721a6SBarry Smith Not Collective 558793721a6SBarry Smith 559793721a6SBarry Smith Output Parameters: 560793721a6SBarry Smith . args - the command line arguments 561793721a6SBarry Smith 562793721a6SBarry Smith Level: intermediate 563793721a6SBarry Smith 564db781477SPatrick Sanan .seealso: `PetscFinalize()`, `PetscInitializeFortran()`, `PetscGetArgs()`, `PetscGetArguments()` 565793721a6SBarry Smith @*/ 566d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFreeArguments(char **args) 567d71ae5a4SJacob Faibussowitsch { 56839a651e2SJacob Faibussowitsch PetscFunctionBegin; 56939a651e2SJacob Faibussowitsch if (args) { 570793721a6SBarry Smith PetscInt i = 0; 571793721a6SBarry Smith 5729566063dSJacob Faibussowitsch while (args[i]) PetscCall(PetscFree(args[i++])); 5739566063dSJacob Faibussowitsch PetscCall(PetscFree(args)); 57439a651e2SJacob Faibussowitsch } 575*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 576793721a6SBarry Smith } 577793721a6SBarry Smith 57827104ee2SJacob Faibussowitsch #if PetscDefined(HAVE_SAWS) 57930befbd2SBarry Smith #include <petscconfiginfo.h> 58030befbd2SBarry Smith 581d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode PetscInitializeSAWs(const char help[]) 582d71ae5a4SJacob Faibussowitsch { 58327104ee2SJacob Faibussowitsch PetscFunctionBegin; 58411525c0dSBarry Smith if (!PetscGlobalRank) { 58530befbd2SBarry Smith char cert[PETSC_MAX_PATH_LEN], root[PETSC_MAX_PATH_LEN], *intro, programname[64], *appline, *options, version[64]; 58611525c0dSBarry Smith int port; 587ffbd1cfbSBarry Smith PetscBool flg, rootlocal = PETSC_FALSE, flg2, selectport = PETSC_FALSE; 58811525c0dSBarry Smith size_t applinelen, introlen; 589ffbd1cfbSBarry Smith char sawsurl[256]; 59011525c0dSBarry Smith 5919566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(NULL, NULL, "-saws_log", &flg)); 59211525c0dSBarry Smith if (flg) { 59311525c0dSBarry Smith char sawslog[PETSC_MAX_PATH_LEN]; 59411525c0dSBarry Smith 5959566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-saws_log", sawslog, sizeof(sawslog), NULL)); 59611525c0dSBarry Smith if (sawslog[0]) { 597792fecdfSBarry Smith PetscCallSAWs(SAWs_Set_Use_Logfile, (sawslog)); 59811525c0dSBarry Smith } else { 599792fecdfSBarry Smith PetscCallSAWs(SAWs_Set_Use_Logfile, (NULL)); 60011525c0dSBarry Smith } 60111525c0dSBarry Smith } 6029566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-saws_https", cert, sizeof(cert), &flg)); 60348a46eb9SPierre Jolivet if (flg) PetscCallSAWs(SAWs_Set_Use_HTTPS, (cert)); 6049566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-saws_port_auto_select", &selectport, NULL)); 605ffbd1cfbSBarry Smith if (selectport) { 606792fecdfSBarry Smith PetscCallSAWs(SAWs_Get_Available_Port, (&port)); 607792fecdfSBarry Smith PetscCallSAWs(SAWs_Set_Port, (port)); 608ffbd1cfbSBarry Smith } else { 6099566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetInt(NULL, NULL, "-saws_port", &port, &flg)); 61048a46eb9SPierre Jolivet if (flg) PetscCallSAWs(SAWs_Set_Port, (port)); 611ffbd1cfbSBarry Smith } 6129566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-saws_root", root, sizeof(root), &flg)); 61311525c0dSBarry Smith if (flg) { 614792fecdfSBarry Smith PetscCallSAWs(SAWs_Set_Document_Root, (root)); 6159566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(root, ".", &rootlocal)); 6169c1e0ce8SBarry Smith } else { 6179566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(NULL, NULL, "-saws_options", &flg)); 6189c1e0ce8SBarry Smith if (flg) { 6199566063dSJacob Faibussowitsch PetscCall(PetscStrreplace(PETSC_COMM_WORLD, "${PETSC_DIR}/share/petsc/saws", root, sizeof(root))); 620792fecdfSBarry Smith PetscCallSAWs(SAWs_Set_Document_Root, (root)); 6219c1e0ce8SBarry Smith } 62211525c0dSBarry Smith } 6239566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(NULL, NULL, "-saws_local", &flg2)); 62411525c0dSBarry Smith if (flg2) { 62511525c0dSBarry Smith char jsdir[PETSC_MAX_PATH_LEN]; 62628b400f6SJacob Faibussowitsch PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_SUP, "-saws_local option requires -saws_root option"); 6279566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(jsdir, sizeof(jsdir), "%s/js", root)); 6289566063dSJacob Faibussowitsch PetscCall(PetscTestDirectory(jsdir, 'r', &flg)); 62928b400f6SJacob Faibussowitsch PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "-saws_local option requires js directory in root directory"); 630792fecdfSBarry Smith PetscCallSAWs(SAWs_Push_Local_Header, ()); 63111525c0dSBarry Smith } 6329566063dSJacob Faibussowitsch PetscCall(PetscGetProgramName(programname, sizeof(programname))); 6339566063dSJacob Faibussowitsch PetscCall(PetscStrlen(help, &applinelen)); 63411525c0dSBarry Smith introlen = 4096 + applinelen; 63530a8c9c0SSurtai Han applinelen += 1024; 6369566063dSJacob Faibussowitsch PetscCall(PetscMalloc(applinelen, &appline)); 6379566063dSJacob Faibussowitsch PetscCall(PetscMalloc(introlen, &intro)); 63811525c0dSBarry Smith 63911525c0dSBarry Smith if (rootlocal) { 6409566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(appline, applinelen, "%s.c.html", programname)); 6419566063dSJacob Faibussowitsch PetscCall(PetscTestFile(appline, 'r', &rootlocal)); 64211525c0dSBarry Smith } 6439566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetAll(NULL, &options)); 64411525c0dSBarry Smith if (rootlocal && help) { 6459566063dSJacob Faibussowitsch PetscCall(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)); 64611525c0dSBarry Smith } else if (help) { 6479566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(appline, applinelen, "<center>Running %s %s</center><br><center><pre>%s</pre></center><br>", programname, options, help)); 64811525c0dSBarry Smith } else { 6499566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(appline, applinelen, "<center> Running %s %s</center><br>\n", programname, options)); 65011525c0dSBarry Smith } 6519566063dSJacob Faibussowitsch PetscCall(PetscFree(options)); 6529566063dSJacob Faibussowitsch PetscCall(PetscGetVersion(version, sizeof(version))); 6539371c9d4SSatish Balay PetscCall(PetscSNPrintf(intro, introlen, 6549371c9d4SSatish Balay "<body>\n" 655a17b96a8SKyle Gerard Felker "<center><h2> <a href=\"https://petsc.org/\">PETSc</a> Application Web server powered by <a href=\"https://bitbucket.org/saws/saws\">SAWs</a> </h2></center>\n" 656df62c222SBarry 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" 6579371c9d4SSatish Balay "%s", 6589371c9d4SSatish Balay version, petscconfigureoptions, appline)); 659792fecdfSBarry Smith PetscCallSAWs(SAWs_Push_Body, ("index.html", 0, intro)); 6609566063dSJacob Faibussowitsch PetscCall(PetscFree(intro)); 6619566063dSJacob Faibussowitsch PetscCall(PetscFree(appline)); 662ffbd1cfbSBarry Smith if (selectport) { 663aa573868SBarry Smith PetscBool silent; 6647d812c46SBarry Smith 6657d812c46SBarry Smith /* another process may have grabbed the port so keep trying */ 66639a651e2SJacob Faibussowitsch while (SAWs_Initialize()) { 667792fecdfSBarry Smith PetscCallSAWs(SAWs_Get_Available_Port, (&port)); 668792fecdfSBarry Smith PetscCallSAWs(SAWs_Set_Port, (port)); 6697d812c46SBarry Smith } 6707d812c46SBarry Smith 6719566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-saws_port_auto_select_silent", &silent, NULL)); 672aa573868SBarry Smith if (!silent) { 673792fecdfSBarry Smith PetscCallSAWs(SAWs_Get_FullURL, (sizeof(sawsurl), sawsurl)); 6749566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Point your browser to %s for SAWs\n", sawsurl)); 675ffbd1cfbSBarry Smith } 6767d812c46SBarry Smith } else { 677792fecdfSBarry Smith PetscCallSAWs(SAWs_Initialize, ()); 678aa573868SBarry Smith } 6799566063dSJacob Faibussowitsch PetscCall(PetscCitationsRegister("@TechReport{ saws,\n" 6800af79b04SBarry Smith " Author = {Matt Otten and Jed Brown and Barry Smith},\n" 6810af79b04SBarry Smith " Title = {Scientific Application Web Server (SAWs) Users Manual},\n" 6820af79b04SBarry Smith " Institution = {Argonne National Laboratory},\n" 6839371c9d4SSatish Balay " Year = 2013\n}\n", 6849371c9d4SSatish Balay NULL)); 68511525c0dSBarry Smith } 686*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 68711525c0dSBarry Smith } 68811525c0dSBarry Smith #endif 68911525c0dSBarry Smith 6904dfee713SSatish Balay /* Things must be done before MPI_Init() when MPI is not yet initialized, and can be shared between C init and Fortran init */ 691d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode PetscPreMPIInit_Private(void) 692d71ae5a4SJacob Faibussowitsch { 6934dfee713SSatish Balay PetscFunctionBegin; 6944dfee713SSatish Balay #if defined(PETSC_HAVE_HWLOC_SOLARIS_BUG) 6954dfee713SSatish Balay /* see MPI.py for details on this bug */ 6964dfee713SSatish Balay (void)setenv("HWLOC_COMPONENTS", "-x86", 1); 6974dfee713SSatish Balay #endif 698*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6994dfee713SSatish Balay } 7004dfee713SSatish Balay 701a4af0ceeSJacob Faibussowitsch #if PetscDefined(HAVE_ADIOS) 702a56f64adSBarry Smith #include <adios.h> 70322580e64SBarry Smith #include <adios_read.h> 7047b56e58cSSatish Balay int64_t Petsc_adios_group; 705a56f64adSBarry Smith #endif 706a4af0ceeSJacob Faibussowitsch #if PetscDefined(HAVE_OPENMP) 707cd1b99a6SBarry Smith #include <omp.h> 708f90b075cSBarry Smith PetscInt PetscNumOMPThreads; 709cd1b99a6SBarry Smith #endif 710a56f64adSBarry Smith 711a4af0ceeSJacob Faibussowitsch #include <petsc/private/deviceimpl.h> 712a4af0ceeSJacob Faibussowitsch #if PetscDefined(HAVE_CUDA) 7130e6b6b59SJacob Faibussowitsch #include <petscdevice_cuda.h> 714a4af0ceeSJacob Faibussowitsch // REMOVE ME 715a4af0ceeSJacob Faibussowitsch cudaStream_t PetscDefaultCudaStream = NULL; 716a4af0ceeSJacob Faibussowitsch #endif 717a4af0ceeSJacob Faibussowitsch #if PetscDefined(HAVE_HIP) 7180e6b6b59SJacob Faibussowitsch #include <petscdevice_hip.h> 719a4af0ceeSJacob Faibussowitsch // REMOVE ME 720a4af0ceeSJacob Faibussowitsch hipStream_t PetscDefaultHipStream = NULL; 721a4af0ceeSJacob Faibussowitsch #endif 722a4af0ceeSJacob Faibussowitsch 72327104ee2SJacob Faibussowitsch #if PetscDefined(HAVE_DLFCN_H) 724bc8a24ecSBarry Smith #include <dlfcn.h> 725bc8a24ecSBarry Smith #endif 726a4af0ceeSJacob Faibussowitsch #if PetscDefined(USE_LOG) 727a4af0ceeSJacob Faibussowitsch PETSC_INTERN PetscErrorCode PetscLogInitialize(void); 728a4af0ceeSJacob Faibussowitsch #endif 729a4af0ceeSJacob Faibussowitsch #if PetscDefined(HAVE_VIENNACL) 7303274405dSPierre Jolivet PETSC_EXTERN PetscErrorCode PetscViennaCLInit(void); 731a4af0ceeSJacob Faibussowitsch PetscBool PetscViennaCLSynchronize = PETSC_FALSE; 732a4af0ceeSJacob Faibussowitsch #endif 733bc8a24ecSBarry Smith 734660278c0SBarry Smith PetscBool PetscCIEnabled = PETSC_FALSE, PetscCIEnabledPortableErrorOutput = PETSC_FALSE; 735660278c0SBarry Smith 73685649d77SJunchao Zhang /* 73785649d77SJunchao Zhang PetscInitialize_Common - shared code between C and Fortran initialization 73885649d77SJunchao Zhang 73985649d77SJunchao Zhang prog: program name 74002101c96SSatish Balay file: optional PETSc database file name. Might be in Fortran string format when 'ftn' is true 74185649d77SJunchao Zhang help: program help message 74202101c96SSatish Balay ftn: is it called from Fortran initilization (petscinitializef_)? 74385649d77SJunchao Zhang readarguments,len: used when fortran is true 74485649d77SJunchao Zhang */ 745d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode PetscInitialize_Common(const char *prog, const char *file, const char *help, PetscBool ftn, PetscBool readarguments, PetscInt len) 746d71ae5a4SJacob Faibussowitsch { 74785649d77SJunchao Zhang PetscMPIInt size; 74885649d77SJunchao Zhang PetscBool flg = PETSC_TRUE; 74985649d77SJunchao Zhang char hostname[256]; 75085649d77SJunchao Zhang 75127104ee2SJacob Faibussowitsch PetscFunctionBegin; 752*3ba16761SJacob Faibussowitsch if (PetscInitializeCalled) PetscFunctionReturn(PETSC_SUCCESS); 75339a651e2SJacob Faibussowitsch /* these must be initialized in a routine, not as a constant declaration */ 75439a651e2SJacob Faibussowitsch PETSC_STDOUT = stdout; 75539a651e2SJacob Faibussowitsch PETSC_STDERR = stderr; 75639a651e2SJacob Faibussowitsch 7579566063dSJacob Faibussowitsch /* PetscCall can be used from now */ 75839a651e2SJacob Faibussowitsch PetscErrorHandlingInitialized = PETSC_TRUE; 75939a651e2SJacob Faibussowitsch 76085649d77SJunchao Zhang /* 76185649d77SJunchao Zhang The checking over compatible runtime libraries is complicated by the MPI ABI initiative 76285649d77SJunchao Zhang https://wiki.mpich.org/mpich/index.php/ABI_Compatibility_Initiative which started with 76385649d77SJunchao Zhang MPICH v3.1 (Released February 2014) 76485649d77SJunchao Zhang IBM MPI v2.1 (December 2014) 76585649d77SJunchao Zhang Intel MPI Library v5.0 (2014) 76685649d77SJunchao Zhang Cray MPT v7.0.0 (June 2014) 76785649d77SJunchao Zhang As of July 31, 2017 the ABI number still appears to be 12, that is all of the versions 76885649d77SJunchao Zhang listed above and since that time are compatible. 76985649d77SJunchao Zhang 77085649d77SJunchao Zhang Unfortunately the MPI ABI initiative has not defined a way to determine the ABI number 77185649d77SJunchao Zhang at compile time or runtime. Thus we will need to systematically track the allowed versions 77285649d77SJunchao Zhang and how they are represented in the mpi.h and MPI_Get_library_version() output in order 77385649d77SJunchao Zhang to perform the checking. 77485649d77SJunchao Zhang 77585649d77SJunchao Zhang Currently we only check for pre MPI ABI versions (and packages that do not follow the MPI ABI). 77685649d77SJunchao Zhang 77785649d77SJunchao Zhang Questions: 77885649d77SJunchao Zhang 77985649d77SJunchao Zhang Should the checks for ABI incompatibility be only on the major version number below? 78085649d77SJunchao Zhang Presumably the output to stderr will be removed before a release. 78185649d77SJunchao Zhang */ 78285649d77SJunchao Zhang 78385649d77SJunchao Zhang #if defined(PETSC_HAVE_MPI_GET_LIBRARY_VERSION) 78485649d77SJunchao Zhang { 78585649d77SJunchao Zhang char mpilibraryversion[MPI_MAX_LIBRARY_VERSION_STRING]; 78685649d77SJunchao Zhang PetscMPIInt mpilibraryversionlength; 78739a651e2SJacob Faibussowitsch 7889566063dSJacob Faibussowitsch PetscCallMPI(MPI_Get_library_version(mpilibraryversion, &mpilibraryversionlength)); 78985649d77SJunchao Zhang /* check for MPICH versions before MPI ABI initiative */ 79085649d77SJunchao Zhang #if defined(MPICH_VERSION) 79185649d77SJunchao Zhang #if MPICH_NUMVERSION < 30100000 79285649d77SJunchao Zhang { 79385649d77SJunchao Zhang char *ver, *lf; 79485649d77SJunchao Zhang PetscBool flg = PETSC_FALSE; 79539a651e2SJacob Faibussowitsch 7969566063dSJacob Faibussowitsch PetscCall(PetscStrstr(mpilibraryversion, "MPICH Version:", &ver)); 79739a651e2SJacob Faibussowitsch if (ver) { 7989566063dSJacob Faibussowitsch PetscCall(PetscStrchr(ver, '\n', &lf)); 79939a651e2SJacob Faibussowitsch if (lf) { 80085649d77SJunchao Zhang *lf = 0; 8019566063dSJacob Faibussowitsch PetscCall(PetscStrendswith(ver, MPICH_VERSION, &flg)); 80285649d77SJunchao Zhang } 80385649d77SJunchao Zhang } 80485649d77SJunchao Zhang if (!flg) { 805d5b396e8SSatish Balay PetscCall(PetscInfo(NULL, "PETSc warning --- MPICH library version \n%s does not match what PETSc was compiled with %s.\n", mpilibraryversion, MPICH_VERSION)); 80685649d77SJunchao Zhang flg = PETSC_TRUE; 80785649d77SJunchao Zhang } 80885649d77SJunchao Zhang } 80985649d77SJunchao Zhang #endif 81085649d77SJunchao Zhang /* check for OpenMPI version, it is not part of the MPI ABI initiative (is it part of another initiative that needs to be handled?) */ 81185649d77SJunchao Zhang #elif defined(OMPI_MAJOR_VERSION) 81285649d77SJunchao Zhang { 81385649d77SJunchao Zhang char *ver, bs[MPI_MAX_LIBRARY_VERSION_STRING], *bsf; 81485649d77SJunchao Zhang PetscBool flg = PETSC_FALSE; 81585649d77SJunchao Zhang #define PSTRSZ 2 81685649d77SJunchao Zhang char ompistr1[PSTRSZ][MPI_MAX_LIBRARY_VERSION_STRING] = {"Open MPI", "FUJITSU MPI"}; 81785649d77SJunchao Zhang char ompistr2[PSTRSZ][MPI_MAX_LIBRARY_VERSION_STRING] = {"v", "Library "}; 81885649d77SJunchao Zhang int i; 81985649d77SJunchao Zhang for (i = 0; i < PSTRSZ; i++) { 8209566063dSJacob Faibussowitsch PetscCall(PetscStrstr(mpilibraryversion, ompistr1[i], &ver)); 82139a651e2SJacob Faibussowitsch if (ver) { 8229566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(bs, MPI_MAX_LIBRARY_VERSION_STRING, "%s%d.%d", ompistr2[i], OMPI_MAJOR_VERSION, OMPI_MINOR_VERSION)); 8239566063dSJacob Faibussowitsch PetscCall(PetscStrstr(ver, bs, &bsf)); 82439a651e2SJacob Faibussowitsch if (bsf) flg = PETSC_TRUE; 82585649d77SJunchao Zhang break; 82685649d77SJunchao Zhang } 82785649d77SJunchao Zhang } 82885649d77SJunchao Zhang if (!flg) { 829*3ba16761SJacob Faibussowitsch PetscCall(PetscInfo(NULL, "PETSc warning --- Open MPI library version \n%s does not match what PETSc was compiled with %d.%d.\n", mpilibraryversion, OMPI_MAJOR_VERSION, OMPI_MINOR_VERSION)); 83085649d77SJunchao Zhang flg = PETSC_TRUE; 83185649d77SJunchao Zhang } 83285649d77SJunchao Zhang } 83385649d77SJunchao Zhang #endif 83485649d77SJunchao Zhang } 83585649d77SJunchao Zhang #endif 83685649d77SJunchao Zhang 837e8953f01SSatish Balay #if defined(PETSC_HAVE_DLADDR) && !(defined(__cray__) && defined(__clang__)) 83885649d77SJunchao Zhang /* These symbols are currently in the OpenMPI and MPICH libraries; they may not always be, in that case the test will simply not detect the problem */ 83939a651e2SJacob Faibussowitsch PetscCheck(!dlsym(RTLD_DEFAULT, "ompi_mpi_init") || !dlsym(RTLD_DEFAULT, "MPID_Abort"), PETSC_COMM_SELF, PETSC_ERR_MPI_LIB_INCOMP, "Application was linked against both OpenMPI and MPICH based MPI libraries and will not run correctly"); 84085649d77SJunchao Zhang #endif 84185649d77SJunchao Zhang 84285649d77SJunchao Zhang /* on Windows - set printf to default to printing 2 digit exponents */ 84385649d77SJunchao Zhang #if defined(PETSC_HAVE__SET_OUTPUT_FORMAT) 84485649d77SJunchao Zhang _set_output_format(_TWO_DIGIT_EXPONENT); 84585649d77SJunchao Zhang #endif 84685649d77SJunchao Zhang 8479566063dSJacob Faibussowitsch PetscCall(PetscOptionsCreateDefault()); 84885649d77SJunchao Zhang 84985649d77SJunchao Zhang PetscFinalizeCalled = PETSC_FALSE; 85085649d77SJunchao Zhang 8519566063dSJacob Faibussowitsch PetscCall(PetscSetProgramName(prog)); 8529566063dSJacob Faibussowitsch PetscCall(PetscSpinlockCreate(&PetscViewerASCIISpinLockOpen)); 8539566063dSJacob Faibussowitsch PetscCall(PetscSpinlockCreate(&PetscViewerASCIISpinLockStdout)); 8549566063dSJacob Faibussowitsch PetscCall(PetscSpinlockCreate(&PetscViewerASCIISpinLockStderr)); 8559566063dSJacob Faibussowitsch PetscCall(PetscSpinlockCreate(&PetscCommSpinLock)); 85685649d77SJunchao Zhang 85785649d77SJunchao Zhang if (PETSC_COMM_WORLD == MPI_COMM_NULL) PETSC_COMM_WORLD = MPI_COMM_WORLD; 8589566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_set_errhandler(PETSC_COMM_WORLD, MPI_ERRORS_RETURN)); 85985649d77SJunchao Zhang 86085649d77SJunchao Zhang if (PETSC_MPI_ERROR_CLASS == MPI_ERR_LASTCODE) { 8619566063dSJacob Faibussowitsch PetscCallMPI(MPI_Add_error_class(&PETSC_MPI_ERROR_CLASS)); 8629566063dSJacob Faibussowitsch PetscCallMPI(MPI_Add_error_code(PETSC_MPI_ERROR_CLASS, &PETSC_MPI_ERROR_CODE)); 86385649d77SJunchao Zhang } 86485649d77SJunchao Zhang 86585649d77SJunchao Zhang /* Done after init due to a bug in MPICH-GM? */ 8669566063dSJacob Faibussowitsch PetscCall(PetscErrorPrintfInitialize()); 86785649d77SJunchao Zhang 8689566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(MPI_COMM_WORLD, &PetscGlobalRank)); 8699566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(MPI_COMM_WORLD, &PetscGlobalSize)); 87085649d77SJunchao Zhang 87185649d77SJunchao Zhang MPIU_BOOL = MPI_INT; 87285649d77SJunchao Zhang MPIU_ENUM = MPI_INT; 87385649d77SJunchao Zhang MPIU_FORTRANADDR = (sizeof(void *) == sizeof(int)) ? MPI_INT : MPIU_INT64; 87485649d77SJunchao Zhang if (sizeof(size_t) == sizeof(unsigned)) MPIU_SIZE_T = MPI_UNSIGNED; 87585649d77SJunchao Zhang else if (sizeof(size_t) == sizeof(unsigned long)) MPIU_SIZE_T = MPI_UNSIGNED_LONG; 87685649d77SJunchao Zhang #if defined(PETSC_SIZEOF_LONG_LONG) 87785649d77SJunchao Zhang else if (sizeof(size_t) == sizeof(unsigned long long)) MPIU_SIZE_T = MPI_UNSIGNED_LONG_LONG; 87885649d77SJunchao Zhang #endif 87939a651e2SJacob Faibussowitsch else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "Could not find MPI type for size_t"); 88085649d77SJunchao Zhang 88185649d77SJunchao Zhang /* 88285649d77SJunchao Zhang Initialized the global complex variable; this is because with 88385649d77SJunchao Zhang shared libraries the constructors for global variables 88485649d77SJunchao Zhang are not called; at least on IRIX. 88585649d77SJunchao Zhang */ 88685649d77SJunchao Zhang #if defined(PETSC_HAVE_COMPLEX) 88785649d77SJunchao Zhang { 88885649d77SJunchao Zhang #if defined(PETSC_CLANGUAGE_CXX) && !defined(PETSC_USE_REAL___FLOAT128) 88985649d77SJunchao Zhang PetscComplex ic(0.0, 1.0); 89085649d77SJunchao Zhang PETSC_i = ic; 89185649d77SJunchao Zhang #else 89285649d77SJunchao Zhang PETSC_i = _Complex_I; 89385649d77SJunchao Zhang #endif 89485649d77SJunchao Zhang } 89585649d77SJunchao Zhang #endif /* PETSC_HAVE_COMPLEX */ 89685649d77SJunchao Zhang 89785649d77SJunchao Zhang /* 89885649d77SJunchao Zhang Create the PETSc MPI reduction operator that sums of the first 89985649d77SJunchao Zhang half of the entries and maxes the second half. 90085649d77SJunchao Zhang */ 9019566063dSJacob Faibussowitsch PetscCallMPI(MPI_Op_create(MPIU_MaxSum_Local, 1, &MPIU_MAXSUM_OP)); 90285649d77SJunchao Zhang 903613bf2b2SPierre Jolivet #if defined(PETSC_HAVE_REAL___FLOAT128) 9049566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_contiguous(2, MPI_DOUBLE, &MPIU___FLOAT128)); 9059566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&MPIU___FLOAT128)); 9069566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_contiguous(4, MPI_DOUBLE, &MPIU___COMPLEX128)); 9079566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&MPIU___COMPLEX128)); 90885649d77SJunchao Zhang #endif 9099e517322SPierre Jolivet #if defined(PETSC_HAVE_REAL___FP16) 9109566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_contiguous(2, MPI_CHAR, &MPIU___FP16)); 9119566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&MPIU___FP16)); 91285649d77SJunchao Zhang #endif 91385649d77SJunchao Zhang 91485649d77SJunchao Zhang #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16) 9159566063dSJacob Faibussowitsch PetscCallMPI(MPI_Op_create(PetscSum_Local, 1, &MPIU_SUM)); 916613bf2b2SPierre Jolivet PetscCallMPI(MPI_Op_create(PetscMax_Local, 1, &MPIU_MAX)); 917613bf2b2SPierre Jolivet PetscCallMPI(MPI_Op_create(PetscMin_Local, 1, &MPIU_MIN)); 9189e517322SPierre Jolivet #elif defined(PETSC_HAVE_REAL___FLOAT128) || defined(PETSC_HAVE_REAL___FP16) 9199e517322SPierre Jolivet PetscCallMPI(MPI_Op_create(PetscSum_Local, 1, &MPIU_SUM___FP16___FLOAT128)); 92085649d77SJunchao Zhang #endif 92185649d77SJunchao Zhang 9229566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_contiguous(2, MPIU_SCALAR, &MPIU_2SCALAR)); 92362e5d2d2SJDBetteridge PetscCallMPI(MPI_Op_create(PetscGarbageKeySortedIntersect, 1, &Petsc_Garbage_SetIntersectOp)); 9249566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&MPIU_2SCALAR)); 92585649d77SJunchao Zhang 92685649d77SJunchao Zhang /* create datatypes used by MPIU_MAXLOC, MPIU_MINLOC and PetscSplitReduction_Op */ 92785649d77SJunchao Zhang #if !defined(PETSC_HAVE_MPIUNI) 92885649d77SJunchao Zhang { 92985649d77SJunchao Zhang PetscMPIInt blockSizes[2] = {1, 1}; 93093d501b3SJacob Faibussowitsch MPI_Aint blockOffsets[2] = {offsetof(struct petsc_mpiu_real_int, v), offsetof(struct petsc_mpiu_real_int, i)}; 93185649d77SJunchao Zhang MPI_Datatype blockTypes[2] = {MPIU_REAL, MPIU_INT}, tmpStruct; 93285649d77SJunchao Zhang 9339566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_create_struct(2, blockSizes, blockOffsets, blockTypes, &tmpStruct)); 93493d501b3SJacob Faibussowitsch PetscCallMPI(MPI_Type_create_resized(tmpStruct, 0, sizeof(struct petsc_mpiu_real_int), &MPIU_REAL_INT)); 9359566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&tmpStruct)); 9369566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&MPIU_REAL_INT)); 93785649d77SJunchao Zhang } 93885649d77SJunchao Zhang { 93985649d77SJunchao Zhang PetscMPIInt blockSizes[2] = {1, 1}; 94093d501b3SJacob Faibussowitsch MPI_Aint blockOffsets[2] = {offsetof(struct petsc_mpiu_scalar_int, v), offsetof(struct petsc_mpiu_scalar_int, i)}; 94185649d77SJunchao Zhang MPI_Datatype blockTypes[2] = {MPIU_SCALAR, MPIU_INT}, tmpStruct; 94285649d77SJunchao Zhang 9439566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_create_struct(2, blockSizes, blockOffsets, blockTypes, &tmpStruct)); 94493d501b3SJacob Faibussowitsch PetscCallMPI(MPI_Type_create_resized(tmpStruct, 0, sizeof(struct petsc_mpiu_scalar_int), &MPIU_SCALAR_INT)); 9459566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&tmpStruct)); 9469566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&MPIU_SCALAR_INT)); 94785649d77SJunchao Zhang } 94885649d77SJunchao Zhang #endif 94985649d77SJunchao Zhang 95085649d77SJunchao Zhang #if defined(PETSC_USE_64BIT_INDICES) 9519566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_contiguous(2, MPIU_INT, &MPIU_2INT)); 9529566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&MPIU_2INT)); 95385649d77SJunchao Zhang #endif 9549566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_contiguous(4, MPI_INT, &MPI_4INT)); 9559566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&MPI_4INT)); 9569566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_contiguous(4, MPIU_INT, &MPIU_4INT)); 9579566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&MPIU_4INT)); 95885649d77SJunchao Zhang 95985649d77SJunchao Zhang /* 96085649d77SJunchao Zhang Attributes to be set on PETSc communicators 96185649d77SJunchao Zhang */ 9629566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, Petsc_Counter_Attr_Delete_Fn, &Petsc_Counter_keyval, (void *)0)); 9639566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, Petsc_InnerComm_Attr_Delete_Fn, &Petsc_InnerComm_keyval, (void *)0)); 9649566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, Petsc_OuterComm_Attr_Delete_Fn, &Petsc_OuterComm_keyval, (void *)0)); 9659566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, Petsc_ShmComm_Attr_Delete_Fn, &Petsc_ShmComm_keyval, (void *)0)); 96662e5d2d2SJDBetteridge PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_CreationIdx_keyval, (void *)0)); 96762e5d2d2SJDBetteridge PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Garbage_HMap_keyval, (void *)0)); 96885649d77SJunchao Zhang 96985649d77SJunchao Zhang #if defined(PETSC_HAVE_FORTRAN) 9709566063dSJacob Faibussowitsch if (ftn) PetscCall(PetscInitFortran_Private(readarguments, file, len)); 97185649d77SJunchao Zhang else 97285649d77SJunchao Zhang #endif 9739566063dSJacob Faibussowitsch PetscCall(PetscOptionsInsert(NULL, &PetscGlobalArgc, &PetscGlobalArgs, file)); 97485649d77SJunchao Zhang 97585649d77SJunchao Zhang /* call a second time so it can look in the options database */ 9769566063dSJacob Faibussowitsch PetscCall(PetscErrorPrintfInitialize()); 97785649d77SJunchao Zhang 97885649d77SJunchao Zhang /* 97985649d77SJunchao Zhang Check system options and print help 98085649d77SJunchao Zhang */ 9819566063dSJacob Faibussowitsch PetscCall(PetscOptionsCheckInitial_Private(help)); 98285649d77SJunchao Zhang 983a4af0ceeSJacob Faibussowitsch /* 9840e6b6b59SJacob Faibussowitsch Creates the logging data structures; this is enabled even if logging is not turned on 9850e6b6b59SJacob Faibussowitsch This is the last thing we do before returning to the user code to prevent having the 9860e6b6b59SJacob Faibussowitsch logging numbers contaminated by any startup time associated with MPI 9870e6b6b59SJacob Faibussowitsch */ 9880e6b6b59SJacob Faibussowitsch #if defined(PETSC_USE_LOG) 9890e6b6b59SJacob Faibussowitsch PetscCall(PetscLogInitialize()); 9900e6b6b59SJacob Faibussowitsch #endif 9910e6b6b59SJacob Faibussowitsch 9920e6b6b59SJacob Faibussowitsch /* 993a4af0ceeSJacob Faibussowitsch Initialize PetscDevice and PetscDeviceContext 994a4af0ceeSJacob Faibussowitsch 995a4af0ceeSJacob Faibussowitsch Note to any future devs thinking of moving this, proper initialization requires: 996a4af0ceeSJacob Faibussowitsch 1. MPI initialized 997a4af0ceeSJacob Faibussowitsch 2. Options DB initialized 9980e6b6b59SJacob Faibussowitsch 3. Petsc error handling initialized, specifically signal handlers. This expects to set up 9990e6b6b59SJacob Faibussowitsch its own SIGSEV handler via the push/pop interface. 10000e6b6b59SJacob Faibussowitsch 4. Logging initialized 1001a4af0ceeSJacob Faibussowitsch */ 10029566063dSJacob Faibussowitsch PetscCall(PetscDeviceInitializeFromOptions_Internal(PETSC_COMM_WORLD)); 1003a4af0ceeSJacob Faibussowitsch 1004a4af0ceeSJacob Faibussowitsch #if PetscDefined(HAVE_VIENNACL) 1005a4af0ceeSJacob Faibussowitsch flg = PETSC_FALSE; 10069566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(NULL, NULL, "-log_summary", &flg)); 10079566063dSJacob Faibussowitsch if (!flg) PetscCall(PetscOptionsHasName(NULL, NULL, "-log_view", &flg)); 10089566063dSJacob Faibussowitsch if (!flg) PetscCall(PetscOptionsGetBool(NULL, NULL, "-viennacl_synchronize", &flg, NULL)); 1009a4af0ceeSJacob Faibussowitsch PetscViennaCLSynchronize = flg; 10109566063dSJacob Faibussowitsch PetscCall(PetscViennaCLInit()); 1011a4af0ceeSJacob Faibussowitsch #endif 1012a4af0ceeSJacob Faibussowitsch 10139566063dSJacob Faibussowitsch PetscCall(PetscCitationsInitialize()); 101485649d77SJunchao Zhang 101585649d77SJunchao Zhang #if defined(PETSC_HAVE_SAWS) 10169566063dSJacob Faibussowitsch PetscCall(PetscInitializeSAWs(ftn ? NULL : help)); 101727104ee2SJacob Faibussowitsch flg = PETSC_FALSE; 10189566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(NULL, NULL, "-stack_view", &flg)); 10199566063dSJacob Faibussowitsch if (flg) PetscCall(PetscStackViewSAWs()); 102085649d77SJunchao Zhang #endif 102185649d77SJunchao Zhang 102285649d77SJunchao Zhang /* 102385649d77SJunchao Zhang Load the dynamic libraries (on machines that support them), this registers all 102485649d77SJunchao Zhang the solvers etc. (On non-dynamic machines this initializes the PetscDraw and PetscViewer classes) 102585649d77SJunchao Zhang */ 10269566063dSJacob Faibussowitsch PetscCall(PetscInitialize_DynamicLibraries()); 102785649d77SJunchao Zhang 10289566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size)); 10299566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL, "PETSc successfully started: number of processors = %d\n", size)); 103096dcfd6cSBarry Smith PetscCall(PetscGetHostName(hostname, sizeof(hostname))); 10319566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL, "Running on machine: %s\n", hostname)); 103285649d77SJunchao Zhang #if defined(PETSC_HAVE_OPENMP) 103385649d77SJunchao Zhang { 103485649d77SJunchao Zhang PetscBool omp_view_flag; 103585649d77SJunchao Zhang char *threads = getenv("OMP_NUM_THREADS"); 103685649d77SJunchao Zhang 103785649d77SJunchao Zhang if (threads) { 10389566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL, "Number of OpenMP threads %s (as given by OMP_NUM_THREADS)\n", threads)); 103985649d77SJunchao Zhang (void)sscanf(threads, "%" PetscInt_FMT, &PetscNumOMPThreads); 104085649d77SJunchao Zhang } else { 10412f840973SStefano Zampini PetscNumOMPThreads = (PetscInt)omp_get_max_threads(); 10429566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL, "Number of OpenMP threads %" PetscInt_FMT " (as given by omp_get_max_threads())\n", PetscNumOMPThreads)); 104385649d77SJunchao Zhang } 1044d0609cedSBarry Smith PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "OpenMP options", "Sys"); 10459566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-omp_num_threads", "Number of OpenMP threads to use (can also use environmental variable OMP_NUM_THREADS", "None", PetscNumOMPThreads, &PetscNumOMPThreads, &flg)); 10469566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-omp_view", "Display OpenMP number of threads", NULL, &omp_view_flag)); 1047d0609cedSBarry Smith PetscOptionsEnd(); 104885649d77SJunchao Zhang if (flg) { 10499566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL, "Number of OpenMP theads %" PetscInt_FMT " (given by -omp_num_threads)\n", PetscNumOMPThreads)); 105085649d77SJunchao Zhang omp_set_num_threads((int)PetscNumOMPThreads); 105185649d77SJunchao Zhang } 105248a46eb9SPierre Jolivet if (omp_view_flag) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "OpenMP: number of threads %" PetscInt_FMT "\n", PetscNumOMPThreads)); 105385649d77SJunchao Zhang } 105485649d77SJunchao Zhang #endif 105585649d77SJunchao Zhang 105685649d77SJunchao Zhang #if defined(PETSC_USE_PETSC_MPI_EXTERNAL32) 105785649d77SJunchao Zhang /* 105885649d77SJunchao Zhang Tell MPI about our own data representation converter, this would/should be used if extern32 is not supported by the MPI 105985649d77SJunchao Zhang 106085649d77SJunchao Zhang Currently not used because it is not supported by MPICH. 106185649d77SJunchao Zhang */ 10629566063dSJacob Faibussowitsch if (!PetscBinaryBigEndian()) PetscCallMPI(MPI_Register_datarep((char *)"petsc", PetscDataRep_read_conv_fn, PetscDataRep_write_conv_fn, PetscDataRep_extent_fn, NULL)); 106385649d77SJunchao Zhang #endif 106485649d77SJunchao Zhang 106585649d77SJunchao Zhang #if defined(PETSC_SERIALIZE_FUNCTIONS) 10669566063dSJacob Faibussowitsch PetscCall(PetscFPTCreate(10000)); 106785649d77SJunchao Zhang #endif 106885649d77SJunchao Zhang 106985649d77SJunchao Zhang #if defined(PETSC_HAVE_HWLOC) 107085649d77SJunchao Zhang { 107185649d77SJunchao Zhang PetscViewer viewer; 10729566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetViewer(PETSC_COMM_WORLD, NULL, NULL, "-process_view", &viewer, NULL, &flg)); 107385649d77SJunchao Zhang if (flg) { 10749566063dSJacob Faibussowitsch PetscCall(PetscProcessPlacementView(viewer)); 10759566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 107685649d77SJunchao Zhang } 107785649d77SJunchao Zhang } 107885649d77SJunchao Zhang #endif 107985649d77SJunchao Zhang 108085649d77SJunchao Zhang flg = PETSC_TRUE; 10819566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-viewfromoptions", &flg, NULL)); 10829566063dSJacob Faibussowitsch if (!flg) PetscCall(PetscOptionsPushGetViewerOff(PETSC_TRUE)); 108385649d77SJunchao Zhang 108485649d77SJunchao Zhang #if defined(PETSC_HAVE_ADIOS) 1085*3ba16761SJacob Faibussowitsch PetscCallExternal(adios_init_noxml, PETSC_COMM_WORLD); 1086*3ba16761SJacob Faibussowitsch PetscCallExternal(adios_declare_group, &Petsc_adios_group, "PETSc", "", adios_stat_default); 1087*3ba16761SJacob Faibussowitsch PetscCallExternal(adios_select_method, Petsc_adios_group, "MPI", "", ""); 1088*3ba16761SJacob Faibussowitsch PetscCallExternal(adios_read_init_method, ADIOS_READ_METHOD_BP, PETSC_COMM_WORLD, ""); 108985649d77SJunchao Zhang #endif 109085649d77SJunchao Zhang 109185649d77SJunchao Zhang #if defined(__VALGRIND_H) 109285649d77SJunchao Zhang PETSC_RUNNING_ON_VALGRIND = RUNNING_ON_VALGRIND ? PETSC_TRUE : PETSC_FALSE; 109385649d77SJunchao Zhang #if defined(PETSC_USING_DARWIN) && defined(PETSC_BLASLAPACK_SDOT_RETURNS_DOUBLE) 10949566063dSJacob Faibussowitsch if (PETSC_RUNNING_ON_VALGRIND) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "WARNING: Running valgrind with the MacOS native BLAS and LAPACK can fail. If it fails suggest configuring with --download-fblaslapack or --download-f2cblaslapack")); 109585649d77SJunchao Zhang #endif 109685649d77SJunchao Zhang #endif 109785649d77SJunchao Zhang /* 109885649d77SJunchao Zhang Set flag that we are completely initialized 109985649d77SJunchao Zhang */ 110085649d77SJunchao Zhang PetscInitializeCalled = PETSC_TRUE; 110185649d77SJunchao Zhang 11029566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(NULL, NULL, "-python", &flg)); 11039566063dSJacob Faibussowitsch if (flg) PetscCall(PetscPythonInitialize(NULL, NULL)); 1104f1f2ae84SBarry Smith 1105f1f2ae84SBarry Smith PetscCall(PetscOptionsHasName(NULL, NULL, "-mpi_linear_solver_server", &flg)); 1106f1f2ae84SBarry Smith if (PetscDefined(USE_SINGLE_LIBRARY) && flg) PetscCall(PCMPIServerBegin()); 1107f1f2ae84SBarry Smith else PetscCheck(!flg, PETSC_COMM_WORLD, PETSC_ERR_SUP, "PETSc configured using -with-single-library=0; -mpi_linear_solver_server not supported in that case"); 1108*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 110985649d77SJunchao Zhang } 111085649d77SJunchao Zhang 1111e5c89e4eSSatish Balay /*@C 1112e5c89e4eSSatish Balay PetscInitialize - Initializes the PETSc database and MPI. 1113811af0c4SBarry Smith `PetscInitialize()` calls MPI_Init() if that has yet to be called, 1114e5c89e4eSSatish Balay so this routine should always be called near the beginning of 1115e5c89e4eSSatish Balay your program -- usually the very first line! 1116e5c89e4eSSatish Balay 1117811af0c4SBarry Smith Collective on `MPI_COMM_WORLD` or `PETSC_COMM_WORLD` if it has been set 1118e5c89e4eSSatish Balay 1119e5c89e4eSSatish Balay Input Parameters: 1120e5c89e4eSSatish Balay + argc - count of number of command line arguments 1121e5c89e4eSSatish Balay . args - the command line arguments 1122be10d61cSLisandro Dalcin . file - [optional] PETSc database file, append ":yaml" to filename to specify YAML options format. 1123be10d61cSLisandro Dalcin Use NULL or empty string to not check for code specific file. 1124be10d61cSLisandro Dalcin Also checks ~/.petscrc, .petscrc and petscrc. 1125c5b5d8d5SVaclav Hapla Use -skip_petscrc in the code specific file (or command line) to skip ~/.petscrc, .petscrc and petscrc files. 11260298fd71SBarry Smith - help - [optional] Help message to print, use NULL for no message 1127e5c89e4eSSatish Balay 1128811af0c4SBarry Smith If you wish PETSc code to run ONLY on a subcommunicator of `MPI_COMM_WORLD`, create that 1129811af0c4SBarry Smith communicator first and assign it to `PETSC_COMM_WORLD` BEFORE calling `PetscInitialize()`. Thus if you are running a 1130811af0c4SBarry Smith four process job and two processes will run PETSc and have `PetscInitialize()` and PetscFinalize() and two process will not, 1131811af0c4SBarry Smith then do this. If ALL processes in the job are using `PetscInitialize()` and `PetscFinalize()` then you don't need to do this, even 113205827820SBarry Smith if different subcommunicators of the job are doing different things with PETSc. 1133e5c89e4eSSatish Balay 1134e5c89e4eSSatish Balay Options Database Keys: 11357ca660e7SBarry Smith + -help [intro] - prints help method for each option; if intro is given the program stops after printing the introductory help message 11367ca660e7SBarry Smith . -start_in_debugger [noxterm,dbx,xdb,gdb,...] - Starts program in debugger 1137e5c89e4eSSatish Balay . -on_error_attach_debugger [noxterm,dbx,xdb,gdb,...] - Starts debugger when error detected 11388a690491SBarry Smith . -on_error_emacs <machinename> - causes emacsclient to jump to error file 1139811af0c4SBarry Smith . -on_error_abort - calls `abort()` when error detected (no traceback) 1140811af0c4SBarry Smith . -on_error_mpiabort - calls `MPI_abort()` when error detected 11411af3601dSBarry Smith . -error_output_stdout - prints PETSc error messages to stdout instead of the default stderr 11428a690491SBarry Smith . -error_output_none - does not print the error messages (but handles errors in the same way as if this was not called) 1143bf4d2887SBarry Smith . -debugger_ranks [rank1,rank2,...] - Indicates ranks to start in debugger 1144e5c89e4eSSatish Balay . -debugger_pause [sleeptime] (in seconds) - Pauses debugger 1145e5c89e4eSSatish Balay . -stop_for_debugger - Print message on how to attach debugger manually to 1146e5c89e4eSSatish Balay process and wait (-debugger_pause) seconds for attachment 114779dccf82SBarry Smith . -malloc - Indicates use of PETSc error-checking malloc (on by default for debug version of libraries) (deprecated, use -malloc_debug) 114879dccf82SBarry Smith . -malloc no - Indicates not to use error-checking malloc (deprecated, use -malloc_debug no) 1149811af0c4SBarry Smith . -malloc_debug - check for memory corruption at EVERY malloc or free, see `PetscMallocSetDebug()` 1150aee23540SBarry Smith . -malloc_dump - prints a list of all unfreed memory at the end of the run 115192f119d6SBarry Smith . -malloc_test - like -malloc_dump -malloc_debug, but only active for debugging builds, ignored in optimized build. May want to set in PETSC_OPTIONS environmental variable 1152811af0c4SBarry Smith . -malloc_view - show a list of all allocated memory during `PetscFinalize()` 115392f119d6SBarry Smith . -malloc_view_threshold <t> - only list memory allocations of size greater than t with -malloc_view 1154608c71bfSMatthew G. Knepley . -malloc_requested_size - malloc logging will record the requested size rather than size after alignment 115592f119d6SBarry Smith . -fp_trap - Stops on floating point exceptions 1156e5c89e4eSSatish Balay . -no_signal_handler - Indicates not to trap error signals 1157e5c89e4eSSatish Balay . -shared_tmp - indicates /tmp directory is shared by all processors 1158e5c89e4eSSatish Balay . -not_shared_tmp - each processor has own /tmp 1159e5c89e4eSSatish Balay . -tmp - alternative name of /tmp directory 1160e5c89e4eSSatish Balay . -get_total_flops - returns total flops done by all processors 11610841954dSBarry Smith - -memory_view - Print memory usage at end of run 1162e5c89e4eSSatish Balay 1163c5b5d8d5SVaclav Hapla Options Database Keys for Option Database: 1164c5b5d8d5SVaclav Hapla + -skip_petscrc - skip the default option files ~/.petscrc, .petscrc, petscrc 1165c5b5d8d5SVaclav Hapla . -options_monitor - monitor all set options to standard output for the whole program run 1166811af0c4SBarry Smith - -options_monitor_cancel - cancel options monitoring hard-wired using `PetscOptionsMonitorSet()` 1167c5b5d8d5SVaclav Hapla 1168c5b5d8d5SVaclav Hapla Options -options_monitor_{all,cancel} are 1169c5b5d8d5SVaclav Hapla position-independent and apply to all options set since the PETSc start. 1170c5b5d8d5SVaclav Hapla They can be used also in option files. 1171c5b5d8d5SVaclav Hapla 1172811af0c4SBarry Smith See `PetscOptionsMonitorSet()` to do monitoring programmatically. 1173c5b5d8d5SVaclav Hapla 1174e5c89e4eSSatish Balay Options Database Keys for Profiling: 1175a7f22e61SSatish Balay See Users-Manual: ch_profiling for details. 1176811af0c4SBarry Smith + -info [filename][:[~]<list,of,classnames>[:[~]self]] - Prints verbose information. See `PetscInfo()`. 1177217044c2SLisandro Dalcin . -log_sync - Enable barrier synchronization for all events. This option is useful to debug imbalance within each event, 1178217044c2SLisandro Dalcin however it slows things down and gives a distorted view of the overall runtime. 1179495fc317SBarry Smith . -log_trace [filename] - Print traces of all PETSc calls to the screen (useful to determine where a program 1180811af0c4SBarry Smith hangs without running in the debugger). See `PetscLogTraceBegin()`. 1181811af0c4SBarry Smith . -log_view [:filename:format] - Prints summary of flop and timing information to screen or file, see `PetscLogView()`. 1182811af0c4SBarry Smith . -log_view_memory - Includes in the summary from -log_view the memory used in each event, see `PetscLogView()`. 1183811af0c4SBarry Smith . -log_view_gpu_time - Includes in the summary from -log_view the time used in each GPU kernel, see `PetscLogView(). 11849a9a5d4cSBarry Smith . -log_summary [filename] - (Deprecated, use -log_view) Prints summary of flop and timing information to screen. If the filename is specified the 1185495fc317SBarry Smith summary is written to the file. See PetscLogView(). 1186f5d6ab90SLisandro Dalcin . -log_exclude: <vec,mat,pc,ksp,snes> - excludes subset of object classes from logging 1187811af0c4SBarry Smith . -log_all [filename] - Logs extensive profiling information See `PetscLogDump()`. 1188811af0c4SBarry Smith . -log [filename] - Logs basic profiline information See `PetscLogDump()`. 1189c2f74817SBarry Smith . -log_mpe [filename] - Creates a logfile viewable by the utility Jumpshot (in MPICH distribution) 1190811af0c4SBarry Smith . -viewfromoptions on,off - Enable or disable `XXXSetFromOptions()` calls, for applications with many small solves turn this off 1191c2f74817SBarry Smith - -check_pointer_intensity 0,1,2 - if pointers are checked for validity (debug version only), using 0 will result in faster code 1192495fc317SBarry Smith 11936a77f485SPierre Jolivet Only one of -log_trace, -log_view, -log_all, -log, or -log_mpe may be used at a time 1194e5c89e4eSSatish Balay 1195ffbd1cfbSBarry Smith Options Database Keys for SAWs: 1196ffbd1cfbSBarry Smith + -saws_port <portnumber> - port number to publish SAWs data, default is 8080 1197ffbd1cfbSBarry Smith . -saws_port_auto_select - have SAWs select a new unique port number where it publishes the data, the URL is printed to the screen 1198ffbd1cfbSBarry Smith this is useful when you are running many jobs that utilize SAWs at the same time 1199ffbd1cfbSBarry Smith . -saws_log <filename> - save a log of all SAWs communication 1200ffbd1cfbSBarry Smith . -saws_https <certificate file> - have SAWs use HTTPS instead of HTTP 1201ffbd1cfbSBarry Smith - -saws_root <directory> - allow SAWs to have access to the given directory to search for requested resources and files 1202ffbd1cfbSBarry Smith 1203e5c89e4eSSatish Balay Environmental Variables: 1204811af0c4SBarry Smith + `PETSC_TMP` - alternative tmp directory 1205811af0c4SBarry Smith . `PETSC_SHARED_TMP` - tmp is shared by all processes 1206811af0c4SBarry Smith . `PETSC_NOT_SHARED_TMP` - each process has its own private tmp 1207811af0c4SBarry Smith . `PETSC_OPTIONS` - a string containing additional options for petsc in the form of command line "-key value" pairs 1208811af0c4SBarry Smith . `PETSC_OPTIONS_YAML` - (requires configuring PETSc to use libyaml) a string containing additional options for petsc in the form of a YAML document 1209811af0c4SBarry Smith . `PETSC_VIEWER_SOCKET_PORT` - socket number to use for socket viewer 1210811af0c4SBarry Smith - `PETSC_VIEWER_SOCKET_MACHINE` - machine to use for socket viewer to connect to 1211e5c89e4eSSatish Balay 1212e5c89e4eSSatish Balay Level: beginner 1213e5c89e4eSSatish Balay 1214811af0c4SBarry Smith Note: 1215811af0c4SBarry Smith If for some reason you must call `MPI_Init()` separately, call 1216811af0c4SBarry Smith it before `PetscInitialize()`. 1217e5c89e4eSSatish Balay 1218811af0c4SBarry Smith Fortran Notes: 1219811af0c4SBarry Smith In Fortran this routine can be called with 1220811af0c4SBarry Smith .vb 1221811af0c4SBarry Smith call PetscInitialize(ierr) 1222811af0c4SBarry Smith call PetscInitialize(file,ierr) or 1223811af0c4SBarry Smith call PetscInitialize(file,help,ierr) 1224811af0c4SBarry Smith .ve 1225e5c89e4eSSatish Balay 1226811af0c4SBarry Smith If your main program is C but you call Fortran code that also uses PETSc you need to call `PetscInitializeFortran()` soon after 1227811af0c4SBarry Smith calling `PetscInitialize()`. 1228e5c89e4eSSatish Balay 1229db781477SPatrick Sanan .seealso: `PetscFinalize()`, `PetscInitializeFortran()`, `PetscGetArgs()`, `PetscInitializeNoArguments()`, `PetscLogGpuTime()` 1230e5c89e4eSSatish Balay @*/ 1231d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscInitialize(int *argc, char ***args, const char file[], const char help[]) 1232d71ae5a4SJacob Faibussowitsch { 123385649d77SJunchao Zhang PetscMPIInt flag; 123421ef0414SBarry Smith const char *prog = "Unknown Name", *mpienv; 1235e5c89e4eSSatish Balay 123627104ee2SJacob Faibussowitsch PetscFunctionBegin; 1237*3ba16761SJacob Faibussowitsch if (PetscInitializeCalled) PetscFunctionReturn(PETSC_SUCCESS); 12389566063dSJacob Faibussowitsch PetscCallMPI(MPI_Initialized(&flag)); 1239e5c89e4eSSatish Balay if (!flag) { 124039a651e2SJacob Faibussowitsch PetscCheck(PETSC_COMM_WORLD == MPI_COMM_NULL, PETSC_COMM_SELF, PETSC_ERR_SUP, "You cannot set PETSC_COMM_WORLD if you have not initialized MPI first"); 12419566063dSJacob Faibussowitsch PetscCall(PetscPreMPIInit_Private()); 12425e765c61SJed Brown #if defined(PETSC_HAVE_MPI_INIT_THREAD) 12435e765c61SJed Brown { 124439a651e2SJacob Faibussowitsch PetscMPIInt PETSC_UNUSED provided; 12459566063dSJacob Faibussowitsch PetscCallMPI(MPI_Init_thread(argc, args, PETSC_MPI_THREAD_REQUIRED, &provided)); 12465e765c61SJed Brown } 12475e765c61SJed Brown #else 12489566063dSJacob Faibussowitsch PetscCallMPI(MPI_Init(argc, args)); 12495e765c61SJed Brown #endif 125021ef0414SBarry Smith if (PetscDefined(HAVE_MPIUNI)) { 125121ef0414SBarry Smith mpienv = getenv("PMI_SIZE"); 125221ef0414SBarry Smith if (!mpienv) mpienv = getenv("OMPI_COMM_WORLD_SIZE"); 125321ef0414SBarry Smith if (mpienv) { 125421ef0414SBarry Smith PetscInt isize; 125521ef0414SBarry Smith PetscCall(PetscOptionsStringToInt(mpienv, &isize)); 125621ef0414SBarry Smith if (isize != 1) printf("You are using an MPI-uni (sequential) install of PETSc but trying to launch parallel jobs; you need full MPI version of PETSc\n"); 125721ef0414SBarry Smith PetscCheck(isize == 1, MPI_COMM_SELF, PETSC_ERR_MPI, "You are using an MPI-uni (sequential) install of PETSc but trying to launch parallel jobs; you need full MPI version of PETSc"); 125821ef0414SBarry Smith } 125921ef0414SBarry Smith } 1260e5c89e4eSSatish Balay PetscBeganMPI = PETSC_TRUE; 1261e5c89e4eSSatish Balay } 12624dfee713SSatish Balay 126385649d77SJunchao Zhang if (argc && *argc) prog = **args; 1264e5c89e4eSSatish Balay if (argc && args) { 1265e5c89e4eSSatish Balay PetscGlobalArgc = *argc; 1266e5c89e4eSSatish Balay PetscGlobalArgs = *args; 1267e5c89e4eSSatish Balay } 1268811af0c4SBarry Smith PetscCall(PetscInitialize_Common(prog, file, help, PETSC_FALSE, PETSC_FALSE, 0)); 1269*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1270e5c89e4eSSatish Balay } 1271e5c89e4eSSatish Balay 1272a4af0ceeSJacob Faibussowitsch #if PetscDefined(USE_LOG) 127395c0884eSLisandro Dalcin PETSC_INTERN PetscObject *PetscObjects; 1274ecfe9a72SLisandro Dalcin PETSC_INTERN PetscInt PetscObjectsCounts; 1275ecfe9a72SLisandro Dalcin PETSC_INTERN PetscInt PetscObjectsMaxCounts; 127695c0884eSLisandro Dalcin PETSC_INTERN PetscBool PetscObjectsLog; 12774097062eSBarry Smith #endif 1278e5c89e4eSSatish Balay 1279008a6e76SBarry Smith /* 1280008a6e76SBarry Smith Frees all the MPI types and operations that PETSc may have created 1281008a6e76SBarry Smith */ 1282d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFreeMPIResources(void) 1283d71ae5a4SJacob Faibussowitsch { 1284008a6e76SBarry Smith PetscFunctionBegin; 1285613bf2b2SPierre Jolivet #if defined(PETSC_HAVE_REAL___FLOAT128) 12869566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&MPIU___FLOAT128)); 12879566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&MPIU___COMPLEX128)); 1288008a6e76SBarry Smith #endif 12899e517322SPierre Jolivet #if defined(PETSC_HAVE_REAL___FP16) 12909566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&MPIU___FP16)); 1291008a6e76SBarry Smith #endif 1292008a6e76SBarry Smith 1293de272c7aSSatish Balay #if defined(PETSC_USE_REAL___FLOAT128) || defined(PETSC_USE_REAL___FP16) 12949566063dSJacob Faibussowitsch PetscCallMPI(MPI_Op_free(&MPIU_SUM)); 1295613bf2b2SPierre Jolivet PetscCallMPI(MPI_Op_free(&MPIU_MAX)); 1296613bf2b2SPierre Jolivet PetscCallMPI(MPI_Op_free(&MPIU_MIN)); 12979e517322SPierre Jolivet #elif defined(PETSC_HAVE_REAL___FLOAT128) || defined(PETSC_HAVE_REAL___FP16) 12989e517322SPierre Jolivet PetscCallMPI(MPI_Op_free(&MPIU_SUM___FP16___FLOAT128)); 1299008a6e76SBarry Smith #endif 1300008a6e76SBarry Smith 13019566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&MPIU_2SCALAR)); 13029566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&MPIU_REAL_INT)); 13039566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&MPIU_SCALAR_INT)); 130440df0d72SSatish Balay #if defined(PETSC_USE_64BIT_INDICES) 13059566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&MPIU_2INT)); 1306008a6e76SBarry Smith #endif 13079566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&MPI_4INT)); 13089566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&MPIU_4INT)); 13099566063dSJacob Faibussowitsch PetscCallMPI(MPI_Op_free(&MPIU_MAXSUM_OP)); 131062e5d2d2SJDBetteridge PetscCallMPI(MPI_Op_free(&Petsc_Garbage_SetIntersectOp)); 1311*3ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1312008a6e76SBarry Smith } 1313008a6e76SBarry Smith 1314a4af0ceeSJacob Faibussowitsch #if PetscDefined(USE_LOG) 1315a4af0ceeSJacob Faibussowitsch PETSC_INTERN PetscErrorCode PetscLogFinalize(void); 1316a4af0ceeSJacob Faibussowitsch #endif 1317a4af0ceeSJacob Faibussowitsch 1318e5c89e4eSSatish Balay /*@C 1319e5c89e4eSSatish Balay PetscFinalize - Checks for options to be called at the conclusion 1320811af0c4SBarry Smith of the program. `MPI_Finalize()` is called only if the user had not 1321811af0c4SBarry Smith called `MPI_Init()` before calling `PetscInitialize()`. 1322e5c89e4eSSatish Balay 1323811af0c4SBarry Smith Collective on `PETSC_COMM_WORLD` 1324e5c89e4eSSatish Balay 1325e5c89e4eSSatish Balay Options Database Keys: 1326811af0c4SBarry Smith + -options_view - Calls `PetscOptionsView()` 1327e5c89e4eSSatish Balay . -options_left - Prints unused options that remain in the database 13287eb1d149SBarry 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 1329e5c89e4eSSatish Balay . -mpidump - Calls PetscMPIDump() 1330811af0c4SBarry Smith . -malloc_dump <optional filename> - Calls `PetscMallocDump()`, displays all memory allocated that has not been freed 1331e5c89e4eSSatish Balay . -malloc_info - Prints total memory usage 133292f119d6SBarry Smith - -malloc_view <optional filename> - Prints list of all memory allocated and where 1333e5c89e4eSSatish Balay 1334e5c89e4eSSatish Balay Level: beginner 1335e5c89e4eSSatish Balay 1336e5c89e4eSSatish Balay Note: 1337811af0c4SBarry Smith See `PetscInitialize()` for other runtime options. 1338e5c89e4eSSatish Balay 1339db781477SPatrick Sanan .seealso: `PetscInitialize()`, `PetscOptionsView()`, `PetscMallocDump()`, `PetscMPIDump()`, `PetscEnd()` 1340e5c89e4eSSatish Balay @*/ 1341d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscFinalize(void) 1342d71ae5a4SJacob Faibussowitsch { 13434bb5149bSJed Brown PetscMPIInt rank; 1344a8d2bbe5SBarry Smith PetscInt nopt; 13452bf49c77SBarry Smith PetscBool flg1 = PETSC_FALSE, flg2 = PETSC_FALSE, flg3 = PETSC_FALSE; 1346dff31646SBarry Smith PetscBool flg; 134710463e74SBarry Smith #if defined(PETSC_USE_LOG) 134810463e74SBarry Smith char mname[PETSC_MAX_PATH_LEN]; 134910463e74SBarry Smith #endif 1350e5c89e4eSSatish Balay 13513cbbc5ffSBarry Smith PetscFunctionBegin; 135239a651e2SJacob Faibussowitsch PetscCheck(PetscInitializeCalled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "PetscInitialize() must be called before PetscFinalize()"); 13539566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL, "PetscFinalize() called\n")); 1354b022a5c1SBarry Smith 1355f1f2ae84SBarry Smith PetscCall(PetscOptionsHasName(NULL, NULL, "-mpi_linear_solver_server", &flg)); 1356f1f2ae84SBarry Smith if (PetscDefined(USE_SINGLE_LIBRARY) && flg) PetscCall(PCMPIServerEnd()); 1357f1f2ae84SBarry Smith 135862e5d2d2SJDBetteridge /* Clean up Garbage automatically on COMM_SELF and COMM_WORLD at finalize */ 135962e5d2d2SJDBetteridge { 136062e5d2d2SJDBetteridge union 136162e5d2d2SJDBetteridge { 136262e5d2d2SJDBetteridge MPI_Comm comm; 136362e5d2d2SJDBetteridge void *ptr; 136462e5d2d2SJDBetteridge } ucomm; 136562e5d2d2SJDBetteridge PetscMPIInt flg; 136662e5d2d2SJDBetteridge void *tmp; 136762e5d2d2SJDBetteridge 136862e5d2d2SJDBetteridge PetscCallMPI(MPI_Comm_get_attr(PETSC_COMM_SELF, Petsc_InnerComm_keyval, &ucomm, &flg)); 136962e5d2d2SJDBetteridge if (flg) PetscCallMPI(MPI_Comm_get_attr(ucomm.comm, Petsc_Garbage_HMap_keyval, &tmp, &flg)); 137062e5d2d2SJDBetteridge if (flg) PetscCall(PetscGarbageCleanup(PETSC_COMM_SELF)); 137162e5d2d2SJDBetteridge PetscCallMPI(MPI_Comm_get_attr(PETSC_COMM_WORLD, Petsc_InnerComm_keyval, &ucomm, &flg)); 137262e5d2d2SJDBetteridge if (flg) PetscCallMPI(MPI_Comm_get_attr(ucomm.comm, Petsc_Garbage_HMap_keyval, &tmp, &flg)); 137362e5d2d2SJDBetteridge if (flg) PetscCall(PetscGarbageCleanup(PETSC_COMM_WORLD)); 137462e5d2d2SJDBetteridge } 137562e5d2d2SJDBetteridge 13769566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 1377a56f64adSBarry Smith #if defined(PETSC_HAVE_ADIOS) 1378*3ba16761SJacob Faibussowitsch PetscCallExternal(adios_read_finalize_method, ADIOS_READ_METHOD_BP_AGGREGATE); 1379*3ba16761SJacob Faibussowitsch PetscCallExternal(adios_finalize, rank); 1380a56f64adSBarry Smith #endif 13819566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(NULL, NULL, "-citations", &flg)); 1382dff31646SBarry Smith if (flg) { 13831f817a21SBarry Smith char *cits, filename[PETSC_MAX_PATH_LEN]; 13841f817a21SBarry Smith FILE *fd = PETSC_STDOUT; 13851f817a21SBarry Smith 13869566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-citations", filename, sizeof(filename), NULL)); 138748a46eb9SPierre Jolivet if (filename[0]) PetscCall(PetscFOpen(PETSC_COMM_WORLD, filename, "w", &fd)); 13889566063dSJacob Faibussowitsch PetscCall(PetscSegBufferGet(PetscCitationsList, 1, &cits)); 1389dff31646SBarry Smith cits[0] = 0; 13909566063dSJacob Faibussowitsch PetscCall(PetscSegBufferExtractAlloc(PetscCitationsList, &cits)); 13919566063dSJacob Faibussowitsch PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "If you publish results based on this computation please cite the following:\n")); 13929566063dSJacob Faibussowitsch PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "===========================================================================\n")); 13939566063dSJacob Faibussowitsch PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "%s", cits)); 13949566063dSJacob Faibussowitsch PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "===========================================================================\n")); 13959566063dSJacob Faibussowitsch PetscCall(PetscFClose(PETSC_COMM_WORLD, fd)); 13969566063dSJacob Faibussowitsch PetscCall(PetscFree(cits)); 1397dff31646SBarry Smith } 13989566063dSJacob Faibussowitsch PetscCall(PetscSegBufferDestroy(&PetscCitationsList)); 1399dff31646SBarry Smith 1400c2a97968SBarry Smith #if defined(PETSC_HAVE_SSL) && defined(PETSC_USE_SOCKET_VIEWER) 140104102261SBarry Smith /* TextBelt is run for testing purposes only, please do not use this feature often */ 140204102261SBarry Smith { 140304102261SBarry Smith PetscInt nmax = 2; 140404102261SBarry Smith char **buffs; 14059566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(2, &buffs)); 14069566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetStringArray(NULL, NULL, "-textbelt", buffs, &nmax, &flg1)); 140704102261SBarry Smith if (flg1) { 140828b400f6SJacob Faibussowitsch PetscCheck(nmax, PETSC_COMM_WORLD, PETSC_ERR_USER, "-textbelt requires either the phone number or number,\"message\""); 140904102261SBarry Smith if (nmax == 1) { 14109566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(128, &buffs[1])); 14119566063dSJacob Faibussowitsch PetscCall(PetscGetProgramName(buffs[1], 32)); 14129566063dSJacob Faibussowitsch PetscCall(PetscStrcat(buffs[1], " has completed")); 141304102261SBarry Smith } 14149566063dSJacob Faibussowitsch PetscCall(PetscTextBelt(PETSC_COMM_WORLD, buffs[0], buffs[1], NULL)); 14159566063dSJacob Faibussowitsch PetscCall(PetscFree(buffs[0])); 14169566063dSJacob Faibussowitsch PetscCall(PetscFree(buffs[1])); 141704102261SBarry Smith } 14189566063dSJacob Faibussowitsch PetscCall(PetscFree(buffs)); 141904102261SBarry Smith } 1420763ec7b1SBarry Smith { 1421763ec7b1SBarry Smith PetscInt nmax = 2; 1422763ec7b1SBarry Smith char **buffs; 14239566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(2, &buffs)); 14249566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetStringArray(NULL, NULL, "-tellmycell", buffs, &nmax, &flg1)); 1425763ec7b1SBarry Smith if (flg1) { 142628b400f6SJacob Faibussowitsch PetscCheck(nmax, PETSC_COMM_WORLD, PETSC_ERR_USER, "-tellmycell requires either the phone number or number,\"message\""); 1427763ec7b1SBarry Smith if (nmax == 1) { 14289566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(128, &buffs[1])); 14299566063dSJacob Faibussowitsch PetscCall(PetscGetProgramName(buffs[1], 32)); 14309566063dSJacob Faibussowitsch PetscCall(PetscStrcat(buffs[1], " has completed")); 1431763ec7b1SBarry Smith } 14329566063dSJacob Faibussowitsch PetscCall(PetscTellMyCell(PETSC_COMM_WORLD, buffs[0], buffs[1], NULL)); 14339566063dSJacob Faibussowitsch PetscCall(PetscFree(buffs[0])); 14349566063dSJacob Faibussowitsch PetscCall(PetscFree(buffs[1])); 1435763ec7b1SBarry Smith } 14369566063dSJacob Faibussowitsch PetscCall(PetscFree(buffs)); 1437763ec7b1SBarry Smith } 143804102261SBarry Smith #endif 143904102261SBarry Smith 14402d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS) 14419566063dSJacob Faibussowitsch PetscCall(PetscFPTDestroy()); 14422d53ad75SBarry Smith #endif 14432d53ad75SBarry Smith 1444e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1445dff31646SBarry Smith flg = PETSC_FALSE; 14469566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-saw_options", &flg, NULL)); 14471baa6e33SBarry Smith if (flg) PetscCall(PetscOptionsSAWsDestroy()); 1448d5649816SBarry Smith #endif 1449d5649816SBarry Smith 1450681455b2SBarry Smith #if defined(PETSC_HAVE_X) 1451681455b2SBarry Smith flg1 = PETSC_FALSE; 14529566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-x_virtual", &flg1, NULL)); 1453681455b2SBarry Smith if (flg1) { 1454681455b2SBarry Smith /* this is a crude hack, but better than nothing */ 14559566063dSJacob Faibussowitsch PetscCall(PetscPOpen(PETSC_COMM_WORLD, NULL, "pkill -9 Xvfb", "r", NULL)); 1456681455b2SBarry Smith } 1457681455b2SBarry Smith #endif 1458681455b2SBarry Smith 145967584ceeSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY) 14609566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_info", &flg2, NULL)); 1461e5c89e4eSSatish Balay if (!flg2) { 146290d69ab7SBarry Smith flg2 = PETSC_FALSE; 14639566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-memory_view", &flg2, NULL)); 1464e5c89e4eSSatish Balay } 146548a46eb9SPierre Jolivet if (flg2) PetscCall(PetscMemoryView(PETSC_VIEWER_STDOUT_WORLD, "Summary of Memory Usage in PETSc\n")); 146667584ceeSBarry Smith #endif 1467e5c89e4eSSatish Balay 1468e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG) 146990d69ab7SBarry Smith flg1 = PETSC_FALSE; 14709566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-get_total_flops", &flg1, NULL)); 1471e5c89e4eSSatish Balay if (flg1) { 1472e5c89e4eSSatish Balay PetscLogDouble flops = 0; 14739566063dSJacob Faibussowitsch PetscCallMPI(MPI_Reduce(&petsc_TotalFlops, &flops, 1, MPI_DOUBLE, MPI_SUM, 0, PETSC_COMM_WORLD)); 14749566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Total flops over all processors %g\n", flops)); 1475e5c89e4eSSatish Balay } 1476e5c89e4eSSatish Balay #endif 1477e5c89e4eSSatish Balay 1478e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG) 1479e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE) 1480e5c89e4eSSatish Balay mname[0] = 0; 14819566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-log_mpe", mname, sizeof(mname), &flg1)); 1482e5c89e4eSSatish Balay if (flg1) { 14839566063dSJacob Faibussowitsch if (mname[0]) PetscCall(PetscLogMPEDump(mname)); 14849566063dSJacob Faibussowitsch else PetscCall(PetscLogMPEDump(0)); 1485e5c89e4eSSatish Balay } 1486e5c89e4eSSatish Balay #endif 1487356e5837SBarry Smith #endif 1488a297a907SKarl Rupp 1489dd710f27SBarry Smith /* 1490dd710f27SBarry Smith Free all objects registered with PetscObjectRegisterDestroy() such as PETSC_VIEWER_XXX_(). 1491dd710f27SBarry Smith */ 14929566063dSJacob Faibussowitsch PetscCall(PetscObjectRegisterDestroyAll()); 1493dd710f27SBarry Smith 1494356e5837SBarry Smith #if defined(PETSC_USE_LOG) 14959566063dSJacob Faibussowitsch PetscCall(PetscOptionsPushGetViewerOff(PETSC_FALSE)); 14969566063dSJacob Faibussowitsch PetscCall(PetscLogViewFromOptions()); 14979566063dSJacob Faibussowitsch PetscCall(PetscOptionsPopGetViewerOff()); 149887c3beb6SLisandro Dalcin 1499356e5837SBarry Smith mname[0] = 0; 15009566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-log_summary", mname, sizeof(mname), &flg1)); 1501e5c89e4eSSatish Balay if (flg1) { 150291eabc43SBarry Smith PetscViewer viewer; 15039566063dSJacob Faibussowitsch PetscCall((*PetscHelpPrintf)(PETSC_COMM_WORLD, "\n\n WARNING: -log_summary is being deprecated; switch to -log_view\n\n\n")); 150491eabc43SBarry Smith if (mname[0]) { 15059566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIOpen(PETSC_COMM_WORLD, mname, &viewer)); 15069566063dSJacob Faibussowitsch PetscCall(PetscLogView(viewer)); 15079566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 150833f85c2fSBarry Smith } else { 150933f85c2fSBarry Smith viewer = PETSC_VIEWER_STDOUT_WORLD; 15109566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_DEFAULT)); 15119566063dSJacob Faibussowitsch PetscCall(PetscLogView(viewer)); 15129566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 151333f85c2fSBarry Smith } 1514e5c89e4eSSatish Balay } 1515a297a907SKarl Rupp 1516dd710f27SBarry Smith /* 1517dd710f27SBarry Smith Free any objects created by the last block of code. 1518dd710f27SBarry Smith */ 15199566063dSJacob Faibussowitsch PetscCall(PetscObjectRegisterDestroyAll()); 1520dd710f27SBarry Smith 1521dd710f27SBarry Smith mname[0] = 0; 15229566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-log_all", mname, sizeof(mname), &flg1)); 15239566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-log", mname, sizeof(mname), &flg2)); 15249566063dSJacob Faibussowitsch if (flg1 || flg2) PetscCall(PetscLogDump(mname)); 1525e5c89e4eSSatish Balay #endif 152610463e74SBarry Smith 152790d69ab7SBarry Smith flg1 = PETSC_FALSE; 15289566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-no_signal_handler", &flg1, NULL)); 15299566063dSJacob Faibussowitsch if (!flg1) PetscCall(PetscPopSignalHandler()); 153090d69ab7SBarry Smith flg1 = PETSC_FALSE; 15319566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-mpidump", &flg1, NULL)); 15321baa6e33SBarry Smith if (flg1) PetscCall(PetscMPIDump(stdout)); 153390d69ab7SBarry Smith flg1 = PETSC_FALSE; 153490d69ab7SBarry Smith flg2 = PETSC_FALSE; 15358bb29257SSatish Balay /* preemptive call to avoid listing this option in options table as unused */ 15369566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(NULL, NULL, "-malloc_dump", &flg1)); 15379566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(NULL, NULL, "-objects_dump", &flg1)); 15389566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-options_view", &flg2, NULL)); 1539e4c476e2SSatish Balay 1540e5c89e4eSSatish Balay if (flg2) { 1541be56827dSJed Brown PetscViewer viewer; 15429566063dSJacob Faibussowitsch PetscCall(PetscViewerCreate(PETSC_COMM_WORLD, &viewer)); 15439566063dSJacob Faibussowitsch PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII)); 15449566063dSJacob Faibussowitsch PetscCall(PetscOptionsView(NULL, viewer)); 15459566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 1546e5c89e4eSSatish Balay } 1547e5c89e4eSSatish Balay 1548e5c89e4eSSatish Balay /* to prevent PETSc -options_left from warning */ 15499566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(NULL, NULL, "-nox", &flg1)); 15509566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(NULL, NULL, "-nox_warning", &flg1)); 1551e5c89e4eSSatish Balay 155233fc4174SSatish Balay flg3 = PETSC_FALSE; /* default value is required */ 15539566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-options_left", &flg3, &flg1)); 15549245e749SBarry Smith if (PetscUnlikelyDebug(!flg1)) flg3 = PETSC_TRUE; 1555e5c89e4eSSatish Balay if (flg3) { 15563de2bfdfSBarry Smith if (!flg2 && flg1) { /* have not yet printed the options */ 1557be56827dSJed Brown PetscViewer viewer; 15589566063dSJacob Faibussowitsch PetscCall(PetscViewerCreate(PETSC_COMM_WORLD, &viewer)); 15599566063dSJacob Faibussowitsch PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII)); 15609566063dSJacob Faibussowitsch PetscCall(PetscOptionsView(NULL, viewer)); 15619566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 1562e5c89e4eSSatish Balay } 15639566063dSJacob Faibussowitsch PetscCall(PetscOptionsAllUsed(NULL, &nopt)); 15643de2bfdfSBarry Smith if (nopt) { 15659566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_WORLD, "WARNING! There are options you set that were not used!\n")); 15669566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_WORLD, "WARNING! could be spelling mistake, etc!\n")); 15673de2bfdfSBarry Smith if (nopt == 1) { 15689566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_WORLD, "There is one unused database option. It is:\n")); 1569e5c89e4eSSatish Balay } else { 15709566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_WORLD, "There are %" PetscInt_FMT " unused database options. They are:\n", nopt)); 1571e5c89e4eSSatish Balay } 15723de2bfdfSBarry Smith } else if (flg3 && flg1) { 15739566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_WORLD, "There are no unused options.\n")); 1574df12ba86SBarry Smith } 15759566063dSJacob Faibussowitsch PetscCall(PetscOptionsLeft(NULL)); 1576e5c89e4eSSatish Balay } 1577e5c89e4eSSatish Balay 1578e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1579d45a07a7SBarry Smith if (!PetscGlobalRank) { 15809566063dSJacob Faibussowitsch PetscCall(PetscStackSAWsViewOff()); 1581792fecdfSBarry Smith PetscCallSAWs(SAWs_Finalize, ()); 1582d45a07a7SBarry Smith } 1583ec957eceSBarry Smith #endif 1584ec957eceSBarry Smith 15854097062eSBarry Smith #if defined(PETSC_USE_LOG) 158610463e74SBarry Smith /* 1587dbc8283eSBarry Smith List all objects the user may have forgot to free 15882eff7a51SBarry Smith */ 158905df10baSBarry Smith if (PetscObjectsLog) { 15909566063dSJacob Faibussowitsch PetscCall(PetscOptionsHasName(NULL, NULL, "-objects_dump", &flg1)); 1591a64a8e02SBarry Smith if (flg1) { 1592a64a8e02SBarry Smith MPI_Comm local_comm; 15937eb1d149SBarry Smith char string[64]; 1594a64a8e02SBarry Smith 15959566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-objects_dump", string, sizeof(string), NULL)); 1596afd7ed4bSMatthew G. Knepley PetscCallMPI(MPI_Comm_dup(PETSC_COMM_WORLD, &local_comm)); 15979566063dSJacob Faibussowitsch PetscCall(PetscSequentialPhaseBegin_Private(local_comm, 1)); 15989566063dSJacob Faibussowitsch PetscCall(PetscObjectsDump(stdout, (string[0] == 'a') ? PETSC_TRUE : PETSC_FALSE)); 15999566063dSJacob Faibussowitsch PetscCall(PetscSequentialPhaseEnd_Private(local_comm, 1)); 16009566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_free(&local_comm)); 16010a1571b3SBarry Smith } 160205df10baSBarry Smith } 16034097062eSBarry Smith #endif 16044097062eSBarry Smith 16054097062eSBarry Smith #if defined(PETSC_USE_LOG) 1606dbc8283eSBarry Smith PetscObjectsCounts = 0; 1607dbc8283eSBarry Smith PetscObjectsMaxCounts = 0; 16089566063dSJacob Faibussowitsch PetscCall(PetscFree(PetscObjects)); 16094097062eSBarry Smith #endif 16102eff7a51SBarry Smith 161133f85c2fSBarry Smith /* 161233f85c2fSBarry Smith Destroy any packages that registered a finalize 161333f85c2fSBarry Smith */ 16149566063dSJacob Faibussowitsch PetscCall(PetscRegisterFinalizeAll()); 161533f85c2fSBarry Smith 1616101409b8SToby Isaac #if defined(PETSC_USE_LOG) 16179566063dSJacob Faibussowitsch PetscCall(PetscLogFinalize()); 1618101409b8SToby Isaac #endif 1619101409b8SToby Isaac 162033f85c2fSBarry Smith /* 162148dd1dffSBarry Smith Print PetscFunctionLists that have not been properly freed 162248dd1dffSBarry Smith */ 16232e956fe4SStefano Zampini if (PetscPrintFunctionList) PetscCall(PetscFunctionListPrintAll()); 162437e93019SBarry Smith 16254028d114SSatish Balay if (petsc_history) { 16269566063dSJacob Faibussowitsch PetscCall(PetscCloseHistoryFile(&petsc_history)); 162702c9f0b5SLisandro Dalcin petsc_history = NULL; 1628e5c89e4eSSatish Balay } 16299566063dSJacob Faibussowitsch PetscCall(PetscOptionsHelpPrintedDestroy(&PetscOptionsHelpPrintedSingleton)); 16309566063dSJacob Faibussowitsch PetscCall(PetscInfoDestroy()); 1631e5c89e4eSSatish Balay 163267584ceeSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY) 163392f119d6SBarry Smith if (!(PETSC_RUNNING_ON_VALGRIND)) { 1634e5c89e4eSSatish Balay char fname[PETSC_MAX_PATH_LEN]; 163592f119d6SBarry Smith char sname[PETSC_MAX_PATH_LEN]; 1636e5c89e4eSSatish Balay FILE *fd; 1637ed9cf6e9SBarry Smith int err; 1638e5c89e4eSSatish Balay 1639dc92acbaSJed Brown flg2 = PETSC_FALSE; 164092f119d6SBarry Smith flg3 = PETSC_FALSE; 16419566063dSJacob Faibussowitsch if (PetscDefined(USE_DEBUG)) PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_test", &flg2, NULL)); 16429566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(NULL, NULL, "-malloc_debug", &flg3, NULL)); 164392f119d6SBarry Smith fname[0] = 0; 16449566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-malloc_dump", fname, sizeof(fname), &flg1)); 1645e5c89e4eSSatish Balay if (flg1 && fname[0]) { 1646*3ba16761SJacob Faibussowitsch PetscCall(PetscSNPrintf(sname, sizeof(sname), "%s_%d", fname, rank)); 16479371c9d4SSatish Balay fd = fopen(sname, "w"); 16489371c9d4SSatish Balay PetscCheck(fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open log file: %s", sname); 16499566063dSJacob Faibussowitsch PetscCall(PetscMallocDump(fd)); 1650ed9cf6e9SBarry Smith err = fclose(fd); 165128b400f6SJacob Faibussowitsch PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_SYS, "fclose() failed on file"); 165292f119d6SBarry Smith } else if (flg1 || flg2 || flg3) { 1653e5c89e4eSSatish Balay MPI_Comm local_comm; 1654e5c89e4eSSatish Balay 1655afd7ed4bSMatthew G. Knepley PetscCallMPI(MPI_Comm_dup(PETSC_COMM_WORLD, &local_comm)); 16569566063dSJacob Faibussowitsch PetscCall(PetscSequentialPhaseBegin_Private(local_comm, 1)); 16579566063dSJacob Faibussowitsch PetscCall(PetscMallocDump(stdout)); 16589566063dSJacob Faibussowitsch PetscCall(PetscSequentialPhaseEnd_Private(local_comm, 1)); 16599566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_free(&local_comm)); 1660e5c89e4eSSatish Balay } 1661e5c89e4eSSatish Balay fname[0] = 0; 16629566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString(NULL, NULL, "-malloc_view", fname, sizeof(fname), &flg1)); 1663e5c89e4eSSatish Balay if (flg1 && fname[0]) { 1664*3ba16761SJacob Faibussowitsch PetscCall(PetscSNPrintf(sname, sizeof(sname), "%s_%d", fname, rank)); 16659371c9d4SSatish Balay fd = fopen(sname, "w"); 16669371c9d4SSatish Balay PetscCheck(fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open log file: %s", sname); 16679566063dSJacob Faibussowitsch PetscCall(PetscMallocView(fd)); 1668ed9cf6e9SBarry Smith err = fclose(fd); 166928b400f6SJacob Faibussowitsch PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_SYS, "fclose() failed on file"); 167092f119d6SBarry Smith } else if (flg1) { 167192f119d6SBarry Smith MPI_Comm local_comm; 167292f119d6SBarry Smith 1673afd7ed4bSMatthew G. Knepley PetscCallMPI(MPI_Comm_dup(PETSC_COMM_WORLD, &local_comm)); 16749566063dSJacob Faibussowitsch PetscCall(PetscSequentialPhaseBegin_Private(local_comm, 1)); 16759566063dSJacob Faibussowitsch PetscCall(PetscMallocView(stdout)); 16769566063dSJacob Faibussowitsch PetscCall(PetscSequentialPhaseEnd_Private(local_comm, 1)); 16779566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_free(&local_comm)); 1678e5c89e4eSSatish Balay } 1679e5c89e4eSSatish Balay } 168067584ceeSBarry Smith #endif 168120e2c332SMatthew G. Knepley 16825486ca60SMatthew G. Knepley /* 16835486ca60SMatthew G. Knepley Close any open dynamic libraries 16845486ca60SMatthew G. Knepley */ 16859566063dSJacob Faibussowitsch PetscCall(PetscFinalize_DynamicLibraries()); 16865486ca60SMatthew G. Knepley 1687e5c89e4eSSatish Balay /* Can be destroyed only after all the options are used */ 16889566063dSJacob Faibussowitsch PetscCall(PetscOptionsDestroyDefault()); 1689e5c89e4eSSatish Balay 1690e5c89e4eSSatish Balay PetscGlobalArgc = 0; 169102c9f0b5SLisandro Dalcin PetscGlobalArgs = NULL; 1692e5c89e4eSSatish Balay 1693c2b86a48SJunchao Zhang #if defined(PETSC_HAVE_KOKKOS) 1694c2b86a48SJunchao Zhang if (PetscBeganKokkos) { 16959566063dSJacob Faibussowitsch PetscCall(PetscKokkosFinalize_Private()); 1696c2b86a48SJunchao Zhang PetscBeganKokkos = PETSC_FALSE; 169745639126SStefano Zampini PetscKokkosInitialized = PETSC_FALSE; 1698c2b86a48SJunchao Zhang } 1699c2b86a48SJunchao Zhang #endif 1700c2b86a48SJunchao Zhang 170171438e86SJunchao Zhang #if defined(PETSC_HAVE_NVSHMEM) 170271438e86SJunchao Zhang if (PetscBeganNvshmem) { 17039566063dSJacob Faibussowitsch PetscCall(PetscNvshmemFinalize()); 170471438e86SJunchao Zhang PetscBeganNvshmem = PETSC_FALSE; 170571438e86SJunchao Zhang } 170671438e86SJunchao Zhang #endif 1707a0e72f99SJunchao Zhang 17089566063dSJacob Faibussowitsch PetscCall(PetscFreeMPIResources()); 1709e5c89e4eSSatish Balay 1710dbc8283eSBarry Smith /* 1711efb80d3cSBarry Smith Destroy any known inner MPI_Comm's and attributes pointing to them 1712efb80d3cSBarry Smith Note this will not destroy any new communicators the user has created. 1713efb80d3cSBarry Smith 1714efb80d3cSBarry Smith If all PETSc objects were not destroyed those left over objects will have hanging references to 1715efb80d3cSBarry Smith the MPI_Comms that were freed; but that is ok because those PETSc objects will never be used again 1716dbc8283eSBarry Smith */ 1717b770b1f6SSatish Balay { 1718dbc8283eSBarry Smith PetscCommCounter *counter; 1719dbc8283eSBarry Smith PetscMPIInt flg; 1720dbc8283eSBarry Smith MPI_Comm icomm; 17219371c9d4SSatish Balay union 17229371c9d4SSatish Balay { 17239371c9d4SSatish Balay MPI_Comm comm; 17249371c9d4SSatish Balay void *ptr; 17259371c9d4SSatish Balay } ucomm; 17269566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_get_attr(PETSC_COMM_SELF, Petsc_InnerComm_keyval, &ucomm, &flg)); 1727dbc8283eSBarry Smith if (flg) { 1728265f3f35SJed Brown icomm = ucomm.comm; 17299566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_get_attr(icomm, Petsc_Counter_keyval, &counter, &flg)); 173028b400f6SJacob Faibussowitsch PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory"); 1731dbc8283eSBarry Smith 17329566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_delete_attr(PETSC_COMM_SELF, Petsc_InnerComm_keyval)); 17339566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_delete_attr(icomm, Petsc_Counter_keyval)); 17349566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_free(&icomm)); 1735dbc8283eSBarry Smith } 17369566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_get_attr(PETSC_COMM_WORLD, Petsc_InnerComm_keyval, &ucomm, &flg)); 1737dbc8283eSBarry Smith if (flg) { 1738265f3f35SJed Brown icomm = ucomm.comm; 17399566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_get_attr(icomm, Petsc_Counter_keyval, &counter, &flg)); 174028b400f6SJacob Faibussowitsch PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_ARG_CORRUPT, "Inner MPI_Comm does not have expected tag/name counter, problem with corrupted memory"); 1741dbc8283eSBarry Smith 17429566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_delete_attr(PETSC_COMM_WORLD, Petsc_InnerComm_keyval)); 17439566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_delete_attr(icomm, Petsc_Counter_keyval)); 17449566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_free(&icomm)); 1745dbc8283eSBarry Smith } 1746b770b1f6SSatish Balay } 1747dbc8283eSBarry Smith 17489566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Counter_keyval)); 17499566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_free_keyval(&Petsc_InnerComm_keyval)); 17509566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_free_keyval(&Petsc_OuterComm_keyval)); 17519566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_free_keyval(&Petsc_ShmComm_keyval)); 175262e5d2d2SJDBetteridge PetscCallMPI(MPI_Comm_free_keyval(&Petsc_CreationIdx_keyval)); 175362e5d2d2SJDBetteridge PetscCallMPI(MPI_Comm_free_keyval(&Petsc_Garbage_HMap_keyval)); 1754480cf27aSJed Brown 17559566063dSJacob Faibussowitsch PetscCall(PetscSpinlockDestroy(&PetscViewerASCIISpinLockOpen)); 17569566063dSJacob Faibussowitsch PetscCall(PetscSpinlockDestroy(&PetscViewerASCIISpinLockStdout)); 17579566063dSJacob Faibussowitsch PetscCall(PetscSpinlockDestroy(&PetscViewerASCIISpinLockStderr)); 17589566063dSJacob Faibussowitsch PetscCall(PetscSpinlockDestroy(&PetscCommSpinLock)); 1759ef19f930SBarry Smith 1760e5c89e4eSSatish Balay if (PetscBeganMPI) { 176199b1327fSBarry Smith PetscMPIInt flag; 17629566063dSJacob Faibussowitsch PetscCallMPI(MPI_Finalized(&flag)); 176339a651e2SJacob Faibussowitsch PetscCheck(!flag, PETSC_COMM_SELF, PETSC_ERR_LIB, "MPI_Finalize() has already been called, even though MPI_Init() was called by PetscInitialize()"); 176439a651e2SJacob Faibussowitsch /* wait until the very last moment to disable error handling */ 176539a651e2SJacob Faibussowitsch PetscErrorHandlingInitialized = PETSC_FALSE; 17669566063dSJacob Faibussowitsch PetscCallMPI(MPI_Finalize()); 176739a651e2SJacob Faibussowitsch } else PetscErrorHandlingInitialized = PETSC_FALSE; 176839a651e2SJacob Faibussowitsch 1769e5c89e4eSSatish Balay /* 1770e5c89e4eSSatish Balay 1771e5c89e4eSSatish Balay Note: In certain cases PETSC_COMM_WORLD is never MPI_Comm_free()ed because 1772e5c89e4eSSatish Balay the communicator has some outstanding requests on it. Specifically if the 1773e5c89e4eSSatish Balay flag PETSC_HAVE_BROKEN_REQUEST_FREE is set (for IBM MPI implementation). See 1774e5c89e4eSSatish Balay src/vec/utils/vpscat.c. Due to this the memory allocated in PetscCommDuplicate() 1775e5c89e4eSSatish Balay is never freed as it should be. Thus one may obtain messages of the form 17760e5e90baSSatish Balay [ 1] 8 bytes PetscCommDuplicate() line 645 in src/sys/mpiu.c indicating the 1777e5c89e4eSSatish Balay memory was not freed. 1778e5c89e4eSSatish Balay 1779e5c89e4eSSatish Balay */ 17809566063dSJacob Faibussowitsch PetscCall(PetscMallocClear()); 17819566063dSJacob Faibussowitsch PetscCall(PetscStackReset()); 1782a297a907SKarl Rupp 1783e5c89e4eSSatish Balay PetscInitializeCalled = PETSC_FALSE; 1784e5c89e4eSSatish Balay PetscFinalizeCalled = PETSC_TRUE; 17857ce81a4bSJacob Faibussowitsch #if defined(PETSC_USE_COVERAGE) 178656883f60SBarry Smith /* 178756883f60SBarry Smith flush gcov, otherwise during CI the flushing continues into the next pipeline resulting in git not being able to delete directories since the 178856883f60SBarry Smith gcov files are still being added to the directories as git tries to remove the directories. 178956883f60SBarry Smith */ 179056883f60SBarry Smith __gcov_flush(); 179156883f60SBarry Smith #endif 17921724198aSStefano Zampini /* To match PetscFunctionBegin() at the beginning of this function */ 17931724198aSStefano Zampini PetscStackClearTop; 1794*3ba16761SJacob Faibussowitsch return PETSC_SUCCESS; 1795e5c89e4eSSatish Balay } 1796e5c89e4eSSatish Balay 179743db4dbbSBarry Smith #if defined(PETSC_MISSING_LAPACK_lsame_) 1798d71ae5a4SJacob Faibussowitsch PETSC_EXTERN int lsame_(char *a, char *b) 1799d71ae5a4SJacob Faibussowitsch { 180043db4dbbSBarry Smith if (*a == *b) return 1; 180143db4dbbSBarry Smith if (*a + 32 == *b) return 1; 180243db4dbbSBarry Smith if (*a - 32 == *b) return 1; 180343db4dbbSBarry Smith return 0; 180443db4dbbSBarry Smith } 1805a70650f6SBarry Smith #endif 180643db4dbbSBarry Smith 180743db4dbbSBarry Smith #if defined(PETSC_MISSING_LAPACK_lsame) 1808d71ae5a4SJacob Faibussowitsch PETSC_EXTERN int lsame(char *a, char *b) 1809d71ae5a4SJacob Faibussowitsch { 181043db4dbbSBarry Smith if (*a == *b) return 1; 181143db4dbbSBarry Smith if (*a + 32 == *b) return 1; 181243db4dbbSBarry Smith if (*a - 32 == *b) return 1; 181343db4dbbSBarry Smith return 0; 181443db4dbbSBarry Smith } 181543db4dbbSBarry Smith #endif 1816