forked from petsc/petsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpetscerror.h
1774 lines (1363 loc) · 60.1 KB
/
petscerror.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Contains all error handling interfaces for PETSc.
*/
#pragma once
#include <petscmacros.h>
#include <petscsystypes.h>
#if defined(__cplusplus)
#include <exception> // std::exception
#endif
/* SUBMANSEC = Sys */
#define SETERRQ1(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
#define SETERRQ2(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
#define SETERRQ3(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
#define SETERRQ4(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
#define SETERRQ5(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
#define SETERRQ6(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
#define SETERRQ7(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
#define SETERRQ8(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
#define SETERRQ9(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "SETERRQ", ) SETERRQ(__VA_ARGS__)
/*MC
SETERRQ - Macro to be called when an error has been detected,
Synopsis:
#include <petscsys.h>
PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
Collective
Input Parameters:
+ comm - An MPI communicator, use `PETSC_COMM_SELF` unless you know all ranks of another communicator will detect the error
. ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
- message - error message
Level: beginner
Notes:
This is rarely needed, one should use `PetscCheck()` and `PetscCall()` and friends to automatically handle error conditions.
Once the error handler is called the calling function is then returned from with the given error code.
Experienced users can set the error handler with `PetscPushErrorHandler()`.
Fortran Note:
`SETERRQ()` may be called from Fortran subroutines but `SETERRA()` must be called from the
Fortran main program.
.seealso: `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
`PetscError()`, `PetscCall()`, `CHKMEMQ`, `CHKERRA()`, `PetscCallMPI()`, `PetscErrorCode`
M*/
#define SETERRQ(comm, ierr, ...) \
do { \
PetscErrorCode ierr_seterrq_petsc_ = PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__); \
return ierr_seterrq_petsc_ ? ierr_seterrq_petsc_ : PETSC_ERR_RETURN; \
} while (0)
/*
Returned from PETSc functions that are called from MPI, such as related to attributes
Do not confuse PETSC_MPI_ERROR_CODE and PETSC_ERR_MPI, the first is registered with MPI and returned to MPI as
an error code, the latter is a regular PETSc error code passed within PETSc code indicating an error was detected in an MPI call.
*/
PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CLASS;
PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CODE;
/*MC
SETERRMPI - Macro to be called when an error has been detected within an MPI callback function
No Fortran Support
Synopsis:
#include <petscsys.h>
PetscErrorCode SETERRMPI(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
Collective
Input Parameters:
+ comm - An MPI communicator, use `PETSC_COMM_SELF` unless you know all ranks of another communicator will detect the error
. ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
- message - error message
Level: developer
Note:
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`
which is registered with `MPI_Add_error_code()` when PETSc is initialized.
.seealso: `SETERRQ()`, `PetscCall()`, `PetscCallMPI()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `PetscErrorCode`
M*/
#define SETERRMPI(comm, ierr, ...) return ((void)PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__), PETSC_MPI_ERROR_CODE)
/*MC
SETERRA - Fortran-only macro that can be called when an error has been detected from the main program
Synopsis:
#include <petscsys.h>
PetscErrorCode SETERRA(MPI_Comm comm,PetscErrorCode ierr,char *message)
Collective
Input Parameters:
+ comm - An MPI communicator, so that the error can be collective
. ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
- message - error message in the printf format
Level: beginner
Notes:
This should only be used with Fortran. With C/C++, use `SETERRQ()`.
`SETERRQ()` may be called from Fortran subroutines but `SETERRA()` must be called from the
Fortran main program.
.seealso: `SETERRQ()`, `SETERRABORT()`, `PetscCall()`, `CHKERRA()`, `PetscCallAbort()`, `PetscErrorCode`
M*/
/*MC
SETERRABORT - Macro that can be called when an error has been detected,
Synopsis:
#include <petscsys.h>
PetscErrorCode SETERRABORT(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
Collective
Input Parameters:
+ comm - An MPI communicator, so that the error can be collective
. ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
- message - error message in the printf format
Level: beginner
Notes:
This function just calls `MPI_Abort()`.
This should only be called in routines that cannot return an error code, such as in C++ constructors.
Fortran Note:
Use `SETERRA()` in Fortran main program and `SETERRQ()` in Fortran subroutines
Developer Note:
In Fortran `SETERRA()` could be called `SETERRABORT()` since they serve the same purpose
.seealso: `SETERRQ()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `PetscCall()`, `CHKMEMQ`, `PetscErrorCode`
M*/
#define SETERRABORT(comm, ierr, ...) \
do { \
(void)PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__); \
MPI_Abort(comm, ierr); \
} while (0)
/*MC
PetscCheck - Check that a particular condition is true
Synopsis:
#include <petscerror.h>
void PetscCheck(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
Collective; No Fortran Support
Input Parameters:
+ cond - The boolean condition
. comm - The communicator on which the check can be collective on
. ierr - A nonzero error code, see include/petscerror.h for the complete list
- message - Error message in printf format
Level: beginner
Notes:
Enabled in both optimized and debug builds.
As a general rule, `PetscCheck()` is used to check "usage error" (for example, passing an incorrect value as a function argument),
`PetscAssert()` is used to "check for bugs in PETSc" (for example, is a value in a PETSc data structure nonsensical).
However, for functions that are called in a "hot spot", for example, thousands of times in a loop, `PetscAssert()` should be used instead
of `PetscCheck()` since the former is compiled out in PETSc's optimization code.
Calls `SETERRQ()` if the assertion fails, so can only be called from functions returning a
`PetscErrorCode` (or equivalent type after conversion).
.seealso: `PetscAssert()`, `SETERRQ()`, `PetscError()`, `PetscCall()`, `PetscCheckAbort()`, `PetscErrorCode`
M*/
#define PetscCheck(cond, comm, ierr, ...) \
do { \
if (PetscUnlikely(!(cond))) SETERRQ(comm, ierr, __VA_ARGS__); \
} while (0)
/*MC
PetscCheckAbort - Check that a particular condition is true, otherwise prints error and aborts
Synopsis:
#include <petscerror.h>
void PetscCheckAbort(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
Collective; No Fortran Support
Input Parameters:
+ cond - The boolean condition
. comm - The communicator on which the check can be collective on
. ierr - A nonzero error code, see include/petscerror.h for the complete list
- message - Error message in printf format
Level: developer
Notes:
Enabled in both optimized and debug builds.
Calls `SETERRABORT()` if the assertion fails, can be called from a function that does not return an
error code, such as a C++ constructor. usually `PetscCheck()` should be used.
.seealso: `PetscAssertAbort()`, `PetscAssert()`, `SETERRQ()`, `PetscError()`, `PetscCall()`, `PetscCheck()`, `SETERRABORT()`, `PetscErrorCode`
M*/
#define PetscCheckAbort(cond, comm, ierr, ...) \
do { \
if (PetscUnlikely(!(cond))) SETERRABORT(comm, ierr, __VA_ARGS__); \
} while (0)
/*MC
PetscAssert - Assert that a particular condition is true
Synopsis:
#include <petscerror.h>
void PetscAssert(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
Collective; No Fortran Support
Input Parameters:
+ cond - The boolean condition
. comm - The communicator on which the check can be collective on
. ierr - A nonzero error code, see include/petscerror.h for the complete list
- message - Error message in `printf()` format
Level: beginner
Notes:
Equivalent to `PetscCheck()` if debugging is enabled, and `PetscAssume(cond)` otherwise.
See `PetscCheck()` for usage and behaviour.
This is needed instead of simply using `assert()` because this correctly handles the collective nature of errors under MPI
.seealso: `PetscCheck()`, `SETERRQ()`, `PetscError()`, `PetscAssertAbort()`, `PetscErrorCode`
M*/
#if PetscDefined(USE_DEBUG)
#define PetscAssert(cond, comm, ierr, ...) PetscCheck(cond, comm, ierr, __VA_ARGS__)
#else
#define PetscAssert(cond, ...) PetscAssume(cond)
#endif
/*MC
PetscAssertAbort - Assert that a particular condition is true, otherwise prints error and aborts
Synopsis:
#include <petscerror.h>
void PetscAssertAbort(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
Collective; No Fortran Support
Input Parameters:
+ cond - The boolean condition
. comm - The communicator on which the check can be collective on
. ierr - A nonzero error code, see include/petscerror.h for the complete list
- message - Error message in printf format
Level: beginner
Note:
Enabled only in debug builds. See `PetscCheckAbort()` for usage.
.seealso: `PetscCheckAbort()`, `PetscAssert()`, `PetscCheck()`, `SETERRABORT()`, `PetscError()`
M*/
#if PetscDefined(USE_DEBUG)
#define PetscAssertAbort(cond, comm, ierr, ...) PetscCheckAbort(cond, comm, ierr, __VA_ARGS__)
#else
#define PetscAssertAbort(cond, comm, ierr, ...) PetscAssume(cond)
#endif
/*MC
PetscCall - Calls a PETSc function and then checks the resulting error code, if it is
non-zero it calls the error handler and returns from the current function with the error
code.
Synopsis:
#include <petscerror.h>
void PetscCall(PetscFunction(args))
Not Collective
Input Parameter:
. PetscFunction - any PETSc function that returns an error code
Level: beginner
Notes:
Once the error handler is called the calling function is then returned from with the given
error code. Experienced users can set the error handler with `PetscPushErrorHandler()`.
`PetscCall()` cannot be used in functions returning a datatype not convertible to
`PetscErrorCode`. For example, `PetscCall()` may not be used in functions returning void, use
`PetscCallAbort()` or `PetscCallVoid()` in this case.
Example Usage:
.vb
PetscCall(PetscInitiailize(...)); // OK to call even when PETSc is not yet initialized!
struct my_struct
{
void *data;
} my_complex_type;
struct my_struct bar(void)
{
PetscCall(foo(15)); // ERROR PetscErrorCode not convertible to struct my_struct!
}
PetscCall(bar()) // ERROR input not convertible to PetscErrorCode
.ve
It is also possible to call this directly on a `PetscErrorCode` variable
.vb
PetscCall(ierr); // check if ierr is nonzero
.ve
Should not be used to call callback functions provided by users, `PetscCallBack()` should be used in that situation.
`PetscUseTypeMethod()` or `PetscTryTypeMethod()` should be used when calling functions pointers contained in a PETSc object's `ops` array
Fortran Notes:
The Fortran function from which this is used must declare a variable PetscErrorCode ierr and ierr must be
the final argument to the PETSc function being called.
In the main program and in Fortran subroutines that do not have ierr as the final return parameter one
should use `PetscCallA()`
Example Fortran Usage:
.vb
PetscErrorCode ierr
Vec v
...
PetscCall(VecShift(v,1.0,ierr))
PetscCallA(VecShift(v,1.0,ierr))
.ve
.seealso: `SETERRQ()`, `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`,
`PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`,
`CHKERRMPI()`, `PetscCallBack()`, `PetscCallAbort()`, `PetscCallVoid()`
M*/
/*MC
PetscCallA - Fortran-only macro that should be used in the main program to call PETSc functions instead of using
PetscCall() which should be used in other Fortran subroutines
Synopsis:
#include <petscsys.h>
PetscErrorCode PetscCallA(PetscFunction(arguments,ierr))
Collective
Input Parameter:
. PetscFunction(arguments,ierr) - the call to the function
Level: beginner
Notes:
This should only be used with Fortran. With C/C++, use `PetscCall()` always.
Use `SETERRA()` to set an error in a Fortran main program and `SETERRQ()` in Fortran subroutines
.seealso: `SETERRQ()`, `SETERRA()`, `SETERRABORT()`, `PetscCall()`, `CHKERRA()`, `PetscCallAbort()`
M*/
/*MC
PetscCallBack - Calls a user provided PETSc callback function and then checks the resulting error code, if it is non-zero it calls the error
handler and returns from the current function with the error code.
Synopsis:
#include <petscerror.h>
void PetscCallBack(const char *functionname,PetscFunction(args))
Not Collective; No Fortran Support
Input Parameters:
+ functionname - the name of the function being called, this can be a string with spaces that describes the meaning of the callback
- PetscFunction - user provided callback function that returns an error code
Example Usage:
.vb
PetscCallBack("XXX callback to do something",a->callback(...));
.ve
Level: developer
Notes:
Once the error handler is called the calling function is then returned from with the given
error code. Experienced users can set the error handler with `PetscPushErrorHandler()`.
`PetscCallBack()` should only be called in PETSc when a call is being made to a user provided call-back routine.
.seealso: `SETERRQ()`, `PetscCheck()`, `PetscCall()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`
`PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`, `CHKERRMPI()`, `PetscCall()`
M*/
/*MC
PetscCallVoid - Like `PetscCall()` but for functions returning `void`
Synopsis:
#include <petscerror.h>
void PetscCall(PetscFunction(args))
Not Collective; No Fortran Support
Input Parameter:
. PetscFunction - any PETSc function that returns an error code
Example Usage:
.vb
void foo()
{
KSP ksp;
PetscFunctionBeginUser;
// OK, properly handles PETSc error codes
PetscCallVoid(KSPCreate(PETSC_COMM_WORLD, &ksp));
PetscFunctionReturn(PETSC_SUCCESS);
}
PetscErrorCode bar()
{
KSP ksp;
PetscFunctionBeginUser;
// ERROR, Non-void function 'bar' should return a value
PetscCallVoid(KSPCreate(PETSC_COMM_WORLD, &ksp));
// OK, returning PetscErrorCode
PetscCall(KSPCreate(PETSC_COMM_WORLD, &ksp));
PetscFunctionReturn(PETSC_SUCCESS);
}
.ve
Level: beginner
Notes:
Has identical usage to `PetscCall()`, except that it returns `void` on error instead of a
`PetscErrorCode`. See `PetscCall()` for more detailed discussion.
Note that users should prefer `PetscCallAbort()` to this routine. While this routine does
"handle" errors by returning from the enclosing function, it effectively gobbles the
error. Since the enclosing function itself returns `void`, its callers have no way of knowing
that the routine returned early due to an error. `PetscCallAbort()` at least ensures that the
program crashes gracefully.
.seealso: `PetscCall()`, `PetscErrorCode`
M*/
#if defined(PETSC_CLANG_STATIC_ANALYZER)
void PetscCall(PetscErrorCode);
void PetscCallBack(const char *, PetscErrorCode);
void PetscCallVoid(PetscErrorCode);
#else
#define PetscCall(...) \
do { \
PetscErrorCode ierr_petsc_call_q_; \
PetscStackUpdateLine; \
ierr_petsc_call_q_ = __VA_ARGS__; \
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, " "); \
} while (0)
#define PetscCallBack(function, ...) \
do { \
PetscErrorCode ierr_petsc_call_q_; \
PetscStackUpdateLine; \
PetscStackPushExternal(function); \
ierr_petsc_call_q_ = __VA_ARGS__; \
PetscStackPop; \
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, " "); \
} while (0)
#define PetscCallVoid(...) \
do { \
PetscErrorCode ierr_petsc_call_void_; \
PetscStackUpdateLine; \
ierr_petsc_call_void_ = __VA_ARGS__; \
if (PetscUnlikely(ierr_petsc_call_void_ != PETSC_SUCCESS)) { \
ierr_petsc_call_void_ = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_void_, PETSC_ERROR_REPEAT, " "); \
(void)ierr_petsc_call_void_; \
return; \
} \
} while (0)
#endif
/*MC
CHKERRQ - Checks error code returned from PETSc function
Synopsis:
#include <petscsys.h>
void CHKERRQ(PetscErrorCode ierr)
Not Collective
Input Parameter:
. ierr - nonzero error code
Level: deprecated
Note:
Deprecated in favor of `PetscCall()`. This routine behaves identically to it.
.seealso: `PetscCall()`
M*/
#define CHKERRQ(...) PetscCall(__VA_ARGS__)
#define CHKERRV(...) PetscCallVoid(__VA_ARGS__)
PETSC_EXTERN void PetscMPIErrorString(PetscMPIInt, char *);
/*MC
PetscCallMPI - Checks error code returned from MPI calls, if non-zero it calls the error
handler and then returns
Synopsis:
#include <petscerror.h>
void PetscCallMPI(MPI_Function(args))
Not Collective
Input Parameter:
. MPI_Function - an MPI function that returns an MPI error code
Level: beginner
Notes:
Always returns the error code `PETSC_ERR_MPI`; the MPI error code and string are embedded in
the string error message. Do not use this to call any other routines (for example PETSc
routines), it should only be used for direct MPI calls. The user may configure PETSc with the
`--with-strict-petscerrorcode` option to check this at compile-time, otherwise they must
check this themselves.
This routine can only be used in functions returning `PetscErrorCode` themselves. If the
calling function returns a different type, use `PetscCallMPIAbort()` instead.
Example Usage:
.vb
PetscCallMPI(MPI_Comm_size(...)); // OK, calling MPI function
PetscCallMPI(PetscFunction(...)); // ERROR, use PetscCall() instead!
.ve
Fortran Notes:
The Fortran function from which this is used must declare a variable `PetscErrorCode` ierr and ierr must be
the final argument to the MPI function being called.
In the main program and in Fortran subroutines that do not have ierr as the final return parameter one
should use `PetscCallMPIA()`
Fortran Usage:
.vb
PetscErrorCode ierr or integer ierr
...
PetscCallMPI(MPI_Comm_size(...,ierr))
PetscCallMPIA(MPI_Comm_size(...,ierr)) ! Will abort after calling error handler
PetscCallMPI(MPI_Comm_size(...,eflag)) ! ERROR, final argument must be ierr
.ve
.seealso: `SETERRMPI()`, `PetscCall()`, `SETERRQ()`, `SETERRABORT()`, `PetscCallAbort()`,
`PetscCallMPIAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
`PetscError()`, `CHKMEMQ`
M*/
/*MC
PetscCallMPIAbort - Like `PetscCallMPI()` but calls `MPI_Abort()` on error
Synopsis:
#include <petscerror.h>
void PetscCallMPIAbort(MPI_Comm comm, MPI_Function(args))
Not Collective
Input Parameters:
+ comm - the MPI communicator to abort on
- MPI_Function - an MPI function that returns an MPI error code
Level: beginner
Notes:
Usage is identical to `PetscCallMPI()`. See `PetscCallMPI()` for detailed discussion.
This routine may be used in functions returning `void` or other non-`PetscErrorCode` types.
Fortran Note:
In Fortran this is called `PetscCallMPIA()` and is intended to be used in the main program while `PetscCallMPI()` is
used in Fortran subroutines.
Developer Note:
This should have the same name in Fortran.
.seealso: `PetscCallMPI()`, `PetscCallAbort()`, `SETERRABORT()`
M*/
#if defined(PETSC_CLANG_STATIC_ANALYZER)
void PetscCallMPI(PetscMPIInt);
void PetscCallMPIAbort(MPI_Comm, PetscMPIInt);
#else
#define PetscCallMPI_Private(__PETSC_STACK_POP_FUNC__, __SETERR_FUNC__, __COMM__, ...) \
do { \
PetscMPIInt ierr_petsc_call_mpi_; \
PetscStackUpdateLine; \
PetscStackPushExternal("MPI function"); \
{ \
ierr_petsc_call_mpi_ = __VA_ARGS__; \
} \
__PETSC_STACK_POP_FUNC__; \
if (PetscUnlikely(ierr_petsc_call_mpi_ != MPI_SUCCESS)) { \
char petsc_mpi_7_errorstring[2 * MPI_MAX_ERROR_STRING]; \
PetscMPIErrorString(ierr_petsc_call_mpi_, (char *)petsc_mpi_7_errorstring); \
__SETERR_FUNC__(__COMM__, PETSC_ERR_MPI, "MPI error %d %s", (int)ierr_petsc_call_mpi_, petsc_mpi_7_errorstring); \
} \
} while (0)
#define PetscCallMPI(...) PetscCallMPI_Private(PetscStackPop, SETERRQ, PETSC_COMM_SELF, __VA_ARGS__)
#define PetscCallMPIAbort(comm, ...) PetscCallMPI_Private(PetscStackPopNoCheck(PETSC_FUNCTION_NAME), SETERRABORT, comm, __VA_ARGS__)
#endif
/*MC
CHKERRMPI - Checks error code returned from MPI calls, if non-zero it calls the error
handler and then returns
Synopsis:
#include <petscerror.h>
void CHKERRMPI(PetscErrorCode ierr)
Not Collective
Input Parameter:
. ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
Level: deprecated
Note:
Deprecated in favor of `PetscCallMPI()`. This routine behaves identically to it.
.seealso: `PetscCallMPI()`
M*/
#define CHKERRMPI(...) PetscCallMPI(__VA_ARGS__)
/*MC
PetscCallAbort - Checks error code returned from PETSc function, if non-zero it aborts immediately by calling `MPI_Abort()`
Synopsis:
#include <petscerror.h>
void PetscCallAbort(MPI_Comm comm, PetscErrorCode ierr)
Collective
Input Parameters:
+ comm - the MPI communicator on which to abort
- ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
Level: intermediate
Notes:
This macro has identical type and usage semantics to `PetscCall()` with the important caveat
that this macro does not return. Instead, if ierr is nonzero it calls the PETSc error handler
and then immediately calls `MPI_Abort()`. It can therefore be used anywhere.
As per `MPI_Abort()` semantics the communicator passed must be valid, although there is currently
no attempt made at handling any potential errors from `MPI_Abort()`. Note that while
`MPI_Abort()` is required to terminate only those processes which reside on comm, it is often
the case that `MPI_Abort()` terminates *all* processes.
Example Usage:
.vb
PetscErrorCode boom(void) { return PETSC_ERR_MEM; }
void foo(void)
{
PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
}
double bar(void)
{
PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
}
PetscCallAbort(MPI_COMM_NULL,boom()); // ERROR, communicator should be valid
struct baz
{
baz()
{
PetscCallAbort(PETSC_COMM_SELF,boom()); // OK
}
~baz()
{
PetscCallAbort(PETSC_COMM_SELF,boom()); // OK (in fact the only way to handle PETSc errors)
}
};
.ve
Fortran Note:
Use `PetscCallA()`.
Developer Note:
This should have the same name in Fortran as in C.
.seealso: `SETERRABORT()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`,
`SETERRQ()`, `CHKMEMQ`, `PetscCallMPI()`, `PetscCallCXXAbort()`
M*/
#if defined(PETSC_CLANG_STATIC_ANALYZER)
void PetscCallAbort(MPI_Comm, PetscErrorCode);
void PetscCallContinue(PetscErrorCode);
#else
#define PetscCallAbort(comm, ...) \
do { \
PetscErrorCode ierr_petsc_call_abort_; \
PetscStackUpdateLine; \
ierr_petsc_call_abort_ = __VA_ARGS__; \
if (PetscUnlikely(ierr_petsc_call_abort_ != PETSC_SUCCESS)) { \
ierr_petsc_call_abort_ = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_abort_, PETSC_ERROR_REPEAT, " "); \
(void)MPI_Abort(comm, (PetscMPIInt)ierr_petsc_call_abort_); \
} \
} while (0)
#define PetscCallContinue(...) \
do { \
PetscErrorCode ierr_petsc_call_continue_; \
PetscStackUpdateLine; \
ierr_petsc_call_continue_ = __VA_ARGS__; \
if (PetscUnlikely(ierr_petsc_call_continue_ != PETSC_SUCCESS)) { \
ierr_petsc_call_continue_ = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_petsc_call_continue_, PETSC_ERROR_REPEAT, " "); \
(void)ierr_petsc_call_continue_; \
} \
} while (0)
#endif
/*MC
CHKERRABORT - Checks error code returned from PETSc function. If non-zero it aborts immediately.
Synopsis:
#include <petscerror.h>
void CHKERRABORT(MPI_Comm comm, PetscErrorCode ierr)
Not Collective
Input Parameters:
+ comm - the MPI communicator
- ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
Level: deprecated
Note:
Deprecated in favor of `PetscCallAbort()`. This routine behaves identically to it.
.seealso: `PetscCallAbort()`, `PetscErrorCode`
M*/
#define CHKERRABORT(comm, ...) PetscCallAbort(comm, __VA_ARGS__)
#define CHKERRCONTINUE(...) PetscCallContinue(__VA_ARGS__)
/*MC
CHKERRA - Fortran-only replacement for use of `CHKERRQ()` in the main program, which aborts immediately
Synopsis:
#include <petscsys.h>
PetscErrorCode CHKERRA(PetscErrorCode ierr)
Not Collective
Input Parameter:
. ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
Level: deprecated
Note:
This macro is rarely needed, normal usage is `PetscCallA()` in the main Fortran program.
Developer Note:
Why isn't this named `CHKERRABORT()` in Fortran?
.seealso: `PetscCall()`, `PetscCallA()`, `PetscCallAbort()`, `CHKERRQ()`, `SETERRA()`, `SETERRQ()`, `SETERRABORT()`
M*/
PETSC_EXTERN PetscBool petscwaitonerrorflg;
PETSC_EXTERN PetscBool petscindebugger;
PETSC_EXTERN PetscBool petscabortmpifinalize;
/*MC
PETSCABORT - Call `MPI_Abort()` with an informative error code
Synopsis:
#include <petscsys.h>
PETSCABORT(MPI_Comm comm, PetscErrorCode ierr)
Collective; No Fortran Support
Input Parameters:
+ comm - An MPI communicator, so that the error can be collective
- ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
Level: advanced
Notes:
If the option `-start_in_debugger` was used then this calls `abort()` to stop the program in the debugger.
if `PetscCIEnabledPortableErrorOutput` is set, which means the code is running in the PETSc test harness (make test),
and `comm` is `MPI_COMM_WORLD` it strives to exit cleanly without calling `MPI_Abort()` and instead calling `MPI_Finalize()`.
This is currently only used when an error propagates up to the C `main()` program and is detected by a `PetscCall()`, `PetscCallMPI()`,
or is set in `main()` with `SETERRQ()`. Abort calls such as `SETERRABORT()`,
`PetscCheckAbort()`, `PetscCallMPIAbort()`, and `PetscCallAbort()` always call `MPI_Abort()` and do not have any special
handling for the test harness.
Developer Note:
Should the other abort calls also pass through this call instead of calling `MPI_Abort()` directly?
.seealso: `PetscError()`, `PetscCall()`, `SETERRABORT()`, `PetscCheckAbort()`, `PetscCallMPIAbort()`, `PetscCall()`, `PetscCallMPI()`,
`PetscCallAbort()`, `MPI_Abort()`, `PetscErrorCode`
M*/
#if defined(PETSC_CLANG_STATIC_ANALYZER)
void PETSCABORT(MPI_Comm, PetscErrorCode);
#else
#define PETSCABORT(comm, ...) \
do { \
PetscErrorCode ierr_petsc_abort_; \
if (petscwaitonerrorflg) { ierr_petsc_abort_ = PetscSleep(1000); } \
if (petscindebugger) { \
abort(); \
} else { \
PetscMPIInt size_; \
ierr_petsc_abort_ = __VA_ARGS__; \
MPI_Comm_size(comm, &size_); \
if (PetscCIEnabledPortableErrorOutput && (size_ == PetscGlobalSize || petscabortmpifinalize) && ierr_petsc_abort_ != PETSC_ERR_SIG) { \
MPI_Finalize(); \
exit(0); \
} else if (PetscCIEnabledPortableErrorOutput && PetscGlobalSize == 1) { \
exit(0); \
} else { \
MPI_Abort(comm, (PetscMPIInt)ierr_petsc_abort_); \
} \
} \
} while (0)
#endif
#ifdef PETSC_CLANGUAGE_CXX
/*MC
PetscCallThrow - Checks error code, if non-zero it calls the C++ error handler which throws
an exception
Synopsis:
#include <petscerror.h>
void PetscCallThrow(PetscErrorCode ierr)
Not Collective
Input Parameter:
. ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
Level: beginner
Notes:
Requires PETSc to be configured with clanguage of c++. Throws a std::runtime_error() on error.
Once the error handler throws the exception you can use `PetscCallVoid()` which returns without
an error code (bad idea since the error is ignored) or `PetscCallAbort()` to have `MPI_Abort()`
called immediately.
.seealso: `SETERRQ()`, `PetscCall()`, `SETERRABORT()`, `PetscCallAbort()`, `PetscTraceBackErrorHandler()`,
`PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
M*/
#define PetscCallThrow(...) \
do { \
PetscStackUpdateLine; \
PetscErrorCode ierr_petsc_call_throw_ = __VA_ARGS__; \
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); \
} while (0)
/*MC
CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
Synopsis:
#include <petscerror.h>
void CHKERRXX(PetscErrorCode ierr)
Not Collective
Input Parameter:
. ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
Level: deprecated
Note:
Deprecated in favor of `PetscCallThrow()`. This routine behaves identically to it.
.seealso: `PetscCallThrow()`
M*/
#define CHKERRXX(...) PetscCallThrow(__VA_ARGS__)
#endif
#define PetscCallCXX_Private(__SETERR_FUNC__, __COMM__, ...) \
do { \
PetscStackUpdateLine; \
try { \
__VA_ARGS__; \
} catch (const std::exception &e) { \
__SETERR_FUNC__(__COMM__, PETSC_ERR_LIB, "%s", e.what()); \
} \
} while (0)
/*MC
PetscCallCXX - Checks C++ function calls and if they throw an exception, catch it and then
return a PETSc error code
Synopsis:
#include <petscerror.h>
void PetscCallCXX(...) noexcept;
Not Collective
Input Parameter:
. __VA_ARGS__ - An arbitrary expression
Level: beginner
Notes:
`PetscCallCXX(...)` is a macro replacement for
.vb
try {
__VA_ARGS__;
} catch (const std::exception& e) {
return ConvertToPetscErrorCode(e);
}
.ve
Due to the fact that it catches any (reasonable) exception, it is essentially noexcept.
If you cannot return a `PetscErrorCode` use `PetscCallCXXAbort()` instead.
Example Usage:
.vb
void foo(void) { throw std::runtime_error("error"); }
void bar()
{
PetscCallCXX(foo()); // ERROR bar() does not return PetscErrorCode
}
PetscErrorCode baz()
{
PetscCallCXX(foo()); // OK
PetscCallCXX(
bar();
foo(); // OK multiple statements allowed
);
}
struct bop
{
bop()
{
PetscCallCXX(foo()); // ERROR returns PetscErrorCode, cannot be used in constructors
}
};
// ERROR contains do-while, cannot be used as function-try block
PetscErrorCode qux() PetscCallCXX(
bar();
baz();
foo();
return 0;
)
.ve
.seealso: `PetscCallCXXAbort()`, `PetscCallThrow()`, `SETERRQ()`, `PetscCall()`,
`SETERRABORT()`, `PetscCallAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
`PetscError()`, `CHKMEMQ`
M*/
#define PetscCallCXX(...) PetscCallCXX_Private(SETERRQ, PETSC_COMM_SELF, __VA_ARGS__)
/*MC
PetscCallCXXAbort - Like `PetscCallCXX()` but calls `MPI_Abort()` instead of returning an
error-code
Synopsis:
#include <petscerror.h>
void PetscCallCXXAbort(MPI_Comm comm, ...) noexcept;
Collective; No Fortran Support
Input Parameters:
+ comm - The MPI communicator to abort on
- __VA_ARGS__ - An arbitrary expression
Level: beginner
Notes:
This macro may be used to check C++ expressions for exceptions in cases where you cannot
return an error code. This includes constructors, destructors, copy/move assignment functions
or constructors among others.
If an exception is caught, the macro calls `SETERRABORT()` on `comm`. The exception must
derive from `std::exception` in order to be caught.
If the routine _can_ return an error-code it is highly advised to use `PetscCallCXX()`
instead.