xref: /petsc/include/petscerror.h (revision 98921bda46e76d7aaed9e0138c5ff9d0ce93f355)
154a8ef01SBarry Smith /*
2f621e05eSBarry Smith     Contains all error handling interfaces for PETSc.
354a8ef01SBarry Smith */
426bd1501SBarry Smith #if !defined(PETSCERROR_H)
526bd1501SBarry Smith #define PETSCERROR_H
66c7e564aSBarry Smith 
7e1bf4ed2SJacob Faibussowitsch #include <petscmacros.h>
8e1bf4ed2SJacob Faibussowitsch #include <petscsystypes.h>
9e1bf4ed2SJacob Faibussowitsch 
1054a8ef01SBarry Smith /*
11329ffe3dSLois Curfman McInnes      These are the generic error codes. These error codes are used
12e2d1d2b7SBarry Smith      many different places in the PETSc source code. The string versions are
130e5e90baSSatish Balay      at src/sys/error/err.c any changes here must also be made there
14e951c290SBarry Smith      These are also define in src/sys/f90-mod/petscerror.h any CHANGES here
150f9cf654SBarry Smith      must be also made there.
1645d48df9SBarry Smith 
1754a8ef01SBarry Smith */
182a6744ebSBarry Smith #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
192a6744ebSBarry Smith 
2045d48df9SBarry Smith #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
2147794344SBarry Smith #define PETSC_ERR_SUP              56   /* no support for requested operation */
22e2d1d2b7SBarry Smith #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
23e2d1d2b7SBarry Smith #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
2445d48df9SBarry Smith #define PETSC_ERR_SIG              59   /* signal received */
25f1caa5a4SBarry Smith #define PETSC_ERR_FP               72   /* floating point exception */
26a8c6a408SBarry Smith #define PETSC_ERR_COR              74   /* corrupted PETSc object */
27a8c6a408SBarry Smith #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
28329ffe3dSLois Curfman McInnes #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
29329ffe3dSLois Curfman McInnes #define PETSC_ERR_MEMC             78   /* memory corruption */
30b3cc6726SBarry Smith #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
311302d50aSBarry Smith #define PETSC_ERR_USER             83   /* user has not provided needed function */
324e2ffeddSBarry Smith #define PETSC_ERR_SYS              88   /* error in system call */
33a8b45ee7SBarry Smith #define PETSC_ERR_POINTER          70   /* pointer does not point to valid address */
343d96e996SBarry Smith #define PETSC_ERR_MPI_LIB_INCOMP   87   /* MPI library at runtime is not compatible with MPI user compiled with */
3545d48df9SBarry Smith 
3645d48df9SBarry Smith #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
3745d48df9SBarry Smith #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
38a8c6a408SBarry Smith #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
3945d48df9SBarry Smith #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
4045d48df9SBarry Smith #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
414f227f7cSBarry Smith #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
424f227f7cSBarry Smith #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
436831982aSBarry Smith #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
44d252947aSBarry Smith #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
458cda6cd7SBarry Smith #define PETSC_ERR_ARG_TYPENOTSET   89   /* the type of the object has not yet been set */
46a8c6a408SBarry Smith #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
474482741eSBarry Smith #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
48958c9bccSBarry Smith #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
494f227f7cSBarry Smith 
504f227f7cSBarry Smith #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
514f227f7cSBarry Smith #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
524f227f7cSBarry Smith #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
53a8c6a408SBarry Smith #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
5445d48df9SBarry Smith 
55329ffe3dSLois Curfman McInnes #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
569e3b2f23SBarry Smith #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
5754a8ef01SBarry Smith 
5868e69593SBarry Smith #define PETSC_ERR_INT_OVERFLOW     84
593855c12bSBarry Smith 
60bf3909cdSBarry Smith #define PETSC_ERR_FLOP_COUNT       90
61e113a28aSBarry Smith #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
6292e8f287SBarry Smith #define PETSC_ERR_MISSING_FACTOR   92  /* MatGetFactor() failed */
63691b26d3SBarry Smith #define PETSC_ERR_OPT_OVERWRITE    93  /* attempted to over write options which should not be changed */
64691b26d3SBarry Smith #define PETSC_ERR_WRONG_MPI_SIZE   94  /* example/application run with number of MPI ranks it does not support */
65691b26d3SBarry Smith #define PETSC_ERR_USER_INPUT       95  /* missing or incorrect user input */
66589f383fSBarry Smith #define PETSC_ERR_GPU_RESOURCE     96  /* unable to load a GPU resource, for example cuBLAS */
67589f383fSBarry Smith #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 */
68ffc4695bSBarry Smith #define PETSC_ERR_MPI              98  /* general MPI error */
69ffc4695bSBarry Smith #define PETSC_ERR_MAX_VALUE        99  /* this is always the one more than the largest error code */
70b9eb5ee8SHong Zhang 
71*98921bdaSJacob Faibussowitsch #define SETERRQ1(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
72*98921bdaSJacob Faibussowitsch #define SETERRQ2(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
73*98921bdaSJacob Faibussowitsch #define SETERRQ3(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
74*98921bdaSJacob Faibussowitsch #define SETERRQ4(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
75*98921bdaSJacob Faibussowitsch #define SETERRQ5(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
76*98921bdaSJacob Faibussowitsch #define SETERRQ6(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
77*98921bdaSJacob Faibussowitsch #define SETERRQ7(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
78*98921bdaSJacob Faibussowitsch #define SETERRQ8(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
79*98921bdaSJacob Faibussowitsch #define SETERRQ9(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use SETERRQ() (since version 3.17)\"") SETERRQ(__VA_ARGS__)
80*98921bdaSJacob Faibussowitsch 
8130de9b25SBarry Smith /*MC
821957e957SBarry Smith    SETERRQ - Macro to be called when an error has been detected,
8330de9b25SBarry Smith 
8430de9b25SBarry Smith    Synopsis:
85aaa7dc30SBarry Smith    #include <petscsys.h>
86*98921bdaSJacob Faibussowitsch    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
8730de9b25SBarry Smith 
88d083f849SBarry Smith    Collective
8930de9b25SBarry Smith 
9030de9b25SBarry Smith    Input Parameters:
913af045c5SBarry Smith +  comm - A communicator, use PETSC_COMM_SELF unless you know all ranks of another communicator will detect the error
923af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
9330de9b25SBarry Smith -  message - error message
9430de9b25SBarry Smith 
9530de9b25SBarry Smith   Level: beginner
9630de9b25SBarry Smith 
9730de9b25SBarry Smith    Notes:
9830de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
9930de9b25SBarry Smith 
10030de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
10130de9b25SBarry Smith 
1023b1008b8SBarry Smith    Fortran Notes:
1033b1008b8SBarry Smith       SETERRQ() may be called from Fortran subroutines but SETERRA() must be called from the
1043b1008b8SBarry Smith       Fortran main program.
1053b1008b8SBarry Smith 
106*98921bdaSJacob Faibussowitsch .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRA(), CHKERRMPI()
10730de9b25SBarry Smith M*/
108*98921bdaSJacob Faibussowitsch #define SETERRQ(comm,ierr,...) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,__VA_ARGS__)
109986eef2eSBarry Smith 
110ffc4695bSBarry Smith /*
111ffc4695bSBarry Smith     Returned from PETSc functions that are called from MPI, such as related to attributes
112ffc4695bSBarry Smith       Do not confuse PETSC_MPI_ERROR_CODE and PETSC_ERR_MPI, the first is registered with MPI and returned to MPI as
113888a1fb3SBarry Smith       an error code, the latter is a regular PETSc error code passed within PETSc code indicating an error was detected in an MPI call.
114ffc4695bSBarry Smith */
115ffc4695bSBarry Smith PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CLASS;
116ffc4695bSBarry Smith PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CODE;
117ffc4695bSBarry Smith 
118986eef2eSBarry Smith /*MC
119986eef2eSBarry Smith    SETERRMPI - Macro to be called when an error has been detected within an MPI callback function
120986eef2eSBarry Smith 
121986eef2eSBarry Smith    Synopsis:
122986eef2eSBarry Smith    #include <petscsys.h>
123*98921bdaSJacob Faibussowitsch    PetscErrorCode SETERRMPI(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
124986eef2eSBarry Smith 
125d083f849SBarry Smith    Collective
126986eef2eSBarry Smith 
127986eef2eSBarry Smith    Input Parameters:
128986eef2eSBarry Smith +  comm - A communicator, use PETSC_COMM_SELF unless you know all ranks of another communicator will detect the error
129986eef2eSBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
130986eef2eSBarry Smith -  message - error message
131986eef2eSBarry Smith 
132986eef2eSBarry Smith   Level: developer
133986eef2eSBarry Smith 
134986eef2eSBarry Smith    Notes:
135888a1fb3SBarry Smith     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
136986eef2eSBarry Smith     which is registered with MPI_Add_error_code() when PETSc is initialized.
137986eef2eSBarry Smith 
138*98921bdaSJacob Faibussowitsch .seealso: SETERRQ(), CHKERRQ(), CHKERRMPI(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKMEMQ
139986eef2eSBarry Smith M*/
140*98921bdaSJacob Faibussowitsch #define SETERRMPI(comm,ierr,...) return (PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,__VA_ARGS__),PETSC_MPI_ERROR_CODE)
141ee8199e6SMatthew G. Knepley 
142ee8199e6SMatthew G. Knepley /*MC
143f388eb8bSPatrick Sanan    SETERRA - Fortran-only macro that can be called when an error has been detected from the main program
144f388eb8bSPatrick Sanan 
145f388eb8bSPatrick Sanan    Synopsis:
146f388eb8bSPatrick Sanan    #include <petscsys.h>
147f388eb8bSPatrick Sanan    PetscErrorCode SETERRA(MPI_Comm comm,PetscErrorCode ierr,char *message)
148f388eb8bSPatrick Sanan 
149f388eb8bSPatrick Sanan    Collective
150f388eb8bSPatrick Sanan 
151f388eb8bSPatrick Sanan    Input Parameters:
152f388eb8bSPatrick Sanan +  comm - A communicator, so that the error can be collective
153f388eb8bSPatrick Sanan .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
154f388eb8bSPatrick Sanan -  message - error message in the printf format
155f388eb8bSPatrick Sanan 
156f388eb8bSPatrick Sanan   Level: beginner
157f388eb8bSPatrick Sanan 
158f388eb8bSPatrick Sanan    Notes:
159f388eb8bSPatrick Sanan     This should only be used with Fortran. With C/C++, use SETERRQ().
160f388eb8bSPatrick Sanan 
161f388eb8bSPatrick Sanan    Fortran Notes:
162f388eb8bSPatrick Sanan       SETERRQ() may be called from Fortran subroutines but SETERRA() must be called from the
163f388eb8bSPatrick Sanan       Fortran main program.
164f388eb8bSPatrick Sanan 
165f388eb8bSPatrick Sanan .seealso: SETERRQ(), SETERRABORT(), CHKERRQ(), CHKERRA(), CHKERRABORT()
166f388eb8bSPatrick Sanan M*/
167f388eb8bSPatrick Sanan 
168f388eb8bSPatrick Sanan /*MC
169fa190f98SMatthew G. Knepley    SETERRABORT - Macro that can be called when an error has been detected,
170fa190f98SMatthew G. Knepley 
171fa190f98SMatthew G. Knepley    Synopsis:
172fa190f98SMatthew G. Knepley    #include <petscsys.h>
173*98921bdaSJacob Faibussowitsch    PetscErrorCode SETERRABORT(MPI_Comm comm,PetscErrorCode ierr,char *message,...)
174fa190f98SMatthew G. Knepley 
175d083f849SBarry Smith    Collective
176fa190f98SMatthew G. Knepley 
177fa190f98SMatthew G. Knepley    Input Parameters:
178fa190f98SMatthew G. Knepley +  comm - A communicator, so that the error can be collective
1793af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
180fa190f98SMatthew G. Knepley -  message - error message in the printf format
181fa190f98SMatthew G. Knepley 
182fa190f98SMatthew G. Knepley   Level: beginner
183fa190f98SMatthew G. Knepley 
184fa190f98SMatthew G. Knepley    Notes:
185fa190f98SMatthew G. Knepley     This function just calls MPI_Abort().
186fa190f98SMatthew G. Knepley 
187*98921bdaSJacob Faibussowitsch .seealso: SETERRQ(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ
188fa190f98SMatthew G. Knepley M*/
189*98921bdaSJacob Faibussowitsch #define SETERRABORT(comm,ierr,...) do {                                                        \
190*98921bdaSJacob Faibussowitsch     PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,__VA_ARGS__); \
191*98921bdaSJacob Faibussowitsch     MPI_Abort(comm,ierr);                                                                      \
192*98921bdaSJacob Faibussowitsch   } while (0)
1939a00fa46SSatish Balay 
19430de9b25SBarry Smith /*MC
195888a1fb3SBarry Smith    CHKERRQ - Checks error code returned from PETSc function, if non-zero it calls the error handler and then returns. Use CHKERRMPI() for checking errors from MPI calls
19630de9b25SBarry Smith 
19730de9b25SBarry Smith    Synopsis:
198aaa7dc30SBarry Smith    #include <petscsys.h>
1993af045c5SBarry Smith    PetscErrorCode CHKERRQ(PetscErrorCode ierr)
20030de9b25SBarry Smith 
201eca87e8dSBarry Smith    Not Collective
20230de9b25SBarry Smith 
20330de9b25SBarry Smith    Input Parameters:
2043af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
20530de9b25SBarry Smith 
20630de9b25SBarry Smith   Level: beginner
20730de9b25SBarry Smith 
20830de9b25SBarry Smith    Notes:
20930de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
21030de9b25SBarry Smith 
21130de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
2125324ea47SKris Buschelman 
2133af045c5SBarry Smith     CHKERRQ(ierr) is fundamentally a macro replacement for
2143af045c5SBarry Smith          if (ierr) return(PetscError(...,ierr,...));
2155324ea47SKris Buschelman 
2165324ea47SKris Buschelman     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
2175324ea47SKris Buschelman     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
2185324ea47SKris Buschelman     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
21958ebbce7SBarry Smith     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
2203af045c5SBarry Smith          if (ierr) {PetscError(....); return(YourReturnType);}
2210298fd71SBarry Smith     where you may pass back a NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
22258ebbce7SBarry Smith     MPI_Abort() returned immediately.
22358ebbce7SBarry Smith 
2247037b0edSPatrick Sanan    Fortran Notes:
2257037b0edSPatrick Sanan       CHKERRQ() may be called from Fortran subroutines but CHKERRA() must be called from the
2267037b0edSPatrick Sanan       Fortran main program.
2277037b0edSPatrick Sanan 
228*98921bdaSJacob Faibussowitsch .seealso: SETERRQ(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKMEMQ, CHKERRA()
22930de9b25SBarry Smith M*/
230064a246eSJacob Faibussowitsch #if !defined(PETSC_CLANG_STATIC_ANALYZER)
231ee74f559SJed Brown #define CHKERRQ(ierr)          do {PetscErrorCode ierr__ = (ierr); if (PetscUnlikely(ierr__)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr__,PETSC_ERROR_REPEAT," ");} while (0)
2320219f5dfSJed Brown #define CHKERRV(ierr)          do {PetscErrorCode ierr__ = (ierr); if (PetscUnlikely(ierr__)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr__,PETSC_ERROR_REPEAT," ");return;}} while (0)
233064a246eSJacob Faibussowitsch #else
234064a246eSJacob Faibussowitsch #define CHKERRQ(ierr)
235064a246eSJacob Faibussowitsch #define CHKERRV(ierr)
236064a246eSJacob Faibussowitsch #endif
237064a246eSJacob Faibussowitsch 
2387037b0edSPatrick Sanan /*MC
239f388eb8bSPatrick Sanan    CHKERRA - Fortran-only replacement for CHKERRQ in the main program, which aborts immediately
240f388eb8bSPatrick Sanan 
241f388eb8bSPatrick Sanan    Synopsis:
242f388eb8bSPatrick Sanan    #include <petscsys.h>
243f388eb8bSPatrick Sanan    PetscErrorCode CHKERRA(PetscErrorCode ierr)
244f388eb8bSPatrick Sanan 
245f388eb8bSPatrick Sanan    Not Collective
246f388eb8bSPatrick Sanan 
247f388eb8bSPatrick Sanan    Input Parameters:
248f388eb8bSPatrick Sanan .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
249f388eb8bSPatrick Sanan 
250f388eb8bSPatrick Sanan   Level: beginner
251f388eb8bSPatrick Sanan 
252f388eb8bSPatrick Sanan    Notes:
253f388eb8bSPatrick Sanan       This should only be used with Fortran. With C/C++, use CHKERRQ() in normal usage,
254f388eb8bSPatrick Sanan       or CHKERRABORT() if wanting to abort immediately on error.
255f388eb8bSPatrick Sanan 
256f388eb8bSPatrick Sanan    Fortran Notes:
257f388eb8bSPatrick Sanan       CHKERRQ() may be called from Fortran subroutines but CHKERRA() must be called from the
258f388eb8bSPatrick Sanan       Fortran main program.
259f388eb8bSPatrick Sanan 
260f388eb8bSPatrick Sanan .seealso: CHKERRQ(), CHKERRABORT(), SETERRA(), SETERRQ(), SETERRABORT()
261f388eb8bSPatrick Sanan M*/
262f388eb8bSPatrick Sanan 
263f388eb8bSPatrick Sanan /*MC
2647037b0edSPatrick Sanan    CHKERRABORT - Checks error code returned from PETSc function. If non-zero it aborts immediately.
2657037b0edSPatrick Sanan 
2667037b0edSPatrick Sanan    Synopsis:
2677037b0edSPatrick Sanan    #include <petscsys.h>
2687037b0edSPatrick Sanan    PetscErrorCode CHKERRABORT(MPI_Comm comm,PetscErrorCode ierr)
2697037b0edSPatrick Sanan 
2707037b0edSPatrick Sanan    Not Collective
2717037b0edSPatrick Sanan 
2727037b0edSPatrick Sanan    Input Parameters:
2737037b0edSPatrick Sanan .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
2747037b0edSPatrick Sanan 
2757037b0edSPatrick Sanan   Level: intermediate
2767037b0edSPatrick Sanan 
277*98921bdaSJacob Faibussowitsch .seealso: SETERRABORT(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, CHKERRMPI()
2787037b0edSPatrick Sanan M*/
2796d210af2SJacob Faibussowitsch #if defined(PETSC_CLANG_STATIC_ANALYZER)
280064a246eSJacob Faibussowitsch #define CHKERRABORT(comm,ierr)
281064a246eSJacob Faibussowitsch #define CHKERRCONTINUE(ierr)
2826d210af2SJacob Faibussowitsch #else
2836d210af2SJacob Faibussowitsch #define CHKERRABORT(comm,ierr) do {PetscErrorCode ierr__ = (ierr); if (PetscUnlikely(ierr__)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr__,PETSC_ERROR_REPEAT," ");MPI_Abort(comm,ierr);}} while (0)
2846d210af2SJacob Faibussowitsch #define CHKERRCONTINUE(ierr)   do {PetscErrorCode ierr__ = (ierr); if (PetscUnlikely(ierr__)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr__,PETSC_ERROR_REPEAT," ");}} while (0)
285064a246eSJacob Faibussowitsch #endif
286986eef2eSBarry Smith 
2877c66cc67SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscAbortFindSourceFile_Private(const char*,PetscInt*);
2881e4f893aSSatish Balay PETSC_EXTERN PetscBool petscwaitonerrorflg;
2891e4f893aSSatish Balay PETSC_EXTERN PetscBool petscindebugger;
2907c66cc67SJunchao Zhang 
2917c66cc67SJunchao Zhang /*MC
2927c66cc67SJunchao Zhang    PETSCABORT - Call MPI_Abort with an informative error code
2937c66cc67SJunchao Zhang 
2947c66cc67SJunchao Zhang    Synopsis:
2957c66cc67SJunchao Zhang    #include <petscsys.h>
2967c66cc67SJunchao Zhang    PETSCABORT(MPI_Comm comm, PetscErrorCode ierr)
2977c66cc67SJunchao Zhang 
2987c66cc67SJunchao Zhang    Collective
2997c66cc67SJunchao Zhang 
3007c66cc67SJunchao Zhang    Input Parameters:
3017c66cc67SJunchao Zhang +  comm - A communicator, so that the error can be collective
3027c66cc67SJunchao Zhang -  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
3037c66cc67SJunchao Zhang 
304bf4d2887SBarry Smith    Level: advanced
3057c66cc67SJunchao Zhang 
306bf4d2887SBarry Smith    Notes:
307bf4d2887SBarry Smith    We pass MPI_Abort() an error code of format XX_YYYY_ZZZ, where XX, YYYY are an index and line number of the file
3087233276eSBarry Smith    where PETSCABORT is called, respectively. ZZZ is the PETSc error code.
3097c66cc67SJunchao Zhang 
3107233276eSBarry Smith    If XX is zero, this means that the call was made in the routine main().
3117233276eSBarry Smith    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[]
3127c66cc67SJunchao Zhang      is out of date. PETSc developers have to update it.
313a0760fecSJunchao Zhang    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.
3147233276eSBarry Smith 
315bf4d2887SBarry Smith    If the option -start_in_debugger was used then this calls abort() to stop the program in the debugger.
316bf4d2887SBarry Smith 
3177c66cc67SJunchao Zhang M*/
3187c66cc67SJunchao Zhang #define PETSCABORT(comm,ierr)  \
3197c66cc67SJunchao Zhang    do {                                                               \
3207c66cc67SJunchao Zhang       PetscInt       idx = 0;                                         \
3217c66cc67SJunchao Zhang       PetscMPIInt    errcode;                                         \
3227c66cc67SJunchao Zhang       PetscAbortFindSourceFile_Private(__FILE__,&idx);                \
32365f093c6SBarry Smith       errcode = (PetscMPIInt)(0*idx*10000000 + 0*__LINE__*1000 + ierr);   \
324baae8e41SSatish Balay       if (petscwaitonerrorflg) PetscSleep(1000);                      \
325bf4d2887SBarry Smith       if (petscindebugger) abort();                                   \
326bf4d2887SBarry Smith       else MPI_Abort(comm,errcode);                                   \
3277c66cc67SJunchao Zhang    } while (0)
328986eef2eSBarry Smith 
329986eef2eSBarry Smith /*MC
330888a1fb3SBarry Smith    CHKERRMPI - Checks error code returned from MPI calls, if non-zero it calls the error handler and then returns
331986eef2eSBarry Smith 
332986eef2eSBarry Smith    Synopsis:
333986eef2eSBarry Smith    #include <petscsys.h>
334986eef2eSBarry Smith    PetscErrorCode CHKERRMPI(PetscErrorCode ierr)
335986eef2eSBarry Smith 
336986eef2eSBarry Smith    Not Collective
337986eef2eSBarry Smith 
338986eef2eSBarry Smith    Input Parameters:
339986eef2eSBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
340986eef2eSBarry Smith 
341888a1fb3SBarry Smith   Level: intermediate
342986eef2eSBarry Smith 
343986eef2eSBarry Smith    Notes:
344888a1fb3SBarry Smith     Always returns the error code PETSC_ERR_MPI; the MPI error code and string are embedded in the string error message
345986eef2eSBarry Smith 
346*98921bdaSJacob Faibussowitsch .seealso: SETERRMPI(), CHKERRQ(), SETERRQ(), SETERRABORT(), CHKERRABORT(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKMEMQ
347986eef2eSBarry Smith M*/
3486d210af2SJacob Faibussowitsch #if defined(PETSC_CLANG_STATIC_ANALYZER)
3496d210af2SJacob Faibussowitsch #define CHKERRMPI(ierr)
3506d210af2SJacob Faibussowitsch #else
351ffc4695bSBarry Smith #define CHKERRMPI(ierr) \
352ffc4695bSBarry Smith do { \
3533aa7491dSLisandro Dalcin   PetscErrorCode _7_errorcode = (ierr); \
3543aa7491dSLisandro Dalcin   if (PetscUnlikely(_7_errorcode)) { \
3553aa7491dSLisandro Dalcin     char _7_errorstring[MPI_MAX_ERROR_STRING]; \
356a964e738SLisandro Dalcin     PetscMPIInt _7_resultlen; \
357a964e738SLisandro Dalcin     MPI_Error_string(_7_errorcode,(char*)_7_errorstring,&_7_resultlen); (void)_7_resultlen; \
358*98921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MPI,"MPI error %d %s",(int)_7_errorcode,_7_errorstring); \
359ffc4695bSBarry Smith   } \
360ffc4695bSBarry Smith } while (0)
361064a246eSJacob Faibussowitsch #endif
36285614651SBarry Smith 
363fd705b32SMatthew Knepley #ifdef PETSC_CLANGUAGE_CXX
364fd705b32SMatthew Knepley 
365cc26af49SMatthew Knepley /*MC
366cc26af49SMatthew Knepley    CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
367cc26af49SMatthew Knepley 
368cc26af49SMatthew Knepley    Synopsis:
369aaa7dc30SBarry Smith    #include <petscsys.h>
3703af045c5SBarry Smith    void CHKERRXX(PetscErrorCode ierr)
371cc26af49SMatthew Knepley 
372eca87e8dSBarry Smith    Not Collective
373cc26af49SMatthew Knepley 
374cc26af49SMatthew Knepley    Input Parameters:
3753af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
376cc26af49SMatthew Knepley 
377cc26af49SMatthew Knepley   Level: beginner
378cc26af49SMatthew Knepley 
379cc26af49SMatthew Knepley    Notes:
380cc26af49SMatthew Knepley     Once the error handler throws a ??? exception.
381cc26af49SMatthew Knepley 
382cc26af49SMatthew Knepley     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
383cc26af49SMatthew Knepley     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.
384cc26af49SMatthew Knepley 
385*98921bdaSJacob Faibussowitsch .seealso: SETERRQ(), CHKERRQ(), SETERRABORT(), CHKERRABORT(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKMEMQ
386cc26af49SMatthew Knepley M*/
3873af045c5SBarry Smith #define CHKERRXX(ierr)  do {if (PetscUnlikely(ierr)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_IN_CXX,0);}} while (0)
388fd705b32SMatthew Knepley #endif
389fd705b32SMatthew Knepley 
3903f520e80SJunchao Zhang /*MC
3913f520e80SJunchao Zhang    CHKERRCXX - Checks C++ function calls and if they throw an exception, catch it and then return a PETSc error code
3923f520e80SJunchao Zhang 
3933f520e80SJunchao Zhang    Synopsis:
3943f520e80SJunchao Zhang    #include <petscsys.h>
3953f520e80SJunchao Zhang    CHKERRCXX(func);
3963f520e80SJunchao Zhang 
3973f520e80SJunchao Zhang    Not Collective
3983f520e80SJunchao Zhang 
3993f520e80SJunchao Zhang    Input Parameters:
4003f520e80SJunchao Zhang .  func - C++ function calls
4013f520e80SJunchao Zhang 
4023f520e80SJunchao Zhang   Level: beginner
4033f520e80SJunchao Zhang 
4043f520e80SJunchao Zhang   Notes:
4053f520e80SJunchao Zhang    For example,
4063f520e80SJunchao Zhang 
4073f520e80SJunchao Zhang $     void foo(int x) {throw std::runtime_error("error");}
4083f520e80SJunchao Zhang $     CHKERRCXX(foo(1));
4093f520e80SJunchao Zhang 
410*98921bdaSJacob Faibussowitsch .seealso: CHKERRXX(), SETERRQ(), CHKERRQ(), SETERRABORT(), CHKERRABORT(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKMEMQ
4113f520e80SJunchao Zhang M*/
412*98921bdaSJacob Faibussowitsch #define CHKERRCXX(func) do {try {func;} catch (const std::exception& e) { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"%s", e.what()); }} while (0)
4133f520e80SJunchao Zhang 
41430de9b25SBarry Smith /*MC
41530de9b25SBarry Smith    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
41630de9b25SBarry Smith 
41730de9b25SBarry Smith    Synopsis:
418aaa7dc30SBarry Smith    #include <petscsys.h>
41991d3bdf4SKris Buschelman    CHKMEMQ;
42030de9b25SBarry Smith 
421eca87e8dSBarry Smith    Not Collective
422eca87e8dSBarry Smith 
42330de9b25SBarry Smith   Level: beginner
42430de9b25SBarry Smith 
42530de9b25SBarry Smith    Notes:
426a17b96a8SKyle Gerard Felker     We highly recommend using Valgrind https://petsc.org/release/faq/#valgrind or for NVIDIA CUDA systems
4275ed36255SBarry Smith     https://docs.nvidia.com/cuda/cuda-memcheck/index.html for finding memory problems. The ``CHKMEMQ`` macro is useful on systems that
4285ed36255SBarry Smith     do not have valgrind, but is not as good as valgrind or cuda-memcheck.
4291957e957SBarry Smith 
43079dccf82SBarry Smith     Must run with the option -malloc_debug (-malloc_test in debug mode; or if PetscMallocSetDebug() called) to enable this option
43130de9b25SBarry Smith 
43230de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
43330de9b25SBarry Smith 
43430de9b25SBarry Smith     By defaults prints location where memory that is corrupted was allocated.
43530de9b25SBarry Smith 
436f621e05eSBarry Smith     Use CHKMEMA for functions that return void
437f621e05eSBarry Smith 
438*98921bdaSJacob Faibussowitsch .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), PetscMallocValidate()
43930de9b25SBarry Smith M*/
4406d210af2SJacob Faibussowitsch #if defined(PETSC_CLANG_STATIC_ANALYZER)
441064a246eSJacob Faibussowitsch #define CHKMEMQ
442064a246eSJacob Faibussowitsch #define CHKMEMA
4436d210af2SJacob Faibussowitsch #else
4446d210af2SJacob Faibussowitsch #define CHKMEMQ do {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__);CHKERRQ(_7_ierr);} while (0)
4456d210af2SJacob Faibussowitsch #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__)
446064a246eSJacob Faibussowitsch #endif
447668f157eSBarry Smith /*E
448668f157eSBarry Smith   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
449668f157eSBarry Smith 
450668f157eSBarry Smith   Level: advanced
451668f157eSBarry Smith 
452d736bfebSBarry Smith   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
453d736bfebSBarry Smith 
45495452b02SPatrick Sanan   Developer Notes:
45595452b02SPatrick Sanan     This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandler()
456668f157eSBarry Smith 
457d736bfebSBarry Smith .seealso: PetscError(), SETERRXX()
458668f157eSBarry Smith E*/
459d736bfebSBarry Smith typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
4604b209cf6SBarry Smith 
461eb9e708aSLisandro Dalcin #if defined(__clang_analyzer__)
462eb9e708aSLisandro Dalcin __attribute__((analyzer_noreturn))
463eb9e708aSLisandro Dalcin #endif
4643ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...) PETSC_ATTRIBUTE_FORMAT(7,8);
465eb9e708aSLisandro Dalcin 
466014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
467014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
468efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
469efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
470efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
471efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
472efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
473efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
474efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
475efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
476014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
4778d359177SBarry Smith PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int,void*);
478014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
479014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
48028559dc8SJed Brown PETSC_EXTERN PetscErrorCode PetscCheckPointerSetIntensity(PetscInt);
481c2a741eeSJunchao Zhang PETSC_EXTERN void PetscSignalSegvCheckPointerOrMpi(void);
482c2a741eeSJunchao Zhang PETSC_DEPRECATED_FUNCTION("Use PetscSignalSegvCheckPointerOrMpi() (since version 3.13)") PETSC_STATIC_INLINE void PetscSignalSegvCheckPointer(void) {PetscSignalSegvCheckPointerOrMpi();}
483329f5518SBarry Smith 
484639ff905SBarry Smith /*MC
485639ff905SBarry Smith     PetscErrorPrintf - Prints error messages.
486639ff905SBarry Smith 
487639ff905SBarry Smith    Synopsis:
488aaa7dc30SBarry Smith     #include <petscsys.h>
489639ff905SBarry Smith      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
490639ff905SBarry Smith 
491639ff905SBarry Smith     Not Collective
492639ff905SBarry Smith 
493f899ff85SJose E. Roman     Input Parameter:
494639ff905SBarry Smith .   format - the usual printf() format string
495639ff905SBarry Smith 
496639ff905SBarry Smith    Options Database Keys:
4971957e957SBarry Smith +    -error_output_stdout - cause error messages to be printed to stdout instead of the  (default) stderr
498e1bc860dSBarry Smith -    -error_output_none - to turn off all printing of error messages (does not change the way the error is handled.)
499639ff905SBarry Smith 
50095452b02SPatrick Sanan    Notes:
50195452b02SPatrick Sanan     Use
502639ff905SBarry Smith $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
503639ff905SBarry Smith $                        error is handled.) and
5041957e957SBarry Smith $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on or you can use your own function
505639ff905SBarry Smith 
506639ff905SBarry Smith           Use
507639ff905SBarry Smith      PETSC_STDERR = FILE* obtained from a file open etc. to have stderr printed to the file.
508639ff905SBarry Smith      PETSC_STDOUT = FILE* obtained from a file open etc. to have stdout printed to the file.
509639ff905SBarry Smith 
510639ff905SBarry Smith           Use
511639ff905SBarry Smith       PetscPushErrorHandler() to provide your own error handler that determines what kind of messages to print
512639ff905SBarry Smith 
513639ff905SBarry Smith    Level: developer
514639ff905SBarry Smith 
515639ff905SBarry Smith     Fortran Note:
516639ff905SBarry Smith     This routine is not supported in Fortran.
517639ff905SBarry Smith 
5182b60790dSJed Brown .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscHelpPrintf(), PetscPrintf(), PetscPushErrorHandler(), PetscVFPrintf(), PetscHelpPrintf()
519639ff905SBarry Smith M*/
5203ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[],...) PETSC_ATTRIBUTE_FORMAT(1,2);
521639ff905SBarry Smith 
522329f5518SBarry Smith typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
523014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
524014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
525014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
526aba4c478SBarry Smith PETSC_EXTERN PetscErrorCode PetscDetermineInitialFPTrap(void);
52754a8ef01SBarry Smith 
5283a40ed3dSBarry Smith /*
5293a40ed3dSBarry Smith       Allows the code to build a stack frame as it runs
5303a40ed3dSBarry Smith */
5313a40ed3dSBarry Smith 
53227104ee2SJacob Faibussowitsch #if defined(PETSC_USE_DEBUG)
53399cd645aSJed Brown #define PETSCSTACKSIZE 64
5343a40ed3dSBarry Smith typedef struct  {
5350e33f6ddSBarry Smith   const char *function[PETSCSTACKSIZE];
5360e33f6ddSBarry Smith   const char *file[PETSCSTACKSIZE];
537184914b5SBarry Smith         int  line[PETSCSTACKSIZE];
538362febeeSStefano Zampini         int  petscroutine[PETSCSTACKSIZE]; /* 0 external called from petsc, 1 petsc functions, 2 petsc user functions */
539184914b5SBarry Smith         int  currentsize;
540a2f94806SJed Brown         int  hotdepth;
541362febeeSStefano Zampini   PetscBool  check; /* runtime option to check for correct Push/Pop semantics at runtime */
5423a40ed3dSBarry Smith } PetscStack;
54327104ee2SJacob Faibussowitsch PETSC_EXTERN PetscStack petscstack;
54427104ee2SJacob Faibussowitsch #else
54527104ee2SJacob Faibussowitsch typedef struct {
54627104ee2SJacob Faibussowitsch   char Silence_empty_struct_has_size_0_in_C_size_1_in_Cpp;
54727104ee2SJacob Faibussowitsch } PetscStack;
54827104ee2SJacob Faibussowitsch #endif
5493a40ed3dSBarry Smith 
5505d12eec7SSatish Balay #if defined(PETSC_SERIALIZE_FUNCTIONS)
5515d12eec7SSatish Balay #include <petsc/private/petscfptimpl.h>
5525d12eec7SSatish Balay /*
5535d12eec7SSatish Balay    Registers the current function into the global function pointer to function name table
5545d12eec7SSatish Balay 
5555d12eec7SSatish Balay    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
5565d12eec7SSatish Balay */
5575d12eec7SSatish Balay #define PetscRegister__FUNCT__() do { \
5585d12eec7SSatish Balay   static PetscBool __chked = PETSC_FALSE; \
5595d12eec7SSatish Balay   if (!__chked) {\
5605d12eec7SSatish Balay   void *ptr; PetscDLSym(NULL,PETSC_FUNCTION_NAME,&ptr);\
5615d12eec7SSatish Balay   __chked = PETSC_TRUE;\
5625d12eec7SSatish Balay   }} while (0)
5635d12eec7SSatish Balay #else
5645d12eec7SSatish Balay #define PetscRegister__FUNCT__()
5655d12eec7SSatish Balay #endif
5665d12eec7SSatish Balay 
5676d210af2SJacob Faibussowitsch #if defined(PETSC_CLANG_STATIC_ANALYZER)
5686d210af2SJacob Faibussowitsch #define PetscStackPushNoCheck(funct,petsc_routine,hot)
5696d210af2SJacob Faibussowitsch #define PetscStackPopNoCheck
5706d210af2SJacob Faibussowitsch #define PetscStackClearTop
5716d210af2SJacob Faibussowitsch #define PetscFunctionBegin
5726d210af2SJacob Faibussowitsch #define PetscFunctionBeginUser
5736d210af2SJacob Faibussowitsch #define PetscFunctionBeginHot
5746d210af2SJacob Faibussowitsch #define PetscFunctionReturn(a)    return a
5756d210af2SJacob Faibussowitsch #define PetscFunctionReturnVoid() return
5766d210af2SJacob Faibussowitsch #define PetscStackPop
5776d210af2SJacob Faibussowitsch #define PetscStackPush(f)
5786d210af2SJacob Faibussowitsch #elif defined(PETSC_USE_DEBUG)
579441dd030SJed Brown /* Stack handling is based on the following two "NoCheck" macros.  These should only be called directly by other error
580441dd030SJed Brown  * handling macros.  We record the line of the call, which may or may not be the location of the definition.  But is at
581441dd030SJed Brown  * least more useful than "unknown" because it can distinguish multiple calls from the same function.
582441dd030SJed Brown  */
58327104ee2SJacob Faibussowitsch #define PetscStackPushNoCheck(funct,petsc_routine,hot) do {             \
584e04113cfSBarry Smith     PetscStackSAWsTakeAccess();                                         \
58527104ee2SJacob Faibussowitsch     if (petscstack.currentsize < PETSCSTACKSIZE) {                      \
58627104ee2SJacob Faibussowitsch       petscstack.function[petscstack.currentsize]     = funct;          \
58727104ee2SJacob Faibussowitsch       petscstack.file[petscstack.currentsize]         = __FILE__;       \
58827104ee2SJacob Faibussowitsch       petscstack.line[petscstack.currentsize]         = __LINE__;       \
58927104ee2SJacob Faibussowitsch       petscstack.petscroutine[petscstack.currentsize] = petsc_routine;  \
590441dd030SJed Brown     }                                                                   \
59127104ee2SJacob Faibussowitsch     ++petscstack.currentsize;                                           \
59227104ee2SJacob Faibussowitsch     petscstack.hotdepth += (hot || petscstack.hotdepth);                \
593e04113cfSBarry Smith     PetscStackSAWsGrantAccess();                                        \
594441dd030SJed Brown   } while (0)
595441dd030SJed Brown 
59627104ee2SJacob Faibussowitsch #define PetscStackPopNoCheck(funct)                    do {             \
597362febeeSStefano Zampini     PetscStackSAWsTakeAccess();                                         \
5981724198aSStefano Zampini     if (PetscUnlikely(petscstack.currentsize <= 0)) {                   \
5991724198aSStefano Zampini       if (PetscUnlikely(petscstack.check)) {                            \
60027104ee2SJacob Faibussowitsch         printf("Invalid stack size %d, pop %s\n",                       \
60127104ee2SJacob Faibussowitsch                petscstack.currentsize,funct);                           \
6021724198aSStefano Zampini       }                                                                 \
603362febeeSStefano Zampini     } else {                                                            \
60427104ee2SJacob Faibussowitsch       if (--petscstack.currentsize < PETSCSTACKSIZE) {                  \
60527104ee2SJacob Faibussowitsch         if (PetscUnlikely(                                              \
60627104ee2SJacob Faibussowitsch               petscstack.check                                &&        \
60727104ee2SJacob Faibussowitsch               petscstack.petscroutine[petscstack.currentsize] &&        \
60827104ee2SJacob Faibussowitsch               (petscstack.function[petscstack.currentsize]    !=        \
60927104ee2SJacob Faibussowitsch                (const char*)funct))) {                                  \
610ecbde57cSMatthew G. Knepley           /* We need this string comparison because "unknown" can be defined in different static strings: */ \
611ecbde57cSMatthew G. Knepley           PetscBool _cmpflg;                                            \
612ecbde57cSMatthew G. Knepley           const char *_funct = petscstack.function[petscstack.currentsize]; \
613ecbde57cSMatthew G. Knepley           PetscStrcmp(_funct,funct,&_cmpflg);                           \
614ecbde57cSMatthew G. Knepley           if (!_cmpflg)                                                 \
615ecbde57cSMatthew G. Knepley             printf("Invalid stack: push from %s, pop from %s\n", _funct,funct); \
616362febeeSStefano Zampini         }                                                               \
61727104ee2SJacob Faibussowitsch         petscstack.function[petscstack.currentsize] = PETSC_NULLPTR;    \
61827104ee2SJacob Faibussowitsch         petscstack.file[petscstack.currentsize]     = PETSC_NULLPTR;    \
61927104ee2SJacob Faibussowitsch         petscstack.line[petscstack.currentsize]     = 0;                \
62027104ee2SJacob Faibussowitsch         petscstack.petscroutine[petscstack.currentsize] = 0;            \
621362febeeSStefano Zampini       }                                                                 \
62227104ee2SJacob Faibussowitsch       petscstack.hotdepth = PetscMax(petscstack.hotdepth-1,0);          \
623362febeeSStefano Zampini     }                                                                   \
624362febeeSStefano Zampini     PetscStackSAWsGrantAccess();                                        \
625362febeeSStefano Zampini   } while (0)
626362febeeSStefano Zampini 
62727104ee2SJacob Faibussowitsch #define PetscStackClearTop                             do {             \
628e04113cfSBarry Smith     PetscStackSAWsTakeAccess();                                         \
6291724198aSStefano Zampini     if (petscstack.currentsize > 0 &&                                   \
6301724198aSStefano Zampini         --petscstack.currentsize < PETSCSTACKSIZE) {                    \
63127104ee2SJacob Faibussowitsch       petscstack.function[petscstack.currentsize]     = PETSC_NULLPTR;  \
63227104ee2SJacob Faibussowitsch       petscstack.file[petscstack.currentsize]         = PETSC_NULLPTR;  \
63327104ee2SJacob Faibussowitsch       petscstack.line[petscstack.currentsize]         = 0;              \
63427104ee2SJacob Faibussowitsch       petscstack.petscroutine[petscstack.currentsize] = 0;              \
635441dd030SJed Brown     }                                                                   \
63627104ee2SJacob Faibussowitsch     petscstack.hotdepth = PetscMax(petscstack.hotdepth-1,0);            \
637e04113cfSBarry Smith     PetscStackSAWsGrantAccess();                                        \
638441dd030SJed Brown   } while (0)
639441dd030SJed Brown 
64030de9b25SBarry Smith /*MC
6411957e957SBarry Smith    PetscFunctionBegin - First executable line of each PETSc function,  used for error handling. Final
6421957e957SBarry Smith       line of PETSc functions should be PetscFunctionReturn(0);
64330de9b25SBarry Smith 
64430de9b25SBarry Smith    Synopsis:
645aaa7dc30SBarry Smith    #include <petscsys.h>
64630de9b25SBarry Smith    void PetscFunctionBegin;
64730de9b25SBarry Smith 
648eca87e8dSBarry Smith    Not Collective
649eca87e8dSBarry Smith 
65030de9b25SBarry Smith    Usage:
65130de9b25SBarry Smith .vb
65230de9b25SBarry Smith      int something;
65330de9b25SBarry Smith 
65430de9b25SBarry Smith      PetscFunctionBegin;
65530de9b25SBarry Smith .ve
65630de9b25SBarry Smith 
65730de9b25SBarry Smith    Notes:
6581957e957SBarry Smith      Use PetscFunctionBeginUser for application codes.
6591957e957SBarry Smith 
66030de9b25SBarry Smith      Not available in Fortran
66130de9b25SBarry Smith 
66230de9b25SBarry Smith    Level: developer
66330de9b25SBarry Smith 
6641957e957SBarry Smith .seealso: PetscFunctionReturn(), PetscFunctionBeginHot(), PetscFunctionBeginUser()
66530de9b25SBarry Smith 
66630de9b25SBarry Smith M*/
667441dd030SJed Brown #define PetscFunctionBegin do {                               \
668362febeeSStefano Zampini     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,1,PETSC_FALSE); \
669a2f94806SJed Brown     PetscRegister__FUNCT__();                                 \
670a2f94806SJed Brown   } while (0)
671a2f94806SJed Brown 
672a2f94806SJed Brown /*MC
673a2f94806SJed Brown    PetscFunctionBeginHot - Substitute for PetscFunctionBegin to be used in functions that are called in
674a2f94806SJed Brown    performance-critical circumstances.  Use of this function allows for lighter profiling by default.
675a2f94806SJed Brown 
676a2f94806SJed Brown    Synopsis:
677aaa7dc30SBarry Smith    #include <petscsys.h>
678a2f94806SJed Brown    void PetscFunctionBeginHot;
679a2f94806SJed Brown 
680a2f94806SJed Brown    Not Collective
681a2f94806SJed Brown 
682a2f94806SJed Brown    Usage:
683a2f94806SJed Brown .vb
684a2f94806SJed Brown      int something;
685a2f94806SJed Brown 
686a2f94806SJed Brown      PetscFunctionBeginHot;
687a2f94806SJed Brown .ve
688a2f94806SJed Brown 
689a2f94806SJed Brown    Notes:
690a2f94806SJed Brown      Not available in Fortran
691a2f94806SJed Brown 
692a2f94806SJed Brown    Level: developer
693a2f94806SJed Brown 
694a2f94806SJed Brown .seealso: PetscFunctionBegin, PetscFunctionReturn()
695a2f94806SJed Brown 
696a2f94806SJed Brown M*/
697a2f94806SJed Brown #define PetscFunctionBeginHot do {                           \
698362febeeSStefano Zampini     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,1,PETSC_TRUE); \
6992d53ad75SBarry Smith     PetscRegister__FUNCT__();                                \
70053c77d0aSJed Brown   } while (0)
70153c77d0aSJed Brown 
702a8d2bbe5SBarry Smith /*MC
703a8d2bbe5SBarry Smith    PetscFunctionBeginUser - First executable line of user provided PETSc routine
704a8d2bbe5SBarry Smith 
705a8d2bbe5SBarry Smith    Synopsis:
706aaa7dc30SBarry Smith    #include <petscsys.h>
707a8d2bbe5SBarry Smith    void PetscFunctionBeginUser;
708a8d2bbe5SBarry Smith 
709a8d2bbe5SBarry Smith    Not Collective
710a8d2bbe5SBarry Smith 
711a8d2bbe5SBarry Smith    Usage:
712a8d2bbe5SBarry Smith .vb
713a8d2bbe5SBarry Smith      int something;
714a8d2bbe5SBarry Smith 
715ac285190SBarry Smith      PetscFunctionBeginUser;
716a8d2bbe5SBarry Smith .ve
717a8d2bbe5SBarry Smith 
718a8d2bbe5SBarry Smith    Notes:
7191957e957SBarry Smith       Final line of PETSc functions should be PetscFunctionReturn(0) except for main().
7201957e957SBarry Smith 
721a8d2bbe5SBarry Smith       Not available in Fortran
722a8d2bbe5SBarry Smith 
723ac285190SBarry Smith       This is identical to PetscFunctionBegin except it labels the routine as a user
724ac285190SBarry Smith       routine instead of as a PETSc library routine.
725ac285190SBarry Smith 
726a2f94806SJed Brown    Level: intermediate
727a8d2bbe5SBarry Smith 
728a2f94806SJed Brown .seealso: PetscFunctionReturn(), PetscFunctionBegin, PetscFunctionBeginHot
729a8d2bbe5SBarry Smith 
730a8d2bbe5SBarry Smith M*/
73127104ee2SJacob Faibussowitsch #define PetscFunctionBeginUser do {                           \
732362febeeSStefano Zampini     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,2,PETSC_FALSE); \
733a8d2bbe5SBarry Smith     PetscRegister__FUNCT__();                                 \
734a8d2bbe5SBarry Smith   } while (0)
735a8d2bbe5SBarry Smith 
73627104ee2SJacob Faibussowitsch #define PetscStackPush(n)       do {        \
737362febeeSStefano Zampini     PetscStackPushNoCheck(n,0,PETSC_FALSE); \
73815681b3cSBarry Smith     CHKMEMQ;                                \
73915681b3cSBarry Smith   } while (0)
7403a40ed3dSBarry Smith 
74127104ee2SJacob Faibussowitsch #define PetscStackPop           do {             \
742441dd030SJed Brown       CHKMEMQ;                                   \
743362febeeSStefano Zampini       PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
74415681b3cSBarry Smith     } while (0)
745d64ed03dSBarry Smith 
74630de9b25SBarry Smith /*MC
74730de9b25SBarry Smith    PetscFunctionReturn - Last executable line of each PETSc function
74830de9b25SBarry Smith         used for error handling. Replaces return()
74930de9b25SBarry Smith 
75030de9b25SBarry Smith    Synopsis:
751aaa7dc30SBarry Smith    #include <petscsys.h>
75230de9b25SBarry Smith    void PetscFunctionReturn(0);
75330de9b25SBarry Smith 
754eca87e8dSBarry Smith    Not Collective
755eca87e8dSBarry Smith 
75630de9b25SBarry Smith    Usage:
75730de9b25SBarry Smith .vb
75830de9b25SBarry Smith     ....
75930de9b25SBarry Smith      PetscFunctionReturn(0);
76030de9b25SBarry Smith    }
76130de9b25SBarry Smith .ve
76230de9b25SBarry Smith 
76330de9b25SBarry Smith    Notes:
76430de9b25SBarry Smith      Not available in Fortran
76530de9b25SBarry Smith 
76630de9b25SBarry Smith    Level: developer
76730de9b25SBarry Smith 
76830de9b25SBarry Smith .seealso: PetscFunctionBegin()
76930de9b25SBarry Smith 
77030de9b25SBarry Smith M*/
77127104ee2SJacob Faibussowitsch #define PetscFunctionReturn(a)    do {          \
772362febeeSStefano Zampini     PetscStackPopNoCheck(PETSC_FUNCTION_NAME);  \
77327104ee2SJacob Faibussowitsch     return a;                                   \
77427104ee2SJacob Faibussowitsch   } while (0)
775d64ed03dSBarry Smith 
77627104ee2SJacob Faibussowitsch #define PetscFunctionReturnVoid() do {          \
777362febeeSStefano Zampini     PetscStackPopNoCheck(PETSC_FUNCTION_NAME);  \
77827104ee2SJacob Faibussowitsch     return;                                     \
77927104ee2SJacob Faibussowitsch   } while (0)
78027104ee2SJacob Faibussowitsch #else /* PETSC_USE_DEBUG */
78127104ee2SJacob Faibussowitsch #define PetscStackPushNoCheck(funct,petsc_routine,hot)
78227104ee2SJacob Faibussowitsch #define PetscStackPopNoCheck
78327104ee2SJacob Faibussowitsch #define PetscStackClearTop
7843a40ed3dSBarry Smith #define PetscFunctionBegin
7850bdf7c52SPeter Brune #define PetscFunctionBeginUser
786a2f94806SJed Brown #define PetscFunctionBeginHot
78727104ee2SJacob Faibussowitsch #define PetscFunctionReturn(a)    return a
7885665465eSBarry Smith #define PetscFunctionReturnVoid() return
789812af9f3SBarry Smith #define PetscStackPop             CHKMEMQ
790812af9f3SBarry Smith #define PetscStackPush(f)         CHKMEMQ
79127104ee2SJacob Faibussowitsch #endif /* PETSC_USE_DEBUG */
7923a40ed3dSBarry Smith 
7936d210af2SJacob Faibussowitsch #if defined(PETSC_CLANG_STATIC_ANALYZER)
7946d210af2SJacob Faibussowitsch #define PetscStackCall(name,routine)
7956d210af2SJacob Faibussowitsch #define PetscStackCallStandard(name,routine)
7966d210af2SJacob Faibussowitsch #else
797eb6b5d47SBarry Smith /*
798eb6b5d47SBarry Smith     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
799eb6b5d47SBarry Smith 
800eb6b5d47SBarry Smith    Input Parameters:
801eb6b5d47SBarry Smith +   name - string that gives the name of the function being called
802fd3f9acdSBarry Smith -   routine - actual call to the routine, including ierr = and CHKERRQ(ierr);
803fd3f9acdSBarry Smith 
804dbf62e16SBarry Smith    Note: Often one should use PetscStackCallStandard() instead. This routine is intended for external library routines that DO NOT return error codes
805eb6b5d47SBarry Smith 
806eb6b5d47SBarry Smith    Developer Note: this is so that when a user or external library routine results in a crash or corrupts memory, they get blamed instead of PETSc.
807eb6b5d47SBarry Smith 
808eb6b5d47SBarry Smith */
80930ecc5abSKarl Rupp #define PetscStackCall(name,routine) do { PetscStackPush(name);routine;PetscStackPop; } while (0)
810eb6b5d47SBarry Smith 
811fd3f9acdSBarry Smith /*
812fd3f9acdSBarry Smith     PetscStackCallStandard - Calls an external library routine after pushing the name of the routine on the stack.
813fd3f9acdSBarry Smith 
814fd3f9acdSBarry Smith    Input Parameters:
815fd3f9acdSBarry Smith +   func-  name of the routine
816fd3f9acdSBarry Smith -   args - arguments to the routine surrounded by ()
817fd3f9acdSBarry Smith 
81895452b02SPatrick Sanan    Notes:
81995452b02SPatrick Sanan     This is intended for external package routines that return error codes. Use PetscStackCall() for those that do not.
820dbf62e16SBarry Smith 
821dbf62e16SBarry Smith    Developer Note: this is so that when an external packge routine results in a crash or corrupts memory, they get blamed instead of PETSc.
822fd3f9acdSBarry Smith 
823fd3f9acdSBarry Smith */
824fd3f9acdSBarry Smith #define PetscStackCallStandard(func,args) do {                                                            \
8251d4906efSStefano Zampini     PetscErrorCode __ierr;                                                                                \
8261d4906efSStefano Zampini     PetscStackPush(#func);                                                                                \
8271d4906efSStefano Zampini     __ierr = func args;                                                                                   \
8281d4906efSStefano Zampini     PetscStackPop;                                                                                        \
829*98921bdaSJacob Faibussowitsch     if (__ierr) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s(): error code %d",#func,(int)__ierr); \
830fd3f9acdSBarry Smith   } while (0)
8316d210af2SJacob Faibussowitsch #endif /* PETSC_CLANG_STATIC_ANALYZER */
83206d1fe2cSBarry Smith 
83306d1fe2cSBarry Smith #endif
834