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