xref: /petsc/include/petscerror.h (revision dfbbaf821b4c49d07b4ce746493b0d955783fdf9)
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 /* SUBMANSEC = Sys */
11 
12 /*
13      These are the generic error codes. These error codes are used
14      many different places in the PETSc source code. The string versions are
15      at src/sys/error/err.c any changes here must also be made there
16      These are also define in src/sys/f90-mod/petscerror.h any CHANGES here
17      must be also made there.
18 
19 */
20 #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
21 
22 #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
23 #define PETSC_ERR_SUP              56   /* no support for requested operation */
24 #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
25 #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
26 #define PETSC_ERR_SIG              59   /* signal received */
27 #define PETSC_ERR_FP               72   /* floating point exception */
28 #define PETSC_ERR_COR              74   /* corrupted PETSc object */
29 #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
30 #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
31 #define PETSC_ERR_MEMC             78   /* memory corruption */
32 #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
33 #define PETSC_ERR_USER             83   /* user has not provided needed function */
34 #define PETSC_ERR_SYS              88   /* error in system call */
35 #define PETSC_ERR_POINTER          70   /* pointer does not point to valid address */
36 #define PETSC_ERR_MPI_LIB_INCOMP   87   /* MPI library at runtime is not compatible with MPI user compiled with */
37 
38 #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
39 #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
40 #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
41 #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
42 #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
43 #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
44 #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
45 #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
46 #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
47 #define PETSC_ERR_ARG_TYPENOTSET   89   /* the type of the object has not yet been set */
48 #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
49 #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
50 #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
51 
52 #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
53 #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
54 #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
55 #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
56 
57 #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
58 #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
59 
60 #define PETSC_ERR_INT_OVERFLOW     84
61 
62 #define PETSC_ERR_FLOP_COUNT       90
63 #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
64 #define PETSC_ERR_MISSING_FACTOR   92  /* MatGetFactor() failed */
65 #define PETSC_ERR_OPT_OVERWRITE    93  /* attempted to over write options which should not be changed */
66 #define PETSC_ERR_WRONG_MPI_SIZE   94  /* example/application run with number of MPI ranks it does not support */
67 #define PETSC_ERR_USER_INPUT       95  /* missing or incorrect user input */
68 #define PETSC_ERR_GPU_RESOURCE     96  /* unable to load a GPU resource, for example cuBLAS */
69 #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 */
70 #define PETSC_ERR_MPI              98  /* general MPI error */
71 #define PETSC_ERR_MAX_VALUE        99  /* this is always the one more than the largest error code */
72 
73 #define SETERRQ1(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
74 #define SETERRQ2(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
75 #define SETERRQ3(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
76 #define SETERRQ4(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
77 #define SETERRQ5(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
78 #define SETERRQ6(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
79 #define SETERRQ7(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
80 #define SETERRQ8(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
81 #define SETERRQ9(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
82 
83 /*MC
84    SETERRQ - Macro to be called when an error has been detected,
85 
86    Synopsis:
87    #include <petscsys.h>
88    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
89 
90    Collective
91 
92    Input Parameters:
93 +  comm - A communicator, use `PETSC_COMM_SELF` unless you know all ranks of another communicator will detect the error
94 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
95 -  message - error message
96 
97   Level: beginner
98 
99    Notes:
100     This is rarely needed, one should use `PetscCheck()` and `PetscCall()` and friends to automatically handle error conditions.
101     Once the error handler is called the calling function is then returned from with the given error code.
102 
103     Experienced users can set the error handler with `PetscPushErrorHandler()`.
104 
105    Fortran Notes:
106       SETERRQ() may be called from Fortran subroutines but SETERRA() must be called from the
107       Fortran main program.
108 
109 .seealso: `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`,
110           `PetscError()`, `PetscCall()`, `CHKMEMQ`, `CHKERRA()`, `PetscCallMPI()`
111 M*/
112 #define SETERRQ(comm,ierr,...) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,__VA_ARGS__)
113 
114 /*
115     Returned from PETSc functions that are called from MPI, such as related to attributes
116       Do not confuse PETSC_MPI_ERROR_CODE and PETSC_ERR_MPI, the first is registered with MPI and returned to MPI as
117       an error code, the latter is a regular PETSc error code passed within PETSc code indicating an error was detected in an MPI call.
118 */
119 PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CLASS;
120 PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CODE;
121 
122 /*MC
123    SETERRMPI - Macro to be called when an error has been detected within an MPI callback function
124 
125    Synopsis:
126    #include <petscsys.h>
127    PetscErrorCode SETERRMPI(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
128 
129    Collective
130 
131    Input Parameters:
132 +  comm - A communicator, use `PETSC_COMM_SELF` unless you know all ranks of another communicator will detect the error
133 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
134 -  message - error message
135 
136   Level: developer
137 
138    Notes:
139     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`
140     which is registered with `MPI_Add_error_code()` when PETSc is initialized.
141 
142 .seealso: `SETERRQ()`, `PetscCall()`, `PetscCallMPI()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
143 M*/
144 #define SETERRMPI(comm,ierr,...) return (PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,__VA_ARGS__),PETSC_MPI_ERROR_CODE)
145 
146 /*MC
147    SETERRA - Fortran-only macro that can be called when an error has been detected from the main program
148 
149    Synopsis:
150    #include <petscsys.h>
151    PetscErrorCode SETERRA(MPI_Comm comm,PetscErrorCode ierr,char *message)
152 
153    Collective
154 
155    Input Parameters:
156 +  comm - A communicator, so that the error can be collective
157 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
158 -  message - error message in the printf format
159 
160   Level: beginner
161 
162    Notes:
163     This should only be used with Fortran. With C/C++, use `SETERRQ()`.
164 
165    Fortran Notes:
166       `SETERRQ()` may be called from Fortran subroutines but `SETERRA()` must be called from the
167       Fortran main program.
168 
169 .seealso: `SETERRQ()`, `SETERRABORT()`, `PetscCall()`, `CHKERRA()`, `PetscCallAbort()`
170 M*/
171 
172 /*MC
173    SETERRABORT - Macro that can be called when an error has been detected,
174 
175    Synopsis:
176    #include <petscsys.h>
177    PetscErrorCode SETERRABORT(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
178 
179    Collective
180 
181    Input Parameters:
182 +  comm - A communicator, so that the error can be collective
183 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
184 -  message - error message in the printf format
185 
186   Level: beginner
187 
188    Notes:
189     This function just calls `MPI_Abort()`.
190 
191     This should only be called in routines that cannot return an error code, such as in C++ constructors.
192 
193 .seealso: `SETERRQ()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `PetscCall()`, `CHKMEMQ`
194 M*/
195 #define SETERRABORT(comm,ierr,...) do {                                                        \
196     PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,__VA_ARGS__); \
197     MPI_Abort(comm,ierr);                                                                      \
198   } while (0)
199 
200 /*MC
201   PetscCheck - Check that a particular condition is true
202 
203   Synopsis:
204   #include <petscerror.h>
205   void PetscCheck(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
206 
207   Collective
208 
209   Input Parameters:
210 + cond    - The boolean condition
211 . comm    - The communicator on which the check can be collective on
212 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
213 - message - Error message in printf format
214 
215   Notes:
216   Enabled in both optimized and debug builds.
217 
218   Calls `SETERRQ()` if the assertion fails, so can only be called from functions returning a
219   `PetscErrorCode` (or equivalent type after conversion).
220 
221   Level: beginner
222 
223 .seealso: `PetscAssert()`, `SETERRQ()`, `PetscError()`, `PetscCall()`, `PetscCheckAbort()`
224 M*/
225 #define PetscCheck(cond,comm,ierr,...) do { if (PetscUnlikely(!(cond))) SETERRQ(comm,ierr,__VA_ARGS__); } while (0)
226 
227 /*MC
228   PetscCheckAbort - Check that a particular condition is true, otherwise prints error and aborts
229 
230   Synopsis:
231   #include <petscerror.h>
232   void PetscCheckAbort(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
233 
234   Collective
235 
236   Input Parameters:
237 + cond    - The boolean condition
238 . comm    - The communicator on which the check can be collective on
239 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
240 - message - Error message in printf format
241 
242   Notes:
243   Enabled in both optimized and debug builds.
244 
245   Calls `SETERRABORT()` if the assertion fails, can be called from a function that does not return an
246   error code, such as a C++ constructor. usually `PetscCheck()` should be used.
247 
248   Level: developer
249 
250 .seealso: `PetscAssert()`, `SETERRQ()`, `PetscError()`, `PetscCall()`, `PetscCheck()`, `SETTERRABORT()`
251 M*/
252 #define PetscCheckAbort(cond,comm,ierr,...) if (PetscUnlikely(!(cond))) SETERRABORT(comm,ierr,__VA_ARGS__)
253 
254 /*MC
255   PetscAssert - Assert that a particular condition is true
256 
257   Synopsis:
258   #include <petscerror.h>
259   void PetscAssert(bool cond, MPI_Comm comm, PetscErrorCode ierr, const char *message, ...)
260 
261   Collective
262 
263   Input Parameters:
264 + cond    - The boolean condition
265 . comm    - The communicator on which the check can be collective on
266 . ierr    - A nonzero error code, see include/petscerror.h for the complete list
267 - message - Error message in printf format
268 
269   Notes:
270   Enabled only in debug builds. Note that any arguments to this macros are still visible to the
271   compiler optimized builds (so must still contain valid code) but are guaranteed to not be
272   executed.
273 
274   See `PetscCheck()` for usage and behaviour.
275 
276   This is needed instead of simply using `assert()` because this correctly handles the collective nature of errors under MPI
277 
278   Level: beginner
279 
280 .seealso: `PetscCheck()`, `SETERRQ()`, `PetscError()`
281 M*/
282 #define PetscAssert(cond,comm,ierr,...) do { if (PetscUnlikelyDebug(!(cond))) SETERRQ(comm,ierr,__VA_ARGS__); } while (0)
283 
284 /*MC
285   PetscCall - Calls a PETSc function and then checks the resulting error code, if it is non-zero it calls the error
286   handler and returns from the current function with the error code.
287 
288   Synopsis:
289   #include <petscerror.h>
290   void PetscCall(PetscFunction(args))
291 
292   Not Collective
293 
294   Input Parameter:
295 . PetscFunction - any PETSc function that returns an error code
296 
297   Notes:
298   Once the error handler is called the calling function is then returned from with the given
299   error code. Experienced users can set the error handler with `PetscPushErrorHandler()`.
300 
301   `PetscCall()` cannot be used in functions returning a datatype not convertible to
302   `PetscErrorCode`. For example, `PetscCall()` may not be used in functions returning void, use
303   `PetscCallVoid()` in this case.
304 
305   Example Usage:
306 .vb
307   PetscCall(PetscInitiailize(...)); // OK to call even when PETSc is not yet initialized!
308 
309   struct my_struct
310   {
311     void *data;
312   } my_complex_type;
313 
314   struct my_struct bar(void)
315   {
316     PetscCall(foo(15)); // ERROR PetscErrorCode not convertible to struct my_struct!
317   }
318 
319   PetscCall(bar()) // ERROR input not convertible to PetscErrorCode
320 .ve
321 
322   It is also possible to call this directly on a `PetscErrorCode` variable
323 .vb
324   PetscCall(ierr);  // check if ierr is nonzero
325 .ve
326 
327   Should not be used to call callback functions provided by users, `PetscCallBack()` should be used in that situation.
328 
329   `PetscUseTypeMethod()` or `PetscTryTypeMethod()` should be used when calling functions pointers contained in a PETSc object's `ops` array
330 
331   Fortran Notes:
332     The Fortran function from which this is used must declare a variable PetscErrorCode ierr and ierr must be
333     the final argument to the PETSc function being called.
334 
335     In the main program and in Fortran subroutines that do not have ierr as the final return parameter one
336     should use `PetscCallA()`
337 
338   Example Fortran Usage:
339 .vb
340   PetscErrorCode ierr
341   Vec v
342 
343   ...
344   PetscCall(VecShift(v,1.0,ierr))
345   PetscCallA(VecShift(v,1.0,ierr))
346 .ve
347 
348   Level: beginner
349 
350 .seealso: `SETERRQ()`, `PetscCheck()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`
351           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`, `CHKERRMPI()`, `PetscCallBack()`
352 M*/
353 
354 /*MC
355   PetscCallBack - Calls a user provided PETSc callback function and then checks the resulting error code, if it is non-zero it calls the error
356   handler and returns from the current function with the error code.
357 
358   Synopsis:
359   #include <petscerror.h>
360   void PetscCallBack(const char *functionname,PetscFunction(args))
361 
362   Not Collective
363 
364   Input Parameters:
365 + functionname - the name of the function being called, this can be a string with spaces that describes the meaning of the callback
366 - PetscFunction - user provided callback function that returns an error code
367 
368   Notes:
369   Once the error handler is called the calling function is then returned from with the given
370   error code. Experienced users can set the error handler with `PetscPushErrorHandler()`.
371 
372   `PetscCallBack()` should only be called in PETSc when a call is being made to a user provided call-back routine.
373 
374   Example Usage:
375 .vb
376   PetscCallBack("XXX callback to do something",a->callback(...));
377 .ve
378 
379   Level: developer
380 
381 .seealso: `SETERRQ()`, `PetscCheck()`, `PetscCall()`, `PetscAssert()`, `PetscTraceBackErrorHandler()`, `PetscCallMPI()`
382           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`, `CHKERRA()`, `CHKERRMPI()`, `PetscCall()`
383 M*/
384 
385 #if defined(PETSC_CLANG_STATIC_ANALYZER)
386 void PetscCall(PetscErrorCode);
387 void PetscCallBack(const char *,PetscErrorCode);
388 void PetscCallVoid(PetscErrorCode);
389 #else
390 #define PetscCall(...) do {                                             \
391     PetscErrorCode ierr_q_;                                             \
392     PetscStackUpdateLine;                                               \
393     ierr_q_ = __VA_ARGS__;                                              \
394     if (PetscUnlikely(ierr_q_)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr_q_,PETSC_ERROR_REPEAT," "); \
395   } while (0)
396 #define PetscCallBack(function,...) do {                                \
397     PetscErrorCode ierr_q_;                                             \
398     PetscStackUpdateLine;                                               \
399     PetscStackPushExternal(function);                                   \
400     ierr_q_ = __VA_ARGS__;                                              \
401     PetscStackPop;                                                      \
402     if (PetscUnlikely(ierr_q_)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr_q_,PETSC_ERROR_REPEAT," "); \
403   } while (0)
404 #define PetscCallVoid(...) do {                                         \
405     PetscErrorCode ierr_void_;                                          \
406     PetscStackUpdateLine;                                               \
407     ierr_void_ = __VA_ARGS__;                                           \
408     if (PetscUnlikely(ierr_void_)) {(void)PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr_void_,PETSC_ERROR_REPEAT," "); return;} \
409   } while (0)
410 #endif
411 
412 /*MC
413   CHKERRQ - Checks error code returned from PETSc function
414 
415   Synopsis:
416   #include <petscsys.h>
417   void CHKERRQ(PetscErrorCode ierr)
418 
419   Not Collective
420 
421   Input Parameters:
422 . ierr - nonzero error code
423 
424   Notes:
425   Deprecated in favor of `PetscCall()`. This routine behaves identically to it.
426 
427   Level: deprecated
428 
429 .seealso: `PetscCall()`
430 M*/
431 #define CHKERRQ(...) PetscCall(__VA_ARGS__)
432 #define CHKERRV(...) PetscCallVoid(__VA_ARGS__)
433 
434 PETSC_EXTERN void PetscMPIErrorString(PetscMPIInt, char*);
435 
436 /*MC
437   PetscCallMPI - Checks error code returned from MPI calls, if non-zero it calls the error
438   handler and then returns
439 
440   Synopsis:
441   #include <petscerror.h>
442   void PetscCallMPI(MPI_Function(args))
443 
444   Not Collective
445 
446   Input Parameters:
447 . MPI_Function - an MPI function that returns an MPI error code
448 
449   Notes:
450   Always returns the error code `PETSC_ERR_MPI`; the MPI error code and string are embedded in
451   the string error message. Do not use this to call any other routines (for example PETSc
452   routines), it should only be used for direct MPI calls. Due to limitations of the
453   preprocessor this can unfortunately not easily be enforced, so the user should take care to
454   check this themselves.
455 
456   Example Usage:
457 .vb
458   PetscCallMPI(MPI_Comm_size(...)); // OK, calling MPI function
459 
460   PetscCallMPI(PetscFunction(...)); // ERROR, use PetscCall() instead!
461 .ve
462 
463   Fortran Notes:
464     The Fortran function from which this is used must declare a variable `PetscErrorCode` ierr and ierr must be
465     the final argument to the MPI function being called.
466 
467     In the main program and in Fortran subroutines that do not have ierr as the final return parameter one
468     should use `PetscCallMPIA()`
469 
470   Fortran Usage:
471 .vb
472   PetscErrorCode ierr or integer ierr
473   ...
474   PetscCallMPI(MPI_Comm_size(...,ierr))
475   PetscCallMPIA(MPI_Comm_size(...,ierr)) ! Will abort after calling error handler
476 
477   PetscCallMPI(MPI_Comm_size(...,eflag)) ! ERROR, final argument must be ierr
478 .ve
479 
480   Level: beginner
481 
482 .seealso: `SETERRMPI()`, `PetscCall()`, `SETERRQ()`, `SETERRABORT()`, `PetscCallAbort()`,
483           `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
484 M*/
485 #if defined(PETSC_CLANG_STATIC_ANALYZER)
486 void PetscCallMPI(PetscMPIInt);
487 #else
488 #define PetscCallMPI(...) do {                                          \
489     PetscMPIInt _7_errorcode;                                           \
490     char _7_errorstring[2*MPI_MAX_ERROR_STRING];                        \
491     PetscStackUpdateLine;                                               \
492     PetscStackPushExternal("MPI function");                             \
493     {_7_errorcode = __VA_ARGS__;}                                       \
494     PetscStackPop;                                                      \
495     if (PetscUnlikely(_7_errorcode)) {                                  \
496       PetscMPIErrorString(_7_errorcode,(char*)_7_errorstring);          \
497       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MPI,"MPI error %d %s",(int)_7_errorcode,_7_errorstring); \
498     }                                                                   \
499   } while (0)
500 #endif
501 
502 /*MC
503   CHKERRMPI - Checks error code returned from MPI calls, if non-zero it calls the error
504   handler and then returns
505 
506   Synopsis:
507   #include <petscerror.h>
508   void CHKERRMPI(PetscErrorCode ierr)
509 
510   Not Collective
511 
512   Input Parameter:
513 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
514 
515   Notes:
516   Deprecated in favor of `PetscCallMPI()`. This routine behaves identically to it.
517 
518   Level: deprecated
519 
520 .seealso: `PetscCallMPI()`
521 M*/
522 #define CHKERRMPI(...) PetscCallMPI(__VA_ARGS__)
523 
524 /*MC
525   PetscCallAbort - Checks error code returned from PETSc function, if non-zero it aborts immediately
526 
527   Synopsis:
528   #include <petscerror.h>
529   void PetscCallAbort(MPI_Comm comm, PetscErrorCode ierr)
530 
531   Collective on comm
532 
533   Input Parameters:
534 + comm - the MPI communicator on which to abort
535 - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
536 
537   Notes:
538   This macro has identical type and usage semantics to `PetscCall()` with the important caveat
539   that this macro does not return. Instead, if ierr is nonzero it calls the PETSc error handler
540   and then immediately calls `MPI_Abort()`. It can therefore be used anywhere.
541 
542   As per `MPI_Abort()` semantics the communicator passed must be valid, although there is currently
543   no attempt made at handling any potential errors from `MPI_Abort()`. Note that while
544   `MPI_Abort()` is required to terminate only those processes which reside on comm, it is often
545   the case that `MPI_Abort()` terminates *all* processes.
546 
547   Example Usage:
548 .vb
549   PetscErrorCode boom(void) { return PETSC_ERR_MEM; }
550 
551   void foo(void)
552   {
553     PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
554   }
555 
556   double bar(void)
557   {
558     PetscCallAbort(PETSC_COMM_WORLD,boom()); // OK, does not return a type
559   }
560 
561   PetscCallAbort(MPI_COMM_NULL,boom()); // ERROR, communicator should be valid
562 
563   struct baz
564   {
565     baz()
566     {
567       PetscCallAbort(PETSC_COMM_SELF,boom()); // OK
568     }
569 
570     ~baz()
571     {
572       PetscCallAbort(PETSC_COMM_SELF,boom()); // OK (in fact the only way to handle PETSc errors)
573     }
574   };
575 .ve
576 
577   Level: intermediate
578 
579 .seealso: `SETERRABORT()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`,
580           `SETERRQ()`, `CHKMEMQ`, `PetscCallMPI()`
581 M*/
582 #if defined(PETSC_CLANG_STATIC_ANALYZER)
583 void PetscCallAbort(MPI_Comm,PetscErrorCode);
584 void PetscCallContinue(PetscErrorCode);
585 #else
586 #define PetscCallAbort(comm,...) do {                                                          \
587     PetscErrorCode ierr_abort_ = __VA_ARGS__;                                                  \
588     if (PetscUnlikely(ierr_abort_)) {                                                          \
589       PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr_abort_,PETSC_ERROR_REPEAT," "); \
590       MPI_Abort(comm,ierr_abort_);                                                             \
591     }                                                                                          \
592   } while (0)
593 #define PetscCallContinue(...)   do {                                                          \
594     PetscErrorCode ierr_continue_ = __VA_ARGS__;                                               \
595     if (PetscUnlikely(ierr_continue_)) PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr_continue_,PETSC_ERROR_REPEAT," "); \
596   } while (0)
597 #endif
598 
599 /*MC
600   CHKERRABORT - Checks error code returned from PETSc function. If non-zero it aborts immediately.
601 
602   Synopsis:
603   #include <petscerror.h>
604   void CHKERRABORT(MPI_Comm comm, PetscErrorCode ierr)
605 
606   Not Collective
607 
608   Input Parameters:
609 + comm - the MPI communicator
610 - ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
611 
612   Notes:
613   Deprecated in favor of `PetscCallAbort()`. This routine behaves identically to it.
614 
615   Level: deprecated
616 
617 .seealso: `PetscCallAbort()`
618 M*/
619 #define CHKERRABORT(comm,...) PetscCallAbort(comm,__VA_ARGS__)
620 #define CHKERRCONTINUE(...)   PetscCallContinue(__VA_ARGS__)
621 
622 /*MC
623    CHKERRA - Fortran-only replacement for use of `CHKERRQ()` in the main program, which aborts immediately
624 
625    Synopsis:
626    #include <petscsys.h>
627    PetscErrorCode CHKERRA(PetscErrorCode ierr)
628 
629    Not Collective
630 
631    Input Parameters:
632 .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
633 
634   Level: deprecated
635 
636    Note:
637    This macro is rarely needed, normal usage is `PetscCallA()` in the main Fortran program.
638 
639 .seealso: `PetscCall()`, `PetscCallA()`, `PetscCallAbort()`, `CHKERRQ()`, `SETERRA()`, `SETERRQ()`, `SETERRABORT()`
640 M*/
641 
642 PETSC_EXTERN PetscBool petscwaitonerrorflg;
643 PETSC_EXTERN PetscBool petscindebugger;
644 
645 /*MC
646    PETSCABORT - Call MPI_Abort with an informative error code
647 
648    Synopsis:
649    #include <petscsys.h>
650    PETSCABORT(MPI_Comm comm, PetscErrorCode ierr)
651 
652    Collective
653 
654    Input Parameters:
655 +  comm - A communicator, so that the error can be collective
656 -  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
657 
658    Level: advanced
659 
660    Notes:
661    If the option -start_in_debugger was used then this calls abort() to stop the program in the debugger.
662 
663    if `PetscCIEnabledPortableErrorOutput` is set it strives to exit cleanly without call `MPI_Abort()`
664 
665  M*/
666 #define PETSCABORT(comm,...) do {                                                              \
667     if (petscwaitonerrorflg) PetscSleep(1000);                                                 \
668     if (petscindebugger) abort();                                                              \
669     else {                                                                                     \
670       PetscErrorCode ierr_petsc_abort_ = __VA_ARGS__;                                          \
671       PetscMPIInt    size;                                                                     \
672       MPI_Comm_size(comm,&size);                                                               \
673       if (PetscCIEnabledPortableErrorOutput && size == PetscGlobalSize && ierr_petsc_abort_ != PETSC_ERR_SIG) { \
674         MPI_Finalize(); exit(0);                                                               \
675       } else if (PetscCIEnabledPortableErrorOutput && PetscGlobalSize == 1) {                  \
676         exit(0);                                                        \
677       } else {                                                                                 \
678         MPI_Abort(comm,(PetscMPIInt)ierr_petsc_abort_);                 \
679       }                                                                                        \
680     }                                                                                          \
681   } while (0)
682 
683 #ifdef PETSC_CLANGUAGE_CXX
684 /*MC
685   PetscCallThrow - Checks error code, if non-zero it calls the C++ error handler which throws
686   an exception
687 
688   Synopsis:
689   #include <petscerror.h>
690   void PetscCallThrow(PetscErrorCode ierr)
691 
692   Not Collective
693 
694   Input Parameter:
695 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
696 
697   Notes:
698   Requires PETSc to be configured with clanguage = c++. Throws a std::runtime_error() on error.
699 
700   Once the error handler throws the exception you can use `PetscCallVoid()` which returns without
701   an error code (bad idea since the error is ignored) or `PetscCallAbort()` to have `MPI_Abort()`
702   called immediately.
703 
704   Level: beginner
705 
706 .seealso: `SETERRQ()`, `PetscCall()`, `SETERRABORT()`, `PetscCallAbort()`, `PetscTraceBackErrorHandler()`,
707           `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
708 M*/
709 #define PetscCallThrow(...) do {                                                                    \
710     PetscErrorCode ierr_cxx_ = __VA_ARGS__;                                                    \
711     if (PetscUnlikely(ierr_cxx_)) PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr_cxx_,PETSC_ERROR_IN_CXX,PETSC_NULLPTR); \
712   } while (0)
713 
714 /*MC
715   CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
716 
717   Synopsis:
718   #include <petscerror.h>
719   void CHKERRXX(PetscErrorCode ierr)
720 
721   Not Collective
722 
723   Input Parameter:
724 . ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
725 
726   Notes:
727   Deprecated in favor of `PetscCallThrow()`. This routine behaves identically to it.
728 
729   Level: deprecated
730 
731 .seealso: `PetscCallThrow()`
732 M*/
733 #define CHKERRXX(...) PetscCallThrow(__VA_ARGS__)
734 #endif
735 
736 /*MC
737   PetscCallCXX - Checks C++ function calls and if they throw an exception, catch it and then
738   return a PETSc error code
739 
740   Synopsis:
741   #include <petscerror.h>
742   void PetscCallCXX(expr) noexcept;
743 
744   Not Collective
745 
746   Input Parameter:
747 . expr - An arbitrary expression
748 
749   Notes:
750   PetscCallCXX(expr) is a macro replacement for
751 .vb
752   try {
753     expr;
754   } catch (const std::exception& e) {
755     return ConvertToPetscErrorCode(e);
756   }
757 .ve
758   Due to the fact that it catches any (reasonable) exception, it is essentially noexcept.
759 
760   Example Usage:
761 .vb
762   void foo(void) { throw std::runtime_error("error"); }
763 
764   void bar()
765   {
766     PetscCallCXX(foo()); // ERROR bar() does not return PetscErrorCode
767   }
768 
769   PetscErrorCode baz()
770   {
771     PetscCallCXX(foo()); // OK
772 
773     PetscCallCXX(
774       bar();
775       foo(); // OK mutliple statements allowed
776     );
777   }
778 
779   struct bop
780   {
781     bop()
782     {
783       PetscCallCXX(foo()); // ERROR returns PetscErrorCode, cannot be used in constructors
784     }
785   };
786 
787   // ERROR contains do-while, cannot be used as function-try block
788   PetscErrorCode qux() PetscCallCXX(
789     bar();
790     baz();
791     foo();
792     return 0;
793   )
794 .ve
795 
796   Level: beginner
797 
798 .seealso: `PetscCallThrow()`, `SETERRQ()`, `PetscCall()`, `SETERRABORT()`, `PetscCallAbort()`,
799           `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`
800 M*/
801 #define PetscCallCXX(...) do {                                  \
802     PetscStackUpdateLine;                                       \
803     try {                                                       \
804       __VA_ARGS__;                                              \
805     } catch (const std::exception& e) {                         \
806       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"%s",e.what());     \
807     }                                                           \
808   } while (0)
809 
810 /*MC
811   CHKERRCXX - Checks C++ function calls and if they throw an exception, catch it and then
812   return a PETSc error code
813 
814   Synopsis:
815   #include <petscerror.h>
816   void CHKERRCXX(func) noexcept;
817 
818   Not Collective
819 
820   Input Parameter:
821 . func - C++ function calls
822 
823   Notes:
824   Deprecated in favor of `PetscCallCXX()`. This routine behaves identically to it.
825 
826   Level: deprecated
827 
828 .seealso: `PetscCallCXX()`
829 M*/
830 #define CHKERRCXX(...) PetscCallCXX(__VA_ARGS__)
831 
832 /*MC
833    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
834 
835    Synopsis:
836    #include <petscsys.h>
837    CHKMEMQ;
838 
839    Not Collective
840 
841   Level: beginner
842 
843    Notes:
844     We highly recommend using Valgrind https://petsc.org/release/faq/#valgrind or for NVIDIA CUDA systems
845     https://docs.nvidia.com/cuda/cuda-memcheck/index.html for finding memory problems. The ``CHKMEMQ`` macro is useful on systems that
846     do not have valgrind, but is not as good as valgrind or cuda-memcheck.
847 
848     Must run with the option -malloc_debug (-malloc_test in debug mode; or if `PetscMallocSetDebug()` called) to enable this option
849 
850     Once the error handler is called the calling function is then returned from with the given error code.
851 
852     By defaults prints location where memory that is corrupted was allocated.
853 
854     Use `CHKMEMA` for functions that return void
855 
856 .seealso: `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `SETERRQ()`, `PetscMallocValidate()`
857 M*/
858 #if defined(PETSC_CLANG_STATIC_ANALYZER)
859 #define CHKMEMQ
860 #define CHKMEMA
861 #else
862 #define CHKMEMQ do { \
863     PetscErrorCode ierr_memq_ = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__); \
864     if (PetscUnlikely(ierr_memq_)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr_memq_,PETSC_ERROR_REPEAT," "); \
865   } while (0)
866 #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__)
867 #endif
868 
869 /*E
870   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
871 
872   Level: advanced
873 
874   `PETSC_ERROR_IN_CXX` indicates the error was detected in C++ and an exception should be generated
875 
876   Developer Notes:
877     This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandler()
878 
879 .seealso: `PetscError()`, `SETERRQ()`
880 E*/
881 typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
882 
883 #if defined(__clang_analyzer__)
884 __attribute__((analyzer_noreturn))
885 #endif
886 PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...) PETSC_ATTRIBUTE_COLD PETSC_ATTRIBUTE_FORMAT(7,8);
887 
888 PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
889 PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
890 PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
891 PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
892 PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
893 PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
894 PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
895 PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
896 PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*) PETSC_ATTRIBUTE_COLD;
897 PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
898 PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
899 PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int,void*);
900 PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
901 PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
902 PETSC_EXTERN PetscErrorCode PetscCheckPointerSetIntensity(PetscInt);
903 PETSC_EXTERN void PetscSignalSegvCheckPointerOrMpi(void);
904 PETSC_DEPRECATED_FUNCTION("Use PetscSignalSegvCheckPointerOrMpi() (since version 3.13)") static inline void PetscSignalSegvCheckPointer(void) {PetscSignalSegvCheckPointerOrMpi();}
905 
906 /*MC
907     PetscErrorPrintf - Prints error messages.
908 
909    Synopsis:
910     #include <petscsys.h>
911      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
912 
913     Not Collective
914 
915     Input Parameter:
916 .   format - the usual printf() format string
917 
918    Options Database Keys:
919 +    -error_output_stdout - cause error messages to be printed to stdout instead of the (default) stderr
920 -    -error_output_none - to turn off all printing of error messages (does not change the way the error is handled.)
921 
922    Notes:
923     Use
924 $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
925 $                        error is handled.) and
926 $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on or you can use your own function
927 
928           Use
929      `PETSC_STDERR` = FILE* obtained from a file open etc. to have stderr printed to the file.
930      `PETSC_STDOUT` = FILE* obtained from a file open etc. to have stdout printed to the file.
931 
932           Use
933       `PetscPushErrorHandler()` to provide your own error handler that determines what kind of messages to print
934 
935    Level: developer
936 
937     Fortran Note:
938     This routine is not supported in Fortran.
939 
940 .seealso: `PetscFPrintf()`, `PetscSynchronizedPrintf()`, `PetscHelpPrintf()`, `PetscPrintf()`, `PetscPushErrorHandler()`, `PetscVFPrintf()`, `PetscHelpPrintf()`
941 M*/
942 PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[],...) PETSC_ATTRIBUTE_FORMAT(1,2);
943 
944 /*E
945      PetscFPTrap - types of floating point exceptions that may be trapped
946 
947      Currently only `PETSC_FP_TRAP_OFF` and `PETSC_FP_TRAP_ON` are handled. All others are treated as `PETSC_FP_TRAP_ON`.
948 
949      Level: intermediate
950 
951 .seealso: `PetscSetFPTrap()`, `PetscPushFPTrap()`
952  E*/
953 typedef enum {PETSC_FP_TRAP_OFF=0, PETSC_FP_TRAP_INDIV=1, PETSC_FP_TRAP_FLTOPERR=2, PETSC_FP_TRAP_FLTOVF=4, PETSC_FP_TRAP_FLTUND=8, PETSC_FP_TRAP_FLTDIV=16, PETSC_FP_TRAP_FLTINEX=32} PetscFPTrap;
954 #define  PETSC_FP_TRAP_ON  (PETSC_FP_TRAP_INDIV | PETSC_FP_TRAP_FLTOPERR | PETSC_FP_TRAP_FLTOVF | PETSC_FP_TRAP_FLTDIV | PETSC_FP_TRAP_FLTINEX)
955 PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
956 PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
957 PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
958 PETSC_EXTERN PetscErrorCode PetscDetermineInitialFPTrap(void);
959 
960 /*
961       Allows the code to build a stack frame as it runs
962 */
963 
964 #if defined(PETSC_USE_DEBUG)
965 #define PETSCSTACKSIZE 64
966 typedef struct  {
967   const char *function[PETSCSTACKSIZE];
968   const char *file[PETSCSTACKSIZE];
969         int  line[PETSCSTACKSIZE];
970         int  petscroutine[PETSCSTACKSIZE]; /* 0 external called from petsc, 1 petsc functions, 2 petsc user functions */
971         int  currentsize;
972         int  hotdepth;
973         PetscBool  check; /* option to check for correct Push/Pop semantics, true for default petscstack but not other stacks */
974 } PetscStack;
975 PETSC_EXTERN PetscStack petscstack;
976 #else
977 typedef struct {
978   char Silence_empty_struct_has_size_0_in_C_size_1_in_Cpp;
979 } PetscStack;
980 #endif
981 
982 #if defined(PETSC_SERIALIZE_FUNCTIONS)
983 #include <petsc/private/petscfptimpl.h>
984 /*
985    Registers the current function into the global function pointer to function name table
986 
987    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
988 */
989 #define PetscRegister__FUNCT__() do { \
990   static PetscBool __chked = PETSC_FALSE; \
991   if (!__chked) {\
992   void *ptr; PetscDLSym(NULL,PETSC_FUNCTION_NAME,&ptr);\
993   __chked = PETSC_TRUE;\
994   }} while (0)
995 #else
996 #define PetscRegister__FUNCT__()
997 #endif
998 
999 #if defined(PETSC_CLANG_STATIC_ANALYZER)
1000 #define PetscStackPushNoCheck(funct,petsc_routine,hot)
1001 #define PetscStackUpdateLine
1002 #define PetscStackPushExternal(funct)
1003 #define PetscStackPopNoCheck
1004 #define PetscStackClearTop
1005 #define PetscFunctionBegin
1006 #define PetscFunctionBeginUser
1007 #define PetscFunctionBeginHot
1008 #define PetscFunctionReturn(a)    return a
1009 #define PetscFunctionReturnVoid() return
1010 #define PetscStackPop
1011 #define PetscStackPush(f)
1012 #elif defined(PETSC_USE_DEBUG)
1013 
1014 #define PetscStackPush_Private(stack__,file__,func__,line__,petsc_routine__,hot__) do { \
1015     if (stack__.currentsize < PETSCSTACKSIZE) {                                         \
1016       stack__.function[stack__.currentsize]     = func__;                               \
1017       if (petsc_routine__) {                                                            \
1018         stack__.file[stack__.currentsize]         = file__;                             \
1019         stack__.line[stack__.currentsize]         = line__;                             \
1020       } else {                                                                          \
1021         stack__.file[stack__.currentsize]         = PETSC_NULLPTR;                      \
1022         stack__.line[stack__.currentsize]         = 0;                                  \
1023       }                                                                                 \
1024       stack__.petscroutine[stack__.currentsize] = petsc_routine__;                      \
1025     }                                                                                   \
1026     ++stack__.currentsize;                                                              \
1027     stack__.hotdepth += (hot__ || stack__.hotdepth);                                    \
1028   } while (0)
1029 
1030 /* uses PetscCheckAbort() because may be used in a function that does not return an error code */
1031 #define PetscStackPop_Private(stack__,func__) do {                                             \
1032     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__);\
1033     if (--stack__.currentsize < PETSCSTACKSIZE) {\
1034       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",stack__.function[stack__.currentsize],stack__.file[stack__.currentsize],stack__.line[stack__.currentsize],func__,__FILE__,__LINE__); \
1035       stack__.function[stack__.currentsize]     = PETSC_NULLPTR;                             \
1036       stack__.file[stack__.currentsize]         = PETSC_NULLPTR;                             \
1037       stack__.line[stack__.currentsize]         = 0;                                         \
1038       stack__.petscroutine[stack__.currentsize] = 0;                                         \
1039     }                                                                                        \
1040     stack__.hotdepth = PetscMax(stack__.hotdepth-1,0);                                       \
1041   } while (0)
1042 
1043 /*MC
1044    PetscStackPushNoCheck - Pushes a new function name and line number onto the PETSc default stack that tracks where the running program is
1045    currently in the source code.
1046 
1047    Not Collective
1048 
1049    Synopsis:
1050    #include <petscsys.h>
1051    void PetscStackPushNoCheck(char *funct,int petsc_routine,PetscBool hot);
1052 
1053    Input Parameters:
1054 +  funct - the function name
1055 .  petsc_routine - 2 user function, 1 PETSc function, 0 some other function
1056 -  hot - indicates that the function may be called often so expensive error checking should be turned off inside the function
1057 
1058    Level: developer
1059 
1060    Notes:
1061    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
1062    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
1063    help debug the problem.
1064 
1065    This version does not check the memory corruption (an expensive operation), use `PetscStackPush()` to check the memory.
1066 
1067    Use `PetscStackPushExternal()` for a function call that is about to be made to a non-PETSc or user function (such as BLAS etc).
1068 
1069    The default stack is a global variable called `petscstack`.
1070 
1071 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1072           `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPush()`, `PetscStackPop`,
1073           `PetscStackPushExternal()`
1074 M*/
1075 #define PetscStackPushNoCheck(funct,petsc_routine,hot) do {                             \
1076     PetscStackSAWsTakeAccess();                                                         \
1077     PetscStackPush_Private(petscstack,__FILE__,funct,__LINE__,petsc_routine,hot);       \
1078     PetscStackSAWsGrantAccess();                                                        \
1079   } while (0)
1080 
1081 /*MC
1082    PetscStackUpdateLine - in a function that has a `PetscFunctionBegin` or `PetscFunctionBeginUser` updates the stack line number to the
1083    current line number.
1084 
1085    Not Collective
1086 
1087    Synopsis:
1088    #include <petscsys.h>
1089    void PetscStackUpdateLine
1090 
1091    Level: developer
1092 
1093    Notes:
1094    Using `PetscCall()` and friends automatically handles this process
1095 
1096    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
1097    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1098    help debug the problem.
1099 
1100    The default stack is a global variable called petscstack.
1101 
1102    This is used by `PetscCall()` and is otherwise not like to be needed
1103 
1104 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPop`, `PetscCall()`
1105 M*/
1106 #define PetscStackUpdateLine                                         \
1107   if (petscstack.currentsize > 0 && petscstack.function[petscstack.currentsize-1] == PETSC_FUNCTION_NAME){ \
1108     petscstack.line[petscstack.currentsize-1] = __LINE__;              \
1109   }
1110 
1111 /*MC
1112    PetscStackPushExternal - Pushes a new function name onto the PETSc default stack that tracks where the running program is
1113    currently in the source code. Does not include the filename or line number since this is called by the calling routine
1114    for non-PETSc or user functions.
1115 
1116    Not Collective
1117 
1118    Synopsis:
1119    #include <petscsys.h>
1120    void PetscStackPushExternal(char *funct);
1121 
1122    Input Parameters:
1123 .  funct - the function name
1124 
1125    Level: developer
1126 
1127    Notes:
1128    Using `PetscCallExternal()` and friends automatically handles this process
1129 
1130    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
1131    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1132    help debug the problem.
1133 
1134    The default stack is a global variable called `petscstack`.
1135 
1136    This is to be used when calling an external package function such as a BLAS function.
1137 
1138    This also updates the stack line number for the current stack function.
1139 
1140 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1141           `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1142 M*/
1143 #define PetscStackPushExternal(funct) do {PetscStackUpdateLine; PetscStackPushNoCheck(funct,0,PETSC_TRUE);} while (0);
1144 
1145 /*MC
1146    PetscStackPopNoCheck - Pops a function name from the PETSc default stack that tracks where the running program is
1147    currently in the source code.
1148 
1149    Not Collective
1150 
1151    Synopsis:
1152    #include <petscsys.h>
1153    void PetscStackPopNoCheck(char *funct);
1154 
1155    Input Parameter:
1156 .   funct - the function name
1157 
1158    Level: developer
1159 
1160    Notes:
1161    Using `PetscCall()`, `PetscCallExternal()`, `PetscCallBack()` and friends negates the need to call this
1162 
1163    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
1164    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1165    help debug the problem.
1166 
1167    The default stack is a global variable called petscstack.
1168 
1169    Developer Note:
1170    `PetscStackPopNoCheck()` takes a function argument while  `PetscStackPop` does not, this difference is likely just historical.
1171 
1172 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1173 M*/
1174 #define PetscStackPopNoCheck(funct)                    do {     \
1175     PetscStackSAWsTakeAccess();                                 \
1176     PetscStackPop_Private(petscstack,funct);                    \
1177     PetscStackSAWsGrantAccess();                                \
1178   } while (0)
1179 
1180 #define PetscStackClearTop                             do {             \
1181     PetscStackSAWsTakeAccess();                                         \
1182     if (petscstack.currentsize > 0 &&                                   \
1183         --petscstack.currentsize < PETSCSTACKSIZE) {                    \
1184       petscstack.function[petscstack.currentsize]     = PETSC_NULLPTR;  \
1185       petscstack.file[petscstack.currentsize]         = PETSC_NULLPTR;  \
1186       petscstack.line[petscstack.currentsize]         = 0;              \
1187       petscstack.petscroutine[petscstack.currentsize] = 0;              \
1188     }                                                                   \
1189     petscstack.hotdepth = PetscMax(petscstack.hotdepth-1,0);            \
1190     PetscStackSAWsGrantAccess();                                        \
1191   } while (0)
1192 
1193 /*MC
1194    PetscFunctionBegin - First executable line of each PETSc function,  used for error handling. Final
1195       line of PETSc functions should be `PetscFunctionReturn`(0);
1196 
1197    Synopsis:
1198    #include <petscsys.h>
1199    void PetscFunctionBegin;
1200 
1201    Not Collective
1202 
1203    Usage:
1204 .vb
1205      int something;
1206 
1207      PetscFunctionBegin;
1208 .ve
1209 
1210    Notes:
1211      Use `PetscFunctionBeginUser` for application codes.
1212 
1213      Not available in Fortran
1214 
1215    Level: developer
1216 
1217 .seealso: `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`
1218 
1219 M*/
1220 #define PetscFunctionBegin do {                               \
1221     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,1,PETSC_FALSE); \
1222     PetscRegister__FUNCT__();                                 \
1223   } while (0)
1224 
1225 /*MC
1226    PetscFunctionBeginHot - Substitute for `PetscFunctionBegin` to be used in functions that are called in
1227    performance-critical circumstances.  Use of this function allows for lighter profiling by default.
1228 
1229    Synopsis:
1230    #include <petscsys.h>
1231    void PetscFunctionBeginHot;
1232 
1233    Not Collective
1234 
1235    Usage:
1236 .vb
1237      int something;
1238 
1239      PetscFunctionBeginHot;
1240 .ve
1241 
1242    Notes:
1243      Not available in Fortran
1244 
1245    Level: developer
1246 
1247 .seealso: `PetscFunctionBegin`, `PetscFunctionReturn()`, `PetscStackPushNoCheck()`
1248 
1249 M*/
1250 #define PetscFunctionBeginHot do {                           \
1251     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,1,PETSC_TRUE); \
1252     PetscRegister__FUNCT__();                                \
1253   } while (0)
1254 
1255 /*MC
1256    PetscFunctionBeginUser - First executable line of user provided routines
1257 
1258    Synopsis:
1259    #include <petscsys.h>
1260    void PetscFunctionBeginUser;
1261 
1262    Not Collective
1263 
1264    Usage:
1265 .vb
1266      int something;
1267 
1268      PetscFunctionBeginUser;
1269 .ve
1270 
1271    Notes:
1272       Functions that incorporate this must call `PetscFunctionReturn()` instead of return except for main().
1273 
1274       May be used before `PetscInitialize()`
1275 
1276       Not available in Fortran
1277 
1278       This is identical to `PetscFunctionBegin` except it labels the routine as a user
1279       routine instead of as a PETSc library routine.
1280 
1281    Level: intermediate
1282 
1283 .seealso: `PetscFunctionReturn()`, `PetscFunctionBegin`, `PetscFunctionBeginHot`, `PetscStackPushNoCheck()`
1284 
1285 M*/
1286 #define PetscFunctionBeginUser do {                           \
1287     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,2,PETSC_FALSE); \
1288     PetscRegister__FUNCT__();                                 \
1289   } while (0)
1290 
1291 /*MC
1292    PetscStackPush - Pushes a new function name and line number onto the PETSc default stack that tracks where the running program is
1293    currently in the source code and verifies the memory is not corrupted.
1294 
1295    Not Collective
1296 
1297    Synopsis:
1298    #include <petscsys.h>
1299    void PetscStackPush(char *funct)
1300 
1301    Input Parameter:
1302 .  funct - the function name
1303 
1304    Level: developer
1305 
1306    Notes:
1307    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
1308    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1309    help debug the problem.
1310 
1311    The default stack is a global variable called petscstack.
1312 
1313 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPopNoCheck()`, `PetscCall()`, `PetscFunctionBegin()`,
1314           `PetscFunctionReturn()`, `PetscFunctionBeginHot()`, `PetscFunctionBeginUser()`, `PetscStackPushNoCheck()`, `PetscStackPop`
1315 M*/
1316 #define PetscStackPush(n)       do {        \
1317     PetscStackPushNoCheck(n,0,PETSC_FALSE); \
1318     CHKMEMQ;                                \
1319   } while (0)
1320 
1321 /*MC
1322    PetscStackPop - Pops a function name from the PETSc default stack that tracks where the running program is
1323    currently in the source code and verifies the memory is not corrupted.
1324 
1325    Not Collective
1326 
1327    Synopsis:
1328    #include <petscsys.h>
1329    void PetscStackPop
1330 
1331    Level: developer
1332 
1333    Notes:
1334    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
1335    occurred, for example, when a signal is received. It is recommended to use the debugger if extensive information is needed to
1336    help debug the problem.
1337 
1338    The default stack is a global variable called petscstack.
1339 
1340 .seealso: `PetscAttachDebugger()`, `PetscStackCopy()`, `PetscStackView()`, `PetscStackPushNoCheck()`, `PetscStackPopNoCheck()`, `PetscStackPush()`
1341 M*/
1342 #define PetscStackPop           do {       \
1343       CHKMEMQ;                                   \
1344       PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
1345     } while (0)
1346 
1347 /*MC
1348    PetscFunctionReturn - Last executable line of each PETSc function
1349         used for error handling. Replaces `return()`
1350 
1351    Synopsis:
1352    #include <petscsys.h>
1353    void PetscFunctionReturn(0);
1354 
1355    Not Collective
1356 
1357    Usage:
1358 .vb
1359     ....
1360      PetscFunctionReturn(0);
1361    }
1362 .ve
1363 
1364    Note:
1365      Not available in Fortran
1366 
1367    Level: developer
1368 
1369 .seealso: `PetscFunctionBegin()`, `PetscStackPopNoCheck()`
1370 
1371 M*/
1372 #define PetscFunctionReturn(a)    do {          \
1373     PetscStackPopNoCheck(PETSC_FUNCTION_NAME);  \
1374     return a;                                   \
1375   } while (0)
1376 
1377 #define PetscFunctionReturnVoid() do {          \
1378     PetscStackPopNoCheck(PETSC_FUNCTION_NAME);  \
1379     return;                                     \
1380   } while (0)
1381 #else /* PETSC_USE_DEBUG */
1382 #define PetscStackPushNoCheck(funct,petsc_routine,hot)
1383 #define PetscStackUpdateLine
1384 #define PetscStackPushExternal(funct)
1385 #define PetscStackPopNoCheck
1386 #define PetscStackClearTop
1387 #define PetscFunctionBegin
1388 #define PetscFunctionBeginUser
1389 #define PetscFunctionBeginHot
1390 #define PetscFunctionReturn(a)    return a
1391 #define PetscFunctionReturnVoid() return
1392 #define PetscStackPop             CHKMEMQ
1393 #define PetscStackPush(f)         CHKMEMQ
1394 #endif /* PETSC_USE_DEBUG */
1395 
1396 #if defined(PETSC_CLANG_STATIC_ANALYZER)
1397 #define PetscStackCallExternalVoid(name,routine)
1398 #define PetscCallExternal(func,...)
1399 #else
1400 /*MC
1401     PetscStackCallExternalVoid - Calls an external library routine or user function after pushing the name of the routine on the stack.
1402 
1403    Input Parameters:
1404 +   name - string that gives the name of the function being called
1405 -   routine - actual call to the routine, for example, functionname(a,b)
1406 
1407    Level: developer
1408 
1409    Note:
1410    Often one should use `PetscCallExternal()` instead. This routine is intended for external library routines that DO NOT return error codes
1411 
1412    In debug mode this also checks the memory for corruption at the end of the function call.
1413 
1414    Certain external packages, such as BLAS/LAPACK may have their own macros for managing the call, error checking, etc.
1415 
1416    Developer Note:
1417    This is so that when a user or external library routine results in a crash or corrupts memory, they get blamed instead of PETSc.
1418 
1419 .seealso: `PetscCall()`, `PetscStackPushNoCheck()`, `PetscStackPush()`, `PetscCallExternal()`, `PetscCallBLAS()`
1420 @*/
1421 #define PetscStackCallExternalVoid(name,routine) do { PetscStackPush(name);routine;PetscStackPop; } while (0)
1422 
1423 /*MC
1424     PetscCallExternal - Calls an external library routine that returns an error code after pushing the name of the routine on the stack.
1425 
1426    Input Parameters:
1427 +   func-  name of the routine
1428 -   args - arguments to the routine
1429 
1430    Level: developer
1431 
1432    Notes:
1433    This is intended for external package routines that return error codes. Use `PetscStackCallExternalVoid()` for those that do not.
1434 
1435    In debug mode this also checks the memory for corruption at the end of the function call.
1436 
1437    Assumes the error return code of the function is an integer and that a value of 0 indicates success
1438 
1439    Developer Note:
1440    This is so that when an external packge routine results in a crash or corrupts memory, they get blamed instead of PETSc.
1441 
1442 .seealso: `PetscCall()`, `PetscStackPushNoCheck()`, `PetscStackPush()`, `PetscStackCallExternalVoid()`
1443 M*/
1444 #define PetscCallExternal(func,...) do {                                                  \
1445     PetscStackPush(PetscStringize(func));                                                      \
1446     PetscErrorCode __ierr = func(__VA_ARGS__);                                                 \
1447     PetscStackPop;                                                                             \
1448     PetscCheck(!__ierr,PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s(): error code %d",PetscStringize(func),__ierr); \
1449   } while (0)
1450 #endif /* PETSC_CLANG_STATIC_ANALYZER */
1451 
1452 #endif
1453