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