Actual source code: init.c

petsc-master 2016-07-22
Report Typos and Errors
  1: /*

  3:    This file defines part of the initialization of PETSc

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

  9: #include <petscsys.h>        /*I  "petscsys.h"   I*/
 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: #endif
 72: MPI_Datatype MPIU_2SCALAR = 0;
 73: #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
 74: MPI_Datatype MPIU_2INT = 0;
 75: #endif
 76: MPI_Datatype MPIU_BOOL;
 77: MPI_Datatype MPIU_ENUM;

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

 96: /* ------------------------------------------------------------------------------*/
 97: /*
 98:    Optional file where all PETSc output from various prints is saved
 99: */
100: 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: }

146: 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: */

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

190: void Petsc_MPI_DebuggerOnError(MPI_Comm *comm,PetscMPIInt *flag)
191: {

195:   (*PetscErrorPrintf)("MPI error %d\n",*flag);
196:   PetscAttachDebugger();
197:   if (ierr) MPI_Abort(*comm,*flag); /* hopeless so get out */
198: }

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

206:    Collective on PETSC_COMM_WORLD

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

210:    Level: advanced

212:    Note:
213:    See PetscInitialize() for more general runtime options.

215: .seealso: PetscInitialize(), PetscOptionsView(), PetscMallocDump(), PetscMPIDump(), PetscFinalize()
216: @*/
217: PetscErrorCode  PetscEnd(void)
218: {
220:   PetscFinalize();
221:   exit(0);
222:   return 0;
223: }

225: PetscBool PetscOptionsPublish = PETSC_FALSE;
226: extern PetscErrorCode PetscSetUseTrMalloc_Private(void);
227: extern PetscBool      petscsetmallocvisited;
228: static char           emacsmachinename[256];

230: PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = 0;
231: PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = 0;

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

240:    Input Parameter:
241: +  help - the help function (may be NULL)
242: -  version - the version function (may be NULL)

244:    Level: developer

246:    Concepts: package help message

248: @*/
249: PetscErrorCode  PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm),PetscErrorCode (*version)(MPI_Comm))
250: {
252:   PetscExternalHelpFunction    = help;
253:   PetscExternalVersionFunction = version;
254:   return(0);
255: }

257: #if defined(PETSC_USE_LOG)
258: extern PetscBool   PetscObjectsLog;
259: #endif

263: PetscErrorCode  PetscOptionsCheckInitial_Private(void)
264: {
265:   char              string[64],mname[PETSC_MAX_PATH_LEN],*f;
266:   MPI_Comm          comm = PETSC_COMM_WORLD;
267:   PetscBool         flg1 = PETSC_FALSE,flg2 = PETSC_FALSE,flg3 = PETSC_FALSE,flag;
268:   PetscErrorCode    ierr;
269:   PetscReal         si;
270:   PetscInt          intensity;
271:   int               i;
272:   PetscMPIInt       rank;
273:   char              version[256];
274: #if !defined(PETSC_HAVE_THREADSAFETY)
275:   PetscReal         logthreshold;
276: #endif
277: #if defined(PETSC_USE_LOG)
278:   PetscViewerFormat format;
279:   PetscBool         flg4 = PETSC_FALSE;
280: #endif
281: 
283:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

285: #if !defined(PETSC_HAVE_THREADSAFETY)
286:   /*
287:       Setup the memory management; support for tracing malloc() usage
288:   */
289:   PetscOptionsHasName(NULL,NULL,"-malloc_log",&flg3);
290:   logthreshold = 0.0;
291:   PetscOptionsGetReal(NULL,NULL,"-malloc_log_threshold",&logthreshold,&flg1);
292:   if (flg1) flg3 = PETSC_TRUE;
293: #if defined(PETSC_USE_DEBUG)
294:   PetscOptionsGetBool(NULL,NULL,"-malloc",&flg1,&flg2);
295:   if ((!flg2 || flg1) && !petscsetmallocvisited) {
296:     if (flg2 || !(PETSC_RUNNING_ON_VALGRIND)) {
297:       /* turn off default -malloc if valgrind is being used */
298:       PetscSetUseTrMalloc_Private();
299:     }
300:   }
301: #else
302:   PetscOptionsGetBool(NULL,NULL,"-malloc_dump",&flg1,NULL);
303:   PetscOptionsGetBool(NULL,NULL,"-malloc",&flg2,NULL);
304:   if (flg1 || flg2 || flg3) {PetscSetUseTrMalloc_Private();}
305: #endif
306:   if (flg3) {
307:     PetscMallocSetDumpLogThreshold((PetscLogDouble)logthreshold);
308:   }
309:   flg1 = PETSC_FALSE;
310:   PetscOptionsGetBool(NULL,NULL,"-malloc_debug",&flg1,NULL);
311:   if (flg1) {
312:     PetscSetUseTrMalloc_Private();
313:     PetscMallocDebug(PETSC_TRUE);
314:   }
315:   flg1 = PETSC_FALSE;
316:   PetscOptionsGetBool(NULL,NULL,"-malloc_test",&flg1,NULL);
317: #if defined(PETSC_USE_DEBUG)
318:   if (flg1 && !PETSC_RUNNING_ON_VALGRIND) {
319:     PetscSetUseTrMalloc_Private();
320:     PetscMallocSetDumpLog();
321:     PetscMallocDebug(PETSC_TRUE);
322:   }
323: #endif

325:   flg1 = PETSC_FALSE;
326:   PetscOptionsGetBool(NULL,NULL,"-malloc_info",&flg1,NULL);
327:   if (!flg1) {
328:     flg1 = PETSC_FALSE;
329:     PetscOptionsGetBool(NULL,NULL,"-memory_view",&flg1,NULL);
330:   }
331:   if (flg1) {
332:     PetscMemorySetGetMaximumUsage();
333:   }
334: #endif

336: #if defined(PETSC_USE_LOG)
337:   PetscOptionsHasName(NULL,NULL,"-objects_dump",&PetscObjectsLog);
338: #endif

340:   /*
341:       Set the display variable for graphics
342:   */
343:   PetscSetDisplay();

345:   /*
346:       Print the PETSc version information
347:   */
348:   PetscOptionsHasName(NULL,NULL,"-v",&flg1);
349:   PetscOptionsHasName(NULL,NULL,"-version",&flg2);
350:   PetscOptionsHasName(NULL,NULL,"-help",&flg3);
351:   if (flg1 || flg2 || flg3) {

353:     /*
354:        Print "higher-level" package version message
355:     */
356:     if (PetscExternalVersionFunction) {
357:       (*PetscExternalVersionFunction)(comm);
358:     }

360:     PetscGetVersion(version,256);
361:     (*PetscHelpPrintf)(comm,"--------------------------------------------\
362: ------------------------------\n");
363:     (*PetscHelpPrintf)(comm,"%s\n",version);
364:     (*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO);
365:     (*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n");
366:     (*PetscHelpPrintf)(comm,"See docs/faq.html for problems.\n");
367:     (*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n");
368:     (*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR);
369:     (*PetscHelpPrintf)(comm,"--------------------------------------------\
370: ------------------------------\n");
371:   }

373:   /*
374:        Print "higher-level" package help message
375:   */
376:   if (flg3) {
377:     if (PetscExternalHelpFunction) {
378:       (*PetscExternalHelpFunction)(comm);
379:     }
380:   }

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

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

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

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

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

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

525:   PetscOptionsGetViewer(PETSC_COMM_WORLD,NULL,"-log_view",NULL,&format,&flg4);
526:   if (flg4) {
527:     if (format == PETSC_VIEWER_ASCII_XML){
528:       PetscLogNestedBegin();
529:     } else {
530:       PetscLogDefaultBegin();
531:     }
532:   }
533: #endif

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

537: #if defined(PETSC_HAVE_CUDA)
538:   PetscOptionsHasName(NULL,NULL,"-cuda_show_devices",&flg1);
539:   if (flg1) {
540:     struct cudaDeviceProp prop;
541:     int                   devCount;
542:     PetscInt              device;
543:     cudaError_t           err = cudaSuccess;

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

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

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

574:         /* next determine the rank and then set the device via a mod */
575:         MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
576:         device = rank % devCount;
577:         err    = cudaSetDevice((int)device);
578:         if (err != cudaSuccess) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"error in cudaSetDevice %s",cudaGetErrorString(err));
579:       }

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

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

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

602:     PetscCUDAInitialized = PETSC_TRUE;
603:   }
604: #endif


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

660: #if defined(PETSC_HAVE_POPEN)
661:   {
662:   char machine[128];
663:   PetscOptionsGetString(NULL,NULL,"-popen_machine",machine,128,&flg1);
664:   if (flg1) {
665:     PetscPOpenSetMachine(machine);
666:   }
667:   }
668: #endif

670:   PetscOptionsGetReal(NULL,NULL,"-petsc_sleep",&si,&flg1);
671:   if (flg1) {
672:     PetscSleep(si);
673:   }

675:   PetscOptionsGetString(NULL,NULL,"-info_exclude",mname,PETSC_MAX_PATH_LEN,&flg1);
676:   if (flg1) {
677:     PetscStrstr(mname,"null",&f);
678:     if (f) {
679:       PetscInfoDeactivateClass(0);
680:     }
681:   }

683: #if defined(PETSC_HAVE_CUSP) || defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_VECCUDA)
684:   PetscOptionsHasName(NULL,NULL,"-log_summary",&flg3);
685:   if (!flg3) {
686:   PetscOptionsHasName(NULL,NULL,"-log_view",&flg3);
687:   }
688: #endif
689: #if defined(PETSC_HAVE_CUSP)
690:   PetscOptionsGetBool(NULL,NULL,"-cusp_synchronize",&flg3,NULL);
691:   PetscCUSPSynchronize = flg3;
692: #elif defined(PETSC_HAVE_VIENNACL)
693:   PetscOptionsGetBool(NULL,NULL,"-viennacl_synchronize",&flg3,NULL);
694:   PetscViennaCLSynchronize = flg3;
695: #elif defined(PETSC_HAVE_VECCUDA)
696:   PetscOptionsGetBool(NULL,NULL,"-cuda_synchronize",&flg3,NULL);
697:   PetscCUDASynchronize = flg3;
698: #endif

700: #if defined(PETSC_HAVE_VIENNACL)
701:   PetscViennaCLInit();
702: #endif

704:   return(0);
705: }