Actual source code: petscerror.h
1: /*
2: Contains all error handling interfaces for PETSc.
3: */
4: #pragma once
5: // IWYU pragma: private, include "petscsys.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 <petscsys.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 <petscsys.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 <petscsys.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 <petscsys.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 <petscsys.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 <petscsys.h>
320: void PetscCall(PetscFunction(...))
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 <petscsys.h>
391: void PetscCallNull(PetscFunction(...))
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 <petscsys.h>
435: void PetscCallBack(const char *functionname, PetscFunction(...))
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 <petscsys.h>
471: void PetscCallVoid(PetscFunction(...))
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 <petscsys.h>
525: void PetscCallReturnMPI(PetscFunction(...))
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 PetscDefined(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 <petscsys.h>
631: void PetscCallMPI(MPI_Function(...))
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 <petscsys.h>
685: void PetscCallMPIReturnMPI(MPI_Function(...))
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 <petscsys.h>
705: void PetscCallMPINull(MPI_Function(...))
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 <petscsys.h>
739: void PetscCallMPIAbort(MPI_Comm comm, MPI_Function(...))
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 PetscDefined(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 the error code returned from MPI calls, if different from `MPI_SUCCESS` it calls the error handler and then returns
793: Synopsis:
794: #include <petscsys.h>
795: void CHKERRMPI(PetscMPIInt ierr)
797: Not Collective
799: Input Parameter:
800: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
802: Level: deprecated
804: Note:
805: Deprecated in favor of `PetscCallMPI()`. This routine behaves identically to it.
807: .seealso: `PetscCallMPI()`
808: M*/
809: #define CHKERRMPI(...) PetscCallMPI(__VA_ARGS__)
811: /*MC
812: PetscCallAbort - Checks error code returned from PETSc function, if non-zero it aborts immediately by calling `MPI_Abort()`
814: Synopsis:
815: #include <petscsys.h>
816: void PetscCallAbort(MPI_Comm comm, PetscErrorCode ierr)
818: Collective
820: Input Parameters:
821: + comm - the MPI communicator on which to abort
822: - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
824: Level: intermediate
826: Notes:
827: This macro has identical type and usage semantics to `PetscCall()` with the important caveat
828: that this macro does not return. Instead, if ierr is nonzero it calls the PETSc error handler
829: and then immediately calls `MPI_Abort()`. It can therefore be used anywhere.
831: As per `MPI_Abort()` semantics the communicator passed must be valid, although there is currently
832: no attempt made at handling any potential errors from `MPI_Abort()`. Note that while
833: `MPI_Abort()` is required to terminate only those processes which reside on comm, it is often
834: the case that `MPI_Abort()` terminates *all* processes.
836: Example Usage:
837: .vb
838: PetscErrorCode boom(void) { return PETSC_ERR_MEM; }
840: void foo(void)
841: {
842: PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
843: }
845: double bar(void)
846: {
847: PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
848: }
850: PetscCallAbort(MPI_COMM_NULL,boom()); // ERROR, communicator should be valid
852: struct baz
853: {
854: baz()
855: {
856: PetscCallAbort(PETSC_COMM_SELF,boom()); // OK
857: }
859: ~baz()
860: {
861: PetscCallAbort(PETSC_COMM_SELF,boom()); // OK (in fact the only way to handle PETSc errors)
862: }
863: };
864: .ve
866: Fortran Note:
867: Use `PetscCallA()`.
869: Developer Note:
870: This should have the same name in Fortran as in C.
872: .seealso: `SETERRABORT()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`,
873: `SETERRQ()`, `CHKMEMQ`, `PetscCallMPI()`, `PetscCallCXXAbort()`
874: M*/
875: #if PetscDefined(CLANG_STATIC_ANALYZER)
876: void PetscCallAbort(MPI_Comm, PetscErrorCode);
877: void PetscCallContinue(PetscErrorCode);
878: #else
879: #define PetscCallAbort(comm, ...) \
880: do { \
881: PetscErrorCode ierr_petsc_call_abort_; \
882: PetscStackUpdateLine; \
883: ierr_petsc_call_abort_ = __VA_ARGS__; \
884: if (PetscUnlikely(ierr_petsc_call_abort_ != PETSC_SUCCESS)) { \
885: ierr_petsc_call_abort_ = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_abort_, PETSC_ERROR_REPEAT, " "); \
886: (void)MPI_Abort(comm, (PetscMPIInt)ierr_petsc_call_abort_); \
887: } \
888: } while (0)
889: #define PetscCallContinue(...) \
890: do { \
891: PetscErrorCode ierr_petsc_call_continue_; \
892: PetscStackUpdateLine; \
893: ierr_petsc_call_continue_ = __VA_ARGS__; \
894: if (PetscUnlikely(ierr_petsc_call_continue_ != PETSC_SUCCESS)) { \
895: ierr_petsc_call_continue_ = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_continue_, PETSC_ERROR_REPEAT, " "); \
896: (void)ierr_petsc_call_continue_; \
897: } \
898: } while (0)
899: #endif
901: /*MC
902: CHKERRABORT - Checks error code returned from PETSc function. If non-zero it aborts immediately.
904: Synopsis:
905: #include <petscsys.h>
906: void CHKERRABORT(MPI_Comm comm, PetscErrorCode ierr)
908: Not Collective
910: Input Parameters:
911: + comm - the MPI communicator
912: - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
914: Level: deprecated
916: Note:
917: Deprecated in favor of `PetscCallAbort()`. This routine behaves identically to it.
919: .seealso: `PetscCallAbort()`, `PetscErrorCode`
920: M*/
921: #define CHKERRABORT(comm, ...) PetscCallAbort(comm, __VA_ARGS__)
922: #define CHKERRCONTINUE(...) PetscCallContinue(__VA_ARGS__)
924: /*MC
925: CHKERRA - Fortran-only replacement for use of `CHKERRQ()` in the main program, which aborts immediately
927: Synopsis:
928: #include <petscsys.h>
929: PetscErrorCode CHKERRA(PetscErrorCode ierr)
931: Not Collective
933: Input Parameter:
934: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
936: Level: deprecated
938: Note:
939: This macro is rarely needed, normal usage is `PetscCallA()` in the main Fortran program.
941: Developer Note:
942: Why isn't this named `CHKERRABORT()` in Fortran?
944: .seealso: `PetscCall()`, `PetscCallA()`, `PetscCallAbort()`, `CHKERRQ()`, `SETERRA()`, `SETERRQ()`, `SETERRABORT()`
945: M*/
947: PETSC_EXTERN PetscBool petscwaitonerrorflg;
948: PETSC_EXTERN PetscBool petscindebugger;
949: PETSC_EXTERN PetscBool petscabortmpifinalize;
951: #if PetscDefined(CLANG_STATIC_ANALYZER)
952: void PETSCABORTWITHERR_Private(MPI_Comm, PetscErrorCode);
953: #else
954: #define PETSCABORTWITHIERR_Private(comm, ierr) \
955: do { \
956: PetscMPIInt size_; \
957: (void)MPI_Comm_size(comm, &size_); \
958: if (PetscCIEnabledPortableErrorOutput && (size_ == PetscGlobalSize || petscabortmpifinalize) && (ierr) != PETSC_ERR_SIG) { \
959: (void)MPI_Finalize(); \
960: exit(0); \
961: } else if (PetscCIEnabledPortableErrorOutput && PetscGlobalSize == 1) { \
962: exit(0); \
963: } else { \
964: (void)MPI_Abort(comm, (PetscMPIInt)(ierr)); \
965: } \
966: } while (0)
967: #endif
969: /*MC
970: PETSCABORT - Call `MPI_Abort()` with an informative error code
972: Synopsis:
973: #include <petscsys.h>
974: PETSCABORT(MPI_Comm comm, PetscErrorCode ierr)
976: Collective; No Fortran Support
978: Input Parameters:
979: + comm - An MPI communicator, so that the error can be collective
980: - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
982: Level: advanced
984: Notes:
985: If the option `-start_in_debugger` was used then this calls `abort()` to stop the program in the debugger.
987: if `PetscCIEnabledPortableErrorOutput` is set, which means the code is running in the PETSc test harness (make test),
988: and `comm` is `MPI_COMM_WORLD` it strives to exit cleanly without calling `MPI_Abort()` and instead calling `MPI_Finalize()`.
990: This is currently only used when an error propagates up to the C `main()` program and is detected by a `PetscCall()`, `PetscCallMPI()`,
991: or is set in `main()` with `SETERRQ()`. Abort calls such as `SETERRABORT()`,
992: `PetscCheckAbort()`, `PetscCallMPIAbort()`, and `PetscCallAbort()` always call `MPI_Abort()` and do not have any special
993: handling for the test harness.
995: Developer Note:
996: Should the other abort calls also pass through this call instead of calling `MPI_Abort()` directly?
998: .seealso: `PetscError()`, `PetscCall()`, `SETERRABORT()`, `PetscCheckAbort()`, `PetscCallMPIAbort()`, `PetscCall()`, `PetscCallMPI()`,
999: `PetscCallAbort()`, `MPI_Abort()`, `PetscErrorCode`
1000: M*/
1001: #if PetscDefined(CLANG_STATIC_ANALYZER)
1002: void PETSCABORT(MPI_Comm, PetscErrorCode);
1003: #else
1004: #define PETSCABORT(comm, ...) \
1005: do { \
1006: PetscErrorCode ierr_petsc_abort_; \
1007: if (petscwaitonerrorflg) ierr_petsc_abort_ = PetscSleep(1000); \
1008: if (petscindebugger) abort(); \
1009: else { \
1010: ierr_petsc_abort_ = __VA_ARGS__; \
1011: PETSCABORTWITHIERR_Private(comm, ierr_petsc_abort_); \
1012: } \
1013: } while (0)
1014: #endif
1016: #if PetscDefined(CLANGUAGE_CXX)
1017: /*MC
1018: PetscCallThrow - Checks error code, if non-zero it calls the C++ error handler which throws
1019: an exception
1021: Synopsis:
1022: #include <petscsys.h>
1023: void PetscCallThrow(PetscErrorCode ierr)
1025: Not Collective
1027: Input Parameter:
1028: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
1030: Level: beginner
1032: Notes:
1033: Requires PETSc to be configured with clanguage of c++. Throws a std::runtime_error() on error.
1035: Once the error handler throws the exception you can use `PetscCallVoid()` which returns without
1036: an error code (bad idea since the error is ignored) or `PetscCallAbort()` to have `MPI_Abort()`
1037: called immediately.
1039: .seealso: `SETERRQ()`, `PetscCall()`, `SETERRABORT()`, `PetscCallAbort()`, `PetscTraceBackErrorHandler()`,
1040: `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
1041: M*/
1042: #define PetscCallThrow(...) \
1043: do { \
1044: PetscStackUpdateLine; \
1045: PetscErrorCode ierr_petsc_call_throw_ = __VA_ARGS__; \
1046: 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); \
1047: } while (0)
1049: /*MC
1050: CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
1052: Synopsis:
1053: #include <petscsys.h>
1054: void CHKERRXX(PetscErrorCode ierr)
1056: Not Collective
1058: Input Parameter:
1059: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
1061: Level: deprecated
1063: Note:
1064: Deprecated in favor of `PetscCallThrow()`. This routine behaves identically to it.
1066: .seealso: `PetscCallThrow()`
1067: M*/
1068: #define CHKERRXX(...) PetscCallThrow(__VA_ARGS__)
1069: #endif
1071: #define PetscCallCXX_Private(__SETERR_FUNC__, __COMM__, ...) \
1072: do { \
1073: PetscStackUpdateLine; \
1074: try { \
1075: __VA_ARGS__; \
1076: } catch (const std::exception &e) { \
1077: __SETERR_FUNC__(__COMM__, PETSC_ERR_LIB, "%s", e.what()); \
1078: } \
1079: } while (0)
1081: /*MC
1082: PetscCallCXX - Checks C++ function calls and if they throw an exception, catch it and then
1083: return a PETSc error code
1085: Synopsis:
1086: #include <petscsys.h>
1087: void PetscCallCXX(...) noexcept;
1089: Not Collective
1091: Input Parameter:
1092: . __VA_ARGS__ - An arbitrary expression
1094: Level: beginner
1096: Notes:
1097: `PetscCallCXX(...)` is a macro replacement for
1098: .vb
1099: try {
1100: __VA_ARGS__;
1101: } catch (const std::exception& e) {
1102: return ConvertToPetscErrorCode(e);
1103: }
1104: .ve
1105: Due to the fact that it catches any (reasonable) exception, it is essentially noexcept.
1107: If you cannot return a `PetscErrorCode` use `PetscCallCXXAbort()` instead.
1109: Example Usage:
1110: .vb
1111: void foo(void) { throw std::runtime_error("error"); }
1113: void bar()
1114: {
1115: PetscCallCXX(foo()); // ERROR bar() does not return PetscErrorCode
1116: }
1118: PetscErrorCode baz()
1119: {
1120: PetscCallCXX(foo()); // OK
1122: PetscCallCXX(
1123: bar();
1124: foo(); // OK multiple statements allowed
1125: );
1126: }
1128: struct bop
1129: {
1130: bop()
1131: {
1132: PetscCallCXX(foo()); // ERROR returns PetscErrorCode, cannot be used in constructors
1133: }
1134: };
1136: // ERROR contains do-while, cannot be used as function-try block
1137: PetscErrorCode qux() PetscCallCXX(
1138: bar();
1139: baz();
1140: foo();
1141: return 0;
1142: )
1143: .ve
1145: .seealso: `PetscCallCXXAbort()`, `PetscCallThrow()`, `SETERRQ()`, `PetscCall()`,
1146: `SETERRABORT()`, `PetscCallAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
1147: `PetscError()`, `CHKMEMQ`
1148: M*/
1149: #define PetscCallCXX(...) PetscCallCXX_Private(SETERRQ, PETSC_COMM_SELF, __VA_ARGS__)
1151: /*MC
1152: PetscCallCXXAbort - Like `PetscCallCXX()` but calls `MPI_Abort()` instead of returning an
1153: error-code
1155: Synopsis:
1156: #include <petscsys.h>
1157: void PetscCallCXXAbort(MPI_Comm comm, ...) noexcept;
1159: Collective; No Fortran Support
1161: Input Parameters:
1162: + comm - The MPI communicator to abort on
1163: - __VA_ARGS__ - An arbitrary expression
1165: Level: beginner
1167: Notes:
1168: This macro may be used to check C++ expressions for exceptions in cases where you cannot
1169: return an error code. This includes constructors, destructors, copy/move assignment functions
1170: or constructors among others.
1172: If an exception is caught, the macro calls `SETERRABORT()` on `comm`. The exception must
1173: derive from `std::exception` in order to be caught.
1175: If the routine _can_ return an error-code it is highly advised to use `PetscCallCXX()`
1176: instead.
1178: See `PetscCallCXX()` for additional discussion.
1180: Example Usage:
1181: .vb
1182: class Foo
1183: {
1184: std::vector<int> data_;
1186: public:
1187: // normally std::vector::reserve() may raise an exception, but since we handle it with
1188: // PetscCallCXXAbort() we may mark this routine as noexcept!
1189: Foo() noexcept
1190: {
1191: PetscCallCXXAbort(PETSC_COMM_SELF, data_.reserve(10));
1192: }
1193: };
1195: std::vector<int> bar()
1196: {
1197: std::vector<int> v;
1199: PetscFunctionBegin;
1200: // OK!
1201: PetscCallCXXAbort(PETSC_COMM_SELF, v.emplace_back(1));
1202: PetscFunctionReturn(v);
1203: }
1205: PetscErrorCode baz()
1206: {
1207: std::vector<int> v;
1209: PetscFunctionBegin;
1210: // WRONG! baz() returns a PetscErrorCode, prefer PetscCallCXX() instead
1211: PetscCallCXXAbort(PETSC_COMM_SELF, v.emplace_back(1));
1212: PetscFunctionReturn(PETSC_SUCCESS);
1213: }
1214: .ve
1216: .seealso: `PetscCallCXX()`, `SETERRABORT()`, `PetscCallAbort()`
1217: M*/
1218: #define PetscCallCXXAbort(comm, ...) PetscCallCXX_Private(SETERRABORT, comm, __VA_ARGS__)
1220: /*MC
1221: CHKERRCXX - Checks C++ function calls and if they throw an exception, catch it and then
1222: return a PETSc error code
1224: Synopsis:
1225: #include <petscsys.h>
1226: void CHKERRCXX(func) noexcept;
1228: Not Collective
1230: Input Parameter:
1231: . func - C++ function calls
1233: Level: deprecated
1235: Note:
1236: Deprecated in favor of `PetscCallCXX()`. This routine behaves identically to it.
1238: .seealso: `PetscCallCXX()`
1239: M*/
1240: #define CHKERRCXX(...) PetscCallCXX(__VA_ARGS__)
1242: /*MC
1243: CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
1245: Synopsis:
1246: #include <petscsys.h>
1247: CHKMEMQ;
1249: Not Collective
1251: Level: beginner
1253: Notes:
1254: We recommend using Valgrind <https://petsc.org/release/faq/#valgrind> or Compute Sanitizer
1255: <https://developer.nvidia.com/compute-sanitizer> on NVIDIA CUDA systems for finding memory problems. The ``CHKMEMQ``
1256: macro is useful on systems that do not have `valgrind`, but is not as good as `valgrind` or `compute-sanitizer`.
1258: Must run with the option `-malloc_debug` (`-malloc_test` in debug mode; or if `PetscMallocSetDebug()` called) to enable this option
1260: Once the error handler is called the calling function is then returned from with the given error code.
1262: By defaults prints location where memory that is corrupted was allocated.
1264: Use `CHKMEMA` for functions that return `void`
1266: .seealso: `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `SETERRQ()`, `PetscMallocValidate()`
1267: M*/
1268: #if PetscDefined(CLANG_STATIC_ANALYZER)
1269: #define CHKMEMQ
1270: #define CHKMEMA
1271: #else
1272: #define CHKMEMQ \
1273: do { \
1274: PetscErrorCode ierr_petsc_memq_ = PetscMallocValidate(__LINE__, PETSC_FUNCTION_NAME, __FILE__); \
1275: if (PetscUnlikely(ierr_petsc_memq_ != PETSC_SUCCESS)) return PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_memq_, PETSC_ERROR_REPEAT, " "); \
1276: } while (0)
1277: #define CHKMEMA PetscMallocValidate(__LINE__, PETSC_FUNCTION_NAME, __FILE__)
1278: #endif
1280: /*E
1281: PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
1283: Level: advanced
1285: Note:
1286: `PETSC_ERROR_IN_CXX` indicates the error was detected in C++ and an exception should be generated
1288: Developer Note:
1289: This is currently used to decide when to print the detailed information about the run in `PetscTraceBackErrorHandler()`
1291: .seealso: `PetscError()`, `SETERRQ()`
1292: E*/
1293: typedef enum {
1294: PETSC_ERROR_INITIAL = 0,
1295: PETSC_ERROR_REPEAT = 1,
1296: PETSC_ERROR_IN_CXX = 2
1297: } PetscErrorType;
1299: #if defined(__clang_analyzer__)
1300: __attribute__((analyzer_noreturn))
1301: #endif
1302: PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, ...) PETSC_ATTRIBUTE_COLD PETSC_ATTRIBUTE_FORMAT(7, 8);
1304: PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
1305: PETSC_EXTERN PetscErrorCode PetscErrorMessage(PetscErrorCode, const char *[], const char *[]);
1306: PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1307: PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1308: PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1309: PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1310: PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1311: PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1312: PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *), void *);
1313: PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
1314: PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int, void *);
1315: PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int, void *), void *);
1316: PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
1317: PETSC_EXTERN PetscErrorCode PetscCheckPointerSetIntensity(PetscInt);
1318: PETSC_EXTERN void PetscSignalSegvCheckPointerOrMpi(void);
1319: PETSC_DEPRECATED_FUNCTION(3, 13, 0, "PetscSignalSegvCheckPointerOrMpi()", ) static inline void PetscSignalSegvCheckPointer(void)
1320: {
1321: PetscSignalSegvCheckPointerOrMpi();
1322: }
1324: /*MC
1325: PetscErrorPrintf - Prints error messages.
1327: Synopsis:
1328: #include <petscsys.h>
1329: PetscErrorCode (*PetscErrorPrintf)(const char format[], ...);
1331: Not Collective; No Fortran Support
1333: Input Parameter:
1334: . format - the usual `printf()` format string
1336: Options Database Keys:
1337: + -error_output_stdout - cause error messages to be printed to stdout instead of the (default) stderr
1338: - -error_output_none - to turn off all printing of error messages (does not change the way the error is handled.)
1340: Level: developer
1342: Notes:
1343: Use
1344: .vb
1345: PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the error is handled) and
1346: PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on or you can use your own function
1347: .ve
1348: Use
1349: .vb
1350: PETSC_STDERR = FILE* obtained from a file open etc. to have stderr printed to the file.
1351: PETSC_STDOUT = FILE* obtained from a file open etc. to have stdout printed to the file.
1352: .ve
1354: You can change how error messages are printed by replacing the function pointer with your own function, for example,
1355: where `mylogfile` is a `FILE` the application has opened,
1356: .vb
1357: PetscErrorCode MyErrorPrintf(const char format[], ...)
1358: {
1359: va_list Argp;
1361: // do not use PetscFunctionBegin here, this is called while an error is being handled
1362: va_start(Argp, format);
1363: (void)vfprintf(mylogfile, format, Argp);
1364: va_end(Argp);
1365: return PETSC_SUCCESS;
1366: }
1367: .ve
1368: then set
1369: .vb
1370: PetscErrorPrintf = MyErrorPrintf;
1371: .ve
1373: Use
1374: .vb
1375: `PetscPushErrorHandler()` to provide your own error handler that determines what kind of messages to print
1376: .ve
1378: .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscHelpPrintf()`, `PetscPrintf()`, `PetscPushErrorHandler()`, `PetscVFPrintf()`,
1379: `PetscErrorPrintfNone()`, `PetscErrorPrintfDefault()`
1380: M*/
1381: PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
1383: /*E
1384: PetscFPTrap - types of floating point exceptions that may be trapped
1386: Currently only `PETSC_FP_TRAP_OFF` and `PETSC_FP_TRAP_ON` are handled. All others are treated as `PETSC_FP_TRAP_ON`.
1388: Level: intermediate
1390: .seealso: `PetscSetFPTrap()`, `PetscFPTrapPush()`
1391: E*/
1392: typedef enum {
1393: PETSC_FP_TRAP_OFF = 0,
1394: PETSC_FP_TRAP_INDIV = 1,
1395: PETSC_FP_TRAP_FLTOPERR = 2,
1396: PETSC_FP_TRAP_FLTOVF = 4,
1397: PETSC_FP_TRAP_FLTUND = 8,
1398: PETSC_FP_TRAP_FLTDIV = 16,
1399: PETSC_FP_TRAP_FLTINEX = 32,
1400: PETSC_FP_TRAP_ON = 63
1401: } PetscFPTrap;
1403: PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
1404: PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
1405: PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
1406: PETSC_EXTERN PetscErrorCode PetscDetermineInitialFPTrap(void);
1408: /*
1409: Allows the code to build a stack frame as it runs
1410: */
1412: #define PETSCSTACKSIZE 64
1414: /*S
1415: PetscStack - Fixed-size record of the chain of PETSc functions that are currently active on the calling thread;
1416: maintained by `PetscFunctionBegin`/`PetscFunctionReturn` to provide meaningful PETSc-level back traces and to drive the SAWs stack viewer
1418: Level: developer
1420: Notes:
1421: The default stack is a global variable called `petscstack`. It is maintained only when PETSc is configured `--with-debugging`;
1422: otherwise the stack-manipulation macros become no-ops. With thread safety enabled, `petscstack` is declared with `PETSC_TLS`,
1423: which gives each thread its own instance using thread-local storage.
1424: Inspect a stack with `PetscStackView()` or `PetscStackViewSAWs()`.
1426: Only the first `PETSCSTACKSIZE` frames are stored; the `currentsize` field counts all active calls, including frames that are not stored,
1427: so that the stack unwinds correctly.
1429: .seealso: `PetscFunctionBegin`, `PetscFunctionBeginUser`, `PetscFunctionReturn()`, `PetscStackView()`, `PetscStackViewSAWs()`, `PetscMallocGetStack()`, `PETSC_TLS`
1430: S*/
1431: typedef struct {
1432: const char *function[PETSCSTACKSIZE];
1433: const char *file[PETSCSTACKSIZE];
1434: int line[PETSCSTACKSIZE];
1435: int petscroutine[PETSCSTACKSIZE]; /* 0 external called from PETSc, 1 PETSc functions, 2 PETSc user functions */
1436: int currentsize;
1437: int hotdepth;
1438: PetscBool3 check; /* check Push/Pop semantics; UNKNOWN initializes petscstack from the process-wide default */
1439: } PetscStack;
1440: #if PetscDefined(USE_DEBUG)
1441: PETSC_EXTERN PETSC_TLS PetscStack petscstack;
1442: PETSC_EXTERN PetscBool petscstackcheck;
1443: #endif
1445: /* These are used by the PetscStackPushNoCheck() machinery below; their full SAWs counterparts
1446: (PetscObjectSAWs*(), PetscStackViewSAWs(), etc.) live in petscsys.h */
1447: #if PetscDefined(HAVE_SAWS)
1448: PETSC_EXTERN void PetscStackSAWsGrantAccess(void);
1449: PETSC_EXTERN void PetscStackSAWsTakeAccess(void);
1450: #else
1451: #define PetscStackSAWsTakeAccess()
1452: #define PetscStackSAWsGrantAccess()
1453: #endif
1455: #if PetscDefined(CLANG_STATIC_ANALYZER) || defined(__clang_analyzer__)
1456: #define PetscStackPushNoCheck(funct, petsc_routine, hot)
1457: #define PetscStackUpdateLine
1458: #define PetscStackPushExternal(funct)
1459: #define PetscStackPopNoCheck(funct)
1460: #define PetscStackClearTop
1461: #define PetscFunctionBegin
1462: #define PetscFunctionBeginUser
1463: #define PetscFunctionBeginHot
1464: #define PetscFunctionReturn(...) return __VA_ARGS__
1465: #define PetscFunctionReturnVoid() return
1466: #define PetscStackPop
1467: #define PetscStackPush(f)
1468: #define PetscStackPush_Private(stack__, file__, func__, line__, petsc_routine__, hot__) \
1469: (void)(file__); \
1470: (void)(func__); \
1471: (void)(line__)
1472: #define PetscStackPop_Private(stack__, func__) (void)(func__)
1473: #elif PetscDefined(USE_DEBUG)
1475: #define PetscStackPush_Private(stack__, file__, func__, line__, petsc_routine__, hot__) \
1476: do { \
1477: if ((stack__).currentsize < PETSCSTACKSIZE) { \
1478: (stack__).function[(stack__).currentsize] = func__; \
1479: if (petsc_routine__) { \
1480: (stack__).file[(stack__).currentsize] = file__; \
1481: (stack__).line[(stack__).currentsize] = line__; \
1482: } else { \
1483: (stack__).file[(stack__).currentsize] = PETSC_NULLPTR; \
1484: (stack__).line[(stack__).currentsize] = 0; \
1485: } \
1486: (stack__).petscroutine[(stack__).currentsize] = petsc_routine__; \
1487: } \
1488: ++(stack__).currentsize; \
1489: (stack__).hotdepth += ((hot__) || (stack__).hotdepth); \
1490: } while (0)
1492: /* uses PetscCheckAbort() because may be used in a function that does not return an error code */
1493: #define PetscStackPop_Private(stack__, func__) \
1494: do { \
1495: PetscCheckAbort(!PetscBool3ToBool((stack__).check) || (stack__).currentsize > 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid stack size %d, pop %s %s:%d. Check for missing or mismatched PetscFunctionBegin/PetscFunctionReturn.\n", (stack__).currentsize, func__, __FILE__, __LINE__); \
1496: if ((stack__).currentsize > 0 && --(stack__).currentsize < PETSCSTACKSIZE) { \
1497: PetscCheckAbort(!PetscBool3ToBool((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. Check for missing or mismatched PetscFunctionBegin/PetscFunctionReturn.\n", \
1498: (stack__).function[(stack__).currentsize], (stack__).file[(stack__).currentsize], (stack__).line[(stack__).currentsize], func__, __FILE__, __LINE__); \
1499: (stack__).function[(stack__).currentsize] = PETSC_NULLPTR; \
1500: (stack__).file[(stack__).currentsize] = PETSC_NULLPTR; \
1501: (stack__).line[(stack__).currentsize] = 0; \
1502: (stack__).petscroutine[(stack__).currentsize] = 0; \
1503: } \
1504: (stack__).hotdepth = (stack__).hotdepth > 0 ? (stack__).hotdepth - 1 : 0; \
1505: } while (0)
1507: /*MC
1508: PetscStackPushNoCheck - Pushes a new function name and line number onto the PETSc default stack that tracks where the running program is
1509: currently in the source code.
1511: Synopsis:
1512: #include <petscsys.h>
1513: void PetscStackPushNoCheck(char *funct, int petsc_routine, PetscBool hot);
1515: Not Collective
1517: Input Parameters:
1518: + funct - the function name
1519: . petsc_routine - 2 user function, 1 PETSc function, 0 some other function
1520: - hot - indicates that the function may be called often so expensive error checking should be turned off inside the function
1522: Level: developer
1524: Notes:
1525: 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
1526: 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
1527: help debug the problem.
1529: This version does not check the memory corruption (an expensive operation), use `PetscStackPush()` to check the memory.
1531: Use `PetscStackPushExternal()` for a function call that is about to be made to a non-PETSc or user function (such as BLAS etc).
1533: The default stack is a global variable called `petscstack`.
1534: With thread safety enabled, this variable has a separate instance for each thread.
1536: .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1537: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPush()`, `PetscStackPop`,
1538: `PetscStackPushExternal()`
1539: M*/
1540: #define PetscStackPushNoCheck(funct, petsc_routine, hot) \
1541: do { \
1542: PetscStackSAWsTakeAccess(); \
1543: if (petscstack.check == PETSC_BOOL3_UNKNOWN) petscstack.check = PetscBoolToBool3(petscstackcheck); \
1544: PetscStackPush_Private(petscstack, __FILE__, funct, __LINE__, petsc_routine, hot); \
1545: PetscStackSAWsGrantAccess(); \
1546: } while (0)
1548: /*MC
1549: PetscStackUpdateLine - in a function that has a `PetscFunctionBegin` or `PetscFunctionBeginUser` updates the stack line number to the
1550: current line number.
1552: Synopsis:
1553: #include <petscsys.h>
1554: void PetscStackUpdateLine
1556: Not Collective
1558: Level: developer
1560: Notes:
1561: Using `PetscCall()` and friends automatically handles this process
1563: 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
1564: occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1565: help debug the problem.
1567: The default stack is a global variable called `petscstack`.
1568: With thread safety enabled, this variable has a separate instance for each thread.
1570: This is used by `PetscCall()` and is otherwise not like to be needed
1572: .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPop`, `PetscCall()`
1573: M*/
1574: #define PetscStackUpdateLine \
1575: do { \
1576: if (petscstack.currentsize > 0 && petscstack.currentsize <= PETSCSTACKSIZE && petscstack.function[petscstack.currentsize - 1] == PETSC_FUNCTION_NAME) petscstack.line[petscstack.currentsize - 1] = __LINE__; \
1577: } while (0)
1579: /*MC
1580: PetscStackPushExternal - Pushes a new function name onto the PETSc default stack that tracks where the running program is
1581: currently in the source code. Does not include the filename or line number since this is called by the calling routine
1582: for non-PETSc or user functions.
1584: Synopsis:
1585: #include <petscsys.h>
1586: void PetscStackPushExternal(char *funct);
1588: Not Collective
1590: Input Parameter:
1591: . funct - the function name
1593: Level: developer
1595: Notes:
1596: Using `PetscCallExternal()` and friends automatically handles this process
1598: 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
1599: occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1600: help debug the problem.
1602: The default stack is a global variable called `petscstack`.
1603: With thread safety enabled, this variable has a separate instance for each thread.
1605: This is to be used when calling an external package function such as a BLAS function.
1607: This also updates the stack line number for the current stack function.
1609: .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1610: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1611: M*/
1612: #define PetscStackPushExternal(funct) \
1613: do { \
1614: PetscStackUpdateLine; \
1615: PetscStackPushNoCheck(funct, 0, PETSC_TRUE); \
1616: } while (0)
1618: /*MC
1619: PetscStackPopNoCheck - Pops a function name from the PETSc default stack that tracks where the running program is
1620: currently in the source code.
1622: Synopsis:
1623: #include <petscsys.h>
1624: void PetscStackPopNoCheck(char *funct);
1626: Not Collective
1628: Input Parameter:
1629: . funct - the function name
1631: Level: developer
1633: Notes:
1634: Using `PetscCall()`, `PetscCallExternal()`, `PetscCallBack()` and friends negates the need to call this
1636: 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
1637: occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1638: help debug the problem.
1640: The default stack is a global variable called `petscstack`.
1641: With thread safety enabled, this variable has a separate instance for each thread.
1643: Developer Note:
1644: `PetscStackPopNoCheck()` takes a function argument while `PetscStackPop` does not, this difference is likely just historical.
1646: .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1647: M*/
1648: #define PetscStackPopNoCheck(funct) \
1649: do { \
1650: PetscStackSAWsTakeAccess(); \
1651: PetscStackPop_Private(petscstack, funct); \
1652: PetscStackSAWsGrantAccess(); \
1653: } while (0)
1655: #define PetscStackClearTop \
1656: do { \
1657: PetscStackSAWsTakeAccess(); \
1658: if (petscstack.currentsize > 0 && --petscstack.currentsize < PETSCSTACKSIZE) { \
1659: petscstack.function[petscstack.currentsize] = PETSC_NULLPTR; \
1660: petscstack.file[petscstack.currentsize] = PETSC_NULLPTR; \
1661: petscstack.line[petscstack.currentsize] = 0; \
1662: petscstack.petscroutine[petscstack.currentsize] = 0; \
1663: } \
1664: petscstack.hotdepth = petscstack.hotdepth > 0 ? petscstack.hotdepth - 1 : 0; \
1665: PetscStackSAWsGrantAccess(); \
1666: } while (0)
1668: /*MC
1669: PetscFunctionBegin - First executable line of each PETSc function, used for error handling. Final
1670: line of PETSc functions should be `PetscFunctionReturn`(PETSC_SUCCESS);
1672: Synopsis:
1673: #include <petscsys.h>
1674: void PetscFunctionBegin;
1676: Not Collective; No Fortran Support
1678: Usage:
1679: .vb
1680: int something;
1682: PetscFunctionBegin;
1683: .ve
1685: Level: developer
1687: Note:
1688: Use `PetscFunctionBeginUser` for application codes.
1690: .seealso: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`
1692: M*/
1693: #define PetscFunctionBegin \
1694: do { \
1695: PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 1, PETSC_FALSE); \
1696: } while (0)
1698: /*MC
1699: PetscFunctionBeginHot - Substitute for `PetscFunctionBegin` to be used in functions that are called in
1700: performance-critical circumstances. Use of this function allows for lighter profiling by default.
1702: Synopsis:
1703: #include <petscsys.h>
1704: void PetscFunctionBeginHot;
1706: Not Collective; No Fortran Support
1708: Usage:
1709: .vb
1710: int something;
1712: PetscFunctionBeginHot;
1713: .ve
1715: Level: developer
1717: .seealso: `PetscFunctionBegin`, `PetscFunctionReturn()`, `PetscStackPushNoCheck()`
1719: M*/
1720: #define PetscFunctionBeginHot \
1721: do { \
1722: PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 1, PETSC_TRUE); \
1723: } while (0)
1725: /*MC
1726: PetscFunctionBeginUser - First executable line of user provided routines
1728: Synopsis:
1729: #include <petscsys.h>
1730: void PetscFunctionBeginUser;
1732: Not Collective; No Fortran Support
1734: Usage:
1735: .vb
1736: int something;
1738: PetscFunctionBeginUser;
1739: .ve
1741: Level: intermediate
1743: Notes:
1744: Functions that incorporate this must call `PetscFunctionReturn()` instead of return except for main().
1746: May be used before `PetscInitialize()`
1748: This is identical to `PetscFunctionBegin` except it labels the routine as a user
1749: routine instead of as a PETSc library routine.
1751: .seealso: `PetscFunctionReturn()`, `PetscFunctionBegin`, `PetscFunctionBeginHot`, `PetscStackPushNoCheck()`
1752: M*/
1753: #define PetscFunctionBeginUser \
1754: do { \
1755: PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 2, PETSC_FALSE); \
1756: } while (0)
1758: /*MC
1759: PetscStackPush - Pushes a new function name and line number onto the PETSc default stack that tracks where the running program is
1760: currently in the source code and verifies the memory is not corrupted.
1762: Synopsis:
1763: #include <petscsys.h>
1764: void PetscStackPush(char *funct)
1766: Not Collective
1768: Input Parameter:
1769: . funct - the function name
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`.
1779: With thread safety enabled, this variable has a separate instance for each thread.
1781: .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1782: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1783: M*/
1784: #define PetscStackPush(n) \
1785: do { \
1786: PetscStackPushNoCheck(n, 0, PETSC_FALSE); \
1787: CHKMEMQ; \
1788: } while (0)
1790: /*MC
1791: PetscStackPop - Pops a function name from the PETSc default stack that tracks where the running program is
1792: currently in the source code and verifies the memory is not corrupted.
1794: Synopsis:
1795: #include <petscsys.h>
1796: void PetscStackPop
1798: Not Collective
1800: Level: developer
1802: Notes:
1803: 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
1804: occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1805: help debug the problem.
1807: The default stack is a global variable called `petscstack`.
1808: With thread safety enabled, this variable has a separate instance for each thread.
1810: .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPopNoCheck()`, `PetscStackPush()`
1811: M*/
1812: #define PetscStackPop \
1813: do { \
1814: CHKMEMQ; \
1815: PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1816: } while (0)
1818: /*MC
1819: PetscFunctionReturn - Last executable line of each PETSc function used for error
1820: handling. Replaces `return()`.
1822: Synopsis:
1823: #include <petscsys.h>
1824: void PetscFunctionReturn(...)
1826: Not Collective; No Fortran Support
1828: Level: beginner
1830: Notes:
1831: This routine is a macro, so while it does not "return" anything itself, it does return from
1832: the function in the literal sense.
1834: Usually the return value is `PETSC_SUCCESS` (for example in any function returning
1835: `PetscErrorCode`), however it is possible to return any arbitrary type. The arguments of
1836: this macro are placed before the `return` statement as-is.
1838: Any routine which returns via `PetscFunctionReturn()` must begin with a corresponding
1839: `PetscFunctionBegin`.
1841: For routines which return `void` use `PetscFunctionReturnVoid()` instead.
1843: Example Usage:
1844: .vb
1845: PetscErrorCode foo(int *x)
1846: {
1847: PetscFunctionBegin; // don't forget the begin!
1848: *x = 10;
1849: PetscFunctionReturn(PETSC_SUCCESS);
1850: }
1851: .ve
1853: May return any arbitrary type\:
1854: .vb
1855: struct Foo
1856: {
1857: int x;
1858: };
1860: struct Foo make_foo(int value)
1861: {
1862: struct Foo f;
1864: PetscFunctionBegin;
1865: f.x = value;
1866: PetscFunctionReturn(f);
1867: }
1868: .ve
1870: .seealso: `PetscFunctionBegin`, `PetscFunctionBeginUser`, `PetscFunctionReturnVoid()`,
1871: `PetscStackPopNoCheck()`
1872: M*/
1873: #define PetscFunctionReturn(...) \
1874: do { \
1875: PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1876: return __VA_ARGS__; \
1877: } while (0)
1879: /*MC
1880: PetscFunctionReturnVoid - Like `PetscFunctionReturn()` but returns `void`
1882: Synopsis:
1883: #include <petscsys.h>
1884: void PetscFunctionReturnVoid()
1886: Not Collective
1888: Level: beginner
1890: Note:
1891: Behaves identically to `PetscFunctionReturn()` except that it returns `void`. That is, this
1892: macro culminates with `return`.
1894: Example Usage:
1895: .vb
1896: void foo()
1897: {
1898: PetscFunctionBegin; // must start with PetscFunctionBegin!
1899: bar();
1900: baz();
1901: PetscFunctionReturnVoid();
1902: }
1903: .ve
1905: .seealso: `PetscFunctionReturn()`, `PetscFunctionBegin`, `PetscFunctionBeginUser`
1906: M*/
1907: #define PetscFunctionReturnVoid() \
1908: do { \
1909: PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1910: return; \
1911: } while (0)
1912: #else /* PETSC_USE_DEBUG */
1913: #define PetscStackPushNoCheck(funct, petsc_routine, hot)
1914: #define PetscStackUpdateLine
1915: #define PetscStackPushExternal(funct)
1916: #define PetscStackPopNoCheck(...)
1917: #define PetscStackClearTop
1918: #define PetscFunctionBegin
1919: #define PetscFunctionBeginUser
1920: #define PetscFunctionBeginHot
1921: #define PetscFunctionReturn(...) return __VA_ARGS__
1922: #define PetscFunctionReturnVoid() return
1923: #define PetscStackPop CHKMEMQ
1924: #define PetscStackPush(f) CHKMEMQ
1925: #endif /* PETSC_USE_DEBUG */
1927: #if PetscDefined(CLANG_STATIC_ANALYZER)
1928: #define PetscCallExternalVoid(...)
1929: template <typename F, typename... Args>
1930: void PetscCallExternal(F, Args...);
1931: template <typename F, typename... Args>
1932: void PetscCallExternalAbort(F, Args...);
1933: #else
1934: /*MC
1935: PetscCallExternalVoid - Calls an external library routine or user function after pushing the name of the routine on the stack.
1937: Input Parameters:
1938: + name - string that gives the name of the function being called
1939: - routine - actual call to the routine, for example, `functionname(a,b)`
1941: Level: developer
1943: Notes:
1944: Often one should use `PetscCallExternal()` instead. This routine is intended for external library routines that DO NOT return error codes.
1945: They may return a value or may return `void`.
1947: In debug mode this also checks the memory for corruption at the end of the function call.
1949: Certain external packages, such as BLAS/LAPACK may have their own macros, `PetscCallBLAS()` for managing the call, error checking, etc.
1951: Developer Note:
1952: This is so that when a user or external library routine results in a crash or corrupts memory, they get blamed instead of PETSc.
1954: .seealso: `PetscCall()`, `PetscStackPushNoCheck()`, `PetscStackPush()`, `PetscCallExternal()`, `PetscCallBLAS()`
1955: @*/
1956: #define PetscCallExternalVoid(name, ...) \
1957: do { \
1958: PetscStackPushExternal(name); \
1959: __VA_ARGS__; \
1960: PetscStackPop; \
1961: } while (0)
1963: /*MC
1964: PetscCallExternal - Calls an external library routine that returns an error code after pushing the name of the routine on the stack.
1966: Input Parameters:
1967: + func - name of the routine
1968: - args - arguments to the routine
1970: Level: developer
1972: Notes:
1973: This is intended for external package routines that return error codes. Use `PetscCallExternalVoid()` for those that do not.
1975: In debug mode this also checks the memory for corruption at the end of the function call.
1977: Assumes the error return code of the function is an integer and that a value of 0 indicates success
1979: Developer Note:
1980: This is so that when an external package routine results in a crash or corrupts memory, they get blamed instead of PETSc.
1982: .seealso: `PetscCall()`, `PetscStackPushNoCheck()`, `PetscStackPush()`, `PetscCallExternalVoid()`, `PetscCallExternalAbort()`
1983: M*/
1984: #define PetscCallExternal(func, ...) \
1985: do { \
1986: /* func must not be parenthesized; callers pass !func to invert an external library's success convention */ \
1987: PetscStackPush(PetscStringize(func)); \
1988: int ierr_petsc_call_external_ = (int)func(__VA_ARGS__); \
1989: PetscStackPop; \
1990: PetscCheck(ierr_petsc_call_external_ == 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in %s(): error code %d", PetscStringize(func), ierr_petsc_call_external_); \
1991: } while (0)
1993: /*MC
1994: 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
1996: Input Parameters:
1997: + func - name of the routine
1998: - args - arguments to the routine
2000: Level: developer
2002: Notes:
2003: This is intended for external package routines that return error codes. Use `PetscCallExternalVoid()` for those that do not.
2005: In debug mode this also checks the memory for corruption at the end of the function call.
2007: Assumes the error return code of the function is an integer and that a value of 0 indicates success
2009: Developer Note:
2010: This is so that when an external package routine results in a crash or corrupts memory, they get blamed instead of PETSc.
2012: .seealso: `PetscCall()`, `PetscStackPushNoCheck()`, `PetscStackPush()`, `PetscCallExternalVoid()`, `PetscCallExternal()`
2013: M*/
2014: #define PetscCallExternalAbort(func, ...) \
2015: do { \
2016: PetscStackUpdateLine; \
2017: int ierr_petsc_call_external_ = func(__VA_ARGS__); \
2018: if (PetscUnlikely(ierr_petsc_call_external_ != 0)) { \
2019: (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_); \
2020: PETSCABORTWITHIERR_Private(PETSC_COMM_SELF, PETSC_ERR_LIB); \
2021: } \
2022: } while (0)
2023: #endif /* PETSC_CLANG_STATIC_ANALYZER */