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: /* ------------------------Nasty global variables -------------------------------*/
20: /*
21: Indicates if PETSc started up MPI, or it was
22: already started before PETSc was initialized.
23: */
24: PetscTruth PetscBeganMPI = PETSC_FALSE;
25: PetscTruth PetscInitializeCalled = PETSC_FALSE;
26: PetscTruth PetscFinalizeCalled = PETSC_FALSE;
27: PetscMPIInt PetscGlobalRank = -1;
28: PetscMPIInt PetscGlobalSize = -1;
30: #if defined(PETSC_USE_COMPLEX)
31: #if defined(PETSC_COMPLEX_INSTANTIATE)
32: template <> class std::complex<double>; /* instantiate complex template class */
33: #endif
34: MPI_Datatype MPIU_COMPLEX;
35: PetscScalar PETSC_i;
36: #else
37: PetscScalar PETSC_i = 0.0;
38: #endif
39: MPI_Datatype MPIU_2SCALAR = 0;
40: MPI_Datatype MPIU_2INT = 0;
41: /*
42: These are needed by petscbt.h
43: */
44: #include petscbt.h
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;
54: PetscErrorCode (*PetscVFPrintf)(FILE*,const char[],va_list) = PetscVFPrintfDefault;
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: int err;
77: PetscGetArchType(arch,10);
78: PetscGetDate(date,64);
79: PetscGetVersion(version,256);
80: MPI_Comm_size(PETSC_COMM_WORLD,&size);
81: if (filename) {
82: PetscFixFilename(filename,fname);
83: } else {
84: PetscGetHomeDirectory(pfile,240);
85: PetscStrcat(pfile,"/.petschistory");
86: PetscFixFilename(pfile,fname);
87: }
89: *fd = fopen(fname,"a"); if (!fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open file: %s",fname);
90: PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
91: PetscFPrintf(PETSC_COMM_SELF,*fd,"%s %s\n",version,date);
92: PetscGetProgramName(pname,PETSC_MAX_PATH_LEN);
93: PetscFPrintf(PETSC_COMM_SELF,*fd,"%s on a %s, %d proc. with options:\n",pname,arch,size);
94: PetscOptionsPrint(*fd);
95: PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
96: err = fflush(*fd);
97: if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
98: }
99: return(0);
100: }
104: PetscErrorCode PetscLogCloseHistoryFile(FILE **fd)
105: {
107: PetscMPIInt rank;
108: char date[64];
109: int err;
112: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
113: if (!rank) {
114: PetscGetDate(date,64);
115: PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
116: PetscFPrintf(PETSC_COMM_SELF,*fd,"Finished at %s\n",date);
117: PetscFPrintf(PETSC_COMM_SELF,*fd,"---------------------------------------------------------\n");
118: err = fflush(*fd);
119: if (err) SETERRQ(PETSC_ERR_SYS,"fflush() failed on file");
120: err = fclose(*fd);
121: if (err) SETERRQ(PETSC_ERR_SYS,"fclose() failed on file");
122: }
123: return(0);
124: }
126: /* ------------------------------------------------------------------------------*/
128: /*
129: This is ugly and probably belongs somewhere else, but I want to
130: be able to put a true MPI abort error handler with command line args.
132: This is so MPI errors in the debugger will leave all the stack
133: frames. The default abort cleans up and exits.
134: */
138: void Petsc_MPI_AbortOnError(MPI_Comm *comm,PetscMPIInt *flag)
139: {
141: (*PetscErrorPrintf)("MPI error %d\n",(int)*flag);
142: abort();
143: }
147: void Petsc_MPI_DebuggerOnError(MPI_Comm *comm,PetscMPIInt *flag)
148: {
152: (*PetscErrorPrintf)("MPI error %d\n",(int)*flag);
153: PetscAttachDebugger();
154: if (ierr) { /* hopeless so get out */
155: MPI_Finalize();
156: exit(*flag);
157: }
158: }
162: /*@C
163: PetscEnd - Calls PetscFinalize() and then ends the program. This is useful if one
164: wishes a clean exit somewhere deep in the program.
166: Collective on PETSC_COMM_WORLD
168: Options Database Keys are the same as for PetscFinalize()
170: Level: advanced
172: Note:
173: See PetscInitialize() for more general runtime options.
175: .seealso: PetscInitialize(), PetscOptionsPrint(), PetscMallocDump(), PetscMPIDump(), PetscFinalize()
176: @*/
177: PetscErrorCode PetscEnd(void)
178: {
180: PetscFinalize();
181: exit(0);
182: return 0;
183: }
185: PetscTruth PetscOptionsPublish = PETSC_FALSE;
186: EXTERN PetscErrorCode PetscSetUseTrMalloc_Private(void);
188: static char emacsmachinename[256];
190: PetscErrorCode (*PetscExternalVersionFunction)(MPI_Comm) = 0;
191: PetscErrorCode (*PetscExternalHelpFunction)(MPI_Comm) = 0;
195: /*@C
196: PetscSetHelpVersionFunctions - Sets functions that print help and version information
197: before the PETSc help and version information is printed. Must call BEFORE PetscInitialize().
198: This routine enables a "higher-level" package that uses PETSc to print its messages first.
200: Input Parameter:
201: + help - the help function (may be PETSC_NULL)
202: - version - the version function (may be PETSc null)
204: Level: developer
206: Concepts: package help message
208: @*/
209: PetscErrorCode PetscSetHelpVersionFunctions(PetscErrorCode (*help)(MPI_Comm),PetscErrorCode (*version)(MPI_Comm))
210: {
212: PetscExternalHelpFunction = help;
213: PetscExternalVersionFunction = version;
214: return(0);
215: }
219: PetscErrorCode PetscOptionsCheckInitial_Private(void)
220: {
221: char string[64],mname[PETSC_MAX_PATH_LEN],*f;
222: MPI_Comm comm = PETSC_COMM_WORLD;
223: PetscTruth flg1,flg2,flg3,flag,flgz,flgzout;
225: PetscInt si;
226: int i;
227: PetscMPIInt rank;
228: char version[256];
231: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
233: /*
234: Setup the memory management; support for tracing malloc() usage
235: */
236: PetscOptionsHasName(PETSC_NULL,"-malloc_log",&flg3);
237: #if defined(PETSC_USE_DEBUG)
238: PetscOptionsGetTruth(PETSC_NULL,"-malloc",&flg1,&flg2);
239: if ((!flg2 || flg1) && !petscsetmallocvisited) {
240: PetscSetUseTrMalloc_Private();
241: }
242: #else
243: PetscOptionsHasName(PETSC_NULL,"-malloc_dump",&flg1);
244: PetscOptionsHasName(PETSC_NULL,"-malloc",&flg2);
245: if (flg1 || flg2 || flg3) {PetscSetUseTrMalloc_Private();}
246: #endif
247: if (flg3) {
248: PetscMallocSetDumpLog();
249: }
250: PetscOptionsHasName(PETSC_NULL,"-malloc_debug",&flg1);
251: if (flg1) {
252: PetscSetUseTrMalloc_Private();
253: PetscMallocDebug(PETSC_TRUE);
254: }
256: PetscOptionsHasName(PETSC_NULL,"-malloc_info",&flg1);
257: if (!flg1) {
258: PetscOptionsHasName(PETSC_NULL,"-memory_info",&flg1);
259: }
260: if (flg1) {
261: PetscMemorySetGetMaximumUsage();
262: }
264: /*
265: Set the display variable for graphics
266: */
267: PetscSetDisplay();
269: /*
270: Print the PETSc version information
271: */
272: PetscOptionsHasName(PETSC_NULL,"-v",&flg1);
273: PetscOptionsHasName(PETSC_NULL,"-version",&flg2);
274: PetscOptionsHasName(PETSC_NULL,"-help",&flg3);
275: if (flg1 || flg2 || flg3){
277: /*
278: Print "higher-level" package version message
279: */
280: if (PetscExternalVersionFunction) {
281: (*PetscExternalVersionFunction)(comm);
282: }
284: PetscGetVersion(version,256);
285: (*PetscHelpPrintf)(comm,"--------------------------------------------\
286: ------------------------------\n");
287: (*PetscHelpPrintf)(comm,"%s\n",version);
288: (*PetscHelpPrintf)(comm,"%s",PETSC_AUTHOR_INFO);
289: (*PetscHelpPrintf)(comm,"See docs/copyright.html for copyright information\n");
290: (*PetscHelpPrintf)(comm,"See docs/changes/index.html for recent updates.\n");
291: (*PetscHelpPrintf)(comm,"See docs/troubleshooting.html for problems.\n");
292: (*PetscHelpPrintf)(comm,"See docs/manualpages/index.html for help. \n");
293: (*PetscHelpPrintf)(comm,"Libraries linked from %s\n",PETSC_LIB_DIR);
294: (*PetscHelpPrintf)(comm,"--------------------------------------------\
295: ------------------------------\n");
296: }
298: /*
299: Print "higher-level" package help message
300: */
301: if (flg3){
302: if (PetscExternalHelpFunction) {
303: (*PetscExternalHelpFunction)(comm);
304: }
305: }
307: /*
308: Setup the error handling
309: */
310: PetscOptionsHasName(PETSC_NULL,"-fp_trap",&flg1);
311: if (flg1) { PetscSetFPTrap(PETSC_FP_TRAP_ON); }
312: PetscOptionsHasName(PETSC_NULL,"-on_error_abort",&flg1);
313: if (flg1) { PetscPushErrorHandler(PetscAbortErrorHandler,0);CHKERRQ(ierr)}
314: PetscOptionsHasName(PETSC_NULL,"-on_error_mpiabort",&flg1);
315: if (flg1) { PetscPushErrorHandler(PetscMPIAbortErrorHandler,0);CHKERRQ(ierr)}
316: PetscOptionsHasName(PETSC_NULL,"-mpi_return_on_error",&flg1);
317: if (flg1) {
318: MPI_Errhandler_set(comm,MPI_ERRORS_RETURN);
319: }
320: PetscOptionsHasName(PETSC_NULL,"-no_signal_handler",&flg1);
321: if (!flg1) { PetscPushSignalHandler(PetscDefaultSignalHandler,(void*)0);CHKERRQ(ierr) }
323: /*
324: Setup debugger information
325: */
326: PetscSetDefaultDebugger();
327: PetscOptionsGetString(PETSC_NULL,"-on_error_attach_debugger",string,64,&flg1);
328: if (flg1) {
329: MPI_Errhandler err_handler;
331: PetscSetDebuggerFromString(string);
332: MPI_Errhandler_create((MPI_Handler_function*)Petsc_MPI_DebuggerOnError,&err_handler);
333: MPI_Errhandler_set(comm,err_handler);
334: PetscPushErrorHandler(PetscAttachDebuggerErrorHandler,0);
335: }
336: PetscOptionsGetString(PETSC_NULL,"-debug_terminal",string,64,&flg1);
337: if (flg1) { PetscSetDebugTerminal(string); }
338: PetscOptionsGetString(PETSC_NULL,"-start_in_debugger",string,64,&flg1);
339: PetscOptionsGetString(PETSC_NULL,"-stop_for_debugger",string,64,&flg2);
340: if (flg1 || flg2) {
341: PetscMPIInt size;
342: PetscInt lsize,*nodes;
343: MPI_Errhandler err_handler;
344: /*
345: we have to make sure that all processors have opened
346: connections to all other processors, otherwise once the
347: debugger has stated it is likely to receive a SIGUSR1
348: and kill the program.
349: */
350: MPI_Comm_size(PETSC_COMM_WORLD,&size);
351: if (size > 2) {
352: PetscMPIInt dummy;
353: MPI_Status status;
354: for (i=0; i<size; i++) {
355: if (rank != i) {
356: MPI_Send(&dummy,1,MPI_INT,i,109,PETSC_COMM_WORLD);
357: }
358: }
359: for (i=0; i<size; i++) {
360: if (rank != i) {
361: MPI_Recv(&dummy,1,MPI_INT,i,109,PETSC_COMM_WORLD,&status);
362: }
363: }
364: }
365: /* check if this processor node should be in debugger */
366: PetscMalloc(size*sizeof(PetscInt),&nodes);
367: lsize = size;
368: PetscOptionsGetIntArray(PETSC_NULL,"-debugger_nodes",nodes,&lsize,&flag);
369: if (flag) {
370: for (i=0; i<lsize; i++) {
371: if (nodes[i] == rank) { flag = PETSC_FALSE; break; }
372: }
373: }
374: if (!flag) {
375: PetscSetDebuggerFromString(string);
376: PetscPushErrorHandler(PetscAbortErrorHandler,0);
377: if (flg1) {
378: PetscAttachDebugger();
379: } else {
380: PetscStopForDebugger();
381: }
382: MPI_Errhandler_create((MPI_Handler_function*)Petsc_MPI_AbortOnError,&err_handler);
383: MPI_Errhandler_set(comm,err_handler);
384: }
385: PetscFree(nodes);
386: }
388: PetscOptionsGetString(PETSC_NULL,"-on_error_emacs",emacsmachinename,128,&flg1);
389: if (flg1 && !rank) {PetscPushErrorHandler(PetscEmacsClientErrorHandler,emacsmachinename);CHKERRQ(ierr)}
391: #if defined(PETSC_USE_SOCKET_VIEWER)
392: /*
393: Activates new sockets for zope if needed
394: */
395: ierr=PetscOptionsHasName(PETSC_NULL,"-zope", &flgz);
396: ierr=PetscOptionsHasName(PETSC_NULL,"-nostdout", &flgzout);
397: if(flgz){
399: int sockfd;
400: char hostname[256];
401: char username[256];
402: int remoteport = 9999;
403: ierr=PetscOptionsGetString(PETSC_NULL, "-zope", hostname, 256, &flgz);
404: if(!hostname[0]){
405: ierr=PetscGetHostName(hostname,256); }
406: ierr=PetscOpenSocket(hostname, remoteport, &sockfd);
407: PetscGetUserName(username, 256);
408: PETSC_ZOPEFD = fdopen(sockfd, "w");
409: if(flgzout){
410: PETSC_STDOUT = PETSC_ZOPEFD;
411: fprintf(PETSC_STDOUT, "<<<user>>> %s\n",username);
412: fprintf(PETSC_STDOUT, "<<<start>>>");
413: }
414: else{
415: fprintf(PETSC_ZOPEFD, "<<<user>>> %s\n",username);
416: fprintf(PETSC_ZOPEFD, "<<<start>>>");
417: }}
418: #endif
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," -help: prints help method for each option");
489: (*PetscHelpPrintf)(comm," -on_error_abort: cause an abort when an error is");
490: (*PetscHelpPrintf)(comm," detected. Useful \n only when run in the debugger\n");
491: (*PetscHelpPrintf)(comm," -on_error_attach_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
492: (*PetscHelpPrintf)(comm," start the debugger in new xterm\n");
493: (*PetscHelpPrintf)(comm," unless noxterm is given\n");
494: (*PetscHelpPrintf)(comm," -start_in_debugger [gdb,dbx,xxgdb,ups,noxterm]\n");
495: (*PetscHelpPrintf)(comm," start all processes in the debugger\n");
496: (*PetscHelpPrintf)(comm," -on_error_emacs <machinename>\n");
497: (*PetscHelpPrintf)(comm," emacs jumps to error file\n");
498: (*PetscHelpPrintf)(comm," -debugger_nodes [n1,n2,..] Nodes to start in debugger\n");
499: (*PetscHelpPrintf)(comm," -debugger_pause [m] : delay (in seconds) to attach debugger\n");
500: (*PetscHelpPrintf)(comm," -stop_for_debugger : prints message on how to attach debugger manually\n");
501: (*PetscHelpPrintf)(comm," waits the delay for you to attach\n");
502: (*PetscHelpPrintf)(comm," -display display: Location where graphics and debuggers are displayed\n");
503: (*PetscHelpPrintf)(comm," -no_signal_handler: do not trap error signals\n");
504: (*PetscHelpPrintf)(comm," -mpi_return_on_error: MPI returns error code, rather than abort on internal error\n");
505: (*PetscHelpPrintf)(comm," -fp_trap: stop on floating point exceptions\n");
506: (*PetscHelpPrintf)(comm," note on IBM RS6000 this slows run greatly\n");
507: (*PetscHelpPrintf)(comm," -malloc_dump <optional filename>: dump list of unfreed memory at conclusion\n");
508: (*PetscHelpPrintf)(comm," -malloc: use our error checking malloc\n");
509: (*PetscHelpPrintf)(comm," -malloc no: don't use error checking malloc\n");
510: (*PetscHelpPrintf)(comm," -malloc_info: prints total memory usage\n");
511: (*PetscHelpPrintf)(comm," -malloc_log: keeps log of all memory allocations\n");
512: (*PetscHelpPrintf)(comm," -malloc_debug: enables extended checking for memory corruption\n");
513: (*PetscHelpPrintf)(comm," -options_table: dump list of options inputted\n");
514: (*PetscHelpPrintf)(comm," -options_left: dump list of unused options\n");
515: (*PetscHelpPrintf)(comm," -options_left no: don't dump list of unused options\n");
516: (*PetscHelpPrintf)(comm," -tmp tmpdir: alternative /tmp directory\n");
517: (*PetscHelpPrintf)(comm," -shared_tmp: tmp directory is shared by all processors\n");
518: (*PetscHelpPrintf)(comm," -not_shared_tmp: each processor has separate tmp directory\n");
519: (*PetscHelpPrintf)(comm," -memory_info: print memory usage at end of run\n");
520: #if defined(PETSC_USE_LOG)
521: (*PetscHelpPrintf)(comm," -get_total_flops: total flops over all processors\n");
522: (*PetscHelpPrintf)(comm," -log[_all _summary]: logging objects and events\n");
523: (*PetscHelpPrintf)(comm," -log_trace [filename]: prints trace of all PETSc calls\n");
524: #if defined(PETSC_HAVE_MPE)
525: (*PetscHelpPrintf)(comm," -log_mpe: Also create logfile viewable through upshot\n");
526: #endif
527: (*PetscHelpPrintf)(comm," -info <optional filename>: print informative messages about the calculations\n");
528: #endif
529: (*PetscHelpPrintf)(comm," -v: prints PETSc version number and release date\n");
530: (*PetscHelpPrintf)(comm," -options_file <file>: reads options from file\n");
531: (*PetscHelpPrintf)(comm," -petsc_sleep n: sleeps n seconds before running program\n");
532: (*PetscHelpPrintf)(comm,"-----------------------------------------------\n");
533: }
535: PetscOptionsGetInt(PETSC_NULL,"-petsc_sleep",&si,&flg1);
536: if (flg1) {
537: PetscSleep(si);
538: }
540: PetscOptionsGetString(PETSC_NULL,"-info_exclude",mname,PETSC_MAX_PATH_LEN,&flg1);
541: PetscStrstr(mname,"null",&f);
542: if (f) {
543: PetscInfoDeactivateClass(PETSC_NULL);
544: }
547: return(0);
548: }