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 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;
56: /* ------------------------------------------------------------------------------*/
57: /*
58: Optional file where all PETSc output from various prints is saved
59: */
60: FILE *petsc_history = PETSC_NULL;
64: PetscErrorCode PetscLogOpenHistoryFile(const char filename[],FILE **fd)
65: {
67: PetscMPIInt rank,size;
68: char pfile[PETSC_MAX_PATH_LEN],pname[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN],date[64];
69: char version[256];
72: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
73: if (!rank) {
74: char arch[10];
75: PetscGetArchType(arch,10);
76: PetscGetDate(date,64);
77: PetscGetVersion(&version,256);
78: MPI_Comm_size(PETSC_COMM_WORLD,&size);
79: if (filename) {
80: PetscFixFilename(filename,fname);
81: } else {
82: PetscGetHomeDirectory(pfile,240);
83: PetscStrcat(pfile,"/.petschistory");
84: PetscFixFilename(pfile,fname);
85: }
87: *fd = fopen(fname,"a"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open file: %s",fname);
88: PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
89: PetscFPrintf(PETSC_COMM_SELF,*fd,"%s %s\n",version,date);
90: PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);
91: PetscFPrintf(PETSC_COMM_SELF,*fd,"%s on a %s, %d proc. with options:\n",pname,arch,size);
92: PetscOptionsPrint(*fd);
93: PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
94: fflush(*fd);
95: }
96: return(0);
97: }
101: PetscErrorCode PetscLogCloseHistoryFile(FILE **fd)
102: {
104: PetscMPIInt rank;
105: char date[64];
108: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
109: if (!rank) {
110: PetscGetDate(date,64);
111: PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
112: PetscFPrintf(PETSC_COMM_SELF,*fd,"Finished at %s\n",date);
113: PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
114: fflush(*fd);
115: fclose(*fd);
116: }
117: return(0);
118: }
120: /* ------------------------------------------------------------------------------*/
122: /*
123: This is ugly and probably belongs somewhere else, but I want to
124: be able to put a true MPI abort error handler with command line args.
126: This is so MPI errors in the debugger will leave all the stack
127: frames. The default abort cleans up and exits.
128: */
132: void Petsc_MPI_AbortOnError(MPI_Comm *comm,PetscMPIInt *flag)
133: {
135: (*PetscErrorPrintf)("MPI error %d\n",(int)*flag);
136: abort();
137: }
141: void Petsc_MPI_DebuggerOnError(MPI_Comm *comm,PetscMPIInt *flag)
142: {
146: (*PetscErrorPrintf)("MPI error %d\n",(int)*flag);
147: PetscAttachDebugger();
148: if (ierr) { /* hopeless so get out */
149: MPI_Finalize();
150: exit(*flag);
151: }
152: }
156: /*@C
157: PetscEnd - Calls PetscFinalize() and then ends the program. This is useful if one
158: wishes a clean exit somewhere deep in the program.
160: Collective on PETSC_COMM_WORLD
162: Options Database Keys are the same as for PetscFinalize()
164: Level: advanced
166: Note:
167: See PetscInitialize() for more general runtime options.
169: .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscFinalize()
170: @*/
171: PetscErrorCode PetscEnd(void)
172: {
174: PetscFinalize();
175: exit(0);
176: return 0;
177: }
179: PetscTruth PetscOptionsPublish = PETSC_FALSE;
180: EXTERN PetscErrorCode PetscSetUseTrMalloc_Private(void);
182: static char emacsmachinename[256];
184: PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = 0;
185: PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm) = 0;
189: /*@C
190: PetscSetHelpVersionFunctions - Sets functions that print help and version information
191: before the PETSc help and version information is printed. Must call BEFORE PetscInitialize().
192: This routine enables a "higher-level" package that uses PETSc to print its messages first.
194: Input Parameter:
195: + help - the help function (may be PETSC_NULL)
196: - version - the version function (may be PETSc null)
198: Level: developer
200: Concepts: package help message
202: @*/
203: PetscErrorCode PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm),PetscErrorCode (*version)(MPI_Comm))
204: {
206: PetscExternalHelpFunction = help;
207: PetscExternalVersionFunction = version;
208: return(0);
209: }
213: PetscErrorCode PetscOptionsCheckInitial_Private(void)
214: {
215: char string[64],mname[PETSC_MAX_PATH_LEN],*f;
216: MPI_Comm comm = PETSC_COMM_WORLD;
217: PetscTruth flg1,flg2,flg3,flag;
219: PetscInt si;
220: int i;
221: PetscMPIInt rank;
222: char version[256];
225: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
227: /*
228: Setup the memory management; support for tracing malloc() usage
229: */
230: PetscOptionsHasName(PETSC_NULL,"-malloc_log",&flg3);
231: #if defined(PETSC_USE_DEBUG)
232: PetscOptionsGetTruth(PETSC_NULL,"-malloc",&flg1,&flg2);
233: if ((!flg2 || flg1) && !petscsetmallocvisited) {
234: PetscSetUseTrMalloc_Private();
235: }
236: #else
237: PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);
238: PetscOptionsHasName(PETSC_NULL,"-malloc",&flg2);
239: if (flg1 || flg2 || flg3) {PetscSetUseTrMalloc_Private();}
240: #endif
241: if (flg3) {
242: PetscMallocSetDumpLog();
243: }
244: PetscOptionsHasName(PETSC_NULL,"-malloc_debug",&flg1);
245: if (flg1) {
246: PetscSetUseTrMalloc_Private();
247: PetscMallocDebug(PETSC_TRUE);
248: }
250: PetscOptionsHasName(PETSC_NULL,"-malloc_info",&flg1);
251: if (!flg1) {
252: PetscOptionsHasName(PETSC_NULL,"-memory_info",&flg1);
253: }
254: if (flg1) {
255: PetscMemorySetGetMaximumUsage();
256: }
258: /*
259: Set the display variable for graphics
260: */
261: PetscSetDisplay();
263: /*
264: Print the PETSc version information
265: */
266: PetscOptionsHasName(PETSC_NULL,"-v",&flg1);
267: PetscOptionsHasName(PETSC_NULL,"-version",&flg2);
268: PetscOptionsHasName(PETSC_NULL,"-help",&flg3);
269: if (flg1 || flg2 || flg3){
271: /*
272: Print "higher-level" package version message
273: */
274: if (PetscExternalVersionFunction) {
275: (*PetscExternalVersionFunction)(comm);
276: }
278: PetscGetVersion(&version,256);
279: (*PetscHelpPrintf)(comm,"--------------------------------------------\
280: ------------------------------\n");
281: (*PetscHelpPrintf)(comm,"%s\n",version);
282: (*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO);
283: (*PetscHelpPrintf)(comm,"See docs/copyright.html for copyright information\n");
284: (*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n");
285: (*PetscHelpPrintf)(comm,"See docs/troubleshooting.html for problems.\n");
286: (*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n");
287: (*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR);
288: (*PetscHelpPrintf)(comm,"--------------------------------------------\
289: ------------------------------\n");
290: }
292: /*
293: Print "higher-level" package help message
294: */
295: if (flg3){
296: if (PetscExternalHelpFunction) {
297: (*PetscExternalHelpFunction)(comm);
298: }
299: }
301: /*
302: Setup the error handling
303: */
304: PetscOptionsHasName(PETSC_NULL,"-fp_trap",&flg1);
305: if (flg1) { PetscSetFPTrap(PETSC_FP_TRAP_ON); }
306: PetscOptionsHasName(PETSC_NULL,"-on_error_abort",&flg1);
307: if (flg1) { PetscPushErrorHandler(PetscAbortErrorHandler,0);CHKERRQ(ierr)}
308: PetscOptionsHasName(PETSC_NULL,"-on_error_mpiabort",&flg1);
309: if (flg1) { PetscPushErrorHandler(PetscMPIAbortErrorHandler,0);CHKERRQ(ierr)}
310: PetscOptionsHasName(PETSC_NULL,"-mpi_return_on_error",&flg1);
311: if (flg1) {
312: MPI_Errhandler_set(comm,MPI_ERRORS_RETURN);
313: }
314: PetscOptionsHasName(PETSC_NULL,"-error_output_none",&flg1);
315: if (flg1) {
316: PetscErrorPrintf = PetscErrorPrintfNone;
317: }
318: PetscOptionsHasName(PETSC_NULL,"-no_signal_handler",&flg1);
319: if (!flg1) { PetscPushSignalHandler(PetscDefaultSignalHandler,(void*)0);CHKERRQ(ierr) }
321: /*
322: Setup debugger information
323: */
324: PetscSetDefaultDebugger();
325: PetscOptionsGetString(PETSC_NULL,"-on_error_attach_debugger",string,64,&flg1);
326: if (flg1) {
327: MPI_Errhandler err_handler;
329: PetscSetDebuggerFromString(string);
330: MPI_Errhandler_create((MPI_Handler_function*)Petsc_MPI_DebuggerOnError,&err_handler);
331: MPI_Errhandler_set(comm,err_handler);
332: PetscPushErrorHandler(PetscAttachDebuggerErrorHandler,0);
333: }
334: PetscOptionsGetString(PETSC_NULL,"-start_in_debugger",string,64,&flg1);
335: PetscOptionsGetString(PETSC_NULL,"-stop_for_debugger",string,64,&flg2);
336: if (flg1 || flg2) {
337: PetscMPIInt size;
338: PetscInt lsize,*nodes;
339: MPI_Errhandler err_handler;
340: /*
341: we have to make sure that all processors have opened
342: connections to all other processors, otherwise once the
343: debugger has stated it is likely to receive a SIGUSR1
344: and kill the program.
345: */
346: MPI_Comm_size(PETSC_COMM_WORLD,&size);
347: if (size > 2) {
348: PetscMPIInt dummy;
349: MPI_Status status;
350: for (i=0; i<size; i++) {
351: if (rank != i) {
352: MPI_Send(&dummy,1,MPI_INT,i,109,PETSC_COMM_WORLD);
353: }
354: }
355: for (i=0; i<size; i++) {
356: if (rank != i) {
357: MPI_Recv(&dummy,1,MPI_INT,i,109,PETSC_COMM_WORLD,&status);
358: }
359: }
360: }
361: /* check if this processor node should be in debugger */
362: PetscMalloc(size*sizeof(PetscInt),&nodes);
363: lsize = size;
364: PetscOptionsGetIntArray(PETSC_NULL,"-debugger_nodes",nodes,&lsize,&flag);
365: if (flag) {
366: for (i=0; i<lsize; i++) {
367: if (nodes[i] == rank) { flag = PETSC_FALSE; break; }
368: }
369: }
370: if (!flag) {
371: PetscSetDebuggerFromString(string);
372: PetscPushErrorHandler(PetscAbortErrorHandler,0);
373: if (flg1) {
374: PetscAttachDebugger();
375: } else {
376: PetscStopForDebugger();
377: }
378: MPI_Errhandler_create((MPI_Handler_function*)Petsc_MPI_AbortOnError,&err_handler);
379: MPI_Errhandler_set(comm,err_handler);
380: }
381: PetscFree(nodes);
382: }
384: PetscOptionsGetString(PETSC_NULL,"-on_error_emacs",emacsmachinename,128,&flg1);
385: if (flg1 && !rank) {PetscPushErrorHandler(PetscEmacsClientErrorHandler,emacsmachinename);CHKERRQ(ierr)}
387: /*
388: Setup profiling and logging
389: */
390: #if defined (PETSC_USE_INFO)
391: PetscOptionsHasName(PETSC_NULL,"-info",&flg1);
392: if (flg1) {
393: char logname[PETSC_MAX_PATH_LEN]; logname[0] = 0;
394: PetscOptionsGetString(PETSC_NULL,"-info",logname,250,&flg1);
395: if (logname[0]) {
396: PetscInfoAllow(PETSC_TRUE,logname);
397: } else {
398: PetscInfoAllow(PETSC_TRUE,PETSC_NULL);
399: }
400: }
401: #endif
402: #if defined(PETSC_USE_LOG)
403: mname[0] = 0;
404: PetscOptionsGetString(PETSC_NULL,"-log_history",mname,PETSC_MAX_PATH_LEN,&flg1);
405: if (flg1) {
406: if (mname[0]) {
407: PetscLogOpenHistoryFile(mname,&petsc_history);
408: } else {
409: PetscLogOpenHistoryFile(0,&petsc_history);
410: }
411: }
412: #if defined(PETSC_HAVE_MPE)
413: PetscOptionsHasName(PETSC_NULL,"-log_mpe",&flg1);
414: if (flg1) PetscLogMPEBegin();
415: #endif
416: PetscOptionsHasName(PETSC_NULL,"-log_all",&flg1);
417: PetscOptionsHasName(PETSC_NULL,"-log",&flg2);
418: PetscOptionsHasName(PETSC_NULL,"-log_summary",&flg3);
419: if (flg1) { PetscLogAllBegin(); }
420: else if (flg2 || flg3) { PetscLogBegin();}
421:
422: PetscOptionsGetString(PETSC_NULL,"-log_trace",mname,250,&flg1);
423: if (flg1) {
424: char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN];
425: FILE *file;
426: if (mname[0]) {
427: sprintf(name,"%s.%d",mname,rank);
428: PetscFixFilename(name,fname);
429: file = fopen(fname,"w");
430: if (!file) {
431: SETERRQ1(PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname);
432: }
433: } else {
434: file = stdout;
435: }
436: PetscLogTraceBegin(file);
437: }
438: #endif
440: /*
441: Setup building of stack frames for all function calls
442: */
443: #if defined(PETSC_USE_DEBUG)
444: PetscStackCreate();
445: #endif
447: PetscOptionsHasName(PETSC_NULL,"-options_gui",&PetscOptionsPublish);
449: /*
450: Print basic help message
451: */
452: PetscOptionsHasName(PETSC_NULL,"-help",&flg1);
453: if (flg1) {
454: (*PetscHelpPrintf)(comm,"Options for all PETSc programs:\n");
455: (*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is");
456: (*PetscHelpPrintf)(comm," detected. Useful \n only when run in the debugger\n");
457: (*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
458: (*PetscHelpPrintf)(comm," start the debugger in new xterm\n");
459: (*PetscHelpPrintf)(comm," unless noxterm is given\n");
460: (*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
461: (*PetscHelpPrintf)(comm," start all processes in the debugger\n");
462: (*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n");
463: (*PetscHelpPrintf)(comm," emacs jumps to error file\n");
464: (*PetscHelpPrintf)(comm," -debugger_nodes [n1,n2,..] Nodes to start in debugger\n");
465: (*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n");
466: (*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n");
467: (*PetscHelpPrintf)(comm," waits the delay for you to attach\n");
468: (*PetscHelpPrintf)(comm," -display display: Location where graphics and debuggers are displayed\n");
469: (*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n");
470: (*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n");
471: (*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n");
472: (*PetscHelpPrintf)(comm," note on IBM RS6000 this slows run greatly\n");
473: (*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n");
474: (*PetscHelpPrintf)(comm," -malloc: use our error checking malloc\n");
475: (*PetscHelpPrintf)(comm," -malloc no: don't use error checking malloc\n");
476: (*PetscHelpPrintf)(comm," -mallocinfo: prints total memory usage\n");
477: (*PetscHelpPrintf)(comm," -malloc_debug: enables extended checking for memory corruption\n");
478: (*PetscHelpPrintf)(comm," -options_table: dump list of options inputted\n");
479: (*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n");
480: (*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n");
481: (*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n");
482: (*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n");
483: (*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has separate tmp directory\n");
484: (*PetscHelpPrintf)(comm," -memory_info: print memory usage at end of run\n");
485: #if defined(PETSC_USE_LOG)
486: (*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n");
487: (*PetscHelpPrintf)(comm," -log[_all _summary]: logging objects and events\n");
488: (*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n");
489: #if defined(PETSC_HAVE_MPE)
490: (*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through upshot\n");
491: #endif
492: (*PetscHelpPrintf)(comm," -info <optional filename>: print informative messages about the calculations\n");
493: #endif
494: (*PetscHelpPrintf)(comm," -v: prints PETSc version number and release date\n");
495: (*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n");
496: (*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n");
497: (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
498: }
500: PetscOptionsGetInt(PETSC_NULL,"-petsc_sleep",&si,&flg1);
501: if (flg1) {
502: PetscSleep(si);
503: }
505: PetscOptionsGetString(PETSC_NULL,"-info_exclude",mname,PETSC_MAX_PATH_LEN,&flg1);
506: PetscStrstr(mname,"null",&f);
507: if (f) {
508: PetscInfoDeactivateClass(PETSC_NULL);
509: }
511: return(0);
512: }