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,"-debug_terminal",string,64,&flg1);
338: if (flg1) { PetscSetDebugTerminal(string); }
339: PetscOptionsGetString(PETSC_NULL,"-start_in_debugger",string,64,&flg1);
340: PetscOptionsGetString(PETSC_NULL,"-stop_for_debugger",string,64,&flg2);
341: if (flg1 || flg2) {
342: PetscMPIInt size;
343: PetscInt lsize,*nodes;
344: MPI_Errhandler err_handler;
345: /*
346: we have to make sure that all processors have opened
347: connections to all other processors, otherwise once the
348: debugger has stated it is likely to receive a SIGUSR1
349: and kill the program.
350: */
351: MPI_Comm_size(PETSC_COMM_WORLD,&size);
352: if (size > 2) {
353: PetscMPIInt dummy;
354: MPI_Status status;
355: for (i=0; i<size; i++) {
356: if (rank != i) {
357: MPI_Send(&dummy,1,MPI_INT,i,109,PETSC_COMM_WORLD);
358: }
359: }
360: for (i=0; i<size; i++) {
361: if (rank != i) {
362: MPI_Recv(&dummy,1,MPI_INT,i,109,PETSC_COMM_WORLD,&status);
363: }
364: }
365: }
366: /* check if this processor node should be in debugger */
367: PetscMalloc(size*sizeof(PetscInt),&nodes);
368: lsize = size;
369: PetscOptionsGetIntArray(PETSC_NULL,"-debugger_nodes",nodes,&lsize,&flag);
370: if (flag) {
371: for (i=0; i<lsize; i++) {
372: if (nodes[i] == rank) { flag = PETSC_FALSE; break; }
373: }
374: }
375: if (!flag) {
376: PetscSetDebuggerFromString(string);
377: PetscPushErrorHandler(PetscAbortErrorHandler,0);
378: if (flg1) {
379: PetscAttachDebugger();
380: } else {
381: PetscStopForDebugger();
382: }
383: MPI_Errhandler_create((MPI_Handler_function*)Petsc_MPI_AbortOnError,&err_handler);
384: MPI_Errhandler_set(comm,err_handler);
385: }
386: PetscFree(nodes);
387: }
389: PetscOptionsGetString(PETSC_NULL,"-on_error_emacs",emacsmachinename,128,&flg1);
390: if (flg1 && !rank) {PetscPushErrorHandler(PetscEmacsClientErrorHandler,emacsmachinename);CHKERRQ(ierr)}
393: /*
394: Activates new sockets for zope if needed
395: */
396: ierr=PetscOptionsHasName(PETSC_NULL,"-zope", &flgz);
397: ierr=PetscOptionsHasName(PETSC_NULL,"-nostdout", &flgzout);
398: if(flgz){
400: int sockfd;
401: char hostname[256];
402: char username[256];
403: int remoteport = 9999;
404: ierr=PetscOptionsGetString(PETSC_NULL, "-zope", hostname, 256, &flgz);
405: if(!hostname[0]){
406: ierr=PetscGetHostName(hostname,256); }
407: ierr=PetscOpenSocket(hostname, remoteport, &sockfd);
408: PetscGetUserName(username, 256);
409: PETSC_ZOPEFD = fdopen(sockfd, "w");
410: if(flgzout){
411: PETSC_STDOUT = PETSC_ZOPEFD;
412: fprintf(PETSC_STDOUT, "<<<user>>> %s\n",username);
413: fprintf(PETSC_STDOUT, "<<<start>>>");
414: }
415: else{
416: fprintf(PETSC_ZOPEFD, "<<<user>>> %s\n",username);
417: fprintf(PETSC_ZOPEFD, "<<<start>>>");
418: }}
420: /*
421: Setup profiling and logging
422: */
423: #if defined (PETSC_USE_INFO)
424: PetscOptionsHasName(PETSC_NULL,"-info",&flg1);
425: if (flg1) {
426: char logname[PETSC_MAX_PATH_LEN]; logname[0] = 0;
427: PetscOptionsGetString(PETSC_NULL,"-info",logname,250,&flg1);
428: if (logname[0]) {
429: PetscInfoAllow(PETSC_TRUE,logname);
430: } else {
431: PetscInfoAllow(PETSC_TRUE,PETSC_NULL);
432: }
433: }
434: #endif
435: #if defined(PETSC_USE_LOG)
436: mname[0] = 0;
437: PetscOptionsGetString(PETSC_NULL,"-log_history",mname,PETSC_MAX_PATH_LEN,&flg1);
438: if (flg1) {
439: if (mname[0]) {
440: PetscLogOpenHistoryFile(mname,&petsc_history);
441: } else {
442: PetscLogOpenHistoryFile(0,&petsc_history);
443: }
444: }
445: #if defined(PETSC_HAVE_MPE)
446: PetscOptionsHasName(PETSC_NULL,"-log_mpe",&flg1);
447: if (flg1) PetscLogMPEBegin();
448: #endif
449: PetscOptionsHasName(PETSC_NULL,"-log_all",&flg1);
450: PetscOptionsHasName(PETSC_NULL,"-log",&flg2);
451: PetscOptionsHasName(PETSC_NULL,"-log_summary",&flg3);
452: if (flg1) { PetscLogAllBegin(); }
453: else if (flg2 || flg3) { PetscLogBegin();}
454:
455: PetscOptionsGetString(PETSC_NULL,"-log_trace",mname,250,&flg1);
456: if (flg1) {
457: char name[PETSC_MAX_PATH_LEN],fname[PETSC_MAX_PATH_LEN];
458: FILE *file;
459: if (mname[0]) {
460: sprintf(name,"%s.%d",mname,rank);
461: PetscFixFilename(name,fname);
462: file = fopen(fname,"w");
463: if (!file) {
464: SETERRQ1(PETSC_ERR_FILE_OPEN,"Unable to open trace file: %s",fname);
465: }
466: } else {
467: file = PETSC_STDOUT;
468: }
469: PetscLogTraceBegin(file);
470: }
471: #endif
473: /*
474: Setup building of stack frames for all function calls
475: */
476: #if defined(PETSC_USE_DEBUG)
477: PetscStackCreate();
478: #endif
480: PetscOptionsHasName(PETSC_NULL,"-options_gui",&PetscOptionsPublish);
482: /*
483: Print basic help message
484: */
485: PetscOptionsHasName(PETSC_NULL,"-help",&flg1);
486: if (flg1) {
487: (*PetscHelpPrintf)(comm,"Options for all PETSc programs:\n");
488: (*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is");
489: (*PetscHelpPrintf)(comm," detected. Useful \n only when run in the debugger\n");
490: (*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
491: (*PetscHelpPrintf)(comm," start the debugger in new xterm\n");
492: (*PetscHelpPrintf)(comm," unless noxterm is given\n");
493: (*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
494: (*PetscHelpPrintf)(comm," start all processes in the debugger\n");
495: (*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n");
496: (*PetscHelpPrintf)(comm," emacs jumps to error file\n");
497: (*PetscHelpPrintf)(comm," -debugger_nodes [n1,n2,..] Nodes to start in debugger\n");
498: (*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n");
499: (*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n");
500: (*PetscHelpPrintf)(comm," waits the delay for you to attach\n");
501: (*PetscHelpPrintf)(comm," -display display: Location where graphics and debuggers are displayed\n");
502: (*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n");
503: (*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n");
504: (*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n");
505: (*PetscHelpPrintf)(comm," note on IBM RS6000 this slows run greatly\n");
506: (*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n");
507: (*PetscHelpPrintf)(comm," -malloc: use our error checking malloc\n");
508: (*PetscHelpPrintf)(comm," -malloc no: don't use error checking malloc\n");
509: (*PetscHelpPrintf)(comm," -mallocinfo: prints total memory usage\n");
510: (*PetscHelpPrintf)(comm," -malloc_debug: enables extended checking for memory corruption\n");
511: (*PetscHelpPrintf)(comm," -options_table: dump list of options inputted\n");
512: (*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n");
513: (*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n");
514: (*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n");
515: (*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n");
516: (*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has separate tmp directory\n");
517: (*PetscHelpPrintf)(comm," -memory_info: print memory usage at end of run\n");
518: #if defined(PETSC_USE_LOG)
519: (*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n");
520: (*PetscHelpPrintf)(comm," -log[_all _summary]: logging objects and events\n");
521: (*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n");
522: #if defined(PETSC_HAVE_MPE)
523: (*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through upshot\n");
524: #endif
525: (*PetscHelpPrintf)(comm," -info <optional filename>: print informative messages about the calculations\n");
526: #endif
527: (*PetscHelpPrintf)(comm," -v: prints PETSc version number and release date\n");
528: (*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n");
529: (*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n");
530: (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
531: }
533: PetscOptionsGetInt(PETSC_NULL,"-petsc_sleep",&si,&flg1);
534: if (flg1) {
535: PetscSleep(si);
536: }
538: PetscOptionsGetString(PETSC_NULL,"-info_exclude",mname,PETSC_MAX_PATH_LEN,&flg1);
539: PetscStrstr(mname,"null",&f);
540: if (f) {
541: PetscInfoDeactivateClass(PETSC_NULL);
542: }
545: return(0);
546: }