Actual source code: init.c

petsc-master 2018-05-25
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>

 14: #if defined(PETSC_HAVE_SYS_SYSINFO_H)
 15: #include <sys/sysinfo.h>
 16: #endif
 17: #if defined(PETSC_HAVE_UNISTD_H)
 18: #include <unistd.h>
 19: #endif
 20: #if defined(PETSC_HAVE_CUDA)
 21: #include <cuda_runtime.h>
 22: #endif

 24: #if defined(PETSC_HAVE_VIENNACL)
 25: PETSC_EXTERN PetscErrorCode PetscViennaCLInit();
 26: #endif

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

 38: PetscMPIInt PetscGlobalRank       = -1;
 39: PetscMPIInt PetscGlobalSize       = -1;

 41: #if defined(PETSC_HAVE_COMPLEX)
 42: #if defined(PETSC_COMPLEX_INSTANTIATE)
 43: template <> class std::complex<double>; /* instantiate complex template class */
 44: #endif
 45: #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
 46: MPI_Datatype MPIU_C_DOUBLE_COMPLEX;
 47: MPI_Datatype MPIU_C_COMPLEX;
 48: #endif

 50: /*MC
 51:    PETSC_i - the imaginary number i

 53:    Synopsis:
 54:    #include <petscsys.h>
 55:    PetscComplex PETSC_i;

 57:    Level: beginner

 59:    Note:
 60:    Complex numbers are automatically available if PETSc located a working complex implementation

 62: .seealso: PetscRealPart(), PetscImaginaryPart(), PetscRealPartComplex(), PetscImaginaryPartComplex()
 63: M*/
 64: PetscComplex PETSC_i;
 65: #endif
 66: #if defined(PETSC_USE_REAL___FLOAT128)
 67: MPI_Datatype MPIU___FLOAT128 = 0;
 68: #if defined(PETSC_HAVE_COMPLEX)
 69: MPI_Datatype MPIU___COMPLEX128 = 0;
 70: #endif
 71: #elif defined(PETSC_USE_REAL___FP16)
 72: MPI_Datatype MPIU___FP16 = 0;
 73: #endif
 74: MPI_Datatype MPIU_2SCALAR = 0;
 75: #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
 76: MPI_Datatype MPIU_2INT = 0;
 77: #endif
 78: MPI_Datatype MPIU_BOOL;
 79: MPI_Datatype MPIU_ENUM;

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

 93: /* ------------------------------------------------------------------------------*/
 94: /*
 95:    Optional file where all PETSc output from various prints is saved
 96: */
 97: FILE *petsc_history = NULL;

 99: PetscErrorCode  PetscOpenHistoryFile(const char filename[],FILE **fd)
100: {
102:   PetscMPIInt    rank,size;
103:   char           pfile[PETSC_MAX_PATH_LEN],pname[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN],date[64];
104:   char           version[256];

107:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
108:   if (!rank) {
109:     char        arch[10];
110:     int         err;

112:     PetscGetArchType(arch,10);
113:     PetscGetDate(date,64);
114:     PetscGetVersion(version,256);
115:     MPI_Comm_size(PETSC_COMM_WORLD,&size);
116:     if (filename) {
117:       PetscFixFilename(filename,fname);
118:     } else {
119:       PetscGetHomeDirectory(pfile,240);
120:       PetscStrcat(pfile,"/.petschistory");
121:       PetscFixFilename(pfile,fname);
122:     }

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

127:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
128:     PetscFPrintf(PETSC_COMM_SELF,*fd,"%s %s\n",version,date);
129:     PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);
130:     PetscFPrintf(PETSC_COMM_SELF,*fd,"%s on a %s, %d proc. with options:\n",pname,arch,size);
131:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");

133:     err = fflush(*fd);
134:     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
135:   }
136:   return(0);
137: }

139: PetscErrorCode  PetscCloseHistoryFile(FILE **fd)
140: {
142:   PetscMPIInt    rank;
143:   char           date[64];
144:   int            err;

147:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
148:   if (!rank) {
149:     PetscGetDate(date,64);
150:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
151:     PetscFPrintf(PETSC_COMM_SELF,*fd,"Finished at %s\n",date);
152:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
153:     err  = fflush(*fd);
154:     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
155:     err = fclose(*fd);
156:     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
157:   }
158:   return(0);
159: }

161: /* ------------------------------------------------------------------------------*/

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

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

172: void Petsc_MPI_AbortOnError(MPI_Comm *comm,PetscMPIInt *flag)
173: {
175:   (*PetscErrorPrintf)("MPI error %d\n",*flag);
176:   abort();
177: }

179: void Petsc_MPI_DebuggerOnError(MPI_Comm *comm,PetscMPIInt *flag)
180: {

184:   (*PetscErrorPrintf)("MPI error %d\n",*flag);
185:   PetscAttachDebugger();
186:   if (ierr) MPI_Abort(*comm,*flag); /* hopeless so get out */
187: }

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

193:    Collective on PETSC_COMM_WORLD

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

197:    Level: advanced

199:    Note:
200:    See PetscInitialize() for more general runtime options.

202: .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscFinalize()
203: @*/
204: PetscErrorCode  PetscEnd(void)
205: {
207:   PetscFinalize();
208:   exit(0);
209:   return 0;
210: }

212: PetscBool PetscOptionsPublish = PETSC_FALSE;
213: extern PetscErrorCode PetscSetUseTrMalloc_Private(void);
214: extern PetscErrorCode PetscSetUseHBWMalloc_Private(void);
215: extern PetscBool      petscsetmallocvisited;
216: static char           emacsmachinename[256];

218: PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = 0;
219: PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = 0;

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

226:    Input Parameter:
227: +  help - the help function (may be NULL)
228: -  version - the version function (may be NULL)

230:    Level: developer

232:    Concepts: package help message

234: @*/
235: PetscErrorCode  PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm),PetscErrorCode (*version)(MPI_Comm))
236: {
238:   PetscExternalHelpFunction    = help;
239:   PetscExternalVersionFunction = version;
240:   return(0);
241: }

243: #if defined(PETSC_USE_LOG)
244: extern PetscBool   PetscObjectsLog;
245: #endif

247: PetscErrorCode  PetscOptionsCheckInitial_Private(void)
248: {
249:   char              string[64],mname[PETSC_MAX_PATH_LEN],*f;
250:   MPI_Comm          comm = PETSC_COMM_WORLD;
251:   PetscBool         flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flag;
252:   PetscErrorCode    ierr;
253:   PetscReal         si;
254:   PetscInt          intensity;
255:   int               i;
256:   PetscMPIInt       rank;
257:   char              version[256],helpoptions[256];
258: #if !defined(PETSC_HAVE_THREADSAFETY)
259:   PetscReal         logthreshold;
260: #endif
261: #if defined(PETSC_USE_LOG)
262:   PetscViewerFormat format;
263:   PetscBool         flg4 = PETSC_FALSE;
264: #endif

267:   MPI_Comm_rank(comm,&rank);

269: #if !defined(PETSC_HAVE_THREADSAFETY)
270:   /*
271:       Setup the memory management; support for tracing malloc() usage
272:   */
273:   PetscOptionsHasName(NULL,NULL,"-malloc_log",&flg3);
274:   logthreshold = 0.0;
275:   PetscOptionsGetReal(NULL,NULL,"-malloc_log_threshold",&logthreshold,&flg1);
276:   if (flg1) flg3 = PETSC_TRUE;
277: #if defined(PETSC_USE_DEBUG)
278:   PetscOptionsGetBool(NULL,NULL,"-malloc",&flg1,&flg2);
279:   if ((!flg2 || flg1) && !petscsetmallocvisited) {
280:     if (flg2 || !(PETSC_RUNNING_ON_VALGRIND)) {
281:       /* turn off default -malloc if valgrind is being used */
282:       PetscSetUseTrMalloc_Private();
283:     }
284:   }
285: #else
286:   PetscOptionsGetBool(NULL,NULL,"-malloc_dump",&flg1,NULL);
287:   PetscOptionsGetBool(NULL,NULL,"-malloc",&flg2,NULL);
288:   if (flg1 || flg2 || flg3) {PetscSetUseTrMalloc_Private();}
289: #endif
290:   if (flg3) {
291:     PetscMallocSetDumpLogThreshold((PetscLogDouble)logthreshold);
292:   }
293:   PetscOptionsGetBool(NULL,NULL,"-malloc_coalesce",&flg1,&flg2);
294:   if (flg2) {PetscMallocSetCoalesce(flg1);}
295:   flg1 = PETSC_FALSE;
296:   PetscOptionsGetBool(NULL,NULL,"-malloc_debug",&flg1,NULL);
297:   if (flg1) {
298:     PetscSetUseTrMalloc_Private();
299:     PetscMallocDebug(PETSC_TRUE);
300:   }
301:   flg1 = PETSC_FALSE;
302:   PetscOptionsGetBool(NULL,NULL,"-malloc_test",&flg1,NULL);
303: #if defined(PETSC_USE_DEBUG)
304:   if (flg1 && !PETSC_RUNNING_ON_VALGRIND) {
305:     PetscSetUseTrMalloc_Private();
306:     PetscMallocSetDumpLog();
307:     PetscMallocDebug(PETSC_TRUE);
308:   }
309: #endif
310:   flg1 = PETSC_FALSE;
311:   PetscOptionsGetBool(NULL,NULL,"-malloc_hbw",&flg1,NULL);
312:   /* ignore this option if malloc is already set */
313:   if (flg1 && !petscsetmallocvisited) {PetscSetUseHBWMalloc_Private();}

315:   flg1 = PETSC_FALSE;
316:   PetscOptionsGetBool(NULL,NULL,"-malloc_info",&flg1,NULL);
317:   if (!flg1) {
318:     flg1 = PETSC_FALSE;
319:     PetscOptionsGetBool(NULL,NULL,"-memory_view",&flg1,NULL);
320:   }
321:   if (flg1) {
322:     PetscMemorySetGetMaximumUsage();
323:   }
324: #endif

326: #if defined(PETSC_USE_LOG)
327:   PetscOptionsHasName(NULL,NULL,"-objects_dump",&PetscObjectsLog);
328: #endif

330:   /*
331:       Set the display variable for graphics
332:   */
333:   PetscSetDisplay();

335:   /*
336:       Print the PETSc version information
337:   */
338:   PetscOptionsHasName(NULL,NULL,"-v",&flg1);
339:   PetscOptionsHasName(NULL,NULL,"-version",&flg2);
340:   PetscOptionsHasName(NULL,NULL,"-help",&flg3);
341:   if (flg1 || flg2 || flg3) {

343:     /*
344:        Print "higher-level" package version message
345:     */
346:     if (PetscExternalVersionFunction) {
347:       (*PetscExternalVersionFunction)(comm);
348:     }

350:     PetscGetVersion(version,256);
351:     (*PetscHelpPrintf)(comm,"--------------------------------------------------------------------------\n");
352:     (*PetscHelpPrintf)(comm,"%s\n",version);
353:     (*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO);
354:     (*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n");
355:     (*PetscHelpPrintf)(comm,"See docs/faq.html for problems.\n");
356:     (*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n");
357:     (*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR);
358:     (*PetscHelpPrintf)(comm,"--------------------------------------------------------------------------\n");
359:   }

361:   /*
362:        Print "higher-level" package help message
363:   */
364:   if (flg3) {
365:     if (PetscExternalHelpFunction) {
366:       (*PetscExternalHelpFunction)(comm);
367:     }
368:   }

370:   PetscOptionsGetString(NULL,NULL,"-help",helpoptions,sizeof(helpoptions),&flg1);
371:   if (flg1) {
372:     PetscStrcmp(helpoptions,"intro",&flg2);
373:     if (flg2) {
374:       PetscOptionsDestroyDefault();
375:       PetscFreeMPIResources();
376:       MPI_Finalize();
377:       exit(0);
378:     }
379:   }

381:   /*
382:       Setup the error handling
383:   */
384:   flg1 = PETSC_FALSE;
385:   PetscOptionsGetBool(NULL,NULL,"-on_error_abort",&flg1,NULL);
386:   if (flg1) {
387:     MPI_Comm_set_errhandler(comm,MPI_ERRORS_ARE_FATAL);
388:     PetscPushErrorHandler(PetscAbortErrorHandler,0);
389:   }
390:   flg1 = PETSC_FALSE;
391:   PetscOptionsGetBool(NULL,NULL,"-on_error_mpiabort",&flg1,NULL);
392:   if (flg1) { PetscPushErrorHandler(PetscMPIAbortErrorHandler,0);}
393:   flg1 = PETSC_FALSE;
394:   PetscOptionsGetBool(NULL,NULL,"-mpi_return_on_error",&flg1,NULL);
395:   if (flg1) {
396:     MPI_Comm_set_errhandler(comm,MPI_ERRORS_RETURN);
397:   }
398:   flg1 = PETSC_FALSE;
399:   PetscOptionsGetBool(NULL,NULL,"-no_signal_handler",&flg1,NULL);
400:   if (!flg1) {PetscPushSignalHandler(PetscSignalHandlerDefault,(void*)0);}
401:   flg1 = PETSC_FALSE;
402:   PetscOptionsGetBool(NULL,NULL,"-fp_trap",&flg1,NULL);
403:   if (flg1) {PetscSetFPTrap(PETSC_FP_TRAP_ON);}
404:   PetscOptionsGetInt(NULL,NULL,"-check_pointer_intensity",&intensity,&flag);

407:   /*
408:       Setup debugger information
409:   */
410:   PetscSetDefaultDebugger();
411:   PetscOptionsGetString(NULL,NULL,"-on_error_attach_debugger",string,64,&flg1);
412:   if (flg1) {
413:     MPI_Errhandler err_handler;

415:     PetscSetDebuggerFromString(string);
416:     MPI_Comm_create_errhandler((MPI_Handler_function*)Petsc_MPI_DebuggerOnError,&err_handler);
417:     MPI_Comm_set_errhandler(comm,err_handler);
418:     PetscPushErrorHandler(PetscAttachDebuggerErrorHandler,0);
419:   }
420:   PetscOptionsGetString(NULL,NULL,"-debug_terminal",string,64,&flg1);
421:   if (flg1) { PetscSetDebugTerminal(string); }
422:   PetscOptionsGetString(NULL,NULL,"-start_in_debugger",string,64,&flg1);
423:   PetscOptionsGetString(NULL,NULL,"-stop_for_debugger",string,64,&flg2);
424:   if (flg1 || flg2) {
425:     PetscMPIInt    size;
426:     PetscInt       lsize,*nodes;
427:     MPI_Errhandler err_handler;
428:     /*
429:        we have to make sure that all processors have opened
430:        connections to all other processors, otherwise once the
431:        debugger has stated it is likely to receive a SIGUSR1
432:        and kill the program.
433:     */
434:     MPI_Comm_size(comm,&size);
435:     if (size > 2) {
436:       PetscMPIInt dummy = 0;
437:       MPI_Status  status;
438:       for (i=0; i<size; i++) {
439:         if (rank != i) {
440:           MPI_Send(&dummy,1,MPI_INT,i,109,comm);
441:         }
442:       }
443:       for (i=0; i<size; i++) {
444:         if (rank != i) {
445:           MPI_Recv(&dummy,1,MPI_INT,i,109,comm,&status);
446:         }
447:       }
448:     }
449:     /* check if this processor node should be in debugger */
450:     PetscMalloc1(size,&nodes);
451:     lsize = size;
452:     PetscOptionsGetIntArray(NULL,NULL,"-debugger_nodes",nodes,&lsize,&flag);
453:     if (flag) {
454:       for (i=0; i<lsize; i++) {
455:         if (nodes[i] == rank) { flag = PETSC_FALSE; break; }
456:       }
457:     }
458:     if (!flag) {
459:       PetscSetDebuggerFromString(string);
460:       PetscPushErrorHandler(PetscAbortErrorHandler,0);
461:       if (flg1) {
462:         PetscAttachDebugger();
463:       } else {
464:         PetscStopForDebugger();
465:       }
466:       MPI_Comm_create_errhandler((MPI_Handler_function*)Petsc_MPI_AbortOnError,&err_handler);
467:       MPI_Comm_set_errhandler(comm,err_handler);
468:     }
469:     PetscFree(nodes);
470:   }

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

475:   /*
476:         Setup profiling and logging
477:   */
478: #if defined(PETSC_USE_INFO)
479:   {
480:     char logname[PETSC_MAX_PATH_LEN]; logname[0] = 0;
481:     PetscOptionsGetString(NULL,NULL,"-info",logname,250,&flg1);
482:     if (flg1 && logname[0]) {
483:       PetscInfoAllow(PETSC_TRUE,logname);
484:     } else if (flg1) {
485:       PetscInfoAllow(PETSC_TRUE,NULL);
486:     }
487:   }
488: #endif
489: #if defined(PETSC_USE_LOG)
490:   mname[0] = 0;
491:   PetscOptionsGetString(NULL,NULL,"-history",mname,PETSC_MAX_PATH_LEN,&flg1);
492:   if (flg1) {
493:     if (mname[0]) {
494:       PetscOpenHistoryFile(mname,&petsc_history);
495:     } else {
496:       PetscOpenHistoryFile(NULL,&petsc_history);
497:     }
498:   }
499: #if defined(PETSC_HAVE_MPE)
500:   flg1 = PETSC_FALSE;
501:   PetscOptionsHasName(NULL,NULL,"-log_mpe",&flg1);
502:   if (flg1) {PetscLogMPEBegin();}
503: #endif
504:   flg1 = PETSC_FALSE;
505:   flg3 = PETSC_FALSE;
506:   PetscOptionsGetBool(NULL,NULL,"-log_all",&flg1,NULL);
507:   PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);
508:   if (flg1)                      { PetscLogAllBegin(); }
509:   else if (flg3)                 { PetscLogDefaultBegin();}

511:   PetscOptionsGetString(NULL,NULL,"-log_trace",mname,250,&flg1);
512:   if (flg1) {
513:     char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN];
514:     FILE *file;
515:     if (mname[0]) {
516:       PetscSNPrintf(name,PETSC_MAX_PATH_LEN,"%s.%d",mname,rank);
517:       PetscFixFilename(name,fname);
518:       file = fopen(fname,"w");
519:       if (!file) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname);
520:     } else file = PETSC_STDOUT;
521:     PetscLogTraceBegin(file);
522:   }

524:   PetscOptionsGetViewer(comm,NULL,"-log_view",NULL,&format,&flg4);
525:   if (flg4) {
526:     if (format == PETSC_VIEWER_ASCII_XML) {
527:       PetscLogNestedBegin();
528:     } else {
529:       PetscLogDefaultBegin();
530:     }
531:   }
532:   if (flg4 && format == PETSC_VIEWER_ASCII_XML) {
533:     PetscReal threshold = PetscRealConstant(0.01);
534:     PetscOptionsGetReal(NULL,NULL,"-log_threshold",&threshold,&flg1);
535:     if (flg1) {PetscLogSetThreshold((PetscLogDouble)threshold,NULL);}
536:   }
537: #endif

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


542: #if defined(PETSC_HAVE_CUDA)
543:   PetscOptionsHasName(NULL,NULL,"-cuda_show_devices",&flg1);
544:   if (flg1) {
545:     struct cudaDeviceProp prop;
546:     int                   devCount;
547:     PetscInt              device;
548:     cudaError_t           err = cudaSuccess;

550:     err = cudaGetDeviceCount(&devCount);
551:     if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceCount %s",cudaGetErrorString(err));
552:     for (device = 0; device < devCount; ++device) {
553:       err = cudaGetDeviceProperties(&prop, (int)device);
554:       if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceProperties %s",cudaGetErrorString(err));
555:       PetscPrintf(comm, "CUDA device %D: %s\n", device, prop.name);
556:     }
557:   }
558:   if (!PetscCUDAInitialized) {
559:     PetscMPIInt size;
560:     MPI_Comm_size(comm,&size);
561:     if (size>1) {
562:       int         devCount;
563:       PetscInt    device;
564:       PetscMPIInt rank;
565:       cudaError_t err = cudaSuccess;

567:       /* check to see if we force multiple ranks to hit the same GPU */
568:       PetscOptionsGetInt(NULL,NULL,"-cuda_set_device", &device, &flg1);
569:       if (flg1) {
570:         err = cudaSetDevice((int)device);
571:         if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDevice %s",cudaGetErrorString(err));
572:       } else {
573:         /* we're not using the same GPU on multiple MPI threads. So try to allocated different   GPUs to different processes */

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

579:         /* next determine the rank and then set the device via a mod */
580:         MPI_Comm_rank(comm,&rank);
581:         device = rank % devCount;
582:         err    = cudaSetDevice((int)device);
583:         if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDevice %s",cudaGetErrorString(err));
584:       }

586:       /* set the device flags so that it can map host memory ... do NOT throw exception on err!=cudaSuccess
587:        multiple devices may try to set the flags on the same device. So long as one of them succeeds, things
588:        are ok. */
589:       err = cudaSetDeviceFlags(cudaDeviceMapHost);
590:       if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDeviceFlags %s",cudaGetErrorString(err));
591:     } else {
592:       PetscInt    device;
593:       cudaError_t err = cudaSuccess;

595:       /* the code below works for serial GPU simulations */
596:       PetscOptionsGetInt(NULL,NULL,"-cuda_set_device", &device, &flg1);
597:       if (flg1) {
598:         err = cudaSetDevice((int)device);
599:         if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDevice %s",cudaGetErrorString(err));
600:       }

602:       /* set the device flags so that it can map host memory ... here, we error check. */
603:       err = cudaSetDeviceFlags(cudaDeviceMapHost);
604:       if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDeviceFlags %s",cudaGetErrorString(err));
605:     }

607:     PetscCUDAInitialized = PETSC_TRUE;
608:   }
609: #endif


612:   /*
613:        Print basic help message
614:   */
615:   PetscOptionsHasName(NULL,NULL,"-help",&flg1);
616:   if (flg1) {
617:     (*PetscHelpPrintf)(comm,"Options for all PETSc programs:\n");
618:     (*PetscHelpPrintf)(comm," -help: prints help method for each option\n");
619:     (*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is detected. Useful \n ");
620:     (*PetscHelpPrintf)(comm,"       only when run in the debugger\n");
621:     (*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
622:     (*PetscHelpPrintf)(comm,"       start the debugger in new xterm\n");
623:     (*PetscHelpPrintf)(comm,"       unless noxterm is given\n");
624:     (*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
625:     (*PetscHelpPrintf)(comm,"       start all processes in the debugger\n");
626:     (*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n");
627:     (*PetscHelpPrintf)(comm,"    emacs jumps to error file\n");
628:     (*PetscHelpPrintf)(comm," -debugger_nodes [n1,n2,..] Nodes to start in debugger\n");
629:     (*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n");
630:     (*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n");
631:     (*PetscHelpPrintf)(comm,"                      waits the delay for you to attach\n");
632:     (*PetscHelpPrintf)(comm," -display display: Location where X window graphics and debuggers are displayed\n");
633:     (*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n");
634:     (*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n");
635:     (*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n");
636:     (*PetscHelpPrintf)(comm,"           note on IBM RS6000 this slows run greatly\n");
637:     (*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n");
638:     (*PetscHelpPrintf)(comm," -malloc: use our error checking malloc\n");
639:     (*PetscHelpPrintf)(comm," -malloc no: don't use error checking malloc\n");
640:     (*PetscHelpPrintf)(comm," -malloc_info: prints total memory usage\n");
641:     (*PetscHelpPrintf)(comm," -malloc_log: keeps log of all memory allocations\n");
642:     (*PetscHelpPrintf)(comm," -malloc_debug: enables extended checking for memory corruption\n");
643:     (*PetscHelpPrintf)(comm," -options_table: dump list of options inputted\n");
644:     (*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n");
645:     (*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n");
646:     (*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n");
647:     (*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n");
648:     (*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has separate tmp directory\n");
649:     (*PetscHelpPrintf)(comm," -memory_view: print memory usage at end of run\n");
650: #if defined(PETSC_USE_LOG)
651:     (*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n");
652:     (*PetscHelpPrintf)(comm," -log_view [:filename:[format]]: logging objects and events\n");
653:     (*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n");
654: #if defined(PETSC_HAVE_MPE)
655:     (*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through Jumpshot\n");
656: #endif
657:     (*PetscHelpPrintf)(comm," -info <optional filename>: print informative messages about the calculations\n");
658: #endif
659:     (*PetscHelpPrintf)(comm," -v: prints PETSc version number and release date\n");
660:     (*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n");
661:     (*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n");
662:     (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
663:   }

665: #if defined(PETSC_HAVE_POPEN)
666:   {
667:   char machine[128];
668:   PetscOptionsGetString(NULL,NULL,"-popen_machine",machine,128,&flg1);
669:   if (flg1) {
670:     PetscPOpenSetMachine(machine);
671:   }
672:   }
673: #endif

675:   PetscOptionsGetReal(NULL,NULL,"-petsc_sleep",&si,&flg1);
676:   if (flg1) {
677:     PetscSleep(si);
678:   }

680:   PetscOptionsGetString(NULL,NULL,"-info_exclude",mname,PETSC_MAX_PATH_LEN,&flg1);
681:   if (flg1) {
682:     PetscStrstr(mname,"null",&f);
683:     if (f) {
684:       PetscInfoDeactivateClass(0);
685:     }
686:   }

688: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_VECCUDA)
689:   PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);
690:   if (!flg3) {
691:     PetscOptionsHasName(NULL,NULL,"-log_view",&flg3);
692:   }
693: #endif
694: #if defined(PETSC_HAVE_VIENNACL)
695:   PetscOptionsGetBool(NULL,NULL,"-viennacl_synchronize",&flg3,NULL);
696:   PetscViennaCLSynchronize = flg3;
697: #endif
698: #if defined(PETSC_HAVE_VECCUDA)
699:   PetscOptionsGetBool(NULL,NULL,"-cuda_synchronize",&flg3,NULL);
700:   PetscCUDASynchronize = flg3;
701: #endif

703: #if defined(PETSC_HAVE_VIENNACL)
704:   PetscViennaCLInit();
705: #endif

707:   return(0);
708: }