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