xref: /petsc/include/petscerror.h (revision 8f3f2bf165a4f0437decc2ac63b7a7aad35aeb6e)
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     In Fortran MPI_Abort() is always called
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(comm,ierr,s) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s)
98 #define SETERRMPI(comm,ierr,s) return (PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s),PETSC_MPI_ERROR_CODE)
99 
100 /*MC
101    SETERRQ1 - Macro that is called when an error has been detected,
102 
103    Synopsis:
104    #include <petscsys.h>
105    PetscErrorCode SETERRQ1(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg)
106 
107    Collective on MPI_Comm
108 
109    Input Parameters:
110 +  comm - A communicator, so that the error can be collective
111 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
112 .  message - error message in the printf format
113 -  arg - argument (for example an integer, string or double)
114 
115   Level: beginner
116 
117    Notes:
118     Once the error handler is called the calling function is then returned from with the given error code.
119 
120    Experienced users can set the error handler with PetscPushErrorHandler().
121 
122    Concepts: error^setting condition
123 
124 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
125 M*/
126 #define SETERRQ1(comm,ierr,s,a1) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1)
127 
128 /*MC
129    SETERRQ2 - Macro that is called when an error has been detected,
130 
131    Synopsis:
132    #include <petscsys.h>
133    PetscErrorCode SETERRQ2(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2)
134 
135    Collective on MPI_Comm
136 
137    Input Parameters:
138 +  comm - A communicator, so that the error can be collective
139 .  ierr - 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(), SETERRQ3()
154 M*/
155 #define SETERRQ2(comm,ierr,s,a1,a2) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2)
156 
157 /*MC
158    SETERRQ3 - Macro that is called when an error has been detected,
159 
160    Synopsis:
161    #include <petscsys.h>
162    PetscErrorCode SETERRQ3(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
163 
164    Collective on MPI_Comm
165 
166    Input Parameters:
167 +  comm - A communicator, so that the error can be collective
168 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
169 .  message - error message in the printf format
170 .  arg1 - argument (for example an integer, string or double)
171 .  arg2 - argument (for example an integer, string or double)
172 -  arg3 - argument (for example an integer, string or double)
173 
174   Level: beginner
175 
176    Notes:
177     Once the error handler is called the calling function is then returned from with the given error code.
178 
179     There are also versions for 4, 5, 6 and 7 arguments.
180 
181    Experienced users can set the error handler with PetscPushErrorHandler().
182 
183    Concepts: error^setting condition
184 
185 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
186 M*/
187 #define SETERRQ3(comm,ierr,s,a1,a2,a3) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3)
188 
189 /*MC
190    SETERRQ4 - Macro that is called when an error has been detected,
191 
192    Synopsis:
193    #include <petscsys.h>
194    PetscErrorCode SETERRQ4(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
195 
196    Collective on MPI_Comm
197 
198    Input Parameters:
199 +  comm - A communicator, so that the error can be collective
200 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
201 .  message - error message in the printf format
202 .  arg1 - argument (for example an integer, string or double)
203 .  arg2 - argument (for example an integer, string or double)
204 .  arg3 - argument (for example an integer, string or double)
205 -  arg4 - argument (for example an integer, string or double)
206 
207   Level: beginner
208 
209    Notes:
210     Once the error handler is called the calling function is then returned from with the given error code.
211 
212     There are also versions for 4, 5, 6 and 7 arguments.
213 
214    Experienced users can set the error handler with PetscPushErrorHandler().
215 
216    Concepts: error^setting condition
217 
218 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
219 M*/
220 #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)
221 
222 /*MC
223    SETERRQ5 - Macro that is called when an error has been detected,
224 
225    Synopsis:
226    #include <petscsys.h>
227    PetscErrorCode SETERRQ5(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
228 
229    Collective on MPI_COmm
230 
231    Input Parameters:
232 +  comm - A communicator, so that the error can be collective
233 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
234 .  message - error message in the printf format
235 .  arg1 - argument (for example an integer, string or double)
236 .  arg2 - argument (for example an integer, string or double)
237 .  arg3 - argument (for example an integer, string or double)
238 .  arg4 - argument (for example an integer, string or double)
239 -  arg5 - argument (for example an integer, string or double)
240 
241   Level: beginner
242 
243    Notes:
244     Once the error handler is called the calling function is then returned from with the given error code.
245 
246     There are also versions for 4, 5, 6 and 7 arguments.
247 
248    Experienced users can set the error handler with PetscPushErrorHandler().
249 
250    Concepts: error^setting condition
251 
252 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
253 M*/
254 #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)
255 
256 /*MC
257    SETERRQ6 - Macro that is called when an error has been detected,
258 
259    Synopsis:
260    #include <petscsys.h>
261    PetscErrorCode SETERRQ6(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
262 
263    Collective on MPI_Comm
264 
265    Input Parameters:
266 +  comm - A communicator, so that the error can be collective
267 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
268 .  message - error message in the printf format
269 .  arg1 - argument (for example an integer, string or double)
270 .  arg2 - argument (for example an integer, string or double)
271 .  arg3 - argument (for example an integer, string or double)
272 .  arg4 - argument (for example an integer, string or double)
273 .  arg5 - argument (for example an integer, string or double)
274 -  arg6 - argument (for example an integer, string or double)
275 
276   Level: beginner
277 
278    Notes:
279     Once the error handler is called the calling function is then returned from with the given error code.
280 
281     There are also versions for 4, 5, 6 and 7 arguments.
282 
283    Experienced users can set the error handler with PetscPushErrorHandler().
284 
285    Concepts: error^setting condition
286 
287 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
288 M*/
289 #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)
290 
291 /*MC
292    SETERRQ7 - Macro that is called when an error has been detected,
293 
294    Synopsis:
295    #include <petscsys.h>
296    PetscErrorCode SETERRQ7(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
297 
298    Collective on MPI_Comm
299 
300    Input Parameters:
301 +  comm - A communicator, so that the error can be collective
302 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
303 .  message - error message in the printf format
304 .  arg1 - argument (for example an integer, string or double)
305 .  arg2 - argument (for example an integer, string or double)
306 .  arg3 - argument (for example an integer, string or double)
307 .  arg4 - argument (for example an integer, string or double)
308 .  arg5 - argument (for example an integer, string or double)
309 .  arg6 - argument (for example an integer, string or double)
310 -  arg7 - argument (for example an integer, string or double)
311 
312   Level: beginner
313 
314    Notes:
315     Once the error handler is called the calling function is then returned from with the given error code.
316 
317     There are also versions for 4, 5, 6 and 7 arguments.
318 
319    Experienced users can set the error handler with PetscPushErrorHandler().
320 
321    Concepts: error^setting condition
322 
323 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
324 M*/
325 #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)
326 
327 /*MC
328    SETERRQ8 - Macro that is called when an error has been detected,
329 
330    Synopsis:
331    #include <petscsys.h>
332    PetscErrorCode SETERRQ8(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
333 
334    Collective on MPI_Comm
335 
336    Input Parameters:
337 +  comm - A communicator, so that the error can be collective
338 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
339 .  message - error message in the printf format
340 .  arg1 - argument (for example an integer, string or double)
341 .  arg2 - argument (for example an integer, string or double)
342 .  arg3 - argument (for example an integer, string or double)
343 .  arg4 - argument (for example an integer, string or double)
344 .  arg5 - argument (for example an integer, string or double)
345 .  arg6 - argument (for example an integer, string or double)
346 .  arg7 - argument (for example an integer, string or double)
347 -  arg8 - argument (for example an integer, string or double)
348 
349   Level: beginner
350 
351    Notes:
352     Once the error handler is called the calling function is then returned from with the given error code.
353 
354     There are also versions for 4, 5, 6 and 7 arguments.
355 
356    Experienced users can set the error handler with PetscPushErrorHandler().
357 
358    Concepts: error^setting condition
359 
360 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
361 M*/
362 #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)
363 
364 /*MC
365    SETERRABORT - Macro that can be called when an error has been detected,
366 
367    Synopsis:
368    #include <petscsys.h>
369    PetscErrorCode SETERRABORT(MPI_Comm comm,PetscErrorCode ierr,char *message)
370 
371    Collective on MPI_Comm
372 
373    Input Parameters:
374 +  comm - A communicator, so that the error can be collective
375 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
376 -  message - error message in the printf format
377 
378   Level: beginner
379 
380    Notes:
381     This function just calls MPI_Abort().
382 
383    Concepts: error^setting condition
384 
385 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
386 M*/
387 #define SETERRABORT(comm,ierr,s) do {PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s);MPI_Abort(comm,ierr);} while (0)
388 
389 /*MC
390    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
391 
392    Synopsis:
393    #include <petscsys.h>
394    PetscErrorCode CHKERRQ(PetscErrorCode ierr)
395 
396    Not Collective
397 
398    Input Parameters:
399 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
400 
401   Level: beginner
402 
403    Notes:
404     Once the error handler is called the calling function is then returned from with the given error code.
405 
406     Experienced users can set the error handler with PetscPushErrorHandler().
407 
408     CHKERRQ(ierr) is fundamentally a macro replacement for
409          if (ierr) return(PetscError(...,ierr,...));
410 
411     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
412     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
413     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
414     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
415          if (ierr) {PetscError(....); return(YourReturnType);}
416     where you may pass back a NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
417     MPI_Abort() returned immediately.
418 
419     In Fortran MPI_Abort() is always called
420 
421    Concepts: error^setting condition
422 
423 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
424 M*/
425 #define CHKERRQ(ierr)          do {if (PetscUnlikely(ierr)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_REPEAT," ");} while (0)
426 #define CHKERRV(ierr)          do {if (PetscUnlikely(ierr)) {ierr = PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_REPEAT," ");return;}} while(0)
427 #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)
428 #define CHKERRCONTINUE(ierr)   do {if (PetscUnlikely(ierr)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_REPEAT," ");}} while (0)
429 #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)
430 
431 #ifdef PETSC_CLANGUAGE_CXX
432 
433 /*MC
434    CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
435 
436    Synopsis:
437    #include <petscsys.h>
438    void CHKERRXX(PetscErrorCode ierr)
439 
440    Not Collective
441 
442    Input Parameters:
443 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
444 
445   Level: beginner
446 
447    Notes:
448     Once the error handler throws a ??? exception.
449 
450     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
451     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.
452 
453    Concepts: error^setting condition
454 
455 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKERRQ(), CHKMEMQ
456 M*/
457 #define CHKERRXX(ierr)  do {if (PetscUnlikely(ierr)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_IN_CXX,0);}} while(0)
458 
459 #endif
460 
461 #define CHKERRCUDA(err)   do {if (PetscUnlikely(err)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUDA error %d",err);} while(0)
462 #define CHKERRCUBLAS(err) do {if (PetscUnlikely(err)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"CUBLAS error %d",err);} while(0)
463 
464 /*MC
465    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
466 
467    Synopsis:
468    #include <petscsys.h>
469    CHKMEMQ;
470 
471    Not Collective
472 
473   Level: beginner
474 
475    Notes:
476     We highly recommend using valgrind http://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind for finding memory problems. This is useful
477     on systems that do not have valgrind, but much much less useful.
478 
479     Must run with the option -malloc_debug to enable this option
480 
481     Once the error handler is called the calling function is then returned from with the given error code.
482 
483     By defaults prints location where memory that is corrupted was allocated.
484 
485     Use CHKMEMA for functions that return void
486 
487    Concepts: memory corruption
488 
489 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
490           PetscMallocValidate()
491 M*/
492 #define CHKMEMQ do {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__);CHKERRQ(_7_ierr);} while(0)
493 
494 #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__)
495 
496 /*E
497   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
498 
499   Level: advanced
500 
501   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
502 
503   Developer Notes:
504     This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandler()
505 
506 .seealso: PetscError(), SETERRXX()
507 E*/
508 typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
509 
510 #if defined(__clang_analyzer__)
511 __attribute__((analyzer_noreturn))
512 #endif
513 PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...);
514 
515 PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
516 PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
517 PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
518 PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
519 PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
520 PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
521 PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
522 PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
523 PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
524 PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
525 PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
526 PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int,void*);
527 PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
528 PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
529 PETSC_EXTERN PetscErrorCode PetscCheckPointerSetIntensity(PetscInt);
530 
531 /*MC
532     PetscErrorPrintf - Prints error messages.
533 
534    Synopsis:
535     #include <petscsys.h>
536      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
537 
538     Not Collective
539 
540     Input Parameters:
541 .   format - the usual printf() format string
542 
543    Options Database Keys:
544 +    -error_output_stdout - cause error messages to be printed to stdout instead of the  (default) stderr
545 -    -error_output_none - to turn off all printing of error messages (does not change the way the error is handled.)
546 
547    Notes:
548     Use
549 $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
550 $                        error is handled.) and
551 $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on or you can use your own function
552 
553           Use
554      PETSC_STDERR = FILE* obtained from a file open etc. to have stderr printed to the file.
555      PETSC_STDOUT = FILE* obtained from a file open etc. to have stdout printed to the file.
556 
557           Use
558       PetscPushErrorHandler() to provide your own error handler that determines what kind of messages to print
559 
560    Level: developer
561 
562     Fortran Note:
563     This routine is not supported in Fortran.
564 
565     Concepts: error messages^printing
566     Concepts: printing^error messages
567 
568 .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscHelpPrintf(), PetscPrintf(), PetscErrorHandlerPush(), PetscVFPrintf(), PetscHelpPrintf()
569 M*/
570 PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[],...);
571 
572 typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
573 PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
574 PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
575 PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
576 
577 /*
578       Allows the code to build a stack frame as it runs
579 */
580 
581 #define PETSCSTACKSIZE 64
582 
583 typedef struct  {
584   const char      *function[PETSCSTACKSIZE];
585   const char      *file[PETSCSTACKSIZE];
586         int       line[PETSCSTACKSIZE];
587         PetscBool petscroutine[PETSCSTACKSIZE];
588         int       currentsize;
589         int       hotdepth;
590 } PetscStack;
591 
592 PETSC_EXTERN PetscStack *petscstack;
593 
594 PetscErrorCode  PetscStackCopy(PetscStack*,PetscStack*);
595 PetscErrorCode  PetscStackPrint(PetscStack *,FILE*);
596 #if defined(PETSC_SERIALIZE_FUNCTIONS)
597 #include <petsc/private/petscfptimpl.h>
598 /*
599    Registers the current function into the global function pointer to function name table
600 
601    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
602 */
603 #define PetscRegister__FUNCT__() do { \
604   static PetscBool __chked = PETSC_FALSE; \
605   if (!__chked) {\
606   void *ptr; PetscDLSym(NULL,PETSC_FUNCTION_NAME,&ptr);\
607   __chked = PETSC_TRUE;\
608   }} while (0)
609 #else
610 #define PetscRegister__FUNCT__()
611 #endif
612 
613 #if defined(PETSC_USE_DEBUG)
614 PETSC_STATIC_INLINE PetscBool PetscStackActive(void)
615 {
616   return(petscstack ? PETSC_TRUE : PETSC_FALSE);
617 }
618 
619 /* Stack handling is based on the following two "NoCheck" macros.  These should only be called directly by other error
620  * handling macros.  We record the line of the call, which may or may not be the location of the definition.  But is at
621  * least more useful than "unknown" because it can distinguish multiple calls from the same function.
622  */
623 
624 #define PetscStackPushNoCheck(funct,petsc_routine,hot)                        \
625   do {                                                                        \
626     PetscStackSAWsTakeAccess();                                                \
627     if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {         \
628       petscstack->function[petscstack->currentsize]  = funct;               \
629       petscstack->file[petscstack->currentsize]      = __FILE__;            \
630       petscstack->line[petscstack->currentsize]      = __LINE__;            \
631       petscstack->petscroutine[petscstack->currentsize] = petsc_routine;    \
632       petscstack->currentsize++;                                             \
633     }                                                                         \
634     if (petscstack) {                                                        \
635       petscstack->hotdepth += (hot || petscstack->hotdepth);                \
636     }                                                                         \
637     PetscStackSAWsGrantAccess();                                               \
638   } while (0)
639 
640 #define PetscStackPopNoCheck                                            \
641   do {                                                                  \
642     PetscStackSAWsTakeAccess();                                          \
643     if (petscstack && petscstack->currentsize > 0) {                  \
644       petscstack->currentsize--;                                       \
645       petscstack->function[petscstack->currentsize]  = 0;             \
646       petscstack->file[petscstack->currentsize]      = 0;             \
647       petscstack->line[petscstack->currentsize]      = 0;             \
648       petscstack->petscroutine[petscstack->currentsize] = PETSC_FALSE;\
649     }                                                                   \
650     if (petscstack) {                                                  \
651       petscstack->hotdepth = PetscMax(petscstack->hotdepth-1,0);      \
652     }                                                                   \
653     PetscStackSAWsGrantAccess();                                         \
654   } while (0)
655 
656 /*MC
657    PetscFunctionBegin - First executable line of each PETSc function,  used for error handling. Final
658       line of PETSc functions should be PetscFunctionReturn(0);
659 
660    Synopsis:
661    #include <petscsys.h>
662    void PetscFunctionBegin;
663 
664    Not Collective
665 
666    Usage:
667 .vb
668      int something;
669 
670      PetscFunctionBegin;
671 .ve
672 
673    Notes:
674      Use PetscFunctionBeginUser for application codes.
675 
676      Not available in Fortran
677 
678    Level: developer
679 
680 .seealso: PetscFunctionReturn(), PetscFunctionBeginHot(), PetscFunctionBeginUser()
681 
682 .keywords: traceback, error handling
683 M*/
684 #define PetscFunctionBegin do {                                        \
685     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_TRUE,PETSC_FALSE); \
686     PetscRegister__FUNCT__();                                          \
687   } while (0)
688 
689 /*MC
690    PetscFunctionBeginHot - Substitute for PetscFunctionBegin to be used in functions that are called in
691    performance-critical circumstances.  Use of this function allows for lighter profiling by default.
692 
693    Synopsis:
694    #include <petscsys.h>
695    void PetscFunctionBeginHot;
696 
697    Not Collective
698 
699    Usage:
700 .vb
701      int something;
702 
703      PetscFunctionBeginHot;
704 .ve
705 
706    Notes:
707      Not available in Fortran
708 
709    Level: developer
710 
711 .seealso: PetscFunctionBegin, PetscFunctionReturn()
712 
713 .keywords: traceback, error handling
714 M*/
715 #define PetscFunctionBeginHot do {                                     \
716     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_TRUE,PETSC_TRUE);  \
717     PetscRegister__FUNCT__();                                          \
718   } while (0)
719 
720 /*MC
721    PetscFunctionBeginUser - First executable line of user provided PETSc routine
722 
723    Synopsis:
724    #include <petscsys.h>
725    void PetscFunctionBeginUser;
726 
727    Not Collective
728 
729    Usage:
730 .vb
731      int something;
732 
733      PetscFunctionBeginUser;
734 .ve
735 
736    Notes:
737       Final line of PETSc functions should be PetscFunctionReturn(0) except for main().
738 
739       Not available in Fortran
740 
741       This is identical to PetscFunctionBegin except it labels the routine as a user
742       routine instead of as a PETSc library routine.
743 
744    Level: intermediate
745 
746 .seealso: PetscFunctionReturn(), PetscFunctionBegin, PetscFunctionBeginHot
747 
748 .keywords: traceback, error handling
749 M*/
750 #define PetscFunctionBeginUser                                          \
751   do {                                                                  \
752     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_FALSE,PETSC_FALSE); \
753     PetscRegister__FUNCT__();                                           \
754   } while (0)
755 
756 
757 #define PetscStackPush(n) \
758   do {                                                                  \
759     PetscStackPushNoCheck(n,PETSC_FALSE,PETSC_FALSE);                   \
760     CHKMEMQ;                                                            \
761   } while (0)
762 
763 #define PetscStackPop                           \
764     do {                                        \
765       CHKMEMQ;                                  \
766       PetscStackPopNoCheck;                     \
767     } while (0)
768 
769 /*MC
770    PetscFunctionReturn - Last executable line of each PETSc function
771         used for error handling. Replaces return()
772 
773    Synopsis:
774    #include <petscsys.h>
775    void PetscFunctionReturn(0);
776 
777    Not Collective
778 
779    Usage:
780 .vb
781     ....
782      PetscFunctionReturn(0);
783    }
784 .ve
785 
786    Notes:
787      Not available in Fortran
788 
789    Level: developer
790 
791 .seealso: PetscFunctionBegin()
792 
793 .keywords: traceback, error handling
794 M*/
795 #define PetscFunctionReturn(a) \
796   do {                                                                \
797     PetscStackPopNoCheck;                                             \
798     return(a);} while (0)
799 
800 #define PetscFunctionReturnVoid() \
801   do {                                                                \
802     PetscStackPopNoCheck;                                             \
803     return;} while (0)
804 
805 #else
806 
807 PETSC_STATIC_INLINE PetscBool PetscStackActive(void) {return PETSC_FALSE;}
808 #define PetscStackPushNoCheck(funct,petsc_routine,hot) do {} while (0)
809 #define PetscStackPopNoCheck                           do {} while (0)
810 #define PetscFunctionBegin
811 #define PetscFunctionBeginUser
812 #define PetscFunctionBeginHot
813 #define PetscFunctionReturn(a)    return(a)
814 #define PetscFunctionReturnVoid() return
815 #define PetscStackPop             CHKMEMQ
816 #define PetscStackPush(f)         CHKMEMQ
817 
818 #endif
819 
820 /*
821     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
822 
823    Input Parameters:
824 +   name - string that gives the name of the function being called
825 -   routine - actual call to the routine, including ierr = and CHKERRQ(ierr);
826 
827    Note: Often one should use PetscStackCallStandard() instead. This routine is intended for external library routines that DO NOT return error codes
828 
829    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.
830 
831 
832 
833 */
834 #define PetscStackCall(name,routine) do { PetscStackPush(name);routine;PetscStackPop; } while(0)
835 
836 /*
837     PetscStackCallStandard - Calls an external library routine after pushing the name of the routine on the stack.
838 
839    Input Parameters:
840 +   func-  name of the routine
841 -   args - arguments to the routine surrounded by ()
842 
843    Notes:
844     This is intended for external package routines that return error codes. Use PetscStackCall() for those that do not.
845 
846    Developer Note: this is so that when an external packge routine results in a crash or corrupts memory, they get blamed instead of PETSc.
847 
848 */
849 #define PetscStackCallStandard(func,args) do {                        \
850     PetscStackPush(#func);ierr = func args;PetscStackPop; if (ierr) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s(): error code %d",#func,(int)ierr); \
851   } while (0)
852 
853 PETSC_EXTERN PetscErrorCode PetscStackCreate(void);
854 PETSC_EXTERN PetscErrorCode PetscStackView(FILE*);
855 PETSC_EXTERN PetscErrorCode PetscStackDestroy(void);
856 
857 #endif
858