xref: /petsc/include/petscerror.h (revision 267267bdcbfbae225216e8078da17f7ca9d5f149)
1 /*
2     Contains all error handling interfaces for PETSc.
3 */
4 #if !defined(PETSCERROR_H)
5 #define PETSCERROR_H
6 
7 #include <petscmacros.h>
8 #include <petscsystypes.h>
9 
10 /*
11      These are the generic error codes. These error codes are used
12      many different places in the PETSc source code. The string versions are
13      at src/sys/error/err.c any changes here must also be made there
14      These are also define in src/sys/f90-mod/petscerror.h any CHANGES here
15      must be also made there.
16 
17 */
18 #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
19 
20 #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
21 #define PETSC_ERR_SUP              56   /* no support for requested operation */
22 #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
23 #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
24 #define PETSC_ERR_SIG              59   /* signal received */
25 #define PETSC_ERR_FP               72   /* floating point exception */
26 #define PETSC_ERR_COR              74   /* corrupted PETSc object */
27 #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
28 #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
29 #define PETSC_ERR_MEMC             78   /* memory corruption */
30 #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
31 #define PETSC_ERR_USER             83   /* user has not provided needed function */
32 #define PETSC_ERR_SYS              88   /* error in system call */
33 #define PETSC_ERR_POINTER          70   /* pointer does not point to valid address */
34 #define PETSC_ERR_MPI_LIB_INCOMP   87   /* MPI library at runtime is not compatible with MPI user compiled with */
35 
36 #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
37 #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
38 #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
39 #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
40 #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
41 #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
42 #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
43 #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
44 #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
45 #define PETSC_ERR_ARG_TYPENOTSET   89   /* the type of the object has not yet been set */
46 #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
47 #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
48 #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
49 
50 #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
51 #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
52 #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
53 #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
54 
55 #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
56 #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
57 
58 #define PETSC_ERR_INT_OVERFLOW     84
59 
60 #define PETSC_ERR_FLOP_COUNT       90
61 #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
62 #define PETSC_ERR_MISSING_FACTOR   92  /* MatGetFactor() failed */
63 #define PETSC_ERR_OPT_OVERWRITE    93  /* attempted to over write options which should not be changed */
64 #define PETSC_ERR_WRONG_MPI_SIZE   94  /* example/application run with number of MPI ranks it does not support */
65 #define PETSC_ERR_USER_INPUT       95  /* missing or incorrect user input */
66 #define PETSC_ERR_GPU_RESOURCE     96  /* unable to load a GPU resource, for example cuBLAS */
67 #define PETSC_ERR_GPU              97  /* An error from a GPU call, this may be due to lack of resources on the GPU or a true error in the call */
68 #define PETSC_ERR_MPI              98  /* general MPI error */
69 #define PETSC_ERR_MAX_VALUE        99  /* this is always the one more than the largest error code */
70 
71 #define SETERRQ1(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
72 #define SETERRQ2(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
73 #define SETERRQ3(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
74 #define SETERRQ4(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
75 #define SETERRQ5(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
76 #define SETERRQ6(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
77 #define SETERRQ7(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
78 #define SETERRQ8(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
79 #define SETERRQ9(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
80 
81 /*MC
82    SETERRQ - Macro to be called when an error has been detected,
83 
84    Synopsis:
85    #include <petscsys.h>
86    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
87 
88    Collective
89 
90    Input Parameters:
91 +  comm - A communicator, use PETSC_COMM_SELF unless you know all ranks of another communicator will detect the error
92 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
93 -  message - error message
94 
95   Level: beginner
96 
97    Notes:
98     Once the error handler is called the calling function is then returned from with the given error code.
99 
100     Experienced users can set the error handler with PetscPushErrorHandler().
101 
102    Fortran Notes:
103       SETERRQ() may be called from Fortran subroutines but SETERRA() must be called from the
104       Fortran main program.
105 
106 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRA(), CHKERRMPI()
107 M*/
108 #define SETERRQ(comm,ierr,...) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,__VA_ARGS__)
109 
110 /*
111     Returned from PETSc functions that are called from MPI, such as related to attributes
112       Do not confuse PETSC_MPI_ERROR_CODE and PETSC_ERR_MPI, the first is registered with MPI and returned to MPI as
113       an error code, the latter is a regular PETSc error code passed within PETSc code indicating an error was detected in an MPI call.
114 */
115 PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CLASS;
116 PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CODE;
117 
118 /*MC
119    SETERRMPI - Macro to be called when an error has been detected within an MPI callback function
120 
121    Synopsis:
122    #include <petscsys.h>
123    PetscErrorCode SETERRMPI(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
124 
125    Collective
126 
127    Input Parameters:
128 +  comm - A communicator, use PETSC_COMM_SELF unless you know all ranks of another communicator will detect the error
129 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
130 -  message - error message
131 
132   Level: developer
133 
134    Notes:
135     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
136     which is registered with MPI_Add_error_code() when PETSc is initialized.
137 
138 .seealso: SETERRQ(), CHKERRQ(), CHKERRMPI(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKMEMQ
139 M*/
140 #define SETERRMPI(comm,ierr,...) return (PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,__VA_ARGS__),PETSC_MPI_ERROR_CODE)
141 
142 /*MC
143    SETERRA - Fortran-only macro that can be called when an error has been detected from the main program
144 
145    Synopsis:
146    #include <petscsys.h>
147    PetscErrorCode SETERRA(MPI_Comm comm,PetscErrorCode ierr,char *message)
148 
149    Collective
150 
151    Input Parameters:
152 +  comm - A communicator, so that the error can be collective
153 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
154 -  message - error message in the printf format
155 
156   Level: beginner
157 
158    Notes:
159     This should only be used with Fortran. With C/C++, use SETERRQ().
160 
161    Fortran Notes:
162       SETERRQ() may be called from Fortran subroutines but SETERRA() must be called from the
163       Fortran main program.
164 
165 .seealso: SETERRQ(), SETERRABORT(), CHKERRQ(), CHKERRA(), CHKERRABORT()
166 M*/
167 
168 /*MC
169    SETERRABORT - Macro that can be called when an error has been detected,
170 
171    Synopsis:
172    #include <petscsys.h>
173    PetscErrorCode SETERRABORT(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
174 
175    Collective
176 
177    Input Parameters:
178 +  comm - A communicator, so that the error can be collective
179 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
180 -  message - error message in the printf format
181 
182   Level: beginner
183 
184    Notes:
185     This function just calls MPI_Abort().
186 
187 .seealso: SETERRQ(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ
188 M*/
189 #define SETERRABORT(comm,ierr,...) do {                                                        \
190     PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,__VA_ARGS__); \
191     MPI_Abort(comm,ierr);                                                                      \
192   } while (0)
193 
194 /*MC
195    CHKERRQ - Checks error code returned from PETSc function, if non-zero it calls the error handler and then returns. Use CHKERRMPI() for checking errors from MPI calls
196 
197    Synopsis:
198    #include <petscsys.h>
199    PetscErrorCode CHKERRQ(PetscErrorCode ierr)
200 
201    Not Collective
202 
203    Input Parameters:
204 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
205 
206   Level: beginner
207 
208    Notes:
209     Once the error handler is called the calling function is then returned from with the given error code.
210 
211     Experienced users can set the error handler with PetscPushErrorHandler().
212 
213     CHKERRQ(ierr) is fundamentally a macro replacement for
214          if (ierr) return(PetscError(...,ierr,...));
215 
216     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
217     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
218     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
219     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
220          if (ierr) {PetscError(....); return(YourReturnType);}
221     where you may pass back a NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
222     MPI_Abort() returned immediately.
223 
224    Fortran Notes:
225       CHKERRQ() may be called from Fortran subroutines but CHKERRA() must be called from the
226       Fortran main program.
227 
228 .seealso: SETERRQ(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKMEMQ, CHKERRA()
229 M*/
230 #if !defined(PETSC_CLANG_STATIC_ANALYZER)
231 #define CHKERRQ(ierr)          do {PetscErrorCode ierr__ = (ierr); if (PetscUnlikely(ierr__)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr__,PETSC_ERROR_REPEAT," ");} while (0)
232 #define CHKERRV(ierr)          do {PetscErrorCode ierr__ = (ierr); if (PetscUnlikely(ierr__)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr__,PETSC_ERROR_REPEAT," ");return;}} while (0)
233 #else
234 #define CHKERRQ(ierr)
235 #define CHKERRV(ierr)
236 #endif
237 
238 /*MC
239    CHKERRA - Fortran-only replacement for CHKERRQ in the main program, which aborts immediately
240 
241    Synopsis:
242    #include <petscsys.h>
243    PetscErrorCode CHKERRA(PetscErrorCode ierr)
244 
245    Not Collective
246 
247    Input Parameters:
248 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
249 
250   Level: beginner
251 
252    Notes:
253       This should only be used with Fortran. With C/C++, use CHKERRQ() in normal usage,
254       or CHKERRABORT() if wanting to abort immediately on error.
255 
256    Fortran Notes:
257       CHKERRQ() may be called from Fortran subroutines but CHKERRA() must be called from the
258       Fortran main program.
259 
260 .seealso: CHKERRQ(), CHKERRABORT(), SETERRA(), SETERRQ(), SETERRABORT()
261 M*/
262 
263 /*MC
264    CHKERRABORT - Checks error code returned from PETSc function. If non-zero it aborts immediately.
265 
266    Synopsis:
267    #include <petscsys.h>
268    PetscErrorCode CHKERRABORT(MPI_Comm comm,PetscErrorCode ierr)
269 
270    Not Collective
271 
272    Input Parameters:
273 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
274 
275   Level: intermediate
276 
277 .seealso: SETERRABORT(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, CHKERRMPI()
278 M*/
279 #if defined(PETSC_CLANG_STATIC_ANALYZER)
280 #define CHKERRABORT(comm,ierr)
281 #define CHKERRCONTINUE(ierr)
282 #else
283 #define CHKERRABORT(comm,ierr) do {PetscErrorCode ierr__ = (ierr); if (PetscUnlikely(ierr__)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr__,PETSC_ERROR_REPEAT," ");MPI_Abort(comm,ierr);}} while (0)
284 #define CHKERRCONTINUE(ierr)   do {PetscErrorCode ierr__ = (ierr); if (PetscUnlikely(ierr__)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr__,PETSC_ERROR_REPEAT," ");}} while (0)
285 #endif
286 
287 PETSC_EXTERN PetscErrorCode PetscAbortFindSourceFile_Private(const char*,PetscInt*);
288 PETSC_EXTERN PetscBool petscwaitonerrorflg;
289 PETSC_EXTERN PetscBool petscindebugger;
290 
291 /*MC
292    PETSCABORT - Call MPI_Abort with an informative error code
293 
294    Synopsis:
295    #include <petscsys.h>
296    PETSCABORT(MPI_Comm comm, PetscErrorCode ierr)
297 
298    Collective
299 
300    Input Parameters:
301 +  comm - A communicator, so that the error can be collective
302 -  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
303 
304    Level: advanced
305 
306    Notes:
307    We pass MPI_Abort() an error code of format XX_YYYY_ZZZ, where XX, YYYY are an index and line number of the file
308    where PETSCABORT is called, respectively. ZZZ is the PETSc error code.
309 
310    If XX is zero, this means that the call was made in the routine main().
311    If XX is one, that means 1) the file is not in PETSc (it may be in users code); OR 2) the file is in PETSc but PetscAbortSourceFiles[]
312      is out of date. PETSc developers have to update it.
313    Otherwise, look up the value of XX in the table PetscAbortSourceFiles[] in src/sys/error/err.c to map XX back to the source file where the PETSCABORT() was called.
314 
315    If the option -start_in_debugger was used then this calls abort() to stop the program in the debugger.
316 
317 M*/
318 #define PETSCABORT(comm,ierr)  \
319    do {                                                               \
320       PetscInt       idx = 0;                                         \
321       PetscMPIInt    errcode;                                         \
322       PetscAbortFindSourceFile_Private(__FILE__,&idx);                \
323       errcode = (PetscMPIInt)(0*idx*10000000 + 0*__LINE__*1000 + ierr);   \
324       if (petscwaitonerrorflg) PetscSleep(1000);                      \
325       if (petscindebugger) abort();                                   \
326       else MPI_Abort(comm,errcode);                                   \
327    } while (0)
328 
329 /*MC
330    CHKERRMPI - Checks error code returned from MPI calls, if non-zero it calls the error handler and then returns
331 
332    Synopsis:
333    #include <petscsys.h>
334    PetscErrorCode CHKERRMPI(PetscErrorCode ierr)
335 
336    Not Collective
337 
338    Input Parameters:
339 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
340 
341   Level: intermediate
342 
343    Notes:
344     Always returns the error code PETSC_ERR_MPI; the MPI error code and string are embedded in the string error message
345 
346 .seealso: SETERRMPI(), CHKERRQ(), SETERRQ(), SETERRABORT(), CHKERRABORT(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKMEMQ
347 M*/
348 #if defined(PETSC_CLANG_STATIC_ANALYZER)
349 #define CHKERRMPI(ierr)
350 #else
351 #define CHKERRMPI(ierr) \
352 do { \
353   PetscErrorCode _7_errorcode = (ierr); \
354   if (PetscUnlikely(_7_errorcode)) { \
355     char _7_errorstring[MPI_MAX_ERROR_STRING]; \
356     PetscMPIInt _7_resultlen; \
357     MPI_Error_string(_7_errorcode,(char*)_7_errorstring,&_7_resultlen); (void)_7_resultlen; \
358     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MPI,"MPI error %d %s",(int)_7_errorcode,_7_errorstring); \
359   } \
360 } while (0)
361 #endif
362 
363 #ifdef PETSC_CLANGUAGE_CXX
364 
365 /*MC
366    CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
367 
368    Synopsis:
369    #include <petscsys.h>
370    void CHKERRXX(PetscErrorCode ierr)
371 
372    Not Collective
373 
374    Input Parameters:
375 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
376 
377   Level: beginner
378 
379    Notes:
380     Once the error handler throws a ??? exception.
381 
382     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
383     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.
384 
385 .seealso: SETERRQ(), CHKERRQ(), SETERRABORT(), CHKERRABORT(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKMEMQ
386 M*/
387 #define CHKERRXX(ierr)  do {if (PetscUnlikely(ierr)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_IN_CXX,0);}} while (0)
388 #endif
389 
390 /*MC
391    CHKERRCXX - Checks C++ function calls and if they throw an exception, catch it and then return a PETSc error code
392 
393    Synopsis:
394    #include <petscsys.h>
395    CHKERRCXX(func);
396 
397    Not Collective
398 
399    Input Parameters:
400 .  func - C++ function calls
401 
402   Level: beginner
403 
404   Notes:
405    For example,
406 
407 $     void foo(int x) {throw std::runtime_error("error");}
408 $     CHKERRCXX(foo(1));
409 
410 .seealso: CHKERRXX(), SETERRQ(), CHKERRQ(), SETERRABORT(), CHKERRABORT(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKMEMQ
411 M*/
412 #define CHKERRCXX(func) do {try {func;} catch (const std::exception& e) { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"%s", e.what()); }} while (0)
413 
414 /*MC
415    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
416 
417    Synopsis:
418    #include <petscsys.h>
419    CHKMEMQ;
420 
421    Not Collective
422 
423   Level: beginner
424 
425    Notes:
426     We highly recommend using Valgrind https://petsc.org/release/faq/#valgrind or for NVIDIA CUDA systems
427     https://docs.nvidia.com/cuda/cuda-memcheck/index.html for finding memory problems. The ``CHKMEMQ`` macro is useful on systems that
428     do not have valgrind, but is not as good as valgrind or cuda-memcheck.
429 
430     Must run with the option -malloc_debug (-malloc_test in debug mode; or if PetscMallocSetDebug() called) to enable this option
431 
432     Once the error handler is called the calling function is then returned from with the given error code.
433 
434     By defaults prints location where memory that is corrupted was allocated.
435 
436     Use CHKMEMA for functions that return void
437 
438 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), PetscMallocValidate()
439 M*/
440 #if defined(PETSC_CLANG_STATIC_ANALYZER)
441 #define CHKMEMQ
442 #define CHKMEMA
443 #else
444 #define CHKMEMQ do {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__);CHKERRQ(_7_ierr);} while (0)
445 #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__)
446 #endif
447 /*E
448   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
449 
450   Level: advanced
451 
452   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
453 
454   Developer Notes:
455     This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandler()
456 
457 .seealso: PetscError(), SETERRXX()
458 E*/
459 typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
460 
461 #if defined(__clang_analyzer__)
462 __attribute__((analyzer_noreturn))
463 #endif
464 PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...) PETSC_ATTRIBUTE_FORMAT(7,8);
465 
466 PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
467 PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
468 PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
469 PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
470 PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
471 PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
472 PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
473 PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
474 PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
475 PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
476 PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
477 PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int,void*);
478 PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
479 PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
480 PETSC_EXTERN PetscErrorCode PetscCheckPointerSetIntensity(PetscInt);
481 PETSC_EXTERN void PetscSignalSegvCheckPointerOrMpi(void);
482 PETSC_DEPRECATED_FUNCTION("Use PetscSignalSegvCheckPointerOrMpi() (since version 3.13)") PETSC_STATIC_INLINE void PetscSignalSegvCheckPointer(void) {PetscSignalSegvCheckPointerOrMpi();}
483 
484 /*MC
485     PetscErrorPrintf - Prints error messages.
486 
487    Synopsis:
488     #include <petscsys.h>
489      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
490 
491     Not Collective
492 
493     Input Parameter:
494 .   format - the usual printf() format string
495 
496    Options Database Keys:
497 +    -error_output_stdout - cause error messages to be printed to stdout instead of the  (default) stderr
498 -    -error_output_none - to turn off all printing of error messages (does not change the way the error is handled.)
499 
500    Notes:
501     Use
502 $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
503 $                        error is handled.) and
504 $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on or you can use your own function
505 
506           Use
507      PETSC_STDERR = FILE* obtained from a file open etc. to have stderr printed to the file.
508      PETSC_STDOUT = FILE* obtained from a file open etc. to have stdout printed to the file.
509 
510           Use
511       PetscPushErrorHandler() to provide your own error handler that determines what kind of messages to print
512 
513    Level: developer
514 
515     Fortran Note:
516     This routine is not supported in Fortran.
517 
518 .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscHelpPrintf(), PetscPrintf(), PetscPushErrorHandler(), PetscVFPrintf(), PetscHelpPrintf()
519 M*/
520 PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[],...) PETSC_ATTRIBUTE_FORMAT(1,2);
521 
522 typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
523 PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
524 PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
525 PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
526 PETSC_EXTERN PetscErrorCode PetscDetermineInitialFPTrap(void);
527 
528 /*
529       Allows the code to build a stack frame as it runs
530 */
531 
532 #if defined(PETSC_USE_DEBUG)
533 #define PETSCSTACKSIZE 64
534 typedef struct  {
535   const char *function[PETSCSTACKSIZE];
536   const char *file[PETSCSTACKSIZE];
537         int  line[PETSCSTACKSIZE];
538         int  petscroutine[PETSCSTACKSIZE]; /* 0 external called from petsc, 1 petsc functions, 2 petsc user functions */
539         int  currentsize;
540         int  hotdepth;
541   PetscBool  check; /* runtime option to check for correct Push/Pop semantics at runtime */
542 } PetscStack;
543 PETSC_EXTERN PetscStack petscstack;
544 #else
545 typedef struct {
546   char Silence_empty_struct_has_size_0_in_C_size_1_in_Cpp;
547 } PetscStack;
548 #endif
549 
550 #if defined(PETSC_SERIALIZE_FUNCTIONS)
551 #include <petsc/private/petscfptimpl.h>
552 /*
553    Registers the current function into the global function pointer to function name table
554 
555    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
556 */
557 #define PetscRegister__FUNCT__() do { \
558   static PetscBool __chked = PETSC_FALSE; \
559   if (!__chked) {\
560   void *ptr; PetscDLSym(NULL,PETSC_FUNCTION_NAME,&ptr);\
561   __chked = PETSC_TRUE;\
562   }} while (0)
563 #else
564 #define PetscRegister__FUNCT__()
565 #endif
566 
567 #if defined(PETSC_CLANG_STATIC_ANALYZER)
568 #define PetscStackPushNoCheck(funct,petsc_routine,hot)
569 #define PetscStackPopNoCheck
570 #define PetscStackClearTop
571 #define PetscFunctionBegin
572 #define PetscFunctionBeginUser
573 #define PetscFunctionBeginHot
574 #define PetscFunctionReturn(a)    return a
575 #define PetscFunctionReturnVoid() return
576 #define PetscStackPop
577 #define PetscStackPush(f)
578 #elif defined(PETSC_USE_DEBUG)
579 /* Stack handling is based on the following two "NoCheck" macros.  These should only be called directly by other error
580  * handling macros.  We record the line of the call, which may or may not be the location of the definition.  But is at
581  * least more useful than "unknown" because it can distinguish multiple calls from the same function.
582  */
583 #define PetscStackPushNoCheck(funct,petsc_routine,hot) do {             \
584     PetscStackSAWsTakeAccess();                                         \
585     if (petscstack.currentsize < PETSCSTACKSIZE) {                      \
586       petscstack.function[petscstack.currentsize]     = funct;          \
587       petscstack.file[petscstack.currentsize]         = __FILE__;       \
588       petscstack.line[petscstack.currentsize]         = __LINE__;       \
589       petscstack.petscroutine[petscstack.currentsize] = petsc_routine;  \
590     }                                                                   \
591     ++petscstack.currentsize;                                           \
592     petscstack.hotdepth += (hot || petscstack.hotdepth);                \
593     PetscStackSAWsGrantAccess();                                        \
594   } while (0)
595 
596 #define PetscStackPopNoCheck(funct)                    do {             \
597     PetscStackSAWsTakeAccess();                                         \
598     if (PetscUnlikely(petscstack.currentsize <= 0)) {                   \
599       if (PetscUnlikely(petscstack.check)) {                            \
600         printf("Invalid stack size %d, pop %s\n",                       \
601                petscstack.currentsize,funct);                           \
602       }                                                                 \
603     } else {                                                            \
604       if (--petscstack.currentsize < PETSCSTACKSIZE) {                  \
605         if (PetscUnlikely(                                              \
606               petscstack.check                                &&        \
607               petscstack.petscroutine[petscstack.currentsize] &&        \
608               (petscstack.function[petscstack.currentsize]    !=        \
609                (const char*)funct))) {                                  \
610           /* We need this string comparison because "unknown" can be defined in different static strings: */ \
611           PetscBool _cmpflg;                                            \
612           const char *_funct = petscstack.function[petscstack.currentsize]; \
613           PetscStrcmp(_funct,funct,&_cmpflg);                           \
614           if (!_cmpflg)                                                 \
615             printf("Invalid stack: push from %s, pop from %s\n", _funct,funct); \
616         }                                                               \
617         petscstack.function[petscstack.currentsize] = PETSC_NULLPTR;    \
618         petscstack.file[petscstack.currentsize]     = PETSC_NULLPTR;    \
619         petscstack.line[petscstack.currentsize]     = 0;                \
620         petscstack.petscroutine[petscstack.currentsize] = 0;            \
621       }                                                                 \
622       petscstack.hotdepth = PetscMax(petscstack.hotdepth-1,0);          \
623     }                                                                   \
624     PetscStackSAWsGrantAccess();                                        \
625   } while (0)
626 
627 #define PetscStackClearTop                             do {             \
628     PetscStackSAWsTakeAccess();                                         \
629     if (petscstack.currentsize > 0 &&                                   \
630         --petscstack.currentsize < PETSCSTACKSIZE) {                    \
631       petscstack.function[petscstack.currentsize]     = PETSC_NULLPTR;  \
632       petscstack.file[petscstack.currentsize]         = PETSC_NULLPTR;  \
633       petscstack.line[petscstack.currentsize]         = 0;              \
634       petscstack.petscroutine[petscstack.currentsize] = 0;              \
635     }                                                                   \
636     petscstack.hotdepth = PetscMax(petscstack.hotdepth-1,0);            \
637     PetscStackSAWsGrantAccess();                                        \
638   } while (0)
639 
640 /*MC
641    PetscFunctionBegin - First executable line of each PETSc function,  used for error handling. Final
642       line of PETSc functions should be PetscFunctionReturn(0);
643 
644    Synopsis:
645    #include <petscsys.h>
646    void PetscFunctionBegin;
647 
648    Not Collective
649 
650    Usage:
651 .vb
652      int something;
653 
654      PetscFunctionBegin;
655 .ve
656 
657    Notes:
658      Use PetscFunctionBeginUser for application codes.
659 
660      Not available in Fortran
661 
662    Level: developer
663 
664 .seealso: PetscFunctionReturn(), PetscFunctionBeginHot(), PetscFunctionBeginUser()
665 
666 M*/
667 #define PetscFunctionBegin do {                               \
668     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,1,PETSC_FALSE); \
669     PetscRegister__FUNCT__();                                 \
670   } while (0)
671 
672 /*MC
673    PetscFunctionBeginHot - Substitute for PetscFunctionBegin to be used in functions that are called in
674    performance-critical circumstances.  Use of this function allows for lighter profiling by default.
675 
676    Synopsis:
677    #include <petscsys.h>
678    void PetscFunctionBeginHot;
679 
680    Not Collective
681 
682    Usage:
683 .vb
684      int something;
685 
686      PetscFunctionBeginHot;
687 .ve
688 
689    Notes:
690      Not available in Fortran
691 
692    Level: developer
693 
694 .seealso: PetscFunctionBegin, PetscFunctionReturn()
695 
696 M*/
697 #define PetscFunctionBeginHot do {                           \
698     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,1,PETSC_TRUE); \
699     PetscRegister__FUNCT__();                                \
700   } while (0)
701 
702 /*MC
703    PetscFunctionBeginUser - First executable line of user provided PETSc routine
704 
705    Synopsis:
706    #include <petscsys.h>
707    void PetscFunctionBeginUser;
708 
709    Not Collective
710 
711    Usage:
712 .vb
713      int something;
714 
715      PetscFunctionBeginUser;
716 .ve
717 
718    Notes:
719       Final line of PETSc functions should be PetscFunctionReturn(0) except for main().
720 
721       Not available in Fortran
722 
723       This is identical to PetscFunctionBegin except it labels the routine as a user
724       routine instead of as a PETSc library routine.
725 
726    Level: intermediate
727 
728 .seealso: PetscFunctionReturn(), PetscFunctionBegin, PetscFunctionBeginHot
729 
730 M*/
731 #define PetscFunctionBeginUser do {                           \
732     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,2,PETSC_FALSE); \
733     PetscRegister__FUNCT__();                                 \
734   } while (0)
735 
736 #define PetscStackPush(n)       do {        \
737     PetscStackPushNoCheck(n,0,PETSC_FALSE); \
738     CHKMEMQ;                                \
739   } while (0)
740 
741 #define PetscStackPop           do {             \
742       CHKMEMQ;                                   \
743       PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
744     } while (0)
745 
746 /*MC
747    PetscFunctionReturn - Last executable line of each PETSc function
748         used for error handling. Replaces return()
749 
750    Synopsis:
751    #include <petscsys.h>
752    void PetscFunctionReturn(0);
753 
754    Not Collective
755 
756    Usage:
757 .vb
758     ....
759      PetscFunctionReturn(0);
760    }
761 .ve
762 
763    Notes:
764      Not available in Fortran
765 
766    Level: developer
767 
768 .seealso: PetscFunctionBegin()
769 
770 M*/
771 #define PetscFunctionReturn(a)    do {          \
772     PetscStackPopNoCheck(PETSC_FUNCTION_NAME);  \
773     return a;                                   \
774   } while (0)
775 
776 #define PetscFunctionReturnVoid() do {          \
777     PetscStackPopNoCheck(PETSC_FUNCTION_NAME);  \
778     return;                                     \
779   } while (0)
780 #else /* PETSC_USE_DEBUG */
781 #define PetscStackPushNoCheck(funct,petsc_routine,hot)
782 #define PetscStackPopNoCheck
783 #define PetscStackClearTop
784 #define PetscFunctionBegin
785 #define PetscFunctionBeginUser
786 #define PetscFunctionBeginHot
787 #define PetscFunctionReturn(a)    return a
788 #define PetscFunctionReturnVoid() return
789 #define PetscStackPop             CHKMEMQ
790 #define PetscStackPush(f)         CHKMEMQ
791 #endif /* PETSC_USE_DEBUG */
792 
793 #if defined(PETSC_CLANG_STATIC_ANALYZER)
794 #define PetscStackCall(name,routine)
795 #define PetscStackCallStandard(name,routine)
796 #else
797 /*
798     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
799 
800    Input Parameters:
801 +   name - string that gives the name of the function being called
802 -   routine - actual call to the routine, including ierr = and CHKERRQ(ierr);
803 
804    Note: Often one should use PetscStackCallStandard() instead. This routine is intended for external library routines that DO NOT return error codes
805 
806    Developer Note: this is so that when a user or external library routine results in a crash or corrupts memory, they get blamed instead of PETSc.
807 
808 */
809 #define PetscStackCall(name,routine) do { PetscStackPush(name);routine;PetscStackPop; } while (0)
810 
811 /*
812     PetscStackCallStandard - Calls an external library routine after pushing the name of the routine on the stack.
813 
814    Input Parameters:
815 +   func-  name of the routine
816 -   args - arguments to the routine surrounded by ()
817 
818    Notes:
819     This is intended for external package routines that return error codes. Use PetscStackCall() for those that do not.
820 
821    Developer Note: this is so that when an external packge routine results in a crash or corrupts memory, they get blamed instead of PETSc.
822 
823 */
824 #define PetscStackCallStandard(func,args) do {                                                            \
825     PetscErrorCode __ierr;                                                                                \
826     PetscStackPush(#func);                                                                                \
827     __ierr = func args;                                                                                   \
828     PetscStackPop;                                                                                        \
829     if (__ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s(): error code %d",#func,(int)__ierr); \
830   } while (0)
831 #endif /* PETSC_CLANG_STATIC_ANALYZER */
832 
833 #endif
834