Actual source code: init.c

petsc-3.12.2 2019-11-22
Report Typos and Errors
  1: /*

  3:    This file defines part of the initialization of PETSc

  5:   This file uses regular malloc and free because it cannot known
  6:   what malloc is being used until it has already processed the input.
  7: */

  9:  #include <petscsys.h>
 10:  #include <petsc/private/petscimpl.h>
 11:  #include <petscvalgrind.h>
 12:  #include <petscviewer.h>
 13: #if defined(PETSC_USE_LOG)
 14: PETSC_INTERN PetscErrorCode PetscLogInitialize(void);
 15: #endif

 17: #if defined(PETSC_HAVE_SYS_SYSINFO_H)
 18: #include <sys/sysinfo.h>
 19: #endif
 20: #if defined(PETSC_HAVE_UNISTD_H)
 21: #include <unistd.h>
 22: #endif
 23: #if defined(PETSC_HAVE_CUDA)
 24: #include <cuda_runtime.h>
 25:  #include <petsccublas.h>
 26: #endif

 28: #if defined(PETSC_HAVE_VIENNACL)
 29: PETSC_EXTERN PetscErrorCode PetscViennaCLInit();
 30: #endif

 32: /* ------------------------Nasty global variables -------------------------------*/
 33: /*
 34:      Indicates if PETSc started up MPI, or it was
 35:    already started before PETSc was initialized.
 36: */
 37: PetscBool   PetscBeganMPI         = PETSC_FALSE;
 38: PetscBool   PetscInitializeCalled = PETSC_FALSE;
 39: PetscBool   PetscFinalizeCalled   = PETSC_FALSE;
 40: PetscBool   PetscCUDAInitialized  = PETSC_FALSE;

 42: PetscMPIInt PetscGlobalRank       = -1;
 43: PetscMPIInt PetscGlobalSize       = -1;

 45: #if defined(PETSC_HAVE_COMPLEX)
 46: #if defined(PETSC_COMPLEX_INSTANTIATE)
 47: template <> class std::complex<double>; /* instantiate complex template class */
 48: #endif
 49: #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
 50: MPI_Datatype MPIU_C_DOUBLE_COMPLEX;
 51: MPI_Datatype MPIU_C_COMPLEX;
 52: #endif

 54: /*MC
 55:    PETSC_i - the imaginary number i

 57:    Synopsis:
 58:    #include <petscsys.h>
 59:    PetscComplex PETSC_i;

 61:    Level: beginner

 63:    Note:
 64:    Complex numbers are automatically available if PETSc located a working complex implementation

 66: .seealso: PetscRealPart(), PetscImaginaryPart(), PetscRealPartComplex(), PetscImaginaryPartComplex()
 67: M*/
 68: PetscComplex PETSC_i;
 69: #endif
 70: #if defined(PETSC_USE_REAL___FLOAT128)
 71: MPI_Datatype MPIU___FLOAT128 = 0;
 72: #if defined(PETSC_HAVE_COMPLEX)
 73: MPI_Datatype MPIU___COMPLEX128 = 0;
 74: #endif
 75: #elif defined(PETSC_USE_REAL___FP16)
 76: MPI_Datatype MPIU___FP16 = 0;
 77: #endif
 78: MPI_Datatype MPIU_2SCALAR = 0;
 79: #if defined(PETSC_USE_64BIT_INDICES)
 80: MPI_Datatype MPIU_2INT = 0;
 81: #endif
 82: MPI_Datatype MPIU_BOOL;
 83: MPI_Datatype MPIU_ENUM;
 84: MPI_Datatype MPIU_FORTRANADDR;
 85: MPI_Datatype MPIU_SIZE_T;

 87: /*
 88:        Function that is called to display all error messages
 89: */
 90: PetscErrorCode (*PetscErrorPrintf)(const char [],...)          = PetscErrorPrintfDefault;
 91: PetscErrorCode (*PetscHelpPrintf)(MPI_Comm,const char [],...)  = PetscHelpPrintfDefault;
 92: PetscErrorCode (*PetscVFPrintf)(FILE*,const char[],va_list)    = PetscVFPrintfDefault;
 93: /*
 94:   This is needed to turn on/off GPU synchronization
 95: */
 96: PetscBool PetscViennaCLSynchronize = PETSC_FALSE;
 97: PetscBool PetscCUDASynchronize = PETSC_FALSE;

 99: /* ------------------------------------------------------------------------------*/
100: /*
101:    Optional file where all PETSc output from various prints is saved
102: */
103: PETSC_INTERN FILE *petsc_history;
104: FILE *petsc_history = NULL;

106: PetscErrorCode  PetscOpenHistoryFile(const char filename[],FILE **fd)
107: {
109:   PetscMPIInt    rank,size;
110:   char           pfile[PETSC_MAX_PATH_LEN],pname[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN],date[64];
111:   char           version[256];

114:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
115:   if (!rank) {
116:     char        arch[10];
117:     int         err;

119:     PetscGetArchType(arch,10);
120:     PetscGetDate(date,64);
121:     PetscGetVersion(version,256);
122:     MPI_Comm_size(PETSC_COMM_WORLD,&size);
123:     if (filename) {
124:       PetscFixFilename(filename,fname);
125:     } else {
126:       PetscGetHomeDirectory(pfile,240);
127:       PetscStrcat(pfile,"/.petschistory");
128:       PetscFixFilename(pfile,fname);
129:     }

131:     *fd = fopen(fname,"a");
132:     if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file: %s",fname);

134:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
135:     PetscFPrintf(PETSC_COMM_SELF,*fd,"%s %s\n",version,date);
136:     PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);
137:     PetscFPrintf(PETSC_COMM_SELF,*fd,"%s on a %s, %d proc. with options:\n",pname,arch,size);
138:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");

140:     err = fflush(*fd);
141:     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
142:   }
143:   return(0);
144: }

146: PETSC_INTERN PetscErrorCode PetscCloseHistoryFile(FILE **fd)
147: {
149:   PetscMPIInt    rank;
150:   char           date[64];
151:   int            err;

154:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
155:   if (!rank) {
156:     PetscGetDate(date,64);
157:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
158:     PetscFPrintf(PETSC_COMM_SELF,*fd,"Finished at %s\n",date);
159:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
160:     err  = fflush(*fd);
161:     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
162:     err = fclose(*fd);
163:     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
164:   }
165:   return(0);
166: }

168: /* ------------------------------------------------------------------------------*/

170: /*
171:    This is ugly and probably belongs somewhere else, but I want to
172:   be able to put a true MPI abort error handler with command line args.

174:     This is so MPI errors in the debugger will leave all the stack
175:   frames. The default MP_Abort() cleans up and exits thus providing no useful information
176:   in the debugger hence we call abort() instead of MPI_Abort().
177: */

179: void Petsc_MPI_AbortOnError(MPI_Comm *comm,PetscMPIInt *flag,...)
180: {
182:   (*PetscErrorPrintf)("MPI error %d\n",*flag);
183:   abort();
184: }

186: void Petsc_MPI_DebuggerOnError(MPI_Comm *comm,PetscMPIInt *flag,...)
187: {

191:   (*PetscErrorPrintf)("MPI error %d\n",*flag);
192:   PetscAttachDebugger();
193:   if (ierr) MPI_Abort(*comm,*flag); /* hopeless so get out */
194: }

196: #if defined(PETSC_HAVE_CUDA)
197: /*@C
198:      PetscCUDAInitialize - Initializes the CUDA device and cuBLAS on the device

200:      Logically collective

202:   Input Parameter:
203:   comm - the MPI communicator that will utilize the CUDA devices

205:   Options Database:
206: +  -cuda_initialize <default yes,no> - do the initialization in PetscInitialize(). If -cuda_initialize no is used then the default initialization is done automatically
207:                                when the first CUDA call is made unless you call PetscCUDAInitialize() before any CUDA operations are performed
208: .  -cuda_view - view information about the CUDA devices
209: .  -cuda_synchronize - wait at the end of asynchronize CUDA calls so that their time gets credited to the current event; default with -log_view
210: -  -cuda_set_device <gpu> - integer number of the device

212:   Level: beginner

214:   Notes:
215:    Initializing cuBLAS takes about 1/2 second there it is done by default in PetscInitialize() before logging begins

217: @*/
218: PetscErrorCode PetscCUDAInitialize(MPI_Comm comm)
219: {
220:   PetscErrorCode        ierr;
221:   PetscInt              deviceOpt = 0;
222:   PetscBool             cuda_view_flag = PETSC_FALSE,flg;
223:   struct cudaDeviceProp prop;
224:   int                   devCount,device,devicecnt;
225:   cudaError_t           err = cudaSuccess;
226:   PetscMPIInt           rank,size;

229:   /*
230:      If collecting logging information, by default, wait for GPU to complete its operations
231:      before returning to the CPU in order to get accurate timings of each event
232:   */
233:   PetscOptionsHasName(NULL,NULL,"-log_summary",&PetscCUDASynchronize);
234:   if (!PetscCUDASynchronize) {
235:     PetscOptionsHasName(NULL,NULL,"-log_view",&PetscCUDASynchronize);
236:   }

238:   PetscOptionsBegin(comm,NULL,"CUDA options","Sys");
239:   PetscOptionsInt("-cuda_set_device","Set all MPI ranks to use the specified CUDA device",NULL,deviceOpt,&deviceOpt,&flg);
240:   device = (int)deviceOpt;
241:   PetscOptionsBool("-cuda_synchronize","Wait for the GPU to complete operations before returning to the CPU",NULL,PetscCUDASynchronize,&PetscCUDASynchronize,NULL);
242:   PetscOptionsDeprecated("-cuda_show_devices","-cuda_view","3.12",NULL);
243:   PetscOptionsName("-cuda_view","Display CUDA device information and assignments",NULL,&cuda_view_flag);
244:   PetscOptionsEnd();
245:   if (!PetscCUDAInitialized) {
246:     MPI_Comm_size(comm,&size);

248:     if (size>1 && !flg) {
249:       /* check to see if we force multiple ranks to hit the same GPU */
250:       /* we're not using the same GPU on multiple MPI threads. So try to allocated different   GPUs to different processes */

252:       /* First get the device count */
253:       err   = cudaGetDeviceCount(&devCount);
254:       if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceCount %s",cudaGetErrorString(err));

256:       /* next determine the rank and then set the device via a mod */
257:       MPI_Comm_rank(comm,&rank);
258:       device = rank % devCount;
259:     }
260:     err = cudaSetDevice(device);
261:     if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDevice %s",cudaGetErrorString(err));

263:     /* set the device flags so that it can map host memory */
264:     err = cudaSetDeviceFlags(cudaDeviceMapHost);
265:     if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDeviceFlags %s",cudaGetErrorString(err));

267:     PetscCUBLASInitializeHandle();
268:     PetscCUSOLVERDnInitializeHandle();
269:     PetscCUDAInitialized = PETSC_TRUE;
270:   }
271:   if (cuda_view_flag) {
272:     MPI_Comm_rank(comm,&rank);
273:     err  = cudaGetDeviceCount(&devCount);
274:     if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceCount %s",cudaGetErrorString(err));
275:     for (devicecnt = 0; devicecnt < devCount; ++devicecnt) {
276:       err = cudaGetDeviceProperties(&prop,devicecnt);
277:       if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceProperties %s",cudaGetErrorString(err));
278:       PetscPrintf(comm, "CUDA device %d: %s\n", devicecnt, prop.name);
279:     }
280:     PetscSynchronizedPrintf(comm,"[%d] Using CUDA device %d.\n",rank,device);
281:     PetscSynchronizedFlush(comm,PETSC_STDOUT);
282:   }
283:   return(0);
284: }
285: #endif

287: /*@C
288:    PetscEnd - Calls PetscFinalize() and then ends the program. This is useful if one
289:      wishes a clean exit somewhere deep in the program.

291:    Collective on PETSC_COMM_WORLD

293:    Options Database Keys are the same as for PetscFinalize()

295:    Level: advanced

297:    Note:
298:    See PetscInitialize() for more general runtime options.

300: .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscFinalize()
301: @*/
302: PetscErrorCode  PetscEnd(void)
303: {
305:   PetscFinalize();
306:   exit(0);
307:   return 0;
308: }

310: PetscBool PetscOptionsPublish = PETSC_FALSE;
311: PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void);
312: PETSC_INTERN PetscBool      petscsetmallocvisited;
313: static       char           emacsmachinename[256];

315: PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = 0;
316: PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = 0;

318: /*@C
319:    PetscSetHelpVersionFunctions - Sets functions that print help and version information
320:    before the PETSc help and version information is printed. Must call BEFORE PetscInitialize().
321:    This routine enables a "higher-level" package that uses PETSc to print its messages first.

323:    Input Parameter:
324: +  help - the help function (may be NULL)
325: -  version - the version function (may be NULL)

327:    Level: developer

329: @*/
330: PetscErrorCode  PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm),PetscErrorCode (*version)(MPI_Comm))
331: {
333:   PetscExternalHelpFunction    = help;
334:   PetscExternalVersionFunction = version;
335:   return(0);
336: }

338: #if defined(PETSC_USE_LOG)
339: PETSC_INTERN PetscBool   PetscObjectsLog;
340: #endif

342: PETSC_INTERN PetscErrorCode  PetscOptionsCheckInitial_Private(void)
343: {
344:   char              string[64],mname[PETSC_MAX_PATH_LEN],*f;
345:   MPI_Comm          comm = PETSC_COMM_WORLD;
346:   PetscBool         flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flag;
347:   PetscErrorCode    ierr;
348:   PetscReal         si;
349:   PetscInt          intensity;
350:   int               i;
351:   PetscMPIInt       rank;
352:   char              version[256],helpoptions[256];
353: #if defined(PETSC_USE_LOG)
354:   PetscViewerFormat format;
355:   PetscBool         flg4 = PETSC_FALSE;
356: #endif
357: #if defined(PETSC_HAVE_CUDA)
358:   PetscBool         initCuda = PETSC_TRUE;
359: #endif

362:   MPI_Comm_rank(comm,&rank);

364: #if !defined(PETSC_HAVE_THREADSAFETY)
365:   if (!(PETSC_RUNNING_ON_VALGRIND)) {
366:     /*
367:       Setup the memory management; support for tracing malloc() usage
368:     */
369:     PetscBool         mdebug = PETSC_FALSE, eachcall = PETSC_FALSE, initializenan = PETSC_FALSE, mlog = PETSC_FALSE;

371: #if defined(PETSC_USE_DEBUG)
372:     mdebug        = PETSC_TRUE;
373:     initializenan = PETSC_TRUE;
374:     PetscOptionsHasName(NULL,NULL,"-malloc_test",&flg1);
375: #else
376:     /* don't warn about unused option */
377:     PetscOptionsHasName(NULL,NULL,"-malloc_test",&flg1);
378:     flg1 = PETSC_FALSE;
379: #endif
380:     PetscOptionsGetBool(NULL,NULL,"-malloc_debug",&flg2,&flg3);
381:     if (flg1 || flg2) {
382:       mdebug        = PETSC_TRUE;
383:       eachcall      = PETSC_TRUE;
384:       initializenan = PETSC_TRUE;
385:     } else if (flg3 && !flg2) {
386:       mdebug        = PETSC_FALSE;
387:       eachcall      = PETSC_FALSE;
388:       initializenan = PETSC_FALSE;
389:     }

391:     PetscOptionsHasName(NULL,NULL,"-malloc_view",&mlog);
392:     if (mlog) {
393:       mdebug = PETSC_TRUE;
394:     }
395:     /* the next line is deprecated */
396:     PetscOptionsGetBool(NULL,NULL,"-malloc",&mdebug,NULL);
397:     PetscOptionsGetBool(NULL,NULL,"-malloc_dump",&mdebug,NULL);
398:     PetscOptionsGetBool(NULL,NULL,"-log_view_memory",&mdebug,NULL);
399:     if (mdebug) {
400:       PetscMallocSetDebug(eachcall,initializenan);
401:     }
402:     if (mlog) {
403:       PetscReal logthreshold = 0;
404:       PetscOptionsGetReal(NULL,NULL,"-malloc_view_threshold",&logthreshold,NULL);
405:       PetscMallocViewSet(logthreshold);
406:     }
407:   }

409:   PetscOptionsGetBool(NULL,NULL,"-malloc_coalesce",&flg1,&flg2);
410:   if (flg2) {PetscMallocSetCoalesce(flg1);}
411:   flg1 = PETSC_FALSE;
412:   PetscOptionsGetBool(NULL,NULL,"-malloc_hbw",&flg1,NULL);
413:   /* ignore this option if malloc is already set */
414:   if (flg1 && !petscsetmallocvisited) {PetscSetUseHBWMalloc_Private();}

416:   flg1 = PETSC_FALSE;
417:   PetscOptionsGetBool(NULL,NULL,"-malloc_info",&flg1,NULL);
418:   if (!flg1) {
419:     flg1 = PETSC_FALSE;
420:     PetscOptionsGetBool(NULL,NULL,"-memory_view",&flg1,NULL);
421:   }
422:   if (flg1) {
423:     PetscMemorySetGetMaximumUsage();
424:   }
425: #endif

427: #if defined(PETSC_USE_LOG)
428:   PetscOptionsHasName(NULL,NULL,"-objects_dump",&PetscObjectsLog);
429: #endif

431:   /*
432:       Set the display variable for graphics
433:   */
434:   PetscSetDisplay();

436:   /*
437:       Print the PETSc version information
438:   */
439:   PetscOptionsHasName(NULL,NULL,"-v",&flg1);
440:   PetscOptionsHasName(NULL,NULL,"-version",&flg2);
441:   PetscOptionsHasHelp(NULL,&flg3);
442:   if (flg1 || flg2 || flg3) {

444:     /*
445:        Print "higher-level" package version message
446:     */
447:     if (PetscExternalVersionFunction) {
448:       (*PetscExternalVersionFunction)(comm);
449:     }

451:     PetscGetVersion(version,256);
452:     (*PetscHelpPrintf)(comm,"--------------------------------------------------------------------------\n");
453:     (*PetscHelpPrintf)(comm,"%s\n",version);
454:     (*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO);
455:     (*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n");
456:     (*PetscHelpPrintf)(comm,"See docs/faq.html for problems.\n");
457:     (*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n");
458:     (*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR);
459:     (*PetscHelpPrintf)(comm,"--------------------------------------------------------------------------\n");
460:   }

462:   /*
463:        Print "higher-level" package help message
464:   */
465:   if (flg3) {
466:     if (PetscExternalHelpFunction) {
467:       (*PetscExternalHelpFunction)(comm);
468:     }
469:   }

471:   PetscOptionsGetString(NULL,NULL,"-help",helpoptions,sizeof(helpoptions),&flg1);
472:   if (flg1) {
473:     PetscStrcmp(helpoptions,"intro",&flg2);
474:     if (flg2) {
475:       PetscOptionsDestroyDefault();
476:       PetscFreeMPIResources();
477:       MPI_Finalize();
478:       exit(0);
479:     }
480:   }

482:   /*
483:       Setup the error handling
484:   */
485:   flg1 = PETSC_FALSE;
486:   PetscOptionsGetBool(NULL,NULL,"-on_error_abort",&flg1,NULL);
487:   if (flg1) {
488:     MPI_Comm_set_errhandler(comm,MPI_ERRORS_ARE_FATAL);
489:     PetscPushErrorHandler(PetscAbortErrorHandler,0);
490:   }
491:   flg1 = PETSC_FALSE;
492:   PetscOptionsGetBool(NULL,NULL,"-on_error_mpiabort",&flg1,NULL);
493:   if (flg1) { PetscPushErrorHandler(PetscMPIAbortErrorHandler,0);}
494:   flg1 = PETSC_FALSE;
495:   PetscOptionsGetBool(NULL,NULL,"-mpi_return_on_error",&flg1,NULL);
496:   if (flg1) {
497:     MPI_Comm_set_errhandler(comm,MPI_ERRORS_RETURN);
498:   }
499:   flg1 = PETSC_FALSE;
500:   PetscOptionsGetBool(NULL,NULL,"-no_signal_handler",&flg1,NULL);
501:   if (!flg1) {PetscPushSignalHandler(PetscSignalHandlerDefault,(void*)0);}
502:   flg1 = PETSC_FALSE;
503:   PetscOptionsGetBool(NULL,NULL,"-fp_trap",&flg1,&flag);
504:   if (flag) {PetscSetFPTrap((PetscFPTrap)flg1);}
505:   PetscOptionsGetInt(NULL,NULL,"-check_pointer_intensity",&intensity,&flag);

508:   /*
509:       Setup debugger information
510:   */
511:   PetscSetDefaultDebugger();
512:   PetscOptionsGetString(NULL,NULL,"-on_error_attach_debugger",string,64,&flg1);
513:   if (flg1) {
514:     MPI_Errhandler err_handler;

516:     PetscSetDebuggerFromString(string);
517:     MPI_Comm_create_errhandler(Petsc_MPI_DebuggerOnError,&err_handler);
518:     MPI_Comm_set_errhandler(comm,err_handler);
519:     PetscPushErrorHandler(PetscAttachDebuggerErrorHandler,0);
520:   }
521:   PetscOptionsGetString(NULL,NULL,"-debug_terminal",string,64,&flg1);
522:   if (flg1) { PetscSetDebugTerminal(string); }
523:   PetscOptionsGetString(NULL,NULL,"-start_in_debugger",string,64,&flg1);
524:   PetscOptionsGetString(NULL,NULL,"-stop_for_debugger",string,64,&flg2);
525:   if (flg1 || flg2) {
526:     PetscMPIInt    size;
527:     PetscInt       lsize,*nodes;
528:     MPI_Errhandler err_handler;
529:     /*
530:        we have to make sure that all processors have opened
531:        connections to all other processors, otherwise once the
532:        debugger has stated it is likely to receive a SIGUSR1
533:        and kill the program.
534:     */
535:     MPI_Comm_size(comm,&size);
536:     if (size > 2) {
537:       PetscMPIInt dummy = 0;
538:       MPI_Status  status;
539:       for (i=0; i<size; i++) {
540:         if (rank != i) {
541:           MPI_Send(&dummy,1,MPI_INT,i,109,comm);
542:         }
543:       }
544:       for (i=0; i<size; i++) {
545:         if (rank != i) {
546:           MPI_Recv(&dummy,1,MPI_INT,i,109,comm,&status);
547:         }
548:       }
549:     }
550:     /* check if this processor node should be in debugger */
551:     PetscMalloc1(size,&nodes);
552:     lsize = size;
553:     PetscOptionsGetIntArray(NULL,NULL,"-debugger_nodes",nodes,&lsize,&flag);
554:     if (flag) {
555:       for (i=0; i<lsize; i++) {
556:         if (nodes[i] == rank) { flag = PETSC_FALSE; break; }
557:       }
558:     }
559:     if (!flag) {
560:       PetscSetDebuggerFromString(string);
561:       PetscPushErrorHandler(PetscAbortErrorHandler,0);
562:       if (flg1) {
563:         PetscAttachDebugger();
564:       } else {
565:         PetscStopForDebugger();
566:       }
567:       MPI_Comm_create_errhandler(Petsc_MPI_AbortOnError,&err_handler);
568:       MPI_Comm_set_errhandler(comm,err_handler);
569:     }
570:     PetscFree(nodes);
571:   }

573:   PetscOptionsGetString(NULL,NULL,"-on_error_emacs",emacsmachinename,128,&flg1);
574:   if (flg1 && !rank) {PetscPushErrorHandler(PetscEmacsClientErrorHandler,emacsmachinename);}

576:   /*
577:         Setup profiling and logging
578:   */
579: #if defined(PETSC_USE_INFO)
580:   {
581:     char logname[PETSC_MAX_PATH_LEN]; logname[0] = 0;
582:     PetscOptionsGetString(NULL,NULL,"-info",logname,250,&flg1);
583:     if (flg1 && logname[0]) {
584:       PetscInfoAllow(PETSC_TRUE,logname);
585:     } else if (flg1) {
586:       PetscInfoAllow(PETSC_TRUE,NULL);
587:     }
588:   }
589: #endif
590: #if defined(PETSC_USE_LOG)
591:   mname[0] = 0;
592:   PetscOptionsGetString(NULL,NULL,"-history",mname,PETSC_MAX_PATH_LEN,&flg1);
593:   if (flg1) {
594:     if (mname[0]) {
595:       PetscOpenHistoryFile(mname,&petsc_history);
596:     } else {
597:       PetscOpenHistoryFile(NULL,&petsc_history);
598:     }
599:   }

601:   PetscOptionsGetBool(NULL,NULL,"-log_sync",&PetscLogSyncOn,NULL);

603: #if defined(PETSC_HAVE_MPE)
604:   flg1 = PETSC_FALSE;
605:   PetscOptionsHasName(NULL,NULL,"-log_mpe",&flg1);
606:   if (flg1) {PetscLogMPEBegin();}
607: #endif
608:   flg1 = PETSC_FALSE;
609:   flg3 = PETSC_FALSE;
610:   PetscOptionsGetBool(NULL,NULL,"-log_all",&flg1,NULL);
611:   PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);
612:   if (flg1)                      { PetscLogAllBegin(); }
613:   else if (flg3)                 { PetscLogDefaultBegin();}

615:   PetscOptionsGetString(NULL,NULL,"-log_trace",mname,250,&flg1);
616:   if (flg1) {
617:     char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN];
618:     FILE *file;
619:     if (mname[0]) {
620:       PetscSNPrintf(name,PETSC_MAX_PATH_LEN,"%s.%d",mname,rank);
621:       PetscFixFilename(name,fname);
622:       file = fopen(fname,"w");
623:       if (!file) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname);
624:     } else file = PETSC_STDOUT;
625:     PetscLogTraceBegin(file);
626:   }

628:   PetscOptionsGetViewer(comm,NULL,NULL,"-log_view",NULL,&format,&flg4);
629:   if (flg4) {
630:     if (format == PETSC_VIEWER_ASCII_XML) {
631:       PetscLogNestedBegin();
632:     } else {
633:       PetscLogDefaultBegin();
634:     }
635:   }
636:   if (flg4 && format == PETSC_VIEWER_ASCII_XML) {
637:     PetscReal threshold = PetscRealConstant(0.01);
638:     PetscOptionsGetReal(NULL,NULL,"-log_threshold",&threshold,&flg1);
639:     if (flg1) {PetscLogSetThreshold((PetscLogDouble)threshold,NULL);}
640:   }
641: #endif

643:   PetscOptionsGetBool(NULL,NULL,"-saws_options",&PetscOptionsPublish,NULL);

645: #if defined(PETSC_HAVE_CUDA)
646:   PetscOptionsBegin(comm,NULL,"CUDA initialize","Sys");
647:   PetscOptionsBool("-cuda_initialize","Initialize the CUDA devices and cuBLAS during PetscInitialize()",NULL,initCuda,&initCuda,NULL);
648:   PetscOptionsEnd();
649:   if (initCuda) {PetscCUDAInitialize(PETSC_COMM_WORLD);}
650: #endif

652:   /*
653:        Print basic help message
654:   */
655:   PetscOptionsHasHelp(NULL,&flg1);
656:   if (flg1) {
657:     (*PetscHelpPrintf)(comm,"Options for all PETSc programs:\n");
658:     (*PetscHelpPrintf)(comm," -help: prints help method for each option\n");
659:     (*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is detected. Useful \n ");
660:     (*PetscHelpPrintf)(comm,"       only when run in the debugger\n");
661:     (*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
662:     (*PetscHelpPrintf)(comm,"       start the debugger in new xterm\n");
663:     (*PetscHelpPrintf)(comm,"       unless noxterm is given\n");
664:     (*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
665:     (*PetscHelpPrintf)(comm,"       start all processes in the debugger\n");
666:     (*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n");
667:     (*PetscHelpPrintf)(comm,"    emacs jumps to error file\n");
668:     (*PetscHelpPrintf)(comm," -debugger_nodes [n1,n2,..] Nodes to start in debugger\n");
669:     (*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n");
670:     (*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n");
671:     (*PetscHelpPrintf)(comm,"                      waits the delay for you to attach\n");
672:     (*PetscHelpPrintf)(comm," -display display: Location where X window graphics and debuggers are displayed\n");
673:     (*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n");
674:     (*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n");
675:     (*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n");
676:     (*PetscHelpPrintf)(comm,"           note on IBM RS6000 this slows run greatly\n");
677:     (*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n");
678:     (*PetscHelpPrintf)(comm," -malloc: use PETSc error checking malloc (deprecated, use -malloc_debug)\n");
679:     (*PetscHelpPrintf)(comm," -malloc no: don't use PETSc error checking malloc (deprecated, use -malloc_debug no)\n");
680:     (*PetscHelpPrintf)(comm," -malloc_info: prints total memory usage\n");
681:     (*PetscHelpPrintf)(comm," -malloc_view <optional filename>: keeps log of all memory allocations, displays in PetscFinalize()\n");
682:     (*PetscHelpPrintf)(comm," -malloc_debug <true or false>: enables or disables extended checking for memory corruption\n");
683:     (*PetscHelpPrintf)(comm," -options_view: dump list of options inputted\n");
684:     (*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n");
685:     (*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n");
686:     (*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n");
687:     (*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n");
688:     (*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has separate tmp directory\n");
689:     (*PetscHelpPrintf)(comm," -memory_view: print memory usage at end of run\n");
690: #if defined(PETSC_USE_LOG)
691:     (*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n");
692:     (*PetscHelpPrintf)(comm," -log_view [:filename:[format]]: logging objects and events\n");
693:     (*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n");
694: #if defined(PETSC_HAVE_MPE)
695:     (*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through Jumpshot\n");
696: #endif
697:     (*PetscHelpPrintf)(comm," -info <optional filename>: print informative messages about the calculations\n");
698: #endif
699:     (*PetscHelpPrintf)(comm," -v: prints PETSc version number and release date\n");
700:     (*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n");
701:     (*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n");
702:     (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
703:   }

705: #if defined(PETSC_HAVE_POPEN)
706:   {
707:   char machine[128];
708:   PetscOptionsGetString(NULL,NULL,"-popen_machine",machine,128,&flg1);
709:   if (flg1) {
710:     PetscPOpenSetMachine(machine);
711:   }
712:   }
713: #endif

715:   PetscOptionsGetReal(NULL,NULL,"-petsc_sleep",&si,&flg1);
716:   if (flg1) {
717:     PetscSleep(si);
718:   }

720:   PetscOptionsGetString(NULL,NULL,"-info_exclude",mname,PETSC_MAX_PATH_LEN,&flg1);
721:   if (flg1) {
722:     PetscStrstr(mname,"null",&f);
723:     if (f) {
724:       PetscInfoDeactivateClass(0);
725:     }
726:   }

728: #if defined(PETSC_HAVE_VIENNACL)
729:   PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);
730:   if (!flg3) {
731:     PetscOptionsHasName(NULL,NULL,"-log_view",&flg3);
732:   }
733:   PetscOptionsGetBool(NULL,NULL,"-viennacl_synchronize",&flg3,NULL);
734:   PetscViennaCLSynchronize = flg3;
735:   PetscViennaCLInit();
736: #endif

738:   /*
739:      Creates the logging data structures; this is enabled even if logging is not turned on
740:      This is the last thing we do before returning to the user code to prevent having the
741:      logging numbers contaminated by any startup time associated with MPI and the GPUs
742:   */
743: #if defined(PETSC_USE_LOG)
744:   PetscLogInitialize();
745: #endif

747:   return(0);
748: }