xref: /petsc/include/petscerror.h (revision d6f11fddcbc5503a4d8fb26ab62e20dc044b7e4b)
1 /*
2     Contains all error handling interfaces for PETSc.
3 */
4 #if !defined(__PETSCERROR_H)
5 #define __PETSCERROR_H
6 
7 /*
8      These are the generic error codes. These error codes are used
9      many different places in the PETSc source code. The string versions are
10      at src/sys/error/err.c any changes here must also be made there
11      These are also define in include/petsc/finclude/petscerror.h any CHANGES here
12      must be also made there.
13 
14 */
15 #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
16 
17 #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
18 #define PETSC_ERR_SUP              56   /* no support for requested operation */
19 #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
20 #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
21 #define PETSC_ERR_SIG              59   /* signal received */
22 #define PETSC_ERR_FP               72   /* floating point exception */
23 #define PETSC_ERR_COR              74   /* corrupted PETSc object */
24 #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
25 #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
26 #define PETSC_ERR_MEMC             78   /* memory corruption */
27 #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
28 #define PETSC_ERR_USER             83   /* user has not provided needed function */
29 #define PETSC_ERR_SYS              88   /* error in system call */
30 #define PETSC_ERR_POINTER          70   /* pointer does not point to valid address */
31 #define PETSC_ERR_MPI_LIB_INCOMP   87   /* MPI library at runtime is not compatible with MPI user compiled with */
32 
33 #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
34 #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
35 #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
36 #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
37 #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
38 #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
39 #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
40 #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
41 #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
42 #define PETSC_ERR_ARG_TYPENOTSET   89   /* the type of the object has not yet been set */
43 #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
44 #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
45 #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
46 
47 #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
48 #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
49 #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
50 #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
51 
52 #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
53 #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
54 
55 #define PETSC_ERR_INT_OVERFLOW     84
56 
57 #define PETSC_ERR_FLOP_COUNT       90
58 #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
59 #define PETSC_ERR_MISSING_FACTOR   92  /* MatGetFactor() failed */
60 #define PETSC_ERR_OPT_OVERWRITE    93  /* attempted to over wrote options which should not be changed */
61 
62 #define PETSC_ERR_MAX_VALUE        94  /* this is always the one more than the largest error code */
63 
64 #define PetscStringizeArg(a) #a
65 #define PetscStringize(a) PetscStringizeArg(a)
66 
67 
68 /*MC
69    SETERRQ - Macro to be called when an error has been detected,
70 
71    Synopsis:
72    #include <petscsys.h>
73    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode ierr,char *message)
74 
75    Collective on MPI_Comm
76 
77    Input Parameters:
78 +  comm - A communicator, use PETSC_COMM_SELF unless you know all ranks of another communicator will detect the error
79 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
80 -  message - error message
81 
82   Level: beginner
83 
84    Notes:
85     Once the error handler is called the calling function is then returned from with the given error code.
86 
87     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
88 
89     Experienced users can set the error handler with PetscPushErrorHandler().
90 
91    Concepts: error^setting condition
92 
93 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
94 M*/
95 #define SETERRQ(comm,ierr,s) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s)
96 
97 /*MC
98    SETERRMPI - Macro to be called when an error has been detected within an MPI callback function
99 
100    Synopsis:
101    #include <petscsys.h>
102    PetscErrorCode SETERRMPI(MPI_Comm comm,PetscErrorCode ierr,char *message)
103 
104    Collective on MPI_Comm
105 
106    Input Parameters:
107 +  comm - A communicator, use PETSC_COMM_SELF unless you know all ranks of another communicator will detect the error
108 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
109 -  message - error message
110 
111   Level: developer
112 
113    Notes:
114     This macro is FOR USE IN MPI CALLBACK FUNCTIONS ONLY, such as those passed to MPI_Comm_create_keyval(). It always returns the error code PETSC_MPI_ERROR_CODE
115     which is registered with MPI_Add_error_code() when PETSc is initialized.
116 
117    Concepts: error^setting condition
118 
119 .seealso: SETERRQ(), CHKERRQ(), CHKERRMPI(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
120 M*/
121 #define SETERRMPI(comm,ierr,s) return (PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s),PETSC_MPI_ERROR_CODE)
122 
123 /*MC
124    SETERRQ1 - Macro that is called when an error has been detected,
125 
126    Synopsis:
127    #include <petscsys.h>
128    PetscErrorCode SETERRQ1(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg)
129 
130    Collective on MPI_Comm
131 
132    Input Parameters:
133 +  comm - A communicator, so that the error can be collective
134 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
135 .  message - error message in the printf format
136 -  arg - argument (for example an integer, string or double)
137 
138   Level: beginner
139 
140    Notes:
141     Once the error handler is called the calling function is then returned from with the given error code.
142 
143    Experienced users can set the error handler with PetscPushErrorHandler().
144 
145    Concepts: error^setting condition
146 
147 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
148 M*/
149 #define SETERRQ1(comm,ierr,s,a1) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1)
150 
151 /*MC
152    SETERRQ2 - Macro that is called when an error has been detected,
153 
154    Synopsis:
155    #include <petscsys.h>
156    PetscErrorCode SETERRQ2(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2)
157 
158    Collective on MPI_Comm
159 
160    Input Parameters:
161 +  comm - A communicator, so that the error can be collective
162 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
163 .  message - error message in the printf format
164 .  arg1 - argument (for example an integer, string or double)
165 -  arg2 - argument (for example an integer, string or double)
166 
167   Level: beginner
168 
169    Notes:
170     Once the error handler is called the calling function is then returned from with the given error code.
171 
172    Experienced users can set the error handler with PetscPushErrorHandler().
173 
174    Concepts: error^setting condition
175 
176 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
177 M*/
178 #define SETERRQ2(comm,ierr,s,a1,a2) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2)
179 
180 /*MC
181    SETERRQ3 - Macro that is called when an error has been detected,
182 
183    Synopsis:
184    #include <petscsys.h>
185    PetscErrorCode SETERRQ3(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
186 
187    Collective on MPI_Comm
188 
189    Input Parameters:
190 +  comm - A communicator, so that the error can be collective
191 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
192 .  message - error message in the printf format
193 .  arg1 - argument (for example an integer, string or double)
194 .  arg2 - argument (for example an integer, string or double)
195 -  arg3 - argument (for example an integer, string or double)
196 
197   Level: beginner
198 
199    Notes:
200     Once the error handler is called the calling function is then returned from with the given error code.
201 
202     There are also versions for 4, 5, 6 and 7 arguments.
203 
204    Experienced users can set the error handler with PetscPushErrorHandler().
205 
206    Concepts: error^setting condition
207 
208 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
209 M*/
210 #define SETERRQ3(comm,ierr,s,a1,a2,a3) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3)
211 
212 /*MC
213    SETERRQ4 - Macro that is called when an error has been detected,
214 
215    Synopsis:
216    #include <petscsys.h>
217    PetscErrorCode SETERRQ4(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
218 
219    Collective on MPI_Comm
220 
221    Input Parameters:
222 +  comm - A communicator, so that the error can be collective
223 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
224 .  message - error message in the printf format
225 .  arg1 - argument (for example an integer, string or double)
226 .  arg2 - argument (for example an integer, string or double)
227 .  arg3 - argument (for example an integer, string or double)
228 -  arg4 - argument (for example an integer, string or double)
229 
230   Level: beginner
231 
232    Notes:
233     Once the error handler is called the calling function is then returned from with the given error code.
234 
235     There are also versions for 4, 5, 6 and 7 arguments.
236 
237    Experienced users can set the error handler with PetscPushErrorHandler().
238 
239    Concepts: error^setting condition
240 
241 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
242 M*/
243 #define SETERRQ4(comm,ierr,s,a1,a2,a3,a4) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4)
244 
245 /*MC
246    SETERRQ5 - Macro that is called when an error has been detected,
247 
248    Synopsis:
249    #include <petscsys.h>
250    PetscErrorCode SETERRQ5(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
251 
252    Collective on MPI_COmm
253 
254    Input Parameters:
255 +  comm - A communicator, so that the error can be collective
256 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
257 .  message - error message in the printf format
258 .  arg1 - argument (for example an integer, string or double)
259 .  arg2 - argument (for example an integer, string or double)
260 .  arg3 - argument (for example an integer, string or double)
261 .  arg4 - argument (for example an integer, string or double)
262 -  arg5 - argument (for example an integer, string or double)
263 
264   Level: beginner
265 
266    Notes:
267     Once the error handler is called the calling function is then returned from with the given error code.
268 
269     There are also versions for 4, 5, 6 and 7 arguments.
270 
271    Experienced users can set the error handler with PetscPushErrorHandler().
272 
273    Concepts: error^setting condition
274 
275 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
276 M*/
277 #define SETERRQ5(comm,ierr,s,a1,a2,a3,a4,a5) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5)
278 
279 /*MC
280    SETERRQ6 - Macro that is called when an error has been detected,
281 
282    Synopsis:
283    #include <petscsys.h>
284    PetscErrorCode SETERRQ6(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
285 
286    Collective on MPI_Comm
287 
288    Input Parameters:
289 +  comm - A communicator, so that the error can be collective
290 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
291 .  message - error message in the printf format
292 .  arg1 - argument (for example an integer, string or double)
293 .  arg2 - argument (for example an integer, string or double)
294 .  arg3 - argument (for example an integer, string or double)
295 .  arg4 - argument (for example an integer, string or double)
296 .  arg5 - argument (for example an integer, string or double)
297 -  arg6 - argument (for example an integer, string or double)
298 
299   Level: beginner
300 
301    Notes:
302     Once the error handler is called the calling function is then returned from with the given error code.
303 
304     There are also versions for 4, 5, 6 and 7 arguments.
305 
306    Experienced users can set the error handler with PetscPushErrorHandler().
307 
308    Concepts: error^setting condition
309 
310 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
311 M*/
312 #define SETERRQ6(comm,ierr,s,a1,a2,a3,a4,a5,a6) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6)
313 
314 /*MC
315    SETERRQ7 - Macro that is called when an error has been detected,
316 
317    Synopsis:
318    #include <petscsys.h>
319    PetscErrorCode SETERRQ7(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
320 
321    Collective on MPI_Comm
322 
323    Input Parameters:
324 +  comm - A communicator, so that the error can be collective
325 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
326 .  message - error message in the printf format
327 .  arg1 - argument (for example an integer, string or double)
328 .  arg2 - argument (for example an integer, string or double)
329 .  arg3 - argument (for example an integer, string or double)
330 .  arg4 - argument (for example an integer, string or double)
331 .  arg5 - argument (for example an integer, string or double)
332 .  arg6 - argument (for example an integer, string or double)
333 -  arg7 - argument (for example an integer, string or double)
334 
335   Level: beginner
336 
337    Notes:
338     Once the error handler is called the calling function is then returned from with the given error code.
339 
340     There are also versions for 4, 5, 6 and 7 arguments.
341 
342    Experienced users can set the error handler with PetscPushErrorHandler().
343 
344    Concepts: error^setting condition
345 
346 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
347 M*/
348 #define SETERRQ7(comm,ierr,s,a1,a2,a3,a4,a5,a6,a7) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6,a7)
349 
350 /*MC
351    SETERRQ8 - Macro that is called when an error has been detected,
352 
353    Synopsis:
354    #include <petscsys.h>
355    PetscErrorCode SETERRQ8(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
356 
357    Collective on MPI_Comm
358 
359    Input Parameters:
360 +  comm - A communicator, so that the error can be collective
361 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
362 .  message - error message in the printf format
363 .  arg1 - argument (for example an integer, string or double)
364 .  arg2 - argument (for example an integer, string or double)
365 .  arg3 - argument (for example an integer, string or double)
366 .  arg4 - argument (for example an integer, string or double)
367 .  arg5 - argument (for example an integer, string or double)
368 .  arg6 - argument (for example an integer, string or double)
369 .  arg7 - argument (for example an integer, string or double)
370 -  arg8 - argument (for example an integer, string or double)
371 
372   Level: beginner
373 
374    Notes:
375     Once the error handler is called the calling function is then returned from with the given error code.
376 
377     There are also versions for 4, 5, 6 and 7 arguments.
378 
379    Experienced users can set the error handler with PetscPushErrorHandler().
380 
381    Concepts: error^setting condition
382 
383 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
384 M*/
385 #define SETERRQ8(comm,ierr,s,a1,a2,a3,a4,a5,a6,a7,a8) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6,a7,a8)
386 
387 /*MC
388    SETERRABORT - Macro that can be called when an error has been detected,
389 
390    Synopsis:
391    #include <petscsys.h>
392    PetscErrorCode SETERRABORT(MPI_Comm comm,PetscErrorCode ierr,char *message)
393 
394    Collective on MPI_Comm
395 
396    Input Parameters:
397 +  comm - A communicator, so that the error can be collective
398 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
399 -  message - error message in the printf format
400 
401   Level: beginner
402 
403    Notes:
404     This function just calls MPI_Abort().
405 
406    Concepts: error^setting condition
407 
408 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
409 M*/
410 #define SETERRABORT(comm,ierr,s) do {PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s);MPI_Abort(comm,ierr);} while (0)
411 
412 /*MC
413    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
414 
415    Synopsis:
416    #include <petscsys.h>
417    PetscErrorCode CHKERRQ(PetscErrorCode ierr)
418 
419    Not Collective
420 
421    Input Parameters:
422 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
423 
424   Level: beginner
425 
426    Notes:
427     Once the error handler is called the calling function is then returned from with the given error code.
428 
429     Experienced users can set the error handler with PetscPushErrorHandler().
430 
431     CHKERRQ(ierr) is fundamentally a macro replacement for
432          if (ierr) return(PetscError(...,ierr,...));
433 
434     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
435     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
436     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
437     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
438          if (ierr) {PetscError(....); return(YourReturnType);}
439     where you may pass back a NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
440     MPI_Abort() returned immediately.
441 
442    Concepts: error^setting condition
443 
444 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
445 M*/
446 #define CHKERRQ(ierr)          do {if (PetscUnlikely(ierr)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_REPEAT," ");} while (0)
447 #define CHKERRV(ierr)          do {if (PetscUnlikely(ierr)) {ierr = PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_REPEAT," ");return;}} while(0)
448 #define CHKERRABORT(comm,ierr) do {if (PetscUnlikely(ierr)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_REPEAT," ");MPI_Abort(comm,ierr);}} while (0)
449 #define CHKERRCONTINUE(ierr)   do {if (PetscUnlikely(ierr)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_REPEAT," ");}} while (0)
450 
451 
452 /*MC
453    CHKERRMPI - Checks error code, if non-zero it calls the error handler and then returns
454 
455    Synopsis:
456    #include <petscsys.h>
457    PetscErrorCode CHKERRMPI(PetscErrorCode ierr)
458 
459    Not Collective
460 
461    Input Parameters:
462 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
463 
464   Level: developer
465 
466    Notes:
467     This macro is FOR USE IN MPI CALLBACK FUNCTIONS ONLY, such as those passed to MPI_Comm_create_keyval(). It always returns the error code PETSC_MPI_ERROR_CODE
468     which is registered with MPI_Add_error_code() when PETSc is initialized.
469 
470    Concepts: error^setting condition
471 
472 .seealso: CHKERRQ(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
473 M*/
474 #define CHKERRMPI(ierr)        do {if (PetscUnlikely(ierr)) return (PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_REPEAT," "),PETSC_MPI_ERROR_CODE);} while (0)
475 
476 #ifdef PETSC_CLANGUAGE_CXX
477 
478 /*MC
479    CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
480 
481    Synopsis:
482    #include <petscsys.h>
483    void CHKERRXX(PetscErrorCode ierr)
484 
485    Not Collective
486 
487    Input Parameters:
488 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
489 
490   Level: beginner
491 
492    Notes:
493     Once the error handler throws a ??? exception.
494 
495     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
496     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.
497 
498    Concepts: error^setting condition
499 
500 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKERRQ(), CHKMEMQ
501 M*/
502 #define CHKERRXX(ierr)  do {if (PetscUnlikely(ierr)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_IN_CXX,0);}} while(0)
503 
504 #endif
505 
506 #define CHKERRCUDA(err)   do {if (PetscUnlikely(err)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUDA error %d",err);} while(0)
507 #define CHKERRCUBLAS(err) do {if (PetscUnlikely(err)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUBLAS error %d",err);} while(0)
508 
509 /*MC
510    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
511 
512    Synopsis:
513    #include <petscsys.h>
514    CHKMEMQ;
515 
516    Not Collective
517 
518   Level: beginner
519 
520    Notes:
521     We highly recommend using valgrind http://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind for finding memory problems. This is useful
522     on systems that do not have valgrind, but much much less useful.
523 
524     Must run with the option -malloc_debug to enable this option
525 
526     Once the error handler is called the calling function is then returned from with the given error code.
527 
528     By defaults prints location where memory that is corrupted was allocated.
529 
530     Use CHKMEMA for functions that return void
531 
532    Concepts: memory corruption
533 
534 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
535           PetscMallocValidate()
536 M*/
537 #define CHKMEMQ do {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__);CHKERRQ(_7_ierr);} while(0)
538 
539 #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__)
540 
541 /*E
542   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
543 
544   Level: advanced
545 
546   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
547 
548   Developer Notes:
549     This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandler()
550 
551 .seealso: PetscError(), SETERRXX()
552 E*/
553 typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
554 
555 #if defined(__clang_analyzer__)
556 __attribute__((analyzer_noreturn))
557 #endif
558 PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...);
559 
560 PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
561 PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
562 PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
563 PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
564 PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
565 PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
566 PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
567 PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
568 PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
569 PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
570 PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
571 PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int,void*);
572 PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
573 PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
574 PETSC_EXTERN PetscErrorCode PetscCheckPointerSetIntensity(PetscInt);
575 
576 /*MC
577     PetscErrorPrintf - Prints error messages.
578 
579    Synopsis:
580     #include <petscsys.h>
581      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
582 
583     Not Collective
584 
585     Input Parameters:
586 .   format - the usual printf() format string
587 
588    Options Database Keys:
589 +    -error_output_stdout - cause error messages to be printed to stdout instead of the  (default) stderr
590 -    -error_output_none - to turn off all printing of error messages (does not change the way the error is handled.)
591 
592    Notes:
593     Use
594 $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
595 $                        error is handled.) and
596 $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on or you can use your own function
597 
598           Use
599      PETSC_STDERR = FILE* obtained from a file open etc. to have stderr printed to the file.
600      PETSC_STDOUT = FILE* obtained from a file open etc. to have stdout printed to the file.
601 
602           Use
603       PetscPushErrorHandler() to provide your own error handler that determines what kind of messages to print
604 
605    Level: developer
606 
607     Fortran Note:
608     This routine is not supported in Fortran.
609 
610     Concepts: error messages^printing
611     Concepts: printing^error messages
612 
613 .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscHelpPrintf(), PetscPrintf(), PetscPushErrorHandler(), PetscVFPrintf(), PetscHelpPrintf()
614 M*/
615 PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[],...);
616 
617 typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
618 PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
619 PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
620 PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
621 
622 /*
623       Allows the code to build a stack frame as it runs
624 */
625 
626 #define PETSCSTACKSIZE 64
627 
628 typedef struct  {
629   const char      *function[PETSCSTACKSIZE];
630   const char      *file[PETSCSTACKSIZE];
631         int       line[PETSCSTACKSIZE];
632         PetscBool petscroutine[PETSCSTACKSIZE];
633         int       currentsize;
634         int       hotdepth;
635 } PetscStack;
636 
637 PETSC_EXTERN PetscStack *petscstack;
638 
639 PetscErrorCode  PetscStackCopy(PetscStack*,PetscStack*);
640 PetscErrorCode  PetscStackPrint(PetscStack *,FILE*);
641 #if defined(PETSC_SERIALIZE_FUNCTIONS)
642 #include <petsc/private/petscfptimpl.h>
643 /*
644    Registers the current function into the global function pointer to function name table
645 
646    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
647 */
648 #define PetscRegister__FUNCT__() do { \
649   static PetscBool __chked = PETSC_FALSE; \
650   if (!__chked) {\
651   void *ptr; PetscDLSym(NULL,PETSC_FUNCTION_NAME,&ptr);\
652   __chked = PETSC_TRUE;\
653   }} while (0)
654 #else
655 #define PetscRegister__FUNCT__()
656 #endif
657 
658 #if defined(PETSC_USE_DEBUG)
659 PETSC_STATIC_INLINE PetscBool PetscStackActive(void)
660 {
661   return(petscstack ? PETSC_TRUE : PETSC_FALSE);
662 }
663 
664 /* Stack handling is based on the following two "NoCheck" macros.  These should only be called directly by other error
665  * handling macros.  We record the line of the call, which may or may not be the location of the definition.  But is at
666  * least more useful than "unknown" because it can distinguish multiple calls from the same function.
667  */
668 
669 #define PetscStackPushNoCheck(funct,petsc_routine,hot)                        \
670   do {                                                                        \
671     PetscStackSAWsTakeAccess();                                                \
672     if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {         \
673       petscstack->function[petscstack->currentsize]  = funct;               \
674       petscstack->file[petscstack->currentsize]      = __FILE__;            \
675       petscstack->line[petscstack->currentsize]      = __LINE__;            \
676       petscstack->petscroutine[petscstack->currentsize] = petsc_routine;    \
677       petscstack->currentsize++;                                             \
678     }                                                                         \
679     if (petscstack) {                                                        \
680       petscstack->hotdepth += (hot || petscstack->hotdepth);                \
681     }                                                                         \
682     PetscStackSAWsGrantAccess();                                               \
683   } while (0)
684 
685 #define PetscStackPopNoCheck                                            \
686   do {                                                                  \
687     PetscStackSAWsTakeAccess();                                          \
688     if (petscstack && petscstack->currentsize > 0) {                  \
689       petscstack->currentsize--;                                       \
690       petscstack->function[petscstack->currentsize]  = 0;             \
691       petscstack->file[petscstack->currentsize]      = 0;             \
692       petscstack->line[petscstack->currentsize]      = 0;             \
693       petscstack->petscroutine[petscstack->currentsize] = PETSC_FALSE;\
694     }                                                                   \
695     if (petscstack) {                                                  \
696       petscstack->hotdepth = PetscMax(petscstack->hotdepth-1,0);      \
697     }                                                                   \
698     PetscStackSAWsGrantAccess();                                         \
699   } while (0)
700 
701 /*MC
702    PetscFunctionBegin - First executable line of each PETSc function,  used for error handling. Final
703       line of PETSc functions should be PetscFunctionReturn(0);
704 
705    Synopsis:
706    #include <petscsys.h>
707    void PetscFunctionBegin;
708 
709    Not Collective
710 
711    Usage:
712 .vb
713      int something;
714 
715      PetscFunctionBegin;
716 .ve
717 
718    Notes:
719      Use PetscFunctionBeginUser for application codes.
720 
721      Not available in Fortran
722 
723    Level: developer
724 
725 .seealso: PetscFunctionReturn(), PetscFunctionBeginHot(), PetscFunctionBeginUser()
726 
727 .keywords: traceback, error handling
728 M*/
729 #define PetscFunctionBegin do {                                        \
730     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_TRUE,PETSC_FALSE); \
731     PetscRegister__FUNCT__();                                          \
732   } while (0)
733 
734 /*MC
735    PetscFunctionBeginHot - Substitute for PetscFunctionBegin to be used in functions that are called in
736    performance-critical circumstances.  Use of this function allows for lighter profiling by default.
737 
738    Synopsis:
739    #include <petscsys.h>
740    void PetscFunctionBeginHot;
741 
742    Not Collective
743 
744    Usage:
745 .vb
746      int something;
747 
748      PetscFunctionBeginHot;
749 .ve
750 
751    Notes:
752      Not available in Fortran
753 
754    Level: developer
755 
756 .seealso: PetscFunctionBegin, PetscFunctionReturn()
757 
758 .keywords: traceback, error handling
759 M*/
760 #define PetscFunctionBeginHot do {                                     \
761     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_TRUE,PETSC_TRUE);  \
762     PetscRegister__FUNCT__();                                          \
763   } while (0)
764 
765 /*MC
766    PetscFunctionBeginUser - First executable line of user provided PETSc routine
767 
768    Synopsis:
769    #include <petscsys.h>
770    void PetscFunctionBeginUser;
771 
772    Not Collective
773 
774    Usage:
775 .vb
776      int something;
777 
778      PetscFunctionBeginUser;
779 .ve
780 
781    Notes:
782       Final line of PETSc functions should be PetscFunctionReturn(0) except for main().
783 
784       Not available in Fortran
785 
786       This is identical to PetscFunctionBegin except it labels the routine as a user
787       routine instead of as a PETSc library routine.
788 
789    Level: intermediate
790 
791 .seealso: PetscFunctionReturn(), PetscFunctionBegin, PetscFunctionBeginHot
792 
793 .keywords: traceback, error handling
794 M*/
795 #define PetscFunctionBeginUser                                          \
796   do {                                                                  \
797     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_FALSE,PETSC_FALSE); \
798     PetscRegister__FUNCT__();                                           \
799   } while (0)
800 
801 
802 #define PetscStackPush(n) \
803   do {                                                                  \
804     PetscStackPushNoCheck(n,PETSC_FALSE,PETSC_FALSE);                   \
805     CHKMEMQ;                                                            \
806   } while (0)
807 
808 #define PetscStackPop                           \
809     do {                                        \
810       CHKMEMQ;                                  \
811       PetscStackPopNoCheck;                     \
812     } while (0)
813 
814 /*MC
815    PetscFunctionReturn - Last executable line of each PETSc function
816         used for error handling. Replaces return()
817 
818    Synopsis:
819    #include <petscsys.h>
820    void PetscFunctionReturn(0);
821 
822    Not Collective
823 
824    Usage:
825 .vb
826     ....
827      PetscFunctionReturn(0);
828    }
829 .ve
830 
831    Notes:
832      Not available in Fortran
833 
834    Level: developer
835 
836 .seealso: PetscFunctionBegin()
837 
838 .keywords: traceback, error handling
839 M*/
840 #define PetscFunctionReturn(a) \
841   do {                                                                \
842     PetscStackPopNoCheck;                                             \
843     return(a);} while (0)
844 
845 #define PetscFunctionReturnVoid() \
846   do {                                                                \
847     PetscStackPopNoCheck;                                             \
848     return;} while (0)
849 
850 #else
851 
852 PETSC_STATIC_INLINE PetscBool PetscStackActive(void) {return PETSC_FALSE;}
853 #define PetscStackPushNoCheck(funct,petsc_routine,hot) do {} while (0)
854 #define PetscStackPopNoCheck                           do {} while (0)
855 #define PetscFunctionBegin
856 #define PetscFunctionBeginUser
857 #define PetscFunctionBeginHot
858 #define PetscFunctionReturn(a)    return(a)
859 #define PetscFunctionReturnVoid() return
860 #define PetscStackPop             CHKMEMQ
861 #define PetscStackPush(f)         CHKMEMQ
862 
863 #endif
864 
865 /*
866     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
867 
868    Input Parameters:
869 +   name - string that gives the name of the function being called
870 -   routine - actual call to the routine, including ierr = and CHKERRQ(ierr);
871 
872    Note: Often one should use PetscStackCallStandard() instead. This routine is intended for external library routines that DO NOT return error codes
873 
874    Developer Note: this is so that when a user or external library routine results in a crash or corrupts memory, they get blamed instead of PETSc.
875 
876 
877 
878 */
879 #define PetscStackCall(name,routine) do { PetscStackPush(name);routine;PetscStackPop; } while(0)
880 
881 /*
882     PetscStackCallStandard - Calls an external library routine after pushing the name of the routine on the stack.
883 
884    Input Parameters:
885 +   func-  name of the routine
886 -   args - arguments to the routine surrounded by ()
887 
888    Notes:
889     This is intended for external package routines that return error codes. Use PetscStackCall() for those that do not.
890 
891    Developer Note: this is so that when an external packge routine results in a crash or corrupts memory, they get blamed instead of PETSc.
892 
893 */
894 #define PetscStackCallStandard(func,args) do {                        \
895     PetscStackPush(#func);ierr = func args;PetscStackPop; if (ierr) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s(): error code %d",#func,(int)ierr); \
896   } while (0)
897 
898 PETSC_EXTERN PetscErrorCode PetscStackCreate(void);
899 PETSC_EXTERN PetscErrorCode PetscStackView(FILE*);
900 PETSC_EXTERN PetscErrorCode PetscStackDestroy(void);
901 
902 #endif
903