xref: /petsc/src/sys/objects/init.c (revision d314f959b903bf4d8578b17dc51a9e0771116dff)
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
23d9b72601SDominic Meiser #if defined(PETSC_HAVE_CUDA)
24d9b72601SDominic Meiser #include <cuda_runtime.h>
257fd2f626SJose E. Roman #include <petsccublas.h>
26c2a741eeSJunchao Zhang #if defined(PETSC_HAVE_OMPI_MAJOR_VERSION)
27c2a741eeSJunchao Zhang #include "mpi-ext.h" /* Needed for OpenMPI CUDA-aware check */
28c2a741eeSJunchao Zhang #endif
29d9b72601SDominic Meiser #endif
30555d055bSBarry Smith 
31f0a7718cSKarl Rupp #if defined(PETSC_HAVE_VIENNACL)
32f0a7718cSKarl Rupp PETSC_EXTERN PetscErrorCode PetscViennaCLInit();
33f0a7718cSKarl Rupp #endif
34f0a7718cSKarl Rupp 
35e5c89e4eSSatish Balay /* ------------------------Nasty global variables -------------------------------*/
36e5c89e4eSSatish Balay /*
37e5c89e4eSSatish Balay      Indicates if PETSc started up MPI, or it was
38e5c89e4eSSatish Balay    already started before PETSc was initialized.
39e5c89e4eSSatish Balay */
400cbf60d0SJose E. Roman PetscBool   PetscBeganMPI         = PETSC_FALSE;
418ad20175SVaclav Hapla PetscBool   PetscErrorHandlingInitialized = PETSC_FALSE;
420cbf60d0SJose E. Roman PetscBool   PetscInitializeCalled = PETSC_FALSE;
430cbf60d0SJose E. Roman PetscBool   PetscFinalizeCalled   = PETSC_FALSE;
44bffec52eSKarl Rupp PetscBool   PetscCUDAInitialized  = PETSC_FALSE;
45d6f2c3cbSBarry Smith 
467087cfbeSBarry Smith PetscMPIInt PetscGlobalRank       = -1;
477087cfbeSBarry Smith PetscMPIInt PetscGlobalSize       = -1;
48ba61063dSBarry Smith 
49c2a741eeSJunchao Zhang PetscBool   use_gpu_aware_mpi     = PETSC_TRUE;
50c2a741eeSJunchao Zhang 
5150f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
52e5c89e4eSSatish Balay #if defined(PETSC_COMPLEX_INSTANTIATE)
53e5c89e4eSSatish Balay template <> class std::complex<double>; /* instantiate complex template class */
54e5c89e4eSSatish Balay #endif
552c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
56500d8756SSatish Balay MPI_Datatype MPIU_C_DOUBLE_COMPLEX;
57500d8756SSatish Balay MPI_Datatype MPIU_C_COMPLEX;
582c876bd9SBarry Smith #endif
598619c96cSJed Brown 
608619c96cSJed Brown /*MC
618619c96cSJed Brown    PETSC_i - the imaginary number i
628619c96cSJed Brown 
638619c96cSJed Brown    Synopsis:
648619c96cSJed Brown    #include <petscsys.h>
658619c96cSJed Brown    PetscComplex PETSC_i;
668619c96cSJed Brown 
678619c96cSJed Brown    Level: beginner
688619c96cSJed Brown 
698619c96cSJed Brown    Note:
708cd53115SBarry Smith    Complex numbers are automatically available if PETSc located a working complex implementation
718619c96cSJed Brown 
728619c96cSJed Brown .seealso: PetscRealPart(), PetscImaginaryPart(), PetscRealPartComplex(), PetscImaginaryPartComplex()
738619c96cSJed Brown M*/
7450f81f78SJed Brown PetscComplex PETSC_i;
75e5c89e4eSSatish Balay #endif
76ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
77c90a1750SBarry Smith MPI_Datatype MPIU___FLOAT128 = 0;
787c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
798c764dc5SJose Roman MPI_Datatype MPIU___COMPLEX128 = 0;
808c764dc5SJose Roman #endif
81570b7f6dSBarry Smith #elif defined(PETSC_USE_REAL___FP16)
82570b7f6dSBarry Smith MPI_Datatype MPIU___FP16 = 0;
83c90a1750SBarry Smith #endif
847087cfbeSBarry Smith MPI_Datatype MPIU_2SCALAR = 0;
850354ff80SSatish Balay #if defined(PETSC_USE_64BIT_INDICES)
867087cfbeSBarry Smith MPI_Datatype MPIU_2INT = 0;
8744041f26SJed Brown #endif
888ad47952SJed Brown MPI_Datatype MPIU_BOOL;
898ad47952SJed Brown MPI_Datatype MPIU_ENUM;
907cdaf61dSJed Brown MPI_Datatype MPIU_FORTRANADDR;
91e316c87fSJed Brown MPI_Datatype MPIU_SIZE_T;
9275567043SBarry Smith 
93e5c89e4eSSatish Balay /*
94e5c89e4eSSatish Balay        Function that is called to display all error messages
95e5c89e4eSSatish Balay */
967087cfbeSBarry Smith PetscErrorCode (*PetscErrorPrintf)(const char [],...)          = PetscErrorPrintfDefault;
977087cfbeSBarry Smith PetscErrorCode (*PetscHelpPrintf)(MPI_Comm,const char [],...)  = PetscHelpPrintfDefault;
987087cfbeSBarry Smith PetscErrorCode (*PetscVFPrintf)(FILE*,const char[],va_list)    = PetscVFPrintfDefault;
99bab1f7e6SVictor Minden /*
1004cf1874eSKarl Rupp   This is needed to turn on/off GPU synchronization
1018b5db460SBarry Smith */
1024cf1874eSKarl Rupp PetscBool PetscViennaCLSynchronize = PETSC_FALSE;
10382f73ecaSAlejandro Lamas Daviña PetscBool PetscCUDASynchronize = PETSC_FALSE;
104bab1f7e6SVictor Minden 
105e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
106e5c89e4eSSatish Balay /*
107e5c89e4eSSatish Balay    Optional file where all PETSc output from various prints is saved
108e5c89e4eSSatish Balay */
10995c0884eSLisandro Dalcin PETSC_INTERN FILE *petsc_history;
1100298fd71SBarry Smith FILE *petsc_history = NULL;
111e5c89e4eSSatish Balay 
1127087cfbeSBarry Smith PetscErrorCode  PetscOpenHistoryFile(const char filename[],FILE **fd)
113e5c89e4eSSatish Balay {
114e5c89e4eSSatish Balay   PetscErrorCode ierr;
115e5c89e4eSSatish Balay   PetscMPIInt    rank,size;
116e5c89e4eSSatish Balay   char           pfile[PETSC_MAX_PATH_LEN],pname[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN],date[64];
117e5c89e4eSSatish Balay   char           version[256];
118e5c89e4eSSatish Balay 
119e5c89e4eSSatish Balay   PetscFunctionBegin;
120e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
121e5c89e4eSSatish Balay   if (!rank) {
122e5c89e4eSSatish Balay     char        arch[10];
123f56c2debSBarry Smith     int         err;
124f56c2debSBarry Smith 
125e5c89e4eSSatish Balay     ierr = PetscGetArchType(arch,10);CHKERRQ(ierr);
126e5c89e4eSSatish Balay     ierr = PetscGetDate(date,64);CHKERRQ(ierr);
127a523d312SBarry Smith     ierr = PetscGetVersion(version,256);CHKERRQ(ierr);
128e5c89e4eSSatish Balay     ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
129e5c89e4eSSatish Balay     if (filename) {
130e5c89e4eSSatish Balay       ierr = PetscFixFilename(filename,fname);CHKERRQ(ierr);
131e5c89e4eSSatish Balay     } else {
132589a23caSBarry Smith       ierr = PetscGetHomeDirectory(pfile,sizeof(pfile));CHKERRQ(ierr);
133589a23caSBarry Smith       ierr = PetscStrlcat(pfile,"/.petschistory",sizeof(pfile));CHKERRQ(ierr);
134e5c89e4eSSatish Balay       ierr = PetscFixFilename(pfile,fname);CHKERRQ(ierr);
135e5c89e4eSSatish Balay     }
136e5c89e4eSSatish Balay 
137a297a907SKarl Rupp     *fd = fopen(fname,"a");
138a297a907SKarl Rupp     if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file: %s",fname);
139a297a907SKarl Rupp 
140c0bb3764SVaclav Hapla     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"----------------------------------------\n");CHKERRQ(ierr);
141e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"%s %s\n",version,date);CHKERRQ(ierr);
142589a23caSBarry Smith     ierr = PetscGetProgramName(pname,sizeof(pname));CHKERRQ(ierr);
143e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"%s on a %s, %d proc. with options:\n",pname,arch,size);CHKERRQ(ierr);
144c0bb3764SVaclav Hapla     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"----------------------------------------\n");CHKERRQ(ierr);
145a297a907SKarl Rupp 
146f56c2debSBarry Smith     err = fflush(*fd);
147e32f2f54SBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
148e5c89e4eSSatish Balay   }
149e5c89e4eSSatish Balay   PetscFunctionReturn(0);
150e5c89e4eSSatish Balay }
151e5c89e4eSSatish Balay 
15295c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscCloseHistoryFile(FILE **fd)
153e5c89e4eSSatish Balay {
154e5c89e4eSSatish Balay   PetscErrorCode ierr;
155e5c89e4eSSatish Balay   PetscMPIInt    rank;
156e5c89e4eSSatish Balay   char           date[64];
157f56c2debSBarry Smith   int            err;
158e5c89e4eSSatish Balay 
159e5c89e4eSSatish Balay   PetscFunctionBegin;
160e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
161e5c89e4eSSatish Balay   if (!rank) {
162e5c89e4eSSatish Balay     ierr = PetscGetDate(date,64);CHKERRQ(ierr);
163c0bb3764SVaclav Hapla     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"----------------------------------------\n");CHKERRQ(ierr);
164e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"Finished at %s\n",date);CHKERRQ(ierr);
165c0bb3764SVaclav Hapla     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"----------------------------------------\n");CHKERRQ(ierr);
166f56c2debSBarry Smith     err  = fflush(*fd);
167e32f2f54SBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
168f56c2debSBarry Smith     err = fclose(*fd);
169e32f2f54SBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
170e5c89e4eSSatish Balay   }
171e5c89e4eSSatish Balay   PetscFunctionReturn(0);
172e5c89e4eSSatish Balay }
173e5c89e4eSSatish Balay 
174e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
175e5c89e4eSSatish Balay 
176e5c89e4eSSatish Balay /*
177e5c89e4eSSatish Balay    This is ugly and probably belongs somewhere else, but I want to
178e5c89e4eSSatish Balay   be able to put a true MPI abort error handler with command line args.
179e5c89e4eSSatish Balay 
180e5c89e4eSSatish Balay     This is so MPI errors in the debugger will leave all the stack
1813c311c98SBarry Smith   frames. The default MP_Abort() cleans up and exits thus providing no useful information
1823c311c98SBarry Smith   in the debugger hence we call abort() instead of MPI_Abort().
183e5c89e4eSSatish Balay */
184e5c89e4eSSatish Balay 
18533c7d699SBarry Smith void Petsc_MPI_AbortOnError(MPI_Comm *comm,PetscMPIInt *flag,...)
186e5c89e4eSSatish Balay {
187e5c89e4eSSatish Balay   PetscFunctionBegin;
1883c311c98SBarry Smith   (*PetscErrorPrintf)("MPI error %d\n",*flag);
189e5c89e4eSSatish Balay   abort();
190e5c89e4eSSatish Balay }
191e5c89e4eSSatish Balay 
19233c7d699SBarry Smith void Petsc_MPI_DebuggerOnError(MPI_Comm *comm,PetscMPIInt *flag,...)
193e5c89e4eSSatish Balay {
194e5c89e4eSSatish Balay   PetscErrorCode ierr;
195e5c89e4eSSatish Balay 
196e5c89e4eSSatish Balay   PetscFunctionBegin;
1973c311c98SBarry Smith   (*PetscErrorPrintf)("MPI error %d\n",*flag);
198e5c89e4eSSatish Balay   ierr = PetscAttachDebugger();
19941e02c4dSJunchao Zhang   if (ierr) PETSCABORT(*comm,*flag); /* hopeless so get out */
200e5c89e4eSSatish Balay }
201e5c89e4eSSatish Balay 
2027381773fSBarry Smith #if defined(PETSC_HAVE_CUDA)
2037381773fSBarry Smith /*@C
2047381773fSBarry Smith      PetscCUDAInitialize - Initializes the CUDA device and cuBLAS on the device
2057381773fSBarry Smith 
2067381773fSBarry Smith      Logically collective
2077381773fSBarry Smith 
2087381773fSBarry Smith   Input Parameter:
2097381773fSBarry Smith   comm - the MPI communicator that will utilize the CUDA devices
2107381773fSBarry Smith 
2117381773fSBarry Smith   Options Database:
212fb0502ccSJunchao Zhang +  -cuda_initialize <yes,no> - Default no. Do the initialization in PetscInitialize(). If -cuda_initialize no is used then the default initialization is done automatically
2139d90e779SBarry Smith                                when the first CUDA call is made unless you call PetscCUDAInitialize() before any CUDA operations are performed
2147381773fSBarry Smith .  -cuda_view - view information about the CUDA devices
2157381773fSBarry Smith .  -cuda_synchronize - wait at the end of asynchronize CUDA calls so that their time gets credited to the current event; default with -log_view
216c2a741eeSJunchao Zhang .  -cuda_set_device <gpu> - integer number of the device
217c2a741eeSJunchao Zhang -  -use_gpu_aware_mpi     - Assume the MPI is GPU-aware when communicating data on GPU
2187381773fSBarry Smith 
21990ea27d8SSatish Balay   Level: beginner
22090ea27d8SSatish Balay 
2217381773fSBarry Smith   Notes:
2227381773fSBarry Smith    Initializing cuBLAS takes about 1/2 second there it is done by default in PetscInitialize() before logging begins
2237381773fSBarry Smith 
2247381773fSBarry Smith @*/
2257381773fSBarry Smith PetscErrorCode PetscCUDAInitialize(MPI_Comm comm)
2267381773fSBarry Smith {
2277381773fSBarry Smith   PetscErrorCode        ierr;
2287381773fSBarry Smith   PetscInt              deviceOpt = 0;
2297381773fSBarry Smith   PetscBool             cuda_view_flag = PETSC_FALSE,flg;
2307381773fSBarry Smith   struct cudaDeviceProp prop;
2317381773fSBarry Smith   int                   devCount,device,devicecnt;
2327381773fSBarry Smith   cudaError_t           err = cudaSuccess;
2337381773fSBarry Smith   PetscMPIInt           rank,size;
2347381773fSBarry Smith 
2357381773fSBarry Smith   PetscFunctionBegin;
2367381773fSBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"CUDA options","Sys");CHKERRQ(ierr);
2377381773fSBarry Smith   ierr = PetscOptionsInt("-cuda_set_device","Set all MPI ranks to use the specified CUDA device",NULL,deviceOpt,&deviceOpt,&flg);CHKERRQ(ierr);
2387381773fSBarry Smith   device = (int)deviceOpt;
2397381773fSBarry Smith   ierr = PetscOptionsDeprecated("-cuda_show_devices","-cuda_view","3.12",NULL);CHKERRQ(ierr);
2407381773fSBarry Smith   ierr = PetscOptionsName("-cuda_view","Display CUDA device information and assignments",NULL,&cuda_view_flag);CHKERRQ(ierr);
2417381773fSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
2427381773fSBarry Smith   if (!PetscCUDAInitialized) {
2437381773fSBarry Smith     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
2447381773fSBarry Smith 
2457381773fSBarry Smith     if (size>1 && !flg) {
2467381773fSBarry Smith       /* check to see if we force multiple ranks to hit the same GPU */
2477381773fSBarry Smith       /* we're not using the same GPU on multiple MPI threads. So try to allocated different   GPUs to different processes */
2487381773fSBarry Smith 
2497381773fSBarry Smith       /* First get the device count */
2507381773fSBarry Smith       err = cudaGetDeviceCount(&devCount);
2517381773fSBarry Smith       if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceCount %s",cudaGetErrorString(err));
2527381773fSBarry Smith 
2537381773fSBarry Smith       /* next determine the rank and then set the device via a mod */
2547381773fSBarry Smith       ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
2557381773fSBarry Smith       device = rank % devCount;
2567381773fSBarry Smith     }
2577381773fSBarry Smith     err = cudaSetDevice(device);
2587381773fSBarry Smith     if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDevice %s",cudaGetErrorString(err));
2597381773fSBarry Smith 
2607381773fSBarry Smith     /* set the device flags so that it can map host memory */
2617381773fSBarry Smith     err = cudaSetDeviceFlags(cudaDeviceMapHost);
2627381773fSBarry Smith     if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDeviceFlags %s",cudaGetErrorString(err));
2637381773fSBarry Smith 
2647381773fSBarry Smith     ierr = PetscCUBLASInitializeHandle();CHKERRQ(ierr);
2653bbda9c2SStefano Zampini     ierr = PetscCUSOLVERDnInitializeHandle();CHKERRQ(ierr);
2667381773fSBarry Smith     PetscCUDAInitialized = PETSC_TRUE;
2677381773fSBarry Smith   }
2687381773fSBarry Smith   if (cuda_view_flag) {
2697381773fSBarry Smith     ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
2707381773fSBarry Smith     err  = cudaGetDeviceCount(&devCount);
2717381773fSBarry Smith     if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceCount %s",cudaGetErrorString(err));
2727381773fSBarry Smith     for (devicecnt = 0; devicecnt < devCount; ++devicecnt) {
2737381773fSBarry Smith       err = cudaGetDeviceProperties(&prop,devicecnt);
2747381773fSBarry Smith       if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceProperties %s",cudaGetErrorString(err));
2757381773fSBarry Smith       ierr = PetscPrintf(comm, "CUDA device %d: %s\n", devicecnt, prop.name);CHKERRQ(ierr);
2767381773fSBarry Smith     }
2777381773fSBarry Smith     ierr = PetscSynchronizedPrintf(comm,"[%d] Using CUDA device %d.\n",rank,device);CHKERRQ(ierr);
2787381773fSBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
2797381773fSBarry Smith   }
2807381773fSBarry Smith   PetscFunctionReturn(0);
2817381773fSBarry Smith }
2827381773fSBarry Smith #endif
2837381773fSBarry Smith 
284e5c89e4eSSatish Balay /*@C
285e5c89e4eSSatish Balay    PetscEnd - Calls PetscFinalize() and then ends the program. This is useful if one
286e5c89e4eSSatish Balay      wishes a clean exit somewhere deep in the program.
287e5c89e4eSSatish Balay 
288e5c89e4eSSatish Balay    Collective on PETSC_COMM_WORLD
289e5c89e4eSSatish Balay 
290e5c89e4eSSatish Balay    Options Database Keys are the same as for PetscFinalize()
291e5c89e4eSSatish Balay 
292e5c89e4eSSatish Balay    Level: advanced
293e5c89e4eSSatish Balay 
294e5c89e4eSSatish Balay    Note:
295e5c89e4eSSatish Balay    See PetscInitialize() for more general runtime options.
296e5c89e4eSSatish Balay 
29788c29154SBarry Smith .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscFinalize()
298e5c89e4eSSatish Balay @*/
2997087cfbeSBarry Smith PetscErrorCode  PetscEnd(void)
300e5c89e4eSSatish Balay {
301e5c89e4eSSatish Balay   PetscFunctionBegin;
302e5c89e4eSSatish Balay   PetscFinalize();
303e5c89e4eSSatish Balay   exit(0);
304e5c89e4eSSatish Balay   return 0;
305e5c89e4eSSatish Balay }
306e5c89e4eSSatish Balay 
307ace3abfcSBarry Smith PetscBool PetscOptionsPublish = PETSC_FALSE;
30895c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void);
30995c0884eSLisandro Dalcin PETSC_INTERN PetscBool      petscsetmallocvisited;
310e5c89e4eSSatish Balay static       char           emacsmachinename[256];
311e5c89e4eSSatish Balay 
31202c9f0b5SLisandro Dalcin PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = NULL;
31302c9f0b5SLisandro Dalcin PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = NULL;
314e5c89e4eSSatish Balay 
315e5c89e4eSSatish Balay /*@C
316e5c89e4eSSatish Balay    PetscSetHelpVersionFunctions - Sets functions that print help and version information
317e5c89e4eSSatish Balay    before the PETSc help and version information is printed. Must call BEFORE PetscInitialize().
318e5c89e4eSSatish Balay    This routine enables a "higher-level" package that uses PETSc to print its messages first.
319e5c89e4eSSatish Balay 
320e5c89e4eSSatish Balay    Input Parameter:
3210298fd71SBarry Smith +  help - the help function (may be NULL)
3220298fd71SBarry Smith -  version - the version function (may be NULL)
323e5c89e4eSSatish Balay 
324e5c89e4eSSatish Balay    Level: developer
325e5c89e4eSSatish Balay 
326e5c89e4eSSatish Balay @*/
3277087cfbeSBarry Smith PetscErrorCode  PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm),PetscErrorCode (*version)(MPI_Comm))
328e5c89e4eSSatish Balay {
329e5c89e4eSSatish Balay   PetscFunctionBegin;
330e5c89e4eSSatish Balay   PetscExternalHelpFunction    = help;
331e5c89e4eSSatish Balay   PetscExternalVersionFunction = version;
332e5c89e4eSSatish Balay   PetscFunctionReturn(0);
333e5c89e4eSSatish Balay }
334e5c89e4eSSatish Balay 
33505df10baSBarry Smith #if defined(PETSC_USE_LOG)
33695c0884eSLisandro Dalcin PETSC_INTERN PetscBool   PetscObjectsLog;
33705df10baSBarry Smith #endif
33805df10baSBarry Smith 
339e781f417SStefano Zampini void PetscMPI_Comm_eh(MPI_Comm *comm, PetscMPIInt *err, ...)
340e781f417SStefano Zampini {
341e781f417SStefano Zampini   if (PetscUnlikely(*err)) {
342e781f417SStefano Zampini     PetscMPIInt len;
343e781f417SStefano Zampini     char        errstring[MPI_MAX_ERROR_STRING];
344e781f417SStefano Zampini 
345e781f417SStefano Zampini     MPI_Error_string(*err,errstring,&len);
346e781f417SStefano Zampini     PetscError(MPI_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,PETSC_MPI_ERROR_CODE,PETSC_ERROR_INITIAL,"Internal error in MPI: %s",errstring);
347e781f417SStefano Zampini   }
348e781f417SStefano Zampini   return;
349e781f417SStefano Zampini }
350e781f417SStefano Zampini 
35157171095SVaclav Hapla PETSC_INTERN PetscErrorCode  PetscOptionsCheckInitial_Private(const char help[])
352e5c89e4eSSatish Balay {
353e5ed2c37SJose E. Roman   char              string[64];
354e5c89e4eSSatish Balay   MPI_Comm          comm = PETSC_COMM_WORLD;
35557171095SVaclav Hapla   PetscBool         flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flag,hasHelp;
356e5c89e4eSSatish Balay   PetscErrorCode    ierr;
35767584ceeSBarry Smith   PetscReal         si;
35828559dc8SJed Brown   PetscInt          intensity;
359e5c89e4eSSatish Balay   int               i;
360e5c89e4eSSatish Balay   PetscMPIInt       rank;
361*d314f959SVaclav Hapla   char              version[256];
3622479a3a6SBarry Smith #if defined(PETSC_USE_LOG)
363e5ed2c37SJose E. Roman   char              mname[PETSC_MAX_PATH_LEN];
3642479a3a6SBarry Smith   PetscViewerFormat format;
36567584ceeSBarry Smith   PetscBool         flg4 = PETSC_FALSE;
36667584ceeSBarry Smith #endif
367012f15ecSRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
368fb0502ccSJunchao Zhang   PetscBool         initCUDA = PETSC_FALSE,mpi_gpu_awareness;
369249d9b8bSJunchao Zhang   cudaError_t       cerr;
370249d9b8bSJunchao Zhang   int               devCount = 0;
371012f15ecSRichard Tran Mills #endif
3727381773fSBarry Smith 
373e5c89e4eSSatish Balay   PetscFunctionBegin;
37442218b76SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
375e5c89e4eSSatish Balay 
37667584ceeSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
37792f119d6SBarry Smith   if (!(PETSC_RUNNING_ON_VALGRIND)) {
378e5c89e4eSSatish Balay     /*
379e5c89e4eSSatish Balay       Setup the memory management; support for tracing malloc() usage
380e5c89e4eSSatish Balay     */
381244bdbccSBarry Smith     PetscBool         mdebug = PETSC_FALSE, eachcall = PETSC_FALSE, initializenan = PETSC_FALSE, mlog = PETSC_FALSE;
38292f119d6SBarry Smith 
38376bd3646SJed Brown     if (PetscDefined(USE_DEBUG)) {
38492f119d6SBarry Smith       mdebug        = PETSC_TRUE;
38592f119d6SBarry Smith       initializenan = PETSC_TRUE;
38692f119d6SBarry Smith       ierr   = PetscOptionsHasName(NULL,NULL,"-malloc_test",&flg1);CHKERRQ(ierr);
38776bd3646SJed Brown     } else {
38892f119d6SBarry Smith       /* don't warn about unused option */
38992f119d6SBarry Smith       ierr = PetscOptionsHasName(NULL,NULL,"-malloc_test",&flg1);CHKERRQ(ierr);
39092f119d6SBarry Smith       flg1 = PETSC_FALSE;
39176bd3646SJed Brown     }
39279dccf82SBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_debug",&flg2,&flg3);CHKERRQ(ierr);
39392f119d6SBarry Smith     if (flg1 || flg2) {
39492f119d6SBarry Smith       mdebug        = PETSC_TRUE;
39592f119d6SBarry Smith       eachcall      = PETSC_TRUE;
39692f119d6SBarry Smith       initializenan = PETSC_TRUE;
39779dccf82SBarry Smith     } else if (flg3 && !flg2) {
39879dccf82SBarry Smith       mdebug        = PETSC_FALSE;
39979dccf82SBarry Smith       eachcall      = PETSC_FALSE;
40079dccf82SBarry Smith       initializenan = PETSC_FALSE;
401e5c89e4eSSatish Balay     }
40292f119d6SBarry Smith 
40392f119d6SBarry Smith     ierr = PetscOptionsHasName(NULL,NULL,"-malloc_view",&mlog);CHKERRQ(ierr);
40492f119d6SBarry Smith     if (mlog) {
40592f119d6SBarry Smith       mdebug = PETSC_TRUE;
40692f119d6SBarry Smith     }
40779dccf82SBarry Smith     /* the next line is deprecated */
40892f119d6SBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-malloc",&mdebug,NULL);CHKERRQ(ierr);
40992f119d6SBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_dump",&mdebug,NULL);CHKERRQ(ierr);
41092f119d6SBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-log_view_memory",&mdebug,NULL);CHKERRQ(ierr);
41192f119d6SBarry Smith     if (mdebug) {
41292f119d6SBarry Smith       ierr = PetscMallocSetDebug(eachcall,initializenan);CHKERRQ(ierr);
41392f119d6SBarry Smith     }
41492f119d6SBarry Smith     if (mlog) {
41592f119d6SBarry Smith       PetscReal logthreshold = 0;
41692f119d6SBarry Smith       ierr = PetscOptionsGetReal(NULL,NULL,"-malloc_view_threshold",&logthreshold,NULL);CHKERRQ(ierr);
41792f119d6SBarry Smith       ierr = PetscMallocViewSet(logthreshold);CHKERRQ(ierr);
41892f119d6SBarry Smith     }
4198b254c29SBarry Smith #if defined(PETSC_USE_LOG)
4208b254c29SBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-log_view_memory",&PetscLogMemory,NULL);CHKERRQ(ierr);
4218b254c29SBarry Smith #endif
42292f119d6SBarry Smith   }
42392f119d6SBarry Smith 
424ba282f50SJed Brown   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_coalesce",&flg1,&flg2);CHKERRQ(ierr);
425ba282f50SJed Brown   if (flg2) {ierr = PetscMallocSetCoalesce(flg1);CHKERRQ(ierr);}
42690d69ab7SBarry Smith   flg1 = PETSC_FALSE;
4272f21b5c6SHong Zhang   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_hbw",&flg1,NULL);CHKERRQ(ierr);
428cf6f3811SHong Zhang   /* ignore this option if malloc is already set */
429cf6f3811SHong Zhang   if (flg1 && !petscsetmallocvisited) {ierr = PetscSetUseHBWMalloc_Private();CHKERRQ(ierr);}
430e5c89e4eSSatish Balay 
43190d69ab7SBarry Smith   flg1 = PETSC_FALSE;
432c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_info",&flg1,NULL);CHKERRQ(ierr);
4337783f70dSSatish Balay   if (!flg1) {
43490d69ab7SBarry Smith     flg1 = PETSC_FALSE;
435c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-memory_view",&flg1,NULL);CHKERRQ(ierr);
4367783f70dSSatish Balay   }
437e5c89e4eSSatish Balay   if (flg1) {
438e5c89e4eSSatish Balay     ierr = PetscMemorySetGetMaximumUsage();CHKERRQ(ierr);
439e5c89e4eSSatish Balay   }
44067584ceeSBarry Smith #endif
441e5c89e4eSSatish Balay 
442b4427426SBarry Smith #if defined(PETSC_USE_LOG)
443c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-objects_dump",&PetscObjectsLog);CHKERRQ(ierr);
444b4427426SBarry Smith #endif
44505df10baSBarry Smith 
446e5c89e4eSSatish Balay   /*
447e5c89e4eSSatish Balay       Set the display variable for graphics
448e5c89e4eSSatish Balay   */
449e5c89e4eSSatish Balay   ierr = PetscSetDisplay();CHKERRQ(ierr);
450e5c89e4eSSatish Balay 
45151dcc849SKerry Stevens   /*
45257171095SVaclav Hapla      Print main application help message
45357171095SVaclav Hapla   */
45457171095SVaclav Hapla   ierr = PetscOptionsHasHelp(NULL,&hasHelp);CHKERRQ(ierr);
45557171095SVaclav Hapla   if (help && hasHelp) {
45657171095SVaclav Hapla     ierr = PetscPrintf(comm,help);CHKERRQ(ierr);
45757171095SVaclav Hapla     ierr = PetscPrintf(comm,"----------------------------------------\n");CHKERRQ(ierr);
45857171095SVaclav Hapla   }
45957171095SVaclav Hapla 
46057171095SVaclav Hapla   /*
461e5c89e4eSSatish Balay       Print the PETSc version information
462e5c89e4eSSatish Balay   */
463*d314f959SVaclav Hapla   ierr = PetscOptionsHasName(NULL,NULL,"-version",&flg1);CHKERRQ(ierr);
464*d314f959SVaclav Hapla   if (flg1 || hasHelp) {
465e5c89e4eSSatish Balay     /*
466e5c89e4eSSatish Balay        Print "higher-level" package version message
467e5c89e4eSSatish Balay     */
468e5c89e4eSSatish Balay     if (PetscExternalVersionFunction) {
469e5c89e4eSSatish Balay       ierr = (*PetscExternalVersionFunction)(comm);CHKERRQ(ierr);
470e5c89e4eSSatish Balay     }
471e5c89e4eSSatish Balay 
472a523d312SBarry Smith     ierr = PetscGetVersion(version,256);CHKERRQ(ierr);
473e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"%s\n",version);CHKERRQ(ierr);
474e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO);CHKERRQ(ierr);
475e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n");CHKERRQ(ierr);
47684e42920SBarry Smith     ierr = (*PetscHelpPrintf)(comm,"See docs/faq.html for problems.\n");CHKERRQ(ierr);
477e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n");CHKERRQ(ierr);
478e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR);CHKERRQ(ierr);
479c0bb3764SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm,"----------------------------------------\n");CHKERRQ(ierr);
4807ca660e7SBarry Smith   }
4817ca660e7SBarry Smith 
48294941ca7SBarry Smith   /*
48394941ca7SBarry Smith        Print "higher-level" package help message
48494941ca7SBarry Smith   */
48557171095SVaclav Hapla   if (hasHelp) {
486*d314f959SVaclav Hapla     PetscBool hasHelpIntro;
487*d314f959SVaclav Hapla 
48894941ca7SBarry Smith     if (PetscExternalHelpFunction) {
48994941ca7SBarry Smith       ierr = (*PetscExternalHelpFunction)(comm);CHKERRQ(ierr);
49094941ca7SBarry Smith     }
491*d314f959SVaclav Hapla     ierr = PetscOptionsHasHelpIntro_Internal(NULL,&hasHelpIntro);CHKERRQ(ierr);
492*d314f959SVaclav Hapla     if (hasHelpIntro) {
493bdb346e9SBarry Smith       ierr = PetscOptionsDestroyDefault();CHKERRQ(ierr);
494008a6e76SBarry Smith       ierr = PetscFreeMPIResources();CHKERRQ(ierr);
4957ca660e7SBarry Smith       ierr = MPI_Finalize();CHKERRQ(ierr);
4967ca660e7SBarry Smith       exit(0);
4977ca660e7SBarry Smith     }
498e5c89e4eSSatish Balay   }
499e5c89e4eSSatish Balay 
500e5c89e4eSSatish Balay   /*
501e5c89e4eSSatish Balay       Setup the error handling
502e5c89e4eSSatish Balay   */
50390d69ab7SBarry Smith   flg1 = PETSC_FALSE;
504c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-on_error_abort",&flg1,NULL);CHKERRQ(ierr);
505b59baad1SJed Brown   if (flg1) {
50642218b76SBarry Smith     ierr = MPI_Comm_set_errhandler(comm,MPI_ERRORS_ARE_FATAL);CHKERRQ(ierr);
50702c9f0b5SLisandro Dalcin     ierr = PetscPushErrorHandler(PetscAbortErrorHandler,NULL);CHKERRQ(ierr);
508b59baad1SJed Brown   }
50990d69ab7SBarry Smith   flg1 = PETSC_FALSE;
510c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-on_error_mpiabort",&flg1,NULL);CHKERRQ(ierr);
51102c9f0b5SLisandro Dalcin   if (flg1) { ierr = PetscPushErrorHandler(PetscMPIAbortErrorHandler,NULL);CHKERRQ(ierr);}
51290d69ab7SBarry Smith   flg1 = PETSC_FALSE;
513c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-mpi_return_on_error",&flg1,NULL);CHKERRQ(ierr);
514e5c89e4eSSatish Balay   if (flg1) {
515d54338ecSKarl Rupp     ierr = MPI_Comm_set_errhandler(comm,MPI_ERRORS_RETURN);CHKERRQ(ierr);
516e5c89e4eSSatish Balay   }
517e781f417SStefano Zampini   /* experimental */
518e781f417SStefano Zampini   flg1 = PETSC_FALSE;
519e781f417SStefano Zampini   ierr = PetscOptionsGetBool(NULL,NULL,"-mpi_return_error_string",&flg1,NULL);CHKERRQ(ierr);
520e781f417SStefano Zampini   if (flg1) {
521e781f417SStefano Zampini     MPI_Errhandler eh;
522e781f417SStefano Zampini 
523e781f417SStefano Zampini     ierr = MPI_Comm_create_errhandler(PetscMPI_Comm_eh,&eh);CHKERRQ(ierr);
524e781f417SStefano Zampini     ierr = MPI_Comm_set_errhandler(comm,eh);CHKERRQ(ierr);
525e781f417SStefano Zampini     ierr = MPI_Errhandler_free(&eh);CHKERRQ(ierr);
526e781f417SStefano Zampini   }
52790d69ab7SBarry Smith   flg1 = PETSC_FALSE;
528c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-no_signal_handler",&flg1,NULL);CHKERRQ(ierr);
5298d359177SBarry Smith   if (!flg1) {ierr = PetscPushSignalHandler(PetscSignalHandlerDefault,(void*)0);CHKERRQ(ierr);}
530e5c89e4eSSatish Balay 
531e5c89e4eSSatish Balay   /*
532e5c89e4eSSatish Balay       Setup debugger information
533e5c89e4eSSatish Balay   */
534e5c89e4eSSatish Balay   ierr = PetscSetDefaultDebugger();CHKERRQ(ierr);
535589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-on_error_attach_debugger",string,sizeof(string),&flg1);CHKERRQ(ierr);
536e5c89e4eSSatish Balay   if (flg1) {
537e5c89e4eSSatish Balay     MPI_Errhandler err_handler;
538e5c89e4eSSatish Balay 
539e5c89e4eSSatish Balay     ierr = PetscSetDebuggerFromString(string);CHKERRQ(ierr);
54033c7d699SBarry Smith     ierr = MPI_Comm_create_errhandler(Petsc_MPI_DebuggerOnError,&err_handler);CHKERRQ(ierr);
541d54338ecSKarl Rupp     ierr = MPI_Comm_set_errhandler(comm,err_handler);CHKERRQ(ierr);
54202c9f0b5SLisandro Dalcin     ierr = PetscPushErrorHandler(PetscAttachDebuggerErrorHandler,NULL);CHKERRQ(ierr);
543e5c89e4eSSatish Balay   }
544589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-debug_terminal",string,sizeof(string),&flg1);CHKERRQ(ierr);
5455e96ac45SJed Brown   if (flg1) { ierr = PetscSetDebugTerminal(string);CHKERRQ(ierr); }
546589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-start_in_debugger",string,sizeof(string),&flg1);CHKERRQ(ierr);
547589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-stop_for_debugger",string,sizeof(string),&flg2);CHKERRQ(ierr);
548e5c89e4eSSatish Balay   if (flg1 || flg2) {
549e5c89e4eSSatish Balay     PetscMPIInt    size;
550e5c89e4eSSatish Balay     PetscInt       lsize,*nodes;
551e5c89e4eSSatish Balay     MPI_Errhandler err_handler;
552e5c89e4eSSatish Balay     /*
553e5c89e4eSSatish Balay        we have to make sure that all processors have opened
554e5c89e4eSSatish Balay        connections to all other processors, otherwise once the
555e5c89e4eSSatish Balay        debugger has stated it is likely to receive a SIGUSR1
556e5c89e4eSSatish Balay        and kill the program.
557e5c89e4eSSatish Balay     */
55842218b76SBarry Smith     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
559e5c89e4eSSatish Balay     if (size > 2) {
560533163c2SBarry Smith       PetscMPIInt dummy = 0;
561e5c89e4eSSatish Balay       MPI_Status  status;
562e5c89e4eSSatish Balay       for (i=0; i<size; i++) {
563e5c89e4eSSatish Balay         if (rank != i) {
56442218b76SBarry Smith           ierr = MPI_Send(&dummy,1,MPI_INT,i,109,comm);CHKERRQ(ierr);
565e5c89e4eSSatish Balay         }
566e5c89e4eSSatish Balay       }
567e5c89e4eSSatish Balay       for (i=0; i<size; i++) {
568e5c89e4eSSatish Balay         if (rank != i) {
56942218b76SBarry Smith           ierr = MPI_Recv(&dummy,1,MPI_INT,i,109,comm,&status);CHKERRQ(ierr);
570e5c89e4eSSatish Balay         }
571e5c89e4eSSatish Balay       }
572e5c89e4eSSatish Balay     }
573e5c89e4eSSatish Balay     /* check if this processor node should be in debugger */
574785e854fSJed Brown     ierr  = PetscMalloc1(size,&nodes);CHKERRQ(ierr);
575e5c89e4eSSatish Balay     lsize = size;
576c5929fdfSBarry Smith     ierr  = PetscOptionsGetIntArray(NULL,NULL,"-debugger_nodes",nodes,&lsize,&flag);CHKERRQ(ierr);
577e5c89e4eSSatish Balay     if (flag) {
578e5c89e4eSSatish Balay       for (i=0; i<lsize; i++) {
579e5c89e4eSSatish Balay         if (nodes[i] == rank) { flag = PETSC_FALSE; break; }
580e5c89e4eSSatish Balay       }
581e5c89e4eSSatish Balay     }
582e5c89e4eSSatish Balay     if (!flag) {
583e5c89e4eSSatish Balay       ierr = PetscSetDebuggerFromString(string);CHKERRQ(ierr);
58402c9f0b5SLisandro Dalcin       ierr = PetscPushErrorHandler(PetscAbortErrorHandler,NULL);CHKERRQ(ierr);
585e5c89e4eSSatish Balay       if (flg1) {
586e5c89e4eSSatish Balay         ierr = PetscAttachDebugger();CHKERRQ(ierr);
587e5c89e4eSSatish Balay       } else {
588e5c89e4eSSatish Balay         ierr = PetscStopForDebugger();CHKERRQ(ierr);
589e5c89e4eSSatish Balay       }
59033c7d699SBarry Smith       ierr = MPI_Comm_create_errhandler(Petsc_MPI_AbortOnError,&err_handler);CHKERRQ(ierr);
591d54338ecSKarl Rupp       ierr = MPI_Comm_set_errhandler(comm,err_handler);CHKERRQ(ierr);
592e5c89e4eSSatish Balay     }
593e5c89e4eSSatish Balay     ierr = PetscFree(nodes);CHKERRQ(ierr);
594e5c89e4eSSatish Balay   }
595e5c89e4eSSatish Balay 
596589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-on_error_emacs",emacsmachinename,sizeof(emacsmachinename),&flg1);CHKERRQ(ierr);
597cb9801acSJed Brown   if (flg1 && !rank) {ierr = PetscPushErrorHandler(PetscEmacsClientErrorHandler,emacsmachinename);CHKERRQ(ierr);}
598e5c89e4eSSatish Balay 
599e5c89e4eSSatish Balay   /*
600e5c89e4eSSatish Balay         Setup profiling and logging
601e5c89e4eSSatish Balay   */
6026cf91177SBarry Smith #if defined(PETSC_USE_INFO)
6038bb29257SSatish Balay   {
604e94e781bSJacob Faibussowitsch     ierr = PetscInfoSetFromOptions(NULL);CHKERRQ(ierr);
605e5c89e4eSSatish Balay   }
606865f6aa8SSatish Balay #endif
607aba4c478SBarry Smith   ierr = PetscDetermineInitialFPTrap();
608cc9df77eSBarry Smith   flg1 = PETSC_FALSE;
609cc9df77eSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-fp_trap",&flg1,&flag);CHKERRQ(ierr);
610cc9df77eSBarry Smith   if (flag) {ierr = PetscSetFPTrap((PetscFPTrap)flg1);CHKERRQ(ierr);}
611cc9df77eSBarry Smith   ierr = PetscOptionsGetInt(NULL,NULL,"-check_pointer_intensity",&intensity,&flag);CHKERRQ(ierr);
612cc9df77eSBarry Smith   if (flag) {ierr = PetscCheckPointerSetIntensity(intensity);CHKERRQ(ierr);}
613865f6aa8SSatish Balay #if defined(PETSC_USE_LOG)
614865f6aa8SSatish Balay   mname[0] = 0;
615589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-history",mname,sizeof(mname),&flg1);CHKERRQ(ierr);
616865f6aa8SSatish Balay   if (flg1) {
617865f6aa8SSatish Balay     if (mname[0]) {
618f3dea69dSBarry Smith       ierr = PetscOpenHistoryFile(mname,&petsc_history);CHKERRQ(ierr);
619865f6aa8SSatish Balay     } else {
620706d7a88SBarry Smith       ierr = PetscOpenHistoryFile(NULL,&petsc_history);CHKERRQ(ierr);
621865f6aa8SSatish Balay     }
622865f6aa8SSatish Balay   }
623217044c2SLisandro Dalcin 
624217044c2SLisandro Dalcin   ierr = PetscOptionsGetBool(NULL,NULL,"-log_sync",&PetscLogSyncOn,NULL);CHKERRQ(ierr);
625217044c2SLisandro Dalcin 
626e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE)
62790d69ab7SBarry Smith   flg1 = PETSC_FALSE;
628c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-log_mpe",&flg1);CHKERRQ(ierr);
629495fc317SBarry Smith   if (flg1) {ierr = PetscLogMPEBegin();CHKERRQ(ierr);}
630e5c89e4eSSatish Balay #endif
63190d69ab7SBarry Smith   flg1 = PETSC_FALSE;
63290d69ab7SBarry Smith   flg3 = PETSC_FALSE;
633c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-log_all",&flg1,NULL);CHKERRQ(ierr);
634c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);CHKERRQ(ierr);
635e5c89e4eSSatish Balay   if (flg1)                      { ierr = PetscLogAllBegin();CHKERRQ(ierr); }
636bb1d7374SBarry Smith   else if (flg3)                 { ierr = PetscLogDefaultBegin();CHKERRQ(ierr);}
637e5c89e4eSSatish Balay 
638589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-log_trace",mname,sizeof(mname),&flg1);CHKERRQ(ierr);
639e5c89e4eSSatish Balay   if (flg1) {
640e5c89e4eSSatish Balay     char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN];
641e5c89e4eSSatish Balay     FILE *file;
642e5c89e4eSSatish Balay     if (mname[0]) {
6432e924ca5SSatish Balay       PetscSNPrintf(name,PETSC_MAX_PATH_LEN,"%s.%d",mname,rank);
644e5c89e4eSSatish Balay       ierr = PetscFixFilename(name,fname);CHKERRQ(ierr);
645e5c89e4eSSatish Balay       file = fopen(fname,"w");
646f3dea69dSBarry Smith       if (!file) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname);
647a297a907SKarl Rupp     } else file = PETSC_STDOUT;
648e5c89e4eSSatish Balay     ierr = PetscLogTraceBegin(file);CHKERRQ(ierr);
649e5c89e4eSSatish Balay   }
650bb1d7374SBarry Smith 
65116413a6aSBarry Smith   ierr = PetscOptionsGetViewer(comm,NULL,NULL,"-log_view",NULL,&format,&flg4);CHKERRQ(ierr);
652bb1d7374SBarry Smith   if (flg4) {
653bb1d7374SBarry Smith     if (format == PETSC_VIEWER_ASCII_XML) {
654bb1d7374SBarry Smith       ierr = PetscLogNestedBegin();CHKERRQ(ierr);
655bb1d7374SBarry Smith     } else {
656bb1d7374SBarry Smith       ierr = PetscLogDefaultBegin();CHKERRQ(ierr);
657bb1d7374SBarry Smith     }
658eccbb886SLisandro Dalcin   }
659eccbb886SLisandro Dalcin   if (flg4 && format == PETSC_VIEWER_ASCII_XML) {
660eccbb886SLisandro Dalcin     PetscReal threshold = PetscRealConstant(0.01);
661eccbb886SLisandro Dalcin     ierr = PetscOptionsGetReal(NULL,NULL,"-log_threshold",&threshold,&flg1);CHKERRQ(ierr);
662eccbb886SLisandro Dalcin     if (flg1) {ierr = PetscLogSetThreshold((PetscLogDouble)threshold,NULL);CHKERRQ(ierr);}
663bb1d7374SBarry Smith   }
664e5c89e4eSSatish Balay #endif
665e5c89e4eSSatish Balay 
666c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-saws_options",&PetscOptionsPublish,NULL);CHKERRQ(ierr);
667e5c89e4eSSatish Balay 
6684b0a73b2SBarry Smith #if defined(PETSC_HAVE_CUDA)
6698d4e85a7SStefano Zampini   /*
6708d4e85a7SStefano Zampini      If collecting logging information, by default, wait for GPU to complete its operations
6718d4e85a7SStefano Zampini      before returning to the CPU in order to get accurate timings of each event
6728d4e85a7SStefano Zampini   */
6738d4e85a7SStefano Zampini   ierr = PetscOptionsHasName(NULL,NULL,"-log_summary",&PetscCUDASynchronize);CHKERRQ(ierr);
6748d4e85a7SStefano Zampini   if (!PetscCUDASynchronize) {
6758d4e85a7SStefano Zampini     ierr = PetscOptionsHasName(NULL,NULL,"-log_view",&PetscCUDASynchronize);CHKERRQ(ierr);
6768d4e85a7SStefano Zampini   }
6778d4e85a7SStefano Zampini 
6787381773fSBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"CUDA initialize","Sys");CHKERRQ(ierr);
67947e6383dSRichard Tran Mills   ierr = PetscOptionsBool("-cuda_initialize","Initialize the CUDA devices and cuBLAS during PetscInitialize()",NULL,initCUDA,&initCUDA,NULL);CHKERRQ(ierr);
6808d4e85a7SStefano Zampini   ierr = PetscOptionsBool("-cuda_synchronize","Wait for the GPU to complete operations before returning to the CPU (on by default with -log_summary or -log_view)",NULL,PetscCUDASynchronize,&PetscCUDASynchronize,NULL);CHKERRQ(ierr);
681c2a741eeSJunchao Zhang   ierr = PetscOptionsBool("-use_gpu_aware_mpi","Use GPU-aware MPI",NULL,use_gpu_aware_mpi,&use_gpu_aware_mpi,NULL);CHKERRQ(ierr);
682012f15ecSRichard Tran Mills   ierr = PetscOptionsEnd();CHKERRQ(ierr);
68347e6383dSRichard Tran Mills   if (initCUDA) {ierr = PetscCUDAInitialize(PETSC_COMM_WORLD);CHKERRQ(ierr);}
684fe2aebe2SStefano Zampini   if (use_gpu_aware_mpi) {
685249d9b8bSJunchao Zhang     cerr = cudaGetDeviceCount(&devCount);{if (cerr != cudaErrorNoDevice) CHKERRCUDA(cerr);} /* Catch other errors */
686249d9b8bSJunchao Zhang     if (cerr == cudaErrorNoDevice) devCount = 0; /* CUDA does not say what devCount is under this error */
687fe2aebe2SStefano Zampini   }
688249d9b8bSJunchao Zhang   if (devCount > 0 && use_gpu_aware_mpi) { /* Only do the MPI GPU awareness check when there are GPU(s) */
689223490aaSJed Brown #if defined(PETSC_HAVE_OMPI_MAJOR_VERSION) && defined(MPIX_CUDA_AWARE_SUPPORT) && MPIX_CUDA_AWARE_SUPPORT
690223490aaSJed Brown     /* Trust OpenMPI's compile time cuda query interface */
691c2a741eeSJunchao Zhang     mpi_gpu_awareness = PETSC_TRUE;
692c2a741eeSJunchao Zhang #else
693223490aaSJed Brown     /* For other MPI implementations without cuda query API, we do a GPU MPI call to see if it segfaults.
694223490aaSJed Brown        Note that Spectrum MPI sets OMPI_MAJOR_VERSION and is CUDA-aware, but does not have MPIX_CUDA_AWARE_SUPPORT.
695223490aaSJed Brown     */
696223490aaSJed Brown     mpi_gpu_awareness = PetscCheckMpiGpuAwareness();
697c2a741eeSJunchao Zhang #endif
698c2a741eeSJunchao Zhang     if (!mpi_gpu_awareness) {
699c2a741eeSJunchao Zhang       (*PetscErrorPrintf)("PETSc is configured with GPU support, but your MPI is not GPU-aware. For better performance, please use a GPU-aware MPI.\n");
700c2a741eeSJunchao Zhang       (*PetscErrorPrintf)("For IBM Spectrum MPI on OLCF Summit, you may need jsrun --smpiargs=-gpu.\n");
701c2a741eeSJunchao Zhang       (*PetscErrorPrintf)("For OpenMPI, you need to configure it --with-cuda (https://www.open-mpi.org/faq/?category=buildcuda)\n");
702c2a741eeSJunchao Zhang       (*PetscErrorPrintf)("For MVAPICH2-GDR, you need to set MV2_USE_CUDA=1 (http://mvapich.cse.ohio-state.edu/userguide/gdr/)\n");
703c2a741eeSJunchao Zhang       (*PetscErrorPrintf)("For Cray-MPICH, you need to set MPICH_RDMA_ENABLED_CUDA=1 (https://www.olcf.ornl.gov/tutorials/gpudirect-mpich-enabled-cuda/)\n");
704c2a741eeSJunchao Zhang       (*PetscErrorPrintf)("If you do not care, use option -use_gpu_aware_mpi 0, then PETSc will copy data from GPU to CPU for communication.\n");
705c2a741eeSJunchao Zhang       PETSCABORT(PETSC_COMM_WORLD,PETSC_ERR_LIB);
706c2a741eeSJunchao Zhang     }
707c2a741eeSJunchao Zhang   }
7084b0a73b2SBarry Smith #endif
7094b0a73b2SBarry Smith 
710e5c89e4eSSatish Balay   /*
711e5c89e4eSSatish Balay        Print basic help message
712e5c89e4eSSatish Balay   */
71357171095SVaclav Hapla   if (hasHelp) {
714e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"Options for all PETSc programs:\n");CHKERRQ(ierr);
715*d314f959SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -version: prints PETSc version\n");CHKERRQ(ierr);
716*d314f959SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -help intro: prints example description and PETSc version, and exits\n");CHKERRQ(ierr);
717*d314f959SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -help: prints example description, PETSc version, and available options for used routines\n");CHKERRQ(ierr);
718301d30feSBarry Smith     ierr = (*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is detected. Useful \n ");CHKERRQ(ierr);
719301d30feSBarry Smith     ierr = (*PetscHelpPrintf)(comm,"       only when run in the debugger\n");CHKERRQ(ierr);
720e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");CHKERRQ(ierr);
721e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"       start the debugger in new xterm\n");CHKERRQ(ierr);
722e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"       unless noxterm is given\n");CHKERRQ(ierr);
723e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");CHKERRQ(ierr);
724e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"       start all processes in the debugger\n");CHKERRQ(ierr);
725e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n");CHKERRQ(ierr);
726e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"    emacs jumps to error file\n");CHKERRQ(ierr);
727e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -debugger_nodes [n1,n2,..] Nodes to start in debugger\n");CHKERRQ(ierr);
728e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n");CHKERRQ(ierr);
729e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n");CHKERRQ(ierr);
730e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"                      waits the delay for you to attach\n");CHKERRQ(ierr);
7311cda70a7SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -display display: Location where X window graphics and debuggers are displayed\n");CHKERRQ(ierr);
732e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n");CHKERRQ(ierr);
733e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n");CHKERRQ(ierr);
734e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n");CHKERRQ(ierr);
735e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"           note on IBM RS6000 this slows run greatly\n");CHKERRQ(ierr);
736e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n");CHKERRQ(ierr);
73779dccf82SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -malloc: use PETSc error checking malloc (deprecated, use -malloc_debug)\n");CHKERRQ(ierr);
73879dccf82SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -malloc no: don't use PETSc error checking malloc (deprecated, use -malloc_debug no)\n");CHKERRQ(ierr);
7394161f2a3SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -malloc_info: prints total memory usage\n");CHKERRQ(ierr);
74092f119d6SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -malloc_view <optional filename>: keeps log of all memory allocations, displays in PetscFinalize()\n");CHKERRQ(ierr);
74179dccf82SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -malloc_debug <true or false>: enables or disables extended checking for memory corruption\n");CHKERRQ(ierr);
74226a7e8d4SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -options_view: dump list of options inputted\n");CHKERRQ(ierr);
743e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n");CHKERRQ(ierr);
744e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n");CHKERRQ(ierr);
745e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n");CHKERRQ(ierr);
746e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n");CHKERRQ(ierr);
747a8c7a070SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has separate tmp directory\n");CHKERRQ(ierr);
7480841954dSBarry Smith     ierr = (*PetscHelpPrintf)(comm," -memory_view: print memory usage at end of run\n");CHKERRQ(ierr);
749e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
750e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n");CHKERRQ(ierr);
751185ae32fSMatthew G. Knepley     ierr = (*PetscHelpPrintf)(comm," -log_view [:filename:[format]]: logging objects and events\n");CHKERRQ(ierr);
752e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n");CHKERRQ(ierr);
75356071f75SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -log_exclude <list,of,classnames>: exclude given classes from logging\n");CHKERRQ(ierr);
754e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE)
755495fc317SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through Jumpshot\n");CHKERRQ(ierr);
756e5c89e4eSSatish Balay #endif
757e94e781bSJacob Faibussowitsch #endif
758e94e781bSJacob Faibussowitsch #if defined(PETSC_USE_INFO)
759fe9b927eSVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -info [filename][:[~]<list,of,classnames>[:[~]self]]: print verbose information\n");CHKERRQ(ierr);
760e5c89e4eSSatish Balay #endif
761e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n");CHKERRQ(ierr);
762c5b5d8d5SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -options_monitor: monitor options to standard output, including that set previously e.g. in option files\n");CHKERRQ(ierr);
763c5b5d8d5SVaclav Hapla     ierr = (*PetscHelpPrintf)(comm," -options_monitor_cancel: cancels all hardwired option monitors\n");CHKERRQ(ierr);
764e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n");CHKERRQ(ierr);
765e5c89e4eSSatish Balay   }
766e5c89e4eSSatish Balay 
76774ba8654SBarry Smith #if defined(PETSC_HAVE_POPEN)
76874ba8654SBarry Smith   {
76974ba8654SBarry Smith   char machine[128];
770589a23caSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-popen_machine",machine,sizeof(machine),&flg1);CHKERRQ(ierr);
77174ba8654SBarry Smith   if (flg1) {
77274ba8654SBarry Smith     ierr = PetscPOpenSetMachine(machine);CHKERRQ(ierr);
77374ba8654SBarry Smith   }
77474ba8654SBarry Smith   }
77574ba8654SBarry Smith #endif
77674ba8654SBarry Smith 
777c5929fdfSBarry Smith   ierr = PetscOptionsGetReal(NULL,NULL,"-petsc_sleep",&si,&flg1);CHKERRQ(ierr);
778e5c89e4eSSatish Balay   if (flg1) {
779e5c89e4eSSatish Balay     ierr = PetscSleep(si);CHKERRQ(ierr);
780e5c89e4eSSatish Balay   }
781e5c89e4eSSatish Balay 
782fdc842d1SBarry Smith #if defined(PETSC_HAVE_VIENNACL)
783c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);CHKERRQ(ierr);
784f14045dbSBarry Smith   if (!flg3) {
785c5929fdfSBarry Smith     ierr = PetscOptionsHasName(NULL,NULL,"-log_view",&flg3);CHKERRQ(ierr);
786f14045dbSBarry Smith   }
787c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-viennacl_synchronize",&flg3,NULL);CHKERRQ(ierr);
788f14045dbSBarry Smith   PetscViennaCLSynchronize = flg3;
789fdc842d1SBarry Smith   ierr = PetscViennaCLInit();CHKERRQ(ierr);
7904cf1874eSKarl Rupp #endif
79182f73ecaSAlejandro Lamas Daviña 
792fdc842d1SBarry Smith   /*
793fdc842d1SBarry Smith      Creates the logging data structures; this is enabled even if logging is not turned on
794fdc842d1SBarry Smith      This is the last thing we do before returning to the user code to prevent having the
795fdc842d1SBarry Smith      logging numbers contaminated by any startup time associated with MPI and the GPUs
796fdc842d1SBarry Smith   */
797fdc842d1SBarry Smith #if defined(PETSC_USE_LOG)
798fdc842d1SBarry Smith   ierr = PetscLogInitialize();CHKERRQ(ierr);
799f0a7718cSKarl Rupp #endif
800f0a7718cSKarl Rupp 
801e5c89e4eSSatish Balay   PetscFunctionReturn(0);
802e5c89e4eSSatish Balay }
803