Actual source code: petscerror.h
1: /*
2: Contains all error handling interfaces for PETSc.
3: */
6: #include petsc.h
9: /*
10: Defines the directory where the compiled source is located; used
11: in printing error messages. Each makefile has an entry
12: LOCDIR = thedirectory
13: and bmake/common_variables includes in CCPPFLAGS -D__SDIR__='"${LOCDIR}"'
14: which is a flag passed to the C/C++ compilers. This declaration below
15: is only needed if some code is compiled without the -D__SDIR__
16: */
19: #endif
21: /*
22: Defines the function where the compiled source is located; used
23: in printing error messages. This is defined here in case the user
24: does not declare it.
25: */
28: #endif
30: /*
31: These are the generic error codes. These error codes are used
32: many different places in the PETSc source code. The string versions are
33: at src/sys/error/err.c any changes here must also be made there
34: These are also define in include/finclude/petscerror.h any CHANGES here
35: must be also made there.
37: */
38: #define PETSC_ERR_MIN_VALUE 54 /* should always be one less then the smallest value */
40: #define PETSC_ERR_MEM 55 /* unable to allocate requested memory */
41: #define PETSC_ERR_SUP 56 /* no support for requested operation */
42: #define PETSC_ERR_SUP_SYS 57 /* no support for requested operation on this computer system */
43: #define PETSC_ERR_ORDER 58 /* operation done in wrong order */
44: #define PETSC_ERR_SIG 59 /* signal received */
45: #define PETSC_ERR_FP 72 /* floating point exception */
46: #define PETSC_ERR_COR 74 /* corrupted PETSc object */
47: #define PETSC_ERR_LIB 76 /* error in library called by PETSc */
48: #define PETSC_ERR_PLIB 77 /* PETSc library generated inconsistent data */
49: #define PETSC_ERR_MEMC 78 /* memory corruption */
50: #define PETSC_ERR_CONV_FAILED 82 /* iterative method (KSP or SNES) failed */
51: #define PETSC_ERR_USER 83 /* user has not provided needed function */
53: #define PETSC_ERR_ARG_SIZ 60 /* nonconforming object sizes used in operation */
54: #define PETSC_ERR_ARG_IDN 61 /* two arguments not allowed to be the same */
55: #define PETSC_ERR_ARG_WRONG 62 /* wrong argument (but object probably ok) */
56: #define PETSC_ERR_ARG_CORRUPT 64 /* null or corrupted PETSc object as argument */
57: #define PETSC_ERR_ARG_OUTOFRANGE 63 /* input argument, out of range */
58: #define PETSC_ERR_ARG_BADPTR 68 /* invalid pointer argument */
59: #define PETSC_ERR_ARG_NOTSAMETYPE 69 /* two args must be same object type */
60: #define PETSC_ERR_ARG_NOTSAMECOMM 80 /* two args must be same communicators */
61: #define PETSC_ERR_ARG_WRONGSTATE 73 /* object in argument is in wrong state, e.g. unassembled mat */
62: #define PETSC_ERR_ARG_INCOMP 75 /* two arguments are incompatible */
63: #define PETSC_ERR_ARG_NULL 85 /* argument is null that should not be */
64: #define PETSC_ERR_ARG_UNKNOWN_TYPE 86 /* type name doesn't match any registered type */
65: #define PETSC_ERR_ARG_DOMAIN 87 /* argument is not in domain of function */
67: #define PETSC_ERR_FILE_OPEN 65 /* unable to open file */
68: #define PETSC_ERR_FILE_READ 66 /* unable to read from file */
69: #define PETSC_ERR_FILE_WRITE 67 /* unable to write to file */
70: #define PETSC_ERR_FILE_UNEXPECTED 79 /* unexpected data in file */
72: #define PETSC_ERR_MAT_LU_ZRPVT 71 /* detected a zero pivot during LU factorization */
73: #define PETSC_ERR_MAT_CH_ZRPVT 81 /* detected a zero pivot during Cholesky factorization */
75: #define PETSC_ERR_MAX_VALUE 88 /* this is always the one more than the largest error code */
77: #if defined(PETSC_USE_ERRORCHECKING)
79: /*MC
80: SETERRQ - Macro that is called when an error has been detected,
82: Not Collective
84: Synopsis:
85: void SETERRQ(PetscErrorCode errorcode,char *message)
88: Input Parameters:
89: + errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
90: - message - error message
92: Level: beginner
94: Notes:
95: Once the error handler is called the calling function is then returned from with the given error code.
97: See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
100: Experienced users can set the error handler with PetscPushErrorHandler().
102: Concepts: error^setting condition
104: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
105: M*/
106: #define SETERRQ(n,s) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);}
108: /*MC
109: SETERRQ1 - Macro that is called when an error has been detected,
111: Not Collective
113: Synopsis:
114: void SETERRQ1(PetscErrorCode errorcode,char *formatmessage,arg)
117: Input Parameters:
118: + errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
119: . message - error message in the printf format
120: - arg - argument (for example an integer, string or double)
122: Level: beginner
124: Notes:
125: Once the error handler is called the calling function is then returned from with the given error code.
127: Experienced users can set the error handler with PetscPushErrorHandler().
129: Concepts: error^setting condition
131: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
132: M*/
133: #define SETERRQ1(n,s,a1) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1);}
135: /*MC
136: SETERRQ2 - Macro that is called when an error has been detected,
138: Not Collective
140: Synopsis:
141: void SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
144: Input Parameters:
145: + errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
146: . message - error message in the printf format
147: . arg1 - argument (for example an integer, string or double)
148: - arg2 - argument (for example an integer, string or double)
150: Level: beginner
152: Notes:
153: Once the error handler is called the calling function is then returned from with the given error code.
155: Experienced users can set the error handler with PetscPushErrorHandler().
157: Concepts: error^setting condition
159: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
160: M*/
161: #define SETERRQ2(n,s,a1,a2) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2);}
163: /*MC
164: SETERRQ3 - Macro that is called when an error has been detected,
166: Not Collective
168: Synopsis:
169: void SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
172: Input Parameters:
173: + errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
174: . message - error message in the printf format
175: . arg1 - argument (for example an integer, string or double)
176: . arg2 - argument (for example an integer, string or double)
177: - arg3 - argument (for example an integer, string or double)
179: Level: beginner
181: Notes:
182: Once the error handler is called the calling function is then returned from with the given error code.
184: There are also versions for 4, 5, 6 and 7 arguments.
186: Experienced users can set the error handler with PetscPushErrorHandler().
188: Concepts: error^setting condition
190: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
191: M*/
192: #define SETERRQ3(n,s,a1,a2,a3) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3);}
194: #define SETERRQ4(n,s,a1,a2,a3,a4) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4);}
195: #define SETERRQ5(n,s,a1,a2,a3,a4,a5) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5);}
196: #define SETERRQ6(n,s,a1,a2,a3,a4,a5,a6) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5,a6);}
197: #define SETERRQ7(n,s,a1,a2,a3,a4,a5,a6,a7) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5,a6,a7);}
198: #define SETERRABORT(comm,n,s) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);MPI_Abort(comm,n);}
200: /*MC
201: CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
203: Not Collective
205: Synopsis:
206: void CHKERRQ(PetscErrorCode errorcode)
209: Input Parameters:
210: . errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
212: Level: beginner
214: Notes:
215: Once the error handler is called the calling function is then returned from with the given error code.
217: Experienced users can set the error handler with PetscPushErrorHandler().
219: CHKERRQ(n) is fundamentally a macro replacement for
220: if (n) return(PetscError(...,n,...));
222: Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
223: highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
224: it cannot be used in functions which return(void) or any other datatype. In these types of functions,
225: a more appropriate construct for using PETSc Error Handling would be
226: if (n) {PetscError(....); return(YourReturnType);}
228: Concepts: error^setting condition
230: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
231: M*/
232: #define CHKERRQ(n) if (n) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
234: #define CHKERRABORT(comm,n) if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");MPI_Abort(comm,n);}
235: #define CHKERRCONTINUE(n) if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
237: /*MC
238: CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
240: Not Collective
242: Synopsis:
243: CHKMEMQ;
245: Level: beginner
247: Notes:
248: Must run with the option -malloc_debug to enable this option
250: Once the error handler is called the calling function is then returned from with the given error code.
252: By defaults prints location where memory that is corrupted was allocated.
254: Use CHKMEMA for functions that return void
256: Concepts: memory corruption
258: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
259: PetscMallocValidate()
260: M*/
261: #define CHKMEMQ {PetscErrorCode _7_PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);CHKERRQ(_7_ierr);}
263: #define CHKMEMA {PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);}
265: #if defined(PETSC_UNDERSCORE_CHKERR)
267: #define _ __g
269: #endif
271: #define PETSC_EXCEPTIONS_MAX 256
277: EXTERN PetscErrorCode PetscExceptionPush(PetscErrorCode);
278: EXTERN void PetscExceptionPop(PetscErrorCode);
280: EXTERN PetscErrorCode PetscErrorSetCatchable(PetscErrorCode,PetscTruth);
281: EXTERN PetscTruth PetscErrorIsCatchable(PetscErrorCode);
282: /*MC
283: PetscExceptionCaught - Indicates if a specific exception zierr was caught.
285: Not Collective
287: Synopsis:
288: PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr);
290: Input Parameters:
291: + xierr - error code returned from PetscExceptionTry1()
292: - zierr - error code you want it to be
294: Level: advanced
296: Notes:
297: PETSc must not be configured using the option --with-errorchecking=0 for this to work
299: Use PetscExceptionValue() to see if the current error code is one that has been "tried"
301: Concepts: exceptions, exception hanlding
303: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
304: CHKERRQ(), PetscExceptionTry1(), PetscExceptionValue()
305: M*/
306: PETSC_STATIC_INLINE PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr) {
307: PetscInt i;
308: if (xierr != zierr) return PETSC_FALSE;
309: for (i=0; i<PetscErrorUncatchableCount; i++) {
310: if (PetscErrorUncatchable[i] == zierr) {
311: return PETSC_FALSE;
312: }
313: }
314: return PETSC_TRUE;
315: }
317: /*MC
318: PetscExceptionValue - Indicates if the error code is one that is currently being tried
320: Not Collective
322: Synopsis:
323: PetscTruth PetscExceptionValue(PetscErrorCode xierr);
325: Input Parameters:
326: . xierr - error code
328: Level: developer
330: Notes:
331: PETSc must not be configured using the option --with-errorchecking=0 for this to work
333: Use PetscExceptionCaught() to see if the current error code is EXACTLY the one you want
335: Concepts: exceptions, exception hanlding
337: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
338: CHKERRQ(), PetscExceptionTry1(), PetscExceptionCaught()
339: M*/
340: PETSC_STATIC_INLINE PetscTruth PetscExceptionValue(PetscErrorCode zierr) {
341: PetscInt i;
342: for (i=0; i<PetscExceptionsCount; i++) {
343: if (PetscExceptions[i] == zierr) {
344: return PETSC_TRUE;
345: }
346: }
347: return PETSC_FALSE;
348: }
350: /*MC
351: PetscExceptionTry1 - Runs the routine, causing a particular error code to be treated as an exception,
352: rather than an error. That is if that error code is treated the program returns to this level,
353: but does not call the error handlers
355: Not Collective
357: Synopsis:
358: PetscExceptionTry1(PetscErrorCode routine(....),PetscErrorCode);
360: Level: advanced
362: Notes:
363: PETSc must not be configured using the option --with-errorchecking=0 for this to work
365: Note: In general, the outer most try on an exception is the one that will be caught (that is trys down in
366: PETSc code will not usually handle an exception that was issued above). See SNESSolve() for an example
367: of how the local try is ignored if a higher (in the stack) one is also in effect.
369: Concepts: exceptions, exception hanlding
371: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
372: CHKERRQ(), PetscExceptionCaught(), PetscExceptionPush(), PetscExceptionPop()
373: M*/
375: #define PetscExceptionTry1(a,b) (PetscExceptionTmp = PetscExceptionPush(b)) ? PetscExceptionTmp : (PetscExceptionTmp = a , PetscExceptionPop(b),PetscExceptionTmp)
377: #else
379: /*
380: These are defined to be empty for when error checking is turned off, with config/configure.py --with-errorchecking=0
381: */
383: #define SETERRQ(n,s) ;
384: #define SETERRQ1(n,s,a1) ;
385: #define SETERRQ2(n,s,a1,a2) ;
386: #define SETERRQ3(n,s,a1,a2,a3) ;
387: #define SETERRQ4(n,s,a1,a2,a3,a4) ;
388: #define SETERRQ5(n,s,a1,a2,a3,a4,a5) ;
389: #define SETERRQ6(n,s,a1,a2,a3,a4,a5,a6) ;
390: #define SETERRABORT(comm,n,s) ;
392: #define CHKERRQ(n) ;
393: #define CHKERRABORT(comm,n) ;
394: #define CHKERRCONTINUE(n) ;
396: #define CHKMEMQ ;
398: #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
399: #define _
401: #endif
403: #define PetscErrorSetCatchable(a,b) 0
404: #define PetscExceptionCaught(a,b) PETSC_FALSE
405: #define PetscExceptionValue(a) PETSC_FALSE
406: #define PetscExceptionTry1(a,b) a
407: #endif
409: EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
410: EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
411: EXTERN PetscErrorCode PetscTraceBackErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
412: EXTERN PetscErrorCode PetscIgnoreErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
413: EXTERN PetscErrorCode PetscEmacsClientErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
414: EXTERN PetscErrorCode PetscStopErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
415: EXTERN PetscErrorCode PetscAbortErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
416: EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
417: EXTERN PetscErrorCode PetscReturnErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
418: EXTERN PetscErrorCode PetscError(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,...) PETSC_PRINTF_FORMAT_CHECK(7,8);
419: EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*),void*);
420: EXTERN PetscErrorCode PetscPopErrorHandler(void);
421: EXTERN PetscErrorCode PetscDefaultSignalHandler(int,void*);
422: EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
423: EXTERN PetscErrorCode PetscPopSignalHandler(void);
425: typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
426: EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
428: /*
429: Allows the code to build a stack frame as it runs
430: */
431: #if defined(PETSC_USE_DEBUG)
433: #define PETSCSTACKSIZE 15
435: typedef struct {
436: const char *function[PETSCSTACKSIZE];
437: const char *file[PETSCSTACKSIZE];
438: const char *directory[PETSCSTACKSIZE];
439: int line[PETSCSTACKSIZE];
440: int currentsize;
441: } PetscStack;
444: EXTERN PetscErrorCode PetscStackCopy(PetscStack*,PetscStack*);
445: EXTERN PetscErrorCode PetscStackPrint(PetscStack*,FILE* fp);
447: #define PetscStackActive (petscstack != 0)
450: /*MC
452: used for error handling.
454: Synopsis:
457: Usage:
458: .vb
459: int something;
462: .ve
464: Notes:
465: Not available in Fortran
467: Level: developer
469: .seealso: PetscFunctionReturn()
471: .keywords: traceback, error handling
472: M*/
474: {\
475: if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) { \
476: petscstack->function[petscstack->currentsize] = __FUNCT__; \
477: petscstack->file[petscstack->currentsize] = __FILE__; \
478: petscstack->directory[petscstack->currentsize] = __SDIR__; \
479: petscstack->line[petscstack->currentsize] = __LINE__; \
480: petscstack->currentsize++; \
481: }}
483: #define PetscStackPush(n) \
484: {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) { \
485: petscstack->function[petscstack->currentsize] = n; \
486: petscstack->file[petscstack->currentsize] = "unknown"; \
487: petscstack->directory[petscstack->currentsize] = "unknown"; \
488: petscstack->line[petscstack->currentsize] = 0; \
489: petscstack->currentsize++; \
490: }}
492: #define PetscStackPop \
493: {if (petscstack && petscstack->currentsize > 0) { \
494: petscstack->currentsize--; \
495: petscstack->function[petscstack->currentsize] = 0; \
496: petscstack->file[petscstack->currentsize] = 0; \
497: petscstack->directory[petscstack->currentsize] = 0; \
498: petscstack->line[petscstack->currentsize] = 0; \
499: }};
501: /*MC
502: PetscFunctionReturn - Last executable line of each PETSc function
503: used for error handling. Replaces return()
505: Synopsis:
506: void return(0);
508: Usage:
509: .vb
510: ....
511: return(0);
512: }
513: .ve
515: Notes:
516: Not available in Fortran
518: Level: developer
522: .keywords: traceback, error handling
523: M*/
524: #define PetscFunctionReturn(a) \
525: {\
526: PetscStackPop; \
527: return(a);}
529: #define PetscFunctionReturnVoid() \
530: {\
531: PetscStackPop; \
532: return;}
535: #else
538: #define PetscFunctionReturn(a) return(a)
539: #define PetscFunctionReturnVoid() return
540: #define PetscStackPop
541: #define PetscStackPush(f)
542: #define PetscStackActive 0
544: #endif
546: EXTERN PetscErrorCode PetscStackCreate(void);
547: EXTERN PetscErrorCode PetscStackView(PetscViewer);
548: EXTERN PetscErrorCode PetscStackDestroy(void);
549: EXTERN PetscErrorCode PetscStackPublish(void);
550: EXTERN PetscErrorCode PetscStackDepublish(void);
554: #endif