xref: /petsc/include/petscerror.h (revision 2d30e087755efd99e28fdfe792ffbeb2ee1ea928)
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     This is rarely needed, one should use `PetscCheck()` and `PetscCall()` and friends to automatically handle error conditions.
101     Once the error handler is called the calling function is then returned from with the given error code.
102 
103     Experienced users can set the error handler with `PetscPushErrorHandler()`.
104 
105    Fortran Notes:
106       SETERRQ() may be called from Fortran subroutines but SETERRA() must be called from the
107       Fortran main program.
108 
109 .seealso: `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
110           `PetscError()`, `PetscCall()`, `CHKMEMQ`, `CHKERRA()`, `PetscCallMPI()`
111 M*/
112 #define SETERRQ(comm, ierr, ...) return PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__)
113 
114 /*
115     Returned from PETSc functions that are called from MPI, such as related to attributes
116       Do not confuse PETSC_MPI_ERROR_CODE and PETSC_ERR_MPI, the first is registered with MPI and returned to MPI as
117       an error code, the latter is a regular PETSc error code passed within PETSc code indicating an error was detected in an MPI call.
118 */
119 PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CLASS;
120 PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CODE;
121 
122 /*MC
123    SETERRMPI - Macro to be called when an error has been detected within an MPI callback function
124 
125    Synopsis:
126    #include <petscsys.h>
127    PetscErrorCode SETERRMPI(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
128 
129    Collective
130 
131    Input Parameters:
132 +  comm - A communicator, use `PETSC_COMM_SELF` unless you know all ranks of another communicator will detect the error
133 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
134 -  message - error message
135 
136   Level: developer
137 
138    Notes:
139     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`
140     which is registered with `MPI_Add_error_code()` when PETSc is initialized.
141 
142 .seealso: `SETERRQ()`, `PetscCall()`, `PetscCallMPI()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
143 M*/
144 #define SETERRMPI(comm, ierr, ...) return (PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__), PETSC_MPI_ERROR_CODE)
145 
146 /*MC
147    SETERRA - Fortran-only macro that can be called when an error has been detected from the main program
148 
149    Synopsis:
150    #include <petscsys.h>
151    PetscErrorCode SETERRA(MPI_Comm comm,PetscErrorCode ierr,char *message)
152 
153    Collective
154 
155    Input Parameters:
156 +  comm - A communicator, so that the error can be collective
157 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
158 -  message - error message in the printf format
159 
160   Level: beginner
161 
162    Notes:
163     This should only be used with Fortran. With C/C++, use `SETERRQ()`.
164 
165    Fortran Notes:
166       `SETERRQ()` may be called from Fortran subroutines but `SETERRA()` must be called from the
167       Fortran main program.
168 
169 .seealso: `SETERRQ()`, `SETERRABORT()`, `PetscCall()`, `CHKERRA()`, `PetscCallAbort()`
170 M*/
171 
172 /*MC
173    SETERRABORT - Macro that can be called when an error has been detected,
174 
175    Synopsis:
176    #include <petscsys.h>
177    PetscErrorCode SETERRABORT(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
178 
179    Collective
180 
181    Input Parameters:
182 +  comm - A communicator, so that the error can be collective
183 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
184 -  message - error message in the printf format
185 
186   Level: beginner
187 
188    Notes:
189     This function just calls `MPI_Abort()`.
190 
191     This should only be called in routines that cannot return an error code, such as in C++ constructors.
192 
193 .seealso: `SETERRQ()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `PetscCall()`, `CHKMEMQ`
194 M*/
195 #define SETERRABORT(comm, ierr, ...) \
196   do { \
197     PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__); \
198     MPI_Abort(comm, ierr); \
199   } while (0)
200 
201 /*MC
202   PetscCheck - Check that a particular condition is true
203 
204   Synopsis:
205   #include <petscerror.h>
206   void PetscCheck(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
207 
208   Collective
209 
210   Input Parameters:
211 + cond    - The boolean condition
212 . comm    - The communicator on which the check can be collective on
213 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
214 - message - Error message in printf format
215 
216   Notes:
217   Enabled in both optimized and debug builds.
218 
219   Calls `SETERRQ()` if the assertion fails, so can only be called from functions returning a
220   `PetscErrorCode` (or equivalent type after conversion).
221 
222   Level: beginner
223 
224 .seealso: `PetscAssert()`, `SETERRQ()`, `PetscError()`, `PetscCall()`, `PetscCheckAbort()`
225 M*/
226 #define PetscCheck(cond, comm, ierr, ...) \
227   do { \
228     if (PetscUnlikely(!(cond))) SETERRQ(comm, ierr, __VA_ARGS__); \
229   } while (0)
230 
231 /*MC
232   PetscCheckAbort - Check that a particular condition is true, otherwise prints error and aborts
233 
234   Synopsis:
235   #include <petscerror.h>
236   void PetscCheckAbort(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
237 
238   Collective
239 
240   Input Parameters:
241 + cond    - The boolean condition
242 . comm    - The communicator on which the check can be collective on
243 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
244 - message - Error message in printf format
245 
246   Notes:
247   Enabled in both optimized and debug builds.
248 
249   Calls `SETERRABORT()` if the assertion fails, can be called from a function that does not return an
250   error code, such as a C++ constructor. usually `PetscCheck()` should be used.
251 
252   Level: developer
253 
254 .seealso: `PetscAssert()`, `SETERRQ()`, `PetscError()`, `PetscCall()`, `PetscCheck()`, `SETTERRABORT()`
255 M*/
256 #define PetscCheckAbort(cond, comm, ierr, ...) \
257   if (PetscUnlikely(!(cond))) SETERRABORT(comm, ierr, __VA_ARGS__)
258 
259 /*MC
260   PetscAssert - Assert that a particular condition is true
261 
262   Synopsis:
263   #include <petscerror.h>
264   void PetscAssert(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
265 
266   Collective
267 
268   Input Parameters:
269 + cond    - The boolean condition
270 . comm    - The communicator on which the check can be collective on
271 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
272 - message - Error message in printf format
273 
274   Notes:
275   Enabled only in debug builds. Note that any arguments to this macros are still visible to the
276   compiler optimized builds (so must still contain valid code) but are guaranteed to not be
277   executed.
278 
279   See `PetscCheck()` for usage and behaviour.
280 
281   This is needed instead of simply using `assert()` because this correctly handles the collective nature of errors under MPI
282 
283   Level: beginner
284 
285 .seealso: `PetscCheck()`, `SETERRQ()`, `PetscError()`
286 M*/
287 #define PetscAssert(cond, comm, ierr, ...) \
288   do { \
289     if (PetscUnlikelyDebug(!(cond))) SETERRQ(comm, ierr, __VA_ARGS__); \
290   } while (0)
291 
292 /*MC
293   PetscCall - Calls a PETSc function and then checks the resulting error code, if it is non-zero it calls the error
294   handler and returns from the current function with the error code.
295 
296   Synopsis:
297   #include <petscerror.h>
298   void PetscCall(PetscFunction(args))
299 
300   Not Collective
301 
302   Input Parameter:
303 . PetscFunction - any PETSc function that returns an error code
304 
305   Notes:
306   Once the error handler is called the calling function is then returned from with the given
307   error code. Experienced users can set the error handler with `PetscPushErrorHandler()`.
308 
309   `PetscCall()` cannot be used in functions returning a datatype not convertible to
310   `PetscErrorCode`. For example, `PetscCall()` may not be used in functions returning void, use
311   `PetscCallVoid()` in this case.
312 
313   Example Usage:
314 .vb
315   PetscCall(PetscInitiailize(...)); // OK to call even when PETSc is not yet initialized!
316 
317   struct my_struct
318   {
319     void *data;
320   } my_complex_type;
321 
322   struct my_struct bar(void)
323   {
324     PetscCall(foo(15)); // ERROR PetscErrorCode not convertible to struct my_struct!
325   }
326 
327   PetscCall(bar()) // ERROR input not convertible to PetscErrorCode
328 .ve
329 
330   It is also possible to call this directly on a `PetscErrorCode` variable
331 .vb
332   PetscCall(ierr);  // check if ierr is nonzero
333 .ve
334 
335   Should not be used to call callback functions provided by users, `PetscCallBack()` should be used in that situation.
336 
337   `PetscUseTypeMethod()` or `PetscTryTypeMethod()` should be used when calling functions pointers contained in a PETSc object's `ops` array
338 
339   Fortran Notes:
340     The Fortran function from which this is used must declare a variable PetscErrorCode ierr and ierr must be
341     the final argument to the PETSc function being called.
342 
343     In the main program and in Fortran subroutines that do not have ierr as the final return parameter one
344     should use `PetscCallA()`
345 
346   Example Fortran Usage:
347 .vb
348   PetscErrorCode ierr
349   Vec v
350 
351   ...
352   PetscCall(VecShift(v,1.0,ierr))
353   PetscCallA(VecShift(v,1.0,ierr))
354 .ve
355 
356   Level: beginner
357 
358 .seealso: `SETERRQ()`, `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`
359           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`, `CHKERRMPI()`, `PetscCallBack()`
360 M*/
361 
362 /*MC
363   PetscCallBack - Calls a user provided PETSc callback function and then checks the resulting error code, if it is non-zero it calls the error
364   handler and returns from the current function with the error code.
365 
366   Synopsis:
367   #include <petscerror.h>
368   void PetscCallBack(const char *functionname,PetscFunction(args))
369 
370   Not Collective
371 
372   Input Parameters:
373 + functionname - the name of the function being called, this can be a string with spaces that describes the meaning of the callback
374 - PetscFunction - user provided callback function that returns an error code
375 
376   Notes:
377   Once the error handler is called the calling function is then returned from with the given
378   error code. Experienced users can set the error handler with `PetscPushErrorHandler()`.
379 
380   `PetscCallBack()` should only be called in PETSc when a call is being made to a user provided call-back routine.
381 
382   Example Usage:
383 .vb
384   PetscCallBack("XXX callback to do something",a->callback(...));
385 .ve
386 
387   Level: developer
388 
389 .seealso: `SETERRQ()`, `PetscCheck()`, `PetscCall()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`
390           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`, `CHKERRMPI()`, `PetscCall()`
391 M*/
392 
393 #if defined(PETSC_CLANG_STATIC_ANALYZER)
394 void PetscCall(PetscErrorCode);
395 void PetscCallBack(const char *, PetscErrorCode);
396 void PetscCallVoid(PetscErrorCode);
397 #else
398 #define PetscCall(...) \
399   do { \
400     PetscErrorCode ierr_q_; \
401     PetscStackUpdateLine; \
402     ierr_q_ = __VA_ARGS__; \
403     if (PetscUnlikely(ierr_q_)) return PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_q_, PETSC_ERROR_REPEAT, " "); \
404   } while (0)
405 #define PetscCallBack(function, ...) \
406   do { \
407     PetscErrorCode ierr_q_; \
408     PetscStackUpdateLine; \
409     PetscStackPushExternal(function); \
410     ierr_q_ = __VA_ARGS__; \
411     PetscStackPop; \
412     if (PetscUnlikely(ierr_q_)) return PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_q_, PETSC_ERROR_REPEAT, " "); \
413   } while (0)
414 #define PetscCallVoid(...) \
415   do { \
416     PetscErrorCode ierr_void_; \
417     PetscStackUpdateLine; \
418     ierr_void_ = __VA_ARGS__; \
419     if (PetscUnlikely(ierr_void_)) { \
420       (void)PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_void_, PETSC_ERROR_REPEAT, " "); \
421       return; \
422     } \
423   } while (0)
424 #endif
425 
426 /*MC
427   CHKERRQ - Checks error code returned from PETSc function
428 
429   Synopsis:
430   #include <petscsys.h>
431   void CHKERRQ(PetscErrorCode ierr)
432 
433   Not Collective
434 
435   Input Parameters:
436 . ierr - nonzero error code
437 
438   Notes:
439   Deprecated in favor of `PetscCall()`. This routine behaves identically to it.
440 
441   Level: deprecated
442 
443 .seealso: `PetscCall()`
444 M*/
445 #define CHKERRQ(...) PetscCall(__VA_ARGS__)
446 #define CHKERRV(...) PetscCallVoid(__VA_ARGS__)
447 
448 PETSC_EXTERN void PetscMPIErrorString(PetscMPIInt, char *);
449 
450 /*MC
451   PetscCallMPI - Checks error code returned from MPI calls, if non-zero it calls the error
452   handler and then returns
453 
454   Synopsis:
455   #include <petscerror.h>
456   void PetscCallMPI(MPI_Function(args))
457 
458   Not Collective
459 
460   Input Parameters:
461 . MPI_Function - an MPI function that returns an MPI error code
462 
463   Notes:
464   Always returns the error code `PETSC_ERR_MPI`; the MPI error code and string are embedded in
465   the string error message. Do not use this to call any other routines (for example PETSc
466   routines), it should only be used for direct MPI calls. Due to limitations of the
467   preprocessor this can unfortunately not easily be enforced, so the user should take care to
468   check this themselves.
469 
470   Example Usage:
471 .vb
472   PetscCallMPI(MPI_Comm_size(...)); // OK, calling MPI function
473 
474   PetscCallMPI(PetscFunction(...)); // ERROR, use PetscCall() instead!
475 .ve
476 
477   Fortran Notes:
478     The Fortran function from which this is used must declare a variable `PetscErrorCode` ierr and ierr must be
479     the final argument to the MPI function being called.
480 
481     In the main program and in Fortran subroutines that do not have ierr as the final return parameter one
482     should use `PetscCallMPIA()`
483 
484   Fortran Usage:
485 .vb
486   PetscErrorCode ierr or integer ierr
487   ...
488   PetscCallMPI(MPI_Comm_size(...,ierr))
489   PetscCallMPIA(MPI_Comm_size(...,ierr)) ! Will abort after calling error handler
490 
491   PetscCallMPI(MPI_Comm_size(...,eflag)) ! ERROR, final argument must be ierr
492 .ve
493 
494   Level: beginner
495 
496 .seealso: `SETERRMPI()`, `PetscCall()`, `SETERRQ()`, `SETERRABORT()`, `PetscCallAbort()`,
497           `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
498 M*/
499 #if defined(PETSC_CLANG_STATIC_ANALYZER)
500 void PetscCallMPI(PetscMPIInt);
501 #else
502 #define PetscCallMPI(...) \
503   do { \
504     PetscMPIInt _7_errorcode; \
505     char        _7_errorstring[2 * MPI_MAX_ERROR_STRING]; \
506     PetscStackUpdateLine; \
507     PetscStackPushExternal("MPI function"); \
508     { _7_errorcode = __VA_ARGS__; } \
509     PetscStackPop; \
510     if (PetscUnlikely(_7_errorcode)) { \
511       PetscMPIErrorString(_7_errorcode, (char *)_7_errorstring); \
512       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_MPI, "MPI error %d %s", (int)_7_errorcode, _7_errorstring); \
513     } \
514   } while (0)
515 #endif
516 
517 /*MC
518   CHKERRMPI - Checks error code returned from MPI calls, if non-zero it calls the error
519   handler and then returns
520 
521   Synopsis:
522   #include <petscerror.h>
523   void CHKERRMPI(PetscErrorCode ierr)
524 
525   Not Collective
526 
527   Input Parameter:
528 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
529 
530   Notes:
531   Deprecated in favor of `PetscCallMPI()`. This routine behaves identically to it.
532 
533   Level: deprecated
534 
535 .seealso: `PetscCallMPI()`
536 M*/
537 #define CHKERRMPI(...) PetscCallMPI(__VA_ARGS__)
538 
539 /*MC
540   PetscCallAbort - Checks error code returned from PETSc function, if non-zero it aborts immediately
541 
542   Synopsis:
543   #include <petscerror.h>
544   void PetscCallAbort(MPI_Comm comm, PetscErrorCode ierr)
545 
546   Collective on comm
547 
548   Input Parameters:
549 + comm - the MPI communicator on which to abort
550 - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
551 
552   Notes:
553   This macro has identical type and usage semantics to `PetscCall()` with the important caveat
554   that this macro does not return. Instead, if ierr is nonzero it calls the PETSc error handler
555   and then immediately calls `MPI_Abort()`. It can therefore be used anywhere.
556 
557   As per `MPI_Abort()` semantics the communicator passed must be valid, although there is currently
558   no attempt made at handling any potential errors from `MPI_Abort()`. Note that while
559   `MPI_Abort()` is required to terminate only those processes which reside on comm, it is often
560   the case that `MPI_Abort()` terminates *all* processes.
561 
562   Example Usage:
563 .vb
564   PetscErrorCode boom(void) { return PETSC_ERR_MEM; }
565 
566   void foo(void)
567   {
568     PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
569   }
570 
571   double bar(void)
572   {
573     PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
574   }
575 
576   PetscCallAbort(MPI_COMM_NULL,boom()); // ERROR, communicator should be valid
577 
578   struct baz
579   {
580     baz()
581     {
582       PetscCallAbort(PETSC_COMM_SELF,boom()); // OK
583     }
584 
585     ~baz()
586     {
587       PetscCallAbort(PETSC_COMM_SELF,boom()); // OK (in fact the only way to handle PETSc errors)
588     }
589   };
590 .ve
591 
592   Level: intermediate
593 
594 .seealso: `SETERRABORT()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`,
595           `SETERRQ()`, `CHKMEMQ`, `PetscCallMPI()`
596 M*/
597 #if defined(PETSC_CLANG_STATIC_ANALYZER)
598 void PetscCallAbort(MPI_Comm, PetscErrorCode);
599 void PetscCallContinue(PetscErrorCode);
600 #else
601 #define PetscCallAbort(comm, ...) \
602   do { \
603     PetscErrorCode ierr_abort_ = __VA_ARGS__; \
604     if (PetscUnlikely(ierr_abort_)) { \
605       PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_abort_, PETSC_ERROR_REPEAT, " "); \
606       MPI_Abort(comm, ierr_abort_); \
607     } \
608   } while (0)
609 #define PetscCallContinue(...) \
610   do { \
611     PetscErrorCode ierr_continue_ = __VA_ARGS__; \
612     if (PetscUnlikely(ierr_continue_)) PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_continue_, PETSC_ERROR_REPEAT, " "); \
613   } while (0)
614 #endif
615 
616 /*MC
617   CHKERRABORT - Checks error code returned from PETSc function. If non-zero it aborts immediately.
618 
619   Synopsis:
620   #include <petscerror.h>
621   void CHKERRABORT(MPI_Comm comm, PetscErrorCode ierr)
622 
623   Not Collective
624 
625   Input Parameters:
626 + comm - the MPI communicator
627 - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
628 
629   Notes:
630   Deprecated in favor of `PetscCallAbort()`. This routine behaves identically to it.
631 
632   Level: deprecated
633 
634 .seealso: `PetscCallAbort()`
635 M*/
636 #define CHKERRABORT(comm, ...) PetscCallAbort(comm, __VA_ARGS__)
637 #define CHKERRCONTINUE(...)    PetscCallContinue(__VA_ARGS__)
638 
639 /*MC
640    CHKERRA - Fortran-only replacement for use of `CHKERRQ()` in the main program, which aborts immediately
641 
642    Synopsis:
643    #include <petscsys.h>
644    PetscErrorCode CHKERRA(PetscErrorCode ierr)
645 
646    Not Collective
647 
648    Input Parameters:
649 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
650 
651   Level: deprecated
652 
653    Note:
654    This macro is rarely needed, normal usage is `PetscCallA()` in the main Fortran program.
655 
656 .seealso: `PetscCall()`, `PetscCallA()`, `PetscCallAbort()`, `CHKERRQ()`, `SETERRA()`, `SETERRQ()`, `SETERRABORT()`
657 M*/
658 
659 PETSC_EXTERN PetscBool petscwaitonerrorflg;
660 PETSC_EXTERN PetscBool petscindebugger;
661 
662 /*MC
663    PETSCABORT - Call MPI_Abort with an informative error code
664 
665    Synopsis:
666    #include <petscsys.h>
667    PETSCABORT(MPI_Comm comm, PetscErrorCode ierr)
668 
669    Collective
670 
671    Input Parameters:
672 +  comm - A communicator, so that the error can be collective
673 -  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
674 
675    Level: advanced
676 
677    Notes:
678    If the option -start_in_debugger was used then this calls abort() to stop the program in the debugger.
679 
680    if `PetscCIEnabledPortableErrorOutput` is set it strives to exit cleanly without call `MPI_Abort()`
681 
682  M*/
683 #define PETSCABORT(comm, ...) \
684   do { \
685     if (petscwaitonerrorflg) PetscSleep(1000); \
686     if (petscindebugger) abort(); \
687     else { \
688       PetscErrorCode ierr_petsc_abort_ = __VA_ARGS__; \
689       PetscMPIInt    size; \
690       MPI_Comm_size(comm, &size); \
691       if (PetscCIEnabledPortableErrorOutput && size == PetscGlobalSize && ierr_petsc_abort_ != PETSC_ERR_SIG) { \
692         MPI_Finalize(); \
693         exit(0); \
694       } else if (PetscCIEnabledPortableErrorOutput && PetscGlobalSize == 1) { \
695         exit(0); \
696       } else { \
697         MPI_Abort(comm, (PetscMPIInt)ierr_petsc_abort_); \
698       } \
699     } \
700   } while (0)
701 
702 #ifdef PETSC_CLANGUAGE_CXX
703 /*MC
704   PetscCallThrow - Checks error code, if non-zero it calls the C++ error handler which throws
705   an exception
706 
707   Synopsis:
708   #include <petscerror.h>
709   void PetscCallThrow(PetscErrorCode ierr)
710 
711   Not Collective
712 
713   Input Parameter:
714 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
715 
716   Notes:
717   Requires PETSc to be configured with clanguage = c++. Throws a std::runtime_error() on error.
718 
719   Once the error handler throws the exception you can use `PetscCallVoid()` which returns without
720   an error code (bad idea since the error is ignored) or `PetscCallAbort()` to have `MPI_Abort()`
721   called immediately.
722 
723   Level: beginner
724 
725 .seealso: `SETERRQ()`, `PetscCall()`, `SETERRABORT()`, `PetscCallAbort()`, `PetscTraceBackErrorHandler()`,
726           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
727 M*/
728 #define PetscCallThrow(...) \
729   do { \
730     PetscErrorCode ierr_cxx_ = __VA_ARGS__; \
731     if (PetscUnlikely(ierr_cxx_)) PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_cxx_, PETSC_ERROR_IN_CXX, PETSC_NULLPTR); \
732   } while (0)
733 
734 /*MC
735   CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
736 
737   Synopsis:
738   #include <petscerror.h>
739   void CHKERRXX(PetscErrorCode ierr)
740 
741   Not Collective
742 
743   Input Parameter:
744 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
745 
746   Notes:
747   Deprecated in favor of `PetscCallThrow()`. This routine behaves identically to it.
748 
749   Level: deprecated
750 
751 .seealso: `PetscCallThrow()`
752 M*/
753 #define CHKERRXX(...) PetscCallThrow(__VA_ARGS__)
754 #endif
755 
756 /*MC
757   PetscCallCXX - Checks C++ function calls and if they throw an exception, catch it and then
758   return a PETSc error code
759 
760   Synopsis:
761   #include <petscerror.h>
762   void PetscCallCXX(expr) noexcept;
763 
764   Not Collective
765 
766   Input Parameter:
767 . expr - An arbitrary expression
768 
769   Notes:
770   PetscCallCXX(expr) is a macro replacement for
771 .vb
772   try {
773     expr;
774   } catch (const std::exception& e) {
775     return ConvertToPetscErrorCode(e);
776   }
777 .ve
778   Due to the fact that it catches any (reasonable) exception, it is essentially noexcept.
779 
780   Example Usage:
781 .vb
782   void foo(void) { throw std::runtime_error("error"); }
783 
784   void bar()
785   {
786     PetscCallCXX(foo()); // ERROR bar() does not return PetscErrorCode
787   }
788 
789   PetscErrorCode baz()
790   {
791     PetscCallCXX(foo()); // OK
792 
793     PetscCallCXX(
794       bar();
795       foo(); // OK mutliple statements allowed
796     );
797   }
798 
799   struct bop
800   {
801     bop()
802     {
803       PetscCallCXX(foo()); // ERROR returns PetscErrorCode, cannot be used in constructors
804     }
805   };
806 
807   // ERROR contains do-while, cannot be used as function-try block
808   PetscErrorCode qux() PetscCallCXX(
809     bar();
810     baz();
811     foo();
812     return 0;
813   )
814 .ve
815 
816   Level: beginner
817 
818 .seealso: `PetscCallThrow()`, `SETERRQ()`, `PetscCall()`, `SETERRABORT()`, `PetscCallAbort()`,
819           `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
820 M*/
821 #define PetscCallCXX(...) \
822   do { \
823     PetscStackUpdateLine; \
824     try { \
825       __VA_ARGS__; \
826     } catch (const std::exception &e) { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "%s", e.what()); } \
827   } while (0)
828 
829 /*MC
830   CHKERRCXX - Checks C++ function calls and if they throw an exception, catch it and then
831   return a PETSc error code
832 
833   Synopsis:
834   #include <petscerror.h>
835   void CHKERRCXX(func) noexcept;
836 
837   Not Collective
838 
839   Input Parameter:
840 . func - C++ function calls
841 
842   Notes:
843   Deprecated in favor of `PetscCallCXX()`. This routine behaves identically to it.
844 
845   Level: deprecated
846 
847 .seealso: `PetscCallCXX()`
848 M*/
849 #define CHKERRCXX(...) PetscCallCXX(__VA_ARGS__)
850 
851 /*MC
852    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
853 
854    Synopsis:
855    #include <petscsys.h>
856    CHKMEMQ;
857 
858    Not Collective
859 
860   Level: beginner
861 
862    Notes:
863     We highly recommend using Valgrind https://petsc.org/release/faq/#valgrind or for NVIDIA CUDA systems
864     https://docs.nvidia.com/cuda/cuda-memcheck/index.html for finding memory problems. The ``CHKMEMQ`` macro is useful on systems that
865     do not have valgrind, but is not as good as valgrind or cuda-memcheck.
866 
867     Must run with the option -malloc_debug (-malloc_test in debug mode; or if `PetscMallocSetDebug()` called) to enable this option
868 
869     Once the error handler is called the calling function is then returned from with the given error code.
870 
871     By defaults prints location where memory that is corrupted was allocated.
872 
873     Use `CHKMEMA` for functions that return void
874 
875 .seealso: `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `SETERRQ()`, `PetscMallocValidate()`
876 M*/
877 #if defined(PETSC_CLANG_STATIC_ANALYZER)
878 #define CHKMEMQ
879 #define CHKMEMA
880 #else
881 #define CHKMEMQ \
882   do { \
883     PetscErrorCode ierr_memq_ = PetscMallocValidate(__LINE__, PETSC_FUNCTION_NAME, __FILE__); \
884     if (PetscUnlikely(ierr_memq_)) return PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_memq_, PETSC_ERROR_REPEAT, " "); \
885   } while (0)
886 #define CHKMEMA PetscMallocValidate(__LINE__, PETSC_FUNCTION_NAME, __FILE__)
887 #endif
888 
889 /*E
890   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
891 
892   Level: advanced
893 
894   `PETSC_ERROR_IN_CXX` indicates the error was detected in C++ and an exception should be generated
895 
896   Developer Notes:
897     This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandler()
898 
899 .seealso: `PetscError()`, `SETERRQ()`
900 E*/
901 typedef enum {
902   PETSC_ERROR_INITIAL = 0,
903   PETSC_ERROR_REPEAT  = 1,
904   PETSC_ERROR_IN_CXX  = 2
905 } PetscErrorType;
906 
907 #if defined(__clang_analyzer__)
908 __attribute__((analyzer_noreturn))
909 #endif
910 PETSC_EXTERN PetscErrorCode
911 PetscError(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, ...) PETSC_ATTRIBUTE_COLD PETSC_ATTRIBUTE_FORMAT(7, 8);
912 
913 PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
914 PETSC_EXTERN PetscErrorCode PetscErrorMessage(int, const char *[], char **);
915 PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
916 PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
917 PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
918 PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
919 PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
920 PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
921 PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
922 PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *), void *);
923 PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
924 PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int, void *);
925 PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int, void *), void *);
926 PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
927 PETSC_EXTERN PetscErrorCode PetscCheckPointerSetIntensity(PetscInt);
928 PETSC_EXTERN void           PetscSignalSegvCheckPointerOrMpi(void);
929 PETSC_DEPRECATED_FUNCTION("Use PetscSignalSegvCheckPointerOrMpi() (since version 3.13)") static inline void PetscSignalSegvCheckPointer(void) {
930   PetscSignalSegvCheckPointerOrMpi();
931 }
932 
933 /*MC
934     PetscErrorPrintf - Prints error messages.
935 
936    Synopsis:
937     #include <petscsys.h>
938      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
939 
940     Not Collective
941 
942     Input Parameter:
943 .   format - the usual printf() format string
944 
945    Options Database Keys:
946 +    -error_output_stdout - cause error messages to be printed to stdout instead of the (default) stderr
947 -    -error_output_none - to turn off all printing of error messages (does not change the way the error is handled.)
948 
949    Notes:
950     Use
951 $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
952 $                        error is handled.) and
953 $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on or you can use your own function
954 
955           Use
956      `PETSC_STDERR` = FILE* obtained from a file open etc. to have stderr printed to the file.
957      `PETSC_STDOUT` = FILE* obtained from a file open etc. to have stdout printed to the file.
958 
959           Use
960       `PetscPushErrorHandler()` to provide your own error handler that determines what kind of messages to print
961 
962    Level: developer
963 
964     Fortran Note:
965     This routine is not supported in Fortran.
966 
967 .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscHelpPrintf()`, `PetscPrintf()`, `PetscPushErrorHandler()`, `PetscVFPrintf()`, `PetscHelpPrintf()`
968 M*/
969 PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
970 
971 /*E
972      PetscFPTrap - types of floating point exceptions that may be trapped
973 
974      Currently only `PETSC_FP_TRAP_OFF` and `PETSC_FP_TRAP_ON` are handled. All others are treated as `PETSC_FP_TRAP_ON`.
975 
976      Level: intermediate
977 
978 .seealso: `PetscSetFPTrap()`, `PetscPushFPTrap()`
979  E*/
980 typedef enum {
981   PETSC_FP_TRAP_OFF      = 0,
982   PETSC_FP_TRAP_INDIV    = 1,
983   PETSC_FP_TRAP_FLTOPERR = 2,
984   PETSC_FP_TRAP_FLTOVF   = 4,
985   PETSC_FP_TRAP_FLTUND   = 8,
986   PETSC_FP_TRAP_FLTDIV   = 16,
987   PETSC_FP_TRAP_FLTINEX  = 32
988 } PetscFPTrap;
989 #define PETSC_FP_TRAP_ON (PetscFPTrap)(PETSC_FP_TRAP_INDIV | PETSC_FP_TRAP_FLTOPERR | PETSC_FP_TRAP_FLTOVF | PETSC_FP_TRAP_FLTDIV | PETSC_FP_TRAP_FLTINEX)
990 PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
991 PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
992 PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
993 PETSC_EXTERN PetscErrorCode PetscDetermineInitialFPTrap(void);
994 
995 /*
996       Allows the code to build a stack frame as it runs
997 */
998 
999 #if defined(PETSC_USE_DEBUG)
1000 #define PETSCSTACKSIZE 64
1001 typedef struct {
1002   const char *function[PETSCSTACKSIZE];
1003   const char *file[PETSCSTACKSIZE];
1004   int         line[PETSCSTACKSIZE];
1005   int         petscroutine[PETSCSTACKSIZE]; /* 0 external called from petsc, 1 petsc functions, 2 petsc user functions */
1006   int         currentsize;
1007   int         hotdepth;
1008   PetscBool   check; /* option to check for correct Push/Pop semantics, true for default petscstack but not other stacks */
1009 } PetscStack;
1010 PETSC_EXTERN PetscStack petscstack;
1011 #else
1012 typedef struct {
1013   char Silence_empty_struct_has_size_0_in_C_size_1_in_Cpp;
1014 } PetscStack;
1015 #endif
1016 
1017 #if defined(PETSC_SERIALIZE_FUNCTIONS)
1018 #include <petsc/private/petscfptimpl.h>
1019 /*
1020    Registers the current function into the global function pointer to function name table
1021 
1022    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
1023 */
1024 #define PetscRegister__FUNCT__() \
1025   do { \
1026     static PetscBool __chked = PETSC_FALSE; \
1027     if (!__chked) { \
1028       void *ptr; \
1029       PetscDLSym(NULL, PETSC_FUNCTION_NAME, &ptr); \
1030       __chked = PETSC_TRUE; \
1031     } \
1032   } while (0)
1033 #else
1034 #define PetscRegister__FUNCT__()
1035 #endif
1036 
1037 #if defined(PETSC_CLANG_STATIC_ANALYZER)
1038 #define PetscStackPushNoCheck(funct, petsc_routine, hot)
1039 #define PetscStackUpdateLine
1040 #define PetscStackPushExternal(funct)
1041 #define PetscStackPopNoCheck
1042 #define PetscStackClearTop
1043 #define PetscFunctionBegin
1044 #define PetscFunctionBeginUser
1045 #define PetscFunctionBeginHot
1046 #define PetscFunctionReturn(a)    return a
1047 #define PetscFunctionReturnVoid() return
1048 #define PetscStackPop
1049 #define PetscStackPush(f)
1050 #elif defined(PETSC_USE_DEBUG)
1051 
1052 #define PetscStackPush_Private(stack__, file__, func__, line__, petsc_routine__, hot__) \
1053   do { \
1054     if (stack__.currentsize < PETSCSTACKSIZE) { \
1055       stack__.function[stack__.currentsize] = func__; \
1056       if (petsc_routine__) { \
1057         stack__.file[stack__.currentsize] = file__; \
1058         stack__.line[stack__.currentsize] = line__; \
1059       } else { \
1060         stack__.file[stack__.currentsize] = PETSC_NULLPTR; \
1061         stack__.line[stack__.currentsize] = 0; \
1062       } \
1063       stack__.petscroutine[stack__.currentsize] = petsc_routine__; \
1064     } \
1065     ++stack__.currentsize; \
1066     stack__.hotdepth += (hot__ || stack__.hotdepth); \
1067   } while (0)
1068 
1069 /* uses PetscCheckAbort() because may be used in a function that does not return an error code */
1070 #define PetscStackPop_Private(stack__, func__) \
1071   do { \
1072     PetscCheckAbort(!stack__.check || stack__.currentsize > 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid stack size %d, pop %s %s:%d.\n", stack__.currentsize, func__, __FILE__, __LINE__); \
1073     if (--stack__.currentsize < PETSCSTACKSIZE) { \
1074       PetscCheckAbort(!stack__.check || stack__.petscroutine[stack__.currentsize] != 1 || stack__.function[stack__.currentsize] == (const char *)(func__), PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid stack: push from %s %s:%d. Pop from %s %s:%d.\n", \
1075                       stack__.function[stack__.currentsize], stack__.file[stack__.currentsize], stack__.line[stack__.currentsize], func__, __FILE__, __LINE__); \
1076       stack__.function[stack__.currentsize]     = PETSC_NULLPTR; \
1077       stack__.file[stack__.currentsize]         = PETSC_NULLPTR; \
1078       stack__.line[stack__.currentsize]         = 0; \
1079       stack__.petscroutine[stack__.currentsize] = 0; \
1080     } \
1081     stack__.hotdepth = PetscMax(stack__.hotdepth - 1, 0); \
1082   } while (0)
1083 
1084 /*MC
1085    PetscStackPushNoCheck - Pushes a new function name and line number onto the PETSc default stack that tracks where the running program is
1086    currently in the source code.
1087 
1088    Not Collective
1089 
1090    Synopsis:
1091    #include <petscsys.h>
1092    void PetscStackPushNoCheck(char *funct,int petsc_routine,PetscBool hot);
1093 
1094    Input Parameters:
1095 +  funct - the function name
1096 .  petsc_routine - 2 user function, 1 PETSc function, 0 some other function
1097 -  hot - indicates that the function may be called often so expensive error checking should be turned off inside the function
1098 
1099    Level: developer
1100 
1101    Notes:
1102    In debug mode PETSc maintains a stack of the current function calls that can be used to help to quickly see where a problem has
1103    occurred, for example, when a signal is received without running in the debugger. It is recommended to use the debugger if extensive information is needed to
1104    help debug the problem.
1105 
1106    This version does not check the memory corruption (an expensive operation), use `PetscStackPush()` to check the memory.
1107 
1108    Use `PetscStackPushExternal()` for a function call that is about to be made to a non-PETSc or user function (such as BLAS etc).
1109 
1110    The default stack is a global variable called `petscstack`.
1111 
1112 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1113           `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPush()`, `PetscStackPop`,
1114           `PetscStackPushExternal()`
1115 M*/
1116 #define PetscStackPushNoCheck(funct, petsc_routine, hot) \
1117   do { \
1118     PetscStackSAWsTakeAccess(); \
1119     PetscStackPush_Private(petscstack, __FILE__, funct, __LINE__, petsc_routine, hot); \
1120     PetscStackSAWsGrantAccess(); \
1121   } while (0)
1122 
1123 /*MC
1124    PetscStackUpdateLine - in a function that has a `PetscFunctionBegin` or `PetscFunctionBeginUser` updates the stack line number to the
1125    current line number.
1126 
1127    Not Collective
1128 
1129    Synopsis:
1130    #include <petscsys.h>
1131    void PetscStackUpdateLine
1132 
1133    Level: developer
1134 
1135    Notes:
1136    Using `PetscCall()` and friends automatically handles this process
1137 
1138    In debug mode PETSc maintains a stack of the current function calls that can be used to help to quickly see where a problem has
1139    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1140    help debug the problem.
1141 
1142    The default stack is a global variable called petscstack.
1143 
1144    This is used by `PetscCall()` and is otherwise not like to be needed
1145 
1146 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPop`, `PetscCall()`
1147 M*/
1148 #define PetscStackUpdateLine \
1149   if (petscstack.currentsize > 0 && petscstack.function[petscstack.currentsize - 1] == PETSC_FUNCTION_NAME) { petscstack.line[petscstack.currentsize - 1] = __LINE__; }
1150 
1151 /*MC
1152    PetscStackPushExternal - Pushes a new function name onto the PETSc default stack that tracks where the running program is
1153    currently in the source code. Does not include the filename or line number since this is called by the calling routine
1154    for non-PETSc or user functions.
1155 
1156    Not Collective
1157 
1158    Synopsis:
1159    #include <petscsys.h>
1160    void PetscStackPushExternal(char *funct);
1161 
1162    Input Parameters:
1163 .  funct - the function name
1164 
1165    Level: developer
1166 
1167    Notes:
1168    Using `PetscCallExternal()` and friends automatically handles this process
1169 
1170    In debug mode PETSc maintains a stack of the current function calls that can be used to help to quickly see where a problem has
1171    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1172    help debug the problem.
1173 
1174    The default stack is a global variable called `petscstack`.
1175 
1176    This is to be used when calling an external package function such as a BLAS function.
1177 
1178    This also updates the stack line number for the current stack function.
1179 
1180 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1181           `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1182 M*/
1183 #define PetscStackPushExternal(funct) \
1184   do { \
1185     PetscStackUpdateLine; \
1186     PetscStackPushNoCheck(funct, 0, PETSC_TRUE); \
1187   } while (0);
1188 
1189 /*MC
1190    PetscStackPopNoCheck - Pops a function name from the PETSc default stack that tracks where the running program is
1191    currently in the source code.
1192 
1193    Not Collective
1194 
1195    Synopsis:
1196    #include <petscsys.h>
1197    void PetscStackPopNoCheck(char *funct);
1198 
1199    Input Parameter:
1200 .   funct - the function name
1201 
1202    Level: developer
1203 
1204    Notes:
1205    Using `PetscCall()`, `PetscCallExternal()`, `PetscCallBack()` and friends negates the need to call this
1206 
1207    In debug mode PETSc maintains a stack of the current function calls that can be used to help to quickly see where a problem has
1208    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1209    help debug the problem.
1210 
1211    The default stack is a global variable called petscstack.
1212 
1213    Developer Note:
1214    `PetscStackPopNoCheck()` takes a function argument while  `PetscStackPop` does not, this difference is likely just historical.
1215 
1216 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1217 M*/
1218 #define PetscStackPopNoCheck(funct) \
1219   do { \
1220     PetscStackSAWsTakeAccess(); \
1221     PetscStackPop_Private(petscstack, funct); \
1222     PetscStackSAWsGrantAccess(); \
1223   } while (0)
1224 
1225 #define PetscStackClearTop \
1226   do { \
1227     PetscStackSAWsTakeAccess(); \
1228     if (petscstack.currentsize > 0 && --petscstack.currentsize < PETSCSTACKSIZE) { \
1229       petscstack.function[petscstack.currentsize]     = PETSC_NULLPTR; \
1230       petscstack.file[petscstack.currentsize]         = PETSC_NULLPTR; \
1231       petscstack.line[petscstack.currentsize]         = 0; \
1232       petscstack.petscroutine[petscstack.currentsize] = 0; \
1233     } \
1234     petscstack.hotdepth = PetscMax(petscstack.hotdepth - 1, 0); \
1235     PetscStackSAWsGrantAccess(); \
1236   } while (0)
1237 
1238 /*MC
1239    PetscFunctionBegin - First executable line of each PETSc function,  used for error handling. Final
1240       line of PETSc functions should be `PetscFunctionReturn`(0);
1241 
1242    Synopsis:
1243    #include <petscsys.h>
1244    void PetscFunctionBegin;
1245 
1246    Not Collective
1247 
1248    Usage:
1249 .vb
1250      int something;
1251 
1252      PetscFunctionBegin;
1253 .ve
1254 
1255    Notes:
1256      Use `PetscFunctionBeginUser` for application codes.
1257 
1258      Not available in Fortran
1259 
1260    Level: developer
1261 
1262 .seealso: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`
1263 
1264 M*/
1265 #define PetscFunctionBegin \
1266   do { \
1267     PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 1, PETSC_FALSE); \
1268     PetscRegister__FUNCT__(); \
1269   } while (0)
1270 
1271 /*MC
1272    PetscFunctionBeginHot - Substitute for `PetscFunctionBegin` to be used in functions that are called in
1273    performance-critical circumstances.  Use of this function allows for lighter profiling by default.
1274 
1275    Synopsis:
1276    #include <petscsys.h>
1277    void PetscFunctionBeginHot;
1278 
1279    Not Collective
1280 
1281    Usage:
1282 .vb
1283      int something;
1284 
1285      PetscFunctionBeginHot;
1286 .ve
1287 
1288    Notes:
1289      Not available in Fortran
1290 
1291    Level: developer
1292 
1293 .seealso: `PetscFunctionBegin`, `PetscFunctionReturn()`, `PetscStackPushNoCheck()`
1294 
1295 M*/
1296 #define PetscFunctionBeginHot \
1297   do { \
1298     PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 1, PETSC_TRUE); \
1299     PetscRegister__FUNCT__(); \
1300   } while (0)
1301 
1302 /*MC
1303    PetscFunctionBeginUser - First executable line of user provided routines
1304 
1305    Synopsis:
1306    #include <petscsys.h>
1307    void PetscFunctionBeginUser;
1308 
1309    Not Collective
1310 
1311    Usage:
1312 .vb
1313      int something;
1314 
1315      PetscFunctionBeginUser;
1316 .ve
1317 
1318    Notes:
1319       Functions that incorporate this must call `PetscFunctionReturn()` instead of return except for main().
1320 
1321       May be used before `PetscInitialize()`
1322 
1323       Not available in Fortran
1324 
1325       This is identical to `PetscFunctionBegin` except it labels the routine as a user
1326       routine instead of as a PETSc library routine.
1327 
1328    Level: intermediate
1329 
1330 .seealso: `PetscFunctionReturn()`, `PetscFunctionBegin`, `PetscFunctionBeginHot`, `PetscStackPushNoCheck()`
1331 
1332 M*/
1333 #define PetscFunctionBeginUser \
1334   do { \
1335     PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 2, PETSC_FALSE); \
1336     PetscRegister__FUNCT__(); \
1337   } while (0)
1338 
1339 /*MC
1340    PetscStackPush - Pushes a new function name and line number onto the PETSc default stack that tracks where the running program is
1341    currently in the source code and verifies the memory is not corrupted.
1342 
1343    Not Collective
1344 
1345    Synopsis:
1346    #include <petscsys.h>
1347    void PetscStackPush(char *funct)
1348 
1349    Input Parameter:
1350 .  funct - the function name
1351 
1352    Level: developer
1353 
1354    Notes:
1355    In debug mode PETSc maintains a stack of the current function calls that can be used to help to quickly see where a problem has
1356    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1357    help debug the problem.
1358 
1359    The default stack is a global variable called petscstack.
1360 
1361 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1362           `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1363 M*/
1364 #define PetscStackPush(n) \
1365   do { \
1366     PetscStackPushNoCheck(n, 0, PETSC_FALSE); \
1367     CHKMEMQ; \
1368   } while (0)
1369 
1370 /*MC
1371    PetscStackPop - Pops a function name from the PETSc default stack that tracks where the running program is
1372    currently in the source code and verifies the memory is not corrupted.
1373 
1374    Not Collective
1375 
1376    Synopsis:
1377    #include <petscsys.h>
1378    void PetscStackPop
1379 
1380    Level: developer
1381 
1382    Notes:
1383    In debug mode PETSc maintains a stack of the current function calls that can be used to help to quickly see where a problem has
1384    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1385    help debug the problem.
1386 
1387    The default stack is a global variable called petscstack.
1388 
1389 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPopNoCheck()`, `PetscStackPush()`
1390 M*/
1391 #define PetscStackPop \
1392   do { \
1393     CHKMEMQ; \
1394     PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1395   } while (0)
1396 
1397 /*MC
1398    PetscFunctionReturn - Last executable line of each PETSc function
1399         used for error handling. Replaces `return()`
1400 
1401    Synopsis:
1402    #include <petscsys.h>
1403    void PetscFunctionReturn(0);
1404 
1405    Not Collective
1406 
1407    Usage:
1408 .vb
1409     ....
1410      PetscFunctionReturn(0);
1411    }
1412 .ve
1413 
1414    Note:
1415      Not available in Fortran
1416 
1417    Level: developer
1418 
1419 .seealso: `PetscFunctionBegin()`, `PetscStackPopNoCheck()`
1420 
1421 M*/
1422 #define PetscFunctionReturn(a) \
1423   do { \
1424     PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1425     return a; \
1426   } while (0)
1427 
1428 #define PetscFunctionReturnVoid() \
1429   do { \
1430     PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1431     return; \
1432   } while (0)
1433 #else /* PETSC_USE_DEBUG */
1434 #define PetscStackPushNoCheck(funct, petsc_routine, hot)
1435 #define PetscStackUpdateLine
1436 #define PetscStackPushExternal(funct)
1437 #define PetscStackPopNoCheck
1438 #define PetscStackClearTop
1439 #define PetscFunctionBegin
1440 #define PetscFunctionBeginUser
1441 #define PetscFunctionBeginHot
1442 #define PetscFunctionReturn(a)    return a
1443 #define PetscFunctionReturnVoid() return
1444 #define PetscStackPop             CHKMEMQ
1445 #define PetscStackPush(f)         CHKMEMQ
1446 #endif /* PETSC_USE_DEBUG */
1447 
1448 #if defined(PETSC_CLANG_STATIC_ANALYZER)
1449 #define PetscStackCallExternalVoid(name, routine)
1450 #define PetscCallExternal(func, ...)
1451 #else
1452 /*MC
1453     PetscStackCallExternalVoid - Calls an external library routine or user function after pushing the name of the routine on the stack.
1454 
1455    Input Parameters:
1456 +   name - string that gives the name of the function being called
1457 -   routine - actual call to the routine, for example, functionname(a,b)
1458 
1459    Level: developer
1460 
1461    Note:
1462    Often one should use `PetscCallExternal()` instead. This routine is intended for external library routines that DO NOT return error codes
1463 
1464    In debug mode this also checks the memory for corruption at the end of the function call.
1465 
1466    Certain external packages, such as BLAS/LAPACK may have their own macros for managing the call, error checking, etc.
1467 
1468    Developer Note:
1469    This is so that when a user or external library routine results in a crash or corrupts memory, they get blamed instead of PETSc.
1470 
1471 .seealso: `PetscCall()`, `PetscStackPushNoCheck()`, `PetscStackPush()`, `PetscCallExternal()`, `PetscCallBLAS()`
1472 @*/
1473 #define PetscStackCallExternalVoid(name, routine) \
1474   do { \
1475     PetscStackPush(name); \
1476     routine; \
1477     PetscStackPop; \
1478   } while (0)
1479 
1480 /*MC
1481     PetscCallExternal - Calls an external library routine that returns an error code after pushing the name of the routine on the stack.
1482 
1483    Input Parameters:
1484 +   func-  name of the routine
1485 -   args - arguments to the routine
1486 
1487    Level: developer
1488 
1489    Notes:
1490    This is intended for external package routines that return error codes. Use `PetscStackCallExternalVoid()` for those that do not.
1491 
1492    In debug mode this also checks the memory for corruption at the end of the function call.
1493 
1494    Assumes the error return code of the function is an integer and that a value of 0 indicates success
1495 
1496    Developer Note:
1497    This is so that when an external packge routine results in a crash or corrupts memory, they get blamed instead of PETSc.
1498 
1499 .seealso: `PetscCall()`, `PetscStackPushNoCheck()`, `PetscStackPush()`, `PetscStackCallExternalVoid()`
1500 M*/
1501 #define PetscCallExternal(func, ...) \
1502   do { \
1503     PetscStackPush(PetscStringize(func)); \
1504     PetscErrorCode __ierr = func(__VA_ARGS__); \
1505     PetscStackPop; \
1506     PetscCheck(!__ierr, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in %s(): error code %d", PetscStringize(func), __ierr); \
1507   } while (0)
1508 #endif /* PETSC_CLANG_STATIC_ANALYZER */
1509 
1510 #endif
1511