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 - A 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()`
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: /*
61: Returned from PETSc functions that are called from MPI, such as related to attributes
62: Do not confuse PETSC_MPI_ERROR_CODE and PETSC_ERR_MPI, the first is registered with MPI and returned to MPI as
63: an error code, the latter is a regular PETSc error code passed within PETSc code indicating an error was detected in an MPI call.
64: */
65: PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CLASS;
66: PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CODE;
68: /*MC
69: SETERRMPI - Macro to be called when an error has been detected within an MPI callback function
71: No Fortran Support
73: Synopsis:
74: #include <petscsys.h>
75: PetscErrorCode SETERRMPI(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
77: Collective
79: Input Parameters:
80: + comm - A communicator, use `PETSC_COMM_SELF` unless you know all ranks of another communicator will detect the error
81: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
82: - message - error message
84: Level: developer
86: Notes:
87: 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`
88: which is registered with `MPI_Add_error_code()` when PETSc is initialized.
90: .seealso: `SETERRQ()`, `PetscCall()`, `PetscCallMPI()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
91: M*/
92: #define SETERRMPI(comm, ierr, ...) return ((void)PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__), PETSC_MPI_ERROR_CODE)
94: /*MC
95: SETERRA - Fortran-only macro that can be called when an error has been detected from the main program
97: Synopsis:
98: #include <petscsys.h>
99: PetscErrorCode SETERRA(MPI_Comm comm,PetscErrorCode ierr,char *message)
101: Collective
103: Input Parameters:
104: + comm - A communicator, so that the error can be collective
105: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
106: - message - error message in the printf format
108: Level: beginner
110: Notes:
111: This should only be used with Fortran. With C/C++, use `SETERRQ()`.
113: `SETERRQ()` may be called from Fortran subroutines but `SETERRA()` must be called from the
114: Fortran main program.
116: .seealso: `SETERRQ()`, `SETERRABORT()`, `PetscCall()`, `CHKERRA()`, `PetscCallAbort()`
117: M*/
119: /*MC
120: SETERRABORT - Macro that can be called when an error has been detected,
122: Synopsis:
123: #include <petscsys.h>
124: PetscErrorCode SETERRABORT(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
126: Collective
128: Input Parameters:
129: + comm - A communicator, so that the error can be collective
130: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
131: - message - error message in the printf format
133: Level: beginner
135: Notes:
136: This function just calls `MPI_Abort()`.
138: This should only be called in routines that cannot return an error code, such as in C++ constructors.
140: Fortran Note:
141: Use `SETERRA()` in Fortran main program and `SETERRQ()` in Fortran subroutines
143: Developer Note:
144: In Fortran `SETERRA()` could be called `SETERRABORT()` since they serve the same purpose
146: .seealso: `SETERRQ()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `PetscCall()`, `CHKMEMQ`
147: M*/
148: #define SETERRABORT(comm, ierr, ...) \
149: do { \
150: (void)PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__); \
151: MPI_Abort(comm, ierr); \
152: } while (0)
154: /*MC
155: PetscCheck - Check that a particular condition is true
157: Synopsis:
158: #include <petscerror.h>
159: void PetscCheck(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
161: Collective; No Fortran Support
163: Input Parameters:
164: + cond - The boolean condition
165: . comm - The communicator on which the check can be collective on
166: . ierr - A nonzero error code, see include/petscerror.h for the complete list
167: - message - Error message in printf format
169: Level: beginner
171: Notes:
172: Enabled in both optimized and debug builds.
174: Calls `SETERRQ()` if the assertion fails, so can only be called from functions returning a
175: `PetscErrorCode` (or equivalent type after conversion).
177: .seealso: `PetscAssert()`, `SETERRQ()`, `PetscError()`, `PetscCall()`, `PetscCheckAbort()`
178: M*/
179: #define PetscCheck(cond, comm, ierr, ...) \
180: do { \
181: if (PetscUnlikely(!(cond))) SETERRQ(comm, ierr, __VA_ARGS__); \
182: } while (0)
184: /*MC
185: PetscCheckAbort - Check that a particular condition is true, otherwise prints error and aborts
187: Synopsis:
188: #include <petscerror.h>
189: void PetscCheckAbort(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
191: Collective; No Fortran Support
193: Input Parameters:
194: + cond - The boolean condition
195: . comm - The communicator on which the check can be collective on
196: . ierr - A nonzero error code, see include/petscerror.h for the complete list
197: - message - Error message in printf format
199: Level: developer
201: Notes:
202: Enabled in both optimized and debug builds.
204: Calls `SETERRABORT()` if the assertion fails, can be called from a function that does not return an
205: error code, such as a C++ constructor. usually `PetscCheck()` should be used.
207: .seealso: `PetscAssertAbort()`, `PetscAssert()`, `SETERRQ()`, `PetscError()`, `PetscCall()`, `PetscCheck()`, `SETERRABORT()`
208: M*/
209: #define PetscCheckAbort(cond, comm, ierr, ...) \
210: do { \
211: if (PetscUnlikely(!(cond))) SETERRABORT(comm, ierr, __VA_ARGS__); \
212: } while (0)
214: /*MC
215: PetscAssert - Assert that a particular condition is true
217: Synopsis:
218: #include <petscerror.h>
219: void PetscAssert(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
221: Collective; No Fortran Support
223: Input Parameters:
224: + cond - The boolean condition
225: . comm - The communicator on which the check can be collective on
226: . ierr - A nonzero error code, see include/petscerror.h for the complete list
227: - message - Error message in printf format
229: Level: beginner
231: Notes:
232: Equivalent to `PetscCheck()` if debugging is enabled, and `PetscAssume(cond)` otherwise.
234: See `PetscCheck()` for usage and behaviour.
236: This is needed instead of simply using `assert()` because this correctly handles the collective nature of errors under MPI
238: .seealso: `PetscCheck()`, `SETERRQ()`, `PetscError()`, `PetscAssertAbort()`
239: M*/
240: #if PetscDefined(USE_DEBUG)
241: #define PetscAssert(cond, comm, ierr, ...) PetscCheck(cond, comm, ierr, __VA_ARGS__)
242: #else
243: #define PetscAssert(cond, ...) PetscAssume(cond)
244: #endif
246: /*MC
247: PetscAssertAbort - Assert that a particular condition is true, otherwise prints error and aborts
249: Synopsis:
250: #include <petscerror.h>
251: void PetscAssertAbort(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
253: Collective; No Fortran Support
255: Input Parameters:
256: + cond - The boolean condition
257: . comm - The communicator on which the check can be collective on
258: . ierr - A nonzero error code, see include/petscerror.h for the complete list
259: - message - Error message in printf format
261: Level: beginner
263: Notes:
264: Enabled only in debug builds. See `PetscCheckAbort()` for usage.
266: .seealso: `PetscCheckAbort()`, `PetscAssert()`, `PetscCheck()`, `SETERRABORT()`, `PetscError()`
267: M*/
268: #if PetscDefined(USE_DEBUG)
269: #define PetscAssertAbort(cond, comm, ierr, ...) PetscCheckAbort(cond, comm, ierr, __VA_ARGS__)
270: #else
271: #define PetscAssertAbort(cond, comm, ierr, ...) PetscAssume(cond)
272: #endif
274: /*MC
275: PetscCall - Calls a PETSc function and then checks the resulting error code, if it is
276: non-zero it calls the error handler and returns from the current function with the error
277: code.
279: Synopsis:
280: #include <petscerror.h>
281: void PetscCall(PetscFunction(args))
283: Not Collective
285: Input Parameter:
286: . PetscFunction - any PETSc function that returns an error code
288: Level: beginner
290: Notes:
291: Once the error handler is called the calling function is then returned from with the given
292: error code. Experienced users can set the error handler with `PetscPushErrorHandler()`.
294: `PetscCall()` cannot be used in functions returning a datatype not convertible to
295: `PetscErrorCode`. For example, `PetscCall()` may not be used in functions returning void, use
296: `PetscCallAbort()` or `PetscCallVoid()` in this case.
298: Example Usage:
299: .vb
300: PetscCall(PetscInitiailize(...)); // OK to call even when PETSc is not yet initialized!
302: struct my_struct
303: {
304: void *data;
305: } my_complex_type;
307: struct my_struct bar(void)
308: {
309: PetscCall(foo(15)); // ERROR PetscErrorCode not convertible to struct my_struct!
310: }
312: PetscCall(bar()) // ERROR input not convertible to PetscErrorCode
313: .ve
315: It is also possible to call this directly on a `PetscErrorCode` variable
316: .vb
317: PetscCall(ierr); // check if ierr is nonzero
318: .ve
320: Should not be used to call callback functions provided by users, `PetscCallBack()` should be used in that situation.
322: `PetscUseTypeMethod()` or `PetscTryTypeMethod()` should be used when calling functions pointers contained in a PETSc object's `ops` array
324: Fortran Notes:
325: The Fortran function from which this is used must declare a variable PetscErrorCode ierr and ierr must be
326: the final argument to the PETSc function being called.
328: In the main program and in Fortran subroutines that do not have ierr as the final return parameter one
329: should use `PetscCallA()`
331: Example Fortran Usage:
332: .vb
333: PetscErrorCode ierr
334: Vec v
336: ...
337: PetscCall(VecShift(v,1.0,ierr))
338: PetscCallA(VecShift(v,1.0,ierr))
339: .ve
341: .seealso: `SETERRQ()`, `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`,
342: `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`,
343: `CHKERRMPI()`, `PetscCallBack()`, `PetscCallAbort()`, `PetscCallVoid()`
344: M*/
346: /*MC
347: PetscCallA - Fortran-only macro that should be used in the main program to call PETSc functions instead of using
348: PetscCall() which should be used in other Fortran subroutines
350: Synopsis:
351: #include <petscsys.h>
352: PetscErrorCode PetscCallA(PetscFunction(arguments,ierr))
354: Collective
356: Input Parameter:
357: . PetscFunction(arguments,ierr) - the call to the function
359: Level: beginner
361: Notes:
362: This should only be used with Fortran. With C/C++, use `PetscCall()` always.
364: Use `SETERRA()` to set an error in a Fortran main program and `SETERRQ()` in Fortran subroutines
366: .seealso: `SETERRQ()`, `SETERRA()`, `SETERRABORT()`, `PetscCall()`, `CHKERRA()`, `PetscCallAbort()`
367: M*/
369: /*MC
370: PetscCallBack - Calls a user provided PETSc callback function and then checks the resulting error code, if it is non-zero it calls the error
371: handler and returns from the current function with the error code.
373: Synopsis:
374: #include <petscerror.h>
375: void PetscCallBack(const char *functionname,PetscFunction(args))
377: Not Collective; No Fortran Support
379: Input Parameters:
380: + functionname - the name of the function being called, this can be a string with spaces that describes the meaning of the callback
381: - PetscFunction - user provided callback function that returns an error code
383: Example Usage:
384: .vb
385: PetscCallBack("XXX callback to do something",a->callback(...));
386: .ve
388: Level: developer
390: Notes:
391: Once the error handler is called the calling function is then returned from with the given
392: error code. Experienced users can set the error handler with `PetscPushErrorHandler()`.
394: `PetscCallBack()` should only be called in PETSc when a call is being made to a user provided call-back routine.
396: .seealso: `SETERRQ()`, `PetscCheck()`, `PetscCall()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`
397: `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`, `CHKERRMPI()`, `PetscCall()`
398: M*/
400: /*MC
401: PetscCallVoid - Like `PetscCall()` but for functions returning `void`
403: Synopsis:
404: #include <petscerror.h>
405: void PetscCall(PetscFunction(args))
407: Not Collective; No Fortran Support
409: Input Parameter:
410: . PetscFunction - any PETSc function that returns an error code
412: Example Usage:
413: .vb
414: void foo()
415: {
416: KSP ksp;
418: PetscFunctionBeginUser;
419: // OK, properly handles PETSc error codes
420: PetscCallVoid(KSPCreate(PETSC_COMM_WORLD, &ksp));
421: PetscFunctionReturn(PETSC_SUCCESS);
422: }
424: PetscErrorCode bar()
425: {
426: KSP ksp;
428: PetscFunctionBeginUser;
429: // ERROR, Non-void function 'bar' should return a value
430: PetscCallVoid(KSPCreate(PETSC_COMM_WORLD, &ksp));
431: // OK, returning PetscErrorCode
432: PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
433: PetscFunctionReturn(PETSC_SUCCESS);
434: }
435: .ve
437: Level: beginner
439: Notes:
440: Has identical usage to `PetscCall()`, except that it returns `void` on error instead of a
441: `PetscErrorCode`. See `PetscCall()` for more detailed discussion.
443: Note that users should prefer `PetscCallAbort()` to this routine. While this routine does
444: "handle" errors by returning from the enclosing function, it effectively gobbles the
445: error. Since the enclosing function itself returns `void`, its callers have no way of knowing
446: that the routine returned early due to an error. `PetscCallAbort()` at least ensures that the
447: program crashes gracefully.
449: .seealso: `PetscCall()`, `PetscErrorCode`
450: M*/
451: #if defined(PETSC_CLANG_STATIC_ANALYZER)
452: void PetscCall(PetscErrorCode);
453: void PetscCallBack(const char *, PetscErrorCode);
454: void PetscCallVoid(PetscErrorCode);
455: #else
456: #define PetscCall(...) \
457: do { \
458: PetscErrorCode ierr_petsc_call_q_; \
459: PetscStackUpdateLine; \
460: ierr_petsc_call_q_ = __VA_ARGS__; \
461: 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, " "); \
462: } while (0)
463: #define PetscCallBack(function, ...) \
464: do { \
465: PetscErrorCode ierr_petsc_call_q_; \
466: PetscStackUpdateLine; \
467: PetscStackPushExternal(function); \
468: ierr_petsc_call_q_ = __VA_ARGS__; \
469: PetscStackPop; \
470: 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, " "); \
471: } while (0)
472: #define PetscCallVoid(...) \
473: do { \
474: PetscErrorCode ierr_petsc_call_void_; \
475: PetscStackUpdateLine; \
476: ierr_petsc_call_void_ = __VA_ARGS__; \
477: if (PetscUnlikely(ierr_petsc_call_void_ != PETSC_SUCCESS)) { \
478: ierr_petsc_call_void_ = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_void_, PETSC_ERROR_REPEAT, " "); \
479: (void)ierr_petsc_call_void_; \
480: return; \
481: } \
482: } while (0)
483: #endif
485: /*MC
486: CHKERRQ - Checks error code returned from PETSc function
488: Synopsis:
489: #include <petscsys.h>
490: void CHKERRQ(PetscErrorCode ierr)
492: Not Collective
494: Input Parameter:
495: . ierr - nonzero error code
497: Level: deprecated
499: Note:
500: Deprecated in favor of `PetscCall()`. This routine behaves identically to it.
502: .seealso: `PetscCall()`
503: M*/
504: #define CHKERRQ(...) PetscCall(__VA_ARGS__)
505: #define CHKERRV(...) PetscCallVoid(__VA_ARGS__)
507: PETSC_EXTERN void PetscMPIErrorString(PetscMPIInt, char *);
509: /*MC
510: PetscCallMPI - Checks error code returned from MPI calls, if non-zero it calls the error
511: handler and then returns
513: Synopsis:
514: #include <petscerror.h>
515: void PetscCallMPI(MPI_Function(args))
517: Not Collective
519: Input Parameter:
520: . MPI_Function - an MPI function that returns an MPI error code
522: Level: beginner
524: Notes:
525: Always returns the error code `PETSC_ERR_MPI`; the MPI error code and string are embedded in
526: the string error message. Do not use this to call any other routines (for example PETSc
527: routines), it should only be used for direct MPI calls. The user may configure PETSc with the
528: `--with-strict-petscerrorcode` option to check this at compile-time, otherwise they must
529: check this themselves.
531: This routine can only be used in functions returning `PetscErrorCode` themselves. If the
532: calling function returns a different type, use `PetscCallMPIAbort()` instead.
534: Example Usage:
535: .vb
536: PetscCallMPI(MPI_Comm_size(...)); // OK, calling MPI function
538: PetscCallMPI(PetscFunction(...)); // ERROR, use PetscCall() instead!
539: .ve
541: Fortran Notes:
542: The Fortran function from which this is used must declare a variable `PetscErrorCode` ierr and ierr must be
543: the final argument to the MPI function being called.
545: In the main program and in Fortran subroutines that do not have ierr as the final return parameter one
546: should use `PetscCallMPIA()`
548: Fortran Usage:
549: .vb
550: PetscErrorCode ierr or integer ierr
551: ...
552: PetscCallMPI(MPI_Comm_size(...,ierr))
553: PetscCallMPIA(MPI_Comm_size(...,ierr)) ! Will abort after calling error handler
555: PetscCallMPI(MPI_Comm_size(...,eflag)) ! ERROR, final argument must be ierr
556: .ve
558: .seealso: `SETERRMPI()`, `PetscCall()`, `SETERRQ()`, `SETERRABORT()`, `PetscCallAbort()`,
559: `PetscCallMPIAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
560: `PetscError()`, `CHKMEMQ`
561: M*/
563: /*MC
564: PetscCallMPIAbort - Like `PetscCallMPI()` but calls `MPI_Abort()` on error
566: Synopsis:
567: #include <petscerror.h>
568: void PetscCallMPIAbort(MPI_Comm comm, MPI_Function(args))
570: Not Collective
572: Input Parameters:
573: + comm - the MPI communicator to abort on
574: - MPI_Function - an MPI function that returns an MPI error code
576: Level: beginner
578: Notes:
579: Usage is identical to `PetscCallMPI()`. See `PetscCallMPI()` for detailed discussion.
581: This routine may be used in functions returning `void` or other non-`PetscErrorCode` types.
583: Fortran Note:
584: In Fortran this is called `PetscCallMPIA()` and is intended to be used in the main program while `PetscCallMPI()` is
585: used in Fortran subroutines.
587: Developer Note:
588: This should have the same name in Fortran.
590: .seealso: `PetscCallMPI()`, `PetscCallAbort()`, `SETERRABORT()`
591: M*/
592: #if defined(PETSC_CLANG_STATIC_ANALYZER)
593: void PetscCallMPI(PetscMPIInt);
594: void PetscCallMPIAbort(MPI_Comm, PetscMPIInt);
595: #else
596: #define PetscCallMPI_Private(__PETSC_STACK_POP_FUNC__, __SETERR_FUNC__, __COMM__, ...) \
597: do { \
598: PetscMPIInt ierr_petsc_call_mpi_; \
599: PetscStackUpdateLine; \
600: PetscStackPushExternal("MPI function"); \
601: { \
602: ierr_petsc_call_mpi_ = __VA_ARGS__; \
603: } \
604: __PETSC_STACK_POP_FUNC__; \
605: if (PetscUnlikely(ierr_petsc_call_mpi_ != MPI_SUCCESS)) { \
606: char petsc_mpi_7_errorstring[2 * MPI_MAX_ERROR_STRING]; \
607: PetscMPIErrorString(ierr_petsc_call_mpi_, (char *)petsc_mpi_7_errorstring); \
608: __SETERR_FUNC__(__COMM__, PETSC_ERR_MPI, "MPI error %d %s", (int)ierr_petsc_call_mpi_, petsc_mpi_7_errorstring); \
609: } \
610: } while (0)
612: #define PetscCallMPI(...) PetscCallMPI_Private(PetscStackPop, SETERRQ, PETSC_COMM_SELF, __VA_ARGS__)
613: #define PetscCallMPIAbort(comm, ...) PetscCallMPI_Private(PetscStackPopNoCheck(PETSC_FUNCTION_NAME), SETERRABORT, comm, __VA_ARGS__)
614: #endif
616: /*MC
617: CHKERRMPI - Checks error code returned from MPI calls, if non-zero it calls the error
618: handler and then returns
620: Synopsis:
621: #include <petscerror.h>
622: void CHKERRMPI(PetscErrorCode ierr)
624: Not Collective
626: Input Parameter:
627: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
629: Level: deprecated
631: Note:
632: Deprecated in favor of `PetscCallMPI()`. This routine behaves identically to it.
634: .seealso: `PetscCallMPI()`
635: M*/
636: #define CHKERRMPI(...) PetscCallMPI(__VA_ARGS__)
638: /*MC
639: PetscCallAbort - Checks error code returned from PETSc function, if non-zero it aborts immediately by calling `MPI_Abort()`
641: Synopsis:
642: #include <petscerror.h>
643: void PetscCallAbort(MPI_Comm comm, PetscErrorCode ierr)
645: Collective
647: Input Parameters:
648: + comm - the MPI communicator on which to abort
649: - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
651: Level: intermediate
653: Notes:
654: This macro has identical type and usage semantics to `PetscCall()` with the important caveat
655: that this macro does not return. Instead, if ierr is nonzero it calls the PETSc error handler
656: and then immediately calls `MPI_Abort()`. It can therefore be used anywhere.
658: As per `MPI_Abort()` semantics the communicator passed must be valid, although there is currently
659: no attempt made at handling any potential errors from `MPI_Abort()`. Note that while
660: `MPI_Abort()` is required to terminate only those processes which reside on comm, it is often
661: the case that `MPI_Abort()` terminates *all* processes.
663: Example Usage:
664: .vb
665: PetscErrorCode boom(void) { return PETSC_ERR_MEM; }
667: void foo(void)
668: {
669: PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
670: }
672: double bar(void)
673: {
674: PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
675: }
677: PetscCallAbort(MPI_COMM_NULL,boom()); // ERROR, communicator should be valid
679: struct baz
680: {
681: baz()
682: {
683: PetscCallAbort(PETSC_COMM_SELF,boom()); // OK
684: }
686: ~baz()
687: {
688: PetscCallAbort(PETSC_COMM_SELF,boom()); // OK (in fact the only way to handle PETSc errors)
689: }
690: };
691: .ve
693: Fortran Note:
694: Use `PetscCallA()`.
696: Developer Note:
697: This should have the same name in Fortran as in C.
699: .seealso: `SETERRABORT()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`,
700: `SETERRQ()`, `CHKMEMQ`, `PetscCallMPI()`, `PetscCallCXXAbort()`
701: M*/
702: #if defined(PETSC_CLANG_STATIC_ANALYZER)
703: void PetscCallAbort(MPI_Comm, PetscErrorCode);
704: void PetscCallContinue(PetscErrorCode);
705: #else
706: #define PetscCallAbort(comm, ...) \
707: do { \
708: PetscErrorCode ierr_petsc_call_abort_; \
709: PetscStackUpdateLine; \
710: ierr_petsc_call_abort_ = __VA_ARGS__; \
711: if (PetscUnlikely(ierr_petsc_call_abort_ != PETSC_SUCCESS)) { \
712: ierr_petsc_call_abort_ = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_abort_, PETSC_ERROR_REPEAT, " "); \
713: (void)MPI_Abort(comm, (PetscMPIInt)ierr_petsc_call_abort_); \
714: } \
715: } while (0)
716: #define PetscCallContinue(...) \
717: do { \
718: PetscErrorCode ierr_petsc_call_continue_; \
719: PetscStackUpdateLine; \
720: ierr_petsc_call_continue_ = __VA_ARGS__; \
721: if (PetscUnlikely(ierr_petsc_call_continue_ != PETSC_SUCCESS)) { \
722: ierr_petsc_call_continue_ = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_continue_, PETSC_ERROR_REPEAT, " "); \
723: (void)ierr_petsc_call_continue_; \
724: } \
725: } while (0)
726: #endif
728: /*MC
729: CHKERRABORT - Checks error code returned from PETSc function. If non-zero it aborts immediately.
731: Synopsis:
732: #include <petscerror.h>
733: void CHKERRABORT(MPI_Comm comm, PetscErrorCode ierr)
735: Not Collective
737: Input Parameters:
738: + comm - the MPI communicator
739: - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
741: Level: deprecated
743: Note:
744: Deprecated in favor of `PetscCallAbort()`. This routine behaves identically to it.
746: .seealso: `PetscCallAbort()`
747: M*/
748: #define CHKERRABORT(comm, ...) PetscCallAbort(comm, __VA_ARGS__)
749: #define CHKERRCONTINUE(...) PetscCallContinue(__VA_ARGS__)
751: /*MC
752: CHKERRA - Fortran-only replacement for use of `CHKERRQ()` in the main program, which aborts immediately
754: Synopsis:
755: #include <petscsys.h>
756: PetscErrorCode CHKERRA(PetscErrorCode ierr)
758: Not Collective
760: Input Parameter:
761: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
763: Level: deprecated
765: Note:
766: This macro is rarely needed, normal usage is `PetscCallA()` in the main Fortran program.
768: Developer Note:
769: Why isn't this named `CHKERRABORT()` in Fortran?
771: .seealso: `PetscCall()`, `PetscCallA()`, `PetscCallAbort()`, `CHKERRQ()`, `SETERRA()`, `SETERRQ()`, `SETERRABORT()`
772: M*/
774: PETSC_EXTERN PetscBool petscwaitonerrorflg;
775: PETSC_EXTERN PetscBool petscindebugger;
776: PETSC_EXTERN PetscBool petscabortmpifinalize;
778: /*MC
779: PETSCABORT - Call `MPI_Abort()` with an informative error code
781: Synopsis:
782: #include <petscsys.h>
783: PETSCABORT(MPI_Comm comm, PetscErrorCode ierr)
785: Collective; No Fortran Support
787: Input Parameters:
788: + comm - A communicator, so that the error can be collective
789: - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
791: Level: advanced
793: Notes:
794: If the option `-start_in_debugger` was used then this calls `abort()` to stop the program in the debugger.
796: if `PetscCIEnabledPortableErrorOutput` is set, which means the code is running in the PETSc test harness (make test),
797: and `comm` is `MPI_COMM_WORLD` it strives to exit cleanly without calling `MPI_Abort()` and instead calling `MPI_Finalize()`.
799: This is currently only used when an error propagates up to the C `main()` program and is detected by a `PetscCall()`, `PetscCallMPI()`,
800: or is set in `main()` with `SETERRQ()`. Abort calls such as `SETERRABORT()`,
801: `PetscCheckAbort()`, `PetscCallMPIAbort()`, and `PetscCallAbort()` always call `MPI_Abort()` and do not have any special
802: handling for the test harness.
804: Developer Note:
805: Should the other abort calls also pass through this call instead of calling `MPI_Abort()` directly?
807: .seealso: `PetscError()`, `PetscCall()`, `SETERRABORT()`, `PetscCheckAbort()`, `PetscCallMPIAbort()`, `PetscCall()`, `PetscCallMPI()`,
808: `PetscCallAbort()`, `MPI_Abort()`
809: M*/
810: #if defined(PETSC_CLANG_STATIC_ANALYZER)
811: void PETSCABORT(MPI_Comm, PetscErrorCode);
812: #else
813: #define PETSCABORT(comm, ...) \
814: do { \
815: PetscErrorCode ierr_petsc_abort_; \
816: if (petscwaitonerrorflg) { ierr_petsc_abort_ = PetscSleep(1000); } \
817: if (petscindebugger) { \
818: abort(); \
819: } else { \
820: PetscMPIInt size_; \
821: ierr_petsc_abort_ = __VA_ARGS__; \
822: MPI_Comm_size(comm, &size_); \
823: if (PetscCIEnabledPortableErrorOutput && (size_ == PetscGlobalSize || petscabortmpifinalize) && ierr_petsc_abort_ != PETSC_ERR_SIG) { \
824: MPI_Finalize(); \
825: exit(0); \
826: } else if (PetscCIEnabledPortableErrorOutput && PetscGlobalSize == 1) { \
827: exit(0); \
828: } else { \
829: MPI_Abort(comm, (PetscMPIInt)ierr_petsc_abort_); \
830: } \
831: } \
832: } while (0)
833: #endif
835: #ifdef PETSC_CLANGUAGE_CXX
836: /*MC
837: PetscCallThrow - Checks error code, if non-zero it calls the C++ error handler which throws
838: an exception
840: Synopsis:
841: #include <petscerror.h>
842: void PetscCallThrow(PetscErrorCode ierr)
844: Not Collective
846: Input Parameter:
847: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
849: Level: beginner
851: Notes:
852: Requires PETSc to be configured with clanguage = c++. Throws a std::runtime_error() on error.
854: Once the error handler throws the exception you can use `PetscCallVoid()` which returns without
855: an error code (bad idea since the error is ignored) or `PetscCallAbort()` to have `MPI_Abort()`
856: called immediately.
858: .seealso: `SETERRQ()`, `PetscCall()`, `SETERRABORT()`, `PetscCallAbort()`, `PetscTraceBackErrorHandler()`,
859: `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
860: M*/
861: #define PetscCallThrow(...) \
862: do { \
863: PetscStackUpdateLine; \
864: PetscErrorCode ierr_petsc_call_throw_ = __VA_ARGS__; \
865: 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); \
866: } while (0)
868: /*MC
869: CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
871: Synopsis:
872: #include <petscerror.h>
873: void CHKERRXX(PetscErrorCode ierr)
875: Not Collective
877: Input Parameter:
878: . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
880: Level: deprecated
882: Note:
883: Deprecated in favor of `PetscCallThrow()`. This routine behaves identically to it.
885: .seealso: `PetscCallThrow()`
886: M*/
887: #define CHKERRXX(...) PetscCallThrow(__VA_ARGS__)
888: #endif
890: #define PetscCallCXX_Private(__SETERR_FUNC__, __COMM__, ...) \
891: do { \
892: PetscStackUpdateLine; \
893: try { \
894: __VA_ARGS__; \
895: } catch (const std::exception &e) { \
896: __SETERR_FUNC__(__COMM__, PETSC_ERR_LIB, "%s", e.what()); \
897: } \
898: } while (0)
900: /*MC
901: PetscCallCXX - Checks C++ function calls and if they throw an exception, catch it and then
902: return a PETSc error code
904: Synopsis:
905: #include <petscerror.h>
906: void PetscCallCXX(...) noexcept;
908: Not Collective
910: Input Parameter:
911: . __VA_ARGS__ - An arbitrary expression
913: Level: beginner
915: Notes:
916: `PetscCallCXX(...)` is a macro replacement for
917: .vb
918: try {
919: __VA_ARGS__;
920: } catch (const std::exception& e) {
921: return ConvertToPetscErrorCode(e);
922: }
923: .ve
924: Due to the fact that it catches any (reasonable) exception, it is essentially noexcept.
926: If you cannot return a `PetscErrorCode` use `PetscCallCXXAbort()` instead.
928: Example Usage:
929: .vb
930: void foo(void) { throw std::runtime_error("error"); }
932: void bar()
933: {
934: PetscCallCXX(foo()); // ERROR bar() does not return PetscErrorCode
935: }
937: PetscErrorCode baz()
938: {
939: PetscCallCXX(foo()); // OK
941: PetscCallCXX(
942: bar();
943: foo(); // OK multiple statements allowed
944: );
945: }
947: struct bop
948: {
949: bop()
950: {
951: PetscCallCXX(foo()); // ERROR returns PetscErrorCode, cannot be used in constructors
952: }
953: };
955: // ERROR contains do-while, cannot be used as function-try block
956: PetscErrorCode qux() PetscCallCXX(
957: bar();
958: baz();
959: foo();
960: return 0;
961: )
962: .ve
964: .seealso: `PetscCallCXXAbort()`, `PetscCallThrow()`, `SETERRQ()`, `PetscCall()`,
965: `SETERRABORT()`, `PetscCallAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
966: `PetscError()`, `CHKMEMQ`
967: M*/
968: #define PetscCallCXX(...) PetscCallCXX_Private(SETERRQ, PETSC_COMM_SELF, __VA_ARGS__)
970: /*MC
971: PetscCallCXXAbort - Like `PetscCallCXX()` but calls `MPI_Abort()` instead of returning an
972: error-code
974: Synopsis:
975: #include <petscerror.h>
976: void PetscCallCXXAbort(MPI_Comm comm, ...) noexcept;
978: Collective; No Fortran Support
980: Input Parameters:
981: + comm - The MPI communicator to abort on
982: - __VA_ARGS__ - An arbitrary expression
984: Level: beginner
986: Notes:
987: This macro may be used to check C++ expressions for exceptions in cases where you cannot
988: return an error code. This includes constructors, destructors, copy/move assignment functions
989: or constructors among others.
991: If an exception is caught, the macro calls `SETERRABORT()` on `comm`. The exception must
992: derive from `std::exception` in order to be caught.
994: If the routine _can_ return an error-code it is highly advised to use `PetscCallCXX()`
995: instead.
997: See `PetscCallCXX()` for additional discussion.
999: Example Usage:
1000: .vb
1001: class Foo
1002: {
1003: std::vector<int> data_;
1005: public:
1006: // normally std::vector::reserve() may raise an exception, but since we handle it with
1007: // PetscCallCXXAbort() we may mark this routine as noexcept!
1008: Foo() noexcept
1009: {
1010: PetscCallCXXAbort(PETSC_COMM_SELF, data_.reserve(10));
1011: }
1012: };
1014: std::vector<int> bar()
1015: {
1016: std::vector<int> v;
1018: PetscFunctionBegin;
1019: // OK!
1020: PetscCallCXXAbort(PETSC_COMM_SELF, v.emplace_back(1));
1021: PetscFunctionReturn(v);
1022: }
1024: PetscErrorCode baz()
1025: {
1026: std::vector<int> v;
1028: PetscFunctionBegin;
1029: // WRONG! baz() returns a PetscErrorCode, prefer PetscCallCXX() instead
1030: PetscCallCXXAbort(PETSC_COMM_SELF, v.emplace_back(1));
1031: PetscFunctionReturn(PETSC_SUCCESS);
1032: }
1033: .ve
1035: .seealso: `PetscCallCXX()`, `SETERRABORT()`, `PetscCallAbort()`
1036: M*/
1037: #define PetscCallCXXAbort(comm, ...) PetscCallCXX_Private(SETERRABORT, comm, __VA_ARGS__)
1039: /*MC
1040: CHKERRCXX - Checks C++ function calls and if they throw an exception, catch it and then
1041: return a PETSc error code
1043: Synopsis:
1044: #include <petscerror.h>
1045: void CHKERRCXX(func) noexcept;
1047: Not Collective
1049: Input Parameter:
1050: . func - C++ function calls
1052: Level: deprecated
1054: Note:
1055: Deprecated in favor of `PetscCallCXX()`. This routine behaves identically to it.
1057: .seealso: `PetscCallCXX()`
1058: M*/
1059: #define CHKERRCXX(...) PetscCallCXX(__VA_ARGS__)
1061: /*MC
1062: CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
1064: Synopsis:
1065: #include <petscsys.h>
1066: CHKMEMQ;
1068: Not Collective
1070: Level: beginner
1072: Notes:
1073: We highly recommend using Valgrind https://petsc.org/release/faq/#valgrind or for NVIDIA CUDA systems
1074: https://docs.nvidia.com/cuda/cuda-memcheck/index.html for finding memory problems. The ``CHKMEMQ`` macro is useful on systems that
1075: do not have valgrind, but is not as good as valgrind or cuda-memcheck.
1077: Must run with the option `-malloc_debug` (`-malloc_test` in debug mode; or if `PetscMallocSetDebug()` called) to enable this option
1079: Once the error handler is called the calling function is then returned from with the given error code.
1081: By defaults prints location where memory that is corrupted was allocated.
1083: Use `CHKMEMA` for functions that return void
1085: .seealso: `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `SETERRQ()`, `PetscMallocValidate()`
1086: M*/
1087: #if defined(PETSC_CLANG_STATIC_ANALYZER)
1088: #define CHKMEMQ
1089: #define CHKMEMA
1090: #else
1091: #define CHKMEMQ \
1092: do { \
1093: PetscErrorCode ierr_petsc_memq_ = PetscMallocValidate(__LINE__, PETSC_FUNCTION_NAME, __FILE__); \
1094: if (PetscUnlikely(ierr_petsc_memq_ != PETSC_SUCCESS)) return PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_memq_, PETSC_ERROR_REPEAT, " "); \
1095: } while (0)
1096: #define CHKMEMA PetscMallocValidate(__LINE__, PETSC_FUNCTION_NAME, __FILE__)
1097: #endif
1099: /*E
1100: PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
1102: Level: advanced
1104: Note:
1105: `PETSC_ERROR_IN_CXX` indicates the error was detected in C++ and an exception should be generated
1107: Developer Note:
1108: This is currently used to decide when to print the detailed information about the run in `PetscTraceBackErrorHandler()`
1110: .seealso: `PetscError()`, `SETERRQ()`
1111: E*/
1112: typedef enum {
1113: PETSC_ERROR_INITIAL = 0,
1114: PETSC_ERROR_REPEAT = 1,
1115: PETSC_ERROR_IN_CXX = 2
1116: } PetscErrorType;
1118: #if defined(__clang_analyzer__)
1119: __attribute__((analyzer_noreturn))
1120: #endif
1121: PETSC_EXTERN PetscErrorCode
1122: PetscError(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, ...) PETSC_ATTRIBUTE_COLD PETSC_ATTRIBUTE_FORMAT(7, 8);
1124: PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
1125: PETSC_EXTERN PetscErrorCode PetscErrorMessage(PetscErrorCode, const char *[], char **);
1126: PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1127: PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1128: PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1129: PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1130: PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1131: PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1132: PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *) PETSC_ATTRIBUTE_COLD;
1133: PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm, int, const char *, const char *, PetscErrorCode, PetscErrorType, const char *, void *), void *);
1134: PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
1135: PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int, void *);
1136: PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int, void *), void *);
1137: PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
1138: PETSC_EXTERN PetscErrorCode PetscCheckPointerSetIntensity(PetscInt);
1139: PETSC_EXTERN void PetscSignalSegvCheckPointerOrMpi(void);
1140: PETSC_DEPRECATED_FUNCTION(3, 13, 0, "PetscSignalSegvCheckPointerOrMpi()", ) static inline void PetscSignalSegvCheckPointer(void)
1141: {
1142: PetscSignalSegvCheckPointerOrMpi();
1143: }
1145: /*MC
1146: PetscErrorPrintf - Prints error messages.
1148: Synopsis:
1149: #include <petscsys.h>
1150: PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
1152: Not Collective; No Fortran Support
1154: Input Parameter:
1155: . format - the usual `printf()` format string
1157: Options Database Keys:
1158: + -error_output_stdout - cause error messages to be printed to stdout instead of the (default) stderr
1159: - -error_output_none - to turn off all printing of error messages (does not change the way the error is handled.)
1161: Level: developer
1163: Notes:
1164: Use
1165: .vb
1166: PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
1167: error is handled.) and
1168: PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on or you can use your own function
1169: .ve
1170: Use
1171: .vb
1172: `PETSC_STDERR` = FILE* obtained from a file open etc. to have stderr printed to the file.
1173: `PETSC_STDOUT` = FILE* obtained from a file open etc. to have stdout printed to the file.
1174: .ve
1176: Use
1177: `PetscPushErrorHandler()` to provide your own error handler that determines what kind of messages to print
1179: .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscHelpPrintf()`, `PetscPrintf()`, `PetscPushErrorHandler()`, `PetscVFPrintf()`, `PetscHelpPrintf()`
1180: M*/
1181: PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[], ...) PETSC_ATTRIBUTE_FORMAT(1, 2);
1183: /*E
1184: PetscFPTrap - types of floating point exceptions that may be trapped
1186: Currently only `PETSC_FP_TRAP_OFF` and `PETSC_FP_TRAP_ON` are handled. All others are treated as `PETSC_FP_TRAP_ON`.
1188: Level: intermediate
1190: .seealso: `PetscSetFPTrap()`, `PetscFPTrapPush()`
1191: E*/
1192: typedef enum {
1193: PETSC_FP_TRAP_OFF = 0,
1194: PETSC_FP_TRAP_INDIV = 1,
1195: PETSC_FP_TRAP_FLTOPERR = 2,
1196: PETSC_FP_TRAP_FLTOVF = 4,
1197: PETSC_FP_TRAP_FLTUND = 8,
1198: PETSC_FP_TRAP_FLTDIV = 16,
1199: PETSC_FP_TRAP_FLTINEX = 32
1200: } PetscFPTrap;
1201: #define PETSC_FP_TRAP_ON (PetscFPTrap)(PETSC_FP_TRAP_INDIV | PETSC_FP_TRAP_FLTOPERR | PETSC_FP_TRAP_FLTOVF | PETSC_FP_TRAP_FLTDIV | PETSC_FP_TRAP_FLTINEX)
1202: PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
1203: PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
1204: PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
1205: PETSC_EXTERN PetscErrorCode PetscDetermineInitialFPTrap(void);
1207: /*
1208: Allows the code to build a stack frame as it runs
1209: */
1211: #define PETSCSTACKSIZE 64
1212: typedef struct {
1213: const char *function[PETSCSTACKSIZE];
1214: const char *file[PETSCSTACKSIZE];
1215: int line[PETSCSTACKSIZE];
1216: int petscroutine[PETSCSTACKSIZE]; /* 0 external called from petsc, 1 petsc functions, 2 petsc user functions */
1217: int currentsize;
1218: int hotdepth;
1219: PetscBool check; /* option to check for correct Push/Pop semantics, true for default petscstack but not other stacks */
1220: } PetscStack;
1221: #if defined(PETSC_USE_DEBUG) && !defined(PETSC_HAVE_THREADSAFETY)
1222: PETSC_EXTERN PetscStack petscstack;
1223: #endif
1225: #if defined(PETSC_SERIALIZE_FUNCTIONS)
1226: #include <petsc/private/petscfptimpl.h>
1227: /*
1228: Registers the current function into the global function pointer to function name table
1230: Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
1231: */
1232: #define PetscRegister__FUNCT__() \
1233: do { \
1234: static PetscBool __chked = PETSC_FALSE; \
1235: if (!__chked) { \
1236: void *ptr; \
1237: PetscCallAbort(PETSC_COMM_SELF, PetscDLSym(NULL, PETSC_FUNCTION_NAME, &ptr)); \
1238: __chked = PETSC_TRUE; \
1239: } \
1240: } while (0)
1241: #else
1242: #define PetscRegister__FUNCT__()
1243: #endif
1245: #if defined(PETSC_CLANG_STATIC_ANALYZER) || defined(__clang_analyzer__)
1246: #define PetscStackPushNoCheck(funct, petsc_routine, hot)
1247: #define PetscStackUpdateLine
1248: #define PetscStackPushExternal(funct)
1249: #define PetscStackPopNoCheck
1250: #define PetscStackClearTop
1251: #define PetscFunctionBegin
1252: #define PetscFunctionBeginUser
1253: #define PetscFunctionBeginHot
1254: #define PetscFunctionReturn(...) return __VA_ARGS__
1255: #define PetscFunctionReturnVoid() return
1256: #define PetscStackPop
1257: #define PetscStackPush(f)
1258: #elif defined(PETSC_USE_DEBUG) && !defined(PETSC_HAVE_THREADSAFETY)
1260: #define PetscStackPush_Private(stack__, file__, func__, line__, petsc_routine__, hot__) \
1261: do { \
1262: if (stack__.currentsize < PETSCSTACKSIZE) { \
1263: stack__.function[stack__.currentsize] = func__; \
1264: if (petsc_routine__) { \
1265: stack__.file[stack__.currentsize] = file__; \
1266: stack__.line[stack__.currentsize] = line__; \
1267: } else { \
1268: stack__.file[stack__.currentsize] = PETSC_NULLPTR; \
1269: stack__.line[stack__.currentsize] = 0; \
1270: } \
1271: stack__.petscroutine[stack__.currentsize] = petsc_routine__; \
1272: } \
1273: ++stack__.currentsize; \
1274: stack__.hotdepth += (hot__ || stack__.hotdepth); \
1275: } while (0)
1277: /* uses PetscCheckAbort() because may be used in a function that does not return an error code */
1278: #define PetscStackPop_Private(stack__, func__) \
1279: do { \
1280: 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__); \
1281: if (--stack__.currentsize < PETSCSTACKSIZE) { \
1282: 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", \
1283: stack__.function[stack__.currentsize], stack__.file[stack__.currentsize], stack__.line[stack__.currentsize], func__, __FILE__, __LINE__); \
1284: stack__.function[stack__.currentsize] = PETSC_NULLPTR; \
1285: stack__.file[stack__.currentsize] = PETSC_NULLPTR; \
1286: stack__.line[stack__.currentsize] = 0; \
1287: stack__.petscroutine[stack__.currentsize] = 0; \
1288: } \
1289: stack__.hotdepth = PetscMax(stack__.hotdepth - 1, 0); \
1290: } while (0)
1292: /*MC
1293: PetscStackPushNoCheck - Pushes a new function name and line number onto the PETSc default stack that tracks where the running program is
1294: currently in the source code.
1296: Synopsis:
1297: #include <petscsys.h>
1298: void PetscStackPushNoCheck(char *funct,int petsc_routine,PetscBool hot);
1300: Not Collective
1302: Input Parameters:
1303: + funct - the function name
1304: . petsc_routine - 2 user function, 1 PETSc function, 0 some other function
1305: - hot - indicates that the function may be called often so expensive error checking should be turned off inside the function
1307: Level: developer
1309: Notes:
1310: 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
1311: 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
1312: help debug the problem.
1314: This version does not check the memory corruption (an expensive operation), use `PetscStackPush()` to check the memory.
1316: Use `PetscStackPushExternal()` for a function call that is about to be made to a non-PETSc or user function (such as BLAS etc).
1318: The default stack is a global variable called `petscstack`.
1320: .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1321: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPush()`, `PetscStackPop`,
1322: `PetscStackPushExternal()`
1323: M*/
1324: #define PetscStackPushNoCheck(funct, petsc_routine, hot) \
1325: do { \
1326: PetscStackSAWsTakeAccess(); \
1327: PetscStackPush_Private(petscstack, __FILE__, funct, __LINE__, petsc_routine, hot); \
1328: PetscStackSAWsGrantAccess(); \
1329: } while (0)
1331: /*MC
1332: PetscStackUpdateLine - in a function that has a `PetscFunctionBegin` or `PetscFunctionBeginUser` updates the stack line number to the
1333: current line number.
1335: Synopsis:
1336: #include <petscsys.h>
1337: void PetscStackUpdateLine
1339: Not Collective
1341: Level: developer
1343: Notes:
1344: Using `PetscCall()` and friends automatically handles this process
1346: 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
1347: occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1348: help debug the problem.
1350: The default stack is a global variable called petscstack.
1352: This is used by `PetscCall()` and is otherwise not like to be needed
1354: .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPop`, `PetscCall()`
1355: M*/
1356: #define PetscStackUpdateLine \
1357: do { \
1358: if (petscstack.currentsize > 0 && petscstack.function[petscstack.currentsize - 1] == PETSC_FUNCTION_NAME) { petscstack.line[petscstack.currentsize - 1] = __LINE__; } \
1359: } while (0)
1361: /*MC
1362: PetscStackPushExternal - Pushes a new function name onto the PETSc default stack that tracks where the running program is
1363: currently in the source code. Does not include the filename or line number since this is called by the calling routine
1364: for non-PETSc or user functions.
1366: Synopsis:
1367: #include <petscsys.h>
1368: void PetscStackPushExternal(char *funct);
1370: Not Collective
1372: Input Parameter:
1373: . funct - the function name
1375: Level: developer
1377: Notes:
1378: Using `PetscCallExternal()` and friends automatically handles this process
1380: 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
1381: occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1382: help debug the problem.
1384: The default stack is a global variable called `petscstack`.
1386: This is to be used when calling an external package function such as a BLAS function.
1388: This also updates the stack line number for the current stack function.
1390: .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1391: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1392: M*/
1393: #define PetscStackPushExternal(funct) \
1394: do { \
1395: PetscStackUpdateLine; \
1396: PetscStackPushNoCheck(funct, 0, PETSC_TRUE); \
1397: } while (0)
1399: /*MC
1400: PetscStackPopNoCheck - Pops a function name from the PETSc default stack that tracks where the running program is
1401: currently in the source code.
1403: Synopsis:
1404: #include <petscsys.h>
1405: void PetscStackPopNoCheck(char *funct);
1407: Not Collective
1409: Input Parameter:
1410: . funct - the function name
1412: Level: developer
1414: Notes:
1415: Using `PetscCall()`, `PetscCallExternal()`, `PetscCallBack()` and friends negates the need to call this
1417: 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
1418: occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1419: help debug the problem.
1421: The default stack is a global variable called petscstack.
1423: Developer Note:
1424: `PetscStackPopNoCheck()` takes a function argument while `PetscStackPop` does not, this difference is likely just historical.
1426: .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1427: M*/
1428: #define PetscStackPopNoCheck(funct) \
1429: do { \
1430: PetscStackSAWsTakeAccess(); \
1431: PetscStackPop_Private(petscstack, funct); \
1432: PetscStackSAWsGrantAccess(); \
1433: } while (0)
1435: #define PetscStackClearTop \
1436: do { \
1437: PetscStackSAWsTakeAccess(); \
1438: if (petscstack.currentsize > 0 && --petscstack.currentsize < PETSCSTACKSIZE) { \
1439: petscstack.function[petscstack.currentsize] = PETSC_NULLPTR; \
1440: petscstack.file[petscstack.currentsize] = PETSC_NULLPTR; \
1441: petscstack.line[petscstack.currentsize] = 0; \
1442: petscstack.petscroutine[petscstack.currentsize] = 0; \
1443: } \
1444: petscstack.hotdepth = PetscMax(petscstack.hotdepth - 1, 0); \
1445: PetscStackSAWsGrantAccess(); \
1446: } while (0)
1448: /*MC
1449: PetscFunctionBegin - First executable line of each PETSc function, used for error handling. Final
1450: line of PETSc functions should be `PetscFunctionReturn`(0);
1452: Synopsis:
1453: #include <petscsys.h>
1454: void PetscFunctionBegin;
1456: Not Collective; No Fortran Support
1458: Usage:
1459: .vb
1460: int something;
1462: PetscFunctionBegin;
1463: .ve
1465: Level: developer
1467: Note:
1468: Use `PetscFunctionBeginUser` for application codes.
1470: .seealso: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`
1472: M*/
1473: #define PetscFunctionBegin \
1474: do { \
1475: PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 1, PETSC_FALSE); \
1476: PetscRegister__FUNCT__(); \
1477: } while (0)
1479: /*MC
1480: PetscFunctionBeginHot - Substitute for `PetscFunctionBegin` to be used in functions that are called in
1481: performance-critical circumstances. Use of this function allows for lighter profiling by default.
1483: Synopsis:
1484: #include <petscsys.h>
1485: void PetscFunctionBeginHot;
1487: Not Collective; No Fortran Support
1489: Usage:
1490: .vb
1491: int something;
1493: PetscFunctionBeginHot;
1494: .ve
1496: Level: developer
1498: .seealso: `PetscFunctionBegin`, `PetscFunctionReturn()`, `PetscStackPushNoCheck()`
1500: M*/
1501: #define PetscFunctionBeginHot \
1502: do { \
1503: PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 1, PETSC_TRUE); \
1504: PetscRegister__FUNCT__(); \
1505: } while (0)
1507: /*MC
1508: PetscFunctionBeginUser - First executable line of user provided routines
1510: Synopsis:
1511: #include <petscsys.h>
1512: void PetscFunctionBeginUser;
1514: Not Collective; No Fortran Support
1516: Usage:
1517: .vb
1518: int something;
1520: PetscFunctionBeginUser;
1521: .ve
1523: Level: intermediate
1525: Notes:
1526: Functions that incorporate this must call `PetscFunctionReturn()` instead of return except for main().
1528: May be used before `PetscInitialize()`
1530: This is identical to `PetscFunctionBegin` except it labels the routine as a user
1531: routine instead of as a PETSc library routine.
1533: .seealso: `PetscFunctionReturn()`, `PetscFunctionBegin`, `PetscFunctionBeginHot`, `PetscStackPushNoCheck()`
1535: M*/
1536: #define PetscFunctionBeginUser \
1537: do { \
1538: PetscStackPushNoCheck(PETSC_FUNCTION_NAME, 2, PETSC_FALSE); \
1539: PetscRegister__FUNCT__(); \
1540: } while (0)
1542: /*MC
1543: PetscStackPush - Pushes a new function name and line number onto the PETSc default stack that tracks where the running program is
1544: currently in the source code and verifies the memory is not corrupted.
1546: Synopsis:
1547: #include <petscsys.h>
1548: void PetscStackPush(char *funct)
1550: Not Collective
1552: Input Parameter:
1553: . funct - the function name
1555: Level: developer
1557: Notes:
1558: 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
1559: occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1560: help debug the problem.
1562: The default stack is a global variable called petscstack.
1564: .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1565: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1566: M*/
1567: #define PetscStackPush(n) \
1568: do { \
1569: PetscStackPushNoCheck(n, 0, PETSC_FALSE); \
1570: CHKMEMQ; \
1571: } while (0)
1573: /*MC
1574: PetscStackPop - Pops a function name from the PETSc default stack that tracks where the running program is
1575: currently in the source code and verifies the memory is not corrupted.
1577: Synopsis:
1578: #include <petscsys.h>
1579: void PetscStackPop
1581: Not Collective
1583: Level: developer
1585: Notes:
1586: 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
1587: occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1588: help debug the problem.
1590: The default stack is a global variable called petscstack.
1592: .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPopNoCheck()`, `PetscStackPush()`
1593: M*/
1594: #define PetscStackPop \
1595: do { \
1596: CHKMEMQ; \
1597: PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1598: } while (0)
1600: /*MC
1601: PetscFunctionReturn - Last executable line of each PETSc function used for error
1602: handling. Replaces `return()`.
1604: Synopsis:
1605: #include <petscerror.h>
1606: void PetscFunctionReturn(...)
1608: Not Collective; No Fortran Support
1610: Level: beginner
1612: Notes:
1613: This routine is a macro, so while it does not "return" anything itself, it does return from
1614: the function in the literal sense.
1616: Usually the return value is the integer literal `0` (for example in any function returning
1617: `PetscErrorCode`), however it is possible to return any arbitrary type. The arguments of
1618: this macro are placed before the `return` statement as-is.
1620: Any routine which returns via `PetscFunctionReturn()` must begin with a corresponding
1621: `PetscFunctionBegin`.
1623: For routines which return `void` use `PetscFunctionReturnVoid()` instead.
1625: Example Usage:
1626: .vb
1627: PetscErrorCode foo(int *x)
1628: {
1629: PetscFunctionBegin; // don't forget the begin!
1630: *x = 10;
1631: PetscFunctionReturn(PETSC_SUCCESS);
1632: }
1633: .ve
1635: May return any arbitrary type\:
1636: .vb
1637: struct Foo
1638: {
1639: int x;
1640: };
1642: struct Foo make_foo(int value)
1643: {
1644: struct Foo f;
1646: PetscFunctionBegin;
1647: f.x = value;
1648: PetscFunctionReturn(f);
1649: }
1650: .ve
1652: .seealso: `PetscFunctionBegin`, `PetscFunctionBeginUser`, `PetscFunctionReturnVoid()`,
1653: `PetscStackPopNoCheck()`
1654: M*/
1655: #define PetscFunctionReturn(...) \
1656: do { \
1657: PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1658: return __VA_ARGS__; \
1659: } while (0)
1661: /*MC
1662: PetscFunctionReturnVoid - Like `PetscFunctionReturn()` but returns `void`
1664: Synopsis:
1665: #include <petscerror.h>
1666: void PetscFunctionReturnVoid()
1668: Not Collective
1670: Level: beginner
1672: Note:
1673: Behaves identically to `PetscFunctionReturn()` except that it returns `void`. That is, this
1674: macro culminates with `return`.
1676: Example Usage:
1677: .vb
1678: void foo()
1679: {
1680: PetscFunctionBegin; // must start with PetscFunctionBegin!
1681: bar();
1682: baz();
1683: PetscFunctionReturnVoid();
1684: }
1685: .ve
1687: .seealso: `PetscFunctionReturn()`, `PetscFunctionBegin`, PetscFunctionBeginUser`
1688: M*/
1689: #define PetscFunctionReturnVoid() \
1690: do { \
1691: PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1692: return; \
1693: } while (0)
1694: #else /* PETSC_USE_DEBUG */
1695: #define PetscStackPushNoCheck(funct, petsc_routine, hot)
1696: #define PetscStackUpdateLine
1697: #define PetscStackPushExternal(funct)
1698: #define PetscStackPopNoCheck(...)
1699: #define PetscStackClearTop
1700: #define PetscFunctionBegin
1701: #define PetscFunctionBeginUser
1702: #define PetscFunctionBeginHot
1703: #define PetscFunctionReturn(...) return __VA_ARGS__
1704: #define PetscFunctionReturnVoid() return
1705: #define PetscStackPop CHKMEMQ
1706: #define PetscStackPush(f) CHKMEMQ
1707: #endif /* PETSC_USE_DEBUG */
1709: #if defined(PETSC_CLANG_STATIC_ANALYZER)
1710: #define PetscStackCallExternalVoid(...)
1711: template <typename F, typename... Args>
1712: void PetscCallExternal(F, Args...);
1713: #else
1714: /*MC
1715: PetscStackCallExternalVoid - Calls an external library routine or user function after pushing the name of the routine on the stack.
1717: Input Parameters:
1718: + name - string that gives the name of the function being called
1719: - routine - actual call to the routine, for example, functionname(a,b)
1721: Level: developer
1723: Note:
1724: Often one should use `PetscCallExternal()` instead. This routine is intended for external library routines that DO NOT return error codes
1726: In debug mode this also checks the memory for corruption at the end of the function call.
1728: Certain external packages, such as BLAS/LAPACK may have their own macros for managing the call, error checking, etc.
1730: Developer Note:
1731: This is so that when a user or external library routine results in a crash or corrupts memory, they get blamed instead of PETSc.
1733: .seealso: `PetscCall()`, `PetscStackPushNoCheck()`, `PetscStackPush()`, `PetscCallExternal()`, `PetscCallBLAS()`
1734: @*/
1735: #define PetscStackCallExternalVoid(name, ...) \
1736: do { \
1737: PetscStackPushExternal(name); \
1738: __VA_ARGS__; \
1739: PetscStackPop; \
1740: } while (0)
1742: /*MC
1743: PetscCallExternal - Calls an external library routine that returns an error code after pushing the name of the routine on the stack.
1745: Input Parameters:
1746: + func- name of the routine
1747: - args - arguments to the routine
1749: Level: developer
1751: Notes:
1752: This is intended for external package routines that return error codes. Use `PetscStackCallExternalVoid()` for those that do not.
1754: In debug mode this also checks the memory for corruption at the end of the function call.
1756: Assumes the error return code of the function is an integer and that a value of 0 indicates success
1758: Developer Note:
1759: This is so that when an external package routine results in a crash or corrupts memory, they get blamed instead of PETSc.
1761: .seealso: `PetscCall()`, `PetscStackPushNoCheck()`, `PetscStackPush()`, `PetscStackCallExternalVoid()`
1762: M*/
1763: #define PetscCallExternal(func, ...) \
1764: do { \
1765: PetscStackPush(PetscStringize(func)); \
1766: int ierr_petsc_call_external_ = func(__VA_ARGS__); \
1767: PetscStackPop; \
1768: PetscCheck(ierr_petsc_call_external_ == 0, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in %s(): error code %d", PetscStringize(func), ierr_petsc_call_external_); \
1769: } while (0)
1770: #endif /* PETSC_CLANG_STATIC_ANALYZER */