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