Actual source code: petscerror.h

  1: /*
  2:     Contains all error handling interfaces for PETSc.
  3: */
  4: #pragma once

  6: #include <petscmacros.h>
  7: #include <petscsystypes.h>

  9: #if defined(__cplusplus)
 10:   #include <exception> // std::exception
 11: #endif

 13: /* SUBMANSEC = Sys */

 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__)

 25: /*MC
 26:    SETERRQ - Macro to be called when an error has been detected,

 28:    Synopsis:
 29: #include <petscsys.h>
 30:    PetscErrorCode SETERRQ(MPI_Comm comm, PetscErrorCode ierr, char *message, ...)

 32:    Collective

 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

 39:   Level: beginner

 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.

 45:    Experienced users can set the error handler with `PetscPushErrorHandler()`.

 47:    Fortran Note:
 48:    `SETERRQ()` may be called from Fortran subroutines but `SETERRA()` must be called from the
 49:    Fortran main program.

 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)

 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)

 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;

 74: /*MC
 75:    SETERRMPI - Macro to be called when an error has been detected within an MPI callback function

 77:    No Fortran Support

 79:    Synopsis:
 80: #include <petscsys.h>
 81:    PetscErrorCode SETERRMPI(MPI_Comm comm, PetscErrorCode ierr, char *message, ...)

 83:    Collective

 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

 90:   Level: developer

 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.

 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)

100: /*MC
101:    SETERRA - Fortran-only macro that can be called when an error has been detected from the main program

103:    Synopsis:
104: #include <petscsys.h>
105:    PetscErrorCode SETERRA(MPI_Comm comm, PetscErrorCode ierr, char *message)

107:    Collective

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

114:    Level: beginner

116:    Notes:
117:    This should only be used with Fortran. With C/C++, use `SETERRQ()`.

119:    `SETERRQ()` may be called from Fortran subroutines but `SETERRA()` must be called from the
120:     Fortran main program.

122: .seealso: `SETERRQ()`, `SETERRABORT()`, `PetscCall()`, `CHKERRA()`, `PetscCallAbort()`, `PetscErrorCode`
123: M*/

125: /*MC
126:    SETERRABORT - Macro that can be called when an error has been detected,

128:    Synopsis:
129: #include <petscsys.h>
130:    PetscErrorCode SETERRABORT(MPI_Comm comm, PetscErrorCode ierr, char *message, ...)

132:    Collective

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

139:    Level: beginner

141:    Notes:
142:    This function just calls `MPI_Abort()`.

144:    This should only be called in routines that cannot return an error code, such as in C++ constructors.

146:    Fortran Note:
147:    Use `SETERRA()` in Fortran main program and `SETERRQ()` in Fortran subroutines

149:    Developer Note:
150:    In Fortran `SETERRA()` could be called `SETERRABORT()` since they serve the same purpose

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)

160: /*MC
161:   PetscCheck - Checks that a particular condition is true; if not true, then returns the provided error code

163:   Synopsis:
164: #include <petscerror.h>
165:   void PetscCheck(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)

167:   Collective; No Fortran Support

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

175:   Level: beginner

177:   Notes:
178:   Enabled in both optimized and debug builds.

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.

185:   Calls `SETERRQ()` if the assertion fails, so can only be called from functions returning a
186:   `PetscErrorCode` (or equivalent type after conversion).

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)

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.

199:   Synopsis:
200: #include <petscerror.h>
201:   void PetscCheckReturnMPI(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)

203:   Collective; No Fortran Support

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

211:   Level: beginner

213:   Note:
214:   Enabled in both optimized and debug builds.

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)

223: /*MC
224:   PetscCheckAbort - Check that a particular condition is true, otherwise prints error and aborts

226:   Synopsis:
227: #include <petscerror.h>
228:   void PetscCheckAbort(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)

230:   Collective; No Fortran Support

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

238:   Level: developer

240:   Notes:
241:   Enabled in both optimized and debug builds.

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.

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)

253: /*MC
254:   PetscAssert - Assert that a particular condition is true

256:   Synopsis:
257: #include <petscerror.h>
258:   void PetscAssert(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)

260:   Collective; No Fortran Support

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

268:   Level: beginner

270:   Notes:
271:   Equivalent to `PetscCheck()` if debugging is enabled, and `PetscAssume(cond)` otherwise.

273:   See `PetscCheck()` for usage and behaviour.

275:   This is needed instead of simply using `assert()` because this correctly handles the collective nature of errors under MPI

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

285: /*MC
286:   PetscAssertAbort - Assert that a particular condition is true, otherwise prints error and aborts

288:   Synopsis:
289: #include <petscerror.h>
290:   void PetscAssertAbort(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)

292:   Collective; No Fortran Support

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

300:   Level: beginner

302:   Note:
303:   Enabled only in debug builds. See `PetscCheckAbort()` for usage.

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

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.

318:   Synopsis:
319: #include <petscerror.h>
320:   void PetscCall(PetscFunction(args))

322:   Not Collective

324:   Input Parameter:
325: . PetscFunction - any PETSc function that returns an error code

327:   Level: beginner

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()`.

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.

337:   Example Usage:
338: .vb
339:   PetscCall(PetscInitiailize(...)); // OK to call even when PETSc is not yet initialized!

341:   struct my_struct
342:   {
343:     void *data;
344:   } my_complex_type;

346:   struct my_struct bar(void)
347:   {
348:     PetscCall(foo(15)); // ERROR PetscErrorCode not convertible to struct my_struct!
349:   }

351:   PetscCall(bar()) // ERROR input not convertible to PetscErrorCode
352: .ve

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

359:   Should not be used to call callback functions provided by users, `PetscCallBack()` should be used in that situation.

361:   `PetscUseTypeMethod()` or `PetscTryTypeMethod()` should be used when calling functions pointers contained in a PETSc object's `ops` array

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.

367:     In the main program and in Fortran subroutines that do not have `ierr` as the final return parameter, one
368:     should use `PetscCallA()`

370:   Example Fortran Usage:
371: .vb
372:   PetscErrorCode ierr
373:   Vec v

375:   ...
376:   PetscCall(VecShift(v, 1.0, ierr))
377:   PetscCallA(VecShift(v, 1.0, ierr))
378: .ve

380: .seealso: `SETERRQ()`, `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`,
381:           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`,
382:           `CHKERRMPI()`, `PetscCallBack()`, `PetscCallAbort()`, `PetscCallVoid()`, `PetscCallNull()`
383: M*/

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`

389:   Synopsis:
390: #include <petscerror.h>
391:   void PetscCallNull(PetscFunction(args))

393:   Not Collective; No Fortran Support

395:   Input Parameter:
396: . PetscFunction - any PETSc function that returns something that can be returned as a `NULL`

398:   Level: developer

400: .seealso: `PetscCall()`, `SETERRQ()`, `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`,
401:           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`,
402:           `CHKERRMPI()`, `PetscCallBack()`, `PetscCallAbort()`, `PetscCallVoid()`, `PetscCall()`
403: M*/

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

409:    Synopsis:
410: #include <petscsys.h>
411:    PetscErrorCode PetscCallA(PetscFunction(arguments, ierr))

413:    Collective

415:    Input Parameter:
416: .  PetscFunction(arguments,ierr) - the call to the function

418:   Level: beginner

420:    Notes:
421:    This should only be used with Fortran. With C/C++, use `PetscCall()` always.

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

426: .seealso: `SETERRQ()`, `SETERRA()`, `SETERRABORT()`, `PetscCall()`, `CHKERRA()`, `PetscCallAbort()`
427: M*/

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.

433:   Synopsis:
434: #include <petscerror.h>
435:   void PetscCallBack(const char *functionname, PetscFunction(args))

437:   Not Collective; No Fortran Support

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

443:   Example Usage:
444: .vb
445:   PetscCallBack("XXX callback to do something", a->callback(...));
446: .ve

448:   Level: developer

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.

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()`.

457:   `PetscCallBack()` should only be called in PETSc when a call is being made to a user provided call-back routine.

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

462: .seealso: `SETERRQ()`, `PetscCheck()`, `PetscCall()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`
463:           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`, `CHKERRMPI()`, `PetscCall()`,  `PetscUseTypeMethod()`, `PetscTryTypeMethod()`
464: M*/

466: /*MC
467:   PetscCallVoid - Like `PetscCall()` but for use in functions that return `void`

469:   Synopsis:
470: #include <petscerror.h>
471:   void PetscCallVoid(PetscFunction(args))

473:   Not Collective; No Fortran Support

475:   Input Parameter:
476: . PetscFunction - any PETSc function that returns an error code

478:   Example Usage:
479: .vb
480:   void foo()
481:   {
482:     KSP ksp;

484:     PetscFunctionBeginUser;
485:     // OK, properly handles PETSc error codes
486:     PetscCallVoid(KSPCreate(PETSC_COMM_WORLD, &ksp));
487:     PetscFunctionReturnVoid();
488:   }

490:   PetscErrorCode bar()
491:   {
492:     KSP ksp;

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

503:   Level: beginner

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.

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.

515: .seealso: `PetscCall()`, `PetscErrorCode`, `PetscCallAbort()`, `PetscCallNull()`
516: M*/

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.

523:   Synopsis:
524: #include <petscerror.h>
525:   void PetscCallReturnMPI(PetscFunction(args))

527:   Not Collective

529:   Input Parameter:
530: . PetscFunction - any PETSc function that returns an error code

532:   Level: advanced

534:   Notes:
535:   Note to be confused with `PetscCallMPI()`.

537:   This is be used in a PETSc-provided MPI callback function, such as `MPI_Comm_delete_attr_function function()`.

539:   Currently, it always returns `MPI_ERR_OTHER` on failure

541: .seealso: `PetscCall()`, `PetscCallMPI()`, `SETERRQ()`, `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`,
542:           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`,
543:           `CHKERRMPI()`, `PetscCallBack()`, `PetscCallAbort()`, `PetscCallVoid()`, `PetscCallNull()`
544: M*/

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

601: /*MC
602:   CHKERRQ - Checks error code returned from PETSc function

604:   Synopsis:
605: #include <petscsys.h>
606:   void CHKERRQ(PetscErrorCode ierr)

608:   Not Collective

610:   Input Parameter:
611: . ierr - nonzero error code

613:   Level: deprecated

615:   Note:
616:   Deprecated in favor of `PetscCall()`. This routine behaves identically to it.

618: .seealso: `PetscCall()`
619: M*/
620: #define CHKERRQ(...) PetscCall(__VA_ARGS__)
621: #define CHKERRV(...) PetscCallVoid(__VA_ARGS__)

623: PETSC_EXTERN void PetscMPIErrorString(PetscMPIInt, size_t, char *);

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`

629:   Synopsis:
630: #include <petscerror.h>
631:   void PetscCallMPI(MPI_Function(args))

633:   Not Collective

635:   Input Parameter:
636: . MPI_Function - an MPI function that returns an MPI error code

638:   Level: beginner

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.

647:   This routine can only be used in functions returning `PetscErrorCode` themselves. If the
648:   calling function returns a different type, use `PetscCallMPIAbort()` instead.

650:   Example Usage:
651: .vb
652:   PetscCallMPI(MPI_Comm_size(...)); // OK, calling MPI function

654:   PetscCallMPI(PetscFunction(...)); // ERROR, use PetscCall() instead!
655: .ve

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.

661:     In the main program and in Fortran subroutines that do not have ierr as the final return parameter one
662:     should use `PetscCallMPIA()`

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

671:   PetscCallMPI(MPI_Comm_size(...,eflag)) ! ERROR, final argument must be ierr
672: .ve

674: .seealso: `SETERRMPI()`, `PetscCall()`, `PetscCallReturnMPI()`, `SETERRQ()`, `SETERRABORT()`, `PetscCallAbort()`,
675:           `PetscCallMPIAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
676:           `PetscError()`, `CHKMEMQ`, `PetscCallMPINull()`
677: M*/

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.

683:   Synopsis:
684: #include <petscerror.h>
685:   void PetscCallMPIReturnMPI(MPI_Function(args))

687:   Not Collective

689:   Input Parameter:
690: . MPI_Function - an MPI function that returns an MPI error code

692:   Level: advanced

694: .seealso: `SETERRMPI()`, `PetscCall()`, `PetscCallMPI()`, `PetscCallReturnMPI()`, `SETERRQ()`, `SETERRABORT()`, `PetscCallAbort()`,
695:           `PetscCallMPIAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
696:           `PetscError()`, `CHKMEMQ`, `PetscCallMPINull()`
697: M*/

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`

703:   Synopsis:
704: #include <petscerror.h>
705:   void PetscCallMPINull(MPI_Function(args))

707:   Not Collective; No Fortran Support

709:   Input Parameter:
710: . MPI_Function - an MPI function that returns an MPI error code

712:   Level: beginner

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.

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.

722:   Example Usage:
723: .vb
724:   PetscCallMPINull(MPI_Comm_size(...)); // OK, calling MPI function

726:   PetscCallMPI(PetscFunction(...)); // ERROR, use PetscCall() instead!
727: .ve

729: .seealso: `SETERRMPI()`, `PetscCall()`, `SETERRQ()`, `SETERRABORT()`, `PetscCallAbort()`,
730:           `PetscCallMPIAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
731:           `PetscError()`, `CHKMEMQ`, `PetscCallMPI()`
732: M*/

734: /*MC
735:   PetscCallMPIAbort - Like `PetscCallMPI()` but calls `MPI_Abort()` on error

737:   Synopsis:
738: #include <petscerror.h>
739:   void PetscCallMPIAbort(MPI_Comm comm, MPI_Function(args))

741:   Not Collective

743:   Input Parameters:
744: + comm         - the MPI communicator to abort on
745: - MPI_Function - an MPI function that returns an MPI error code

747:   Level: beginner

749:   Notes:
750:   Usage is identical to `PetscCallMPI()`. See `PetscCallMPI()` for detailed discussion.

752:   This routine may be used in functions returning `void` or other non-`PetscErrorCode` types.

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.

758:   Developer Note:
759:   This should have the same name in Fortran.

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)

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

790: /*MC
791:   CHKERRMPI - Checks error code returned from MPI calls, if non-zero it calls the error
792:   handler and then returns

794:   Synopsis:
795: #include <petscerror.h>
796:   void CHKERRMPI(PetscErrorCode ierr)

798:   Not Collective

800:   Input Parameter:
801: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h

803:   Level: deprecated

805:   Note:
806:   Deprecated in favor of `PetscCallMPI()`. This routine behaves identically to it.

808: .seealso: `PetscCallMPI()`
809: M*/
810: #define CHKERRMPI(...) PetscCallMPI(__VA_ARGS__)

812: /*MC
813:   PetscCallAbort - Checks error code returned from PETSc function, if non-zero it aborts immediately by calling `MPI_Abort()`

815:   Synopsis:
816: #include <petscerror.h>
817:   void PetscCallAbort(MPI_Comm comm, PetscErrorCode ierr)

819:   Collective

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

825:   Level: intermediate

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.

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.

837:   Example Usage:
838: .vb
839:   PetscErrorCode boom(void) { return PETSC_ERR_MEM; }

841:   void foo(void)
842:   {
843:     PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
844:   }

846:   double bar(void)
847:   {
848:     PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
849:   }

851:   PetscCallAbort(MPI_COMM_NULL,boom()); // ERROR, communicator should be valid

853:   struct baz
854:   {
855:     baz()
856:     {
857:       PetscCallAbort(PETSC_COMM_SELF,boom()); // OK
858:     }

860:     ~baz()
861:     {
862:       PetscCallAbort(PETSC_COMM_SELF,boom()); // OK (in fact the only way to handle PETSc errors)
863:     }
864:   };
865: .ve

867:   Fortran Note:
868:   Use `PetscCallA()`.

870:   Developer Note:
871:   This should have the same name in Fortran as in C.

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

902: /*MC
903:   CHKERRABORT - Checks error code returned from PETSc function. If non-zero it aborts immediately.

905:   Synopsis:
906: #include <petscerror.h>
907:   void CHKERRABORT(MPI_Comm comm, PetscErrorCode ierr)

909:   Not Collective

911:   Input Parameters:
912: + comm - the MPI communicator
913: - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h

915:   Level: deprecated

917:   Note:
918:   Deprecated in favor of `PetscCallAbort()`. This routine behaves identically to it.

920: .seealso: `PetscCallAbort()`, `PetscErrorCode`
921: M*/
922: #define CHKERRABORT(comm, ...) PetscCallAbort(comm, __VA_ARGS__)
923: #define CHKERRCONTINUE(...)    PetscCallContinue(__VA_ARGS__)

925: /*MC
926:    CHKERRA - Fortran-only replacement for use of `CHKERRQ()` in the main program, which aborts immediately

928:    Synopsis:
929: #include <petscsys.h>
930:    PetscErrorCode CHKERRA(PetscErrorCode ierr)

932:    Not Collective

934:    Input Parameter:
935: .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h

937:    Level: deprecated

939:    Note:
940:    This macro is rarely needed, normal usage is `PetscCallA()` in the main Fortran program.

942:    Developer Note:
943:    Why isn't this named `CHKERRABORT()` in Fortran?

945: .seealso: `PetscCall()`, `PetscCallA()`, `PetscCallAbort()`, `CHKERRQ()`, `SETERRA()`, `SETERRQ()`, `SETERRABORT()`
946: M*/

948: PETSC_EXTERN PetscBool petscwaitonerrorflg;
949: PETSC_EXTERN PetscBool petscindebugger;
950: PETSC_EXTERN PetscBool petscabortmpifinalize;

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

970: /*MC
971:    PETSCABORT - Call `MPI_Abort()` with an informative error code

973:    Synopsis:
974: #include <petscsys.h>
975:    PETSCABORT(MPI_Comm comm, PetscErrorCode ierr)

977:    Collective; No Fortran Support

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

983:    Level: advanced

985:    Notes:
986:    If the option `-start_in_debugger` was used then this calls `abort()` to stop the program in the debugger.

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()`.

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.

996:    Developer Note:
997:    Should the other abort calls also pass through this call instead of calling `MPI_Abort()` directly?

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

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

1023:   Synopsis:
1024: #include <petscerror.h>
1025:   void PetscCallThrow(PetscErrorCode ierr)

1027:   Not Collective

1029:   Input Parameter:
1030: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h

1032:   Level: beginner

1034:   Notes:
1035:   Requires PETSc to be configured with clanguage of c++. Throws a std::runtime_error() on error.

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.

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)

1051:   /*MC
1052:   CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception

1054:   Synopsis:
1055: #include <petscerror.h>
1056:   void CHKERRXX(PetscErrorCode ierr)

1058:   Not Collective

1060:   Input Parameter:
1061: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h

1063:   Level: deprecated

1065:   Note:
1066:   Deprecated in favor of `PetscCallThrow()`. This routine behaves identically to it.

1068: .seealso: `PetscCallThrow()`
1069: M*/
1070:   #define CHKERRXX(...) PetscCallThrow(__VA_ARGS__)
1071: #endif

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)

1083: /*MC
1084:   PetscCallCXX - Checks C++ function calls and if they throw an exception, catch it and then
1085:   return a PETSc error code

1087:   Synopsis:
1088: #include <petscerror.h>
1089:   void PetscCallCXX(...) noexcept;

1091:   Not Collective

1093:   Input Parameter:
1094: . __VA_ARGS__ - An arbitrary expression

1096:   Level: beginner

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.

1109:   If you cannot return a `PetscErrorCode` use `PetscCallCXXAbort()` instead.

1111:   Example Usage:
1112: .vb
1113:   void foo(void) { throw std::runtime_error("error"); }

1115:   void bar()
1116:   {
1117:     PetscCallCXX(foo()); // ERROR bar() does not return PetscErrorCode
1118:   }

1120:   PetscErrorCode baz()
1121:   {
1122:     PetscCallCXX(foo()); // OK

1124:     PetscCallCXX(
1125:       bar();
1126:       foo(); // OK multiple statements allowed
1127:     );
1128:   }

1130:   struct bop
1131:   {
1132:     bop()
1133:     {
1134:       PetscCallCXX(foo()); // ERROR returns PetscErrorCode, cannot be used in constructors
1135:     }
1136:   };

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

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__)

1153: /*MC
1154:   PetscCallCXXAbort - Like `PetscCallCXX()` but calls `MPI_Abort()` instead of returning an
1155:   error-code

1157:   Synopsis:
1158: #include <petscerror.h>
1159:   void PetscCallCXXAbort(MPI_Comm comm, ...) noexcept;

1161:   Collective; No Fortran Support

1163:   Input Parameters:
1164: + comm        - The MPI communicator to abort on
1165: - __VA_ARGS__ - An arbitrary expression

1167:   Level: beginner

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.

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.

1177:   If the routine _can_ return an error-code it is highly advised to use `PetscCallCXX()`
1178:   instead.

1180:   See `PetscCallCXX()` for additional discussion.

1182:   Example Usage:
1183: .vb
1184:   class Foo
1185:   {
1186:     std::vector<int> data_;

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:   };

1197:   std::vector<int> bar()
1198:   {
1199:     std::vector<int> v;

1201:     PetscFunctionBegin;
1202:     // OK!
1203:     PetscCallCXXAbort(PETSC_COMM_SELF, v.emplace_back(1));
1204:     PetscFunctionReturn(v);
1205:   }

1207:   PetscErrorCode baz()
1208:   {
1209:     std::vector<int> v;

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

1218: .seealso: `PetscCallCXX()`, `SETERRABORT()`, `PetscCallAbort()`
1219: M*/
1220: #define PetscCallCXXAbort(comm, ...) PetscCallCXX_Private(SETERRABORT, comm, __VA_ARGS__)

1222: /*MC
1223:   CHKERRCXX - Checks C++ function calls and if they throw an exception, catch it and then
1224:   return a PETSc error code

1226:   Synopsis:
1227: #include <petscerror.h>
1228:   void CHKERRCXX(func) noexcept;

1230:   Not Collective

1232:   Input Parameter:
1233: . func - C++ function calls

1235:   Level: deprecated

1237:   Note:
1238:   Deprecated in favor of `PetscCallCXX()`. This routine behaves identically to it.

1240: .seealso: `PetscCallCXX()`
1241: M*/
1242: #define CHKERRCXX(...) PetscCallCXX(__VA_ARGS__)

1244: /*MC
1245:    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected

1247:    Synopsis:
1248: #include <petscsys.h>
1249:    CHKMEMQ;

1251:    Not Collective

1253:    Level: beginner

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.

1260:    Must run with the option `-malloc_debug` (`-malloc_test` in debug mode; or if `PetscMallocSetDebug()` called) to enable this option

1262:    Once the error handler is called the calling function is then returned from with the given error code.

1264:    By defaults prints location where memory that is corrupted was allocated.

1266:    Use `CHKMEMA` for functions that return `void`

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

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

1285:   Level: advanced

1287:   Note:
1288:   `PETSC_ERROR_IN_CXX` indicates the error was detected in C++ and an exception should be generated

1290:   Developer Note:
1291:   This is currently used to decide when to print the detailed information about the run in `PetscTraceBackErrorHandler()`

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;

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);

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: }

1328: /*MC
1329:     PetscErrorPrintf - Prints error messages.

1331:    Synopsis:
1332: #include <petscsys.h>
1333:      PetscErrorCode (*PetscErrorPrintf)(const char format[], ...);

1335:     Not Collective; No Fortran Support

1337:     Input Parameter:
1338: .   format - the usual `printf()` format string

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.)

1344:    Level: developer

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

1362: .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscHelpPrintf()`, `PetscPrintf()`, `PetscPushErrorHandler()`, `PetscVFPrintf()`, `PetscHelpPrintf()`
1363: M*/
1364: PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);

1366: /*E
1367:      PetscFPTrap - types of floating point exceptions that may be trapped

1369:      Currently only `PETSC_FP_TRAP_OFF` and `PETSC_FP_TRAP_ON` are handled. All others are treated as `PETSC_FP_TRAP_ON`.

1371:      Level: intermediate

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       = (PETSC_FP_TRAP_INDIV | PETSC_FP_TRAP_FLTOPERR | PETSC_FP_TRAP_FLTOVF | PETSC_FP_TRAP_FLTDIV | PETSC_FP_TRAP_FLTINEX)
1384: } PetscFPTrap;

1386: PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
1387: PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
1388: PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
1389: PETSC_EXTERN PetscErrorCode PetscDetermineInitialFPTrap(void);

1391: /*
1392:       Allows the code to build a stack frame as it runs
1393: */

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

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

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

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)

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)

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] == (const char *)(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)

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.

1485:    Synopsis:
1486: #include <petscsys.h>
1487:    void PetscStackPushNoCheck(char *funct,int petsc_routine,PetscBool hot);

1489:    Not Collective

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

1496:    Level: developer

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.

1503:    This version does not check the memory corruption (an expensive operation), use `PetscStackPush()` to check the memory.

1505:    Use `PetscStackPushExternal()` for a function call that is about to be made to a non-PETSc or user function (such as BLAS etc).

1507:    The default stack is a global variable called `petscstack`.

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)

1520:   /*MC
1521:    PetscStackUpdateLine - in a function that has a `PetscFunctionBegin` or `PetscFunctionBeginUser` updates the stack line number to the
1522:    current line number.

1524:    Synopsis:
1525: #include <petscsys.h>
1526:    void PetscStackUpdateLine

1528:    Not Collective

1530:    Level: developer

1532:    Notes:
1533:    Using `PetscCall()` and friends automatically handles this process

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.

1539:    The default stack is a global variable called `petscstack`.

1541:    This is used by `PetscCall()` and is otherwise not like to be needed

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)

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.

1555:    Synopsis:
1556: #include <petscsys.h>
1557:    void PetscStackPushExternal(char *funct);

1559:    Not Collective

1561:    Input Parameter:
1562: .  funct - the function name

1564:    Level: developer

1566:    Notes:
1567:    Using `PetscCallExternal()` and friends automatically handles this process

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.

1573:    The default stack is a global variable called `petscstack`.

1575:    This is to be used when calling an external package function such as a BLAS function.

1577:    This also updates the stack line number for the current stack function.

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)

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.

1592:    Synopsis:
1593: #include <petscsys.h>
1594:    void PetscStackPopNoCheck(char *funct);

1596:    Not Collective

1598:    Input Parameter:
1599: .   funct - the function name

1601:    Level: developer

1603:    Notes:
1604:    Using `PetscCall()`, `PetscCallExternal()`, `PetscCallBack()` and friends negates the need to call this

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.

1610:    The default stack is a global variable called `petscstack`.

1612:    Developer Note:
1613:    `PetscStackPopNoCheck()` takes a function argument while  `PetscStackPop` does not, this difference is likely just historical.

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)

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)

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);

1641:    Synopsis:
1642: #include <petscsys.h>
1643:    void PetscFunctionBegin;

1645:    Not Collective; No Fortran Support

1647:    Usage:
1648: .vb
1649:      int something;

1651:      PetscFunctionBegin;
1652: .ve

1654:    Level: developer

1656:    Note:
1657:    Use `PetscFunctionBeginUser` for application codes.

1659: .seealso: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`

1661: M*/
1662:   #define PetscFunctionBegin \
1663:     do { \
1664:       PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 1, PETSC_FALSE); \
1665:       PetscRegister__FUNCT__(); \
1666:     } while (0)

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.

1672:    Synopsis:
1673: #include <petscsys.h>
1674:    void PetscFunctionBeginHot;

1676:    Not Collective; No Fortran Support

1678:    Usage:
1679: .vb
1680:      int something;

1682:      PetscFunctionBeginHot;
1683: .ve

1685:    Level: developer

1687: .seealso: `PetscFunctionBegin`, `PetscFunctionReturn()`, `PetscStackPushNoCheck()`

1689: M*/
1690:   #define PetscFunctionBeginHot \
1691:     do { \
1692:       PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 1, PETSC_TRUE); \
1693:       PetscRegister__FUNCT__(); \
1694:     } while (0)

1696:   /*MC
1697:    PetscFunctionBeginUser - First executable line of user provided routines

1699:    Synopsis:
1700: #include <petscsys.h>
1701:    void PetscFunctionBeginUser;

1703:    Not Collective; No Fortran Support

1705:    Usage:
1706: .vb
1707:      int something;

1709:      PetscFunctionBeginUser;
1710: .ve

1712:    Level: intermediate

1714:    Notes:
1715:    Functions that incorporate this must call `PetscFunctionReturn()` instead of return except for main().

1717:    May be used before `PetscInitialize()`

1719:    This is identical to `PetscFunctionBegin` except it labels the routine as a user
1720:    routine instead of as a PETSc library routine.

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)

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.

1734:    Synopsis:
1735: #include <petscsys.h>
1736:    void PetscStackPush(char *funct)

1738:    Not Collective

1740:    Input Parameter:
1741: .  funct - the function name

1743:    Level: developer

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.

1750:    The default stack is a global variable called `petscstack`.

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)

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.

1765:    Synopsis:
1766: #include <petscsys.h>
1767:    void PetscStackPop

1769:    Not Collective

1771:    Level: developer

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.

1778:    The default stack is a global variable called `petscstack`.

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)

1788:   /*MC
1789:    PetscFunctionReturn - Last executable line of each PETSc function used for error
1790:    handling. Replaces `return()`.

1792:    Synopsis:
1793: #include <petscerror.h>
1794:    void PetscFunctionReturn(...)

1796:    Not Collective; No Fortran Support

1798:    Level: beginner

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.

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.

1808:    Any routine which returns via `PetscFunctionReturn()` must begin with a corresponding
1809:    `PetscFunctionBegin`.

1811:    For routines which return `void` use `PetscFunctionReturnVoid()` instead.

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

1823:    May return any arbitrary type\:
1824: .vb
1825:   struct Foo
1826:   {
1827:     int x;
1828:   };

1830:   struct Foo make_foo(int value)
1831:   {
1832:     struct Foo f;

1834:     PetscFunctionBegin;
1835:     f.x = value;
1836:     PetscFunctionReturn(f);
1837:   }
1838: .ve

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)

1849:   /*MC
1850:   PetscFunctionReturnVoid - Like `PetscFunctionReturn()` but returns `void`

1852:   Synopsis:
1853: #include <petscerror.h>
1854:   void PetscFunctionReturnVoid()

1856:   Not Collective

1858:   Level: beginner

1860:   Note:
1861:   Behaves identically to `PetscFunctionReturn()` except that it returns `void`. That is, this
1862:   macro culminates with `return`.

1864:   Example Usage:
1865: .vb
1866:   void foo()
1867:   {
1868:     PetscFunctionBegin; // must start with PetscFunctionBegin!
1869:     bar();
1870:     baz();
1871:     PetscFunctionReturnVoid();
1872:   }
1873: .ve

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 */

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.

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)

1911:    Level: developer

1913:    Notes:
1914:    Often one should use `PetscCallExternal()` instead. This routine is intended for external library routines that DO NOT return error codes

1916:    In debug mode this also checks the memory for corruption at the end of the function call.

1918:    Certain external packages, such as BLAS/LAPACK may have their own macros, `PetscCallBLAS()` for managing the call, error checking, etc.

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.

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)

1932:   /*MC
1933:     PetscCallExternal - Calls an external library routine that returns an error code after pushing the name of the routine on the stack.

1935:    Input Parameters:
1936: +  func - name of the routine
1937: -  args - arguments to the routine

1939:    Level: developer

1941:    Notes:
1942:    This is intended for external package routines that return error codes. Use `PetscStackCallExternalVoid()` for those that do not.

1944:    In debug mode this also checks the memory for corruption at the end of the function call.

1946:    Assumes the error return code of the function is an integer and that a value of 0 indicates success

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.

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)

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

1964:    Input Parameters:
1965: +  func - name of the routine
1966: -  args - arguments to the routine

1968:    Level: developer

1970:    Notes:
1971:    This is intended for external package routines that return error codes. Use `PetscStackCallExternalVoid()` for those that do not.

1973:    In debug mode this also checks the memory for corruption at the end of the function call.

1975:    Assumes the error return code of the function is an integer and that a value of 0 indicates success

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.

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 */