xref: /petsc/include/petscerror.h (revision 4bf112e79d3f87ddad6dbf7b300a6f889c1e7d34)
1 /*
2     Contains all error handling code for PETSc.
3 */
4 #if !defined(__PETSCERROR_H)
5 #define __PETSCERROR_H
6 #include "petsc.h"
7 PETSC_EXTERN_CXX_BEGIN
8 
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.
15 */
16 #if !defined(__SDIR__)
17 #define __SDIR__ "unknowndirectory/"
18 #endif
19 
20 /*
21    Defines the function where the compiled source is located; used
22    in printing error messages.
23 */
24 #if !defined(__FUNCT__)
25 #define __FUNCT__ "User provided function"
26 #endif
27 
28 /*
29      These are the generic error codes. These error codes are used
30      many different places in the PETSc source code. The string versions are
31      at src/sys/src/error/err.c any changes here must also be made there
32 
33 */
34 #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
35 #define PETSC_ERR_MEM_MALLOC_0     85   /* cannot malloc zero size */
36 #define PETSC_ERR_SUP              56   /* no support for requested operation */
37 #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
38 #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
39 #define PETSC_ERR_SIG              59   /* signal received */
40 #define PETSC_ERR_FP               72   /* floating point exception */
41 #define PETSC_ERR_COR              74   /* corrupted PETSc object */
42 #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
43 #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
44 #define PETSC_ERR_MEMC             78   /* memory corruption */
45 #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
46 
47 #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
48 #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
49 #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
50 #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
51 #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
52 #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
53 #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
54 #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
55 #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
56 #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
57 #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
58 #define PETSC_ERR_ARG_UNKNOWN_TYPE 86  /* type name doesn't match any registered type */
59 
60 #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
61 #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
62 #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
63 #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
64 
65 #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
66 #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
67 
68 #if defined(PETSC_USE_DEBUG)
69 
70 /*MC
71    SETERRQ - Macro that is called when an error has been detected,
72 
73    Not Collective
74 
75    Synopsis:
76    void SETERRQ(int errorcode,char *message)
77 
78 
79    Input Parameters:
80 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
81 -  message - error message
82 
83   Level: beginner
84 
85    Notes:
86     Once the error handler is called the calling function is then returned from with the given error code.
87 
88     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
89 
90 
91    Experienced users can set the error handler with PetscPushErrorHandler().
92 
93    Concepts: error^setting condition
94 
95 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
96 M*/
97 #define SETERRQ(n,s)              {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);}
98 
99 /*MC
100    SETERRQ1 - Macro that is called when an error has been detected,
101 
102    Not Collective
103 
104    Synopsis:
105    void SETERRQ1(int errorcode,char *formatmessage,arg)
106 
107 
108    Input Parameters:
109 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
110 .  message - error message in the printf format
111 -  arg - argument (for example an integer, string or double)
112 
113   Level: beginner
114 
115    Notes:
116     Once the error handler is called the calling function is then returned from with the given error code.
117 
118    Experienced users can set the error handler with PetscPushErrorHandler().
119 
120    Concepts: error^setting condition
121 
122 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
123 M*/
124 #define SETERRQ1(n,s,a1)          {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1);}
125 
126 /*MC
127    SETERRQ2 - Macro that is called when an error has been detected,
128 
129    Not Collective
130 
131    Synopsis:
132    void SETERRQ2(int errorcode,char *formatmessage,arg1,arg2)
133 
134 
135    Input Parameters:
136 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
137 .  message - error message in the printf format
138 .  arg1 - argument (for example an integer, string or double)
139 -  arg2 - argument (for example an integer, string or double)
140 
141   Level: beginner
142 
143    Notes:
144     Once the error handler is called the calling function is then returned from with the given error code.
145 
146    Experienced users can set the error handler with PetscPushErrorHandler().
147 
148    Concepts: error^setting condition
149 
150 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
151 M*/
152 #define SETERRQ2(n,s,a1,a2)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2);}
153 
154 /*MC
155    SETERRQ3 - Macro that is called when an error has been detected,
156 
157    Not Collective
158 
159    Synopsis:
160    void SETERRQ3(int errorcode,char *formatmessage,arg1,arg2,arg3)
161 
162 
163    Input Parameters:
164 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
165 .  message - error message in the printf format
166 .  arg1 - argument (for example an integer, string or double)
167 .  arg2 - argument (for example an integer, string or double)
168 -  arg3 - argument (for example an integer, string or double)
169 
170   Level: beginner
171 
172    Notes:
173     Once the error handler is called the calling function is then returned from with the given error code.
174 
175    Experienced users can set the error handler with PetscPushErrorHandler().
176 
177    Concepts: error^setting condition
178 
179 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
180 M*/
181 #define SETERRQ3(n,s,a1,a2,a3)    {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3);}
182 
183 #define SETERRQ4(n,s,a1,a2,a3,a4) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4);}
184 #define SETERRQ5(n,s,a1,a2,a3,a4,a5)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5);}
185 #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);}
186 #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);}
187 #define SETERRABORT(comm,n,s)     {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);MPI_Abort(comm,n);}
188 
189 /*MC
190    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
191 
192    Not Collective
193 
194    Synopsis:
195    void CHKERRQ(int errorcode)
196 
197 
198    Input Parameters:
199 .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
200 
201   Level: beginner
202 
203    Notes:
204     Once the error handler is called the calling function is then returned from with the given error code.
205 
206    Experienced users can set the error handler with PetscPushErrorHandler().
207 
208    Concepts: error^setting condition
209 
210 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
211 M*/
212 #define CHKERRQ(n)             if (n) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
213 
214 #define CHKERRABORT(comm,n)    if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");MPI_Abort(comm,n);}
215 #define CHKERRCONTINUE(n)      if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
216 
217 /*MC
218    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
219 
220    Not Collective
221 
222    Synopsis:
223    CHKMEMQ;
224 
225   Level: beginner
226 
227    Notes:
228     Must run with the option -trdebug to enable this option
229 
230     Once the error handler is called the calling function is then returned from with the given error code.
231 
232     By defaults prints location where memory that is corrupted was allocated.
233 
234    Concepts: memory corruption
235 
236 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2(),
237           PetscTrValid()
238 M*/
239 #define CHKMEMQ {int _7_ierr = PetscTrValid(__LINE__,__FUNCT__,__FILE__,__SDIR__);CHKERRQ(_7_ierr);}
240 
241 #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
242 extern  PetscErrorCode __gierr;
243 #define _   __gierr =
244 #define ___  CHKERRQ(__gierr);
245 #endif
246 
247 #else
248 #define SETERRQ(n,s) ;
249 #define SETERRQ1(n,s,a1) ;
250 #define SETERRQ2(n,s,a1,a2) ;
251 #define SETERRQ3(n,s,a1,a2,a3) ;
252 #define SETERRQ4(n,s,a1,a2,a3,a4) ;
253 #define SETERRABORT(comm,n,s) ;
254 
255 #define CHKERRQ(n)     ;
256 #define CHKERRABORT(comm,n) ;
257 #define CHKERRCONTINUE(n) ;
258 
259 #define CHKMEMQ        ;
260 
261 #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
262 #define _
263 #define ___
264 #endif
265 
266 #endif
267 
268 EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
269 EXTERN PetscErrorCode PetscTraceBackErrorHandler(int,const char*,const char*,const char*,int,int,const char*,void*);
270 EXTERN PetscErrorCode PetscIgnoreErrorHandler(int,const char*,const char*,const char*,int,int,const char*,void*);
271 EXTERN PetscErrorCode PetscEmacsClientErrorHandler(int,const char*,const char*,const char*,int,int,const char*,void*);
272 EXTERN PetscErrorCode PetscStopErrorHandler(int,const char*,const char*,const char*,int,int,const char*,void*);
273 EXTERN PetscErrorCode PetscAbortErrorHandler(int,const char*,const char*,const char*,int,int,const char*,void*);
274 EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(int,const char*,const char*,const char*,int,int,const char*,void*);
275 EXTERN PetscErrorCode PetscError(int,const char*,const char*,const char*,int,int,const char*,...) PETSC_PRINTF_FORMAT_CHECK(7,8);
276 EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(int,const char*,const char*,const char*,int,int,const char*,void*),void*);
277 EXTERN PetscErrorCode PetscPopErrorHandler(void);
278 EXTERN PetscErrorCode PetscDefaultSignalHandler(int,void*);
279 EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
280 EXTERN PetscErrorCode PetscPopSignalHandler(void);
281 
282 typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
283 EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
284 
285 /*
286       Allows the code to build a stack frame as it runs
287 */
288 #if defined(PETSC_USE_STACK)
289 
290 #define PETSCSTACKSIZE 15
291 
292 typedef struct  {
293   const char *function[PETSCSTACKSIZE];
294   const char *file[PETSCSTACKSIZE];
295   const char *directory[PETSCSTACKSIZE];
296         int  line[PETSCSTACKSIZE];
297         int currentsize;
298 } PetscStack;
299 
300 extern PetscStack *petscstack;
301 EXTERN PetscErrorCode PetscStackCopy(PetscStack*,PetscStack*);
302 EXTERN PetscErrorCode PetscStackPrint(PetscStack*,FILE* fp);
303 
304 #define PetscStackActive (petscstack != 0)
305 
306 
307 /*MC
308    PetscFunctionBegin - First executable line of each PETSc function
309         used for error handling.
310 
311    Synopsis:
312    void PetscFunctionBegin;
313 
314    Usage:
315 .vb
316      int something;
317 
318      PetscFunctionBegin;
319 .ve
320 
321    Notes:
322      Not available in Fortran
323 
324    Level: developer
325 
326 .seealso: PetscFunctionReturn()
327 
328 .keywords: traceback, error handling
329 M*/
330 #define PetscFunctionBegin \
331   {\
332    if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
333     petscstack->function[petscstack->currentsize]  = __FUNCT__; \
334     petscstack->file[petscstack->currentsize]      = __FILE__; \
335     petscstack->directory[petscstack->currentsize] = __SDIR__; \
336     petscstack->line[petscstack->currentsize]      = __LINE__; \
337     petscstack->currentsize++; \
338   }}
339 
340 #define PetscStackPush(n) \
341   {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
342     petscstack->function[petscstack->currentsize]  = n; \
343     petscstack->file[petscstack->currentsize]      = "unknown"; \
344     petscstack->directory[petscstack->currentsize] = "unknown"; \
345     petscstack->line[petscstack->currentsize]      = 0; \
346     petscstack->currentsize++; \
347   }}
348 
349 #define PetscStackPop \
350   {if (petscstack && petscstack->currentsize > 0) {     \
351     petscstack->currentsize--; \
352     petscstack->function[petscstack->currentsize]  = 0; \
353     petscstack->file[petscstack->currentsize]      = 0; \
354     petscstack->directory[petscstack->currentsize] = 0; \
355     petscstack->line[petscstack->currentsize]      = 0; \
356   }};
357 
358 /*MC
359    PetscFunctionReturn - Last executable line of each PETSc function
360         used for error handling. Replaces return()
361 
362    Synopsis:
363    void PetscFunctionReturn(0);
364 
365    Usage:
366 .vb
367     ....
368      PetscFunctionReturn(0);
369    }
370 .ve
371 
372    Notes:
373      Not available in Fortran
374 
375    Level: developer
376 
377 .seealso: PetscFunctionBegin()
378 
379 .keywords: traceback, error handling
380 M*/
381 #define PetscFunctionReturn(a) \
382   {\
383   PetscStackPop; \
384   return(a);}
385 
386 #define PetscFunctionReturnVoid() \
387   {\
388   PetscStackPop; \
389   return;}
390 
391 
392 #else
393 
394 #define PetscFunctionBegin
395 #define PetscFunctionReturn(a)  return(a)
396 #define PetscFunctionReturnVoid() return()
397 #define PetscStackPop
398 #define PetscStackPush(f)
399 #define PetscStackActive        0
400 
401 #endif
402 
403 EXTERN PetscErrorCode PetscStackCreate(void);
404 EXTERN PetscErrorCode PetscStackView(PetscViewer);
405 EXTERN PetscErrorCode PetscStackDestroy(void);
406 EXTERN PetscErrorCode PetscStackPublish(void);
407 EXTERN PetscErrorCode PetscStackDepublish(void);
408 
409 
410 PETSC_EXTERN_CXX_END
411 #endif
412