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