Actual source code: init.c

      1: #define PETSC_DLL
      2: /*

      4:    This file defines part of the initialization of PETSc

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

     10:  #include petsc.h
     11:  #include petscsys.h
     12: #if defined(PETSC_HAVE_STDLIB_H)
     13: #include <stdlib.h>
     14: #endif
     15: #if defined(PETSC_HAVE_MALLOC_H)
     16: #include <malloc.h>
     17: #endif
     18: #include "petscfix.h"

     20: /* ------------------------Nasty global variables -------------------------------*/
     21: /*
     22:      Indicates if PETSc started up MPI, or it was
     23:    already started before PETSc was initialized.
     24: */
     25: PetscTruth  PETSC_DLLEXPORT PetscBeganMPI         = PETSC_FALSE;
     26: PetscTruth  PETSC_DLLEXPORT PetscInitializeCalled = PETSC_FALSE;
     27: PetscTruth  PETSC_DLLEXPORT PetscFinalizeCalled   = PETSC_FALSE;
     28: PetscMPIInt PETSC_DLLEXPORT PetscGlobalRank = -1;
     29: PetscMPIInt PETSC_DLLEXPORT PetscGlobalSize = -1;

     31: #if defined(PETSC_USE_COMPLEX)
     32: #if defined(PETSC_COMPLEX_INSTANTIATE)
     33: template <> class std::complex<double>; /* instantiate complex template class */
     34: #endif
     35: MPI_Datatype  PETSC_DLLEXPORT MPIU_COMPLEX;
     36: PetscScalar   PETSC_DLLEXPORT PETSC_i;
     37: #else
     38: PetscScalar   PETSC_DLLEXPORT PETSC_i = 0.0;
     39: #endif
     40: MPI_Datatype  PETSC_DLLEXPORT MPIU_2SCALAR = 0;
     41: MPI_Datatype  PETSC_DLLEXPORT MPIU_2INT = 0;
     42: /*
     43:      These are needed by petscbt.h
     44: */
     45: char     PETSC_DLLEXPORT _BT_mask = ' ';
     46: char     PETSC_DLLEXPORT _BT_c = ' ';
     47: PetscInt PETSC_DLLEXPORT _BT_idx  = 0;

     49: /*
     50:        Function that is called to display all error messages
     51: */
     52: EXTERN PetscErrorCode  PetscErrorPrintfDefault(const char [],...);
     53: EXTERN PetscErrorCode  PetscHelpPrintfDefault(MPI_Comm,const char [],...);
     54: PetscErrorCode PETSC_DLLEXPORT (*PetscErrorPrintf)(const char [],...)          = PetscErrorPrintfDefault;
     55: PetscErrorCode PETSC_DLLEXPORT (*PetscHelpPrintf)(MPI_Comm,const char [],...)  = PetscHelpPrintfDefault;

     57: /* ------------------------------------------------------------------------------*/
     58: /*
     59:    Optional file where all PETSc output from various prints is saved
     60: */
     61: FILE *petsc_history = PETSC_NULL;

     65: PetscErrorCode PETSC_DLLEXPORT PetscLogOpenHistoryFile(const char filename[],FILE **fd)
     66: {
     68:   PetscMPIInt    rank,size;
     69:   char           pfile[PETSC_MAX_PATH_LEN],pname[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN],date[64];
     70:   char           version[256];

     73:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
     74:   if (!rank) {
     75:     char arch[10];
     76:     PetscGetArchType(arch,10);
     77:     PetscGetDate(date,64);
     78:     PetscGetVersion(&version);
     79:     MPI_Comm_size(PETSC_COMM_WORLD,&size);
     80:     if (filename) {
     81:       PetscFixFilename(filename,fname);
     82:     } else {
     83:       PetscGetHomeDirectory(pfile,240);
     84:       PetscStrcat(pfile,"/.petschistory");
     85:       PetscFixFilename(pfile,fname);
     86:     }

     88:     *fd = fopen(fname,"a"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open file: %s",fname);
     89:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
     90:     PetscFPrintf(PETSC_COMM_SELF,*fd,"%s %s\n",version,date);
     91:     PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);
     92:     PetscFPrintf(PETSC_COMM_SELF,*fd,"%s on a %s, %d proc. with options:\n",pname,arch,size);
     93:     PetscOptionsPrint(*fd);
     94:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
     95:     fflush(*fd);
     96:   }
     97:   return(0);
     98: }

    102: PetscErrorCode PETSC_DLLEXPORT PetscLogCloseHistoryFile(FILE **fd)
    103: {
    105:   PetscMPIInt    rank;
    106:   char           date[64];

    109:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
    110:   if (!rank) {
    111:     PetscGetDate(date,64);
    112:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
    113:     PetscFPrintf(PETSC_COMM_SELF,*fd,"Finished at %s\n",date);
    114:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
    115:     fflush(*fd);
    116:     fclose(*fd);
    117:   }
    118:   return(0);
    119: }

    121: /* ------------------------------------------------------------------------------*/

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

    127:     This is so MPI errors in the debugger will leave all the stack
    128:   frames. The default abort cleans up and exits.
    129: */

    133: void Petsc_MPI_AbortOnError(MPI_Comm *comm,PetscMPIInt *flag)
    134: {
    136:   (*PetscErrorPrintf)("MPI error %d\n",(int)*flag);
    137:   abort();
    138: }

    142: void Petsc_MPI_DebuggerOnError(MPI_Comm *comm,PetscMPIInt *flag)
    143: {

    147:   (*PetscErrorPrintf)("MPI error %d\n",(int)*flag);
    148:   PetscAttachDebugger();
    149:   if (ierr) { /* hopeless so get out */
    150:     MPI_Finalize();
    151:     exit(*flag);
    152:   }
    153: }

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

    161:    Collective on PETSC_COMM_WORLD

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

    165:    Level: advanced

    167:    Note:
    168:    See PetscInitialize() for more general runtime options.

    170: .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscFinalize()
    171: @*/
    172: PetscErrorCode PETSC_DLLEXPORT PetscEnd(void)
    173: {
    175:   PetscFinalize();
    176:   exit(0);
    177:   return 0;
    178: }

    180: PetscTruth   PetscOptionsPublish = PETSC_FALSE;
    181: EXTERN PetscErrorCode        PetscSetUseTrMalloc_Private(void);
    183: static char       emacsmachinename[256];

    185: PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = 0;
    186: PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = 0;

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

    195:    Input Parameter:
    196: +  help - the help function (may be PETSC_NULL)
    197: -  version - the version function (may be PETSc null)

    199:    Level: developer

    201:    Concepts: package help message

    203: @*/
    204: PetscErrorCode PETSC_DLLEXPORT PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm),PetscErrorCode (*version)(MPI_Comm))
    205: {
    207:   PetscExternalHelpFunction    = help;
    208:   PetscExternalVersionFunction = version;
    209:   return(0);
    210: }

    214: PetscErrorCode PETSC_DLLEXPORT PetscOptionsCheckInitial_Private(void)
    215: {
    216:   char           string[64],mname[PETSC_MAX_PATH_LEN],*f;
    217:   MPI_Comm       comm = PETSC_COMM_WORLD;
    218:   PetscTruth     flg1,flg2,flg3,flag;
    220:   PetscInt       si;
    221:   int            i;
    222:   PetscMPIInt    rank;
    223:   char           version[256];

    226:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

    228:   /*
    229:       Setup the memory management; support for tracing malloc() usage
    230:   */
    231:   PetscOptionsHasName(PETSC_NULL,"-malloc_log",&flg3);
    232: #if defined(PETSC_USE_DEBUG)
    233:   PetscOptionsGetTruth(PETSC_NULL,"-malloc",&flg1,&flg2);
    234:   if ((!flg2 || flg1) && !petscsetmallocvisited) {
    235:     PetscSetUseTrMalloc_Private();
    236:   }
    237: #else
    238:   PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);
    239:   PetscOptionsHasName(PETSC_NULL,"-malloc",&flg2);
    240:   if (flg1 || flg2 || flg3) {PetscSetUseTrMalloc_Private();}
    241: #endif
    242:   if (flg3) {
    243:     PetscMallocSetDumpLog();
    244:   }
    245:   PetscOptionsHasName(PETSC_NULL,"-malloc_debug",&flg1);
    246:   if (flg1) {
    247:     PetscSetUseTrMalloc_Private();
    248:     PetscMallocDebug(PETSC_TRUE);
    249:   }

    251:   PetscOptionsHasName(PETSC_NULL,"-memory_info",&flg1);
    252:   if (flg1) {
    253:     PetscMemorySetGetMaximumUsage();
    254:   }

    256:   /*
    257:       Set the display variable for graphics
    258:   */
    259:   PetscSetDisplay();

    261:   /*
    262:       Print the PETSc version information
    263:   */
    264:   PetscOptionsHasName(PETSC_NULL,"-v",&flg1);
    265:   PetscOptionsHasName(PETSC_NULL,"-version",&flg2);
    266:   PetscOptionsHasName(PETSC_NULL,"-help",&flg3);
    267:   if (flg1 || flg2 || flg3){

    269:     /*
    270:        Print "higher-level" package version message
    271:     */
    272:     if (PetscExternalVersionFunction) {
    273:       (*PetscExternalVersionFunction)(comm);
    274:     }

    276:     PetscGetVersion(&version);
    277:     (*PetscHelpPrintf)(comm,"--------------------------------------------\
    278: ------------------------------\n");
    279:     (*PetscHelpPrintf)(comm,"%s\n",version);
    280:     (*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO);
    281:     (*PetscHelpPrintf)(comm,"See docs/copyright.html for copyright information\n");
    282:     (*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n");
    283:     (*PetscHelpPrintf)(comm,"See docs/troubleshooting.html for problems.\n");
    284:     (*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n");
    285:     (*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR);
    286:     (*PetscHelpPrintf)(comm,"--------------------------------------------\
    287: ------------------------------\n");
    288:   }

    290:   /*
    291:        Print "higher-level" package help message
    292:   */
    293:   if (flg3){
    294:     if (PetscExternalHelpFunction) {
    295:       (*PetscExternalHelpFunction)(comm);
    296:     }
    297:   }

    299:   /*
    300:       Setup the error handling
    301:   */
    302:   PetscOptionsHasName(PETSC_NULL,"-fp_trap",&flg1);
    303:   if (flg1) { PetscSetFPTrap(PETSC_FP_TRAP_ON); }
    304:   PetscOptionsHasName(PETSC_NULL,"-on_error_abort",&flg1);
    305:   if (flg1) { PetscPushErrorHandler(PetscAbortErrorHandler,0);CHKERRQ(ierr)}
    306:   PetscOptionsHasName(PETSC_NULL,"-on_error_stop",&flg1);
    307:   if (flg1) { PetscPushErrorHandler(PetscStopErrorHandler,0);CHKERRQ(ierr)}
    308:   PetscOptionsHasName(PETSC_NULL,"-mpi_return_on_error",&flg1);
    309:   if (flg1) {
    310:     MPI_Errhandler_set(comm,MPI_ERRORS_RETURN);
    311:   }
    312:   PetscOptionsHasName(PETSC_NULL,"-no_signal_handler",&flg1);
    313:   if (!flg1) { PetscPushSignalHandler(PetscDefaultSignalHandler,(void*)0);CHKERRQ(ierr) }

    315:   /*
    316:       Setup debugger information
    317:   */
    318:   PetscSetDefaultDebugger();
    319:   PetscOptionsGetString(PETSC_NULL,"-on_error_attach_debugger",string,64,&flg1);
    320:   if (flg1) {
    321:     MPI_Errhandler err_handler;

    323:     PetscSetDebuggerFromString(string);
    324:     MPI_Errhandler_create((MPI_Handler_function*)Petsc_MPI_DebuggerOnError,&err_handler);
    325:     MPI_Errhandler_set(comm,err_handler);
    326:     PetscPushErrorHandler(PetscAttachDebuggerErrorHandler,0);
    327:   }
    328:   PetscOptionsGetString(PETSC_NULL,"-start_in_debugger",string,64,&flg1);
    329:   PetscOptionsGetString(PETSC_NULL,"-stop_for_debugger",string,64,&flg2);
    330:   if (flg1 || flg2) {
    331:     PetscMPIInt    size;
    332:     PetscInt       lsize,*nodes;
    333:     MPI_Errhandler err_handler;
    334:     /*
    335:        we have to make sure that all processors have opened
    336:        connections to all other processors, otherwise once the
    337:        debugger has stated it is likely to receive a SIGUSR1
    338:        and kill the program.
    339:     */
    340:     MPI_Comm_size(PETSC_COMM_WORLD,&size);
    341:     if (size > 2) {
    342:       PetscMPIInt dummy;
    343:       MPI_Status  status;
    344:       for (i=0; i<size; i++) {
    345:         if (rank != i) {
    346:           MPI_Send(&dummy,1,MPI_INT,i,109,PETSC_COMM_WORLD);
    347:         }
    348:       }
    349:       for (i=0; i<size; i++) {
    350:         if (rank != i) {
    351:           MPI_Recv(&dummy,1,MPI_INT,i,109,PETSC_COMM_WORLD,&status);
    352:         }
    353:       }
    354:     }
    355:     /* check if this processor node should be in debugger */
    356:     PetscMalloc(size*sizeof(PetscInt),&nodes);
    357:     lsize = size;
    358:     PetscOptionsGetIntArray(PETSC_NULL,"-debugger_nodes",nodes,&lsize,&flag);
    359:     if (flag) {
    360:       for (i=0; i<lsize; i++) {
    361:         if (nodes[i] == rank) { flag = PETSC_FALSE; break; }
    362:       }
    363:     }
    364:     if (!flag) {
    365:       PetscSetDebuggerFromString(string);
    366:       PetscPushErrorHandler(PetscAbortErrorHandler,0);
    367:       if (flg1) {
    368:         PetscAttachDebugger();
    369:       } else {
    370:         PetscStopForDebugger();
    371:       }
    372:       MPI_Errhandler_create((MPI_Handler_function*)Petsc_MPI_AbortOnError,&err_handler);
    373:       MPI_Errhandler_set(comm,err_handler);
    374:     }
    375:     PetscFree(nodes);
    376:   }

    378:   PetscOptionsGetString(PETSC_NULL,"-on_error_emacs",emacsmachinename,128,&flg1);
    379:   if (flg1 && !rank) {PetscPushErrorHandler(PetscEmacsClientErrorHandler,emacsmachinename);CHKERRQ(ierr)}

    381:   /*
    382:         Setup profiling and logging
    383:   */
    384: #if defined(PETSC_USE_LOG)
    385:   mname[0] = 0;
    386:   PetscOptionsGetString(PETSC_NULL,"-log_history",mname,PETSC_MAX_PATH_LEN,&flg1);
    387:   if (flg1) {
    388:     if (mname[0]) {
    389:       PetscLogOpenHistoryFile(mname,&petsc_history);
    390:     } else {
    391:       PetscLogOpenHistoryFile(0,&petsc_history);
    392:     }
    393:   }
    394:   PetscOptionsHasName(PETSC_NULL,"-log_info",&flg1);
    395:   if (flg1) {
    396:     char logname[PETSC_MAX_PATH_LEN]; logname[0] = 0;
    397:     PetscOptionsGetString(PETSC_NULL,"-log_info",logname,250,&flg1);
    398:     if (logname[0]) {
    399:       PetscLogInfoAllow(PETSC_TRUE,logname);
    400:     } else {
    401:       PetscLogInfoAllow(PETSC_TRUE,PETSC_NULL);
    402:     }
    403:   }
    404: #if defined(PETSC_HAVE_MPE)
    405:   PetscOptionsHasName(PETSC_NULL,"-log_mpe",&flg1);
    406:   if (flg1) PetscLogMPEBegin();
    407: #endif
    408:   PetscOptionsHasName(PETSC_NULL,"-log_all",&flg1);
    409:   PetscOptionsHasName(PETSC_NULL,"-log",&flg2);
    410:   PetscOptionsHasName(PETSC_NULL,"-log_summary",&flg3);
    411:   if (flg1)              {  PetscLogAllBegin(); }
    412:   else if (flg2 || flg3) {  PetscLogBegin();}
    413:
    414:   PetscOptionsGetString(PETSC_NULL,"-log_trace",mname,250,&flg1);
    415:   if (flg1) {
    416:     char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN];
    417:     FILE *file;
    418:     if (mname[0]) {
    419:       sprintf(name,"%s.%d",mname,rank);
    420:       PetscFixFilename(name,fname);
    421:       file = fopen(fname,"w");
    422:       if (!file) {
    423:         SETERRQ1(PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname);
    424:       }
    425:     } else {
    426:       file = stdout;
    427:     }
    428:     PetscLogTraceBegin(file);
    429:   }
    430: #endif

    432:   /*
    433:       Setup building of stack frames for all function calls
    434:   */
    435: #if defined(PETSC_USE_DEBUG)
    436:   PetscStackCreate();
    437: #endif

    440:   /*
    441:        Print basic help message
    442:   */
    443:   PetscOptionsHasName(PETSC_NULL,"-help",&flg1);
    444:   if (flg1) {
    445:     (*PetscHelpPrintf)(comm,"Options for all PETSc programs:\n");
    446:     (*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is");
    447:     (*PetscHelpPrintf)(comm," detected. Useful \n       only when run in the debugger\n");
    448:     (*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
    449:     (*PetscHelpPrintf)(comm,"       start the debugger in new xterm\n");
    450:     (*PetscHelpPrintf)(comm,"       unless noxterm is given\n");
    451:     (*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
    452:     (*PetscHelpPrintf)(comm,"       start all processes in the debugger\n");
    453:     (*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n");
    454:     (*PetscHelpPrintf)(comm,"    emacs jumps to error file\n");
    455:     (*PetscHelpPrintf)(comm," -debugger_nodes [n1,n2,..] Nodes to start in debugger\n");
    456:     (*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n");
    457:     (*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n");
    458:     (*PetscHelpPrintf)(comm,"                      waits the delay for you to attach\n");
    459:     (*PetscHelpPrintf)(comm," -display display: Location where graphics and debuggers are displayed\n");
    460:     (*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n");
    461:     (*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n");
    462:     (*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n");
    463:     (*PetscHelpPrintf)(comm,"           note on IBM RS6000 this slows run greatly\n");
    464:     (*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n");
    465:     (*PetscHelpPrintf)(comm," -malloc: use our error checking malloc\n");
    466:     (*PetscHelpPrintf)(comm," -malloc no: don't use error checking malloc\n");
    467:     (*PetscHelpPrintf)(comm," -mallocinfo: prints total memory usage\n");
    468:     (*PetscHelpPrintf)(comm," -malloc_debug: enables extended checking for memory corruption\n");
    469:     (*PetscHelpPrintf)(comm," -options_table: dump list of options inputted\n");
    470:     (*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n");
    471:     (*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n");
    472:     (*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n");
    473:     (*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n");
    474:     (*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has seperate tmp directory\n");
    475:     (*PetscHelpPrintf)(comm," -memory_info: print memory usage at end of run\n");
    476: #if defined(PETSC_USE_LOG)
    477:     (*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n");
    478:     (*PetscHelpPrintf)(comm," -log[_all _summary]: logging objects and events\n");
    479:     (*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n");
    480: #if defined(PETSC_HAVE_MPE)
    481:     (*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through upshot\n");
    482: #endif
    483:     (*PetscHelpPrintf)(comm," -log_info <optional filename>: print informative messages about the calculations\n");
    484: #endif
    485:     (*PetscHelpPrintf)(comm," -v: prints PETSc version number and release date\n");
    486:     (*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n");
    487:     (*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n");
    488:     (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
    489:   }

    491:   PetscOptionsGetInt(PETSC_NULL,"-petsc_sleep",&si,&flg1);
    492:   if (flg1) {
    493:     PetscSleep(si);
    494:   }

    496:   PetscOptionsGetString(PETSC_NULL,"-log_info_exclude",mname,PETSC_MAX_PATH_LEN,&flg1);
    497:   PetscStrstr(mname,"null",&f);
    498:   if (f) {
    499:     PetscLogInfoDeactivateClass(PETSC_NULL);
    500:   }

    502:   return(0);
    503: }