xref: /petsc/include/petscerror.h (revision 3f520e800d194d104ad17fb175c529d6bcfaa0f9)
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 
754a8ef01SBarry Smith /*
8329ffe3dSLois Curfman McInnes      These are the generic error codes. These error codes are used
9e2d1d2b7SBarry Smith      many different places in the PETSc source code. The string versions are
100e5e90baSSatish Balay      at src/sys/error/err.c any changes here must also be made there
11e951c290SBarry Smith      These are also define in src/sys/f90-mod/petscerror.h any CHANGES here
120f9cf654SBarry Smith      must be also made there.
1345d48df9SBarry Smith 
1454a8ef01SBarry Smith */
152a6744ebSBarry Smith #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
162a6744ebSBarry Smith 
1745d48df9SBarry Smith #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
1847794344SBarry Smith #define PETSC_ERR_SUP              56   /* no support for requested operation */
19e2d1d2b7SBarry Smith #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
20e2d1d2b7SBarry Smith #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
2145d48df9SBarry Smith #define PETSC_ERR_SIG              59   /* signal received */
22f1caa5a4SBarry Smith #define PETSC_ERR_FP               72   /* floating point exception */
23a8c6a408SBarry Smith #define PETSC_ERR_COR              74   /* corrupted PETSc object */
24a8c6a408SBarry Smith #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
25329ffe3dSLois Curfman McInnes #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
26329ffe3dSLois Curfman McInnes #define PETSC_ERR_MEMC             78   /* memory corruption */
27b3cc6726SBarry Smith #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
281302d50aSBarry Smith #define PETSC_ERR_USER             83   /* user has not provided needed function */
294e2ffeddSBarry Smith #define PETSC_ERR_SYS              88   /* error in system call */
30a8b45ee7SBarry Smith #define PETSC_ERR_POINTER          70   /* pointer does not point to valid address */
313d96e996SBarry Smith #define PETSC_ERR_MPI_LIB_INCOMP   87   /* MPI library at runtime is not compatible with MPI user compiled with */
3245d48df9SBarry Smith 
3345d48df9SBarry Smith #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
3445d48df9SBarry Smith #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
35a8c6a408SBarry Smith #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
3645d48df9SBarry Smith #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
3745d48df9SBarry Smith #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
384f227f7cSBarry Smith #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
394f227f7cSBarry Smith #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
406831982aSBarry Smith #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
41d252947aSBarry Smith #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
428cda6cd7SBarry Smith #define PETSC_ERR_ARG_TYPENOTSET   89   /* the type of the object has not yet been set */
43a8c6a408SBarry Smith #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
444482741eSBarry Smith #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
45958c9bccSBarry Smith #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
464f227f7cSBarry Smith 
474f227f7cSBarry Smith #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
484f227f7cSBarry Smith #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
494f227f7cSBarry Smith #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
50a8c6a408SBarry Smith #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
5145d48df9SBarry Smith 
52329ffe3dSLois Curfman McInnes #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
539e3b2f23SBarry Smith #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
5454a8ef01SBarry Smith 
5568e69593SBarry Smith #define PETSC_ERR_INT_OVERFLOW     84
563855c12bSBarry Smith 
57bf3909cdSBarry Smith #define PETSC_ERR_FLOP_COUNT       90
58e113a28aSBarry Smith #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
5992e8f287SBarry Smith #define PETSC_ERR_MISSING_FACTOR   92  /* MatGetFactor() failed */
60691b26d3SBarry Smith #define PETSC_ERR_OPT_OVERWRITE    93  /* attempted to over write options which should not be changed */
61691b26d3SBarry Smith #define PETSC_ERR_WRONG_MPI_SIZE   94  /* example/application run with number of MPI ranks it does not support */
62691b26d3SBarry Smith #define PETSC_ERR_USER_INPUT       95  /* missing or incorrect user input */
63589f383fSBarry Smith #define PETSC_ERR_GPU_RESOURCE     96  /* unable to load a GPU resource, for example cuBLAS */
64589f383fSBarry 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 */
65ffc4695bSBarry Smith #define PETSC_ERR_MPI              98  /* general MPI error */
66ffc4695bSBarry Smith #define PETSC_ERR_MAX_VALUE        99  /* this is always the one more than the largest error code */
67b9eb5ee8SHong Zhang 
6859aaf355SLisandro Dalcin #define PetscStringizeArg(a) #a
6959aaf355SLisandro Dalcin #define PetscStringize(a) PetscStringizeArg(a)
70330cf3c9SBarry Smith 
7130de9b25SBarry Smith /*MC
721957e957SBarry Smith    SETERRQ - Macro to be called when an error has been detected,
7330de9b25SBarry Smith 
7430de9b25SBarry Smith    Synopsis:
75aaa7dc30SBarry Smith    #include <petscsys.h>
763af045c5SBarry Smith    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode ierr,char *message)
7730de9b25SBarry Smith 
78d083f849SBarry Smith    Collective
7930de9b25SBarry Smith 
8030de9b25SBarry Smith    Input Parameters:
813af045c5SBarry Smith +  comm - A communicator, use PETSC_COMM_SELF unless you know all ranks of another communicator will detect the error
823af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
8330de9b25SBarry Smith -  message - error message
8430de9b25SBarry Smith 
8530de9b25SBarry Smith   Level: beginner
8630de9b25SBarry Smith 
8730de9b25SBarry Smith    Notes:
8830de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
8930de9b25SBarry Smith 
9030de9b25SBarry Smith     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
9130de9b25SBarry Smith 
9230de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
9330de9b25SBarry Smith 
943b1008b8SBarry Smith    Fortran Notes:
953b1008b8SBarry Smith       SETERRQ() may be called from Fortran subroutines but SETERRA() must be called from the
963b1008b8SBarry Smith       Fortran main program.
973b1008b8SBarry Smith 
98888a1fb3SBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(), CHKERRMPI()
9930de9b25SBarry Smith M*/
1003af045c5SBarry Smith #define SETERRQ(comm,ierr,s) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s)
101986eef2eSBarry Smith 
102ffc4695bSBarry Smith /*
103ffc4695bSBarry Smith     Returned from PETSc functions that are called from MPI, such as related to attributes
104ffc4695bSBarry Smith       Do not confuse PETSC_MPI_ERROR_CODE and PETSC_ERR_MPI, the first is registered with MPI and returned to MPI as
105888a1fb3SBarry 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.
106ffc4695bSBarry Smith */
107ffc4695bSBarry Smith PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CLASS;
108ffc4695bSBarry Smith PETSC_EXTERN PetscMPIInt PETSC_MPI_ERROR_CODE;
109ffc4695bSBarry Smith 
110986eef2eSBarry Smith /*MC
111986eef2eSBarry Smith    SETERRMPI - Macro to be called when an error has been detected within an MPI callback function
112986eef2eSBarry Smith 
113986eef2eSBarry Smith    Synopsis:
114986eef2eSBarry Smith    #include <petscsys.h>
115986eef2eSBarry Smith    PetscErrorCode SETERRMPI(MPI_Comm comm,PetscErrorCode ierr,char *message)
116986eef2eSBarry Smith 
117d083f849SBarry Smith    Collective
118986eef2eSBarry Smith 
119986eef2eSBarry Smith    Input Parameters:
120986eef2eSBarry Smith +  comm - A communicator, use PETSC_COMM_SELF unless you know all ranks of another communicator will detect the error
121986eef2eSBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
122986eef2eSBarry Smith -  message - error message
123986eef2eSBarry Smith 
124986eef2eSBarry Smith   Level: developer
125986eef2eSBarry Smith 
126986eef2eSBarry Smith    Notes:
127888a1fb3SBarry 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
128986eef2eSBarry Smith     which is registered with MPI_Add_error_code() when PETSc is initialized.
129986eef2eSBarry Smith 
130986eef2eSBarry Smith .seealso: SETERRQ(), CHKERRQ(), CHKERRMPI(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
131986eef2eSBarry Smith M*/
13212801b39SBarry Smith #define SETERRMPI(comm,ierr,s) return (PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s),PETSC_MPI_ERROR_CODE)
13330de9b25SBarry Smith 
13430de9b25SBarry Smith /*MC
13530de9b25SBarry Smith    SETERRQ1 - Macro that is called when an error has been detected,
13630de9b25SBarry Smith 
13730de9b25SBarry Smith    Synopsis:
138aaa7dc30SBarry Smith    #include <petscsys.h>
1393af045c5SBarry Smith    PetscErrorCode SETERRQ1(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg)
14030de9b25SBarry Smith 
141d083f849SBarry Smith    Collective
14230de9b25SBarry Smith 
14330de9b25SBarry Smith    Input Parameters:
144fa190f98SMatthew G. Knepley +  comm - A communicator, so that the error can be collective
1453af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
14630de9b25SBarry Smith .  message - error message in the printf format
14730de9b25SBarry Smith -  arg - argument (for example an integer, string or double)
14830de9b25SBarry Smith 
14930de9b25SBarry Smith   Level: beginner
15030de9b25SBarry Smith 
15130de9b25SBarry Smith    Notes:
15230de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
15330de9b25SBarry Smith 
15430de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
15530de9b25SBarry Smith 
15691d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
15730de9b25SBarry Smith M*/
1583af045c5SBarry Smith #define SETERRQ1(comm,ierr,s,a1) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1)
15930de9b25SBarry Smith 
16030de9b25SBarry Smith /*MC
16130de9b25SBarry Smith    SETERRQ2 - Macro that is called when an error has been detected,
16230de9b25SBarry Smith 
16330de9b25SBarry Smith    Synopsis:
164aaa7dc30SBarry Smith    #include <petscsys.h>
1653af045c5SBarry Smith    PetscErrorCode SETERRQ2(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2)
16630de9b25SBarry Smith 
167d083f849SBarry Smith    Collective
16830de9b25SBarry Smith 
16930de9b25SBarry Smith    Input Parameters:
170fa190f98SMatthew G. Knepley +  comm - A communicator, so that the error can be collective
1713af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
17230de9b25SBarry Smith .  message - error message in the printf format
17330de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
17430de9b25SBarry Smith -  arg2 - argument (for example an integer, string or double)
17530de9b25SBarry Smith 
17630de9b25SBarry Smith   Level: beginner
17730de9b25SBarry Smith 
17830de9b25SBarry Smith    Notes:
17930de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
18030de9b25SBarry Smith 
18130de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
18230de9b25SBarry Smith 
1836024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
18430de9b25SBarry Smith M*/
1853af045c5SBarry Smith #define SETERRQ2(comm,ierr,s,a1,a2) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2)
18630de9b25SBarry Smith 
18730de9b25SBarry Smith /*MC
18830de9b25SBarry Smith    SETERRQ3 - Macro that is called when an error has been detected,
18930de9b25SBarry Smith 
19030de9b25SBarry Smith    Synopsis:
191aaa7dc30SBarry Smith    #include <petscsys.h>
1923af045c5SBarry Smith    PetscErrorCode SETERRQ3(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3)
19330de9b25SBarry Smith 
194d083f849SBarry Smith    Collective
19530de9b25SBarry Smith 
19630de9b25SBarry Smith    Input Parameters:
197fa190f98SMatthew G. Knepley +  comm - A communicator, so that the error can be collective
1983af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
19930de9b25SBarry Smith .  message - error message in the printf format
20030de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
20130de9b25SBarry Smith .  arg2 - argument (for example an integer, string or double)
20230de9b25SBarry Smith -  arg3 - argument (for example an integer, string or double)
20330de9b25SBarry Smith 
20430de9b25SBarry Smith   Level: beginner
20530de9b25SBarry Smith 
20630de9b25SBarry Smith    Notes:
20730de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
20830de9b25SBarry Smith 
209f621e05eSBarry Smith     There are also versions for 4, 5, 6 and 7 arguments.
210f621e05eSBarry Smith 
21130de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
21230de9b25SBarry Smith 
2136024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
21430de9b25SBarry Smith M*/
2153af045c5SBarry Smith #define SETERRQ3(comm,ierr,s,a1,a2,a3) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3)
21630de9b25SBarry Smith 
217fa190f98SMatthew G. Knepley /*MC
218fa190f98SMatthew G. Knepley    SETERRQ4 - Macro that is called when an error has been detected,
219fa190f98SMatthew G. Knepley 
220fa190f98SMatthew G. Knepley    Synopsis:
221fa190f98SMatthew G. Knepley    #include <petscsys.h>
22231e9e36bSSatish Balay    PetscErrorCode SETERRQ4(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3,arg4)
223fa190f98SMatthew G. Knepley 
224d083f849SBarry Smith    Collective
225fa190f98SMatthew G. Knepley 
226fa190f98SMatthew G. Knepley    Input Parameters:
227fa190f98SMatthew G. Knepley +  comm - A communicator, so that the error can be collective
2283af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
229fa190f98SMatthew G. Knepley .  message - error message in the printf format
230fa190f98SMatthew G. Knepley .  arg1 - argument (for example an integer, string or double)
231fa190f98SMatthew G. Knepley .  arg2 - argument (for example an integer, string or double)
232fa190f98SMatthew G. Knepley .  arg3 - argument (for example an integer, string or double)
233fa190f98SMatthew G. Knepley -  arg4 - argument (for example an integer, string or double)
234fa190f98SMatthew G. Knepley 
235fa190f98SMatthew G. Knepley   Level: beginner
236fa190f98SMatthew G. Knepley 
237fa190f98SMatthew G. Knepley    Notes:
238fa190f98SMatthew G. Knepley     Once the error handler is called the calling function is then returned from with the given error code.
239fa190f98SMatthew G. Knepley 
240fa190f98SMatthew G. Knepley     There are also versions for 4, 5, 6 and 7 arguments.
241fa190f98SMatthew G. Knepley 
242fa190f98SMatthew G. Knepley    Experienced users can set the error handler with PetscPushErrorHandler().
243fa190f98SMatthew G. Knepley 
244fa190f98SMatthew G. Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
245fa190f98SMatthew G. Knepley M*/
2463af045c5SBarry Smith #define SETERRQ4(comm,ierr,s,a1,a2,a3,a4) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4)
247fa190f98SMatthew G. Knepley 
248fa190f98SMatthew G. Knepley /*MC
249fa190f98SMatthew G. Knepley    SETERRQ5 - Macro that is called when an error has been detected,
250fa190f98SMatthew G. Knepley 
251fa190f98SMatthew G. Knepley    Synopsis:
252fa190f98SMatthew G. Knepley    #include <petscsys.h>
25331e9e36bSSatish Balay    PetscErrorCode SETERRQ5(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3,arg4,arg5)
254fa190f98SMatthew G. Knepley 
255d083f849SBarry Smith    Collective
256fa190f98SMatthew G. Knepley 
257fa190f98SMatthew G. Knepley    Input Parameters:
258fa190f98SMatthew G. Knepley +  comm - A communicator, so that the error can be collective
2593af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
260fa190f98SMatthew G. Knepley .  message - error message in the printf format
261fa190f98SMatthew G. Knepley .  arg1 - argument (for example an integer, string or double)
262fa190f98SMatthew G. Knepley .  arg2 - argument (for example an integer, string or double)
263fa190f98SMatthew G. Knepley .  arg3 - argument (for example an integer, string or double)
264fa190f98SMatthew G. Knepley .  arg4 - argument (for example an integer, string or double)
265fa190f98SMatthew G. Knepley -  arg5 - argument (for example an integer, string or double)
266fa190f98SMatthew G. Knepley 
267fa190f98SMatthew G. Knepley   Level: beginner
268fa190f98SMatthew G. Knepley 
269fa190f98SMatthew G. Knepley    Notes:
270fa190f98SMatthew G. Knepley     Once the error handler is called the calling function is then returned from with the given error code.
271fa190f98SMatthew G. Knepley 
272fa190f98SMatthew G. Knepley     There are also versions for 4, 5, 6 and 7 arguments.
273fa190f98SMatthew G. Knepley 
274fa190f98SMatthew G. Knepley    Experienced users can set the error handler with PetscPushErrorHandler().
275fa190f98SMatthew G. Knepley 
276fa190f98SMatthew G. Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
277fa190f98SMatthew G. Knepley M*/
2783af045c5SBarry Smith #define SETERRQ5(comm,ierr,s,a1,a2,a3,a4,a5) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5)
279fa190f98SMatthew G. Knepley 
280fa190f98SMatthew G. Knepley /*MC
281fa190f98SMatthew G. Knepley    SETERRQ6 - Macro that is called when an error has been detected,
282fa190f98SMatthew G. Knepley 
283fa190f98SMatthew G. Knepley    Synopsis:
284fa190f98SMatthew G. Knepley    #include <petscsys.h>
28531e9e36bSSatish Balay    PetscErrorCode SETERRQ6(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3,arg4,arg5,arg6)
286fa190f98SMatthew G. Knepley 
287d083f849SBarry Smith    Collective
288fa190f98SMatthew G. Knepley 
289fa190f98SMatthew G. Knepley    Input Parameters:
290fa190f98SMatthew G. Knepley +  comm - A communicator, so that the error can be collective
2913af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
292fa190f98SMatthew G. Knepley .  message - error message in the printf format
293fa190f98SMatthew G. Knepley .  arg1 - argument (for example an integer, string or double)
294fa190f98SMatthew G. Knepley .  arg2 - argument (for example an integer, string or double)
295fa190f98SMatthew G. Knepley .  arg3 - argument (for example an integer, string or double)
296fa190f98SMatthew G. Knepley .  arg4 - argument (for example an integer, string or double)
297fa190f98SMatthew G. Knepley .  arg5 - argument (for example an integer, string or double)
298fa190f98SMatthew G. Knepley -  arg6 - argument (for example an integer, string or double)
299fa190f98SMatthew G. Knepley 
300fa190f98SMatthew G. Knepley   Level: beginner
301fa190f98SMatthew G. Knepley 
302fa190f98SMatthew G. Knepley    Notes:
303fa190f98SMatthew G. Knepley     Once the error handler is called the calling function is then returned from with the given error code.
304fa190f98SMatthew G. Knepley 
305fa190f98SMatthew G. Knepley     There are also versions for 4, 5, 6 and 7 arguments.
306fa190f98SMatthew G. Knepley 
307fa190f98SMatthew G. Knepley    Experienced users can set the error handler with PetscPushErrorHandler().
308fa190f98SMatthew G. Knepley 
309fa190f98SMatthew G. Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
310fa190f98SMatthew G. Knepley M*/
3113af045c5SBarry Smith #define SETERRQ6(comm,ierr,s,a1,a2,a3,a4,a5,a6) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6)
312fa190f98SMatthew G. Knepley 
313fa190f98SMatthew G. Knepley /*MC
314fa190f98SMatthew G. Knepley    SETERRQ7 - Macro that is called when an error has been detected,
315fa190f98SMatthew G. Knepley 
316fa190f98SMatthew G. Knepley    Synopsis:
317fa190f98SMatthew G. Knepley    #include <petscsys.h>
31831e9e36bSSatish Balay    PetscErrorCode SETERRQ7(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3,arg4,arg5,arg6,arg7)
319fa190f98SMatthew G. Knepley 
320d083f849SBarry Smith    Collective
321fa190f98SMatthew G. Knepley 
322fa190f98SMatthew G. Knepley    Input Parameters:
323fa190f98SMatthew G. Knepley +  comm - A communicator, so that the error can be collective
3243af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
325fa190f98SMatthew G. Knepley .  message - error message in the printf format
326fa190f98SMatthew G. Knepley .  arg1 - argument (for example an integer, string or double)
327fa190f98SMatthew G. Knepley .  arg2 - argument (for example an integer, string or double)
328fa190f98SMatthew G. Knepley .  arg3 - argument (for example an integer, string or double)
329fa190f98SMatthew G. Knepley .  arg4 - argument (for example an integer, string or double)
330fa190f98SMatthew G. Knepley .  arg5 - argument (for example an integer, string or double)
331fa190f98SMatthew G. Knepley .  arg6 - argument (for example an integer, string or double)
332fa190f98SMatthew G. Knepley -  arg7 - argument (for example an integer, string or double)
333fa190f98SMatthew G. Knepley 
334fa190f98SMatthew G. Knepley   Level: beginner
335fa190f98SMatthew G. Knepley 
336fa190f98SMatthew G. Knepley    Notes:
337fa190f98SMatthew G. Knepley     Once the error handler is called the calling function is then returned from with the given error code.
338fa190f98SMatthew G. Knepley 
339fa190f98SMatthew G. Knepley     There are also versions for 4, 5, 6 and 7 arguments.
340fa190f98SMatthew G. Knepley 
341fa190f98SMatthew G. Knepley    Experienced users can set the error handler with PetscPushErrorHandler().
342fa190f98SMatthew G. Knepley 
343fa190f98SMatthew G. Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
344fa190f98SMatthew G. Knepley M*/
3453af045c5SBarry Smith #define SETERRQ7(comm,ierr,s,a1,a2,a3,a4,a5,a6,a7) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6,a7)
346fa190f98SMatthew G. Knepley 
347fa190f98SMatthew G. Knepley /*MC
348fa190f98SMatthew G. Knepley    SETERRQ8 - Macro that is called when an error has been detected,
349fa190f98SMatthew G. Knepley 
350fa190f98SMatthew G. Knepley    Synopsis:
351fa190f98SMatthew G. Knepley    #include <petscsys.h>
35231e9e36bSSatish Balay    PetscErrorCode SETERRQ8(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8)
353fa190f98SMatthew G. Knepley 
354d083f849SBarry Smith    Collective
355fa190f98SMatthew G. Knepley 
356fa190f98SMatthew G. Knepley    Input Parameters:
357fa190f98SMatthew G. Knepley +  comm - A communicator, so that the error can be collective
3583af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
359fa190f98SMatthew G. Knepley .  message - error message in the printf format
360fa190f98SMatthew G. Knepley .  arg1 - argument (for example an integer, string or double)
361fa190f98SMatthew G. Knepley .  arg2 - argument (for example an integer, string or double)
362fa190f98SMatthew G. Knepley .  arg3 - argument (for example an integer, string or double)
363fa190f98SMatthew G. Knepley .  arg4 - argument (for example an integer, string or double)
364fa190f98SMatthew G. Knepley .  arg5 - argument (for example an integer, string or double)
365fa190f98SMatthew G. Knepley .  arg6 - argument (for example an integer, string or double)
366fa190f98SMatthew G. Knepley .  arg7 - argument (for example an integer, string or double)
367fa190f98SMatthew G. Knepley -  arg8 - argument (for example an integer, string or double)
368fa190f98SMatthew G. Knepley 
369fa190f98SMatthew G. Knepley   Level: beginner
370fa190f98SMatthew G. Knepley 
371fa190f98SMatthew G. Knepley    Notes:
372fa190f98SMatthew G. Knepley     Once the error handler is called the calling function is then returned from with the given error code.
373fa190f98SMatthew G. Knepley 
374fa190f98SMatthew G. Knepley     There are also versions for 4, 5, 6 and 7 arguments.
375fa190f98SMatthew G. Knepley 
376fa190f98SMatthew G. Knepley    Experienced users can set the error handler with PetscPushErrorHandler().
377fa190f98SMatthew G. Knepley 
378fa190f98SMatthew G. Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
379fa190f98SMatthew G. Knepley M*/
3803af045c5SBarry Smith #define SETERRQ8(comm,ierr,s,a1,a2,a3,a4,a5,a6,a7,a8) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6,a7,a8)
381fa190f98SMatthew G. Knepley 
382fa190f98SMatthew G. Knepley /*MC
383ee8199e6SMatthew G. Knepley    SETERRQ9 - Macro that is called when an error has been detected,
384ee8199e6SMatthew G. Knepley 
385ee8199e6SMatthew G. Knepley    Synopsis:
386ee8199e6SMatthew G. Knepley    #include <petscsys.h>
38731e9e36bSSatish Balay    PetscErrorCode SETERRQ9(MPI_Comm comm,PetscErrorCode ierr,char *formatmessage,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9)
388ee8199e6SMatthew G. Knepley 
389d083f849SBarry Smith    Collective
390ee8199e6SMatthew G. Knepley 
391ee8199e6SMatthew G. Knepley    Input Parameters:
392ee8199e6SMatthew G. Knepley +  comm - A communicator, so that the error can be collective
393ee8199e6SMatthew G. Knepley .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
394ee8199e6SMatthew G. Knepley .  message - error message in the printf format
395ee8199e6SMatthew G. Knepley .  arg1 - argument (for example an integer, string or double)
396ee8199e6SMatthew G. Knepley .  arg2 - argument (for example an integer, string or double)
397ee8199e6SMatthew G. Knepley .  arg3 - argument (for example an integer, string or double)
398ee8199e6SMatthew G. Knepley .  arg4 - argument (for example an integer, string or double)
399ee8199e6SMatthew G. Knepley .  arg5 - argument (for example an integer, string or double)
400ee8199e6SMatthew G. Knepley .  arg6 - argument (for example an integer, string or double)
401ee8199e6SMatthew G. Knepley .  arg7 - argument (for example an integer, string or double)
402ee8199e6SMatthew G. Knepley .  arg8 - argument (for example an integer, string or double)
403ee8199e6SMatthew G. Knepley -  arg9 - argument (for example an integer, string or double)
404ee8199e6SMatthew G. Knepley 
405ee8199e6SMatthew G. Knepley   Level: beginner
406ee8199e6SMatthew G. Knepley 
407ee8199e6SMatthew G. Knepley    Notes:
408ee8199e6SMatthew G. Knepley     Once the error handler is called the calling function is then returned from with the given error code.
409ee8199e6SMatthew G. Knepley 
410ee8199e6SMatthew G. Knepley     There are also versions for 0 to 9 arguments.
411ee8199e6SMatthew G. Knepley 
412ee8199e6SMatthew G. Knepley    Experienced users can set the error handler with PetscPushErrorHandler().
413ee8199e6SMatthew G. Knepley 
414ee8199e6SMatthew G. Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
415ee8199e6SMatthew G. Knepley M*/
416ee8199e6SMatthew G. Knepley #define SETERRQ9(comm,ierr,s,a1,a2,a3,a4,a5,a6,a7,a8,a9) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6,a7,a8,a9)
417ee8199e6SMatthew G. Knepley 
418ee8199e6SMatthew G. Knepley /*MC
419fa190f98SMatthew G. Knepley    SETERRABORT - Macro that can be called when an error has been detected,
420fa190f98SMatthew G. Knepley 
421fa190f98SMatthew G. Knepley    Synopsis:
422fa190f98SMatthew G. Knepley    #include <petscsys.h>
4233af045c5SBarry Smith    PetscErrorCode SETERRABORT(MPI_Comm comm,PetscErrorCode ierr,char *message)
424fa190f98SMatthew G. Knepley 
425d083f849SBarry Smith    Collective
426fa190f98SMatthew G. Knepley 
427fa190f98SMatthew G. Knepley    Input Parameters:
428fa190f98SMatthew G. Knepley +  comm - A communicator, so that the error can be collective
4293af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
430fa190f98SMatthew G. Knepley -  message - error message in the printf format
431fa190f98SMatthew G. Knepley 
432fa190f98SMatthew G. Knepley   Level: beginner
433fa190f98SMatthew G. Knepley 
434fa190f98SMatthew G. Knepley    Notes:
435fa190f98SMatthew G. Knepley     This function just calls MPI_Abort().
436fa190f98SMatthew G. Knepley 
437fa190f98SMatthew G. Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
438fa190f98SMatthew G. Knepley M*/
4393af045c5SBarry Smith #define SETERRABORT(comm,ierr,s) do {PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,ierr,PETSC_ERROR_INITIAL,s);MPI_Abort(comm,ierr);} while (0)
4409a00fa46SSatish Balay 
44130de9b25SBarry Smith /*MC
442888a1fb3SBarry 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
44330de9b25SBarry Smith 
44430de9b25SBarry Smith    Synopsis:
445aaa7dc30SBarry Smith    #include <petscsys.h>
4463af045c5SBarry Smith    PetscErrorCode CHKERRQ(PetscErrorCode ierr)
44730de9b25SBarry Smith 
448eca87e8dSBarry Smith    Not Collective
44930de9b25SBarry Smith 
45030de9b25SBarry Smith    Input Parameters:
4513af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
45230de9b25SBarry Smith 
45330de9b25SBarry Smith   Level: beginner
45430de9b25SBarry Smith 
45530de9b25SBarry Smith    Notes:
45630de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
45730de9b25SBarry Smith 
45830de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
4595324ea47SKris Buschelman 
4603af045c5SBarry Smith     CHKERRQ(ierr) is fundamentally a macro replacement for
4613af045c5SBarry Smith          if (ierr) return(PetscError(...,ierr,...));
4625324ea47SKris Buschelman 
4635324ea47SKris Buschelman     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
4645324ea47SKris Buschelman     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
4655324ea47SKris Buschelman     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
46658ebbce7SBarry Smith     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
4673af045c5SBarry Smith          if (ierr) {PetscError(....); return(YourReturnType);}
4680298fd71SBarry Smith     where you may pass back a NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
46958ebbce7SBarry Smith     MPI_Abort() returned immediately.
47058ebbce7SBarry Smith 
4717037b0edSPatrick Sanan    Fortran Notes:
4727037b0edSPatrick Sanan       CHKERRQ() may be called from Fortran subroutines but CHKERRA() must be called from the
4737037b0edSPatrick Sanan       Fortran main program.
4747037b0edSPatrick Sanan 
47591d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
47630de9b25SBarry Smith M*/
477064a246eSJacob Faibussowitsch #if !defined(PETSC_CLANG_STATIC_ANALYZER)
478ee74f559SJed 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)
4790219f5dfSJed 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)
480064a246eSJacob Faibussowitsch #else
481064a246eSJacob Faibussowitsch #define CHKERRQ(ierr)
482064a246eSJacob Faibussowitsch #define CHKERRV(ierr)
483064a246eSJacob Faibussowitsch #endif
484064a246eSJacob Faibussowitsch 
4857037b0edSPatrick Sanan /*MC
4867037b0edSPatrick Sanan    CHKERRABORT - Checks error code returned from PETSc function. If non-zero it aborts immediately.
4877037b0edSPatrick Sanan 
4887037b0edSPatrick Sanan    Synopsis:
4897037b0edSPatrick Sanan    #include <petscsys.h>
4907037b0edSPatrick Sanan    PetscErrorCode CHKERRABORT(MPI_Comm comm,PetscErrorCode ierr)
4917037b0edSPatrick Sanan 
4927037b0edSPatrick Sanan    Not Collective
4937037b0edSPatrick Sanan 
4947037b0edSPatrick Sanan    Input Parameters:
4957037b0edSPatrick Sanan .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
4967037b0edSPatrick Sanan 
4977037b0edSPatrick Sanan   Level: intermediate
4987037b0edSPatrick Sanan 
4997037b0edSPatrick Sanan .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2(), SETERRABORT(), CHKERRMPI()
5007037b0edSPatrick Sanan M*/
501064a246eSJacob Faibussowitsch #if !defined(PETSC_CLANG_STATIC_ANALYZER)
502ee74f559SJed Brown #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)
503ee74f559SJed Brown #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)
504064a246eSJacob Faibussowitsch #else
505064a246eSJacob Faibussowitsch #define CHKERRABORT(comm,ierr)
506064a246eSJacob Faibussowitsch #define CHKERRCONTINUE(ierr)
507064a246eSJacob Faibussowitsch #endif
508986eef2eSBarry Smith 
5097c66cc67SJunchao Zhang PETSC_EXTERN PetscErrorCode PetscAbortFindSourceFile_Private(const char*,PetscInt*);
5101e4f893aSSatish Balay PETSC_EXTERN PetscBool petscwaitonerrorflg;
5111e4f893aSSatish Balay PETSC_EXTERN PetscBool petscindebugger;
5127c66cc67SJunchao Zhang 
5137c66cc67SJunchao Zhang /*MC
5147c66cc67SJunchao Zhang    PETSCABORT - Call MPI_Abort with an informative error code
5157c66cc67SJunchao Zhang 
5167c66cc67SJunchao Zhang    Synopsis:
5177c66cc67SJunchao Zhang    #include <petscsys.h>
5187c66cc67SJunchao Zhang    PETSCABORT(MPI_Comm comm, PetscErrorCode ierr)
5197c66cc67SJunchao Zhang 
5207c66cc67SJunchao Zhang    Collective
5217c66cc67SJunchao Zhang 
5227c66cc67SJunchao Zhang    Input Parameters:
5237c66cc67SJunchao Zhang +  comm - A communicator, so that the error can be collective
5247c66cc67SJunchao Zhang -  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
5257c66cc67SJunchao Zhang 
526bf4d2887SBarry Smith    Level: advanced
5277c66cc67SJunchao Zhang 
528bf4d2887SBarry Smith    Notes:
529bf4d2887SBarry 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
5307233276eSBarry Smith    where PETSCABORT is called, respectively. ZZZ is the PETSc error code.
5317c66cc67SJunchao Zhang 
5327233276eSBarry Smith    If XX is zero, this means that the call was made in the routine main().
5337233276eSBarry 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[]
5347c66cc67SJunchao Zhang      is out of date. PETSc developers have to update it.
535a0760fecSJunchao 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.
5367233276eSBarry Smith 
537bf4d2887SBarry Smith    If the option -start_in_debugger was used then this calls abort() to stop the program in the debugger.
538bf4d2887SBarry Smith 
5397c66cc67SJunchao Zhang M*/
5407c66cc67SJunchao Zhang #define PETSCABORT(comm,ierr)  \
5417c66cc67SJunchao Zhang    do {                                                               \
5427c66cc67SJunchao Zhang       PetscInt       idx = 0;                                         \
5437c66cc67SJunchao Zhang       PetscMPIInt    errcode;                                         \
5447c66cc67SJunchao Zhang       PetscAbortFindSourceFile_Private(__FILE__,&idx);                \
54565f093c6SBarry Smith       errcode = (PetscMPIInt)(0*idx*10000000 + 0*__LINE__*1000 + ierr);   \
546baae8e41SSatish Balay       if (petscwaitonerrorflg) PetscSleep(1000);                      \
547bf4d2887SBarry Smith       if (petscindebugger) abort();                                   \
548bf4d2887SBarry Smith       else MPI_Abort(comm,errcode);                                   \
5497c66cc67SJunchao Zhang    } while (0)
550986eef2eSBarry Smith 
551986eef2eSBarry Smith /*MC
552888a1fb3SBarry Smith    CHKERRMPI - Checks error code returned from MPI calls, if non-zero it calls the error handler and then returns
553986eef2eSBarry Smith 
554986eef2eSBarry Smith    Synopsis:
555986eef2eSBarry Smith    #include <petscsys.h>
556986eef2eSBarry Smith    PetscErrorCode CHKERRMPI(PetscErrorCode ierr)
557986eef2eSBarry Smith 
558986eef2eSBarry Smith    Not Collective
559986eef2eSBarry Smith 
560986eef2eSBarry Smith    Input Parameters:
561986eef2eSBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
562986eef2eSBarry Smith 
563888a1fb3SBarry Smith   Level: intermediate
564986eef2eSBarry Smith 
565986eef2eSBarry Smith    Notes:
566888a1fb3SBarry Smith     Always returns the error code PETSC_ERR_MPI; the MPI error code and string are embedded in the string error message
567986eef2eSBarry Smith 
5687037b0edSPatrick Sanan .seealso: CHKERRQ(), PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2(), SETERRMPI(), SETERRABORT(), CHKERRABORT()
569986eef2eSBarry Smith M*/
570064a246eSJacob Faibussowitsch #if !defined(PETSC_CLANG_STATIC_ANALYZER)
571ffc4695bSBarry Smith #define CHKERRMPI(ierr) \
572ffc4695bSBarry Smith do { \
5733aa7491dSLisandro Dalcin   PetscErrorCode _7_errorcode = (ierr); \
5743aa7491dSLisandro Dalcin   if (PetscUnlikely(_7_errorcode)) { \
5753aa7491dSLisandro Dalcin     char _7_errorstring[MPI_MAX_ERROR_STRING]; \
5760ce65e24SLisandro Dalcin     PetscMPIInt _7_resultlen; \
5770ce65e24SLisandro Dalcin     MPI_Error_string(_7_errorcode,(char*)_7_errorstring,&_7_resultlen); (void)_7_resultlen; \
5783aa7491dSLisandro Dalcin     SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_MPI,"MPI error %d %s",(int)_7_errorcode,_7_errorstring); \
579ffc4695bSBarry Smith   } \
580ffc4695bSBarry Smith } while (0)
581064a246eSJacob Faibussowitsch #else
582064a246eSJacob Faibussowitsch #define CHKERRMPI(ierr)
583064a246eSJacob Faibussowitsch #endif
58485614651SBarry Smith 
585fd705b32SMatthew Knepley #ifdef PETSC_CLANGUAGE_CXX
586fd705b32SMatthew Knepley 
587cc26af49SMatthew Knepley /*MC
588cc26af49SMatthew Knepley    CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
589cc26af49SMatthew Knepley 
590cc26af49SMatthew Knepley    Synopsis:
591aaa7dc30SBarry Smith    #include <petscsys.h>
5923af045c5SBarry Smith    void CHKERRXX(PetscErrorCode ierr)
593cc26af49SMatthew Knepley 
594eca87e8dSBarry Smith    Not Collective
595cc26af49SMatthew Knepley 
596cc26af49SMatthew Knepley    Input Parameters:
5973af045c5SBarry Smith .  ierr - nonzero error code, see the list of standard error codes in include/petscerror.h
598cc26af49SMatthew Knepley 
599cc26af49SMatthew Knepley   Level: beginner
600cc26af49SMatthew Knepley 
601cc26af49SMatthew Knepley    Notes:
602cc26af49SMatthew Knepley     Once the error handler throws a ??? exception.
603cc26af49SMatthew Knepley 
604cc26af49SMatthew Knepley     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
605cc26af49SMatthew Knepley     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.
606cc26af49SMatthew Knepley 
607cc26af49SMatthew Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKERRQ(), CHKMEMQ
608cc26af49SMatthew Knepley M*/
6093af045c5SBarry 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)
610fd705b32SMatthew Knepley 
611fd705b32SMatthew Knepley #endif
612fd705b32SMatthew Knepley 
613*3f520e80SJunchao Zhang /*MC
614*3f520e80SJunchao Zhang    CHKERRCXX - Checks C++ function calls and if they throw an exception, catch it and then return a PETSc error code
615*3f520e80SJunchao Zhang 
616*3f520e80SJunchao Zhang    Synopsis:
617*3f520e80SJunchao Zhang    #include <petscsys.h>
618*3f520e80SJunchao Zhang    CHKERRCXX(func);
619*3f520e80SJunchao Zhang 
620*3f520e80SJunchao Zhang    Not Collective
621*3f520e80SJunchao Zhang 
622*3f520e80SJunchao Zhang    Input Parameters:
623*3f520e80SJunchao Zhang .  func - C++ function calls
624*3f520e80SJunchao Zhang 
625*3f520e80SJunchao Zhang   Level: beginner
626*3f520e80SJunchao Zhang 
627*3f520e80SJunchao Zhang   Notes:
628*3f520e80SJunchao Zhang    For example,
629*3f520e80SJunchao Zhang 
630*3f520e80SJunchao Zhang $     void foo(int x) {throw std::runtime_error("error");}
631*3f520e80SJunchao Zhang $     CHKERRCXX(foo(1));
632*3f520e80SJunchao Zhang 
633*3f520e80SJunchao Zhang .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKERRQ(), CHKMEMQ
634*3f520e80SJunchao Zhang M*/
635*3f520e80SJunchao Zhang #define CHKERRCXX(func) do {try {func;} catch (const std::exception& e) { SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"%s", e.what()); }} while (0)
636*3f520e80SJunchao Zhang 
63759af0bd3SScott Kruger /* TODO: SEK:  Need to figure out the hipsolver issues */
63859af0bd3SScott Kruger #if defined(PETSC_HAVE_HIP)
63959af0bd3SScott Kruger #define CHKERRHIPSOLVER(err) do {if (PetscUnlikely(err)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"HIPSOLVER error %d",err);} while (0)
64059af0bd3SScott Kruger #endif
64130de9b25SBarry Smith /*MC
64230de9b25SBarry Smith    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
64330de9b25SBarry Smith 
64430de9b25SBarry Smith    Synopsis:
645aaa7dc30SBarry Smith    #include <petscsys.h>
64691d3bdf4SKris Buschelman    CHKMEMQ;
64730de9b25SBarry Smith 
648eca87e8dSBarry Smith    Not Collective
649eca87e8dSBarry Smith 
65030de9b25SBarry Smith   Level: beginner
65130de9b25SBarry Smith 
65230de9b25SBarry Smith    Notes:
6535ed36255SBarry Smith     We highly recommend using valgrind https://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind or for NVIDIA CUDA systems
6545ed36255SBarry Smith     https://docs.nvidia.com/cuda/cuda-memcheck/index.html for finding memory problems. The ``CHKMEMQ`` macro is useful on systems that
6555ed36255SBarry Smith     do not have valgrind, but is not as good as valgrind or cuda-memcheck.
6561957e957SBarry Smith 
65779dccf82SBarry Smith     Must run with the option -malloc_debug (-malloc_test in debug mode; or if PetscMallocSetDebug() called) to enable this option
65830de9b25SBarry Smith 
65930de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
66030de9b25SBarry Smith 
66130de9b25SBarry Smith     By defaults prints location where memory that is corrupted was allocated.
66230de9b25SBarry Smith 
663f621e05eSBarry Smith     Use CHKMEMA for functions that return void
664f621e05eSBarry Smith 
6656024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
666ff002950SBarry Smith           PetscMallocValidate()
66730de9b25SBarry Smith M*/
668064a246eSJacob Faibussowitsch #if !defined(PETSC_CLANG_STATIC_ANALYZER)
669efca3c55SSatish Balay #define CHKMEMQ do {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__);CHKERRQ(_7_ierr);} while (0)
67085614651SBarry Smith 
671efca3c55SSatish Balay #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__)
672064a246eSJacob Faibussowitsch #else
673064a246eSJacob Faibussowitsch #define CHKMEMQ
674064a246eSJacob Faibussowitsch #define CHKMEMA
675064a246eSJacob Faibussowitsch #endif
676668f157eSBarry Smith /*E
677668f157eSBarry Smith   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
678668f157eSBarry Smith 
679668f157eSBarry Smith   Level: advanced
680668f157eSBarry Smith 
681d736bfebSBarry Smith   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
682d736bfebSBarry Smith 
68395452b02SPatrick Sanan   Developer Notes:
68495452b02SPatrick Sanan     This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandler()
685668f157eSBarry Smith 
686d736bfebSBarry Smith .seealso: PetscError(), SETERRXX()
687668f157eSBarry Smith E*/
688d736bfebSBarry Smith typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
6894b209cf6SBarry Smith 
690eb9e708aSLisandro Dalcin #if defined(__clang_analyzer__)
691eb9e708aSLisandro Dalcin __attribute__((analyzer_noreturn))
692eb9e708aSLisandro Dalcin #endif
693eb9e708aSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...);
694eb9e708aSLisandro Dalcin 
695014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
696014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
697efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
698efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
699efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
700efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
701efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
702efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
703efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
704efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
705014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
7068d359177SBarry Smith PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int,void*);
707014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
708014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
70928559dc8SJed Brown PETSC_EXTERN PetscErrorCode PetscCheckPointerSetIntensity(PetscInt);
710c2a741eeSJunchao Zhang PETSC_EXTERN void PetscSignalSegvCheckPointerOrMpi(void);
711c2a741eeSJunchao Zhang PETSC_DEPRECATED_FUNCTION("Use PetscSignalSegvCheckPointerOrMpi() (since version 3.13)") PETSC_STATIC_INLINE void PetscSignalSegvCheckPointer(void) {PetscSignalSegvCheckPointerOrMpi();}
712329f5518SBarry Smith 
713639ff905SBarry Smith /*MC
714639ff905SBarry Smith     PetscErrorPrintf - Prints error messages.
715639ff905SBarry Smith 
716639ff905SBarry Smith    Synopsis:
717aaa7dc30SBarry Smith     #include <petscsys.h>
718639ff905SBarry Smith      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
719639ff905SBarry Smith 
720639ff905SBarry Smith     Not Collective
721639ff905SBarry Smith 
722639ff905SBarry Smith     Input Parameters:
723639ff905SBarry Smith .   format - the usual printf() format string
724639ff905SBarry Smith 
725639ff905SBarry Smith    Options Database Keys:
7261957e957SBarry Smith +    -error_output_stdout - cause error messages to be printed to stdout instead of the  (default) stderr
727e1bc860dSBarry Smith -    -error_output_none - to turn off all printing of error messages (does not change the way the error is handled.)
728639ff905SBarry Smith 
72995452b02SPatrick Sanan    Notes:
73095452b02SPatrick Sanan     Use
731639ff905SBarry Smith $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
732639ff905SBarry Smith $                        error is handled.) and
7331957e957SBarry Smith $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on or you can use your own function
734639ff905SBarry Smith 
735639ff905SBarry Smith           Use
736639ff905SBarry Smith      PETSC_STDERR = FILE* obtained from a file open etc. to have stderr printed to the file.
737639ff905SBarry Smith      PETSC_STDOUT = FILE* obtained from a file open etc. to have stdout printed to the file.
738639ff905SBarry Smith 
739639ff905SBarry Smith           Use
740639ff905SBarry Smith       PetscPushErrorHandler() to provide your own error handler that determines what kind of messages to print
741639ff905SBarry Smith 
742639ff905SBarry Smith    Level: developer
743639ff905SBarry Smith 
744639ff905SBarry Smith     Fortran Note:
745639ff905SBarry Smith     This routine is not supported in Fortran.
746639ff905SBarry Smith 
7472b60790dSJed Brown .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscHelpPrintf(), PetscPrintf(), PetscPushErrorHandler(), PetscVFPrintf(), PetscHelpPrintf()
748639ff905SBarry Smith M*/
749639ff905SBarry Smith PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[],...);
750639ff905SBarry Smith 
751329f5518SBarry Smith typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
752014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
753014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
754014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
755aba4c478SBarry Smith PETSC_EXTERN PetscErrorCode PetscDetermineInitialFPTrap(void);
75654a8ef01SBarry Smith 
7573a40ed3dSBarry Smith /*
7583a40ed3dSBarry Smith       Allows the code to build a stack frame as it runs
7593a40ed3dSBarry Smith */
7603a40ed3dSBarry Smith 
76199cd645aSJed Brown #define PETSCSTACKSIZE 64
762184914b5SBarry Smith 
7633a40ed3dSBarry Smith typedef struct  {
7640e33f6ddSBarry Smith   const char *function[PETSCSTACKSIZE];
7650e33f6ddSBarry Smith   const char *file[PETSCSTACKSIZE];
766184914b5SBarry Smith         int  line[PETSCSTACKSIZE];
767362febeeSStefano Zampini         int  petscroutine[PETSCSTACKSIZE]; /* 0 external called from petsc, 1 petsc functions, 2 petsc user functions */
768184914b5SBarry Smith         int  currentsize;
769a2f94806SJed Brown         int  hotdepth;
770362febeeSStefano Zampini   PetscBool  check; /* runtime option to check for correct Push/Pop semantics at runtime */
7713a40ed3dSBarry Smith } PetscStack;
7723a40ed3dSBarry Smith 
7731f46d60fSShri Abhyankar PETSC_EXTERN PetscStack *petscstack;
774184914b5SBarry Smith 
7756f5c2d7aSBarry Smith PetscErrorCode  PetscStackCopy(PetscStack*,PetscStack*);
7766f5c2d7aSBarry Smith PetscErrorCode  PetscStackPrint(PetscStack *,FILE*);
7775d12eec7SSatish Balay #if defined(PETSC_SERIALIZE_FUNCTIONS)
7785d12eec7SSatish Balay #include <petsc/private/petscfptimpl.h>
7795d12eec7SSatish Balay /*
7805d12eec7SSatish Balay    Registers the current function into the global function pointer to function name table
7815d12eec7SSatish Balay 
7825d12eec7SSatish Balay    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
7835d12eec7SSatish Balay */
7845d12eec7SSatish Balay #define PetscRegister__FUNCT__() do { \
7855d12eec7SSatish Balay   static PetscBool __chked = PETSC_FALSE; \
7865d12eec7SSatish Balay   if (!__chked) {\
7875d12eec7SSatish Balay   void *ptr; PetscDLSym(NULL,PETSC_FUNCTION_NAME,&ptr);\
7885d12eec7SSatish Balay   __chked = PETSC_TRUE;\
7895d12eec7SSatish Balay   }} while (0)
7905d12eec7SSatish Balay #else
7915d12eec7SSatish Balay #define PetscRegister__FUNCT__()
7925d12eec7SSatish Balay #endif
7935d12eec7SSatish Balay 
794064a246eSJacob Faibussowitsch #if !defined(PETSC_CLANG_STATIC_ANALYZER)
7952d7c6352SJed Brown #if defined(PETSC_USE_DEBUG)
796dbf62e16SBarry Smith PETSC_STATIC_INLINE PetscBool PetscStackActive(void)
797dbf62e16SBarry Smith {
7985c25fcd7SBarry Smith   return (petscstack ? PETSC_TRUE : PETSC_FALSE);
799dbf62e16SBarry Smith }
8003a40ed3dSBarry Smith 
801441dd030SJed Brown /* Stack handling is based on the following two "NoCheck" macros.  These should only be called directly by other error
802441dd030SJed Brown  * handling macros.  We record the line of the call, which may or may not be the location of the definition.  But is at
803441dd030SJed Brown  * least more useful than "unknown" because it can distinguish multiple calls from the same function.
804441dd030SJed Brown  */
805a2f94806SJed Brown #define PetscStackPushNoCheck(funct,petsc_routine,hot)                     \
806441dd030SJed Brown   do {                                                                     \
807e04113cfSBarry Smith     PetscStackSAWsTakeAccess();                                            \
808362febeeSStefano Zampini     if (petscstack) {                                                      \
809362febeeSStefano Zampini       if (petscstack->currentsize < PETSCSTACKSIZE) {                      \
8105c25fcd7SBarry Smith         petscstack->function[petscstack->currentsize]  = funct;            \
8115c25fcd7SBarry Smith         petscstack->file[petscstack->currentsize]      = __FILE__;         \
8125c25fcd7SBarry Smith         petscstack->line[petscstack->currentsize]      = __LINE__;         \
8135c25fcd7SBarry Smith         petscstack->petscroutine[petscstack->currentsize] = petsc_routine; \
814441dd030SJed Brown       }                                                                    \
815362febeeSStefano Zampini       petscstack->currentsize++;                                           \
8165c25fcd7SBarry Smith       petscstack->hotdepth += (hot || petscstack->hotdepth);               \
817a2f94806SJed Brown     }                                                                      \
818e04113cfSBarry Smith     PetscStackSAWsGrantAccess();                                           \
819441dd030SJed Brown   } while (0)
820441dd030SJed Brown 
821362febeeSStefano Zampini #define PetscStackPopNoCheck(funct)                                                  \
822362febeeSStefano Zampini   do {                                                                               \
823362febeeSStefano Zampini     PetscStackSAWsTakeAccess();                                                      \
824362febeeSStefano Zampini     if (petscstack) {                                                                \
825362febeeSStefano Zampini       if (petscstack->currentsize <= 0) {                                            \
826362febeeSStefano Zampini         if (petscstack->check) {                                                     \
827362febeeSStefano Zampini           printf("Invalid stack size %d, pop %s\n",petscstack->currentsize,funct);   \
828362febeeSStefano Zampini         }                                                                            \
829362febeeSStefano Zampini       } else {                                                                       \
830362febeeSStefano Zampini         petscstack->currentsize--;                                                   \
831362febeeSStefano Zampini         if (petscstack->currentsize < PETSCSTACKSIZE) {                              \
832362febeeSStefano Zampini           if (petscstack->check &&                                                   \
833362febeeSStefano Zampini               petscstack->petscroutine[petscstack->currentsize] &&                   \
834362febeeSStefano Zampini               petscstack->function[petscstack->currentsize] != (const char*)funct) { \
835362febeeSStefano Zampini             printf("Invalid stack: push from %s, pop from %s\n",                     \
836362febeeSStefano Zampini                    petscstack->function[petscstack->currentsize],funct);             \
837362febeeSStefano Zampini           }                                                                          \
838362febeeSStefano Zampini           petscstack->function[petscstack->currentsize]  = NULL;                     \
839362febeeSStefano Zampini           petscstack->file[petscstack->currentsize]      = NULL;                     \
840362febeeSStefano Zampini           petscstack->line[petscstack->currentsize]      = 0;                        \
841362febeeSStefano Zampini           petscstack->petscroutine[petscstack->currentsize] = 0;                     \
842362febeeSStefano Zampini         }                                                                            \
843362febeeSStefano Zampini         petscstack->hotdepth = PetscMax(petscstack->hotdepth-1,0);                   \
844362febeeSStefano Zampini       }                                                                              \
845362febeeSStefano Zampini     }                                                                                \
846362febeeSStefano Zampini     PetscStackSAWsGrantAccess();                                                     \
847362febeeSStefano Zampini   } while (0)
848362febeeSStefano Zampini 
849362febeeSStefano Zampini #define PetscStackClearTop                                       \
8505c25fcd7SBarry Smith   do {                                                           \
851e04113cfSBarry Smith     PetscStackSAWsTakeAccess();                                  \
8525c25fcd7SBarry Smith     if (petscstack && petscstack->currentsize > 0) {             \
8535c25fcd7SBarry Smith       petscstack->currentsize--;                                 \
854ffc31a0eSLisandro Dalcin       petscstack->function[petscstack->currentsize]  = NULL;     \
855ffc31a0eSLisandro Dalcin       petscstack->file[petscstack->currentsize]      = NULL;     \
8565c25fcd7SBarry Smith       petscstack->line[petscstack->currentsize]      = 0;        \
857362febeeSStefano Zampini       petscstack->petscroutine[petscstack->currentsize] = 0;     \
858441dd030SJed Brown     }                                                            \
8595c25fcd7SBarry Smith     if (petscstack) {                                            \
8605c25fcd7SBarry Smith       petscstack->hotdepth = PetscMax(petscstack->hotdepth-1,0); \
861a2f94806SJed Brown     }                                                            \
862e04113cfSBarry Smith     PetscStackSAWsGrantAccess();                                 \
863441dd030SJed Brown   } while (0)
864441dd030SJed Brown 
86530de9b25SBarry Smith /*MC
8661957e957SBarry Smith    PetscFunctionBegin - First executable line of each PETSc function,  used for error handling. Final
8671957e957SBarry Smith       line of PETSc functions should be PetscFunctionReturn(0);
86830de9b25SBarry Smith 
86930de9b25SBarry Smith    Synopsis:
870aaa7dc30SBarry Smith    #include <petscsys.h>
87130de9b25SBarry Smith    void PetscFunctionBegin;
87230de9b25SBarry Smith 
873eca87e8dSBarry Smith    Not Collective
874eca87e8dSBarry Smith 
87530de9b25SBarry Smith    Usage:
87630de9b25SBarry Smith .vb
87730de9b25SBarry Smith      int something;
87830de9b25SBarry Smith 
87930de9b25SBarry Smith      PetscFunctionBegin;
88030de9b25SBarry Smith .ve
88130de9b25SBarry Smith 
88230de9b25SBarry Smith    Notes:
8831957e957SBarry Smith      Use PetscFunctionBeginUser for application codes.
8841957e957SBarry Smith 
88530de9b25SBarry Smith      Not available in Fortran
88630de9b25SBarry Smith 
88730de9b25SBarry Smith    Level: developer
88830de9b25SBarry Smith 
8891957e957SBarry Smith .seealso: PetscFunctionReturn(), PetscFunctionBeginHot(), PetscFunctionBeginUser()
89030de9b25SBarry Smith 
89130de9b25SBarry Smith M*/
892441dd030SJed Brown #define PetscFunctionBegin do {                               \
893362febeeSStefano Zampini     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,1,PETSC_FALSE); \
894a2f94806SJed Brown     PetscRegister__FUNCT__();                                 \
895a2f94806SJed Brown   } while (0)
896a2f94806SJed Brown 
897a2f94806SJed Brown /*MC
898a2f94806SJed Brown    PetscFunctionBeginHot - Substitute for PetscFunctionBegin to be used in functions that are called in
899a2f94806SJed Brown    performance-critical circumstances.  Use of this function allows for lighter profiling by default.
900a2f94806SJed Brown 
901a2f94806SJed Brown    Synopsis:
902aaa7dc30SBarry Smith    #include <petscsys.h>
903a2f94806SJed Brown    void PetscFunctionBeginHot;
904a2f94806SJed Brown 
905a2f94806SJed Brown    Not Collective
906a2f94806SJed Brown 
907a2f94806SJed Brown    Usage:
908a2f94806SJed Brown .vb
909a2f94806SJed Brown      int something;
910a2f94806SJed Brown 
911a2f94806SJed Brown      PetscFunctionBeginHot;
912a2f94806SJed Brown .ve
913a2f94806SJed Brown 
914a2f94806SJed Brown    Notes:
915a2f94806SJed Brown      Not available in Fortran
916a2f94806SJed Brown 
917a2f94806SJed Brown    Level: developer
918a2f94806SJed Brown 
919a2f94806SJed Brown .seealso: PetscFunctionBegin, PetscFunctionReturn()
920a2f94806SJed Brown 
921a2f94806SJed Brown M*/
922a2f94806SJed Brown #define PetscFunctionBeginHot do {                           \
923362febeeSStefano Zampini     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,1,PETSC_TRUE); \
9242d53ad75SBarry Smith     PetscRegister__FUNCT__();                                \
92553c77d0aSJed Brown   } while (0)
92653c77d0aSJed Brown 
927a8d2bbe5SBarry Smith /*MC
928a8d2bbe5SBarry Smith    PetscFunctionBeginUser - First executable line of user provided PETSc routine
929a8d2bbe5SBarry Smith 
930a8d2bbe5SBarry Smith    Synopsis:
931aaa7dc30SBarry Smith    #include <petscsys.h>
932a8d2bbe5SBarry Smith    void PetscFunctionBeginUser;
933a8d2bbe5SBarry Smith 
934a8d2bbe5SBarry Smith    Not Collective
935a8d2bbe5SBarry Smith 
936a8d2bbe5SBarry Smith    Usage:
937a8d2bbe5SBarry Smith .vb
938a8d2bbe5SBarry Smith      int something;
939a8d2bbe5SBarry Smith 
940ac285190SBarry Smith      PetscFunctionBeginUser;
941a8d2bbe5SBarry Smith .ve
942a8d2bbe5SBarry Smith 
943a8d2bbe5SBarry Smith    Notes:
9441957e957SBarry Smith       Final line of PETSc functions should be PetscFunctionReturn(0) except for main().
9451957e957SBarry Smith 
946a8d2bbe5SBarry Smith       Not available in Fortran
947a8d2bbe5SBarry Smith 
948ac285190SBarry Smith       This is identical to PetscFunctionBegin except it labels the routine as a user
949ac285190SBarry Smith       routine instead of as a PETSc library routine.
950ac285190SBarry Smith 
951a2f94806SJed Brown    Level: intermediate
952a8d2bbe5SBarry Smith 
953a2f94806SJed Brown .seealso: PetscFunctionReturn(), PetscFunctionBegin, PetscFunctionBeginHot
954a8d2bbe5SBarry Smith 
955a8d2bbe5SBarry Smith M*/
956a8d2bbe5SBarry Smith #define PetscFunctionBeginUser                                \
957a8d2bbe5SBarry Smith   do {                                                        \
958362febeeSStefano Zampini     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,2,PETSC_FALSE); \
959a8d2bbe5SBarry Smith     PetscRegister__FUNCT__();                                 \
960a8d2bbe5SBarry Smith   } while (0)
961a8d2bbe5SBarry Smith 
9625cd90555SBarry Smith #define PetscStackPush(n)                   \
96361d886c9SShri Abhyankar   do {                                      \
964362febeeSStefano Zampini     PetscStackPushNoCheck(n,0,PETSC_FALSE); \
96515681b3cSBarry Smith     CHKMEMQ;                                \
96615681b3cSBarry Smith   } while (0)
9673a40ed3dSBarry Smith 
968d64ed03dSBarry Smith #define PetscStackPop                            \
969441dd030SJed Brown     do {                                         \
970441dd030SJed Brown       CHKMEMQ;                                   \
971362febeeSStefano Zampini       PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
97215681b3cSBarry Smith     } while (0)
973d64ed03dSBarry Smith 
97430de9b25SBarry Smith /*MC
97530de9b25SBarry Smith    PetscFunctionReturn - Last executable line of each PETSc function
97630de9b25SBarry Smith         used for error handling. Replaces return()
97730de9b25SBarry Smith 
97830de9b25SBarry Smith    Synopsis:
979aaa7dc30SBarry Smith    #include <petscsys.h>
98030de9b25SBarry Smith    void PetscFunctionReturn(0);
98130de9b25SBarry Smith 
982eca87e8dSBarry Smith    Not Collective
983eca87e8dSBarry Smith 
98430de9b25SBarry Smith    Usage:
98530de9b25SBarry Smith .vb
98630de9b25SBarry Smith     ....
98730de9b25SBarry Smith      PetscFunctionReturn(0);
98830de9b25SBarry Smith    }
98930de9b25SBarry Smith .ve
99030de9b25SBarry Smith 
99130de9b25SBarry Smith    Notes:
99230de9b25SBarry Smith      Not available in Fortran
99330de9b25SBarry Smith 
99430de9b25SBarry Smith    Level: developer
99530de9b25SBarry Smith 
99630de9b25SBarry Smith .seealso: PetscFunctionBegin()
99730de9b25SBarry Smith 
99830de9b25SBarry Smith M*/
9995cd90555SBarry Smith #define PetscFunctionReturn(a)                 \
10008246ba0dSJed Brown   do {                                         \
1001362febeeSStefano Zampini     PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
10028246ba0dSJed Brown     return(a);} while (0)
1003d64ed03dSBarry Smith 
1004ff94ddecSSatish Balay #define PetscFunctionReturnVoid()              \
10058246ba0dSJed Brown   do {                                         \
1006362febeeSStefano Zampini     PetscStackPopNoCheck(PETSC_FUNCTION_NAME); \
10078246ba0dSJed Brown     return;} while (0)
10086d385327SIbrahima Ba #else
10096d385327SIbrahima Ba 
1010c82b4e47SJed Brown PETSC_STATIC_INLINE PetscBool PetscStackActive(void) {return PETSC_FALSE;}
1011a2f94806SJed Brown #define PetscStackPushNoCheck(funct,petsc_routine,hot) do {} while (0)
1012441dd030SJed Brown #define PetscStackPopNoCheck                           do {} while (0)
1013362febeeSStefano Zampini #define PetscStackClearTop                             do {} while (0)
10143a40ed3dSBarry Smith #define PetscFunctionBegin
10150bdf7c52SPeter Brune #define PetscFunctionBeginUser
1016a2f94806SJed Brown #define PetscFunctionBeginHot
10173a40ed3dSBarry Smith #define PetscFunctionReturn(a)    return(a)
10185665465eSBarry Smith #define PetscFunctionReturnVoid() return
1019812af9f3SBarry Smith #define PetscStackPop             CHKMEMQ
1020812af9f3SBarry Smith #define PetscStackPush(f)         CHKMEMQ
10213a40ed3dSBarry Smith 
10223a40ed3dSBarry Smith #endif
10233a40ed3dSBarry Smith 
1024eb6b5d47SBarry Smith /*
1025eb6b5d47SBarry Smith     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
1026eb6b5d47SBarry Smith 
1027eb6b5d47SBarry Smith    Input Parameters:
1028eb6b5d47SBarry Smith +   name - string that gives the name of the function being called
1029fd3f9acdSBarry Smith -   routine - actual call to the routine, including ierr = and CHKERRQ(ierr);
1030fd3f9acdSBarry Smith 
1031dbf62e16SBarry Smith    Note: Often one should use PetscStackCallStandard() instead. This routine is intended for external library routines that DO NOT return error codes
1032eb6b5d47SBarry Smith 
1033eb6b5d47SBarry 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.
1034eb6b5d47SBarry Smith 
1035eb6b5d47SBarry Smith */
103630ecc5abSKarl Rupp #define PetscStackCall(name,routine) do { PetscStackPush(name);routine;PetscStackPop; } while (0)
1037eb6b5d47SBarry Smith 
1038fd3f9acdSBarry Smith /*
1039fd3f9acdSBarry Smith     PetscStackCallStandard - Calls an external library routine after pushing the name of the routine on the stack.
1040fd3f9acdSBarry Smith 
1041fd3f9acdSBarry Smith    Input Parameters:
1042fd3f9acdSBarry Smith +   func-  name of the routine
1043fd3f9acdSBarry Smith -   args - arguments to the routine surrounded by ()
1044fd3f9acdSBarry Smith 
104595452b02SPatrick Sanan    Notes:
104695452b02SPatrick Sanan     This is intended for external package routines that return error codes. Use PetscStackCall() for those that do not.
1047dbf62e16SBarry Smith 
1048dbf62e16SBarry 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.
1049fd3f9acdSBarry Smith 
1050fd3f9acdSBarry Smith */
1051fd3f9acdSBarry Smith #define PetscStackCallStandard(func,args) do {                                                            \
10521d4906efSStefano Zampini     PetscErrorCode __ierr;                                                                                \
10531d4906efSStefano Zampini     PetscStackPush(#func);                                                                                \
10541d4906efSStefano Zampini     __ierr = func args;                                                                                   \
10551d4906efSStefano Zampini     PetscStackPop;                                                                                        \
10561d4906efSStefano Zampini     if (__ierr) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s(): error code %d",#func,(int)__ierr); \
1057fd3f9acdSBarry Smith   } while (0)
1058fd3f9acdSBarry Smith 
1059064a246eSJacob Faibussowitsch #else /* PETSC_CLANG_STATIC_ANALYZER */
1060064a246eSJacob Faibussowitsch PETSC_STATIC_INLINE PetscBool PetscStackActive(void) {return PETSC_FALSE;}
1061064a246eSJacob Faibussowitsch #define PetscStackPushNoCheck(funct,petsc_routine,hot) do {} while (0)
1062064a246eSJacob Faibussowitsch #define PetscStackPopNoCheck                           do {} while (0)
1063362febeeSStefano Zampini #define PetscStackClearTop                             do {} while (0)
1064064a246eSJacob Faibussowitsch #define PetscFunctionBegin
1065064a246eSJacob Faibussowitsch #define PetscFunctionBeginUser
1066064a246eSJacob Faibussowitsch #define PetscFunctionBeginHot
1067064a246eSJacob Faibussowitsch #define PetscFunctionReturn(a)    return(a)
1068064a246eSJacob Faibussowitsch #define PetscFunctionReturnVoid() return
1069064a246eSJacob Faibussowitsch #define PetscStackPop
1070064a246eSJacob Faibussowitsch #define PetscStackPush(f)
1071064a246eSJacob Faibussowitsch #define PetscStackCall(name,routine)
1072064a246eSJacob Faibussowitsch #define PetscStackCallStandard(name,routine)
1073064a246eSJacob Faibussowitsch #endif /* PETSC_CLANG_STATIC_ANALYZER */
1074064a246eSJacob Faibussowitsch 
1075362febeeSStefano Zampini PETSC_EXTERN PetscErrorCode PetscStackCreate(PetscBool);
1076639ff905SBarry Smith PETSC_EXTERN PetscErrorCode PetscStackView(FILE*);
1077014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackDestroy(void);
107806d1fe2cSBarry Smith 
107906d1fe2cSBarry Smith #endif
1080