xref: /petsc/include/petscerror.h (revision bfe80ac4a46d58cb7760074b25f5e81b2f541d8a)
1 /*
2     Contains all error handling interfaces for PETSc.
3 */
4 #pragma once
5 
6 #include <petscmacros.h>
7 #include <petscsystypes.h>
8 
9 #if defined(__cplusplus)
10   #include <exception> // std::exception
11 #endif
12 
13 /* SUBMANSEC = Sys */
14 
15 #define SETERRQ1(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
16 #define SETERRQ2(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
17 #define SETERRQ3(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
18 #define SETERRQ4(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
19 #define SETERRQ5(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
20 #define SETERRQ6(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
21 #define SETERRQ7(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
22 #define SETERRQ8(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
23 #define SETERRQ9(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
24 
25 /*MC
26    SETERRQ - Macro to be called when an error has been detected,
27 
28    Synopsis:
29    #include <petscsys.h>
30    PetscErrorCode SETERRQ(MPI_Comm comm, PetscErrorCode ierr, char *message, ...)
31 
32    Collective
33 
34    Input Parameters:
35 +  comm    - An MPI communicator, use `PETSC_COMM_SELF` unless you know all ranks of another communicator will detect the error
36 .  ierr    - nonzero error code, see the list of standard error codes in include/petscerror.h
37 -  message - error message
38 
39   Level: beginner
40 
41    Notes:
42    This is rarely needed, one should use `PetscCheck()` and `PetscCall()` and friends to automatically handle error conditions.
43    Once the error handler is called the calling function is then returned from with the given error code.
44 
45    Experienced users can set the error handler with `PetscPushErrorHandler()`.
46 
47    Fortran Note:
48    `SETERRQ()` may be called from Fortran subroutines but `SETERRA()` must be called from the
49    Fortran main program.
50 
51 .seealso: `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
52           `PetscError()`, `PetscCall()`, `CHKMEMQ`, `CHKERRA()`, `PetscCallMPI()`, `PetscErrorCode`
53 M*/
54 #define SETERRQ(comm, ierr, ...) \
55   do { \
56     PetscErrorCode ierr_seterrq_petsc_ = PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__); \
57     return ierr_seterrq_petsc_ ? ierr_seterrq_petsc_ : PETSC_ERR_RETURN; \
58   } while (0)
59 
60 #define SETERRQNULL(comm, ierr, ...) \
61   do { \
62     (void)PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__); \
63     return NULL; \
64   } while (0)
65 
66 /*
67     Returned from PETSc functions that are called from MPI, such as related to attributes
68       Do not confuse PETSC_MPI_ERROR_CODE and PETSC_ERR_MPI, the first is registered with MPI and returned to MPI as
69       an error code, the latter is a regular PETSc error code passed within PETSc code indicating an error was detected in an MPI call.
70 */
71 PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CLASS;
72 PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CODE;
73 
74 /*MC
75    SETERRMPI - Macro to be called when an error has been detected within an MPI callback function
76 
77    No Fortran Support
78 
79    Synopsis:
80    #include <petscsys.h>
81    PetscErrorCode SETERRMPI(MPI_Comm comm, PetscErrorCode ierr, char *message, ...)
82 
83    Collective
84 
85    Input Parameters:
86 +  comm    - An MPI communicator, use `PETSC_COMM_SELF` unless you know all ranks of another communicator will detect the error
87 .  ierr    - nonzero error code, see the list of standard error codes in include/petscerror.h
88 -  message - error message
89 
90   Level: developer
91 
92    Note:
93    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`
94   which is registered with `MPI_Add_error_code()` when PETSc is initialized.
95 
96 .seealso: `SETERRQ()`, `PetscCall()`, `PetscCallMPI()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `PetscErrorCode`
97 M*/
98 #define SETERRMPI(comm, ierr, ...) return ((void)PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__), PETSC_MPI_ERROR_CODE)
99 
100 /*MC
101    SETERRA - Fortran-only macro that can be called when an error has been detected from the main program
102 
103    Synopsis:
104    #include <petscsys.h>
105    PetscErrorCode SETERRA(MPI_Comm comm, PetscErrorCode ierr, char *message)
106 
107    Collective
108 
109    Input Parameters:
110 +  comm    - An MPI communicator, so that the error can be collective
111 .  ierr    - nonzero error code, see the list of standard error codes in include/petscerror.h
112 -  message - error message in the `printf()` format
113 
114    Level: beginner
115 
116    Notes:
117    This should only be used with Fortran. With C/C++, use `SETERRQ()`.
118 
119    `SETERRQ()` may be called from Fortran subroutines but `SETERRA()` must be called from the
120     Fortran main program.
121 
122 .seealso: `SETERRQ()`, `SETERRABORT()`, `PetscCall()`, `CHKERRA()`, `PetscCallAbort()`, `PetscErrorCode`
123 M*/
124 
125 /*MC
126    SETERRABORT - Macro that can be called when an error has been detected,
127 
128    Synopsis:
129    #include <petscsys.h>
130    PetscErrorCode SETERRABORT(MPI_Comm comm, PetscErrorCode ierr, char *message, ...)
131 
132    Collective
133 
134    Input Parameters:
135 +  comm    - An MPI communicator, so that the error can be collective
136 .  ierr    - nonzero error code, see the list of standard error codes in include/petscerror.h
137 -  message - error message in the `printf()` format
138 
139    Level: beginner
140 
141    Notes:
142    This function just calls `MPI_Abort()`.
143 
144    This should only be called in routines that cannot return an error code, such as in C++ constructors.
145 
146    Fortran Note:
147    Use `SETERRA()` in Fortran main program and `SETERRQ()` in Fortran subroutines
148 
149    Developer Note:
150    In Fortran `SETERRA()` could be called `SETERRABORT()` since they serve the same purpose
151 
152 .seealso: `SETERRQ()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `PetscCall()`, `CHKMEMQ`, `PetscErrorCode`
153 M*/
154 #define SETERRABORT(comm, ierr, ...) \
155   do { \
156     (void)PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__); \
157     (void)MPI_Abort(comm, ierr); \
158   } while (0)
159 
160 /*MC
161   PetscCheck - Checks that a particular condition is true; if not true, then returns the provided error code
162 
163   Synopsis:
164   #include <petscerror.h>
165   void PetscCheck(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
166 
167   Collective; No Fortran Support
168 
169   Input Parameters:
170 + cond    - The boolean condition
171 . comm    - The communicator on which the check can be collective on
172 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
173 - message - Error message in the `printf()` format
174 
175   Level: beginner
176 
177   Notes:
178   Enabled in both optimized and debug builds.
179 
180   As a general rule, `PetscCheck()` is used to check "usage error" (for example, passing an incorrect value as a function argument),
181   `PetscAssert()` is used to "check for bugs in PETSc" (for example, is a value in a PETSc data structure nonsensical).
182   However, for functions that are called in a "hot spot", for example, thousands of times in a loop, `PetscAssert()` should be used instead
183   of `PetscCheck()` since the former is compiled out in PETSc's optimization code.
184 
185   Calls `SETERRQ()` if the assertion fails, so can only be called from functions returning a
186   `PetscErrorCode` (or equivalent type after conversion).
187 
188 .seealso: `PetscAssert()`, `PetscCheckReturnMPI()`, `SETERRQ()`, `PetscError()`, `PetscCall()`, `PetscCheckAbort()`, `PetscErrorCode`
189 M*/
190 #define PetscCheck(cond, comm, ierr, ...) \
191   do { \
192     if (PetscUnlikely(!(cond))) SETERRQ(comm, ierr, __VA_ARGS__); \
193   } while (0)
194 
195 /*MC
196   PetscCheckReturnMPI - Checks that a particular condition is true; if not true, then returns an MPI error code.
197   To check for errors in PETSc-provided MPI callbacks.
198 
199   Synopsis:
200   #include <petscerror.h>
201   void PetscCheckReturnMPI(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
202 
203   Collective; No Fortran Support
204 
205   Input Parameters:
206 + cond    - The boolean condition
207 . comm    - The communicator on which the check can be collective on
208 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
209 - message - Error message in the `printf()` format
210 
211   Level: beginner
212 
213   Note:
214   Enabled in both optimized and debug builds.
215 
216 .seealso: `PetscCheck()`, `PetscAssert()`, `SETERRQ()`, `PetscError()`, `PetscCall()`, `PetscCheckAbort()`, `PetscErrorCode`
217 M*/
218 #define PetscCheckReturnMPI(cond, comm, ierr, ...) \
219   do { \
220     if (PetscUnlikely(!(cond))) SETERRMPI(comm, ierr, __VA_ARGS__); \
221   } while (0)
222 
223 /*MC
224   PetscCheckAbort - Check that a particular condition is true, otherwise prints error and aborts
225 
226   Synopsis:
227   #include <petscerror.h>
228   void PetscCheckAbort(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
229 
230   Collective; No Fortran Support
231 
232   Input Parameters:
233 + cond    - The boolean condition
234 . comm    - The communicator on which the check can be collective on
235 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
236 - message - Error message in the `printf()` format
237 
238   Level: developer
239 
240   Notes:
241   Enabled in both optimized and debug builds.
242 
243   Calls `SETERRABORT()` if the assertion fails, can be called from a function that does not return an
244   error code, such as a C++ constructor. usually `PetscCheck()` should be used.
245 
246 .seealso: `PetscAssertAbort()`, `PetscAssert()`, `SETERRQ()`, `PetscError()`, `PetscCall()`, `PetscCheck()`, `SETERRABORT()`, `PetscErrorCode`
247 M*/
248 #define PetscCheckAbort(cond, comm, ierr, ...) \
249   do { \
250     if (PetscUnlikely(!(cond))) SETERRABORT(comm, ierr, __VA_ARGS__); \
251   } while (0)
252 
253 /*MC
254   PetscAssert - Assert that a particular condition is true
255 
256   Synopsis:
257   #include <petscerror.h>
258   void PetscAssert(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
259 
260   Collective; No Fortran Support
261 
262   Input Parameters:
263 + cond    - The boolean condition
264 . comm    - The communicator on which the check can be collective on
265 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
266 - message - Error message in `printf()` format
267 
268   Level: beginner
269 
270   Notes:
271   Equivalent to `PetscCheck()` if debugging is enabled, and `PetscAssume(cond)` otherwise.
272 
273   See `PetscCheck()` for usage and behaviour.
274 
275   This is needed instead of simply using `assert()` because this correctly handles the collective nature of errors under MPI
276 
277 .seealso: `PetscCheck()`, `SETERRQ()`, `PetscError()`, `PetscAssertAbort()`, `PetscErrorCode`
278 M*/
279 #if PetscDefined(USE_DEBUG)
280   #define PetscAssert(cond, comm, ierr, ...) PetscCheck(cond, comm, ierr, __VA_ARGS__)
281 #else
282   #define PetscAssert(cond, ...) PetscAssume(cond)
283 #endif
284 
285 /*MC
286   PetscAssertAbort - Assert that a particular condition is true, otherwise prints error and aborts
287 
288   Synopsis:
289   #include <petscerror.h>
290   void PetscAssertAbort(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
291 
292   Collective; No Fortran Support
293 
294   Input Parameters:
295 + cond    - The boolean condition
296 . comm    - The communicator on which the check can be collective on
297 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
298 - message - Error message in the `printf()` format
299 
300   Level: beginner
301 
302   Note:
303   Enabled only in debug builds. See `PetscCheckAbort()` for usage.
304 
305 .seealso: `PetscCheckAbort()`, `PetscAssert()`, `PetscCheck()`, `SETERRABORT()`, `PetscError()`
306 M*/
307 #if PetscDefined(USE_DEBUG)
308   #define PetscAssertAbort(cond, comm, ierr, ...) PetscCheckAbort(cond, comm, ierr, __VA_ARGS__)
309 #else
310   #define PetscAssertAbort(cond, comm, ierr, ...) PetscAssume(cond)
311 #endif
312 
313 /*MC
314   PetscCall - Calls a PETSc function and then checks the resulting error code, if it is
315   non-zero it calls the error handler and returns from the current function with the error
316   code.
317 
318   Synopsis:
319   #include <petscerror.h>
320   void PetscCall(PetscFunction(args))
321 
322   Not Collective
323 
324   Input Parameter:
325 . PetscFunction - any PETSc function that returns an error code
326 
327   Level: beginner
328 
329   Notes:
330   Once the error handler is called the calling function is then returned from with the given
331   error code. Experienced users can set the error handler with `PetscPushErrorHandler()`.
332 
333   `PetscCall()` cannot be used in functions returning a datatype not convertible to
334   `PetscErrorCode`. For example, `PetscCall()` may not be used in functions returning `void`, use
335   `PetscCallAbort()` or `PetscCallVoid()` in this case.
336 
337   Example Usage:
338 .vb
339   PetscCall(PetscInitiailize(...)); // OK to call even when PETSc is not yet initialized!
340 
341   struct my_struct
342   {
343     void *data;
344   } my_complex_type;
345 
346   struct my_struct bar(void)
347   {
348     PetscCall(foo(15)); // ERROR PetscErrorCode not convertible to struct my_struct!
349   }
350 
351   PetscCall(bar()) // ERROR input not convertible to PetscErrorCode
352 .ve
353 
354   It is also possible to call this directly on a `PetscErrorCode` variable
355 .vb
356   PetscCall(ierr);  // check if ierr is nonzero
357 .ve
358 
359   Should not be used to call callback functions provided by users, `PetscCallBack()` should be used in that situation.
360 
361   `PetscUseTypeMethod()` or `PetscTryTypeMethod()` should be used when calling functions pointers contained in a PETSc object's `ops` array
362 
363   Fortran Notes:
364     The Fortran function in which this is used must declare a `PetscErrorCode` variable necessarily named `ierr`, and `ierr` must be
365     the final argument to the PETSc function being called.
366 
367     In the main program and in Fortran subroutines that do not have `ierr` as the final return parameter, one
368     should use `PetscCallA()`
369 
370   Example Fortran Usage:
371 .vb
372   PetscErrorCode ierr
373   Vec v
374 
375   ...
376   PetscCall(VecShift(v, 1.0, ierr))
377   PetscCallA(VecShift(v, 1.0, ierr))
378 .ve
379 
380 .seealso: `SETERRQ()`, `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`,
381           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`,
382           `CHKERRMPI()`, `PetscCallBack()`, `PetscCallAbort()`, `PetscCallVoid()`, `PetscCallNull()`
383 M*/
384 
385 /*MC
386   PetscCallNull - Calls a PETSc function and then checks the resulting error code, if it is
387   non-zero it calls the error handler and returns a `NULL`
388 
389   Synopsis:
390   #include <petscerror.h>
391   void PetscCallNull(PetscFunction(args))
392 
393   Not Collective; No Fortran Support
394 
395   Input Parameter:
396 . PetscFunction - any PETSc function that returns something that can be returned as a `NULL`
397 
398   Level: developer
399 
400 .seealso: `PetscCall()`, `SETERRQ()`, `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`,
401           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`,
402           `CHKERRMPI()`, `PetscCallBack()`, `PetscCallAbort()`, `PetscCallVoid()`, `PetscCall()`
403 M*/
404 
405 /*MC
406    PetscCallA - Fortran-only macro that should be used in the main program and subroutines that do not have `ierr` as the final return parameter, to call PETSc functions instead of using
407    `PetscCall()` which should be used in other Fortran subroutines
408 
409    Synopsis:
410    #include <petscsys.h>
411    PetscErrorCode PetscCallA(PetscFunction(arguments, ierr))
412 
413    Collective
414 
415    Input Parameter:
416 .  PetscFunction(arguments,ierr) - the call to the function
417 
418   Level: beginner
419 
420    Notes:
421    This should only be used with Fortran. With C/C++, use `PetscCall()` always.
422 
423    The Fortran function in which this is used must declare a `PetscErrorCode` variable necessarily named `ierr`
424    Use `SETERRA()` to set an error in a Fortran main program and `SETERRQ()` in Fortran subroutines
425 
426 .seealso: `SETERRQ()`, `SETERRA()`, `SETERRABORT()`, `PetscCall()`, `CHKERRA()`, `PetscCallAbort()`
427 M*/
428 
429 /*MC
430   PetscCallBack - Calls a user provided PETSc callback function and then checks the resulting error code, if it is non-zero it calls the error
431   handler and returns from the current function with the error code.
432 
433   Synopsis:
434   #include <petscerror.h>
435   void PetscCallBack(const char *functionname, PetscFunction(args))
436 
437   Not Collective; No Fortran Support
438 
439   Input Parameters:
440 + functionname - the name of the function being called, this can be a string with spaces that describes the meaning of the callback
441 - PetscFunction - user provided callback function that returns an error code
442 
443   Example Usage:
444 .vb
445   PetscCallBack("XXX callback to do something", a->callback(...));
446 .ve
447 
448   Level: developer
449 
450   Notes:
451   `PetscUseTypeMethod()` and ` PetscTryTypeMethod()` are the preferred API for this functionality. But when the callback functions are associated with a
452   `DMSNES` or `DMTS` this API must be used.
453 
454   Once the error handler is called the calling function is then returned from with the given
455   error code. Experienced users can set the error handler with `PetscPushErrorHandler()`.
456 
457   `PetscCallBack()` should only be called in PETSc when a call is being made to a user provided call-back routine.
458 
459   Developer Note:
460   It would be good to provide a new API for when the callbacks are associated with `DMSNES` or `DMTS` so this routine could be used less
461 
462 .seealso: `SETERRQ()`, `PetscCheck()`, `PetscCall()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`
463           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`, `CHKERRMPI()`, `PetscCall()`,  `PetscUseTypeMethod()`, `PetscTryTypeMethod()`
464 M*/
465 
466 /*MC
467   PetscCallVoid - Like `PetscCall()` but for use in functions that return `void`
468 
469   Synopsis:
470   #include <petscerror.h>
471   void PetscCallVoid(PetscFunction(args))
472 
473   Not Collective; No Fortran Support
474 
475   Input Parameter:
476 . PetscFunction - any PETSc function that returns an error code
477 
478   Example Usage:
479 .vb
480   void foo()
481   {
482     KSP ksp;
483 
484     PetscFunctionBeginUser;
485     // OK, properly handles PETSc error codes
486     PetscCallVoid(KSPCreate(PETSC_COMM_WORLD, &ksp));
487     PetscFunctionReturnVoid();
488   }
489 
490   PetscErrorCode bar()
491   {
492     KSP ksp;
493 
494     PetscFunctionBeginUser;
495     // ERROR, Non-void function 'bar' should return a value
496     PetscCallVoid(KSPCreate(PETSC_COMM_WORLD, &ksp));
497     // OK, returning PetscErrorCode
498     PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
499     PetscFunctionReturn(PETSC_SUCCESS);
500   }
501 .ve
502 
503   Level: beginner
504 
505   Notes:
506   Has identical usage to `PetscCall()`, except that it returns `void` on error instead of a
507   `PetscErrorCode`. See `PetscCall()` for more detailed discussion.
508 
509   Note that users should prefer `PetscCallAbort()` to this routine. While this routine does
510   "handle" errors by returning from the enclosing function, it effectively gobbles the
511   error. Since the enclosing function itself returns `void`, its callers have no way of knowing
512   that the routine returned early due to an error. `PetscCallAbort()` at least ensures that the
513   program crashes gracefully.
514 
515 .seealso: `PetscCall()`, `PetscErrorCode`, `PetscCallAbort()`, `PetscCallNull()`
516 M*/
517 
518 /*MC
519   PetscCallReturnMPI - Calls a PETSc function and then checks the resulting error code, if it is
520   non-zero it calls the error handler and returns from the current function with an MPI error code.
521   To check for errors in PETSc provided MPI callbacks.
522 
523   Synopsis:
524   #include <petscerror.h>
525   void PetscCallReturnMPI(PetscFunction(args))
526 
527   Not Collective
528 
529   Input Parameter:
530 . PetscFunction - any PETSc function that returns an error code
531 
532   Level: advanced
533 
534   Notes:
535   Note to be confused with `PetscCallMPI()`.
536 
537   This is be used in a PETSc-provided MPI callback function, such as `MPI_Comm_delete_attr_function function()`.
538 
539   Currently, it always returns `MPI_ERR_OTHER` on failure
540 
541 .seealso: `PetscCall()`, `PetscCallMPI()`, `SETERRQ()`, `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`,
542           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`,
543           `CHKERRMPI()`, `PetscCallBack()`, `PetscCallAbort()`, `PetscCallVoid()`, `PetscCallNull()`
544 M*/
545 
546 #if defined(PETSC_CLANG_STATIC_ANALYZER)
547 void PetscCall(PetscErrorCode);
548 void PetscCallBack(const char *, PetscErrorCode);
549 void PetscCallVoid(PetscErrorCode);
550 void PetscCallNull(PetscErrorCode);
551 void PetscCallReturnMPI(PetscErrorCode);
552 #else
553   #define PetscCall(...) \
554     do { \
555       PetscErrorCode ierr_petsc_call_q_; \
556       PetscStackUpdateLine; \
557       ierr_petsc_call_q_ = __VA_ARGS__; \
558       if (PetscUnlikely(ierr_petsc_call_q_ != PETSC_SUCCESS)) return PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_q_, PETSC_ERROR_REPEAT, " "); \
559     } while (0)
560   #define PetscCallNull(...) \
561     do { \
562       PetscErrorCode ierr_petsc_call_q_; \
563       PetscStackUpdateLine; \
564       ierr_petsc_call_q_ = __VA_ARGS__; \
565       if (PetscUnlikely(ierr_petsc_call_q_ != PETSC_SUCCESS)) { \
566         (void)PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " "); \
567         PetscFunctionReturn(NULL); \
568       } \
569     } while (0)
570   #define PetscCallBack(function, ...) \
571     do { \
572       PetscErrorCode ierr_petsc_call_q_; \
573       PetscStackUpdateLine; \
574       PetscStackPushExternal(function); \
575       ierr_petsc_call_q_ = __VA_ARGS__; \
576       PetscStackPop; \
577       if (PetscUnlikely(ierr_petsc_call_q_ != PETSC_SUCCESS)) return PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_q_, PETSC_ERROR_REPEAT, " "); \
578     } while (0)
579   #define PetscCallVoid(...) \
580     do { \
581       PetscErrorCode ierr_petsc_call_void_; \
582       PetscStackUpdateLine; \
583       ierr_petsc_call_void_ = __VA_ARGS__; \
584       if (PetscUnlikely(ierr_petsc_call_void_ != PETSC_SUCCESS)) { \
585         (void)PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_void_, PETSC_ERROR_REPEAT, " "); \
586         return; \
587       } \
588     } while (0)
589   #define PetscCallReturnMPI(...) \
590     do { \
591       PetscErrorCode ierr_petsc_call_q_; \
592       PetscStackUpdateLine; \
593       ierr_petsc_call_q_ = __VA_ARGS__; \
594       if (PetscUnlikely(ierr_petsc_call_q_ != PETSC_SUCCESS)) { \
595         (void)PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_q_, PETSC_ERROR_REPEAT, " "); \
596         return MPI_ERR_OTHER; \
597       } \
598     } while (0)
599 #endif
600 
601 /*MC
602   CHKERRQ - Checks error code returned from PETSc function
603 
604   Synopsis:
605   #include <petscsys.h>
606   void CHKERRQ(PetscErrorCode ierr)
607 
608   Not Collective
609 
610   Input Parameter:
611 . ierr - nonzero error code
612 
613   Level: deprecated
614 
615   Note:
616   Deprecated in favor of `PetscCall()`. This routine behaves identically to it.
617 
618 .seealso: `PetscCall()`
619 M*/
620 #define CHKERRQ(...) PetscCall(__VA_ARGS__)
621 #define CHKERRV(...) PetscCallVoid(__VA_ARGS__)
622 
623 PETSC_EXTERN void PetscMPIErrorString(PetscMPIInt, size_t, char *);
624 
625 /*MC
626   PetscCallMPI - Checks error code returned from MPI calls, if non-zero it calls the error
627   handler and then returns a `PetscErrorCode`
628 
629   Synopsis:
630   #include <petscerror.h>
631   void PetscCallMPI(MPI_Function(args))
632 
633   Not Collective
634 
635   Input Parameter:
636 . MPI_Function - an MPI function that returns an MPI error code
637 
638   Level: beginner
639 
640   Notes:
641   Always returns the error code `PETSC_ERR_MPI`; the MPI error code and string are embedded in
642   the string error message. Do not use this to call any other routines (for example PETSc
643   routines), it should only be used for direct MPI calls. The user may configure PETSc with the
644   `--with-strict-petscerrorcode` option to check this at compile-time, otherwise they must
645   check this themselves.
646 
647   This routine can only be used in functions returning `PetscErrorCode` themselves. If the
648   calling function returns a different type, use `PetscCallMPIAbort()` instead.
649 
650   Example Usage:
651 .vb
652   PetscCallMPI(MPI_Comm_size(...)); // OK, calling MPI function
653 
654   PetscCallMPI(PetscFunction(...)); // ERROR, use PetscCall() instead!
655 .ve
656 
657   Fortran Notes:
658     The Fortran function from which this is used must declare a variable `PetscErrorCode` ierr and ierr must be
659     the final argument to the MPI function being called.
660 
661     In the main program and in Fortran subroutines that do not have ierr as the final return parameter one
662     should use `PetscCallMPIA()`
663 
664   Fortran Usage:
665 .vb
666   PetscErrorCode ierr or integer ierr
667   ...
668   PetscCallMPI(MPI_Comm_size(...,ierr))
669   PetscCallMPIA(MPI_Comm_size(...,ierr)) ! Will abort after calling error handler
670 
671   PetscCallMPI(MPI_Comm_size(...,eflag)) ! ERROR, final argument must be ierr
672 .ve
673 
674 .seealso: `SETERRMPI()`, `PetscCall()`, `PetscCallReturnMPI()`, `SETERRQ()`, `SETERRABORT()`, `PetscCallAbort()`,
675           `PetscCallMPIAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
676           `PetscError()`, `CHKMEMQ`, `PetscCallMPINull()`
677 M*/
678 
679 /*MC
680   PetscCallMPIReturnMPI - Checks error code returned from MPI calls, if non-zero it calls the error
681   handler and then returns an MPI error code. To check for errors in PETSc-provided MPI callbacks.
682 
683   Synopsis:
684   #include <petscerror.h>
685   void PetscCallMPIReturnMPI(MPI_Function(args))
686 
687   Not Collective
688 
689   Input Parameter:
690 . MPI_Function - an MPI function that returns an MPI error code
691 
692   Level: advanced
693 
694 .seealso: `SETERRMPI()`, `PetscCall()`, `PetscCallMPI()`, `PetscCallReturnMPI()`, `SETERRQ()`, `SETERRABORT()`, `PetscCallAbort()`,
695           `PetscCallMPIAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
696           `PetscError()`, `CHKMEMQ`, `PetscCallMPINull()`
697 M*/
698 
699 /*MC
700   PetscCallMPINull - Checks error code returned from MPI calls, if non-zero it calls the error
701   handler and then returns a `NULL`
702 
703   Synopsis:
704   #include <petscerror.h>
705   void PetscCallMPINull(MPI_Function(args))
706 
707   Not Collective; No Fortran Support
708 
709   Input Parameter:
710 . MPI_Function - an MPI function that returns an MPI error code
711 
712   Level: beginner
713 
714   Notes:
715   Always passes the error code `PETSC_ERR_MPI` to the error handler `PetscError()`; the MPI error code and string are embedded in
716   the string error message. Do not use this to call any other routines (for example PETSc
717   routines), it should only be used for direct MPI calls.
718 
719   This routine can only be used in functions returning anything that can be returned as a `NULL` themselves. If the
720   calling function returns a different type, use `PetscCallMPIAbort()` instead.
721 
722   Example Usage:
723 .vb
724   PetscCallMPINull(MPI_Comm_size(...)); // OK, calling MPI function
725 
726   PetscCallMPI(PetscFunction(...)); // ERROR, use PetscCall() instead!
727 .ve
728 
729 .seealso: `SETERRMPI()`, `PetscCall()`, `SETERRQ()`, `SETERRABORT()`, `PetscCallAbort()`,
730           `PetscCallMPIAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
731           `PetscError()`, `CHKMEMQ`, `PetscCallMPI()`
732 M*/
733 
734 /*MC
735   PetscCallMPIAbort - Like `PetscCallMPI()` but calls `MPI_Abort()` on error
736 
737   Synopsis:
738   #include <petscerror.h>
739   void PetscCallMPIAbort(MPI_Comm comm, MPI_Function(args))
740 
741   Not Collective
742 
743   Input Parameters:
744 + comm         - the MPI communicator to abort on
745 - MPI_Function - an MPI function that returns an MPI error code
746 
747   Level: beginner
748 
749   Notes:
750   Usage is identical to `PetscCallMPI()`. See `PetscCallMPI()` for detailed discussion.
751 
752   This routine may be used in functions returning `void` or other non-`PetscErrorCode` types.
753 
754   Fortran Note:
755   In Fortran this is called `PetscCallMPIA()` and is intended to be used in the main program while `PetscCallMPI()` is
756   used in Fortran subroutines.
757 
758   Developer Note:
759   This should have the same name in Fortran.
760 
761 .seealso: `PetscCallMPI()`, `PetscCallAbort()`, `SETERRABORT()`
762 M*/
763 #if defined(PETSC_CLANG_STATIC_ANALYZER)
764 void PetscCallMPI(PetscMPIInt);
765 void PetscCallMPIAbort(MPI_Comm, PetscMPIInt);
766 void PetscCallMPINull(PetscMPIInt);
767 #else
768   #define PetscCallMPI_Private(__PETSC_STACK_POP_FUNC__, __SETERR_FUNC__, __COMM__, ...) \
769     do { \
770       PetscMPIInt ierr_petsc_call_mpi_; \
771       PetscStackUpdateLine; \
772       PetscStackPushExternal("MPI function"); \
773       { \
774         ierr_petsc_call_mpi_ = __VA_ARGS__; \
775       } \
776       __PETSC_STACK_POP_FUNC__; \
777       if (PetscUnlikely(ierr_petsc_call_mpi_ != MPI_SUCCESS)) { \
778         char petsc_mpi_7_errorstring[2 * MPI_MAX_ERROR_STRING]; \
779         PetscMPIErrorString(ierr_petsc_call_mpi_, 2 * MPI_MAX_ERROR_STRING, (char *)petsc_mpi_7_errorstring); \
780         __SETERR_FUNC__(__COMM__, PETSC_ERR_MPI, "MPI error %d %s", ierr_petsc_call_mpi_, petsc_mpi_7_errorstring); \
781       } \
782     } while (0)
783 
784   #define PetscCallMPI(...)            PetscCallMPI_Private(PetscStackPop, SETERRQ, PETSC_COMM_SELF, __VA_ARGS__)
785   #define PetscCallMPIReturnMPI(...)   PetscCallMPI_Private(PetscStackPopNoCheck(PETSC_FUNCTION_NAME), SETERRMPI, PETSC_COMM_SELF, __VA_ARGS__)
786   #define PetscCallMPIAbort(comm, ...) PetscCallMPI_Private(PetscStackPopNoCheck(PETSC_FUNCTION_NAME), SETERRABORT, comm, __VA_ARGS__)
787   #define PetscCallMPINull(...)        PetscCallMPI_Private(PetscStackPopNoCheck(PETSC_FUNCTION_NAME), SETERRQNULL, PETSC_COMM_SELF, __VA_ARGS__)
788 #endif
789 
790 /*MC
791   CHKERRMPI - Checks error code returned from MPI calls, if non-zero it calls the error
792   handler and then returns
793 
794   Synopsis:
795   #include <petscerror.h>
796   void CHKERRMPI(PetscErrorCode ierr)
797 
798   Not Collective
799 
800   Input Parameter:
801 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
802 
803   Level: deprecated
804 
805   Note:
806   Deprecated in favor of `PetscCallMPI()`. This routine behaves identically to it.
807 
808 .seealso: `PetscCallMPI()`
809 M*/
810 #define CHKERRMPI(...) PetscCallMPI(__VA_ARGS__)
811 
812 /*MC
813   PetscCallAbort - Checks error code returned from PETSc function, if non-zero it aborts immediately by calling `MPI_Abort()`
814 
815   Synopsis:
816   #include <petscerror.h>
817   void PetscCallAbort(MPI_Comm comm, PetscErrorCode ierr)
818 
819   Collective
820 
821   Input Parameters:
822 + comm - the MPI communicator on which to abort
823 - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
824 
825   Level: intermediate
826 
827   Notes:
828   This macro has identical type and usage semantics to `PetscCall()` with the important caveat
829   that this macro does not return. Instead, if ierr is nonzero it calls the PETSc error handler
830   and then immediately calls `MPI_Abort()`. It can therefore be used anywhere.
831 
832   As per `MPI_Abort()` semantics the communicator passed must be valid, although there is currently
833   no attempt made at handling any potential errors from `MPI_Abort()`. Note that while
834   `MPI_Abort()` is required to terminate only those processes which reside on comm, it is often
835   the case that `MPI_Abort()` terminates *all* processes.
836 
837   Example Usage:
838 .vb
839   PetscErrorCode boom(void) { return PETSC_ERR_MEM; }
840 
841   void foo(void)
842   {
843     PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
844   }
845 
846   double bar(void)
847   {
848     PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
849   }
850 
851   PetscCallAbort(MPI_COMM_NULL,boom()); // ERROR, communicator should be valid
852 
853   struct baz
854   {
855     baz()
856     {
857       PetscCallAbort(PETSC_COMM_SELF,boom()); // OK
858     }
859 
860     ~baz()
861     {
862       PetscCallAbort(PETSC_COMM_SELF,boom()); // OK (in fact the only way to handle PETSc errors)
863     }
864   };
865 .ve
866 
867   Fortran Note:
868   Use `PetscCallA()`.
869 
870   Developer Note:
871   This should have the same name in Fortran as in C.
872 
873 .seealso: `SETERRABORT()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`,
874           `SETERRQ()`, `CHKMEMQ`, `PetscCallMPI()`, `PetscCallCXXAbort()`
875 M*/
876 #if defined(PETSC_CLANG_STATIC_ANALYZER)
877 void PetscCallAbort(MPI_Comm, PetscErrorCode);
878 void PetscCallContinue(PetscErrorCode);
879 #else
880   #define PetscCallAbort(comm, ...) \
881     do { \
882       PetscErrorCode ierr_petsc_call_abort_; \
883       PetscStackUpdateLine; \
884       ierr_petsc_call_abort_ = __VA_ARGS__; \
885       if (PetscUnlikely(ierr_petsc_call_abort_ != PETSC_SUCCESS)) { \
886         ierr_petsc_call_abort_ = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_abort_, PETSC_ERROR_REPEAT, " "); \
887         (void)MPI_Abort(comm, (PetscMPIInt)ierr_petsc_call_abort_); \
888       } \
889     } while (0)
890   #define PetscCallContinue(...) \
891     do { \
892       PetscErrorCode ierr_petsc_call_continue_; \
893       PetscStackUpdateLine; \
894       ierr_petsc_call_continue_ = __VA_ARGS__; \
895       if (PetscUnlikely(ierr_petsc_call_continue_ != PETSC_SUCCESS)) { \
896         ierr_petsc_call_continue_ = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_continue_, PETSC_ERROR_REPEAT, " "); \
897         (void)ierr_petsc_call_continue_; \
898       } \
899     } while (0)
900 #endif
901 
902 /*MC
903   CHKERRABORT - Checks error code returned from PETSc function. If non-zero it aborts immediately.
904 
905   Synopsis:
906   #include <petscerror.h>
907   void CHKERRABORT(MPI_Comm comm, PetscErrorCode ierr)
908 
909   Not Collective
910 
911   Input Parameters:
912 + comm - the MPI communicator
913 - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
914 
915   Level: deprecated
916 
917   Note:
918   Deprecated in favor of `PetscCallAbort()`. This routine behaves identically to it.
919 
920 .seealso: `PetscCallAbort()`, `PetscErrorCode`
921 M*/
922 #define CHKERRABORT(comm, ...) PetscCallAbort(comm, __VA_ARGS__)
923 #define CHKERRCONTINUE(...)    PetscCallContinue(__VA_ARGS__)
924 
925 /*MC
926    CHKERRA - Fortran-only replacement for use of `CHKERRQ()` in the main program, which aborts immediately
927 
928    Synopsis:
929    #include <petscsys.h>
930    PetscErrorCode CHKERRA(PetscErrorCode ierr)
931 
932    Not Collective
933 
934    Input Parameter:
935 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
936 
937    Level: deprecated
938 
939    Note:
940    This macro is rarely needed, normal usage is `PetscCallA()` in the main Fortran program.
941 
942    Developer Note:
943    Why isn't this named `CHKERRABORT()` in Fortran?
944 
945 .seealso: `PetscCall()`, `PetscCallA()`, `PetscCallAbort()`, `CHKERRQ()`, `SETERRA()`, `SETERRQ()`, `SETERRABORT()`
946 M*/
947 
948 PETSC_EXTERN PetscBool petscwaitonerrorflg;
949 PETSC_EXTERN PetscBool petscindebugger;
950 PETSC_EXTERN PetscBool petscabortmpifinalize;
951 
952 #if defined(PETSC_CLANG_STATIC_ANALYZER)
953 void PETSCABORTWITHERR_Private(MPI_Comm, PetscErrorCode);
954 #else
955   #define PETSCABORTWITHIERR_Private(comm, ierr) \
956     do { \
957       PetscMPIInt size_; \
958       (void)MPI_Comm_size(comm, &size_); \
959       if (PetscCIEnabledPortableErrorOutput && (size_ == PetscGlobalSize || petscabortmpifinalize) && ierr != PETSC_ERR_SIG) { \
960         (void)MPI_Finalize(); \
961         exit(0); \
962       } else if (PetscCIEnabledPortableErrorOutput && PetscGlobalSize == 1) { \
963         exit(0); \
964       } else { \
965         (void)MPI_Abort(comm, (PetscMPIInt)ierr); \
966       } \
967     } while (0)
968 #endif
969 
970 /*MC
971    PETSCABORT - Call `MPI_Abort()` with an informative error code
972 
973    Synopsis:
974    #include <petscsys.h>
975    PETSCABORT(MPI_Comm comm, PetscErrorCode ierr)
976 
977    Collective; No Fortran Support
978 
979    Input Parameters:
980 +  comm - An MPI communicator, so that the error can be collective
981 -  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
982 
983    Level: advanced
984 
985    Notes:
986    If the option `-start_in_debugger` was used then this calls `abort()` to stop the program in the debugger.
987 
988    if `PetscCIEnabledPortableErrorOutput` is set, which means the code is running in the PETSc test harness (make test),
989    and `comm` is `MPI_COMM_WORLD` it strives to exit cleanly without calling `MPI_Abort()` and instead calling `MPI_Finalize()`.
990 
991    This is currently only used when an error propagates up to the C `main()` program and is detected by a `PetscCall()`, `PetscCallMPI()`,
992    or is set in `main()` with `SETERRQ()`. Abort calls such as `SETERRABORT()`,
993    `PetscCheckAbort()`, `PetscCallMPIAbort()`, and `PetscCallAbort()` always call `MPI_Abort()` and do not have any special
994    handling for the test harness.
995 
996    Developer Note:
997    Should the other abort calls also pass through this call instead of calling `MPI_Abort()` directly?
998 
999 .seealso: `PetscError()`, `PetscCall()`, `SETERRABORT()`, `PetscCheckAbort()`, `PetscCallMPIAbort()`, `PetscCall()`, `PetscCallMPI()`,
1000           `PetscCallAbort()`, `MPI_Abort()`, `PetscErrorCode`
1001 M*/
1002 #if defined(PETSC_CLANG_STATIC_ANALYZER)
1003 void PETSCABORT(MPI_Comm, PetscErrorCode);
1004 #else
1005   #define PETSCABORT(comm, ...) \
1006     do { \
1007       PetscErrorCode ierr_petsc_abort_; \
1008       if (petscwaitonerrorflg) { ierr_petsc_abort_ = PetscSleep(1000); } \
1009       if (petscindebugger) { \
1010         abort(); \
1011       } else { \
1012         ierr_petsc_abort_ = __VA_ARGS__; \
1013         PETSCABORTWITHIERR_Private(comm, ierr_petsc_abort_); \
1014       } \
1015     } while (0)
1016 #endif
1017 
1018 #ifdef PETSC_CLANGUAGE_CXX
1019   /*MC
1020   PetscCallThrow - Checks error code, if non-zero it calls the C++ error handler which throws
1021   an exception
1022 
1023   Synopsis:
1024   #include <petscerror.h>
1025   void PetscCallThrow(PetscErrorCode ierr)
1026 
1027   Not Collective
1028 
1029   Input Parameter:
1030 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
1031 
1032   Level: beginner
1033 
1034   Notes:
1035   Requires PETSc to be configured with clanguage of c++. Throws a std::runtime_error() on error.
1036 
1037   Once the error handler throws the exception you can use `PetscCallVoid()` which returns without
1038   an error code (bad idea since the error is ignored) or `PetscCallAbort()` to have `MPI_Abort()`
1039   called immediately.
1040 
1041 .seealso: `SETERRQ()`, `PetscCall()`, `SETERRABORT()`, `PetscCallAbort()`, `PetscTraceBackErrorHandler()`,
1042           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
1043 M*/
1044   #define PetscCallThrow(...) \
1045     do { \
1046       PetscStackUpdateLine; \
1047       PetscErrorCode ierr_petsc_call_throw_ = __VA_ARGS__; \
1048       if (PetscUnlikely(ierr_petsc_call_throw_ != PETSC_SUCCESS)) PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_throw_, PETSC_ERROR_IN_CXX, PETSC_NULLPTR); \
1049     } while (0)
1050 
1051   /*MC
1052   CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
1053 
1054   Synopsis:
1055   #include <petscerror.h>
1056   void CHKERRXX(PetscErrorCode ierr)
1057 
1058   Not Collective
1059 
1060   Input Parameter:
1061 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
1062 
1063   Level: deprecated
1064 
1065   Note:
1066   Deprecated in favor of `PetscCallThrow()`. This routine behaves identically to it.
1067 
1068 .seealso: `PetscCallThrow()`
1069 M*/
1070   #define CHKERRXX(...) PetscCallThrow(__VA_ARGS__)
1071 #endif
1072 
1073 #define PetscCallCXX_Private(__SETERR_FUNC__, __COMM__, ...) \
1074   do { \
1075     PetscStackUpdateLine; \
1076     try { \
1077       __VA_ARGS__; \
1078     } catch (const std::exception &e) { \
1079       __SETERR_FUNC__(__COMM__, PETSC_ERR_LIB, "%s", e.what()); \
1080     } \
1081   } while (0)
1082 
1083 /*MC
1084   PetscCallCXX - Checks C++ function calls and if they throw an exception, catch it and then
1085   return a PETSc error code
1086 
1087   Synopsis:
1088   #include <petscerror.h>
1089   void PetscCallCXX(...) noexcept;
1090 
1091   Not Collective
1092 
1093   Input Parameter:
1094 . __VA_ARGS__ - An arbitrary expression
1095 
1096   Level: beginner
1097 
1098   Notes:
1099   `PetscCallCXX(...)` is a macro replacement for
1100 .vb
1101   try {
1102     __VA_ARGS__;
1103   } catch (const std::exception& e) {
1104     return ConvertToPetscErrorCode(e);
1105   }
1106 .ve
1107   Due to the fact that it catches any (reasonable) exception, it is essentially noexcept.
1108 
1109   If you cannot return a `PetscErrorCode` use `PetscCallCXXAbort()` instead.
1110 
1111   Example Usage:
1112 .vb
1113   void foo(void) { throw std::runtime_error("error"); }
1114 
1115   void bar()
1116   {
1117     PetscCallCXX(foo()); // ERROR bar() does not return PetscErrorCode
1118   }
1119 
1120   PetscErrorCode baz()
1121   {
1122     PetscCallCXX(foo()); // OK
1123 
1124     PetscCallCXX(
1125       bar();
1126       foo(); // OK multiple statements allowed
1127     );
1128   }
1129 
1130   struct bop
1131   {
1132     bop()
1133     {
1134       PetscCallCXX(foo()); // ERROR returns PetscErrorCode, cannot be used in constructors
1135     }
1136   };
1137 
1138   // ERROR contains do-while, cannot be used as function-try block
1139   PetscErrorCode qux() PetscCallCXX(
1140     bar();
1141     baz();
1142     foo();
1143     return 0;
1144   )
1145 .ve
1146 
1147 .seealso: `PetscCallCXXAbort()`, `PetscCallThrow()`, `SETERRQ()`, `PetscCall()`,
1148           `SETERRABORT()`, `PetscCallAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
1149           `PetscError()`, `CHKMEMQ`
1150 M*/
1151 #define PetscCallCXX(...) PetscCallCXX_Private(SETERRQ, PETSC_COMM_SELF, __VA_ARGS__)
1152 
1153 /*MC
1154   PetscCallCXXAbort - Like `PetscCallCXX()` but calls `MPI_Abort()` instead of returning an
1155   error-code
1156 
1157   Synopsis:
1158   #include <petscerror.h>
1159   void PetscCallCXXAbort(MPI_Comm comm, ...) noexcept;
1160 
1161   Collective; No Fortran Support
1162 
1163   Input Parameters:
1164 + comm        - The MPI communicator to abort on
1165 - __VA_ARGS__ - An arbitrary expression
1166 
1167   Level: beginner
1168 
1169   Notes:
1170   This macro may be used to check C++ expressions for exceptions in cases where you cannot
1171   return an error code. This includes constructors, destructors, copy/move assignment functions
1172   or constructors among others.
1173 
1174   If an exception is caught, the macro calls `SETERRABORT()` on `comm`. The exception must
1175   derive from `std::exception` in order to be caught.
1176 
1177   If the routine _can_ return an error-code it is highly advised to use `PetscCallCXX()`
1178   instead.
1179 
1180   See `PetscCallCXX()` for additional discussion.
1181 
1182   Example Usage:
1183 .vb
1184   class Foo
1185   {
1186     std::vector<int> data_;
1187 
1188   public:
1189     // normally std::vector::reserve() may raise an exception, but since we handle it with
1190     // PetscCallCXXAbort() we may mark this routine as noexcept!
1191     Foo() noexcept
1192     {
1193       PetscCallCXXAbort(PETSC_COMM_SELF, data_.reserve(10));
1194     }
1195   };
1196 
1197   std::vector<int> bar()
1198   {
1199     std::vector<int> v;
1200 
1201     PetscFunctionBegin;
1202     // OK!
1203     PetscCallCXXAbort(PETSC_COMM_SELF, v.emplace_back(1));
1204     PetscFunctionReturn(v);
1205   }
1206 
1207   PetscErrorCode baz()
1208   {
1209     std::vector<int> v;
1210 
1211     PetscFunctionBegin;
1212     // WRONG! baz() returns a PetscErrorCode, prefer PetscCallCXX() instead
1213     PetscCallCXXAbort(PETSC_COMM_SELF, v.emplace_back(1));
1214     PetscFunctionReturn(PETSC_SUCCESS);
1215   }
1216 .ve
1217 
1218 .seealso: `PetscCallCXX()`, `SETERRABORT()`, `PetscCallAbort()`
1219 M*/
1220 #define PetscCallCXXAbort(comm, ...) PetscCallCXX_Private(SETERRABORT, comm, __VA_ARGS__)
1221 
1222 /*MC
1223   CHKERRCXX - Checks C++ function calls and if they throw an exception, catch it and then
1224   return a PETSc error code
1225 
1226   Synopsis:
1227   #include <petscerror.h>
1228   void CHKERRCXX(func) noexcept;
1229 
1230   Not Collective
1231 
1232   Input Parameter:
1233 . func - C++ function calls
1234 
1235   Level: deprecated
1236 
1237   Note:
1238   Deprecated in favor of `PetscCallCXX()`. This routine behaves identically to it.
1239 
1240 .seealso: `PetscCallCXX()`
1241 M*/
1242 #define CHKERRCXX(...) PetscCallCXX(__VA_ARGS__)
1243 
1244 /*MC
1245    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
1246 
1247    Synopsis:
1248    #include <petscsys.h>
1249    CHKMEMQ;
1250 
1251    Not Collective
1252 
1253    Level: beginner
1254 
1255    Notes:
1256    We recommend using Valgrind <https://petsc.org/release/faq/#valgrind> or for NVIDIA CUDA systems
1257    <https://docs.nvidia.com/cuda/cuda-memcheck/index.html> for finding memory problems. The ``CHKMEMQ`` macro is useful on systems that
1258    do not have valgrind, but is not as good as valgrind or cuda-memcheck.
1259 
1260    Must run with the option `-malloc_debug` (`-malloc_test` in debug mode; or if `PetscMallocSetDebug()` called) to enable this option
1261 
1262    Once the error handler is called the calling function is then returned from with the given error code.
1263 
1264    By defaults prints location where memory that is corrupted was allocated.
1265 
1266    Use `CHKMEMA` for functions that return `void`
1267 
1268 .seealso: `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `SETERRQ()`, `PetscMallocValidate()`
1269 M*/
1270 #if defined(PETSC_CLANG_STATIC_ANALYZER)
1271   #define CHKMEMQ
1272   #define CHKMEMA
1273 #else
1274   #define CHKMEMQ \
1275     do { \
1276       PetscErrorCode ierr_petsc_memq_ = PetscMallocValidate(__LINE__, PETSC_FUNCTION_NAME, __FILE__); \
1277       if (PetscUnlikely(ierr_petsc_memq_ != PETSC_SUCCESS)) return PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_memq_, PETSC_ERROR_REPEAT, " "); \
1278     } while (0)
1279   #define CHKMEMA PetscMallocValidate(__LINE__, PETSC_FUNCTION_NAME, __FILE__)
1280 #endif
1281 
1282 /*E
1283   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
1284 
1285   Level: advanced
1286 
1287   Note:
1288   `PETSC_ERROR_IN_CXX` indicates the error was detected in C++ and an exception should be generated
1289 
1290   Developer Note:
1291   This is currently used to decide when to print the detailed information about the run in `PetscTraceBackErrorHandler()`
1292 
1293 .seealso: `PetscError()`, `SETERRQ()`
1294 E*/
1295 typedef enum {
1296   PETSC_ERROR_INITIAL = 0,
1297   PETSC_ERROR_REPEAT  = 1,
1298   PETSC_ERROR_IN_CXX  = 2
1299 } PetscErrorType;
1300 
1301 #if defined(__clang_analyzer__)
1302 __attribute__((analyzer_noreturn))
1303 #endif
1304 PETSC_EXTERN PetscErrorCode
1305 PetscError(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, ...) PETSC_ATTRIBUTE_COLD PETSC_ATTRIBUTE_FORMAT(7, 8);
1306 
1307 PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
1308 PETSC_EXTERN PetscErrorCode PetscErrorMessage(PetscErrorCode, const char *[], char **);
1309 PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1310 PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1311 PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1312 PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1313 PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1314 PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1315 PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1316 PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *), void *);
1317 PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
1318 PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int, void *);
1319 PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int, void *), void *);
1320 PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
1321 PETSC_EXTERN PetscErrorCode PetscCheckPointerSetIntensity(PetscInt);
1322 PETSC_EXTERN void           PetscSignalSegvCheckPointerOrMpi(void);
1323 PETSC_DEPRECATED_FUNCTION(3, 13, 0, "PetscSignalSegvCheckPointerOrMpi()", ) static inline void PetscSignalSegvCheckPointer(void)
1324 {
1325   PetscSignalSegvCheckPointerOrMpi();
1326 }
1327 
1328 /*MC
1329     PetscErrorPrintf - Prints error messages.
1330 
1331    Synopsis:
1332     #include <petscsys.h>
1333      PetscErrorCode (*PetscErrorPrintf)(const char format[], ...);
1334 
1335     Not Collective; No Fortran Support
1336 
1337     Input Parameter:
1338 .   format - the usual `printf()` format string
1339 
1340    Options Database Keys:
1341 +  -error_output_stdout - cause error messages to be printed to stdout instead of the (default) stderr
1342 -  -error_output_none   - to turn off all printing of error messages (does not change the way the error is handled.)
1343 
1344    Level: developer
1345 
1346    Notes:
1347    Use
1348 .vb
1349      PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the error is handled) and
1350      PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on or you can use your own function
1351 .ve
1352    Use
1353 .vb
1354      `PETSC_STDERR` = FILE* obtained from a file open etc. to have stderr printed to the file.
1355      `PETSC_STDOUT` = FILE* obtained from a file open etc. to have stdout printed to the file.
1356 .ve
1357    Use
1358 .vb
1359       `PetscPushErrorHandler()` to provide your own error handler that determines what kind of messages to print
1360 .ve
1361 
1362 .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscHelpPrintf()`, `PetscPrintf()`, `PetscPushErrorHandler()`, `PetscVFPrintf()`, `PetscHelpPrintf()`
1363 M*/
1364 PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
1365 
1366 /*E
1367      PetscFPTrap - types of floating point exceptions that may be trapped
1368 
1369      Currently only `PETSC_FP_TRAP_OFF` and `PETSC_FP_TRAP_ON` are handled. All others are treated as `PETSC_FP_TRAP_ON`.
1370 
1371      Level: intermediate
1372 
1373 .seealso: `PetscSetFPTrap()`, `PetscFPTrapPush()`
1374  E*/
1375 typedef enum {
1376   PETSC_FP_TRAP_OFF      = 0,
1377   PETSC_FP_TRAP_INDIV    = 1,
1378   PETSC_FP_TRAP_FLTOPERR = 2,
1379   PETSC_FP_TRAP_FLTOVF   = 4,
1380   PETSC_FP_TRAP_FLTUND   = 8,
1381   PETSC_FP_TRAP_FLTDIV   = 16,
1382   PETSC_FP_TRAP_FLTINEX  = 32,
1383   PETSC_FP_TRAP_ON       = 63
1384 } PetscFPTrap;
1385 
1386 PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
1387 PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
1388 PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
1389 PETSC_EXTERN PetscErrorCode PetscDetermineInitialFPTrap(void);
1390 
1391 /*
1392       Allows the code to build a stack frame as it runs
1393 */
1394 
1395 #define PETSCSTACKSIZE 64
1396 typedef struct {
1397   const char *function[PETSCSTACKSIZE];
1398   const char *file[PETSCSTACKSIZE];
1399   int         line[PETSCSTACKSIZE];
1400   int         petscroutine[PETSCSTACKSIZE]; /* 0 external called from petsc, 1 petsc functions, 2 petsc user functions */
1401   int         currentsize;
1402   int         hotdepth;
1403   PetscBool   check; /* option to check for correct Push/Pop semantics, true for default petscstack but not other stacks */
1404 } PetscStack;
1405 #if defined(PETSC_USE_DEBUG) && !defined(PETSC_HAVE_THREADSAFETY)
1406 PETSC_EXTERN PetscStack petscstack;
1407 #endif
1408 
1409 #if defined(PETSC_SERIALIZE_FUNCTIONS)
1410   #include <petsc/private/petscfptimpl.h>
1411   /*
1412    Registers the current function into the global function pointer to function name table
1413 
1414    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
1415 */
1416   #define PetscRegister__FUNCT__() \
1417     do { \
1418       static PetscBool __chked = PETSC_FALSE; \
1419       if (!__chked) { \
1420         void *ptr; \
1421         PetscCallAbort(PETSC_COMM_SELF, PetscDLSym(NULL, PETSC_FUNCTION_NAME, &ptr)); \
1422         __chked = PETSC_TRUE; \
1423       } \
1424     } while (0)
1425 #else
1426   #define PetscRegister__FUNCT__()
1427 #endif
1428 
1429 #if defined(PETSC_CLANG_STATIC_ANALYZER) || defined(__clang_analyzer__)
1430   #define PetscStackPushNoCheck(funct, petsc_routine, hot)
1431   #define PetscStackUpdateLine
1432   #define PetscStackPushExternal(funct)
1433   #define PetscStackPopNoCheck(funct)
1434   #define PetscStackClearTop
1435   #define PetscFunctionBegin
1436   #define PetscFunctionBeginUser
1437   #define PetscFunctionBeginHot
1438   #define PetscFunctionReturn(...)  return __VA_ARGS__
1439   #define PetscFunctionReturnVoid() return
1440   #define PetscStackPop
1441   #define PetscStackPush(f)
1442   #define PetscStackPush_Private(stack__, file__, func__, line__, petsc_routine__, hot__) \
1443     (void)file__; \
1444     (void)func__; \
1445     (void)line__
1446   #define PetscStackPop_Private(stack__, func__) (void)func__
1447 #elif defined(PETSC_USE_DEBUG) && !defined(PETSC_HAVE_THREADSAFETY)
1448 
1449   #define PetscStackPush_Private(stack__, file__, func__, line__, petsc_routine__, hot__) \
1450     do { \
1451       if (stack__.currentsize < PETSCSTACKSIZE) { \
1452         stack__.function[stack__.currentsize] = func__; \
1453         if (petsc_routine__) { \
1454           stack__.file[stack__.currentsize] = file__; \
1455           stack__.line[stack__.currentsize] = line__; \
1456         } else { \
1457           stack__.file[stack__.currentsize] = PETSC_NULLPTR; \
1458           stack__.line[stack__.currentsize] = 0; \
1459         } \
1460         stack__.petscroutine[stack__.currentsize] = petsc_routine__; \
1461       } \
1462       ++stack__.currentsize; \
1463       stack__.hotdepth += (hot__ || stack__.hotdepth); \
1464     } while (0)
1465 
1466   /* uses PetscCheckAbort() because may be used in a function that does not return an error code */
1467   #define PetscStackPop_Private(stack__, func__) \
1468     do { \
1469       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__); \
1470       if (--stack__.currentsize < PETSCSTACKSIZE) { \
1471         PetscCheckAbort(!stack__.check || stack__.petscroutine[stack__.currentsize] != 1 || stack__.function[stack__.currentsize] == func__, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid stack: push from %s %s:%d. Pop from %s %s:%d.\n", \
1472                         stack__.function[stack__.currentsize], stack__.file[stack__.currentsize], stack__.line[stack__.currentsize], func__, __FILE__, __LINE__); \
1473         stack__.function[stack__.currentsize]     = PETSC_NULLPTR; \
1474         stack__.file[stack__.currentsize]         = PETSC_NULLPTR; \
1475         stack__.line[stack__.currentsize]         = 0; \
1476         stack__.petscroutine[stack__.currentsize] = 0; \
1477       } \
1478       stack__.hotdepth = PetscMax(stack__.hotdepth - 1, 0); \
1479     } while (0)
1480 
1481   /*MC
1482    PetscStackPushNoCheck - Pushes a new function name and line number onto the PETSc default stack that tracks where the running program is
1483    currently in the source code.
1484 
1485    Synopsis:
1486    #include <petscsys.h>
1487    void PetscStackPushNoCheck(char *funct,int petsc_routine,PetscBool hot);
1488 
1489    Not Collective
1490 
1491    Input Parameters:
1492 +  funct - the function name
1493 .  petsc_routine - 2 user function, 1 PETSc function, 0 some other function
1494 -  hot - indicates that the function may be called often so expensive error checking should be turned off inside the function
1495 
1496    Level: developer
1497 
1498    Notes:
1499    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
1500    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
1501    help debug the problem.
1502 
1503    This version does not check the memory corruption (an expensive operation), use `PetscStackPush()` to check the memory.
1504 
1505    Use `PetscStackPushExternal()` for a function call that is about to be made to a non-PETSc or user function (such as BLAS etc).
1506 
1507    The default stack is a global variable called `petscstack`.
1508 
1509 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1510           `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPush()`, `PetscStackPop`,
1511           `PetscStackPushExternal()`
1512 M*/
1513   #define PetscStackPushNoCheck(funct, petsc_routine, hot) \
1514     do { \
1515       PetscStackSAWsTakeAccess(); \
1516       PetscStackPush_Private(petscstack, __FILE__, funct, __LINE__, petsc_routine, hot); \
1517       PetscStackSAWsGrantAccess(); \
1518     } while (0)
1519 
1520   /*MC
1521    PetscStackUpdateLine - in a function that has a `PetscFunctionBegin` or `PetscFunctionBeginUser` updates the stack line number to the
1522    current line number.
1523 
1524    Synopsis:
1525    #include <petscsys.h>
1526    void PetscStackUpdateLine
1527 
1528    Not Collective
1529 
1530    Level: developer
1531 
1532    Notes:
1533    Using `PetscCall()` and friends automatically handles this process
1534 
1535    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
1536    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1537    help debug the problem.
1538 
1539    The default stack is a global variable called `petscstack`.
1540 
1541    This is used by `PetscCall()` and is otherwise not like to be needed
1542 
1543 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPop`, `PetscCall()`
1544 M*/
1545   #define PetscStackUpdateLine \
1546     do { \
1547       if (petscstack.currentsize > 0 && petscstack.currentsize < PETSCSTACKSIZE && petscstack.function[petscstack.currentsize - 1] == PETSC_FUNCTION_NAME) { petscstack.line[petscstack.currentsize - 1] = __LINE__; } \
1548     } while (0)
1549 
1550   /*MC
1551    PetscStackPushExternal - Pushes a new function name onto the PETSc default stack that tracks where the running program is
1552    currently in the source code. Does not include the filename or line number since this is called by the calling routine
1553    for non-PETSc or user functions.
1554 
1555    Synopsis:
1556    #include <petscsys.h>
1557    void PetscStackPushExternal(char *funct);
1558 
1559    Not Collective
1560 
1561    Input Parameter:
1562 .  funct - the function name
1563 
1564    Level: developer
1565 
1566    Notes:
1567    Using `PetscCallExternal()` and friends automatically handles this process
1568 
1569    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
1570    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1571    help debug the problem.
1572 
1573    The default stack is a global variable called `petscstack`.
1574 
1575    This is to be used when calling an external package function such as a BLAS function.
1576 
1577    This also updates the stack line number for the current stack function.
1578 
1579 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1580           `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1581 M*/
1582   #define PetscStackPushExternal(funct) \
1583     do { \
1584       PetscStackUpdateLine; \
1585       PetscStackPushNoCheck(funct, 0, PETSC_TRUE); \
1586     } while (0)
1587 
1588   /*MC
1589    PetscStackPopNoCheck - Pops a function name from the PETSc default stack that tracks where the running program is
1590    currently in the source code.
1591 
1592    Synopsis:
1593    #include <petscsys.h>
1594    void PetscStackPopNoCheck(char *funct);
1595 
1596    Not Collective
1597 
1598    Input Parameter:
1599 .   funct - the function name
1600 
1601    Level: developer
1602 
1603    Notes:
1604    Using `PetscCall()`, `PetscCallExternal()`, `PetscCallBack()` and friends negates the need to call this
1605 
1606    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
1607    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1608    help debug the problem.
1609 
1610    The default stack is a global variable called `petscstack`.
1611 
1612    Developer Note:
1613    `PetscStackPopNoCheck()` takes a function argument while  `PetscStackPop` does not, this difference is likely just historical.
1614 
1615 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1616 M*/
1617   #define PetscStackPopNoCheck(funct) \
1618     do { \
1619       PetscStackSAWsTakeAccess(); \
1620       PetscStackPop_Private(petscstack, funct); \
1621       PetscStackSAWsGrantAccess(); \
1622     } while (0)
1623 
1624   #define PetscStackClearTop \
1625     do { \
1626       PetscStackSAWsTakeAccess(); \
1627       if (petscstack.currentsize > 0 && --petscstack.currentsize < PETSCSTACKSIZE) { \
1628         petscstack.function[petscstack.currentsize]     = PETSC_NULLPTR; \
1629         petscstack.file[petscstack.currentsize]         = PETSC_NULLPTR; \
1630         petscstack.line[petscstack.currentsize]         = 0; \
1631         petscstack.petscroutine[petscstack.currentsize] = 0; \
1632       } \
1633       petscstack.hotdepth = PetscMax(petscstack.hotdepth - 1, 0); \
1634       PetscStackSAWsGrantAccess(); \
1635     } while (0)
1636 
1637   /*MC
1638    PetscFunctionBegin - First executable line of each PETSc function,  used for error handling. Final
1639    line of PETSc functions should be `PetscFunctionReturn`(0);
1640 
1641    Synopsis:
1642    #include <petscsys.h>
1643    void PetscFunctionBegin;
1644 
1645    Not Collective; No Fortran Support
1646 
1647    Usage:
1648 .vb
1649      int something;
1650 
1651      PetscFunctionBegin;
1652 .ve
1653 
1654    Level: developer
1655 
1656    Note:
1657    Use `PetscFunctionBeginUser` for application codes.
1658 
1659 .seealso: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`
1660 
1661 M*/
1662   #define PetscFunctionBegin \
1663     do { \
1664       PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 1, PETSC_FALSE); \
1665       PetscRegister__FUNCT__(); \
1666     } while (0)
1667 
1668   /*MC
1669    PetscFunctionBeginHot - Substitute for `PetscFunctionBegin` to be used in functions that are called in
1670    performance-critical circumstances.  Use of this function allows for lighter profiling by default.
1671 
1672    Synopsis:
1673    #include <petscsys.h>
1674    void PetscFunctionBeginHot;
1675 
1676    Not Collective; No Fortran Support
1677 
1678    Usage:
1679 .vb
1680      int something;
1681 
1682      PetscFunctionBeginHot;
1683 .ve
1684 
1685    Level: developer
1686 
1687 .seealso: `PetscFunctionBegin`, `PetscFunctionReturn()`, `PetscStackPushNoCheck()`
1688 
1689 M*/
1690   #define PetscFunctionBeginHot \
1691     do { \
1692       PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 1, PETSC_TRUE); \
1693       PetscRegister__FUNCT__(); \
1694     } while (0)
1695 
1696   /*MC
1697    PetscFunctionBeginUser - First executable line of user provided routines
1698 
1699    Synopsis:
1700    #include <petscsys.h>
1701    void PetscFunctionBeginUser;
1702 
1703    Not Collective; No Fortran Support
1704 
1705    Usage:
1706 .vb
1707      int something;
1708 
1709      PetscFunctionBeginUser;
1710 .ve
1711 
1712    Level: intermediate
1713 
1714    Notes:
1715    Functions that incorporate this must call `PetscFunctionReturn()` instead of return except for main().
1716 
1717    May be used before `PetscInitialize()`
1718 
1719    This is identical to `PetscFunctionBegin` except it labels the routine as a user
1720    routine instead of as a PETSc library routine.
1721 
1722 .seealso: `PetscFunctionReturn()`, `PetscFunctionBegin`, `PetscFunctionBeginHot`, `PetscStackPushNoCheck()`
1723 M*/
1724   #define PetscFunctionBeginUser \
1725     do { \
1726       PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 2, PETSC_FALSE); \
1727       PetscRegister__FUNCT__(); \
1728     } while (0)
1729 
1730   /*MC
1731    PetscStackPush - Pushes a new function name and line number onto the PETSc default stack that tracks where the running program is
1732    currently in the source code and verifies the memory is not corrupted.
1733 
1734    Synopsis:
1735    #include <petscsys.h>
1736    void PetscStackPush(char *funct)
1737 
1738    Not Collective
1739 
1740    Input Parameter:
1741 .  funct - the function name
1742 
1743    Level: developer
1744 
1745    Notes:
1746    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
1747    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1748    help debug the problem.
1749 
1750    The default stack is a global variable called `petscstack`.
1751 
1752 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1753           `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1754 M*/
1755   #define PetscStackPush(n) \
1756     do { \
1757       PetscStackPushNoCheck(n, 0, PETSC_FALSE); \
1758       CHKMEMQ; \
1759     } while (0)
1760 
1761   /*MC
1762    PetscStackPop - Pops a function name from the PETSc default stack that tracks where the running program is
1763    currently in the source code and verifies the memory is not corrupted.
1764 
1765    Synopsis:
1766    #include <petscsys.h>
1767    void PetscStackPop
1768 
1769    Not Collective
1770 
1771    Level: developer
1772 
1773    Notes:
1774    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
1775    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1776    help debug the problem.
1777 
1778    The default stack is a global variable called `petscstack`.
1779 
1780 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPopNoCheck()`, `PetscStackPush()`
1781 M*/
1782   #define PetscStackPop \
1783     do { \
1784       CHKMEMQ; \
1785       PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1786     } while (0)
1787 
1788   /*MC
1789    PetscFunctionReturn - Last executable line of each PETSc function used for error
1790    handling. Replaces `return()`.
1791 
1792    Synopsis:
1793    #include <petscerror.h>
1794    void PetscFunctionReturn(...)
1795 
1796    Not Collective; No Fortran Support
1797 
1798    Level: beginner
1799 
1800    Notes:
1801    This routine is a macro, so while it does not "return" anything itself, it does return from
1802    the function in the literal sense.
1803 
1804    Usually the return value is the integer literal `0` (for example in any function returning
1805    `PetscErrorCode`), however it is possible to return any arbitrary type. The arguments of
1806    this macro are placed before the `return` statement as-is.
1807 
1808    Any routine which returns via `PetscFunctionReturn()` must begin with a corresponding
1809    `PetscFunctionBegin`.
1810 
1811    For routines which return `void` use `PetscFunctionReturnVoid()` instead.
1812 
1813    Example Usage:
1814 .vb
1815    PetscErrorCode foo(int *x)
1816    {
1817      PetscFunctionBegin; // don't forget the begin!
1818      *x = 10;
1819      PetscFunctionReturn(PETSC_SUCCESS);
1820    }
1821 .ve
1822 
1823    May return any arbitrary type\:
1824 .vb
1825   struct Foo
1826   {
1827     int x;
1828   };
1829 
1830   struct Foo make_foo(int value)
1831   {
1832     struct Foo f;
1833 
1834     PetscFunctionBegin;
1835     f.x = value;
1836     PetscFunctionReturn(f);
1837   }
1838 .ve
1839 
1840 .seealso: `PetscFunctionBegin`, `PetscFunctionBeginUser`, `PetscFunctionReturnVoid()`,
1841           `PetscStackPopNoCheck()`
1842 M*/
1843   #define PetscFunctionReturn(...) \
1844     do { \
1845       PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1846       return __VA_ARGS__; \
1847     } while (0)
1848 
1849   /*MC
1850   PetscFunctionReturnVoid - Like `PetscFunctionReturn()` but returns `void`
1851 
1852   Synopsis:
1853   #include <petscerror.h>
1854   void PetscFunctionReturnVoid()
1855 
1856   Not Collective
1857 
1858   Level: beginner
1859 
1860   Note:
1861   Behaves identically to `PetscFunctionReturn()` except that it returns `void`. That is, this
1862   macro culminates with `return`.
1863 
1864   Example Usage:
1865 .vb
1866   void foo()
1867   {
1868     PetscFunctionBegin; // must start with PetscFunctionBegin!
1869     bar();
1870     baz();
1871     PetscFunctionReturnVoid();
1872   }
1873 .ve
1874 
1875 .seealso: `PetscFunctionReturn()`, `PetscFunctionBegin`, PetscFunctionBeginUser`
1876 M*/
1877   #define PetscFunctionReturnVoid() \
1878     do { \
1879       PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1880       return; \
1881     } while (0)
1882 #else /* PETSC_USE_DEBUG */
1883   #define PetscStackPushNoCheck(funct, petsc_routine, hot)
1884   #define PetscStackUpdateLine
1885   #define PetscStackPushExternal(funct)
1886   #define PetscStackPopNoCheck(...)
1887   #define PetscStackClearTop
1888   #define PetscFunctionBegin
1889   #define PetscFunctionBeginUser
1890   #define PetscFunctionBeginHot
1891   #define PetscFunctionReturn(...)  return __VA_ARGS__
1892   #define PetscFunctionReturnVoid() return
1893   #define PetscStackPop             CHKMEMQ
1894   #define PetscStackPush(f)         CHKMEMQ
1895 #endif /* PETSC_USE_DEBUG */
1896 
1897 #if defined(PETSC_CLANG_STATIC_ANALYZER)
1898   #define PetscStackCallExternalVoid(...)
1899 template <typename F, typename... Args>
1900 void PetscCallExternal(F, Args...);
1901 template <typename F, typename... Args>
1902 void PetscCallExternalAbort(F, Args...);
1903 #else
1904   /*MC
1905     PetscStackCallExternalVoid - Calls an external library routine or user function after pushing the name of the routine on the stack.
1906 
1907    Input Parameters:
1908 +   name    - string that gives the name of the function being called
1909 -   routine - actual call to the routine, for example, functionname(a,b)
1910 
1911    Level: developer
1912 
1913    Notes:
1914    Often one should use `PetscCallExternal()` instead. This routine is intended for external library routines that DO NOT return error codes
1915 
1916    In debug mode this also checks the memory for corruption at the end of the function call.
1917 
1918    Certain external packages, such as BLAS/LAPACK may have their own macros, `PetscCallBLAS()` for managing the call, error checking, etc.
1919 
1920    Developer Note:
1921    This is so that when a user or external library routine results in a crash or corrupts memory, they get blamed instead of PETSc.
1922 
1923 .seealso: `PetscCall()`, `PetscStackPushNoCheck()`, `PetscStackPush()`, `PetscCallExternal()`, `PetscCallBLAS()`
1924 @*/
1925   #define PetscStackCallExternalVoid(name, ...) \
1926     do { \
1927       PetscStackPushExternal(name); \
1928       __VA_ARGS__; \
1929       PetscStackPop; \
1930     } while (0)
1931 
1932   /*MC
1933     PetscCallExternal - Calls an external library routine that returns an error code after pushing the name of the routine on the stack.
1934 
1935    Input Parameters:
1936 +  func - name of the routine
1937 -  args - arguments to the routine
1938 
1939    Level: developer
1940 
1941    Notes:
1942    This is intended for external package routines that return error codes. Use `PetscStackCallExternalVoid()` for those that do not.
1943 
1944    In debug mode this also checks the memory for corruption at the end of the function call.
1945 
1946    Assumes the error return code of the function is an integer and that a value of 0 indicates success
1947 
1948    Developer Note:
1949    This is so that when an external package routine results in a crash or corrupts memory, they get blamed instead of PETSc.
1950 
1951 .seealso: `PetscCall()`, `PetscStackPushNoCheck()`, `PetscStackPush()`, `PetscStackCallExternalVoid()`, `PetscCallExternalAbort()`
1952 M*/
1953   #define PetscCallExternal(func, ...) \
1954     do { \
1955       PetscStackPush(PetscStringize(func)); \
1956       int ierr_petsc_call_external_ = (int)func(__VA_ARGS__); \
1957       PetscStackPop; \
1958       PetscCheck(ierr_petsc_call_external_ == 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in %s(): error code %d", PetscStringize(func), ierr_petsc_call_external_); \
1959     } while (0)
1960 
1961   /*MC
1962     PetscCallExternalAbort - Calls an external library routine that returns an error code after pushing the name of the routine on the stack. If the external library function return code indicates an error, this prints the error and aborts
1963 
1964    Input Parameters:
1965 +  func - name of the routine
1966 -  args - arguments to the routine
1967 
1968    Level: developer
1969 
1970    Notes:
1971    This is intended for external package routines that return error codes. Use `PetscStackCallExternalVoid()` for those that do not.
1972 
1973    In debug mode this also checks the memory for corruption at the end of the function call.
1974 
1975    Assumes the error return code of the function is an integer and that a value of 0 indicates success
1976 
1977    Developer Note:
1978    This is so that when an external package routine results in a crash or corrupts memory, they get blamed instead of PETSc.
1979 
1980 .seealso: `PetscCall()`, `PetscStackPushNoCheck()`, `PetscStackPush()`, `PetscStackCallExternalVoid()`, `PetscCallExternal()`
1981 M*/
1982   #define PetscCallExternalAbort(func, ...) \
1983     do { \
1984       PetscStackUpdateLine; \
1985       int ierr_petsc_call_external_ = func(__VA_ARGS__); \
1986       if (PetscUnlikely(ierr_petsc_call_external_ != 0)) { \
1987         (void)PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, PETSC_ERR_LIB, PETSC_ERROR_INITIAL, "Error in %s(): error code %d", PetscStringize(func), ierr_petsc_call_external_); \
1988         PETSCABORTWITHIERR_Private(PETSC_COMM_SELF, PETSC_ERR_LIB); \
1989       } \
1990     } while (0)
1991 #endif /* PETSC_CLANG_STATIC_ANALYZER */
1992