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