Actual source code: init.c

petsc-3.12.5 2020-03-29
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: #if defined(PETSC_USE_LOG)
408:     PetscOptionsGetBool(NULL,NULL,"-log_view_memory",&PetscLogMemory,NULL);
409: #endif
410:   }

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

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

430: #if defined(PETSC_USE_LOG)
431:   PetscOptionsHasName(NULL,NULL,"-objects_dump",&PetscObjectsLog);
432: #endif

434:   /*
435:       Set the display variable for graphics
436:   */
437:   PetscSetDisplay();

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

447:     /*
448:        Print "higher-level" package version message
449:     */
450:     if (PetscExternalVersionFunction) {
451:       (*PetscExternalVersionFunction)(comm);
452:     }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

718:   PetscOptionsGetReal(NULL,NULL,"-petsc_sleep",&si,&flg1);
719:   if (flg1) {
720:     PetscSleep(si);
721:   }

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

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

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

750:   return(0);
751: }