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"
 19:  #include zope.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   PetscBeganMPI         = PETSC_FALSE;
 26: PetscTruth   PetscInitializeCalled = PETSC_FALSE;
 27: PetscTruth   PetscFinalizeCalled   = PETSC_FALSE;
 28: PetscMPIInt  PetscGlobalRank = -1;
 29: PetscMPIInt  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   MPIU_COMPLEX;
 36: PetscScalar    PETSC_i;
 37: #else
 38: PetscScalar    PETSC_i = 0.0;
 39: #endif
 40: MPI_Datatype   MPIU_2SCALAR = 0;
 41: MPI_Datatype   MPIU_2INT = 0;
 42: /*
 43:      These are needed by petscbt.h
 44: */
 45:  #include petscbt.h
 46: char      _BT_mask = ' ';
 47: char      _BT_c = ' ';
 48: PetscInt  _BT_idx  = 0;

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

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

 65: PetscErrorCode  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:     int  err;

 78:     PetscGetArchType(arch,10);
 79:     PetscGetDate(date,64);
 80:     PetscGetVersion(version,256);
 81:     MPI_Comm_size(PETSC_COMM_WORLD,&size);
 82:     if (filename) {
 83:       PetscFixFilename(filename,fname);
 84:     } else {
 85:       PetscGetHomeDirectory(pfile,240);
 86:       PetscStrcat(pfile,"/.petschistory");
 87:       PetscFixFilename(pfile,fname);
 88:     }

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

105: PetscErrorCode  PetscLogCloseHistoryFile(FILE **fd)
106: {
108:   PetscMPIInt    rank;
109:   char           date[64];
110:   int            err;

113:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
114:   if (!rank) {
115:     PetscGetDate(date,64);
116:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
117:     PetscFPrintf(PETSC_COMM_SELF,*fd,"Finished at %s\n",date);
118:     PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
119:     err = fflush(*fd);
120:     if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
121:     err = fclose(*fd);
122:     if (err) SETERRQ(PETSC_ERR_SYS,"fclose() failed on file");
123:   }
124:   return(0);
125: }

127: /* ------------------------------------------------------------------------------*/

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

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

139: void Petsc_MPI_AbortOnError(MPI_Comm *comm,PetscMPIInt *flag)
140: {
142:   (*PetscErrorPrintf)("MPI error %d\n",(int)*flag);
143:   abort();
144: }

148: void Petsc_MPI_DebuggerOnError(MPI_Comm *comm,PetscMPIInt *flag)
149: {

153:   (*PetscErrorPrintf)("MPI error %d\n",(int)*flag);
154:   PetscAttachDebugger();
155:   if (ierr) { /* hopeless so get out */
156:     MPI_Finalize();
157:     exit(*flag);
158:   }
159: }

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

167:    Collective on PETSC_COMM_WORLD

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

171:    Level: advanced

173:    Note:
174:    See PetscInitialize() for more general runtime options.

176: .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscFinalize()
177: @*/
178: PetscErrorCode  PetscEnd(void)
179: {
181:   PetscFinalize();
182:   exit(0);
183:   return 0;
184: }

186: PetscTruth   PetscOptionsPublish = PETSC_FALSE;
187: EXTERN PetscErrorCode        PetscSetUseTrMalloc_Private(void);
189: static char       emacsmachinename[256];

191: PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = 0;
192: PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm)    = 0;

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

201:    Input Parameter:
202: +  help - the help function (may be PETSC_NULL)
203: -  version - the version function (may be PETSc null)

205:    Level: developer

207:    Concepts: package help message

209: @*/
210: PetscErrorCode  PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm),PetscErrorCode (*version)(MPI_Comm))
211: {
213:   PetscExternalHelpFunction    = help;
214:   PetscExternalVersionFunction = version;
215:   return(0);
216: }

220: PetscErrorCode  PetscOptionsCheckInitial_Private(void)
221: {
222:   char           string[64],mname[PETSC_MAX_PATH_LEN],*f;
223:   MPI_Comm       comm = PETSC_COMM_WORLD;
224:   PetscTruth     flg1,flg2,flg3,flag,flgz,flgzout;
226:   PetscInt       si;
227:   int            i;
228:   PetscMPIInt    rank;
229:   char           version[256];

232:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

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

257:   PetscOptionsHasName(PETSC_NULL,"-malloc_info",&flg1);
258:   if (!flg1) {
259:     PetscOptionsHasName(PETSC_NULL,"-memory_info",&flg1);
260:   }
261:   if (flg1) {
262:     PetscMemorySetGetMaximumUsage();
263:   }

265:   /*
266:       Set the display variable for graphics
267:   */
268:   PetscSetDisplay();

270:   /*
271:       Print the PETSc version information
272:   */
273:   PetscOptionsHasName(PETSC_NULL,"-v",&flg1);
274:   PetscOptionsHasName(PETSC_NULL,"-version",&flg2);
275:   PetscOptionsHasName(PETSC_NULL,"-help",&flg3);
276:   if (flg1 || flg2 || flg3){

278:     /*
279:        Print "higher-level" package version message 
280:     */
281:     if (PetscExternalVersionFunction) {
282:       (*PetscExternalVersionFunction)(comm);
283:     }

285:     PetscGetVersion(version,256);
286:     (*PetscHelpPrintf)(comm,"--------------------------------------------\
287: ------------------------------\n");
288:     (*PetscHelpPrintf)(comm,"%s\n",version);
289:     (*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO);
290:     (*PetscHelpPrintf)(comm,"See docs/copyright.html for copyright information\n");
291:     (*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n");
292:     (*PetscHelpPrintf)(comm,"See docs/troubleshooting.html for problems.\n");
293:     (*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n");
294:     (*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR);
295:     (*PetscHelpPrintf)(comm,"--------------------------------------------\
296: ------------------------------\n");
297:   }

299:   /*
300:        Print "higher-level" package help message 
301:   */
302:   if (flg3){
303:     if (PetscExternalHelpFunction) {
304:       (*PetscExternalHelpFunction)(comm);
305:     }
306:   }

308:   /*
309:       Setup the error handling
310:   */
311:   PetscOptionsHasName(PETSC_NULL,"-fp_trap",&flg1);
312:   if (flg1) { PetscSetFPTrap(PETSC_FP_TRAP_ON); }
313:   PetscOptionsHasName(PETSC_NULL,"-on_error_abort",&flg1);
314:   if (flg1) { PetscPushErrorHandler(PetscAbortErrorHandler,0);CHKERRQ(ierr)}
315:   PetscOptionsHasName(PETSC_NULL,"-on_error_mpiabort",&flg1);
316:   if (flg1) { PetscPushErrorHandler(PetscMPIAbortErrorHandler,0);CHKERRQ(ierr)}
317:   PetscOptionsHasName(PETSC_NULL,"-mpi_return_on_error",&flg1);
318:   if (flg1) {
319:     MPI_Errhandler_set(comm,MPI_ERRORS_RETURN);
320:   }
321:   PetscOptionsHasName(PETSC_NULL,"-no_signal_handler",&flg1);
322:   if (!flg1) { PetscPushSignalHandler(PetscDefaultSignalHandler,(void*)0);CHKERRQ(ierr) }

324:   /*
325:       Setup debugger information
326:   */
327:   PetscSetDefaultDebugger();
328:   PetscOptionsGetString(PETSC_NULL,"-on_error_attach_debugger",string,64,&flg1);
329:   if (flg1) {
330:     MPI_Errhandler err_handler;

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

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

390:   /*
391:     Activates new sockets for zope if needed
392:   */
393:   ierr=PetscOptionsHasName(PETSC_NULL,"-zope", &flgz);
394:   ierr=PetscOptionsHasName(PETSC_NULL,"-nostdout", &flgzout);
395:   if(flgz){
397:     int sockfd;
398:     char hostname[256];
399:     char username[256];
400:     int remoteport = 9999;
401:     ierr=PetscOptionsGetString(PETSC_NULL, "-zope", hostname, 256, &flgz);
402:     if(!hostname[0]){
403:       ierr=PetscGetHostName(hostname,256); }
404:     ierr=PetscOpenSocket(hostname, remoteport, &sockfd);
405:     PetscGetUserName(username, 256);
406:     PETSC_ZOPEFD = fdopen(sockfd, "w");
407:     if(flgzout){
408:       PETSC_STDOUT = PETSC_ZOPEFD;
409:       fprintf(PETSC_STDOUT, "<<<user>>> %s\n",username);
410:       fprintf(PETSC_STDOUT, "<<<start>>>");
411:     }
412:     else{
413:       fprintf(PETSC_ZOPEFD, "<<<user>>> %s\n",username);
414:       fprintf(PETSC_ZOPEFD, "<<<start>>>");
415:     }}

417:   /*
418:         Setup profiling and logging
419:   */
420: #if defined (PETSC_USE_INFO)
421:   PetscOptionsHasName(PETSC_NULL,"-info",&flg1);
422:   if (flg1) {
423:     char logname[PETSC_MAX_PATH_LEN]; logname[0] = 0;
424:     PetscOptionsGetString(PETSC_NULL,"-info",logname,250,&flg1);
425:     if (logname[0]) {
426:       PetscInfoAllow(PETSC_TRUE,logname);
427:     } else {
428:       PetscInfoAllow(PETSC_TRUE,PETSC_NULL);
429:     }
430:   }
431: #endif
432: #if defined(PETSC_USE_LOG)
433:   mname[0] = 0;
434:   PetscOptionsGetString(PETSC_NULL,"-log_history",mname,PETSC_MAX_PATH_LEN,&flg1);
435:   if (flg1) {
436:     if (mname[0]) {
437:       PetscLogOpenHistoryFile(mname,&petsc_history);
438:     } else {
439:       PetscLogOpenHistoryFile(0,&petsc_history);
440:     }
441:   }
442: #if defined(PETSC_HAVE_MPE)
443:   PetscOptionsHasName(PETSC_NULL,"-log_mpe",&flg1);
444:   if (flg1) PetscLogMPEBegin();
445: #endif
446:   PetscOptionsHasName(PETSC_NULL,"-log_all",&flg1);
447:   PetscOptionsHasName(PETSC_NULL,"-log",&flg2);
448:   PetscOptionsHasName(PETSC_NULL,"-log_summary",&flg3);
449:   if (flg1)              {  PetscLogAllBegin(); }
450:   else if (flg2 || flg3) {  PetscLogBegin();}
451: 
452:   PetscOptionsGetString(PETSC_NULL,"-log_trace",mname,250,&flg1);
453:   if (flg1) {
454:     char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN];
455:     FILE *file;
456:     if (mname[0]) {
457:       sprintf(name,"%s.%d",mname,rank);
458:       PetscFixFilename(name,fname);
459:       file = fopen(fname,"w");
460:       if (!file) {
461:         SETERRQ1(PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname);
462:       }
463:     } else {
464:       file = PETSC_STDOUT;
465:     }
466:     PetscLogTraceBegin(file);
467:   }
468: #endif

470:   /*
471:       Setup building of stack frames for all function calls
472:   */
473: #if defined(PETSC_USE_DEBUG)
474:   PetscStackCreate();
475: #endif

477:   PetscOptionsHasName(PETSC_NULL,"-options_gui",&PetscOptionsPublish);

479:   /*
480:        Print basic help message
481:   */
482:   PetscOptionsHasName(PETSC_NULL,"-help",&flg1);
483:   if (flg1) {
484:     (*PetscHelpPrintf)(comm,"Options for all PETSc programs:\n");
485:     (*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is");
486:     (*PetscHelpPrintf)(comm," detected. Useful \n       only when run in the debugger\n");
487:     (*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
488:     (*PetscHelpPrintf)(comm,"       start the debugger in new xterm\n");
489:     (*PetscHelpPrintf)(comm,"       unless noxterm is given\n");
490:     (*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
491:     (*PetscHelpPrintf)(comm,"       start all processes in the debugger\n");
492:     (*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n");
493:     (*PetscHelpPrintf)(comm,"    emacs jumps to error file\n");
494:     (*PetscHelpPrintf)(comm," -debugger_nodes [n1,n2,..] Nodes to start in debugger\n");
495:     (*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n");
496:     (*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n");
497:     (*PetscHelpPrintf)(comm,"                      waits the delay for you to attach\n");
498:     (*PetscHelpPrintf)(comm," -display display: Location where graphics and debuggers are displayed\n");
499:     (*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n");
500:     (*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n");
501:     (*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n");
502:     (*PetscHelpPrintf)(comm,"           note on IBM RS6000 this slows run greatly\n");
503:     (*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n");
504:     (*PetscHelpPrintf)(comm," -malloc: use our error checking malloc\n");
505:     (*PetscHelpPrintf)(comm," -malloc no: don't use error checking malloc\n");
506:     (*PetscHelpPrintf)(comm," -mallocinfo: prints total memory usage\n");
507:     (*PetscHelpPrintf)(comm," -malloc_debug: enables extended checking for memory corruption\n");
508:     (*PetscHelpPrintf)(comm," -options_table: dump list of options inputted\n");
509:     (*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n");
510:     (*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n");
511:     (*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n");
512:     (*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n");
513:     (*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has separate tmp directory\n");
514:     (*PetscHelpPrintf)(comm," -memory_info: print memory usage at end of run\n");
515: #if defined(PETSC_USE_LOG)
516:     (*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n");
517:     (*PetscHelpPrintf)(comm," -log[_all _summary]: logging objects and events\n");
518:     (*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n");
519: #if defined(PETSC_HAVE_MPE)
520:     (*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through upshot\n");
521: #endif
522:     (*PetscHelpPrintf)(comm," -info <optional filename>: print informative messages about the calculations\n");
523: #endif
524:     (*PetscHelpPrintf)(comm," -v: prints PETSc version number and release date\n");
525:     (*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n");
526:     (*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n");
527:     (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
528:   }

530:   PetscOptionsGetInt(PETSC_NULL,"-petsc_sleep",&si,&flg1);
531:   if (flg1) {
532:     PetscSleep(si);
533:   }

535:   PetscOptionsGetString(PETSC_NULL,"-info_exclude",mname,PETSC_MAX_PATH_LEN,&flg1);
536:   PetscStrstr(mname,"null",&f);
537:   if (f) {
538:     PetscInfoDeactivateClass(PETSC_NULL);
539:   }

541:   return(0);
542: }