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