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