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: char _BT_mask = ' ';
46: char _BT_c = ' ';
47: PetscInt _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 (*PetscErrorPrintf)(const char [],...) = PetscErrorPrintfDefault;
55: PetscErrorCode (*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 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,256);
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 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 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 PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm),PetscErrorCode (*version)(MPI_Comm))
205: {
207: PetscExternalHelpFunction = help;
208: PetscExternalVersionFunction = version;
209: return(0);
210: }
214: PetscErrorCode 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,"-malloc_info",&flg1);
252: if (!flg1) {
253: PetscOptionsHasName(PETSC_NULL,"-memory_info",&flg1);
254: }
255: if (flg1) {
256: PetscMemorySetGetMaximumUsage();
257: }
259: /*
260: Set the display variable for graphics
261: */
262: PetscSetDisplay();
264: /*
265: Print the PETSc version information
266: */
267: PetscOptionsHasName(PETSC_NULL,"-v",&flg1);
268: PetscOptionsHasName(PETSC_NULL,"-version",&flg2);
269: PetscOptionsHasName(PETSC_NULL,"-help",&flg3);
270: if (flg1 || flg2 || flg3){
272: /*
273: Print "higher-level" package version message
274: */
275: if (PetscExternalVersionFunction) {
276: (*PetscExternalVersionFunction)(comm);
277: }
279: PetscGetVersion(&version,256);
280: (*PetscHelpPrintf)(comm,"--------------------------------------------\
281: ------------------------------\n");
282: (*PetscHelpPrintf)(comm,"%s\n",version);
283: (*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO);
284: (*PetscHelpPrintf)(comm,"See docs/copyright.html for copyright information\n");
285: (*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n");
286: (*PetscHelpPrintf)(comm,"See docs/troubleshooting.html for problems.\n");
287: (*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n");
288: (*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR);
289: (*PetscHelpPrintf)(comm,"--------------------------------------------\
290: ------------------------------\n");
291: }
293: /*
294: Print "higher-level" package help message
295: */
296: if (flg3){
297: if (PetscExternalHelpFunction) {
298: (*PetscExternalHelpFunction)(comm);
299: }
300: }
302: /*
303: Setup the error handling
304: */
305: PetscOptionsHasName(PETSC_NULL,"-fp_trap",&flg1);
306: if (flg1) { PetscSetFPTrap(PETSC_FP_TRAP_ON); }
307: PetscOptionsHasName(PETSC_NULL,"-on_error_abort",&flg1);
308: if (flg1) { PetscPushErrorHandler(PetscAbortErrorHandler,0);CHKERRQ(ierr)}
309: PetscOptionsHasName(PETSC_NULL,"-on_error_stop",&flg1);
310: if (flg1) { PetscPushErrorHandler(PetscStopErrorHandler,0);CHKERRQ(ierr)}
311: PetscOptionsHasName(PETSC_NULL,"-mpi_return_on_error",&flg1);
312: if (flg1) {
313: MPI_Errhandler_set(comm,MPI_ERRORS_RETURN);
314: }
315: PetscOptionsHasName(PETSC_NULL,"-no_signal_handler",&flg1);
316: if (!flg1) { PetscPushSignalHandler(PetscDefaultSignalHandler,(void*)0);CHKERRQ(ierr) }
318: /*
319: Setup debugger information
320: */
321: PetscSetDefaultDebugger();
322: PetscOptionsGetString(PETSC_NULL,"-on_error_attach_debugger",string,64,&flg1);
323: if (flg1) {
324: MPI_Errhandler err_handler;
326: PetscSetDebuggerFromString(string);
327: MPI_Errhandler_create((MPI_Handler_function*)Petsc_MPI_DebuggerOnError,&err_handler);
328: MPI_Errhandler_set(comm,err_handler);
329: PetscPushErrorHandler(PetscAttachDebuggerErrorHandler,0);
330: }
331: PetscOptionsGetString(PETSC_NULL,"-start_in_debugger",string,64,&flg1);
332: PetscOptionsGetString(PETSC_NULL,"-stop_for_debugger",string,64,&flg2);
333: if (flg1 || flg2) {
334: PetscMPIInt size;
335: PetscInt lsize,*nodes;
336: MPI_Errhandler err_handler;
337: /*
338: we have to make sure that all processors have opened
339: connections to all other processors, otherwise once the
340: debugger has stated it is likely to receive a SIGUSR1
341: and kill the program.
342: */
343: MPI_Comm_size(PETSC_COMM_WORLD,&size);
344: if (size > 2) {
345: PetscMPIInt dummy;
346: MPI_Status status;
347: for (i=0; i<size; i++) {
348: if (rank != i) {
349: MPI_Send(&dummy,1,MPI_INT,i,109,PETSC_COMM_WORLD);
350: }
351: }
352: for (i=0; i<size; i++) {
353: if (rank != i) {
354: MPI_Recv(&dummy,1,MPI_INT,i,109,PETSC_COMM_WORLD,&status);
355: }
356: }
357: }
358: /* check if this processor node should be in debugger */
359: PetscMalloc(size*sizeof(PetscInt),&nodes);
360: lsize = size;
361: PetscOptionsGetIntArray(PETSC_NULL,"-debugger_nodes",nodes,&lsize,&flag);
362: if (flag) {
363: for (i=0; i<lsize; i++) {
364: if (nodes[i] == rank) { flag = PETSC_FALSE; break; }
365: }
366: }
367: if (!flag) {
368: PetscSetDebuggerFromString(string);
369: PetscPushErrorHandler(PetscAbortErrorHandler,0);
370: if (flg1) {
371: PetscAttachDebugger();
372: } else {
373: PetscStopForDebugger();
374: }
375: MPI_Errhandler_create((MPI_Handler_function*)Petsc_MPI_AbortOnError,&err_handler);
376: MPI_Errhandler_set(comm,err_handler);
377: }
378: PetscFree(nodes);
379: }
381: PetscOptionsGetString(PETSC_NULL,"-on_error_emacs",emacsmachinename,128,&flg1);
382: if (flg1 && !rank) {PetscPushErrorHandler(PetscEmacsClientErrorHandler,emacsmachinename);CHKERRQ(ierr)}
384: /*
385: Setup profiling and logging
386: */
387: #if defined (PETSC_USE_INFO)
388: PetscOptionsHasName(PETSC_NULL,"-info",&flg1);
389: if (flg1) {
390: char logname[PETSC_MAX_PATH_LEN]; logname[0] = 0;
391: PetscOptionsGetString(PETSC_NULL,"-info",logname,250,&flg1);
392: if (logname[0]) {
393: PetscInfoAllow(PETSC_TRUE,logname);
394: } else {
395: PetscInfoAllow(PETSC_TRUE,PETSC_NULL);
396: }
397: }
398: #endif
399: #if defined(PETSC_USE_LOG)
400: mname[0] = 0;
401: PetscOptionsGetString(PETSC_NULL,"-log_history",mname,PETSC_MAX_PATH_LEN,&flg1);
402: if (flg1) {
403: if (mname[0]) {
404: PetscLogOpenHistoryFile(mname,&petsc_history);
405: } else {
406: PetscLogOpenHistoryFile(0,&petsc_history);
407: }
408: }
409: #if defined(PETSC_HAVE_MPE)
410: PetscOptionsHasName(PETSC_NULL,"-log_mpe",&flg1);
411: if (flg1) PetscLogMPEBegin();
412: #endif
413: PetscOptionsHasName(PETSC_NULL,"-log_all",&flg1);
414: PetscOptionsHasName(PETSC_NULL,"-log",&flg2);
415: PetscOptionsHasName(PETSC_NULL,"-log_summary",&flg3);
416: if (flg1) { PetscLogAllBegin(); }
417: else if (flg2 || flg3) { PetscLogBegin();}
418:
419: PetscOptionsGetString(PETSC_NULL,"-log_trace",mname,250,&flg1);
420: if (flg1) {
421: char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN];
422: FILE *file;
423: if (mname[0]) {
424: sprintf(name,"%s.%d",mname,rank);
425: PetscFixFilename(name,fname);
426: file = fopen(fname,"w");
427: if (!file) {
428: SETERRQ1(PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname);
429: }
430: } else {
431: file = stdout;
432: }
433: PetscLogTraceBegin(file);
434: }
435: #endif
437: /*
438: Setup building of stack frames for all function calls
439: */
440: #if defined(PETSC_USE_DEBUG)
441: PetscStackCreate();
442: #endif
444: PetscOptionsHasName(PETSC_NULL,"-options_gui",&PetscOptionsPublish);
446: /*
447: Print basic help message
448: */
449: PetscOptionsHasName(PETSC_NULL,"-help",&flg1);
450: if (flg1) {
451: (*PetscHelpPrintf)(comm,"Options for all PETSc programs:\n");
452: (*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is");
453: (*PetscHelpPrintf)(comm," detected. Useful \n only when run in the debugger\n");
454: (*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
455: (*PetscHelpPrintf)(comm," start the debugger in new xterm\n");
456: (*PetscHelpPrintf)(comm," unless noxterm is given\n");
457: (*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
458: (*PetscHelpPrintf)(comm," start all processes in the debugger\n");
459: (*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n");
460: (*PetscHelpPrintf)(comm," emacs jumps to error file\n");
461: (*PetscHelpPrintf)(comm," -debugger_nodes [n1,n2,..] Nodes to start in debugger\n");
462: (*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n");
463: (*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n");
464: (*PetscHelpPrintf)(comm," waits the delay for you to attach\n");
465: (*PetscHelpPrintf)(comm," -display display: Location where graphics and debuggers are displayed\n");
466: (*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n");
467: (*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n");
468: (*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n");
469: (*PetscHelpPrintf)(comm," note on IBM RS6000 this slows run greatly\n");
470: (*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n");
471: (*PetscHelpPrintf)(comm," -malloc: use our error checking malloc\n");
472: (*PetscHelpPrintf)(comm," -malloc no: don't use error checking malloc\n");
473: (*PetscHelpPrintf)(comm," -mallocinfo: prints total memory usage\n");
474: (*PetscHelpPrintf)(comm," -malloc_debug: enables extended checking for memory corruption\n");
475: (*PetscHelpPrintf)(comm," -options_table: dump list of options inputted\n");
476: (*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n");
477: (*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n");
478: (*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n");
479: (*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n");
480: (*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has separate tmp directory\n");
481: (*PetscHelpPrintf)(comm," -memory_info: print memory usage at end of run\n");
482: #if defined(PETSC_USE_LOG)
483: (*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n");
484: (*PetscHelpPrintf)(comm," -log[_all _summary]: logging objects and events\n");
485: (*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n");
486: #if defined(PETSC_HAVE_MPE)
487: (*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through upshot\n");
488: #endif
489: (*PetscHelpPrintf)(comm," -info <optional filename>: print informative messages about the calculations\n");
490: #endif
491: (*PetscHelpPrintf)(comm," -v: prints PETSc version number and release date\n");
492: (*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n");
493: (*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n");
494: (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
495: }
497: PetscOptionsGetInt(PETSC_NULL,"-petsc_sleep",&si,&flg1);
498: if (flg1) {
499: PetscSleep(si);
500: }
502: PetscOptionsGetString(PETSC_NULL,"-info_exclude",mname,PETSC_MAX_PATH_LEN,&flg1);
503: PetscStrstr(mname,"null",&f);
504: if (f) {
505: PetscInfoDeactivateClass(PETSC_NULL);
506: }
508: return(0);
509: }