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