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