Actual source code: init.c

petsc-master 2019-09-20
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: extern PetscErrorCode PetscCUBLASInitializeHandle(void);
 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:   Notes:
213:    Initializing cuBLAS takes about 1/2 second there it is done by default in PetscInitialize() before logging begins

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

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

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

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

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

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

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

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

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

288:    Collective on PETSC_COMM_WORLD

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

292:    Level: advanced

294:    Note:
295:    See PetscInitialize() for more general runtime options.

297: .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscFinalize()
298: @*/
299: PetscErrorCode  PetscEnd(void)
300: {
302:   PetscFinalize();
303:   exit(0);
304:   return 0;
305: }

307: PetscBool PetscOptionsPublish = PETSC_FALSE;
308: PETSC_INTERN PetscErrorCode PetscSetUseHBWMalloc_Private(void);
309: PETSC_INTERN PetscBool      petscsetmallocvisited;
310: static       char           emacsmachinename[256];

312: PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = 0;
313: PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = 0;

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

320:    Input Parameter:
321: +  help - the help function (may be NULL)
322: -  version - the version function (may be NULL)

324:    Level: developer

326: @*/
327: PetscErrorCode  PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm),PetscErrorCode (*version)(MPI_Comm))
328: {
330:   PetscExternalHelpFunction    = help;
331:   PetscExternalVersionFunction = version;
332:   return(0);
333: }

335: #if defined(PETSC_USE_LOG)
336: PETSC_INTERN PetscBool   PetscObjectsLog;
337: #endif

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

359:   MPI_Comm_rank(comm,&rank);

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

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

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

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

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

424: #if defined(PETSC_USE_LOG)
425:   PetscOptionsHasName(NULL,NULL,"-objects_dump",&PetscObjectsLog);
426: #endif

428:   /*
429:       Set the display variable for graphics
430:   */
431:   PetscSetDisplay();

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

441:     /*
442:        Print "higher-level" package version message
443:     */
444:     if (PetscExternalVersionFunction) {
445:       (*PetscExternalVersionFunction)(comm);
446:     }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

712:   PetscOptionsGetReal(NULL,NULL,"-petsc_sleep",&si,&flg1);
713:   if (flg1) {
714:     PetscSleep(si);
715:   }

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

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

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

744:   return(0);
745: }