xref: /petsc/src/sys/objects/init.c (revision 7381773f4e66036016c7a176e56a1cca165d6524)
1e5c89e4eSSatish Balay /*
2e5c89e4eSSatish Balay 
3e5c89e4eSSatish Balay    This file defines part of the initialization of PETSc
4e5c89e4eSSatish Balay 
5f2d66bcaSShri Abhyankar   This file uses regular malloc and free because it cannot 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>
25fdc842d1SBarry Smith extern PetscErrorCode PetscCUBLASInitializeHandle(void);
26d9b72601SDominic Meiser #endif
27555d055bSBarry Smith 
28f0a7718cSKarl Rupp #if defined(PETSC_HAVE_VIENNACL)
29f0a7718cSKarl Rupp PETSC_EXTERN PetscErrorCode PetscViennaCLInit();
30f0a7718cSKarl Rupp #endif
31f0a7718cSKarl Rupp 
32e5c89e4eSSatish Balay /* ------------------------Nasty global variables -------------------------------*/
33e5c89e4eSSatish Balay /*
34e5c89e4eSSatish Balay      Indicates if PETSc started up MPI, or it was
35e5c89e4eSSatish Balay    already started before PETSc was initialized.
36e5c89e4eSSatish Balay */
370cbf60d0SJose E. Roman PetscBool   PetscBeganMPI         = PETSC_FALSE;
380cbf60d0SJose E. Roman PetscBool   PetscInitializeCalled = PETSC_FALSE;
390cbf60d0SJose E. Roman PetscBool   PetscFinalizeCalled   = PETSC_FALSE;
40bffec52eSKarl Rupp PetscBool   PetscCUDAInitialized  = PETSC_FALSE;
41d6f2c3cbSBarry Smith 
427087cfbeSBarry Smith PetscMPIInt PetscGlobalRank       = -1;
437087cfbeSBarry Smith PetscMPIInt PetscGlobalSize       = -1;
44ba61063dSBarry Smith 
4550f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
46e5c89e4eSSatish Balay #if defined(PETSC_COMPLEX_INSTANTIATE)
47e5c89e4eSSatish Balay template <> class std::complex<double>; /* instantiate complex template class */
48e5c89e4eSSatish Balay #endif
492c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
50500d8756SSatish Balay MPI_Datatype MPIU_C_DOUBLE_COMPLEX;
51500d8756SSatish Balay MPI_Datatype MPIU_C_COMPLEX;
522c876bd9SBarry Smith #endif
538619c96cSJed Brown 
548619c96cSJed Brown /*MC
558619c96cSJed Brown    PETSC_i - the imaginary number i
568619c96cSJed Brown 
578619c96cSJed Brown    Synopsis:
588619c96cSJed Brown    #include <petscsys.h>
598619c96cSJed Brown    PetscComplex PETSC_i;
608619c96cSJed Brown 
618619c96cSJed Brown    Level: beginner
628619c96cSJed Brown 
638619c96cSJed Brown    Note:
648cd53115SBarry Smith    Complex numbers are automatically available if PETSc located a working complex implementation
658619c96cSJed Brown 
668619c96cSJed Brown .seealso: PetscRealPart(), PetscImaginaryPart(), PetscRealPartComplex(), PetscImaginaryPartComplex()
678619c96cSJed Brown M*/
6850f81f78SJed Brown PetscComplex PETSC_i;
69e5c89e4eSSatish Balay #endif
70ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
71c90a1750SBarry Smith MPI_Datatype MPIU___FLOAT128 = 0;
727c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
738c764dc5SJose Roman MPI_Datatype MPIU___COMPLEX128 = 0;
748c764dc5SJose Roman #endif
75570b7f6dSBarry Smith #elif defined(PETSC_USE_REAL___FP16)
76570b7f6dSBarry Smith MPI_Datatype MPIU___FP16 = 0;
77c90a1750SBarry Smith #endif
787087cfbeSBarry Smith MPI_Datatype MPIU_2SCALAR = 0;
7944041f26SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
807087cfbeSBarry Smith MPI_Datatype MPIU_2INT = 0;
8144041f26SJed Brown #endif
828ad47952SJed Brown MPI_Datatype MPIU_BOOL;
838ad47952SJed Brown MPI_Datatype MPIU_ENUM;
847cdaf61dSJed Brown MPI_Datatype MPIU_FORTRANADDR;
85e316c87fSJed Brown MPI_Datatype MPIU_SIZE_T;
8675567043SBarry Smith 
87e5c89e4eSSatish Balay /*
88e5c89e4eSSatish Balay        Function that is called to display all error messages
89e5c89e4eSSatish Balay */
907087cfbeSBarry Smith PetscErrorCode (*PetscErrorPrintf)(const char [],...)          = PetscErrorPrintfDefault;
917087cfbeSBarry Smith PetscErrorCode (*PetscHelpPrintf)(MPI_Comm,const char [],...)  = PetscHelpPrintfDefault;
927087cfbeSBarry Smith PetscErrorCode (*PetscVFPrintf)(FILE*,const char[],va_list)    = PetscVFPrintfDefault;
93bab1f7e6SVictor Minden /*
944cf1874eSKarl Rupp   This is needed to turn on/off GPU synchronization
958b5db460SBarry Smith */
964cf1874eSKarl Rupp PetscBool PetscViennaCLSynchronize = PETSC_FALSE;
9782f73ecaSAlejandro Lamas Daviña PetscBool PetscCUDASynchronize = PETSC_FALSE;
98bab1f7e6SVictor Minden 
99e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
100e5c89e4eSSatish Balay /*
101e5c89e4eSSatish Balay    Optional file where all PETSc output from various prints is saved
102e5c89e4eSSatish Balay */
10395c0884eSLisandro Dalcin PETSC_INTERN FILE *petsc_history;
1040298fd71SBarry Smith FILE *petsc_history = NULL;
105e5c89e4eSSatish Balay 
1067087cfbeSBarry Smith PetscErrorCode  PetscOpenHistoryFile(const char filename[],FILE **fd)
107e5c89e4eSSatish Balay {
108e5c89e4eSSatish Balay   PetscErrorCode ierr;
109e5c89e4eSSatish Balay   PetscMPIInt    rank,size;
110e5c89e4eSSatish Balay   char           pfile[PETSC_MAX_PATH_LEN],pname[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN],date[64];
111e5c89e4eSSatish Balay   char           version[256];
112e5c89e4eSSatish Balay 
113e5c89e4eSSatish Balay   PetscFunctionBegin;
114e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
115e5c89e4eSSatish Balay   if (!rank) {
116e5c89e4eSSatish Balay     char        arch[10];
117f56c2debSBarry Smith     int         err;
118f56c2debSBarry Smith 
119e5c89e4eSSatish Balay     ierr = PetscGetArchType(arch,10);CHKERRQ(ierr);
120e5c89e4eSSatish Balay     ierr = PetscGetDate(date,64);CHKERRQ(ierr);
121a523d312SBarry Smith     ierr = PetscGetVersion(version,256);CHKERRQ(ierr);
122e5c89e4eSSatish Balay     ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
123e5c89e4eSSatish Balay     if (filename) {
124e5c89e4eSSatish Balay       ierr = PetscFixFilename(filename,fname);CHKERRQ(ierr);
125e5c89e4eSSatish Balay     } else {
126e5c89e4eSSatish Balay       ierr = PetscGetHomeDirectory(pfile,240);CHKERRQ(ierr);
127e5c89e4eSSatish Balay       ierr = PetscStrcat(pfile,"/.petschistory");CHKERRQ(ierr);
128e5c89e4eSSatish Balay       ierr = PetscFixFilename(pfile,fname);CHKERRQ(ierr);
129e5c89e4eSSatish Balay     }
130e5c89e4eSSatish Balay 
131a297a907SKarl Rupp     *fd = fopen(fname,"a");
132a297a907SKarl Rupp     if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file: %s",fname);
133a297a907SKarl Rupp 
134e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");CHKERRQ(ierr);
135e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"%s %s\n",version,date);CHKERRQ(ierr);
136e5c89e4eSSatish Balay     ierr = PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
137e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"%s on a %s, %d proc. with options:\n",pname,arch,size);CHKERRQ(ierr);
138e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");CHKERRQ(ierr);
139a297a907SKarl Rupp 
140f56c2debSBarry Smith     err = fflush(*fd);
141e32f2f54SBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
142e5c89e4eSSatish Balay   }
143e5c89e4eSSatish Balay   PetscFunctionReturn(0);
144e5c89e4eSSatish Balay }
145e5c89e4eSSatish Balay 
14695c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscCloseHistoryFile(FILE **fd)
147e5c89e4eSSatish Balay {
148e5c89e4eSSatish Balay   PetscErrorCode ierr;
149e5c89e4eSSatish Balay   PetscMPIInt    rank;
150e5c89e4eSSatish Balay   char           date[64];
151f56c2debSBarry Smith   int            err;
152e5c89e4eSSatish Balay 
153e5c89e4eSSatish Balay   PetscFunctionBegin;
154e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
155e5c89e4eSSatish Balay   if (!rank) {
156e5c89e4eSSatish Balay     ierr = PetscGetDate(date,64);CHKERRQ(ierr);
157e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");CHKERRQ(ierr);
158e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"Finished at %s\n",date);CHKERRQ(ierr);
159e5c89e4eSSatish Balay     ierr = PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");CHKERRQ(ierr);
160f56c2debSBarry Smith     err  = fflush(*fd);
161e32f2f54SBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
162f56c2debSBarry Smith     err = fclose(*fd);
163e32f2f54SBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
164e5c89e4eSSatish Balay   }
165e5c89e4eSSatish Balay   PetscFunctionReturn(0);
166e5c89e4eSSatish Balay }
167e5c89e4eSSatish Balay 
168e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------*/
169e5c89e4eSSatish Balay 
170e5c89e4eSSatish Balay /*
171e5c89e4eSSatish Balay    This is ugly and probably belongs somewhere else, but I want to
172e5c89e4eSSatish Balay   be able to put a true MPI abort error handler with command line args.
173e5c89e4eSSatish Balay 
174e5c89e4eSSatish Balay     This is so MPI errors in the debugger will leave all the stack
1753c311c98SBarry Smith   frames. The default MP_Abort() cleans up and exits thus providing no useful information
1763c311c98SBarry Smith   in the debugger hence we call abort() instead of MPI_Abort().
177e5c89e4eSSatish Balay */
178e5c89e4eSSatish Balay 
17933c7d699SBarry Smith void Petsc_MPI_AbortOnError(MPI_Comm *comm,PetscMPIInt *flag,...)
180e5c89e4eSSatish Balay {
181e5c89e4eSSatish Balay   PetscFunctionBegin;
1823c311c98SBarry Smith   (*PetscErrorPrintf)("MPI error %d\n",*flag);
183e5c89e4eSSatish Balay   abort();
184e5c89e4eSSatish Balay }
185e5c89e4eSSatish Balay 
18633c7d699SBarry Smith void Petsc_MPI_DebuggerOnError(MPI_Comm *comm,PetscMPIInt *flag,...)
187e5c89e4eSSatish Balay {
188e5c89e4eSSatish Balay   PetscErrorCode ierr;
189e5c89e4eSSatish Balay 
190e5c89e4eSSatish Balay   PetscFunctionBegin;
1913c311c98SBarry Smith   (*PetscErrorPrintf)("MPI error %d\n",*flag);
192e5c89e4eSSatish Balay   ierr = PetscAttachDebugger();
193a297a907SKarl Rupp   if (ierr) MPI_Abort(*comm,*flag); /* hopeless so get out */
194e5c89e4eSSatish Balay }
195e5c89e4eSSatish Balay 
196*7381773fSBarry Smith #if defined(PETSC_HAVE_CUDA)
197*7381773fSBarry Smith /*@C
198*7381773fSBarry Smith      PetscCUDAInitialize - Initializes the CUDA device and cuBLAS on the device
199*7381773fSBarry Smith 
200*7381773fSBarry Smith      Logically collective
201*7381773fSBarry Smith 
202*7381773fSBarry Smith   Input Parameter:
203*7381773fSBarry Smith   comm - the MPI communicator that will utilize the CUDA devices
204*7381773fSBarry Smith 
205*7381773fSBarry Smith   Options Database:
206*7381773fSBarry Smith +  -cuda_initialize - do the initialization in PetscInitialize()
207*7381773fSBarry Smith .  -cuda_view - view information about the CUDA devices
208*7381773fSBarry 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
209*7381773fSBarry Smith -  -cuda_set_device <gpu> - integer number of the device
210*7381773fSBarry Smith 
211*7381773fSBarry Smith   Notes:
212*7381773fSBarry Smith    Initializing cuBLAS takes about 1/2 second there it is done by default in PetscInitialize() before logging begins
213*7381773fSBarry Smith 
214*7381773fSBarry Smith @*/
215*7381773fSBarry Smith PetscErrorCode PetscCUDAInitialize(MPI_Comm comm)
216*7381773fSBarry Smith {
217*7381773fSBarry Smith   PetscErrorCode        ierr;
218*7381773fSBarry Smith   PetscInt              deviceOpt = 0;
219*7381773fSBarry Smith   PetscBool             cuda_view_flag = PETSC_FALSE,flg;
220*7381773fSBarry Smith   struct cudaDeviceProp prop;
221*7381773fSBarry Smith   int                   devCount,device,devicecnt;
222*7381773fSBarry Smith   cudaError_t           err = cudaSuccess;
223*7381773fSBarry Smith   PetscMPIInt           rank,size;
224*7381773fSBarry Smith 
225*7381773fSBarry Smith   PetscFunctionBegin;
226*7381773fSBarry Smith   /*
227*7381773fSBarry Smith      If collecting logging information, by default, wait for GPU to complete its operations
228*7381773fSBarry Smith      before returning to the CPU in order to get accurate timings of each event
229*7381773fSBarry Smith   */
230*7381773fSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-log_summary",&PetscCUDASynchronize);CHKERRQ(ierr);
231*7381773fSBarry Smith   if (!PetscCUDASynchronize) {
232*7381773fSBarry Smith     ierr = PetscOptionsHasName(NULL,NULL,"-log_view",&PetscCUDASynchronize);CHKERRQ(ierr);
233*7381773fSBarry Smith   }
234*7381773fSBarry Smith 
235*7381773fSBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"CUDA options","Sys");CHKERRQ(ierr);
236*7381773fSBarry Smith   ierr = PetscOptionsInt("-cuda_set_device","Set all MPI ranks to use the specified CUDA device",NULL,deviceOpt,&deviceOpt,&flg);CHKERRQ(ierr);
237*7381773fSBarry Smith   device = (int)deviceOpt;
238*7381773fSBarry Smith   ierr = PetscOptionsBool("-cuda_synchronize","Wait for the GPU to complete operations before returning to the CPU",NULL,PetscCUDASynchronize,&PetscCUDASynchronize,NULL);CHKERRQ(ierr);
239*7381773fSBarry Smith   ierr = PetscOptionsDeprecated("-cuda_show_devices","-cuda_view","3.12",NULL);CHKERRQ(ierr);
240*7381773fSBarry Smith   ierr = PetscOptionsName("-cuda_view","Display CUDA device information and assignments",NULL,&cuda_view_flag);CHKERRQ(ierr);
241*7381773fSBarry Smith   ierr = PetscOptionsEnd();CHKERRQ(ierr);
242*7381773fSBarry Smith   if (!PetscCUDAInitialized) {
243*7381773fSBarry Smith     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
244*7381773fSBarry Smith 
245*7381773fSBarry Smith     if (size>1 && !flg) {
246*7381773fSBarry Smith       /* check to see if we force multiple ranks to hit the same GPU */
247*7381773fSBarry Smith       /* we're not using the same GPU on multiple MPI threads. So try to allocated different   GPUs to different processes */
248*7381773fSBarry Smith 
249*7381773fSBarry Smith       /* First get the device count */
250*7381773fSBarry Smith       err   = cudaGetDeviceCount(&devCount);
251*7381773fSBarry Smith       if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceCount %s",cudaGetErrorString(err));
252*7381773fSBarry Smith 
253*7381773fSBarry Smith       /* next determine the rank and then set the device via a mod */
254*7381773fSBarry Smith       ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
255*7381773fSBarry Smith       device = rank % devCount;
256*7381773fSBarry Smith     }
257*7381773fSBarry Smith     err = cudaSetDevice(device);
258*7381773fSBarry Smith     if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDevice %s",cudaGetErrorString(err));
259*7381773fSBarry Smith 
260*7381773fSBarry Smith     /* set the device flags so that it can map host memory */
261*7381773fSBarry Smith     err = cudaSetDeviceFlags(cudaDeviceMapHost);
262*7381773fSBarry Smith     if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDeviceFlags %s",cudaGetErrorString(err));
263*7381773fSBarry Smith 
264*7381773fSBarry Smith     ierr = PetscCUBLASInitializeHandle();CHKERRQ(ierr);
265*7381773fSBarry Smith     PetscCUDAInitialized = PETSC_TRUE;
266*7381773fSBarry Smith   }
267*7381773fSBarry Smith   if (cuda_view_flag) {
268*7381773fSBarry Smith     ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
269*7381773fSBarry Smith     err = cudaGetDeviceCount(&devCount);
270*7381773fSBarry Smith     if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceCount %s",cudaGetErrorString(err));
271*7381773fSBarry Smith     for (devicecnt = 0; devicecnt < devCount; ++devicecnt) {
272*7381773fSBarry Smith       err = cudaGetDeviceProperties(&prop,devicecnt);
273*7381773fSBarry Smith       if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceProperties %s",cudaGetErrorString(err));
274*7381773fSBarry Smith       ierr = PetscPrintf(comm, "CUDA device %d: %s\n", devicecnt, prop.name);CHKERRQ(ierr);
275*7381773fSBarry Smith     }
276*7381773fSBarry Smith     ierr = PetscSynchronizedPrintf(comm,"[%d] Using CUDA device %d.\n",rank,device);CHKERRQ(ierr);
277*7381773fSBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
278*7381773fSBarry Smith   }
279*7381773fSBarry Smith   PetscFunctionReturn(0);
280*7381773fSBarry Smith }
281*7381773fSBarry Smith #endif
282*7381773fSBarry Smith 
283e5c89e4eSSatish Balay /*@C
284e5c89e4eSSatish Balay    PetscEnd - Calls PetscFinalize() and then ends the program. This is useful if one
285e5c89e4eSSatish Balay      wishes a clean exit somewhere deep in the program.
286e5c89e4eSSatish Balay 
287e5c89e4eSSatish Balay    Collective on PETSC_COMM_WORLD
288e5c89e4eSSatish Balay 
289e5c89e4eSSatish Balay    Options Database Keys are the same as for PetscFinalize()
290e5c89e4eSSatish Balay 
291e5c89e4eSSatish Balay    Level: advanced
292e5c89e4eSSatish Balay 
293e5c89e4eSSatish Balay    Note:
294e5c89e4eSSatish Balay    See PetscInitialize() for more general runtime options.
295e5c89e4eSSatish Balay 
29688c29154SBarry Smith .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscFinalize()
297e5c89e4eSSatish Balay @*/
2987087cfbeSBarry Smith PetscErrorCode  PetscEnd(void)
299e5c89e4eSSatish Balay {
300e5c89e4eSSatish Balay   PetscFunctionBegin;
301e5c89e4eSSatish Balay   PetscFinalize();
302e5c89e4eSSatish Balay   exit(0);
303e5c89e4eSSatish Balay   return 0;
304e5c89e4eSSatish Balay }
305e5c89e4eSSatish Balay 
306ace3abfcSBarry Smith PetscBool PetscOptionsPublish = PETSC_FALSE;
30795c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscSetUseTrMalloc_Private(void);
30895c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void);
30995c0884eSLisandro Dalcin PETSC_INTERN PetscBool      petscsetmallocvisited;
310e5c89e4eSSatish Balay static       char           emacsmachinename[256];
311e5c89e4eSSatish Balay 
312e5c89e4eSSatish Balay PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = 0;
313e5c89e4eSSatish Balay PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = 0;
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 
33995c0884eSLisandro Dalcin PETSC_INTERN PetscErrorCode  PetscOptionsCheckInitial_Private(void)
340e5c89e4eSSatish Balay {
341e5c89e4eSSatish Balay   char              string[64],mname[PETSC_MAX_PATH_LEN],*f;
342e5c89e4eSSatish Balay   MPI_Comm          comm = PETSC_COMM_WORLD;
34367584ceeSBarry Smith   PetscBool         flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flag;
344e5c89e4eSSatish Balay   PetscErrorCode    ierr;
34567584ceeSBarry Smith   PetscReal         si;
34628559dc8SJed Brown   PetscInt          intensity;
347e5c89e4eSSatish Balay   int               i;
348e5c89e4eSSatish Balay   PetscMPIInt       rank;
3497ca660e7SBarry Smith   char              version[256],helpoptions[256];
35067584ceeSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
35167584ceeSBarry Smith   PetscReal         logthreshold;
3522479a3a6SBarry Smith #endif
3532479a3a6SBarry Smith #if defined(PETSC_USE_LOG)
3542479a3a6SBarry Smith   PetscViewerFormat format;
35567584ceeSBarry Smith   PetscBool         flg4 = PETSC_FALSE;
35667584ceeSBarry Smith #endif
357012f15ecSRichard Tran Mills #if defined(PETSC_HAVE_CUDA)
358*7381773fSBarry Smith   PetscBool         initCuda = PETSC_TRUE;
359012f15ecSRichard Tran Mills #endif
360*7381773fSBarry Smith 
361e5c89e4eSSatish Balay   PetscFunctionBegin;
36242218b76SBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
363e5c89e4eSSatish Balay 
36467584ceeSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
365e5c89e4eSSatish Balay   /*
366e5c89e4eSSatish Balay       Setup the memory management; support for tracing malloc() usage
367e5c89e4eSSatish Balay   */
368c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-malloc_log",&flg3);CHKERRQ(ierr);
369574034a9SJed Brown   logthreshold = 0.0;
370c5929fdfSBarry Smith   ierr = PetscOptionsGetReal(NULL,NULL,"-malloc_log_threshold",&logthreshold,&flg1);CHKERRQ(ierr);
371574034a9SJed Brown   if (flg1) flg3 = PETSC_TRUE;
3728bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG)
373c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc",&flg1,&flg2);CHKERRQ(ierr);
374e5c89e4eSSatish Balay   if ((!flg2 || flg1) && !petscsetmallocvisited) {
375dc92acbaSJed Brown     if (flg2 || !(PETSC_RUNNING_ON_VALGRIND)) {
376555d055bSBarry Smith       /* turn off default -malloc if valgrind is being used */
377e5c89e4eSSatish Balay       ierr = PetscSetUseTrMalloc_Private();CHKERRQ(ierr);
378555d055bSBarry Smith     }
379e5c89e4eSSatish Balay   }
380e5c89e4eSSatish Balay #else
381c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_dump",&flg1,NULL);CHKERRQ(ierr);
382c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc",&flg2,NULL);CHKERRQ(ierr);
383e5c89e4eSSatish Balay   if (flg1 || flg2 || flg3) {ierr = PetscSetUseTrMalloc_Private();CHKERRQ(ierr);}
384e5c89e4eSSatish Balay #endif
385e5c89e4eSSatish Balay   if (flg3) {
386574034a9SJed Brown     ierr = PetscMallocSetDumpLogThreshold((PetscLogDouble)logthreshold);CHKERRQ(ierr);
387e5c89e4eSSatish Balay   }
388ba282f50SJed Brown   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_coalesce",&flg1,&flg2);CHKERRQ(ierr);
389ba282f50SJed Brown   if (flg2) {ierr = PetscMallocSetCoalesce(flg1);CHKERRQ(ierr);}
39090d69ab7SBarry Smith   flg1 = PETSC_FALSE;
391c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_debug",&flg1,NULL);CHKERRQ(ierr);
392e5c89e4eSSatish Balay   if (flg1) {
393e5c89e4eSSatish Balay     ierr = PetscSetUseTrMalloc_Private();CHKERRQ(ierr);
394e5c89e4eSSatish Balay     ierr = PetscMallocDebug(PETSC_TRUE);CHKERRQ(ierr);
395e5c89e4eSSatish Balay   }
396dc92acbaSJed Brown   flg1 = PETSC_FALSE;
397c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_test",&flg1,NULL);CHKERRQ(ierr);
3988bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG)
399dc92acbaSJed Brown   if (flg1 && !PETSC_RUNNING_ON_VALGRIND) {
400dc92acbaSJed Brown     ierr = PetscSetUseTrMalloc_Private();CHKERRQ(ierr);
401dc92acbaSJed Brown     ierr = PetscMallocSetDumpLog();CHKERRQ(ierr);
402dc92acbaSJed Brown     ierr = PetscMallocDebug(PETSC_TRUE);CHKERRQ(ierr);
403dc92acbaSJed Brown   }
404dc92acbaSJed Brown #endif
4052f21b5c6SHong Zhang   flg1 = PETSC_FALSE;
4062f21b5c6SHong Zhang   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_hbw",&flg1,NULL);CHKERRQ(ierr);
407cf6f3811SHong Zhang   /* ignore this option if malloc is already set */
408cf6f3811SHong Zhang   if (flg1 && !petscsetmallocvisited) {ierr = PetscSetUseHBWMalloc_Private();CHKERRQ(ierr);}
409e5c89e4eSSatish Balay 
41090d69ab7SBarry Smith   flg1 = PETSC_FALSE;
411c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-malloc_info",&flg1,NULL);CHKERRQ(ierr);
4127783f70dSSatish Balay   if (!flg1) {
41390d69ab7SBarry Smith     flg1 = PETSC_FALSE;
414c5929fdfSBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-memory_view",&flg1,NULL);CHKERRQ(ierr);
4157783f70dSSatish Balay   }
416e5c89e4eSSatish Balay   if (flg1) {
417e5c89e4eSSatish Balay     ierr = PetscMemorySetGetMaximumUsage();CHKERRQ(ierr);
418e5c89e4eSSatish Balay   }
41967584ceeSBarry Smith #endif
420e5c89e4eSSatish Balay 
421b4427426SBarry Smith #if defined(PETSC_USE_LOG)
422c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-objects_dump",&PetscObjectsLog);CHKERRQ(ierr);
423b4427426SBarry Smith #endif
42405df10baSBarry Smith 
425e5c89e4eSSatish Balay   /*
426e5c89e4eSSatish Balay       Set the display variable for graphics
427e5c89e4eSSatish Balay   */
428e5c89e4eSSatish Balay   ierr = PetscSetDisplay();CHKERRQ(ierr);
429e5c89e4eSSatish Balay 
43051dcc849SKerry Stevens   /*
431e5c89e4eSSatish Balay       Print the PETSc version information
432e5c89e4eSSatish Balay   */
433c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-v",&flg1);CHKERRQ(ierr);
434c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-version",&flg2);CHKERRQ(ierr);
4352d747510SLisandro Dalcin   ierr = PetscOptionsHasHelp(NULL,&flg3);CHKERRQ(ierr);
436e5c89e4eSSatish Balay   if (flg1 || flg2 || flg3) {
437e5c89e4eSSatish Balay 
438e5c89e4eSSatish Balay     /*
439e5c89e4eSSatish Balay        Print "higher-level" package version message
440e5c89e4eSSatish Balay     */
441e5c89e4eSSatish Balay     if (PetscExternalVersionFunction) {
442e5c89e4eSSatish Balay       ierr = (*PetscExternalVersionFunction)(comm);CHKERRQ(ierr);
443e5c89e4eSSatish Balay     }
444e5c89e4eSSatish Balay 
445a523d312SBarry Smith     ierr = PetscGetVersion(version,256);CHKERRQ(ierr);
4467ca660e7SBarry Smith     ierr = (*PetscHelpPrintf)(comm,"--------------------------------------------------------------------------\n");CHKERRQ(ierr);
447e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"%s\n",version);CHKERRQ(ierr);
448e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO);CHKERRQ(ierr);
449e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n");CHKERRQ(ierr);
45084e42920SBarry Smith     ierr = (*PetscHelpPrintf)(comm,"See docs/faq.html for problems.\n");CHKERRQ(ierr);
451e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n");CHKERRQ(ierr);
452e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR);CHKERRQ(ierr);
4537ca660e7SBarry Smith     ierr = (*PetscHelpPrintf)(comm,"--------------------------------------------------------------------------\n");CHKERRQ(ierr);
4547ca660e7SBarry Smith   }
4557ca660e7SBarry Smith 
45694941ca7SBarry Smith   /*
45794941ca7SBarry Smith        Print "higher-level" package help message
45894941ca7SBarry Smith   */
45994941ca7SBarry Smith   if (flg3) {
46094941ca7SBarry Smith     if (PetscExternalHelpFunction) {
46194941ca7SBarry Smith       ierr = (*PetscExternalHelpFunction)(comm);CHKERRQ(ierr);
46294941ca7SBarry Smith     }
46394941ca7SBarry Smith   }
46494941ca7SBarry Smith 
4657ca660e7SBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-help",helpoptions,sizeof(helpoptions),&flg1);CHKERRQ(ierr);
4667ca660e7SBarry Smith   if (flg1) {
4677ca660e7SBarry Smith     ierr = PetscStrcmp(helpoptions,"intro",&flg2);CHKERRQ(ierr);
4687ca660e7SBarry Smith     if (flg2) {
469bdb346e9SBarry Smith       ierr = PetscOptionsDestroyDefault();CHKERRQ(ierr);
470008a6e76SBarry Smith       ierr = PetscFreeMPIResources();CHKERRQ(ierr);
4717ca660e7SBarry Smith       ierr = MPI_Finalize();CHKERRQ(ierr);
4727ca660e7SBarry Smith       exit(0);
4737ca660e7SBarry Smith     }
474e5c89e4eSSatish Balay   }
475e5c89e4eSSatish Balay 
476e5c89e4eSSatish Balay   /*
477e5c89e4eSSatish Balay       Setup the error handling
478e5c89e4eSSatish Balay   */
47990d69ab7SBarry Smith   flg1 = PETSC_FALSE;
480c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-on_error_abort",&flg1,NULL);CHKERRQ(ierr);
481b59baad1SJed Brown   if (flg1) {
48242218b76SBarry Smith     ierr = MPI_Comm_set_errhandler(comm,MPI_ERRORS_ARE_FATAL);CHKERRQ(ierr);
483b59baad1SJed Brown     ierr = PetscPushErrorHandler(PetscAbortErrorHandler,0);CHKERRQ(ierr);
484b59baad1SJed Brown   }
48590d69ab7SBarry Smith   flg1 = PETSC_FALSE;
486c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-on_error_mpiabort",&flg1,NULL);CHKERRQ(ierr);
487cb9801acSJed Brown   if (flg1) { ierr = PetscPushErrorHandler(PetscMPIAbortErrorHandler,0);CHKERRQ(ierr);}
48890d69ab7SBarry Smith   flg1 = PETSC_FALSE;
489c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-mpi_return_on_error",&flg1,NULL);CHKERRQ(ierr);
490e5c89e4eSSatish Balay   if (flg1) {
491d54338ecSKarl Rupp     ierr = MPI_Comm_set_errhandler(comm,MPI_ERRORS_RETURN);CHKERRQ(ierr);
492e5c89e4eSSatish Balay   }
49390d69ab7SBarry Smith   flg1 = PETSC_FALSE;
494c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-no_signal_handler",&flg1,NULL);CHKERRQ(ierr);
4958d359177SBarry Smith   if (!flg1) {ierr = PetscPushSignalHandler(PetscSignalHandlerDefault,(void*)0);CHKERRQ(ierr);}
49696cc47afSJed Brown   flg1 = PETSC_FALSE;
497c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-fp_trap",&flg1,NULL);CHKERRQ(ierr);
49896cc47afSJed Brown   if (flg1) {ierr = PetscSetFPTrap(PETSC_FP_TRAP_ON);CHKERRQ(ierr);}
499c5929fdfSBarry Smith   ierr = PetscOptionsGetInt(NULL,NULL,"-check_pointer_intensity",&intensity,&flag);CHKERRQ(ierr);
50028559dc8SJed Brown   if (flag) {ierr = PetscCheckPointerSetIntensity(intensity);CHKERRQ(ierr);}
501e5c89e4eSSatish Balay 
502e5c89e4eSSatish Balay   /*
503e5c89e4eSSatish Balay       Setup debugger information
504e5c89e4eSSatish Balay   */
505e5c89e4eSSatish Balay   ierr = PetscSetDefaultDebugger();CHKERRQ(ierr);
506c5929fdfSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-on_error_attach_debugger",string,64,&flg1);CHKERRQ(ierr);
507e5c89e4eSSatish Balay   if (flg1) {
508e5c89e4eSSatish Balay     MPI_Errhandler err_handler;
509e5c89e4eSSatish Balay 
510e5c89e4eSSatish Balay     ierr = PetscSetDebuggerFromString(string);CHKERRQ(ierr);
51133c7d699SBarry Smith     ierr = MPI_Comm_create_errhandler(Petsc_MPI_DebuggerOnError,&err_handler);CHKERRQ(ierr);
512d54338ecSKarl Rupp     ierr = MPI_Comm_set_errhandler(comm,err_handler);CHKERRQ(ierr);
513e5c89e4eSSatish Balay     ierr = PetscPushErrorHandler(PetscAttachDebuggerErrorHandler,0);CHKERRQ(ierr);
514e5c89e4eSSatish Balay   }
515c5929fdfSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-debug_terminal",string,64,&flg1);CHKERRQ(ierr);
5165e96ac45SJed Brown   if (flg1) { ierr = PetscSetDebugTerminal(string);CHKERRQ(ierr); }
517c5929fdfSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-start_in_debugger",string,64,&flg1);CHKERRQ(ierr);
518c5929fdfSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-stop_for_debugger",string,64,&flg2);CHKERRQ(ierr);
519e5c89e4eSSatish Balay   if (flg1 || flg2) {
520e5c89e4eSSatish Balay     PetscMPIInt    size;
521e5c89e4eSSatish Balay     PetscInt       lsize,*nodes;
522e5c89e4eSSatish Balay     MPI_Errhandler err_handler;
523e5c89e4eSSatish Balay     /*
524e5c89e4eSSatish Balay        we have to make sure that all processors have opened
525e5c89e4eSSatish Balay        connections to all other processors, otherwise once the
526e5c89e4eSSatish Balay        debugger has stated it is likely to receive a SIGUSR1
527e5c89e4eSSatish Balay        and kill the program.
528e5c89e4eSSatish Balay     */
52942218b76SBarry Smith     ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
530e5c89e4eSSatish Balay     if (size > 2) {
531533163c2SBarry Smith       PetscMPIInt dummy = 0;
532e5c89e4eSSatish Balay       MPI_Status  status;
533e5c89e4eSSatish Balay       for (i=0; i<size; i++) {
534e5c89e4eSSatish Balay         if (rank != i) {
53542218b76SBarry Smith           ierr = MPI_Send(&dummy,1,MPI_INT,i,109,comm);CHKERRQ(ierr);
536e5c89e4eSSatish Balay         }
537e5c89e4eSSatish Balay       }
538e5c89e4eSSatish Balay       for (i=0; i<size; i++) {
539e5c89e4eSSatish Balay         if (rank != i) {
54042218b76SBarry Smith           ierr = MPI_Recv(&dummy,1,MPI_INT,i,109,comm,&status);CHKERRQ(ierr);
541e5c89e4eSSatish Balay         }
542e5c89e4eSSatish Balay       }
543e5c89e4eSSatish Balay     }
544e5c89e4eSSatish Balay     /* check if this processor node should be in debugger */
545785e854fSJed Brown     ierr  = PetscMalloc1(size,&nodes);CHKERRQ(ierr);
546e5c89e4eSSatish Balay     lsize = size;
547c5929fdfSBarry Smith     ierr  = PetscOptionsGetIntArray(NULL,NULL,"-debugger_nodes",nodes,&lsize,&flag);CHKERRQ(ierr);
548e5c89e4eSSatish Balay     if (flag) {
549e5c89e4eSSatish Balay       for (i=0; i<lsize; i++) {
550e5c89e4eSSatish Balay         if (nodes[i] == rank) { flag = PETSC_FALSE; break; }
551e5c89e4eSSatish Balay       }
552e5c89e4eSSatish Balay     }
553e5c89e4eSSatish Balay     if (!flag) {
554e5c89e4eSSatish Balay       ierr = PetscSetDebuggerFromString(string);CHKERRQ(ierr);
555e5c89e4eSSatish Balay       ierr = PetscPushErrorHandler(PetscAbortErrorHandler,0);CHKERRQ(ierr);
556e5c89e4eSSatish Balay       if (flg1) {
557e5c89e4eSSatish Balay         ierr = PetscAttachDebugger();CHKERRQ(ierr);
558e5c89e4eSSatish Balay       } else {
559e5c89e4eSSatish Balay         ierr = PetscStopForDebugger();CHKERRQ(ierr);
560e5c89e4eSSatish Balay       }
56133c7d699SBarry Smith       ierr = MPI_Comm_create_errhandler(Petsc_MPI_AbortOnError,&err_handler);CHKERRQ(ierr);
562d54338ecSKarl Rupp       ierr = MPI_Comm_set_errhandler(comm,err_handler);CHKERRQ(ierr);
563e5c89e4eSSatish Balay     }
564e5c89e4eSSatish Balay     ierr = PetscFree(nodes);CHKERRQ(ierr);
565e5c89e4eSSatish Balay   }
566e5c89e4eSSatish Balay 
567c5929fdfSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-on_error_emacs",emacsmachinename,128,&flg1);CHKERRQ(ierr);
568cb9801acSJed Brown   if (flg1 && !rank) {ierr = PetscPushErrorHandler(PetscEmacsClientErrorHandler,emacsmachinename);CHKERRQ(ierr);}
569e5c89e4eSSatish Balay 
570e5c89e4eSSatish Balay   /*
571e5c89e4eSSatish Balay         Setup profiling and logging
572e5c89e4eSSatish Balay   */
5736cf91177SBarry Smith #if defined(PETSC_USE_INFO)
5748bb29257SSatish Balay   {
575e5c89e4eSSatish Balay     char logname[PETSC_MAX_PATH_LEN]; logname[0] = 0;
576c5929fdfSBarry Smith     ierr = PetscOptionsGetString(NULL,NULL,"-info",logname,250,&flg1);CHKERRQ(ierr);
5778bb29257SSatish Balay     if (flg1 && logname[0]) {
578fcc2139eSBarry Smith       ierr = PetscInfoAllow(PETSC_TRUE,logname);CHKERRQ(ierr);
5798bb29257SSatish Balay     } else if (flg1) {
5800298fd71SBarry Smith       ierr = PetscInfoAllow(PETSC_TRUE,NULL);CHKERRQ(ierr);
581e5c89e4eSSatish Balay     }
582e5c89e4eSSatish Balay   }
583865f6aa8SSatish Balay #endif
584865f6aa8SSatish Balay #if defined(PETSC_USE_LOG)
585865f6aa8SSatish Balay   mname[0] = 0;
586c5929fdfSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-history",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
587865f6aa8SSatish Balay   if (flg1) {
588865f6aa8SSatish Balay     if (mname[0]) {
589f3dea69dSBarry Smith       ierr = PetscOpenHistoryFile(mname,&petsc_history);CHKERRQ(ierr);
590865f6aa8SSatish Balay     } else {
591706d7a88SBarry Smith       ierr = PetscOpenHistoryFile(NULL,&petsc_history);CHKERRQ(ierr);
592865f6aa8SSatish Balay     }
593865f6aa8SSatish Balay   }
594217044c2SLisandro Dalcin 
595217044c2SLisandro Dalcin   ierr = PetscOptionsGetBool(NULL,NULL,"-log_sync",&PetscLogSyncOn,NULL);CHKERRQ(ierr);
596217044c2SLisandro Dalcin 
597e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE)
59890d69ab7SBarry Smith   flg1 = PETSC_FALSE;
599c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-log_mpe",&flg1);CHKERRQ(ierr);
600495fc317SBarry Smith   if (flg1) {ierr = PetscLogMPEBegin();CHKERRQ(ierr);}
601e5c89e4eSSatish Balay #endif
60290d69ab7SBarry Smith   flg1 = PETSC_FALSE;
60390d69ab7SBarry Smith   flg3 = PETSC_FALSE;
604c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-log_all",&flg1,NULL);CHKERRQ(ierr);
605c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);CHKERRQ(ierr);
606e5c89e4eSSatish Balay   if (flg1)                      { ierr = PetscLogAllBegin();CHKERRQ(ierr); }
607bb1d7374SBarry Smith   else if (flg3)                 { ierr = PetscLogDefaultBegin();CHKERRQ(ierr);}
608e5c89e4eSSatish Balay 
609c5929fdfSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-log_trace",mname,250,&flg1);CHKERRQ(ierr);
610e5c89e4eSSatish Balay   if (flg1) {
611e5c89e4eSSatish Balay     char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN];
612e5c89e4eSSatish Balay     FILE *file;
613e5c89e4eSSatish Balay     if (mname[0]) {
6142e924ca5SSatish Balay       PetscSNPrintf(name,PETSC_MAX_PATH_LEN,"%s.%d",mname,rank);
615e5c89e4eSSatish Balay       ierr = PetscFixFilename(name,fname);CHKERRQ(ierr);
616e5c89e4eSSatish Balay       file = fopen(fname,"w");
617f3dea69dSBarry Smith       if (!file) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname);
618a297a907SKarl Rupp     } else file = PETSC_STDOUT;
619e5c89e4eSSatish Balay     ierr = PetscLogTraceBegin(file);CHKERRQ(ierr);
620e5c89e4eSSatish Balay   }
621bb1d7374SBarry Smith 
62216413a6aSBarry Smith   ierr = PetscOptionsGetViewer(comm,NULL,NULL,"-log_view",NULL,&format,&flg4);CHKERRQ(ierr);
623bb1d7374SBarry Smith   if (flg4) {
624bb1d7374SBarry Smith     if (format == PETSC_VIEWER_ASCII_XML) {
625bb1d7374SBarry Smith       ierr = PetscLogNestedBegin();CHKERRQ(ierr);
626bb1d7374SBarry Smith     } else {
627bb1d7374SBarry Smith       ierr = PetscLogDefaultBegin();CHKERRQ(ierr);
628bb1d7374SBarry Smith     }
629e3ed9ee7SBarry Smith     PetscLogMemory = PETSC_FALSE;
630e3ed9ee7SBarry Smith     ierr = PetscOptionsGetBool(NULL,NULL,"-log_view_memory",&PetscLogMemory,NULL);CHKERRQ(ierr);
631e3ed9ee7SBarry Smith     if (PetscLogMemory) {
632e3ed9ee7SBarry Smith       ierr = PetscSetUseTrMalloc_Private();CHKERRQ(ierr);
633e3ed9ee7SBarry Smith     }
634eccbb886SLisandro Dalcin   }
635eccbb886SLisandro Dalcin   if (flg4 && format == PETSC_VIEWER_ASCII_XML) {
636eccbb886SLisandro Dalcin     PetscReal threshold = PetscRealConstant(0.01);
637eccbb886SLisandro Dalcin     ierr = PetscOptionsGetReal(NULL,NULL,"-log_threshold",&threshold,&flg1);CHKERRQ(ierr);
638eccbb886SLisandro Dalcin     if (flg1) {ierr = PetscLogSetThreshold((PetscLogDouble)threshold,NULL);CHKERRQ(ierr);}
639bb1d7374SBarry Smith   }
640e5c89e4eSSatish Balay #endif
641e5c89e4eSSatish Balay 
642c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-saws_options",&PetscOptionsPublish,NULL);CHKERRQ(ierr);
643e5c89e4eSSatish Balay 
6444b0a73b2SBarry Smith #if defined(PETSC_HAVE_CUDA)
645*7381773fSBarry Smith   ierr = PetscOptionsBegin(comm,NULL,"CUDA initialize","Sys");CHKERRQ(ierr);
646*7381773fSBarry Smith   ierr = PetscOptionsBool("-cuda_initialize","Initialize the CUDA devices and cuBLAS during PetscInitialize()",NULL,initCuda,&initCuda,NULL);CHKERRQ(ierr);
647012f15ecSRichard Tran Mills   ierr = PetscOptionsEnd();CHKERRQ(ierr);
648*7381773fSBarry Smith   if (initCuda) {ierr = PetscCUDAInitialize(PETSC_COMM_WORLD);CHKERRQ(ierr);}
6494b0a73b2SBarry Smith #endif
6504b0a73b2SBarry Smith 
651e5c89e4eSSatish Balay   /*
652e5c89e4eSSatish Balay        Print basic help message
653e5c89e4eSSatish Balay   */
6542d747510SLisandro Dalcin   ierr = PetscOptionsHasHelp(NULL,&flg1);CHKERRQ(ierr);
655e5c89e4eSSatish Balay   if (flg1) {
656e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"Options for all PETSc programs:\n");CHKERRQ(ierr);
657301d30feSBarry Smith     ierr = (*PetscHelpPrintf)(comm," -help: prints help method for each option\n");CHKERRQ(ierr);
658301d30feSBarry Smith     ierr = (*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is detected. Useful \n ");CHKERRQ(ierr);
659301d30feSBarry Smith     ierr = (*PetscHelpPrintf)(comm,"       only when run in the debugger\n");CHKERRQ(ierr);
660e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");CHKERRQ(ierr);
661e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"       start the debugger in new xterm\n");CHKERRQ(ierr);
662e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"       unless noxterm is given\n");CHKERRQ(ierr);
663e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");CHKERRQ(ierr);
664e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"       start all processes in the debugger\n");CHKERRQ(ierr);
665e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n");CHKERRQ(ierr);
666e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"    emacs jumps to error file\n");CHKERRQ(ierr);
667e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -debugger_nodes [n1,n2,..] Nodes to start in debugger\n");CHKERRQ(ierr);
668e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n");CHKERRQ(ierr);
669e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n");CHKERRQ(ierr);
670e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"                      waits the delay for you to attach\n");CHKERRQ(ierr);
6711cda70a7SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -display display: Location where X window graphics and debuggers are displayed\n");CHKERRQ(ierr);
672e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n");CHKERRQ(ierr);
673e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n");CHKERRQ(ierr);
674e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n");CHKERRQ(ierr);
675e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"           note on IBM RS6000 this slows run greatly\n");CHKERRQ(ierr);
676e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n");CHKERRQ(ierr);
677e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -malloc: use our error checking malloc\n");CHKERRQ(ierr);
678e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -malloc no: don't use error checking malloc\n");CHKERRQ(ierr);
6794161f2a3SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -malloc_info: prints total memory usage\n");CHKERRQ(ierr);
6804161f2a3SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -malloc_log: keeps log of all memory allocations\n");CHKERRQ(ierr);
681e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -malloc_debug: enables extended checking for memory corruption\n");CHKERRQ(ierr);
68226a7e8d4SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -options_view: dump list of options inputted\n");CHKERRQ(ierr);
683e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n");CHKERRQ(ierr);
684e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n");CHKERRQ(ierr);
685e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n");CHKERRQ(ierr);
686e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n");CHKERRQ(ierr);
687a8c7a070SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has separate tmp directory\n");CHKERRQ(ierr);
6880841954dSBarry Smith     ierr = (*PetscHelpPrintf)(comm," -memory_view: print memory usage at end of run\n");CHKERRQ(ierr);
689e5c89e4eSSatish Balay #if defined(PETSC_USE_LOG)
690e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n");CHKERRQ(ierr);
691185ae32fSMatthew G. Knepley     ierr = (*PetscHelpPrintf)(comm," -log_view [:filename:[format]]: logging objects and events\n");CHKERRQ(ierr);
692e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n");CHKERRQ(ierr);
693e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MPE)
694495fc317SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through Jumpshot\n");CHKERRQ(ierr);
695e5c89e4eSSatish Balay #endif
6966cf91177SBarry Smith     ierr = (*PetscHelpPrintf)(comm," -info <optional filename>: print informative messages about the calculations\n");CHKERRQ(ierr);
697e5c89e4eSSatish Balay #endif
698e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -v: prints PETSc version number and release date\n");CHKERRQ(ierr);
699e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n");CHKERRQ(ierr);
700e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n");CHKERRQ(ierr);
701e5c89e4eSSatish Balay     ierr = (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");CHKERRQ(ierr);
702e5c89e4eSSatish Balay   }
703e5c89e4eSSatish Balay 
70474ba8654SBarry Smith #if defined(PETSC_HAVE_POPEN)
70574ba8654SBarry Smith   {
70674ba8654SBarry Smith   char machine[128];
707c5929fdfSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-popen_machine",machine,128,&flg1);CHKERRQ(ierr);
70874ba8654SBarry Smith   if (flg1) {
70974ba8654SBarry Smith     ierr = PetscPOpenSetMachine(machine);CHKERRQ(ierr);
71074ba8654SBarry Smith   }
71174ba8654SBarry Smith   }
71274ba8654SBarry Smith #endif
71374ba8654SBarry Smith 
714c5929fdfSBarry Smith   ierr = PetscOptionsGetReal(NULL,NULL,"-petsc_sleep",&si,&flg1);CHKERRQ(ierr);
715e5c89e4eSSatish Balay   if (flg1) {
716e5c89e4eSSatish Balay     ierr = PetscSleep(si);CHKERRQ(ierr);
717e5c89e4eSSatish Balay   }
718e5c89e4eSSatish Balay 
719c5929fdfSBarry Smith   ierr = PetscOptionsGetString(NULL,NULL,"-info_exclude",mname,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr);
7202ea1be5dSBarry Smith   if (flg1) {
721e5c89e4eSSatish Balay     ierr = PetscStrstr(mname,"null",&f);CHKERRQ(ierr);
722e5c89e4eSSatish Balay     if (f) {
723ecd8bba6SJed Brown       ierr = PetscInfoDeactivateClass(0);CHKERRQ(ierr);
724e5c89e4eSSatish Balay     }
7252ea1be5dSBarry Smith   }
726827f890bSBarry Smith 
727fdc842d1SBarry Smith #if defined(PETSC_HAVE_VIENNACL)
728c5929fdfSBarry Smith   ierr = PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);CHKERRQ(ierr);
729f14045dbSBarry Smith   if (!flg3) {
730c5929fdfSBarry Smith     ierr = PetscOptionsHasName(NULL,NULL,"-log_view",&flg3);CHKERRQ(ierr);
731f14045dbSBarry Smith   }
732c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(NULL,NULL,"-viennacl_synchronize",&flg3,NULL);CHKERRQ(ierr);
733f14045dbSBarry Smith   PetscViennaCLSynchronize = flg3;
734fdc842d1SBarry Smith   ierr = PetscViennaCLInit();CHKERRQ(ierr);
7354cf1874eSKarl Rupp #endif
73682f73ecaSAlejandro Lamas Daviña 
737fdc842d1SBarry Smith   /*
738fdc842d1SBarry Smith      Creates the logging data structures; this is enabled even if logging is not turned on
739fdc842d1SBarry Smith      This is the last thing we do before returning to the user code to prevent having the
740fdc842d1SBarry Smith      logging numbers contaminated by any startup time associated with MPI and the GPUs
741fdc842d1SBarry Smith   */
742fdc842d1SBarry Smith #if defined(PETSC_USE_LOG)
743fdc842d1SBarry Smith   ierr = PetscLogInitialize();CHKERRQ(ierr);
744f0a7718cSKarl Rupp #endif
745f0a7718cSKarl Rupp 
746e5c89e4eSSatish Balay   PetscFunctionReturn(0);
747e5c89e4eSSatish Balay }
748