xref: /petsc/src/sys/objects/init.c (revision e57d7714c1ef65e98802735ba860ff395287aedb)
1e5c89e4eSSatish Balay /*
2e5c89e4eSSatish Balay 
3e5c89e4eSSatish Balay    This file defines part of the initialization of PETSc
4e5c89e4eSSatish Balay 
5540e20f2SPierre Jolivet   This file uses regular malloc and free because it cannot be known
6e5c89e4eSSatish Balay   what malloc is being used until it has already processed the input.
7e5c89e4eSSatish Balay */
8ef386f4bSSatish Balay 
9ef386f4bSSatish Balay #include <petscsys.h>        /*I  "petscsys.h"   I*/
10f67a399dSBarry Smith #include <petsc/private/petscimpl.h>
11022afb99SBarry Smith #include <petscvalgrind.h>
12665c2dedSJed Brown #include <petscviewer.h>
13fdc842d1SBarry Smith #if defined(PETSC_USE_LOG)
14fdc842d1SBarry Smith PETSC_INTERN PetscErrorCode PetscLogInitialize(void);
15fdc842d1SBarry Smith #endif
16ef386f4bSSatish Balay 
17ba61063dSBarry Smith #if defined(PETSC_HAVE_SYS_SYSINFO_H)
1851d315f7SKerry Stevens #include <sys/sysinfo.h>
19ba61063dSBarry Smith #endif
20121deb67SSatish Balay #if defined(PETSC_HAVE_UNISTD_H)
2151d315f7SKerry Stevens #include <unistd.h>
22121deb67SSatish Balay #endif
2305035670SJunchao Zhang 
24d9b72601SDominic Meiser #if defined(PETSC_HAVE_CUDA)
25d9b72601SDominic Meiser   #include <cuda_runtime.h>
267fd2f626SJose E. Roman   #include <petsccublas.h>
2705035670SJunchao Zhang #endif
2805035670SJunchao Zhang 
2905035670SJunchao Zhang #if defined(PETSC_HAVE_HIP)
3005035670SJunchao Zhang   #include <hip/hip_runtime.h>
3105035670SJunchao Zhang #endif
3205035670SJunchao Zhang 
3305035670SJunchao Zhang #if defined(PETSC_HAVE_DEVICE)
34c2a741eeSJunchao Zhang   #if defined(PETSC_HAVE_OMPI_MAJOR_VERSION)
35c2a741eeSJunchao Zhang     #include "mpi-ext.h" /* Needed for OpenMPI CUDA-aware check */
36c2a741eeSJunchao Zhang   #endif
37d9b72601SDominic Meiser #endif
38555d055bSBarry Smith 
39f0a7718cSKarl Rupp #if defined(PETSC_HAVE_VIENNACL)
40f0a7718cSKarl Rupp PETSC_EXTERN PetscErrorCode PetscViennaCLInit();
41f0a7718cSKarl Rupp #endif
42f0a7718cSKarl Rupp 
4305035670SJunchao Zhang 
44e5c89e4eSSatish Balay /* ------------------------Nasty global variables -------------------------------*/
45e5c89e4eSSatish Balay /*
46e5c89e4eSSatish Balay      Indicates if PETSc started up MPI, or it was
47e5c89e4eSSatish Balay    already started before PETSc was initialized.
48e5c89e4eSSatish Balay */
490cbf60d0SJose E. Roman PetscBool   PetscBeganMPI                 = PETSC_FALSE;
508ad20175SVaclav Hapla PetscBool   PetscErrorHandlingInitialized = PETSC_FALSE;
510cbf60d0SJose E. Roman PetscBool   PetscInitializeCalled         = PETSC_FALSE;
520cbf60d0SJose E. Roman PetscBool   PetscFinalizeCalled           = PETSC_FALSE;
53d6f2c3cbSBarry Smith 
547087cfbeSBarry Smith PetscMPIInt PetscGlobalRank               = -1;
557087cfbeSBarry Smith PetscMPIInt PetscGlobalSize               = -1;
56ba61063dSBarry Smith 
57c2b86a48SJunchao Zhang #if defined(PETSC_HAVE_KOKKOS)
58c2b86a48SJunchao Zhang PetscBool   PetscBeganKokkos              = PETSC_FALSE;
59c2b86a48SJunchao Zhang #endif
60c2b86a48SJunchao Zhang 
61c2a741eeSJunchao Zhang PetscBool   use_gpu_aware_mpi             = PETSC_TRUE;
62c2a741eeSJunchao Zhang 
6350f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
64e5c89e4eSSatish Balay #if defined(PETSC_COMPLEX_INSTANTIATE)
65e5c89e4eSSatish Balay template <> class std::complex<double>; /* instantiate complex template class */
66e5c89e4eSSatish Balay #endif
672c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
68500d8756SSatish Balay MPI_Datatype MPIU_C_DOUBLE_COMPLEX;
69500d8756SSatish Balay MPI_Datatype MPIU_C_COMPLEX;
702c876bd9SBarry Smith #endif
718619c96cSJed Brown 
728619c96cSJed Brown /*MC
738619c96cSJed Brown    PETSC_i - the imaginary number i
748619c96cSJed Brown 
758619c96cSJed Brown    Synopsis:
768619c96cSJed Brown    #include <petscsys.h>
778619c96cSJed Brown    PetscComplex PETSC_i;
788619c96cSJed Brown 
798619c96cSJed Brown    Level: beginner
808619c96cSJed Brown 
818619c96cSJed Brown    Note:
828cd53115SBarry Smith    Complex numbers are automatically available if PETSc located a working complex implementation
838619c96cSJed Brown 
848619c96cSJed Brown .seealso: PetscRealPart(), PetscImaginaryPart(), PetscRealPartComplex(), PetscImaginaryPartComplex()
858619c96cSJed Brown M*/
8650f81f78SJed Brown PetscComplex PETSC_i;
87e5c89e4eSSatish Balay #endif
88ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
89c90a1750SBarry Smith MPI_Datatype MPIU___FLOAT128 = 0;
907c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
918c764dc5SJose Roman MPI_Datatype MPIU___COMPLEX128 = 0;
928c764dc5SJose Roman #endif
93570b7f6dSBarry Smith #elif defined(PETSC_USE_REAL___FP16)
94570b7f6dSBarry Smith MPI_Datatype MPIU___FP16 = 0;
95c90a1750SBarry Smith #endif
967087cfbeSBarry Smith MPI_Datatype MPIU_2SCALAR = 0;
970354ff80SSatish Balay #if defined(PETSC_USE_64BIT_INDICES)
987087cfbeSBarry Smith MPI_Datatype MPIU_2INT = 0;
9944041f26SJed Brown #endif
1008ad47952SJed Brown MPI_Datatype MPIU_BOOL;
1018ad47952SJed Brown MPI_Datatype MPIU_ENUM;
1027cdaf61dSJed Brown MPI_Datatype MPIU_FORTRANADDR;
103e316c87fSJed Brown MPI_Datatype MPIU_SIZE_T;
10475567043SBarry Smith 
105e5c89e4eSSatish Balay /*
106e5c89e4eSSatish Balay        Function that is called to display all error messages
107e5c89e4eSSatish Balay */
1087087cfbeSBarry Smith PetscErrorCode (*PetscErrorPrintf)(const char [],...)          = PetscErrorPrintfDefault;
1097087cfbeSBarry Smith PetscErrorCode (*PetscHelpPrintf)(MPI_Comm,const char [],...)  = PetscHelpPrintfDefault;
1107087cfbeSBarry Smith PetscErrorCode (*PetscVFPrintf)(FILE*,const char[],va_list)    = PetscVFPrintfDefault;
111bab1f7e6SVictor Minden /*
1124cf1874eSKarl Rupp   This is needed to turn on/off GPU synchronization
1138b5db460SBarry Smith */
1144cf1874eSKarl Rupp PetscBool PetscViennaCLSynchronize = PETSC_FALSE;
115bab1f7e6SVictor Minden 
116e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
117e5c89e4eSSatish Balay /*
118e5c89e4eSSatish Balay    Optional file where all PETSc output from various prints is saved
119e5c89e4eSSatish Balay */
12095c0884eSLisandro Dalcin PETSC_INTERN FILE *petsc_history;
1210298fd71SBarry Smith FILE *petsc_history = NULL;
122e5c89e4eSSatish Balay 
1237087cfbeSBarry Smith PetscErrorCode  PetscOpenHistoryFile(const char filename[],FILE **fd)
124e5c89e4eSSatish Balay {
125e5c89e4eSSatish Balay   PetscErrorCode ierr;
126e5c89e4eSSatish Balay   PetscMPIInt    rank,size;
127e5c89e4eSSatish Balay   char           pfile[PETSC_MAX_PATH_LEN],pname[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN],date[64];
128e5c89e4eSSatish Balay   char           version[256];
129e5c89e4eSSatish Balay 
130e5c89e4eSSatish Balay   PetscFunctionBegin;
131e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
132e5c89e4eSSatish Balay   if (!rank) {
133e5c89e4eSSatish Balay     char        arch[10];
134f56c2debSBarry Smith     int         err;
135f56c2debSBarry Smith 
136e5c89e4eSSatish Balay     ierr = PetscGetArchType(arch,10);CHKERRQ(ierr);
137e5c89e4eSSatish Balay     ierr = PetscGetDate(date,64);CHKERRQ(ierr);
138a523d312SBarry Smith     ierr = PetscGetVersion(version,256);CHKERRQ(ierr);
139e5c89e4eSSatish Balay     ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
140e5c89e4eSSatish Balay     if (filename) {
141e5c89e4eSSatish Balay       ierr = PetscFixFilename(filename,fname);CHKERRQ(ierr);
142e5c89e4eSSatish Balay     } else {
143589a23caSBarry Smith       ierr = PetscGetHomeDirectory(pfile,sizeof(pfile));CHKERRQ(ierr);
144589a23caSBarry Smith       ierr = PetscStrlcat(pfile,"/.petschistory",sizeof(pfile));CHKERRQ(ierr);
145e5c89e4eSSatish Balay       ierr = PetscFixFilename(pfile,fname);CHKERRQ(ierr);
146e5c89e4eSSatish Balay     }
147e5c89e4eSSatish Balay 
148a297a907SKarl Rupp     *fd = fopen(fname,"a");
149a297a907SKarl Rupp     if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file: %s",fname);
150a297a907SKarl Rupp 
151c0bb3764SVaclav Hapla     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"----------------------------------------\n");CHKERRQ(ierr);
152e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"%s %s\n",version,date);CHKERRQ(ierr);
153589a23caSBarry Smith     ierr = PetscGetProgramName(pname,sizeof(pname));CHKERRQ(ierr);
154e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"%s on a %s, %d proc. with options:\n",pname,arch,size);CHKERRQ(ierr);
155c0bb3764SVaclav Hapla     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"----------------------------------------\n");CHKERRQ(ierr);
156a297a907SKarl Rupp 
157f56c2debSBarry Smith     err = fflush(*fd);
158e32f2f54SBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
159e5c89e4eSSatish Balay   }
160e5c89e4eSSatish Balay   PetscFunctionReturn(0);
161e5c89e4eSSatish Balay }
162e5c89e4eSSatish Balay 
16395c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscCloseHistoryFile(FILE **fd)
164e5c89e4eSSatish Balay {
165e5c89e4eSSatish Balay   PetscErrorCode ierr;
166e5c89e4eSSatish Balay   PetscMPIInt    rank;
167e5c89e4eSSatish Balay   char           date[64];
168f56c2debSBarry Smith   int            err;
169e5c89e4eSSatish Balay 
170e5c89e4eSSatish Balay   PetscFunctionBegin;
171e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
172e5c89e4eSSatish Balay   if (!rank) {
173e5c89e4eSSatish Balay     ierr = PetscGetDate(date,64);CHKERRQ(ierr);
174c0bb3764SVaclav Hapla     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"----------------------------------------\n");CHKERRQ(ierr);
175e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"Finished at %s\n",date);CHKERRQ(ierr);
176c0bb3764SVaclav Hapla     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"----------------------------------------\n");CHKERRQ(ierr);
177f56c2debSBarry Smith     err  = fflush(*fd);
178e32f2f54SBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
179f56c2debSBarry Smith     err = fclose(*fd);
180e32f2f54SBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
181e5c89e4eSSatish Balay   }
182e5c89e4eSSatish Balay   PetscFunctionReturn(0);
183e5c89e4eSSatish Balay }
184e5c89e4eSSatish Balay 
185e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
186e5c89e4eSSatish Balay 
187e5c89e4eSSatish Balay /*
188e5c89e4eSSatish Balay    This is ugly and probably belongs somewhere else, but I want to
189e5c89e4eSSatish Balay   be able to put a true MPI abort error handler with command line args.
190e5c89e4eSSatish Balay 
191e5c89e4eSSatish Balay     This is so MPI errors in the debugger will leave all the stack
1923c311c98SBarry Smith   frames. The default MP_Abort() cleans up and exits thus providing no useful information
1933c311c98SBarry Smith   in the debugger hence we call abort() instead of MPI_Abort().
194e5c89e4eSSatish Balay */
195e5c89e4eSSatish Balay 
19633c7d699SBarry Smith void Petsc_MPI_AbortOnError(MPI_Comm *comm,PetscMPIInt *flag,...)
197e5c89e4eSSatish Balay {
198e5c89e4eSSatish Balay   PetscFunctionBegin;
1993c311c98SBarry Smith   (*PetscErrorPrintf)("MPI error %d\n",*flag);
200e5c89e4eSSatish Balay   abort();
201e5c89e4eSSatish Balay }
202e5c89e4eSSatish Balay 
20333c7d699SBarry Smith void Petsc_MPI_DebuggerOnError(MPI_Comm *comm,PetscMPIInt *flag,...)
204e5c89e4eSSatish Balay {
205e5c89e4eSSatish Balay   PetscErrorCode ierr;
206e5c89e4eSSatish Balay 
207e5c89e4eSSatish Balay   PetscFunctionBegin;
2083c311c98SBarry Smith   (*PetscErrorPrintf)("MPI error %d\n",*flag);
209e5c89e4eSSatish Balay   ierr = PetscAttachDebugger();
21041e02c4dSJunchao Zhang   if (ierr) PETSCABORT(*comm,*flag); /* hopeless so get out */
211e5c89e4eSSatish Balay }
212e5c89e4eSSatish Balay 
213e5c89e4eSSatish Balay /*@C
214e5c89e4eSSatish Balay    PetscEnd - Calls PetscFinalize() and then ends the program. This is useful if one
215e5c89e4eSSatish Balay      wishes a clean exit somewhere deep in the program.
216e5c89e4eSSatish Balay 
217e5c89e4eSSatish Balay    Collective on PETSC_COMM_WORLD
218e5c89e4eSSatish Balay 
219e5c89e4eSSatish Balay    Options Database Keys are the same as for PetscFinalize()
220e5c89e4eSSatish Balay 
221e5c89e4eSSatish Balay    Level: advanced
222e5c89e4eSSatish Balay 
223e5c89e4eSSatish Balay    Note:
224e5c89e4eSSatish Balay    See PetscInitialize() for more general runtime options.
225e5c89e4eSSatish Balay 
22688c29154SBarry Smith .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscFinalize()
227e5c89e4eSSatish Balay @*/
2287087cfbeSBarry Smith PetscErrorCode  PetscEnd(void)
229e5c89e4eSSatish Balay {
230e5c89e4eSSatish Balay   PetscFunctionBegin;
231e5c89e4eSSatish Balay   PetscFinalize();
232e5c89e4eSSatish Balay   exit(0);
233e5c89e4eSSatish Balay   return 0;
234e5c89e4eSSatish Balay }
235e5c89e4eSSatish Balay 
236ace3abfcSBarry Smith PetscBool PetscOptionsPublish = PETSC_FALSE;
23795c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void);
23895c0884eSLisandro Dalcin PETSC_INTERN PetscBool      petscsetmallocvisited;
239e5c89e4eSSatish Balay static       char           emacsmachinename[256];
240e5c89e4eSSatish Balay 
24102c9f0b5SLisandro Dalcin PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = NULL;
24202c9f0b5SLisandro Dalcin PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = NULL;
243e5c89e4eSSatish Balay 
244e5c89e4eSSatish Balay /*@C
245e5c89e4eSSatish Balay    PetscSetHelpVersionFunctions - Sets functions that print help and version information
246e5c89e4eSSatish Balay    before the PETSc help and version information is printed. Must call BEFORE PetscInitialize().
247e5c89e4eSSatish Balay    This routine enables a "higher-level" package that uses PETSc to print its messages first.
248e5c89e4eSSatish Balay 
249e5c89e4eSSatish Balay    Input Parameter:
2500298fd71SBarry Smith +  help - the help function (may be NULL)
2510298fd71SBarry Smith -  version - the version function (may be NULL)
252e5c89e4eSSatish Balay 
253e5c89e4eSSatish Balay    Level: developer
254e5c89e4eSSatish Balay 
255e5c89e4eSSatish Balay @*/
2567087cfbeSBarry Smith PetscErrorCode  PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm),PetscErrorCode (*version)(MPI_Comm))
257e5c89e4eSSatish Balay {
258e5c89e4eSSatish Balay   PetscFunctionBegin;
259e5c89e4eSSatish Balay   PetscExternalHelpFunction    = help;
260e5c89e4eSSatish Balay   PetscExternalVersionFunction = version;
261e5c89e4eSSatish Balay   PetscFunctionReturn(0);
262e5c89e4eSSatish Balay }
263e5c89e4eSSatish Balay 
26405df10baSBarry Smith #if defined(PETSC_USE_LOG)
26595c0884eSLisandro Dalcin PETSC_INTERN PetscBool   PetscObjectsLog;
26605df10baSBarry Smith #endif
26705df10baSBarry Smith 
268e781f417SStefano Zampini void PetscMPI_Comm_eh(MPI_Comm *comm, PetscMPIInt *err, ...)
269e781f417SStefano Zampini {
270e781f417SStefano Zampini   if (PetscUnlikely(*err)) {
271e781f417SStefano Zampini     PetscMPIInt len;
272e781f417SStefano Zampini     char        errstring[MPI_MAX_ERROR_STRING];
273e781f417SStefano Zampini 
274e781f417SStefano Zampini     MPI_Error_string(*err,errstring,&len);
275e781f417SStefano Zampini     PetscError(MPI_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,PETSC_MPI_ERROR_CODE,PETSC_ERROR_INITIAL,"Internal error in MPI: %s",errstring);
276e781f417SStefano Zampini   }
277e781f417SStefano Zampini   return;
278e781f417SStefano Zampini }
279e781f417SStefano Zampini 
28005035670SJunchao Zhang /* CUPM stands for 'CUDA Programming Model', which is implemented in either CUDA or HIP.
28105035670SJunchao Zhang    Use the following macros to define CUDA/HIP initialization related vars/routines.
28205035670SJunchao Zhang  */
28305035670SJunchao Zhang #if defined(PETSC_HAVE_CUDA)
28405035670SJunchao Zhang   typedef cudaError_t                             cupmError_t;
28505035670SJunchao Zhang   typedef struct cudaDeviceProp                   cupmDeviceProp;
28605035670SJunchao Zhang   #define cupmGetDeviceCount(x)                   cudaGetDeviceCount(x)
28705035670SJunchao Zhang   #define cupmGetDevice(x)                        cudaGetDevice(x)
28805035670SJunchao Zhang   #define cupmSetDevice(x)                        cudaSetDevice(x)
28905035670SJunchao Zhang   #define cupmSetDeviceFlags(x)                   cudaSetDeviceFlags(x)
29005035670SJunchao Zhang   #define cupmGetDeviceProperties(x,y)            cudaGetDeviceProperties(x,y)
29105035670SJunchao Zhang   #define cupmGetLastError()                      cudaGetLastError()
29205035670SJunchao Zhang   #define cupmDeviceMapHost                       cudaDeviceMapHost
29305035670SJunchao Zhang   #define cupmSuccess                             cudaSuccess
294*e57d7714SBarry Smith   #define cupmErrorMemoryAllocation               cudaErrorMemoryAllocation
295*e57d7714SBarry Smith   #define cupmErrorLaunchOutOfResources           cudaErrorLaunchOutOfResources
29605035670SJunchao Zhang   #define cupmErrorSetOnActiveProcess             cudaErrorSetOnActiveProcess
29705035670SJunchao Zhang   #define CHKERRCUPM(x)                           CHKERRCUDA(x)
29805035670SJunchao Zhang   #define PetscCUPMBLASInitializeHandle()         PetscCUBLASInitializeHandle()
29905035670SJunchao Zhang   #define PetscCUPMSOLVERDnInitializeHandle()     PetscCUSOLVERDnInitializeHandle()
30005035670SJunchao Zhang   #define PetscCUPMInitialize                     PetscCUDAInitialize
30105035670SJunchao Zhang   #define PetscCUPMInitialized                    PetscCUDAInitialized
30205035670SJunchao Zhang   #define PetscCUPMInitializeCheck                PetscCUDAInitializeCheck
30305035670SJunchao Zhang   #define PetscCUPMInitializeAndView              PetscCUDAInitializeAndView
30405035670SJunchao Zhang   #define PetscCUPMSynchronize                    PetscCUDASynchronize
30505035670SJunchao Zhang   #define PetscNotUseCUPM                         PetscNotUseCUDA
30605035670SJunchao Zhang   #define cupmOptionsStr                          "CUDA options"
3074dcd27cbSJunchao Zhang   #define cupmSetDeviceStr                        "-cuda_device"
30805035670SJunchao Zhang   #define cupmViewStr                             "-cuda_view"
30905035670SJunchao Zhang   #define cupmSynchronizeStr                      "-cuda_synchronize"
31005035670SJunchao Zhang   #define PetscCUPMInitializeStr                  "PetscCUDAInitialize"
31105035670SJunchao Zhang   #define PetscOptionsCheckCUPM                   PetscOptionsCheckCUDA
31205035670SJunchao Zhang   #define PetscMPICUPMAwarenessCheck              PetscMPICUDAAwarenessCheck
31305035670SJunchao Zhang   #include "cupminit.inc"
31405035670SJunchao Zhang #endif
31505035670SJunchao Zhang 
31605035670SJunchao Zhang #if defined(PETSC_HAVE_HIP)
31705035670SJunchao Zhang   typedef hipError_t                              cupmError_t;
31805035670SJunchao Zhang   typedef hipDeviceProp_t                         cupmDeviceProp;
31905035670SJunchao Zhang   #define cupmGetDeviceCount(x)                   hipGetDeviceCount(x)
32005035670SJunchao Zhang   #define cupmGetDevice(x)                        hipGetDevice(x)
32105035670SJunchao Zhang   #define cupmSetDevice(x)                        hipSetDevice(x)
32205035670SJunchao Zhang   #define cupmSetDeviceFlags(x)                   hipSetDeviceFlags(x)
32305035670SJunchao Zhang   #define cupmGetDeviceProperties(x,y)            hipGetDeviceProperties(x,y)
32405035670SJunchao Zhang   #define cupmGetLastError()                      hipGetLastError()
32505035670SJunchao Zhang   #define cupmDeviceMapHost                       hipDeviceMapHost
32605035670SJunchao Zhang   #define cupmSuccess                             hipSuccess
327*e57d7714SBarry Smith   #define cupmErrorMemoryAllocation               hipErrorMemoryAllocation
328*e57d7714SBarry Smith   #define cupmErrorLaunchOutOfResources           hipErrorLaunchOutOfResources
32905035670SJunchao Zhang   #define cupmErrorSetOnActiveProcess             hipErrorSetOnActiveProcess
33005035670SJunchao Zhang   #define CHKERRCUPM(x)                           CHKERRQ((x)==hipSuccess? 0:PETSC_ERR_LIB)
33105035670SJunchao Zhang   #define PetscCUPMBLASInitializeHandle()         0
33205035670SJunchao Zhang   #define PetscCUPMSOLVERDnInitializeHandle()     0
33305035670SJunchao Zhang   #define PetscCUPMInitialize                     PetscHIPInitialize
33405035670SJunchao Zhang   #define PetscCUPMInitialized                    PetscHIPInitialized
33505035670SJunchao Zhang   #define PetscCUPMInitializeCheck                PetscHIPInitializeCheck
33605035670SJunchao Zhang   #define PetscCUPMInitializeAndView              PetscHIPInitializeAndView
33705035670SJunchao Zhang   #define PetscCUPMSynchronize                    PetscHIPSynchronize
33805035670SJunchao Zhang   #define PetscNotUseCUPM                         PetscNotUseHIP
33905035670SJunchao Zhang   #define cupmOptionsStr                          "HIP options"
3404dcd27cbSJunchao Zhang   #define cupmSetDeviceStr                        "-hip_device"
34105035670SJunchao Zhang   #define cupmViewStr                             "-hip_view"
34205035670SJunchao Zhang   #define cupmSynchronizeStr                      "-hip_synchronize"
34305035670SJunchao Zhang   #define PetscCUPMInitializeStr                  "PetscHIPInitialize"
34405035670SJunchao Zhang   #define PetscOptionsCheckCUPM                   PetscOptionsCheckHIP
34505035670SJunchao Zhang   #define PetscMPICUPMAwarenessCheck              PetscMPIHIPAwarenessCheck
34605035670SJunchao Zhang   #include "cupminit.inc"
34705035670SJunchao Zhang #endif
34805035670SJunchao Zhang 
34957171095SVaclav Hapla PETSC_INTERN PetscErrorCode  PetscOptionsCheckInitial_Private(const char help[])
350e5c89e4eSSatish Balay {
351e5ed2c37SJose E. Roman   char              string[64];
352e5c89e4eSSatish Balay   MPI_Comm          comm = PETSC_COMM_WORLD;
35305035670SJunchao Zhang   PetscBool         flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flag,hasHelp,logView;
354e5c89e4eSSatish Balay   PetscErrorCode    ierr;
35567584ceeSBarry Smith   PetscReal         si;
35628559dc8SJed Brown   PetscInt          intensity;
357e5c89e4eSSatish Balay   int               i;
358e5c89e4eSSatish Balay   PetscMPIInt       rank;
359d314f959SVaclav Hapla   char              version[256];
3602479a3a6SBarry Smith #if defined(PETSC_USE_LOG)
361e5ed2c37SJose E. Roman   char              mname[PETSC_MAX_PATH_LEN];
3622479a3a6SBarry Smith   PetscViewerFormat format;
36367584ceeSBarry Smith   PetscBool         flg4 = PETSC_FALSE;
36467584ceeSBarry Smith #endif
3657381773fSBarry Smith 
366e5c89e4eSSatish Balay   PetscFunctionBegin;
36742218b76SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
368e5c89e4eSSatish Balay 
36967584ceeSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
37092f119d6SBarry Smith   if (!(PETSC_RUNNING_ON_VALGRIND)) {
371e5c89e4eSSatish Balay     /*
372e5c89e4eSSatish Balay       Setup the memory management; support for tracing malloc() usage
373e5c89e4eSSatish Balay     */
374244bdbccSBarry Smith     PetscBool         mdebug = PETSC_FALSE, eachcall = PETSC_FALSE, initializenan = PETSC_FALSE, mlog = PETSC_FALSE;
37592f119d6SBarry Smith 
37676bd3646SJed Brown     if (PetscDefined(USE_DEBUG)) {
37792f119d6SBarry Smith       mdebug        = PETSC_TRUE;
37892f119d6SBarry Smith       initializenan = PETSC_TRUE;
37992f119d6SBarry Smith       ierr   = PetscOptionsHasName(NULL,NULL,"-malloc_test",&flg1);CHKERRQ(ierr);
38076bd3646SJed Brown     } else {
38192f119d6SBarry Smith       /* don't warn about unused option */
38292f119d6SBarry Smith       ierr = PetscOptionsHasName(NULL,NULL,"-malloc_test",&flg1);CHKERRQ(ierr);
38392f119d6SBarry Smith       flg1 = PETSC_FALSE;
38476bd3646SJed Brown     }
38579dccf82SBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_debug",&flg2,&flg3);CHKERRQ(ierr);
38692f119d6SBarry Smith     if (flg1 || flg2) {
38792f119d6SBarry Smith       mdebug        = PETSC_TRUE;
38892f119d6SBarry Smith       eachcall      = PETSC_TRUE;
38992f119d6SBarry Smith       initializenan = PETSC_TRUE;
39079dccf82SBarry Smith     } else if (flg3 && !flg2) {
39179dccf82SBarry Smith       mdebug        = PETSC_FALSE;
39279dccf82SBarry Smith       eachcall      = PETSC_FALSE;
39379dccf82SBarry Smith       initializenan = PETSC_FALSE;
394e5c89e4eSSatish Balay     }
39592f119d6SBarry Smith 
396608c71bfSMatthew G. Knepley     ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_requested_size",&flg1,&flg2);CHKERRQ(ierr);
397608c71bfSMatthew G. Knepley     if (flg2) {ierr = PetscMallocLogRequestedSizeSet(flg1);CHKERRQ(ierr);}
398608c71bfSMatthew G. Knepley 
39992f119d6SBarry Smith     ierr = PetscOptionsHasName(NULL,NULL,"-malloc_view",&mlog);CHKERRQ(ierr);
40092f119d6SBarry Smith     if (mlog) {
40192f119d6SBarry Smith       mdebug = PETSC_TRUE;
40292f119d6SBarry Smith     }
40379dccf82SBarry Smith     /* the next line is deprecated */
40492f119d6SBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-malloc",&mdebug,NULL);CHKERRQ(ierr);
40592f119d6SBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_dump",&mdebug,NULL);CHKERRQ(ierr);
40692f119d6SBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-log_view_memory",&mdebug,NULL);CHKERRQ(ierr);
40792f119d6SBarry Smith     if (mdebug) {
40892f119d6SBarry Smith       ierr = PetscMallocSetDebug(eachcall,initializenan);CHKERRQ(ierr);
40992f119d6SBarry Smith     }
41092f119d6SBarry Smith     if (mlog) {
41192f119d6SBarry Smith       PetscReal logthreshold = 0;
41292f119d6SBarry Smith       ierr = PetscOptionsGetReal(NULL,NULL,"-malloc_view_threshold",&logthreshold,NULL);CHKERRQ(ierr);
41392f119d6SBarry Smith       ierr = PetscMallocViewSet(logthreshold);CHKERRQ(ierr);
41492f119d6SBarry Smith     }
4158b254c29SBarry Smith #if defined(PETSC_USE_LOG)
4168b254c29SBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-log_view_memory",&PetscLogMemory,NULL);CHKERRQ(ierr);
4178b254c29SBarry Smith #endif
41892f119d6SBarry Smith   }
41992f119d6SBarry Smith 
420ba282f50SJed Brown   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_coalesce",&flg1,&flg2);CHKERRQ(ierr);
421ba282f50SJed Brown   if (flg2) {ierr = PetscMallocSetCoalesce(flg1);CHKERRQ(ierr);}
42290d69ab7SBarry Smith   flg1 = PETSC_FALSE;
4232f21b5c6SHong Zhang   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_hbw",&flg1,NULL);CHKERRQ(ierr);
424cf6f3811SHong Zhang   /* ignore this option if malloc is already set */
425cf6f3811SHong Zhang   if (flg1 && !petscsetmallocvisited) {ierr = PetscSetUseHBWMalloc_Private();CHKERRQ(ierr);}
426e5c89e4eSSatish Balay 
42790d69ab7SBarry Smith   flg1 = PETSC_FALSE;
428c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_info",&flg1,NULL);CHKERRQ(ierr);
4297783f70dSSatish Balay   if (!flg1) {
43090d69ab7SBarry Smith     flg1 = PETSC_FALSE;
431c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-memory_view",&flg1,NULL);CHKERRQ(ierr);
4327783f70dSSatish Balay   }
433e5c89e4eSSatish Balay   if (flg1) {
434e5c89e4eSSatish Balay     ierr = PetscMemorySetGetMaximumUsage();CHKERRQ(ierr);
435e5c89e4eSSatish Balay   }
43667584ceeSBarry Smith #endif
437e5c89e4eSSatish Balay 
438b4427426SBarry Smith #if defined(PETSC_USE_LOG)
439c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-objects_dump",&PetscObjectsLog);CHKERRQ(ierr);
440b4427426SBarry Smith #endif
44105df10baSBarry Smith 
442e5c89e4eSSatish Balay   /*
443e5c89e4eSSatish Balay       Set the display variable for graphics
444e5c89e4eSSatish Balay   */
445e5c89e4eSSatish Balay   ierr = PetscSetDisplay();CHKERRQ(ierr);
446e5c89e4eSSatish Balay 
44751dcc849SKerry Stevens   /*
44857171095SVaclav Hapla      Print main application help message
44957171095SVaclav Hapla   */
45057171095SVaclav Hapla   ierr = PetscOptionsHasHelp(NULL,&hasHelp);CHKERRQ(ierr);
45157171095SVaclav Hapla   if (help && hasHelp) {
45257171095SVaclav Hapla     ierr = PetscPrintf(comm,help);CHKERRQ(ierr);
45357171095SVaclav Hapla     ierr = PetscPrintf(comm,"----------------------------------------\n");CHKERRQ(ierr);
45457171095SVaclav Hapla   }
45557171095SVaclav Hapla 
45657171095SVaclav Hapla   /*
457e5c89e4eSSatish Balay       Print the PETSc version information
458e5c89e4eSSatish Balay   */
459d314f959SVaclav Hapla   ierr = PetscOptionsHasName(NULL,NULL,"-version",&flg1);CHKERRQ(ierr);
460d314f959SVaclav Hapla   if (flg1 || hasHelp) {
461e5c89e4eSSatish Balay     /*
462e5c89e4eSSatish Balay        Print "higher-level" package version message
463e5c89e4eSSatish Balay     */
464e5c89e4eSSatish Balay     if (PetscExternalVersionFunction) {
465e5c89e4eSSatish Balay       ierr = (*PetscExternalVersionFunction)(comm);CHKERRQ(ierr);
466e5c89e4eSSatish Balay     }
467e5c89e4eSSatish Balay 
468a523d312SBarry Smith     ierr = PetscGetVersion(version,256);CHKERRQ(ierr);
469e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"%s\n",version);CHKERRQ(ierr);
470e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO);CHKERRQ(ierr);
471e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n");CHKERRQ(ierr);
47284e42920SBarry Smith     ierr = (*PetscHelpPrintf)(comm,"See docs/faq.html for problems.\n");CHKERRQ(ierr);
473e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n");CHKERRQ(ierr);
474e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR);CHKERRQ(ierr);
475c0bb3764SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm,"----------------------------------------\n");CHKERRQ(ierr);
4767ca660e7SBarry Smith   }
4777ca660e7SBarry Smith 
47894941ca7SBarry Smith   /*
47994941ca7SBarry Smith        Print "higher-level" package help message
48094941ca7SBarry Smith   */
48157171095SVaclav Hapla   if (hasHelp) {
482d314f959SVaclav Hapla     PetscBool hasHelpIntro;
483d314f959SVaclav Hapla 
48494941ca7SBarry Smith     if (PetscExternalHelpFunction) {
48594941ca7SBarry Smith       ierr = (*PetscExternalHelpFunction)(comm);CHKERRQ(ierr);
48694941ca7SBarry Smith     }
487d314f959SVaclav Hapla     ierr = PetscOptionsHasHelpIntro_Internal(NULL,&hasHelpIntro);CHKERRQ(ierr);
488d314f959SVaclav Hapla     if (hasHelpIntro) {
489bdb346e9SBarry Smith       ierr = PetscOptionsDestroyDefault();CHKERRQ(ierr);
490008a6e76SBarry Smith       ierr = PetscFreeMPIResources();CHKERRQ(ierr);
4917ca660e7SBarry Smith       ierr = MPI_Finalize();CHKERRQ(ierr);
4927ca660e7SBarry Smith       exit(0);
4937ca660e7SBarry Smith     }
494e5c89e4eSSatish Balay   }
495e5c89e4eSSatish Balay 
496e5c89e4eSSatish Balay   /*
497e5c89e4eSSatish Balay       Setup the error handling
498e5c89e4eSSatish Balay   */
49990d69ab7SBarry Smith   flg1 = PETSC_FALSE;
500c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-on_error_abort",&flg1,NULL);CHKERRQ(ierr);
501b59baad1SJed Brown   if (flg1) {
50242218b76SBarry Smith     ierr = MPI_Comm_set_errhandler(comm,MPI_ERRORS_ARE_FATAL);CHKERRQ(ierr);
50302c9f0b5SLisandro Dalcin     ierr = PetscPushErrorHandler(PetscAbortErrorHandler,NULL);CHKERRQ(ierr);
504b59baad1SJed Brown   }
50590d69ab7SBarry Smith   flg1 = PETSC_FALSE;
506c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-on_error_mpiabort",&flg1,NULL);CHKERRQ(ierr);
50702c9f0b5SLisandro Dalcin   if (flg1) { ierr = PetscPushErrorHandler(PetscMPIAbortErrorHandler,NULL);CHKERRQ(ierr);}
50890d69ab7SBarry Smith   flg1 = PETSC_FALSE;
509c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-mpi_return_on_error",&flg1,NULL);CHKERRQ(ierr);
510e5c89e4eSSatish Balay   if (flg1) {
511d54338ecSKarl Rupp     ierr = MPI_Comm_set_errhandler(comm,MPI_ERRORS_RETURN);CHKERRQ(ierr);
512e5c89e4eSSatish Balay   }
513e781f417SStefano Zampini   /* experimental */
514e781f417SStefano Zampini   flg1 = PETSC_FALSE;
515e781f417SStefano Zampini   ierr = PetscOptionsGetBool(NULL,NULL,"-mpi_return_error_string",&flg1,NULL);CHKERRQ(ierr);
516e781f417SStefano Zampini   if (flg1) {
517e781f417SStefano Zampini     MPI_Errhandler eh;
518e781f417SStefano Zampini 
519e781f417SStefano Zampini     ierr = MPI_Comm_create_errhandler(PetscMPI_Comm_eh,&eh);CHKERRQ(ierr);
520e781f417SStefano Zampini     ierr = MPI_Comm_set_errhandler(comm,eh);CHKERRQ(ierr);
521e781f417SStefano Zampini     ierr = MPI_Errhandler_free(&eh);CHKERRQ(ierr);
522e781f417SStefano Zampini   }
52390d69ab7SBarry Smith   flg1 = PETSC_FALSE;
524c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-no_signal_handler",&flg1,NULL);CHKERRQ(ierr);
5258d359177SBarry Smith   if (!flg1) {ierr = PetscPushSignalHandler(PetscSignalHandlerDefault,(void*)0);CHKERRQ(ierr);}
526e5c89e4eSSatish Balay 
527e5c89e4eSSatish Balay   /*
528e5c89e4eSSatish Balay       Setup debugger information
529e5c89e4eSSatish Balay   */
530e5c89e4eSSatish Balay   ierr = PetscSetDefaultDebugger();CHKERRQ(ierr);
531589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-on_error_attach_debugger",string,sizeof(string),&flg1);CHKERRQ(ierr);
532e5c89e4eSSatish Balay   if (flg1) {
533e5c89e4eSSatish Balay     MPI_Errhandler err_handler;
534e5c89e4eSSatish Balay 
535e5c89e4eSSatish Balay     ierr = PetscSetDebuggerFromString(string);CHKERRQ(ierr);
53633c7d699SBarry Smith     ierr = MPI_Comm_create_errhandler(Petsc_MPI_DebuggerOnError,&err_handler);CHKERRQ(ierr);
537d54338ecSKarl Rupp     ierr = MPI_Comm_set_errhandler(comm,err_handler);CHKERRQ(ierr);
53802c9f0b5SLisandro Dalcin     ierr = PetscPushErrorHandler(PetscAttachDebuggerErrorHandler,NULL);CHKERRQ(ierr);
539e5c89e4eSSatish Balay   }
540589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-debug_terminal",string,sizeof(string),&flg1);CHKERRQ(ierr);
5415e96ac45SJed Brown   if (flg1) { ierr = PetscSetDebugTerminal(string);CHKERRQ(ierr); }
542589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-start_in_debugger",string,sizeof(string),&flg1);CHKERRQ(ierr);
543589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-stop_for_debugger",string,sizeof(string),&flg2);CHKERRQ(ierr);
544e5c89e4eSSatish Balay   if (flg1 || flg2) {
545e5c89e4eSSatish Balay     PetscMPIInt    size;
546bf4d2887SBarry Smith     PetscInt       lsize,*ranks;
547e5c89e4eSSatish Balay     MPI_Errhandler err_handler;
548e5c89e4eSSatish Balay     /*
549e5c89e4eSSatish Balay        we have to make sure that all processors have opened
550e5c89e4eSSatish Balay        connections to all other processors, otherwise once the
551e5c89e4eSSatish Balay        debugger has stated it is likely to receive a SIGUSR1
552e5c89e4eSSatish Balay        and kill the program.
553e5c89e4eSSatish Balay     */
55442218b76SBarry Smith     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
555e5c89e4eSSatish Balay     if (size > 2) {
556533163c2SBarry Smith       PetscMPIInt dummy = 0;
557e5c89e4eSSatish Balay       MPI_Status  status;
558e5c89e4eSSatish Balay       for (i=0; i<size; i++) {
559e5c89e4eSSatish Balay         if (rank != i) {
56042218b76SBarry Smith           ierr = MPI_Send(&dummy,1,MPI_INT,i,109,comm);CHKERRQ(ierr);
561e5c89e4eSSatish Balay         }
562e5c89e4eSSatish Balay       }
563e5c89e4eSSatish Balay       for (i=0; i<size; i++) {
564e5c89e4eSSatish Balay         if (rank != i) {
56542218b76SBarry Smith           ierr = MPI_Recv(&dummy,1,MPI_INT,i,109,comm,&status);CHKERRQ(ierr);
566e5c89e4eSSatish Balay         }
567e5c89e4eSSatish Balay       }
568e5c89e4eSSatish Balay     }
569e5c89e4eSSatish Balay     /* check if this processor node should be in debugger */
570bf4d2887SBarry Smith     ierr  = PetscMalloc1(size,&ranks);CHKERRQ(ierr);
571e5c89e4eSSatish Balay     lsize = size;
572bf4d2887SBarry Smith     /* Deprecated in 3.14 */
573bf4d2887SBarry Smith     ierr  = PetscOptionsGetIntArray(NULL,NULL,"-debugger_nodes",ranks,&lsize,&flag);CHKERRQ(ierr);
574bf4d2887SBarry Smith     if (flag) {
575bf4d2887SBarry Smith       const char * const quietopt="-options_suppress_deprecated_warnings";
576bf4d2887SBarry Smith       char               msg[4096];
577bf4d2887SBarry Smith       PetscBool          quiet = PETSC_FALSE;
578bf4d2887SBarry Smith 
579bf4d2887SBarry Smith       ierr = PetscOptionsGetBool(NULL,NULL,quietopt,&quiet,NULL);CHKERRQ(ierr);
580bf4d2887SBarry Smith       if (!quiet) {
581bf4d2887SBarry Smith         ierr = PetscStrcpy(msg,"** PETSc DEPRECATION WARNING ** : the option ");CHKERRQ(ierr);
582bf4d2887SBarry Smith         ierr = PetscStrcat(msg,"-debugger_nodes");CHKERRQ(ierr);
583bf4d2887SBarry Smith         ierr = PetscStrcat(msg," is deprecated as of version ");CHKERRQ(ierr);
584bf4d2887SBarry Smith         ierr = PetscStrcat(msg,"3.14");CHKERRQ(ierr);
585bf4d2887SBarry Smith         ierr = PetscStrcat(msg," and will be removed in a future release.");CHKERRQ(ierr);
586bf4d2887SBarry Smith         ierr = PetscStrcat(msg," Please use the option ");CHKERRQ(ierr);
587bf4d2887SBarry Smith         ierr = PetscStrcat(msg,"-debugger_ranks");CHKERRQ(ierr);
588bf4d2887SBarry Smith         ierr = PetscStrcat(msg," instead.");CHKERRQ(ierr);
589bf4d2887SBarry Smith         ierr = PetscStrcat(msg," (Silence this warning with ");CHKERRQ(ierr);
590bf4d2887SBarry Smith         ierr = PetscStrcat(msg,quietopt);CHKERRQ(ierr);
591bf4d2887SBarry Smith         ierr = PetscStrcat(msg,")\n");CHKERRQ(ierr);
592bf4d2887SBarry Smith         ierr = PetscPrintf(comm,msg);CHKERRQ(ierr);
593bf4d2887SBarry Smith       }
594bf4d2887SBarry Smith     } else {
595bf4d2887SBarry Smith       lsize = size;
596bf4d2887SBarry Smith       ierr  = PetscOptionsGetIntArray(NULL,NULL,"-debugger_ranks",ranks,&lsize,&flag);CHKERRQ(ierr);
597bf4d2887SBarry Smith     }
598e5c89e4eSSatish Balay     if (flag) {
599e5c89e4eSSatish Balay       for (i=0; i<lsize; i++) {
600bf4d2887SBarry Smith         if (ranks[i] == rank) { flag = PETSC_FALSE; break; }
601e5c89e4eSSatish Balay       }
602e5c89e4eSSatish Balay     }
603e5c89e4eSSatish Balay     if (!flag) {
604e5c89e4eSSatish Balay       ierr = PetscSetDebuggerFromString(string);CHKERRQ(ierr);
60502c9f0b5SLisandro Dalcin       ierr = PetscPushErrorHandler(PetscAbortErrorHandler,NULL);CHKERRQ(ierr);
606e5c89e4eSSatish Balay       if (flg1) {
607e5c89e4eSSatish Balay         ierr = PetscAttachDebugger();CHKERRQ(ierr);
608e5c89e4eSSatish Balay       } else {
609e5c89e4eSSatish Balay         ierr = PetscStopForDebugger();CHKERRQ(ierr);
610e5c89e4eSSatish Balay       }
61133c7d699SBarry Smith       ierr = MPI_Comm_create_errhandler(Petsc_MPI_AbortOnError,&err_handler);CHKERRQ(ierr);
612d54338ecSKarl Rupp       ierr = MPI_Comm_set_errhandler(comm,err_handler);CHKERRQ(ierr);
6132a2a2941SBarry Smith     } else {
6142a2a2941SBarry Smith       ierr = PetscWaitOnError();CHKERRQ(ierr);
615e5c89e4eSSatish Balay     }
616bf4d2887SBarry Smith     ierr = PetscFree(ranks);CHKERRQ(ierr);
617e5c89e4eSSatish Balay   }
618e5c89e4eSSatish Balay 
619589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-on_error_emacs",emacsmachinename,sizeof(emacsmachinename),&flg1);CHKERRQ(ierr);
620cb9801acSJed Brown   if (flg1 && !rank) {ierr = PetscPushErrorHandler(PetscEmacsClientErrorHandler,emacsmachinename);CHKERRQ(ierr);}
621e5c89e4eSSatish Balay 
622e5c89e4eSSatish Balay   /*
623e5c89e4eSSatish Balay         Setup profiling and logging
624e5c89e4eSSatish Balay   */
6256cf91177SBarry Smith #if defined(PETSC_USE_INFO)
6268bb29257SSatish Balay   {
627e94e781bSJacob Faibussowitsch     ierr = PetscInfoSetFromOptions(NULL);CHKERRQ(ierr);
628e5c89e4eSSatish Balay   }
629865f6aa8SSatish Balay #endif
630aba4c478SBarry Smith   ierr = PetscDetermineInitialFPTrap();
631cc9df77eSBarry Smith   flg1 = PETSC_FALSE;
632cc9df77eSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-fp_trap",&flg1,&flag);CHKERRQ(ierr);
633cc9df77eSBarry Smith   if (flag) {ierr = PetscSetFPTrap((PetscFPTrap)flg1);CHKERRQ(ierr);}
634cc9df77eSBarry Smith   ierr = PetscOptionsGetInt(NULL,NULL,"-check_pointer_intensity",&intensity,&flag);CHKERRQ(ierr);
635cc9df77eSBarry Smith   if (flag) {ierr = PetscCheckPointerSetIntensity(intensity);CHKERRQ(ierr);}
636865f6aa8SSatish Balay #if defined(PETSC_USE_LOG)
637865f6aa8SSatish Balay   mname[0] = 0;
638589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-history",mname,sizeof(mname),&flg1);CHKERRQ(ierr);
639865f6aa8SSatish Balay   if (flg1) {
640865f6aa8SSatish Balay     if (mname[0]) {
641f3dea69dSBarry Smith       ierr = PetscOpenHistoryFile(mname,&petsc_history);CHKERRQ(ierr);
642865f6aa8SSatish Balay     } else {
643706d7a88SBarry Smith       ierr = PetscOpenHistoryFile(NULL,&petsc_history);CHKERRQ(ierr);
644865f6aa8SSatish Balay     }
645865f6aa8SSatish Balay   }
646217044c2SLisandro Dalcin 
647217044c2SLisandro Dalcin   ierr = PetscOptionsGetBool(NULL,NULL,"-log_sync",&PetscLogSyncOn,NULL);CHKERRQ(ierr);
648217044c2SLisandro Dalcin 
649e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE)
65090d69ab7SBarry Smith   flg1 = PETSC_FALSE;
651c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-log_mpe",&flg1);CHKERRQ(ierr);
652495fc317SBarry Smith   if (flg1) {ierr = PetscLogMPEBegin();CHKERRQ(ierr);}
653e5c89e4eSSatish Balay #endif
65490d69ab7SBarry Smith   flg1 = PETSC_FALSE;
65590d69ab7SBarry Smith   flg3 = PETSC_FALSE;
656c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-log_all",&flg1,NULL);CHKERRQ(ierr);
657c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);CHKERRQ(ierr);
658e5c89e4eSSatish Balay   if (flg1)                      { ierr = PetscLogAllBegin();CHKERRQ(ierr); }
659bb1d7374SBarry Smith   else if (flg3)                 { ierr = PetscLogDefaultBegin();CHKERRQ(ierr);}
660e5c89e4eSSatish Balay 
661589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-log_trace",mname,sizeof(mname),&flg1);CHKERRQ(ierr);
662e5c89e4eSSatish Balay   if (flg1) {
663e5c89e4eSSatish Balay     char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN];
664e5c89e4eSSatish Balay     FILE *file;
665e5c89e4eSSatish Balay     if (mname[0]) {
6662e924ca5SSatish Balay       PetscSNPrintf(name,PETSC_MAX_PATH_LEN,"%s.%d",mname,rank);
667e5c89e4eSSatish Balay       ierr = PetscFixFilename(name,fname);CHKERRQ(ierr);
668e5c89e4eSSatish Balay       file = fopen(fname,"w");
669f3dea69dSBarry Smith       if (!file) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname);
670a297a907SKarl Rupp     } else file = PETSC_STDOUT;
671e5c89e4eSSatish Balay     ierr = PetscLogTraceBegin(file);CHKERRQ(ierr);
672e5c89e4eSSatish Balay   }
673bb1d7374SBarry Smith 
67416413a6aSBarry Smith   ierr = PetscOptionsGetViewer(comm,NULL,NULL,"-log_view",NULL,&format,&flg4);CHKERRQ(ierr);
675bb1d7374SBarry Smith   if (flg4) {
676bb1d7374SBarry Smith     if (format == PETSC_VIEWER_ASCII_XML) {
677bb1d7374SBarry Smith       ierr = PetscLogNestedBegin();CHKERRQ(ierr);
678bb1d7374SBarry Smith     } else {
679bb1d7374SBarry Smith       ierr = PetscLogDefaultBegin();CHKERRQ(ierr);
680bb1d7374SBarry Smith     }
681eccbb886SLisandro Dalcin   }
682eccbb886SLisandro Dalcin   if (flg4 && format == PETSC_VIEWER_ASCII_XML) {
683eccbb886SLisandro Dalcin     PetscReal threshold = PetscRealConstant(0.01);
684eccbb886SLisandro Dalcin     ierr = PetscOptionsGetReal(NULL,NULL,"-log_threshold",&threshold,&flg1);CHKERRQ(ierr);
685eccbb886SLisandro Dalcin     if (flg1) {ierr = PetscLogSetThreshold((PetscLogDouble)threshold,NULL);CHKERRQ(ierr);}
686bb1d7374SBarry Smith   }
687e5c89e4eSSatish Balay #endif
688e5c89e4eSSatish Balay 
689c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-saws_options",&PetscOptionsPublish,NULL);CHKERRQ(ierr);
69005035670SJunchao Zhang   ierr = PetscOptionsGetBool(NULL,NULL,"-use_gpu_aware_mpi",&use_gpu_aware_mpi,NULL);CHKERRQ(ierr);
6918d4e85a7SStefano Zampini   /*
69205035670SJunchao Zhang     If collecting logging information, by default, wait for device to complete its operations
6938d4e85a7SStefano Zampini     before returning to the CPU in order to get accurate timings of each event
6948d4e85a7SStefano Zampini   */
695cae85d06SJunchao Zhang   ierr = PetscOptionsHasName(NULL,NULL,"-log_summary",&logView);CHKERRQ(ierr);
69605035670SJunchao Zhang   if (!logView) {ierr = PetscOptionsHasName(NULL,NULL,"-log_view",&logView);CHKERRQ(ierr);}
6978d4e85a7SStefano Zampini 
69805035670SJunchao Zhang #if defined(PETSC_HAVE_CUDA)
69905035670SJunchao Zhang   ierr = PetscOptionsCheckCUDA(logView);CHKERRQ(ierr);
70005035670SJunchao Zhang #endif
701cae85d06SJunchao Zhang 
70205035670SJunchao Zhang #if defined(PETSC_HAVE_HIP)
70305035670SJunchao Zhang   ierr = PetscOptionsCheckHIP(logView);CHKERRQ(ierr);
7044b0a73b2SBarry Smith #endif
7054b0a73b2SBarry Smith 
706e5c89e4eSSatish Balay   /*
707e5c89e4eSSatish Balay        Print basic help message
708e5c89e4eSSatish Balay   */
70957171095SVaclav Hapla   if (hasHelp) {
710e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"Options for all PETSc programs:\n");CHKERRQ(ierr);
711d314f959SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -version: prints PETSc version\n");CHKERRQ(ierr);
712d314f959SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -help intro: prints example description and PETSc version, and exits\n");CHKERRQ(ierr);
713d314f959SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -help: prints example description, PETSc version, and available options for used routines\n");CHKERRQ(ierr);
714301d30feSBarry Smith     ierr = (*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is detected. Useful \n ");CHKERRQ(ierr);
715301d30feSBarry Smith     ierr = (*PetscHelpPrintf)(comm,"       only when run in the debugger\n");CHKERRQ(ierr);
716e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");CHKERRQ(ierr);
717e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"       start the debugger in new xterm\n");CHKERRQ(ierr);
718e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"       unless noxterm is given\n");CHKERRQ(ierr);
719e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");CHKERRQ(ierr);
720e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"       start all processes in the debugger\n");CHKERRQ(ierr);
721e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n");CHKERRQ(ierr);
722e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"    emacs jumps to error file\n");CHKERRQ(ierr);
723bf4d2887SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -debugger_ranks [n1,n2,..] Ranks to start in debugger\n");CHKERRQ(ierr);
724e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n");CHKERRQ(ierr);
725e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n");CHKERRQ(ierr);
726e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"                      waits the delay for you to attach\n");CHKERRQ(ierr);
7271cda70a7SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -display display: Location where X window graphics and debuggers are displayed\n");CHKERRQ(ierr);
728e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n");CHKERRQ(ierr);
729e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n");CHKERRQ(ierr);
730e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n");CHKERRQ(ierr);
731e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"           note on IBM RS6000 this slows run greatly\n");CHKERRQ(ierr);
732e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n");CHKERRQ(ierr);
73379dccf82SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -malloc: use PETSc error checking malloc (deprecated, use -malloc_debug)\n");CHKERRQ(ierr);
73479dccf82SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -malloc no: don't use PETSc error checking malloc (deprecated, use -malloc_debug no)\n");CHKERRQ(ierr);
7354161f2a3SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -malloc_info: prints total memory usage\n");CHKERRQ(ierr);
73692f119d6SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -malloc_view <optional filename>: keeps log of all memory allocations, displays in PetscFinalize()\n");CHKERRQ(ierr);
73779dccf82SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -malloc_debug <true or false>: enables or disables extended checking for memory corruption\n");CHKERRQ(ierr);
73826a7e8d4SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -options_view: dump list of options inputted\n");CHKERRQ(ierr);
739e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n");CHKERRQ(ierr);
740e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n");CHKERRQ(ierr);
741e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n");CHKERRQ(ierr);
742e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n");CHKERRQ(ierr);
743a8c7a070SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has separate tmp directory\n");CHKERRQ(ierr);
7440841954dSBarry Smith     ierr = (*PetscHelpPrintf)(comm," -memory_view: print memory usage at end of run\n");CHKERRQ(ierr);
745e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
746e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n");CHKERRQ(ierr);
747185ae32fSMatthew G. Knepley     ierr = (*PetscHelpPrintf)(comm," -log_view [:filename:[format]]: logging objects and events\n");CHKERRQ(ierr);
748e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n");CHKERRQ(ierr);
74956071f75SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -log_exclude <list,of,classnames>: exclude given classes from logging\n");CHKERRQ(ierr);
750e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE)
751495fc317SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through Jumpshot\n");CHKERRQ(ierr);
752e5c89e4eSSatish Balay #endif
753e94e781bSJacob Faibussowitsch #endif
754e94e781bSJacob Faibussowitsch #if defined(PETSC_USE_INFO)
755fe9b927eSVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -info [filename][:[~]<list,of,classnames>[:[~]self]]: print verbose information\n");CHKERRQ(ierr);
756e5c89e4eSSatish Balay #endif
757e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n");CHKERRQ(ierr);
758c5b5d8d5SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -options_monitor: monitor options to standard output, including that set previously e.g. in option files\n");CHKERRQ(ierr);
759c5b5d8d5SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -options_monitor_cancel: cancels all hardwired option monitors\n");CHKERRQ(ierr);
760e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n");CHKERRQ(ierr);
761e5c89e4eSSatish Balay   }
762e5c89e4eSSatish Balay 
76374ba8654SBarry Smith #if defined(PETSC_HAVE_POPEN)
76474ba8654SBarry Smith   {
76574ba8654SBarry Smith   char machine[128];
766589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-popen_machine",machine,sizeof(machine),&flg1);CHKERRQ(ierr);
76774ba8654SBarry Smith   if (flg1) {
76874ba8654SBarry Smith     ierr = PetscPOpenSetMachine(machine);CHKERRQ(ierr);
76974ba8654SBarry Smith   }
77074ba8654SBarry Smith   }
77174ba8654SBarry Smith #endif
77274ba8654SBarry Smith 
773c5929fdfSBarry Smith   ierr = PetscOptionsGetReal(NULL,NULL,"-petsc_sleep",&si,&flg1);CHKERRQ(ierr);
774e5c89e4eSSatish Balay   if (flg1) {
775e5c89e4eSSatish Balay     ierr = PetscSleep(si);CHKERRQ(ierr);
776e5c89e4eSSatish Balay   }
777e5c89e4eSSatish Balay 
778fdc842d1SBarry Smith #if defined(PETSC_HAVE_VIENNACL)
779c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);CHKERRQ(ierr);
780f14045dbSBarry Smith   if (!flg3) {
781c5929fdfSBarry Smith     ierr = PetscOptionsHasName(NULL,NULL,"-log_view",&flg3);CHKERRQ(ierr);
782f14045dbSBarry Smith   }
783c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-viennacl_synchronize",&flg3,NULL);CHKERRQ(ierr);
784f14045dbSBarry Smith   PetscViennaCLSynchronize = flg3;
785fdc842d1SBarry Smith   ierr = PetscViennaCLInit();CHKERRQ(ierr);
7864cf1874eSKarl Rupp #endif
78782f73ecaSAlejandro Lamas Daviña 
788fdc842d1SBarry Smith   /*
789fdc842d1SBarry Smith      Creates the logging data structures; this is enabled even if logging is not turned on
790fdc842d1SBarry Smith      This is the last thing we do before returning to the user code to prevent having the
791fdc842d1SBarry Smith      logging numbers contaminated by any startup time associated with MPI and the GPUs
792fdc842d1SBarry Smith   */
793fdc842d1SBarry Smith #if defined(PETSC_USE_LOG)
794fdc842d1SBarry Smith   ierr = PetscLogInitialize();CHKERRQ(ierr);
795f0a7718cSKarl Rupp #endif
796f0a7718cSKarl Rupp 
797e5c89e4eSSatish Balay   PetscFunctionReturn(0);
798e5c89e4eSSatish Balay }
799