Actual source code: init.c

petsc-master 2017-01-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>

 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: #if defined(PETSC_HAVE_MATLAB_ENGINE)
 87: PetscErrorCode (*PetscVFPrintf)(FILE*,const char[],va_list)    = PetscVFPrintf_Matlab;
 88: #else
 89: PetscErrorCode (*PetscVFPrintf)(FILE*,const char[],va_list)    = PetscVFPrintfDefault;
 90: #endif
 91: /*
 92:   This is needed to turn on/off GPU synchronization
 93: */
 94: PetscBool PetscCUSPSynchronize = PETSC_FALSE;
 95: PetscBool PetscViennaCLSynchronize = PETSC_FALSE;
 96: PetscBool PetscCUDASynchronize = PETSC_FALSE;

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

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

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

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

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

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

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

144: PetscErrorCode  PetscCloseHistoryFile(FILE **fd)
145: {
147:   PetscMPIInt    rank;
148:   char           date[64];
149:   int            err;

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

166: /* ------------------------------------------------------------------------------*/

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

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

177: void Petsc_MPI_AbortOnError(MPI_Comm *comm,PetscMPIInt *flag)
178: {
180:   (*PetscErrorPrintf)("MPI error %d\n",*flag);
181:   abort();
182: }

184: void Petsc_MPI_DebuggerOnError(MPI_Comm *comm,PetscMPIInt *flag)
185: {

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

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

198:    Collective on PETSC_COMM_WORLD

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

202:    Level: advanced

204:    Note:
205:    See PetscInitialize() for more general runtime options.

207: .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscFinalize()
208: @*/
209: PetscErrorCode  PetscEnd(void)
210: {
212:   PetscFinalize();
213:   exit(0);
214:   return 0;
215: }

217: PetscBool PetscOptionsPublish = PETSC_FALSE;
218: extern PetscErrorCode PetscSetUseTrMalloc_Private(void);
219: extern PetscBool      petscsetmallocvisited;
220: static char           emacsmachinename[256];

222: PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = 0;
223: PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = 0;

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

230:    Input Parameter:
231: +  help - the help function (may be NULL)
232: -  version - the version function (may be NULL)

234:    Level: developer

236:    Concepts: package help message

238: @*/
239: PetscErrorCode  PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm),PetscErrorCode (*version)(MPI_Comm))
240: {
242:   PetscExternalHelpFunction    = help;
243:   PetscExternalVersionFunction = version;
244:   return(0);
245: }

247: #if defined(PETSC_USE_LOG)
248: extern PetscBool   PetscObjectsLog;
249: #endif

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

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

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

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

328:   /*
329:       Set the display variable for graphics
330:   */
331:   PetscSetDisplay();

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

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

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

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

370:   /*
371:       Setup the error handling
372:   */
373:   flg1 = PETSC_FALSE;
374:   PetscOptionsGetBool(NULL,NULL,"-on_error_abort",&flg1,NULL);
375:   if (flg1) {
376:     MPI_Comm_set_errhandler(PETSC_COMM_WORLD,MPI_ERRORS_ARE_FATAL);
377:     PetscPushErrorHandler(PetscAbortErrorHandler,0);
378:   }
379:   flg1 = PETSC_FALSE;
380:   PetscOptionsGetBool(NULL,NULL,"-on_error_mpiabort",&flg1,NULL);
381:   if (flg1) { PetscPushErrorHandler(PetscMPIAbortErrorHandler,0);}
382:   flg1 = PETSC_FALSE;
383:   PetscOptionsGetBool(NULL,NULL,"-mpi_return_on_error",&flg1,NULL);
384:   if (flg1) {
385:     MPI_Comm_set_errhandler(comm,MPI_ERRORS_RETURN);
386:   }
387:   flg1 = PETSC_FALSE;
388:   PetscOptionsGetBool(NULL,NULL,"-no_signal_handler",&flg1,NULL);
389:   if (!flg1) {PetscPushSignalHandler(PetscSignalHandlerDefault,(void*)0);}
390:   flg1 = PETSC_FALSE;
391:   PetscOptionsGetBool(NULL,NULL,"-fp_trap",&flg1,NULL);
392:   if (flg1) {PetscSetFPTrap(PETSC_FP_TRAP_ON);}
393:   PetscOptionsGetInt(NULL,NULL,"-check_pointer_intensity",&intensity,&flag);

396:   /*
397:       Setup debugger information
398:   */
399:   PetscSetDefaultDebugger();
400:   PetscOptionsGetString(NULL,NULL,"-on_error_attach_debugger",string,64,&flg1);
401:   if (flg1) {
402:     MPI_Errhandler err_handler;

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

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

464:   /*
465:         Setup profiling and logging
466:   */
467: #if defined(PETSC_USE_INFO)
468:   {
469:     char logname[PETSC_MAX_PATH_LEN]; logname[0] = 0;
470:     PetscOptionsGetString(NULL,NULL,"-info",logname,250,&flg1);
471:     if (flg1 && logname[0]) {
472:       PetscInfoAllow(PETSC_TRUE,logname);
473:     } else if (flg1) {
474:       PetscInfoAllow(PETSC_TRUE,NULL);
475:     }
476:   }
477: #endif
478: #if defined(PETSC_USE_LOG)
479:   mname[0] = 0;
480:   PetscOptionsGetString(NULL,NULL,"-history",mname,PETSC_MAX_PATH_LEN,&flg1);
481:   if (flg1) {
482:     if (mname[0]) {
483:       PetscOpenHistoryFile(mname,&petsc_history);
484:     } else {
485:       PetscOpenHistoryFile(NULL,&petsc_history);
486:     }
487:   }
488: #if defined(PETSC_HAVE_MPE)
489:   flg1 = PETSC_FALSE;
490:   PetscOptionsHasName(NULL,NULL,"-log_mpe",&flg1);
491:   if (flg1) {PetscLogMPEBegin();}
492: #endif
493:   flg1 = PETSC_FALSE;
494:   flg3 = PETSC_FALSE;
495:   PetscOptionsGetBool(NULL,NULL,"-log_all",&flg1,NULL);
496:   PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);
497:   if (flg1)                      { PetscLogAllBegin(); }
498:   else if (flg3)                 { PetscLogDefaultBegin();}

500:   PetscOptionsGetString(NULL,NULL,"-log_trace",mname,250,&flg1);
501:   if (flg1) {
502:     char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN];
503:     FILE *file;
504:     if (mname[0]) {
505:       sprintf(name,"%s.%d",mname,rank);
506:       PetscFixFilename(name,fname);
507:       file = fopen(fname,"w");
508:       if (!file) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname);
509:     } else file = PETSC_STDOUT;
510:     PetscLogTraceBegin(file);
511:   }

513:   PetscOptionsGetViewer(PETSC_COMM_WORLD,NULL,"-log_view",NULL,&format,&flg4);
514:   if (flg4) {
515:     if (format == PETSC_VIEWER_ASCII_XML){
516:       PetscLogNestedBegin();
517:     } else {
518:       PetscLogDefaultBegin();
519:     }
520:   }
521: #endif

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

525: #if defined(PETSC_HAVE_CUDA)
526:   PetscOptionsHasName(NULL,NULL,"-cuda_show_devices",&flg1);
527:   if (flg1) {
528:     struct cudaDeviceProp prop;
529:     int                   devCount;
530:     PetscInt              device;
531:     cudaError_t           err = cudaSuccess;

533:     err = cudaGetDeviceCount(&devCount);
534:     if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceCount %s",cudaGetErrorString(err));
535:     for (device = 0; device < devCount; ++device) {
536:       err = cudaGetDeviceProperties(&prop, (int)device);
537:       if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaGetDeviceProperties %s",cudaGetErrorString(err));
538:       PetscPrintf(PETSC_COMM_WORLD, "CUDA device %D: %s\n", device, prop.name);
539:     }
540:   }
541:   if (!PetscCUDAInitialized) {
542:     PetscMPIInt size;
543:     MPI_Comm_size(PETSC_COMM_WORLD,&size);
544:     if (size>1) {
545:       int         devCount;
546:       PetscInt    device;
547:       PetscMPIInt rank;
548:       cudaError_t err = cudaSuccess;

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

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

562:         /* next determine the rank and then set the device via a mod */
563:         MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
564:         device = rank % devCount;
565:         err    = cudaSetDevice((int)device);
566:         if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDevice %s",cudaGetErrorString(err));
567:       }

569:       /* set the device flags so that it can map host memory ... do NOT throw exception on err!=cudaSuccess
570:        multiple devices may try to set the flags on the same device. So long as one of them succeeds, things
571:        are ok. */
572:       err = cudaSetDeviceFlags(cudaDeviceMapHost);
573:       if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDeviceFlags %s",cudaGetErrorString(err));
574:     } else {
575:       PetscInt    device;
576:       cudaError_t err = cudaSuccess;

578:       /* the code below works for serial GPU simulations */
579:       PetscOptionsGetInt(NULL,NULL,"-cuda_set_device", &device, &flg1);
580:       if (flg1) {
581:         err = cudaSetDevice((int)device);
582:         if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDevice %s",cudaGetErrorString(err));
583:       }

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

590:     PetscCUDAInitialized = PETSC_TRUE;
591:   }
592: #endif


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

648: #if defined(PETSC_HAVE_POPEN)
649:   {
650:   char machine[128];
651:   PetscOptionsGetString(NULL,NULL,"-popen_machine",machine,128,&flg1);
652:   if (flg1) {
653:     PetscPOpenSetMachine(machine);
654:   }
655:   }
656: #endif

658:   PetscOptionsGetReal(NULL,NULL,"-petsc_sleep",&si,&flg1);
659:   if (flg1) {
660:     PetscSleep(si);
661:   }

663:   PetscOptionsGetString(NULL,NULL,"-info_exclude",mname,PETSC_MAX_PATH_LEN,&flg1);
664:   if (flg1) {
665:     PetscStrstr(mname,"null",&f);
666:     if (f) {
667:       PetscInfoDeactivateClass(0);
668:     }
669:   }

671: #if defined(PETSC_HAVE_CUSP) || defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_VECCUDA)
672:   PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);
673:   if (!flg3) {
674:   PetscOptionsHasName(NULL,NULL,"-log_view",&flg3);
675:   }
676: #endif
677: #if defined(PETSC_HAVE_CUSP)
678:   PetscOptionsGetBool(NULL,NULL,"-cusp_synchronize",&flg3,NULL);
679:   PetscCUSPSynchronize = flg3;
680: #elif defined(PETSC_HAVE_VIENNACL)
681:   PetscOptionsGetBool(NULL,NULL,"-viennacl_synchronize",&flg3,NULL);
682:   PetscViennaCLSynchronize = flg3;
683: #elif defined(PETSC_HAVE_VECCUDA)
684:   PetscOptionsGetBool(NULL,NULL,"-cuda_synchronize",&flg3,NULL);
685:   PetscCUDASynchronize = flg3;
686: #endif

688: #if defined(PETSC_HAVE_VIENNACL)
689:   PetscViennaCLInit();
690: #endif

692:   return(0);
693: }