xref: /petsc/include/petscerror.h (revision db9cea48fc944dc99744aab3343aad054e344d0e)
1 /*
2     Contains all error handling interfaces for PETSc.
3 */
4 #if !defined(PETSCERROR_H)
5 #define PETSCERROR_H
6 
7 #include <petscmacros.h>
8 #include <petscsystypes.h>
9 
10 /* SUBMANSEC = Sys */
11 
12 /*
13      These are the generic error codes. These error codes are used
14      many different places in the PETSc source code. The string versions are
15      at src/sys/error/err.c any changes here must also be made there
16      These are also define in src/sys/f90-mod/petscerror.h any CHANGES here
17      must be also made there.
18 
19 */
20 #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
21 
22 #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
23 #define PETSC_ERR_SUP              56   /* no support for requested operation */
24 #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
25 #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
26 #define PETSC_ERR_SIG              59   /* signal received */
27 #define PETSC_ERR_FP               72   /* floating point exception */
28 #define PETSC_ERR_COR              74   /* corrupted PETSc object */
29 #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
30 #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
31 #define PETSC_ERR_MEMC             78   /* memory corruption */
32 #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
33 #define PETSC_ERR_USER             83   /* user has not provided needed function */
34 #define PETSC_ERR_SYS              88   /* error in system call */
35 #define PETSC_ERR_POINTER          70   /* pointer does not point to valid address */
36 #define PETSC_ERR_MPI_LIB_INCOMP   87   /* MPI library at runtime is not compatible with MPI user compiled with */
37 
38 #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
39 #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
40 #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
41 #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
42 #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
43 #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
44 #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
45 #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
46 #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
47 #define PETSC_ERR_ARG_TYPENOTSET   89   /* the type of the object has not yet been set */
48 #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
49 #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
50 #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
51 
52 #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
53 #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
54 #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
55 #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
56 
57 #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
58 #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
59 
60 #define PETSC_ERR_INT_OVERFLOW     84
61 
62 #define PETSC_ERR_FLOP_COUNT       90
63 #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
64 #define PETSC_ERR_MISSING_FACTOR   92  /* MatGetFactor() failed */
65 #define PETSC_ERR_OPT_OVERWRITE    93  /* attempted to over write options which should not be changed */
66 #define PETSC_ERR_WRONG_MPI_SIZE   94  /* example/application run with number of MPI ranks it does not support */
67 #define PETSC_ERR_USER_INPUT       95  /* missing or incorrect user input */
68 #define PETSC_ERR_GPU_RESOURCE     96  /* unable to load a GPU resource, for example cuBLAS */
69 #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 */
70 #define PETSC_ERR_MPI              98  /* general MPI error */
71 #define PETSC_ERR_MAX_VALUE        99  /* this is always the one more than the largest error code */
72 
73 #define SETERRQ1(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
74 #define SETERRQ2(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
75 #define SETERRQ3(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
76 #define SETERRQ4(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
77 #define SETERRQ5(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
78 #define SETERRQ6(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
79 #define SETERRQ7(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
80 #define SETERRQ8(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
81 #define SETERRQ9(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
82 
83 /*MC
84    SETERRQ - Macro to be called when an error has been detected,
85 
86    Synopsis:
87    #include <petscsys.h>
88    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
89 
90    Collective
91 
92    Input Parameters:
93 +  comm - A communicator, use PETSC_COMM_SELF unless you know all ranks of another communicator will detect the error
94 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
95 -  message - error message
96 
97   Level: beginner
98 
99    Notes:
100     Once the error handler is called the calling function is then returned from with the given error code.
101 
102     Experienced users can set the error handler with PetscPushErrorHandler().
103 
104    Fortran Notes:
105       SETERRQ() may be called from Fortran subroutines but SETERRA() must be called from the
106       Fortran main program.
107 
108 .seealso: `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
109           `PetscError()`, `PetscCall()`, `CHKMEMQ`, `CHKERRA()`, `PetscCallMPI()`
110 M*/
111 #define SETERRQ(comm,ierr,...) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,__VA_ARGS__)
112 
113 /*
114     Returned from PETSc functions that are called from MPI, such as related to attributes
115       Do not confuse PETSC_MPI_ERROR_CODE and PETSC_ERR_MPI, the first is registered with MPI and returned to MPI as
116       an error code, the latter is a regular PETSc error code passed within PETSc code indicating an error was detected in an MPI call.
117 */
118 PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CLASS;
119 PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CODE;
120 
121 /*MC
122    SETERRMPI - Macro to be called when an error has been detected within an MPI callback function
123 
124    Synopsis:
125    #include <petscsys.h>
126    PetscErrorCode SETERRMPI(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
127 
128    Collective
129 
130    Input Parameters:
131 +  comm - A communicator, use PETSC_COMM_SELF unless you know all ranks of another communicator will detect the error
132 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
133 -  message - error message
134 
135   Level: developer
136 
137    Notes:
138     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
139     which is registered with MPI_Add_error_code() when PETSc is initialized.
140 
141 .seealso: `SETERRQ()`, `PetscCall()`, `PetscCallMPI()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
142 M*/
143 #define SETERRMPI(comm,ierr,...) return (PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,__VA_ARGS__),PETSC_MPI_ERROR_CODE)
144 
145 /*MC
146    SETERRA - Fortran-only macro that can be called when an error has been detected from the main program
147 
148    Synopsis:
149    #include <petscsys.h>
150    PetscErrorCode SETERRA(MPI_Comm comm,PetscErrorCode ierr,char *message)
151 
152    Collective
153 
154    Input Parameters:
155 +  comm - A communicator, so that the error can be collective
156 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
157 -  message - error message in the printf format
158 
159   Level: beginner
160 
161    Notes:
162     This should only be used with Fortran. With C/C++, use SETERRQ().
163 
164    Fortran Notes:
165       SETERRQ() may be called from Fortran subroutines but SETERRA() must be called from the
166       Fortran main program.
167 
168 .seealso: `SETERRQ()`, `SETERRABORT()`, `PetscCall()`, `CHKERRA()`, `PetscCallAbort()`
169 M*/
170 
171 /*MC
172    SETERRABORT - Macro that can be called when an error has been detected,
173 
174    Synopsis:
175    #include <petscsys.h>
176    PetscErrorCode SETERRABORT(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
177 
178    Collective
179 
180    Input Parameters:
181 +  comm - A communicator, so that the error can be collective
182 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
183 -  message - error message in the printf format
184 
185   Level: beginner
186 
187    Notes:
188     This function just calls MPI_Abort().
189 
190 .seealso: `SETERRQ()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `PetscCall()`, `CHKMEMQ`
191 M*/
192 #define SETERRABORT(comm,ierr,...) do {                                                        \
193     PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,__VA_ARGS__); \
194     MPI_Abort(comm,ierr);                                                                      \
195   } while (0)
196 
197 /*MC
198   PetscCheck - Check that a particular condition is true
199 
200   Synopsis:
201   #include <petscerror.h>
202   void PetscCheck(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
203 
204   Collective
205 
206   Input Parameters:
207 + cond    - The boolean condition
208 . comm    - The communicator on which the check can be collective on
209 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
210 - message - Error message in printf format
211 
212   Notes:
213   Enabled in both optimized and debug builds.
214 
215   Calls SETERRQ() if the assertion fails, so can only be called from functions returning a
216   PetscErrorCode (or equivalent type after conversion).
217 
218   Level: beginner
219 
220 .seealso: `PetscAssert()`, `SETERRQ()`, `PetscError()`, `PetscCall()`
221 M*/
222 #define PetscCheck(cond,comm,ierr,...) if (PetscUnlikely(!(cond))) SETERRQ(comm,ierr,__VA_ARGS__)
223 
224 /*MC
225   PetscAssert - Assert that a particular condition is true
226 
227   Synopsis:
228   #include <petscerror.h>
229   void PetscAssert(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
230 
231   Collective
232 
233   Input Parameters:
234 + cond    - The boolean condition
235 . comm    - The communicator on which the check can be collective on
236 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
237 - message - Error message in printf format
238 
239   Notes:
240   Enabled only in debug builds. Note that any arguments to this macros are still visible to the
241   compiler optimized builds (so must still contain valid code) but are guaranteed to not be
242   executed.
243 
244   See PetscCheck() for usage and behaviour.
245 
246   Level: beginner
247 
248 .seealso: `PetscCheck()`, `SETERRQ()`, `PetscError()`
249 M*/
250 #define PetscAssert(cond,comm,ierr,...) if (PetscUnlikelyDebug(!(cond))) SETERRQ(comm,ierr,__VA_ARGS__)
251 
252 /*MC
253   PetscCall - Checks error code returned from a PETSc function, if non-zero it calls the error
254   handler and and returns from the current function.
255 
256   Synopsis:
257   #include <petscerror.h>
258   void PetscCall(PetscErrorCode ierr)
259 
260   Not Collective
261 
262   Input Parameter:
263 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
264 
265   Notes:
266   Once the error handler is called the calling function is then returned from with the given
267   error code. Experienced users can set the error handler with PetscPushErrorHandler().
268 
269   PetscCall(ierr) is fundamentally a macro replacement for
270 .vb
271   if (ierr) return PetscError(...,ierr,...);
272 .ve
273 
274   PetscCall() cannot be used in functions returning a datatype not convertable to
275   PetscErrorCode. For example, PetscCall() may not be used in functions returning void, use
276   PetscCallVoid() in this case.
277 
278   Fortran Notes:
279   PetscCall() may be called from Fortran subroutines but CHKERRA() must be called from the
280   Fortran main program.
281 
282   Example Usage:
283 .vb
284   PetscCall(PetscInitiailize(...)); // OK to call even when PETSc is not yet initialized!
285 
286   extern int foo(int);
287 
288   PetscCall(foo(1)); // OK if int is convertable to PetscErrorCode
289 
290   struct my_struct
291   {
292     void *data;
293   } my_complex_type;
294 
295   struct my_struct bar(void)
296   {
297     PetscCall(foo(15)); // ERROR PetscErrorCode not convertable to struct my_struct!
298   }
299 
300   PetscCall(bar()) // ERROR input not convertable to PetscErrorCode
301 .ve
302 
303   Level: beginner
304 
305 .seealso: `SETERRQ()`, `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`,
306           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`
307 M*/
308 #if defined(PETSC_CLANG_STATIC_ANALYZER)
309 void PetscCall(PetscErrorCode);
310 void PetscCallVoid(PetscErrorCode);
311 #else
312 #define PetscCall(...) do {                                                                    \
313     PetscErrorCode ierr_q_ = __VA_ARGS__;                                                      \
314     if (PetscUnlikely(ierr_q_)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr_q_,PETSC_ERROR_REPEAT," "); \
315   } while (0)
316 #define PetscCallVoid(...) do {                                                                \
317     PetscErrorCode ierr_void_ = __VA_ARGS__;                                                   \
318     if (PetscUnlikely(ierr_void_)) {                                                           \
319       (void)PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr_void_,PETSC_ERROR_REPEAT," "); \
320       return;                                                                                  \
321     }                                                                                          \
322   } while (0)
323 #endif
324 
325 /*MC
326   CHKERRQ - Checks error code returned from PETSc function
327 
328   Synopsis:
329   #include <petscsys.h>
330   void CHKERRQ(PetscErrorCode ierr)
331 
332   Not Collective
333 
334   Input Parameters:
335 . ierr - nonzero error code
336 
337   Notes:
338   Deprecated in favor of PetscCall(). This routine behaves identically to it.
339 
340   Level: deprecated
341 
342 .seealso: `PetscCall()`
343 M*/
344 #define CHKERRQ(...) PetscCall(__VA_ARGS__)
345 #define CHKERRV(...) PetscCallVoid(__VA_ARGS__)
346 
347 PETSC_EXTERN void PetscMPIErrorString(PetscMPIInt, char*);
348 
349 /*MC
350   PetscCallMPI - Checks error code returned from MPI calls, if non-zero it calls the error
351   handler and then returns
352 
353   Synopsis:
354   #include <petscerror.h>
355   void PetscCallMPI(PetscErrorCode ierr)
356 
357   Not Collective
358 
359   Input Parameters:
360 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
361 
362   Notes:
363   Always returns the error code PETSC_ERR_MPI; the MPI error code and string are embedded in
364   the string error message. Do not use this to call any other routines (for example PETSc
365   routines), it should only be used for direct MPI calls. Due to limitations of the
366   preprocessor this can unfortunately not easily be enforced, so the user should take care to
367   check this themselves.
368 
369   Example Usage:
370 .vb
371   PetscCallMPI(MPI_Comm_size(...)); // OK, calling MPI function
372 
373   PetscCallMPI(PetscFunction(...)); // ERROR, use PetscCall() instead!
374 .ve
375 
376   Level: beginner
377 
378 .seealso: `SETERRMPI()`, `PetscCall()`, `SETERRQ()`, `SETERRABORT()`, `PetscCallAbort()`,
379           `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
380 M*/
381 #if defined(PETSC_CLANG_STATIC_ANALYZER)
382 void PetscCallMPI(PetscMPIInt);
383 #else
384 #define PetscCallMPI(...) do {                                                                 \
385     PetscMPIInt _7_errorcode = __VA_ARGS__;                                                    \
386     if (PetscUnlikely(_7_errorcode)) {                                                         \
387       char        _7_errorstring[2*MPI_MAX_ERROR_STRING];                                      \
388       PetscMPIErrorString(_7_errorcode,(char*)_7_errorstring); \
389       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MPI,"MPI error %d %s",(int)_7_errorcode,_7_errorstring); \
390     }                                                                                          \
391   } while (0)
392 #endif
393 
394 /*MC
395   CHKERRMPI - Checks error code returned from MPI calls, if non-zero it calls the error
396   handler and then returns
397 
398   Synopsis:
399   #include <petscerror.h>
400   void CHKERRMPI(PetscErrorCode ierr)
401 
402   Not Collective
403 
404   Input Parameter:
405 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
406 
407   Notes:
408   Deprecated in favor of PetscCallMPI(). This routine behaves identically to it.
409 
410   Level: deprecated
411 
412 .seealso: `PetscCallMPI()`
413 M*/
414 #define CHKERRMPI(...) PetscCallMPI(__VA_ARGS__)
415 
416 /*MC
417   PetscCallAbort - Checks error code returned from PETSc function, if non-zero it aborts immediately
418 
419   Synopsis:
420   #include <petscerror.h>
421   void PetscCallAbort(MPI_Comm comm, PetscErrorCode ierr)
422 
423   Collective on comm
424 
425   Input Parameters:
426 + comm - the MPI communicator on which to abort
427 - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
428 
429   Notes:
430   This macro has identical type and usage semantics to PetscCall() with the important caveat
431   that this macro does not return. Instead, if ierr is nonzero it calls the PETSc error handler
432   and then immediately calls MPI_Abort(). It can therefore be used anywhere.
433 
434   As per MPI_Abort semantics the communicator passed must be valid, although there is currently
435   no attempt made at handling any potential errors from MPI_Abort(). Note that while
436   MPI_Abort() is required to terminate only those processes which reside on comm, it is often
437   the case that MPI_Abort() terminates *all* processes.
438 
439   Example Usage:
440 .vb
441   PetscErrorCode boom(void) { return PETSC_ERR_MEM; }
442 
443   void foo(void)
444   {
445     PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
446   }
447 
448   double bar(void)
449   {
450     PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
451   }
452 
453   PetscCallAbort(MPI_COMM_NULL,boom()); // ERROR, communicator should be valid
454 
455   struct baz
456   {
457     baz()
458     {
459       PetscCallAbort(PETSC_COMM_SELF,boom()); // OK
460     }
461 
462     ~baz()
463     {
464       PetscCallAbort(PETSC_COMM_SELF,boom()); // OK (in fact the only way to handle PETSc errors)
465     }
466   };
467 .ve
468 
469   Level: intermediate
470 
471 .seealso: `SETERRABORT()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`,
472           `SETERRQ()`, `CHKMEMQ`, `PetscCallMPI()`
473 M*/
474 #if defined(PETSC_CLANG_STATIC_ANALYZER)
475 void PetscCallAbort(MPI_Comm,PetscErrorCode);
476 void PetscCallContinue(PetscErrorCode);
477 #else
478 #define PetscCallAbort(comm,...) do {                                                          \
479     PetscErrorCode ierr_abort_ = __VA_ARGS__;                                                  \
480     if (PetscUnlikely(ierr_abort_)) {                                                          \
481       PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr_abort_,PETSC_ERROR_REPEAT," "); \
482       MPI_Abort(comm,ierr_abort_);                                                             \
483     }                                                                                          \
484   } while (0)
485 #define PetscCallContinue(...)   do {                                                          \
486     PetscErrorCode ierr_continue_ = __VA_ARGS__;                                               \
487     if (PetscUnlikely(ierr_continue_)) PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr_continue_,PETSC_ERROR_REPEAT," "); \
488   } while (0)
489 #endif
490 
491 /*MC
492   CHKERRABORT - Checks error code returned from PETSc function. If non-zero it aborts immediately.
493 
494   Synopsis:
495   #include <petscerror.h>
496   void CHKERRABORT(MPI_Comm comm, PetscErrorCode ierr)
497 
498   Not Collective
499 
500   Input Parameters:
501 + comm - the MPI communicator
502 - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
503 
504   Notes:
505   Deprecated in favor of PetscCallAbort(). This routine behaves identically to it.
506 
507   Level: deprecated
508 
509 .seealso: `PetscCallAbort()`
510 M*/
511 #define CHKERRABORT(comm,...) PetscCallAbort(comm,__VA_ARGS__)
512 #define CHKERRCONTINUE(...)   PetscCallContinue(__VA_ARGS__)
513 
514 /*MC
515    CHKERRA - Fortran-only replacement for PetscCall in the main program, which aborts immediately
516 
517    Synopsis:
518    #include <petscsys.h>
519    PetscErrorCode CHKERRA(PetscErrorCode ierr)
520 
521    Not Collective
522 
523    Input Parameters:
524 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
525 
526   Level: beginner
527 
528    Notes:
529       This should only be used with Fortran. With C/C++, use PetscCall() in normal usage,
530       or PetscCallAbort() if wanting to abort immediately on error.
531 
532    Fortran Notes:
533       PetscCall() may be called from Fortran subroutines but CHKERRA() must be called from the
534       Fortran main program.
535 
536 .seealso: `PetscCall()`, `PetscCallAbort()`, `SETERRA()`, `SETERRQ()`, `SETERRABORT()`
537 M*/
538 
539 PETSC_EXTERN PetscErrorCode PetscAbortFindSourceFile_Private(const char*,PetscInt*);
540 PETSC_EXTERN PetscBool petscwaitonerrorflg;
541 PETSC_EXTERN PetscBool petscindebugger;
542 
543 /*MC
544    PETSCABORT - Call MPI_Abort with an informative error code
545 
546    Synopsis:
547    #include <petscsys.h>
548    PETSCABORT(MPI_Comm comm, PetscErrorCode ierr)
549 
550    Collective
551 
552    Input Parameters:
553 +  comm - A communicator, so that the error can be collective
554 -  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
555 
556    Level: advanced
557 
558    Notes:
559    We pass MPI_Abort() an error code of format XX_YYYY_ZZZ, where XX, YYYY are an index and line number of the file
560    where PETSCABORT is called, respectively. ZZZ is the PETSc error code.
561 
562    If XX is zero, this means that the call was made in the routine main().
563    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[]
564      is out of date. PETSc developers have to update it.
565    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.
566 
567    If the option -start_in_debugger was used then this calls abort() to stop the program in the debugger.
568 
569  M*/
570 #define PETSCABORT(comm,...) do {                                                              \
571     if (petscwaitonerrorflg) PetscSleep(1000);                                                 \
572     if (petscindebugger) abort();                                                              \
573     else {                                                                                     \
574       PetscErrorCode ierr_petsc_abort_ = __VA_ARGS__;                                          \
575       PetscInt       idx = 0;                                                                  \
576       PetscAbortFindSourceFile_Private(__FILE__,&idx);                                         \
577       MPI_Abort(comm,(PetscMPIInt)(0*idx*10000000 + 0*__LINE__*1000 + ierr_petsc_abort_));     \
578     }                                                                                          \
579   } while (0)
580 
581 #ifdef PETSC_CLANGUAGE_CXX
582 /*MC
583   PetscCallThrow - Checks error code, if non-zero it calls the C++ error handler which throws
584   an exception
585 
586   Synopsis:
587   #include <petscerror.h>
588   void PetscCallThrow(PetscErrorCode ierr)
589 
590   Not Collective
591 
592   Input Parameter:
593 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
594 
595   Notes:
596   Requires PETSc to be configured with clanguage = c++. Throws a std::runtime_error() on error.
597 
598   Once the error handler throws the exception you can use PetscCallVoid() which returns without
599   an error code (bad idea since the error is ignored) or PetscCallAbort() to have MPI_Abort()
600   called immediately.
601 
602   Level: beginner
603 
604 .seealso: `SETERRQ()`, `PetscCall()`, `SETERRABORT()`, `PetscCallAbort()`, `PetscTraceBackErrorHandler()`,
605           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
606 M*/
607 #define PetscCallThrow(...) do {                                                                    \
608     PetscErrorCode ierr_cxx_ = __VA_ARGS__;                                                    \
609     if (PetscUnlikely(ierr_cxx_)) PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr_cxx_,PETSC_ERROR_IN_CXX,PETSC_NULLPTR); \
610   } while (0)
611 
612 /*MC
613   CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
614 
615   Synopsis:
616   #include <petscerror.h>
617   void CHKERRXX(PetscErrorCode ierr)
618 
619   Not Collective
620 
621   Input Parameter:
622 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
623 
624   Notes:
625   Deprecated in favor of PetscCallThrow(). This routine behaves identically to it.
626 
627   Level: deprecated
628 
629 .seealso: `PetscCallThrow()`
630 M*/
631 #define CHKERRXX(...) PetscCallThrow(__VA_ARGS__)
632 #endif
633 
634 /*MC
635   PetscCallCXX - Checks C++ function calls and if they throw an exception, catch it and then
636   return a PETSc error code
637 
638   Synopsis:
639   #include <petscerror.h>
640   void PetscCallCXX(expr) noexcept;
641 
642   Not Collective
643 
644   Input Parameter:
645 . expr - An arbitrary expression
646 
647   Notes:
648   PetscCallCXX(expr) is a macro replacement for
649 .vb
650   try {
651     expr;
652   } catch (const std::exception& e) {
653     return ConvertToPetscErrorCode(e);
654   }
655 .ve
656   Due to the fact that it catches any (reasonable) exception, it is essentially noexcept.
657 
658   Example Usage:
659 .vb
660   void foo(void) { throw std::runtime_error("error"); }
661 
662   void bar()
663   {
664     PetscCallCXX(foo()); // ERROR bar() does not return PetscErrorCode
665   }
666 
667   PetscErrorCode baz()
668   {
669     PetscCallCXX(foo()); // OK
670 
671     PetscCallCXX(
672       bar();
673       foo(); // OK mutliple statements allowed
674     );
675   }
676 
677   struct bop
678   {
679     bop()
680     {
681       PetscCallCXX(foo()); // ERROR returns PetscErrorCode, cannot be used in constructors
682     }
683   };
684 
685   // ERROR contains do-while, cannot be used as function-try block
686   PetscErrorCode qux() PetscCallCXX(
687     bar();
688     baz();
689     foo();
690     return 0;
691   )
692 .ve
693 
694   Level: beginner
695 
696 .seealso: `PetscCallThrow()`, `SETERRQ()`, `PetscCall()`, `SETERRABORT()`, `PetscCallAbort()`,
697           `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
698 M*/
699 #define PetscCallCXX(...) do {                                  \
700     try {                                                       \
701       __VA_ARGS__;                                              \
702     } catch (const std::exception& e) {                         \
703       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"%s",e.what());     \
704     }                                                           \
705   } while (0)
706 
707 /*MC
708   CHKERRCXX - Checks C++ function calls and if they throw an exception, catch it and then
709   return a PETSc error code
710 
711   Synopsis:
712   #include <petscerror.h>
713   void CHKERRCXX(func) noexcept;
714 
715   Not Collective
716 
717   Input Parameter:
718 . func - C++ function calls
719 
720   Notes:
721   Deprecated in favor of PetscCallCXX(). This routine behaves identically to it.
722 
723   Level: deprecated
724 
725 .seealso: `PetscCallCXX()`
726 M*/
727 #define CHKERRCXX(...) PetscCallCXX(__VA_ARGS__)
728 
729 /*MC
730    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
731 
732    Synopsis:
733    #include <petscsys.h>
734    CHKMEMQ;
735 
736    Not Collective
737 
738   Level: beginner
739 
740    Notes:
741     We highly recommend using Valgrind https://petsc.org/release/faq/#valgrind or for NVIDIA CUDA systems
742     https://docs.nvidia.com/cuda/cuda-memcheck/index.html for finding memory problems. The ``CHKMEMQ`` macro is useful on systems that
743     do not have valgrind, but is not as good as valgrind or cuda-memcheck.
744 
745     Must run with the option -malloc_debug (-malloc_test in debug mode; or if PetscMallocSetDebug() called) to enable this option
746 
747     Once the error handler is called the calling function is then returned from with the given error code.
748 
749     By defaults prints location where memory that is corrupted was allocated.
750 
751     Use CHKMEMA for functions that return void
752 
753 .seealso: `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `SETERRQ()`, `PetscMallocValidate()`
754 M*/
755 #if defined(PETSC_CLANG_STATIC_ANALYZER)
756 #define CHKMEMQ
757 #define CHKMEMA
758 #else
759 #define CHKMEMQ PetscCall(PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__));
760 #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__)
761 #endif
762 
763 /*E
764   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
765 
766   Level: advanced
767 
768   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
769 
770   Developer Notes:
771     This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandler()
772 
773 .seealso: `PetscError()`, `SETERRXX()`
774 E*/
775 typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
776 
777 #if defined(__clang_analyzer__)
778 __attribute__((analyzer_noreturn))
779 #endif
780 PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...) PETSC_ATTRIBUTE_COLD PETSC_ATTRIBUTE_FORMAT(7,8);
781 
782 PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
783 PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
784 PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
785 PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
786 PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
787 PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
788 PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
789 PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
790 PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
791 PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
792 PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
793 PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int,void*);
794 PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
795 PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
796 PETSC_EXTERN PetscErrorCode PetscCheckPointerSetIntensity(PetscInt);
797 PETSC_EXTERN void PetscSignalSegvCheckPointerOrMpi(void);
798 PETSC_DEPRECATED_FUNCTION("Use PetscSignalSegvCheckPointerOrMpi() (since version 3.13)") static inline void PetscSignalSegvCheckPointer(void) {PetscSignalSegvCheckPointerOrMpi();}
799 
800 /*MC
801     PetscErrorPrintf - Prints error messages.
802 
803    Synopsis:
804     #include <petscsys.h>
805      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
806 
807     Not Collective
808 
809     Input Parameter:
810 .   format - the usual printf() format string
811 
812    Options Database Keys:
813 +    -error_output_stdout - cause error messages to be printed to stdout instead of the  (default) stderr
814 -    -error_output_none - to turn off all printing of error messages (does not change the way the error is handled.)
815 
816    Notes:
817     Use
818 $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
819 $                        error is handled.) and
820 $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on or you can use your own function
821 
822           Use
823      PETSC_STDERR = FILE* obtained from a file open etc. to have stderr printed to the file.
824      PETSC_STDOUT = FILE* obtained from a file open etc. to have stdout printed to the file.
825 
826           Use
827       PetscPushErrorHandler() to provide your own error handler that determines what kind of messages to print
828 
829    Level: developer
830 
831     Fortran Note:
832     This routine is not supported in Fortran.
833 
834 .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscHelpPrintf()`, `PetscPrintf()`, `PetscPushErrorHandler()`, `PetscVFPrintf()`, `PetscHelpPrintf()`
835 M*/
836 PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[],...) PETSC_ATTRIBUTE_FORMAT(1,2);
837 
838 typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
839 PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
840 PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
841 PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
842 PETSC_EXTERN PetscErrorCode PetscDetermineInitialFPTrap(void);
843 
844 /*
845       Allows the code to build a stack frame as it runs
846 */
847 
848 #if defined(PETSC_USE_DEBUG)
849 #define PETSCSTACKSIZE 64
850 typedef struct  {
851   const char *function[PETSCSTACKSIZE];
852   const char *file[PETSCSTACKSIZE];
853         int  line[PETSCSTACKSIZE];
854         int  petscroutine[PETSCSTACKSIZE]; /* 0 external called from petsc, 1 petsc functions, 2 petsc user functions */
855         int  currentsize;
856         int  hotdepth;
857   PetscBool  check; /* runtime option to check for correct Push/Pop semantics at runtime */
858 } PetscStack;
859 PETSC_EXTERN PetscStack petscstack;
860 #else
861 typedef struct {
862   char Silence_empty_struct_has_size_0_in_C_size_1_in_Cpp;
863 } PetscStack;
864 #endif
865 
866 #if defined(PETSC_SERIALIZE_FUNCTIONS)
867 #include <petsc/private/petscfptimpl.h>
868 /*
869    Registers the current function into the global function pointer to function name table
870 
871    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
872 */
873 #define PetscRegister__FUNCT__() do { \
874   static PetscBool __chked = PETSC_FALSE; \
875   if (!__chked) {\
876   void *ptr; PetscDLSym(NULL,PETSC_FUNCTION_NAME,&ptr);\
877   __chked = PETSC_TRUE;\
878   }} while (0)
879 #else
880 #define PetscRegister__FUNCT__()
881 #endif
882 
883 #if defined(PETSC_CLANG_STATIC_ANALYZER)
884 #define PetscStackPushNoCheck(funct,petsc_routine,hot)
885 #define PetscStackPopNoCheck
886 #define PetscStackClearTop
887 #define PetscFunctionBegin
888 #define PetscFunctionBeginUser
889 #define PetscFunctionBeginHot
890 #define PetscFunctionReturn(a)    return a
891 #define PetscFunctionReturnVoid() return
892 #define PetscStackPop
893 #define PetscStackPush(f)
894 #elif defined(PETSC_USE_DEBUG)
895 #define PetscStackPush_Private(stack__,file__,func__,line__,petsc_routine__,hot__) do { \
896     if (stack__.currentsize < PETSCSTACKSIZE) {                                         \
897       stack__.file[stack__.currentsize]         = file__;                               \
898       stack__.function[stack__.currentsize]     = func__;                               \
899       stack__.line[stack__.currentsize]         = line__;                               \
900       stack__.petscroutine[stack__.currentsize] = petsc_routine__;                      \
901     }                                                                                   \
902     ++stack__.currentsize;                                                              \
903     stack__.hotdepth += (hot__ || stack__.hotdepth);                                    \
904   } while (0)
905 
906 #define PetscStackPop_Private(stack__,func__) do {                                             \
907     if (PetscUnlikely(stack__.currentsize <= 0)) {                                             \
908       if (PetscUnlikely(stack__.check)) {                                                      \
909         printf("Invalid stack size %d, pop %s\n",stack__.currentsize,func__);                  \
910       }                                                                                        \
911     } else {                                                                                   \
912       if (--stack__.currentsize < PETSCSTACKSIZE) {                                            \
913         if (PetscUnlikely(                                                                     \
914               stack__.check                           &&                                       \
915               stack__.petscroutine[stack__.currentsize] &&                                     \
916               (stack__.function[stack__.currentsize]    != (const char*)(func__)))) {          \
917           /* We need this string comparison because "unknown" can be defined in different static strings: */ \
918           PetscBool _cmpflg;                                                                   \
919           const char *_funct = stack__.function[stack__.currentsize];                          \
920           PetscStrcmp(_funct,func__,&_cmpflg);                                                 \
921           if (!_cmpflg) printf("Invalid stack: push from %s, pop from %s\n",_funct,func__);    \
922         }                                                                                      \
923         stack__.function[stack__.currentsize]     = PETSC_NULLPTR;                             \
924         stack__.file[stack__.currentsize]         = PETSC_NULLPTR;                             \
925         stack__.line[stack__.currentsize]         = 0;                                         \
926         stack__.petscroutine[stack__.currentsize] = 0;                                         \
927       }                                                                                        \
928       stack__.hotdepth = PetscMax(stack__.hotdepth-1,0);                                       \
929     }                                                                                          \
930   } while (0)
931 
932 /* Stack handling is based on the following two "NoCheck" macros.  These should only be called directly by other error
933  * handling macros.  We record the line of the call, which may or may not be the location of the definition.  But is at
934  * least more useful than "unknown" because it can distinguish multiple calls from the same function.
935  */
936 #define PetscStackPushNoCheck(funct,petsc_routine,hot) do {                             \
937     PetscStackSAWsTakeAccess();                                                         \
938     PetscStackPush_Private(petscstack,__FILE__,funct,__LINE__,petsc_routine,hot);       \
939     PetscStackSAWsGrantAccess();                                                        \
940   } while (0)
941 
942 #define PetscStackPopNoCheck(funct)                    do {     \
943     PetscStackSAWsTakeAccess();                                 \
944     PetscStackPop_Private(petscstack,funct);                    \
945     PetscStackSAWsGrantAccess();                                \
946   } while (0)
947 
948 #define PetscStackClearTop                             do {             \
949     PetscStackSAWsTakeAccess();                                         \
950     if (petscstack.currentsize > 0 &&                                   \
951         --petscstack.currentsize < PETSCSTACKSIZE) {                    \
952       petscstack.function[petscstack.currentsize]     = PETSC_NULLPTR;  \
953       petscstack.file[petscstack.currentsize]         = PETSC_NULLPTR;  \
954       petscstack.line[petscstack.currentsize]         = 0;              \
955       petscstack.petscroutine[petscstack.currentsize] = 0;              \
956     }                                                                   \
957     petscstack.hotdepth = PetscMax(petscstack.hotdepth-1,0);            \
958     PetscStackSAWsGrantAccess();                                        \
959   } while (0)
960 
961 /*MC
962    PetscFunctionBegin - First executable line of each PETSc function,  used for error handling. Final
963       line of PETSc functions should be PetscFunctionReturn(0);
964 
965    Synopsis:
966    #include <petscsys.h>
967    void PetscFunctionBegin;
968 
969    Not Collective
970 
971    Usage:
972 .vb
973      int something;
974 
975      PetscFunctionBegin;
976 .ve
977 
978    Notes:
979      Use PetscFunctionBeginUser for application codes.
980 
981      Not available in Fortran
982 
983    Level: developer
984 
985 .seealso: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`
986 
987 M*/
988 #define PetscFunctionBegin do {                               \
989     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,1,PETSC_FALSE); \
990     PetscRegister__FUNCT__();                                 \
991   } while (0)
992 
993 /*MC
994    PetscFunctionBeginHot - Substitute for PetscFunctionBegin to be used in functions that are called in
995    performance-critical circumstances.  Use of this function allows for lighter profiling by default.
996 
997    Synopsis:
998    #include <petscsys.h>
999    void PetscFunctionBeginHot;
1000 
1001    Not Collective
1002 
1003    Usage:
1004 .vb
1005      int something;
1006 
1007      PetscFunctionBeginHot;
1008 .ve
1009 
1010    Notes:
1011      Not available in Fortran
1012 
1013    Level: developer
1014 
1015 .seealso: `PetscFunctionBegin`, `PetscFunctionReturn()`
1016 
1017 M*/
1018 #define PetscFunctionBeginHot do {                           \
1019     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,1,PETSC_TRUE); \
1020     PetscRegister__FUNCT__();                                \
1021   } while (0)
1022 
1023 /*MC
1024    PetscFunctionBeginUser - First executable line of user provided PETSc routine
1025 
1026    Synopsis:
1027    #include <petscsys.h>
1028    void PetscFunctionBeginUser;
1029 
1030    Not Collective
1031 
1032    Usage:
1033 .vb
1034      int something;
1035 
1036      PetscFunctionBeginUser;
1037 .ve
1038 
1039    Notes:
1040       Final line of PETSc functions should be PetscFunctionReturn(0) except for main().
1041 
1042       Not available in Fortran
1043 
1044       This is identical to PetscFunctionBegin except it labels the routine as a user
1045       routine instead of as a PETSc library routine.
1046 
1047    Level: intermediate
1048 
1049 .seealso: `PetscFunctionReturn()`, `PetscFunctionBegin`, `PetscFunctionBeginHot`
1050 
1051 M*/
1052 #define PetscFunctionBeginUser do {                           \
1053     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,2,PETSC_FALSE); \
1054     PetscRegister__FUNCT__();                                 \
1055   } while (0)
1056 
1057 #define PetscStackPush(n)       do {        \
1058     PetscStackPushNoCheck(n,0,PETSC_FALSE); \
1059     CHKMEMQ;                                \
1060   } while (0)
1061 
1062 #define PetscStackPop           do {             \
1063       CHKMEMQ;                                   \
1064       PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1065     } while (0)
1066 
1067 /*MC
1068    PetscFunctionReturn - Last executable line of each PETSc function
1069         used for error handling. Replaces return()
1070 
1071    Synopsis:
1072    #include <petscsys.h>
1073    void PetscFunctionReturn(0);
1074 
1075    Not Collective
1076 
1077    Usage:
1078 .vb
1079     ....
1080      PetscFunctionReturn(0);
1081    }
1082 .ve
1083 
1084    Notes:
1085      Not available in Fortran
1086 
1087    Level: developer
1088 
1089 .seealso: `PetscFunctionBegin()`
1090 
1091 M*/
1092 #define PetscFunctionReturn(a)    do {          \
1093     PetscStackPopNoCheck(PETSC_FUNCTION_NAME);  \
1094     return a;                                   \
1095   } while (0)
1096 
1097 #define PetscFunctionReturnVoid() do {          \
1098     PetscStackPopNoCheck(PETSC_FUNCTION_NAME);  \
1099     return;                                     \
1100   } while (0)
1101 #else /* PETSC_USE_DEBUG */
1102 #define PetscStackPushNoCheck(funct,petsc_routine,hot)
1103 #define PetscStackPopNoCheck
1104 #define PetscStackClearTop
1105 #define PetscFunctionBegin
1106 #define PetscFunctionBeginUser
1107 #define PetscFunctionBeginHot
1108 #define PetscFunctionReturn(a)    return a
1109 #define PetscFunctionReturnVoid() return
1110 #define PetscStackPop             CHKMEMQ
1111 #define PetscStackPush(f)         CHKMEMQ
1112 #endif /* PETSC_USE_DEBUG */
1113 
1114 #if defined(PETSC_CLANG_STATIC_ANALYZER)
1115 #define PetscStackCall(name,routine)
1116 #define PetscStackCallStandard(func,...)
1117 #else
1118 /*
1119     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
1120 
1121    Input Parameters:
1122 +   name - string that gives the name of the function being called
1123 -   routine - actual call to the routine, including ierr = and PetscCall(ierr);
1124 
1125    Note: Often one should use PetscStackCallStandard() instead. This routine is intended for external library routines that DO NOT return error codes
1126 
1127    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.
1128 
1129 */
1130 #define PetscStackCall(name,routine) do { PetscStackPush(name);routine;PetscStackPop; } while (0)
1131 
1132 /*
1133     PetscStackCallStandard - Calls an external library routine after pushing the name of the routine on the stack.
1134 
1135    Input Parameters:
1136 +   func-  name of the routine
1137 -   args - arguments to the routine surrounded by ()
1138 
1139    Notes:
1140     This is intended for external package routines that return error codes. Use PetscStackCall() for those that do not.
1141 
1142    Developer Note: this is so that when an external packge routine results in a crash or corrupts memory, they get blamed instead of PETSc.
1143 
1144 */
1145 #define PetscStackCallStandard(func,...) do {                                                  \
1146     PetscStackPush(PetscStringize(func));                                                      \
1147     PetscErrorCode __ierr = func(__VA_ARGS__);                                                 \
1148     PetscStackPop;                                                                             \
1149     PetscCheck(!__ierr,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s(): error code %d",PetscStringize(func),__ierr); \
1150   } while (0)
1151 #endif /* PETSC_CLANG_STATIC_ANALYZER */
1152 
1153 #endif
1154