xref: /petsc/include/petscerror.h (revision efca3c55b02548817e185e5069a2acfe20fa4458)
154a8ef01SBarry Smith /*
2f621e05eSBarry Smith     Contains all error handling interfaces for PETSc.
354a8ef01SBarry Smith */
445d48df9SBarry Smith #if !defined(__PETSCERROR_H)
545d48df9SBarry Smith #define __PETSCERROR_H
66c7e564aSBarry Smith 
754a8ef01SBarry Smith /*
84f227f7cSBarry Smith    Defines the function where the compiled source is located; used
9f621e05eSBarry Smith    in printing error messages. This is defined here in case the user
10f621e05eSBarry Smith    does not declare it.
114f227f7cSBarry Smith */
124a2ae208SSatish Balay #if !defined(__FUNCT__)
13da9b6338SBarry Smith #define __FUNCT__ "User provided function"
144f227f7cSBarry Smith #endif
154f227f7cSBarry Smith 
164f227f7cSBarry Smith /*
17329ffe3dSLois Curfman McInnes      These are the generic error codes. These error codes are used
18e2d1d2b7SBarry Smith      many different places in the PETSc source code. The string versions are
190e5e90baSSatish Balay      at src/sys/error/err.c any changes here must also be made there
200f9cf654SBarry Smith      These are also define in include/finclude/petscerror.h any CHANGES here
210f9cf654SBarry Smith      must be also made there.
2245d48df9SBarry Smith 
2354a8ef01SBarry Smith */
242a6744ebSBarry Smith #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
252a6744ebSBarry Smith 
2645d48df9SBarry Smith #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
2747794344SBarry Smith #define PETSC_ERR_SUP              56   /* no support for requested operation */
28e2d1d2b7SBarry Smith #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
29e2d1d2b7SBarry Smith #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
3045d48df9SBarry Smith #define PETSC_ERR_SIG              59   /* signal received */
31f1caa5a4SBarry Smith #define PETSC_ERR_FP               72   /* floating point exception */
32a8c6a408SBarry Smith #define PETSC_ERR_COR              74   /* corrupted PETSc object */
33a8c6a408SBarry Smith #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
34329ffe3dSLois Curfman McInnes #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
35329ffe3dSLois Curfman McInnes #define PETSC_ERR_MEMC             78   /* memory corruption */
36b3cc6726SBarry Smith #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
371302d50aSBarry Smith #define PETSC_ERR_USER             83   /* user has not provided needed function */
384e2ffeddSBarry Smith #define PETSC_ERR_SYS              88   /* error in system call */
39a8b45ee7SBarry Smith #define PETSC_ERR_POINTER          70   /* pointer does not point to valid address */
4045d48df9SBarry Smith 
4145d48df9SBarry Smith #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
4245d48df9SBarry Smith #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
43a8c6a408SBarry Smith #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
4445d48df9SBarry Smith #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
4545d48df9SBarry Smith #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
464f227f7cSBarry Smith #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
474f227f7cSBarry Smith #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
486831982aSBarry Smith #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
49d252947aSBarry Smith #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
508cda6cd7SBarry Smith #define PETSC_ERR_ARG_TYPENOTSET   89   /* the type of the object has not yet been set */
51a8c6a408SBarry Smith #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
524482741eSBarry Smith #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
53958c9bccSBarry Smith #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
544f227f7cSBarry Smith 
554f227f7cSBarry Smith #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
564f227f7cSBarry Smith #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
574f227f7cSBarry Smith #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
58a8c6a408SBarry Smith #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
5945d48df9SBarry Smith 
60329ffe3dSLois Curfman McInnes #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
619e3b2f23SBarry Smith #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
6254a8ef01SBarry Smith 
633855c12bSBarry Smith #define PETSC_ERR_INT_OVERFLOW     84   /* should always be one less then the smallest value */
643855c12bSBarry Smith 
65bf3909cdSBarry Smith #define PETSC_ERR_FLOP_COUNT       90
66e113a28aSBarry Smith #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
67e113a28aSBarry Smith #define PETSC_ERR_MAX_VALUE        92  /* this is always the one more than the largest error code */
682a6744ebSBarry Smith 
6959aaf355SLisandro Dalcin #define PetscStringizeArg(a) #a
7059aaf355SLisandro Dalcin #define PetscStringize(a) PetscStringizeArg(a)
71330cf3c9SBarry Smith 
72e8b7e333SSatish Balay #if defined(PETSC_USE_ERRORCHECKING)
73e8b7e333SSatish Balay 
7430de9b25SBarry Smith /*MC
7530de9b25SBarry Smith    SETERRQ - Macro that is called when an error has been detected,
7630de9b25SBarry Smith 
7730de9b25SBarry Smith    Synopsis:
78f2ba6396SBarry Smith    #include "petscsys.h"
79e7e72b3dSBarry Smith    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode errorcode,char *message)
8030de9b25SBarry Smith 
81eca87e8dSBarry Smith    Not Collective
8230de9b25SBarry Smith 
8330de9b25SBarry Smith    Input Parameters:
8430de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
8530de9b25SBarry Smith -  message - error message
8630de9b25SBarry Smith 
8730de9b25SBarry Smith   Level: beginner
8830de9b25SBarry Smith 
8930de9b25SBarry Smith    Notes:
9030de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
9130de9b25SBarry Smith 
9230de9b25SBarry Smith     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
9330de9b25SBarry Smith 
9458ebbce7SBarry Smith     In Fortran MPI_Abort() is always called
9530de9b25SBarry Smith 
9630de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
9730de9b25SBarry Smith 
9830de9b25SBarry Smith    Concepts: error^setting condition
9930de9b25SBarry Smith 
10091d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
10130de9b25SBarry Smith M*/
102*efca3c55SSatish Balay #define SETERRQ(comm,n,s)              return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s)
10330de9b25SBarry Smith 
10430de9b25SBarry Smith /*MC
10530de9b25SBarry Smith    SETERRQ1 - Macro that is called when an error has been detected,
10630de9b25SBarry Smith 
10730de9b25SBarry Smith    Synopsis:
108f2ba6396SBarry Smith    #include "petscsys.h"
109e32f2f54SBarry Smith    PetscErrorCode SETERRQ1(MPI_Comm comm,PetscErrorCode errorcode,char *formatmessage,arg)
11030de9b25SBarry Smith 
111eca87e8dSBarry Smith    Not Collective
11230de9b25SBarry Smith 
11330de9b25SBarry Smith    Input Parameters:
11430de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
11530de9b25SBarry Smith .  message - error message in the printf format
11630de9b25SBarry Smith -  arg - argument (for example an integer, string or double)
11730de9b25SBarry Smith 
11830de9b25SBarry Smith   Level: beginner
11930de9b25SBarry Smith 
12030de9b25SBarry Smith    Notes:
12130de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
12230de9b25SBarry Smith 
12330de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
12430de9b25SBarry Smith 
12530de9b25SBarry Smith    Concepts: error^setting condition
12630de9b25SBarry Smith 
12791d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
12830de9b25SBarry Smith M*/
129*efca3c55SSatish Balay #define SETERRQ1(comm,n,s,a1)          return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s,a1)
13030de9b25SBarry Smith 
13130de9b25SBarry Smith /*MC
13230de9b25SBarry Smith    SETERRQ2 - Macro that is called when an error has been detected,
13330de9b25SBarry Smith 
13430de9b25SBarry Smith    Synopsis:
135f2ba6396SBarry Smith    #include "petscsys.h"
136f4442326SMatthew Knepley    PetscErrorCode SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
13730de9b25SBarry Smith 
138eca87e8dSBarry Smith    Not Collective
13930de9b25SBarry Smith 
14030de9b25SBarry Smith    Input Parameters:
14130de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
14230de9b25SBarry Smith .  message - error message in the printf format
14330de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
14430de9b25SBarry Smith -  arg2 - argument (for example an integer, string or double)
14530de9b25SBarry Smith 
14630de9b25SBarry Smith   Level: beginner
14730de9b25SBarry Smith 
14830de9b25SBarry Smith    Notes:
14930de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
15030de9b25SBarry Smith 
15130de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
15230de9b25SBarry Smith 
15330de9b25SBarry Smith    Concepts: error^setting condition
15430de9b25SBarry Smith 
1556024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
15630de9b25SBarry Smith M*/
157*efca3c55SSatish Balay #define SETERRQ2(comm,n,s,a1,a2)       return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s,a1,a2)
15830de9b25SBarry Smith 
15930de9b25SBarry Smith /*MC
16030de9b25SBarry Smith    SETERRQ3 - Macro that is called when an error has been detected,
16130de9b25SBarry Smith 
16230de9b25SBarry Smith    Synopsis:
163f2ba6396SBarry Smith    #include "petscsys.h"
164f4442326SMatthew Knepley    PetscErrorCode SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
16530de9b25SBarry Smith 
166eca87e8dSBarry Smith    Not Collective
16730de9b25SBarry Smith 
16830de9b25SBarry Smith    Input Parameters:
16930de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
17030de9b25SBarry Smith .  message - error message in the printf format
17130de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
17230de9b25SBarry Smith .  arg2 - argument (for example an integer, string or double)
17330de9b25SBarry Smith -  arg3 - argument (for example an integer, string or double)
17430de9b25SBarry Smith 
17530de9b25SBarry Smith   Level: beginner
17630de9b25SBarry Smith 
17730de9b25SBarry Smith    Notes:
17830de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
17930de9b25SBarry Smith 
180f621e05eSBarry Smith     There are also versions for 4, 5, 6 and 7 arguments.
181f621e05eSBarry Smith 
18230de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
18330de9b25SBarry Smith 
18430de9b25SBarry Smith    Concepts: error^setting condition
18530de9b25SBarry Smith 
1866024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
18730de9b25SBarry Smith M*/
188*efca3c55SSatish Balay #define SETERRQ3(comm,n,s,a1,a2,a3)    return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3)
18930de9b25SBarry Smith 
190*efca3c55SSatish Balay #define SETERRQ4(comm,n,s,a1,a2,a3,a4) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4)
191*efca3c55SSatish Balay #define SETERRQ5(comm,n,s,a1,a2,a3,a4,a5)       return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5)
192*efca3c55SSatish Balay #define SETERRQ6(comm,n,s,a1,a2,a3,a4,a5,a6)    return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6)
193*efca3c55SSatish Balay #define SETERRQ7(comm,n,s,a1,a2,a3,a4,a5,a6,a7) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6,a7)
194*efca3c55SSatish Balay #define SETERRQ8(comm,n,s,a1,a2,a3,a4,a5,a6,a7,a8) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6,a7,a8)
195*efca3c55SSatish Balay #define SETERRABORT(comm,n,s)     do {PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s);MPI_Abort(comm,n);} while (0)
1969a00fa46SSatish Balay 
19730de9b25SBarry Smith /*MC
19830de9b25SBarry Smith    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
19930de9b25SBarry Smith 
20030de9b25SBarry Smith    Synopsis:
201f2ba6396SBarry Smith    #include "petscsys.h"
202f4442326SMatthew Knepley    PetscErrorCode CHKERRQ(PetscErrorCode errorcode)
20330de9b25SBarry Smith 
204eca87e8dSBarry Smith    Not Collective
20530de9b25SBarry Smith 
20630de9b25SBarry Smith    Input Parameters:
20730de9b25SBarry Smith .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
20830de9b25SBarry Smith 
20930de9b25SBarry Smith   Level: beginner
21030de9b25SBarry Smith 
21130de9b25SBarry Smith    Notes:
21230de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
21330de9b25SBarry Smith 
21430de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
2155324ea47SKris Buschelman 
216fcecf507SKris Buschelman     CHKERRQ(n) is fundamentally a macro replacement for
2175324ea47SKris Buschelman          if (n) return(PetscError(...,n,...));
2185324ea47SKris Buschelman 
2195324ea47SKris Buschelman     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
2205324ea47SKris Buschelman     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
2215324ea47SKris Buschelman     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
22258ebbce7SBarry Smith     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
2235324ea47SKris Buschelman          if (n) {PetscError(....); return(YourReturnType);}
2240298fd71SBarry Smith     where you may pass back a NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
22558ebbce7SBarry Smith     MPI_Abort() returned immediately.
22658ebbce7SBarry Smith 
22758ebbce7SBarry Smith     In Fortran MPI_Abort() is always called
22830de9b25SBarry Smith 
22930de9b25SBarry Smith    Concepts: error^setting condition
23030de9b25SBarry Smith 
23191d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
23230de9b25SBarry Smith M*/
233*efca3c55SSatish Balay #define CHKERRQ(n)             do {if (PetscUnlikely(n)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_REPEAT," ");} while (0)
23430de9b25SBarry Smith 
235*efca3c55SSatish Balay #define CHKERRV(n)             do {if (PetscUnlikely(n)) {n = PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_REPEAT," ");return;}} while(0)
236*efca3c55SSatish Balay #define CHKERRABORT(comm,n)    do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_REPEAT," ");MPI_Abort(comm,n);}} while (0)
237*efca3c55SSatish Balay #define CHKERRCONTINUE(n)      do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_REPEAT," ");}} while (0)
23885614651SBarry Smith 
239fd705b32SMatthew Knepley #ifdef PETSC_CLANGUAGE_CXX
240fd705b32SMatthew Knepley 
241cc26af49SMatthew Knepley /*MC
242cc26af49SMatthew Knepley    CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
243cc26af49SMatthew Knepley 
244cc26af49SMatthew Knepley    Synopsis:
245f2ba6396SBarry Smith    #include "petscsys.h"
246cc26af49SMatthew Knepley    void CHKERRXX(PetscErrorCode errorcode)
247cc26af49SMatthew Knepley 
248eca87e8dSBarry Smith    Not Collective
249cc26af49SMatthew Knepley 
250cc26af49SMatthew Knepley    Input Parameters:
251cc26af49SMatthew Knepley .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
252cc26af49SMatthew Knepley 
253cc26af49SMatthew Knepley   Level: beginner
254cc26af49SMatthew Knepley 
255cc26af49SMatthew Knepley    Notes:
256cc26af49SMatthew Knepley     Once the error handler throws a ??? exception.
257cc26af49SMatthew Knepley 
258cc26af49SMatthew Knepley     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
259cc26af49SMatthew Knepley     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.
260cc26af49SMatthew Knepley 
261cc26af49SMatthew Knepley    Concepts: error^setting condition
262cc26af49SMatthew Knepley 
263cc26af49SMatthew Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKERRQ(), CHKMEMQ
264cc26af49SMatthew Knepley M*/
265*efca3c55SSatish Balay #define CHKERRXX(n)            do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_IN_CXX,0);}} while(0)
266fd705b32SMatthew Knepley 
267fd705b32SMatthew Knepley #endif
268fd705b32SMatthew Knepley 
26930de9b25SBarry Smith /*MC
27030de9b25SBarry Smith    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
27130de9b25SBarry Smith 
27230de9b25SBarry Smith    Synopsis:
273f2ba6396SBarry Smith    #include "petscsys.h"
27491d3bdf4SKris Buschelman    CHKMEMQ;
27530de9b25SBarry Smith 
276eca87e8dSBarry Smith    Not Collective
277eca87e8dSBarry Smith 
27830de9b25SBarry Smith   Level: beginner
27930de9b25SBarry Smith 
28030de9b25SBarry Smith    Notes:
281ff002950SBarry Smith     Must run with the option -malloc_debug to enable this option
28230de9b25SBarry Smith 
28330de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
28430de9b25SBarry Smith 
28530de9b25SBarry Smith     By defaults prints location where memory that is corrupted was allocated.
28630de9b25SBarry Smith 
287f621e05eSBarry Smith     Use CHKMEMA for functions that return void
288f621e05eSBarry Smith 
28930de9b25SBarry Smith    Concepts: memory corruption
29030de9b25SBarry Smith 
2916024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
292ff002950SBarry Smith           PetscMallocValidate()
29330de9b25SBarry Smith M*/
294*efca3c55SSatish Balay #define CHKMEMQ do {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__);CHKERRQ(_7_ierr);} while(0)
29585614651SBarry Smith 
296*efca3c55SSatish Balay #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__)
297e68848bdSBarry Smith 
298f8e50935SSatish Balay #else /* PETSC_USE_ERRORCHECKING */
299f621e05eSBarry Smith 
300f621e05eSBarry Smith /*
301e2e64c6bSBarry Smith     These are defined to be empty for when error checking is turned off, with ./configure --with-errorchecking=0
302f621e05eSBarry Smith */
303f621e05eSBarry Smith 
3044b209cf6SBarry Smith #define SETERRQ(c,n,s)
3054b209cf6SBarry Smith #define SETERRQ1(c,n,s,a1)
3064b209cf6SBarry Smith #define SETERRQ2(c,n,s,a1,a2)
3074b209cf6SBarry Smith #define SETERRQ3(c,n,s,a1,a2,a3)
3084b209cf6SBarry Smith #define SETERRQ4(c,n,s,a1,a2,a3,a4)
3094b209cf6SBarry Smith #define SETERRQ5(c,n,s,a1,a2,a3,a4,a5)
3104b209cf6SBarry Smith #define SETERRQ6(c,n,s,a1,a2,a3,a4,a5,a6)
3114ef524e9SStefano Zampini #define SETERRQ7(c,n,s,a1,a2,a3,a4,a5,a6,a7)
3124ef524e9SStefano Zampini #define SETERRQ8(c,n,s,a1,a2,a3,a4,a5,a6,a7,a8)
3134b209cf6SBarry Smith #define SETERRABORT(comm,n,s)
31485614651SBarry Smith 
3154f227f7cSBarry Smith #define CHKERRQ(n)     ;
3161ee4faa0SMatthew Knepley #define CHKERRABORT(comm,n) ;
3171ee4faa0SMatthew Knepley #define CHKERRCONTINUE(n) ;
31885614651SBarry Smith #define CHKMEMQ        ;
31985614651SBarry Smith 
3208cabf42eSMatthew G Knepley #ifdef PETSC_CLANGUAGE_CXX
3218cabf42eSMatthew G Knepley #define CHKERRXX(n) ;
3228cabf42eSMatthew G Knepley #endif
3238cabf42eSMatthew G Knepley 
324f8e50935SSatish Balay #endif /* PETSC_USE_ERRORCHECKING */
32554a8ef01SBarry Smith 
326668f157eSBarry Smith /*E
327668f157eSBarry Smith   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
328668f157eSBarry Smith 
329668f157eSBarry Smith   Level: advanced
330668f157eSBarry Smith 
331d736bfebSBarry Smith   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
332d736bfebSBarry Smith 
333668f157eSBarry Smith   Developer Notes: This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandling()
334668f157eSBarry Smith 
335d736bfebSBarry Smith .seealso: PetscError(), SETERRXX()
336668f157eSBarry Smith E*/
337d736bfebSBarry Smith typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
3384b209cf6SBarry Smith 
339014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
340014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
341*efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
342*efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
343*efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
344*efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
345*efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
346*efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
347*efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
348*efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...);
349*efca3c55SSatish Balay PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
350014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
3518d359177SBarry Smith PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int,void*);
352014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
353014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
354329f5518SBarry Smith 
355639ff905SBarry Smith /*MC
356639ff905SBarry Smith     PetscErrorPrintf - Prints error messages.
357639ff905SBarry Smith 
358639ff905SBarry Smith    Synopsis:
359639ff905SBarry Smith     #include "petscsys.h"
360639ff905SBarry Smith      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
361639ff905SBarry Smith 
362639ff905SBarry Smith     Not Collective
363639ff905SBarry Smith 
364639ff905SBarry Smith     Input Parameters:
365639ff905SBarry Smith .   format - the usual printf() format string
366639ff905SBarry Smith 
367639ff905SBarry Smith    Options Database Keys:
368639ff905SBarry Smith +    -error_output_stdout - cause error messages to be printed to stdout instead of the
369639ff905SBarry Smith          (default) stderr
370639ff905SBarry Smith -    -error_output_none to turn off all printing of error messages (does not change the way the
371639ff905SBarry Smith           error is handled.)
372639ff905SBarry Smith 
373639ff905SBarry Smith    Notes: Use
374639ff905SBarry Smith $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
375639ff905SBarry Smith $                        error is handled.) and
376639ff905SBarry Smith $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on
377639ff905SBarry Smith $        of you can use your own function
378639ff905SBarry Smith 
379639ff905SBarry Smith           Use
380639ff905SBarry Smith      PETSC_STDERR = FILE* obtained from a file open etc. to have stderr printed to the file.
381639ff905SBarry Smith      PETSC_STDOUT = FILE* obtained from a file open etc. to have stdout printed to the file.
382639ff905SBarry Smith 
383639ff905SBarry Smith           Use
384639ff905SBarry Smith       PetscPushErrorHandler() to provide your own error handler that determines what kind of messages to print
385639ff905SBarry Smith 
386639ff905SBarry Smith    Level: developer
387639ff905SBarry Smith 
388639ff905SBarry Smith     Fortran Note:
389639ff905SBarry Smith     This routine is not supported in Fortran.
390639ff905SBarry Smith 
391639ff905SBarry Smith     Concepts: error messages^printing
392639ff905SBarry Smith     Concepts: printing^error messages
393639ff905SBarry Smith 
394639ff905SBarry Smith .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscHelpPrintf(), PetscPrintf(), PetscErrorHandlerPush(), PetscVFPrintf(), PetscHelpPrintf()
395639ff905SBarry Smith M*/
396639ff905SBarry Smith PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[],...);
397639ff905SBarry Smith 
398329f5518SBarry Smith typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
399014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
400014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
401014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
40254a8ef01SBarry Smith 
4034396dcddSShri Abhyankar /*  Linux functions CPU_SET and others don't work if sched.h is not included before
4044396dcddSShri Abhyankar     including pthread.h. Also, these functions are active only if either _GNU_SOURCE
4054396dcddSShri Abhyankar     or __USE_GNU is not set (see /usr/include/sched.h and /usr/include/features.h), hence
4064396dcddSShri Abhyankar     set these first.
4074396dcddSShri Abhyankar */
40841117a8dSSatish Balay #if defined(PETSC_HAVE_PTHREADCLASSES) || defined (PETSC_HAVE_OPENMP)
4094396dcddSShri Abhyankar #if defined(PETSC_HAVE_SCHED_H)
4104396dcddSShri Abhyankar #ifndef _GNU_SOURCE
4114396dcddSShri Abhyankar #define _GNU_SOURCE
4124396dcddSShri Abhyankar #endif
4134396dcddSShri Abhyankar #include <sched.h>
4144396dcddSShri Abhyankar #endif
41561d886c9SShri Abhyankar #include <pthread.h>
41661d886c9SShri Abhyankar #endif
41761d886c9SShri Abhyankar 
418bb9aae2fSBarry Smith /*
419bb9aae2fSBarry Smith      This code is for managing thread local global variables. Each of Linux, Microsoft WINDOWS, OpenMP, and Apple OS X have
420bb9aae2fSBarry Smith    different ways to indicate this. On OS X each thread local global is accessed by using a pthread_key_t for that variable.
421bb9aae2fSBarry Smith    Thus we have functions for creating destroying and using the keys. Except for OS X these access functions merely directly
422bb9aae2fSBarry Smith    acess the thread local variable.
423bb9aae2fSBarry Smith */
424bb9aae2fSBarry Smith 
4252e43c059SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES) && !defined(PETSC_PTHREAD_LOCAL)
426047240e1SBarry Smith typedef pthread_key_t PetscThreadKey;
427fd62bef4SShri Abhyankar /* Get the value associated with key */
428047240e1SBarry Smith PETSC_STATIC_INLINE void* PetscThreadLocalGetValue(PetscThreadKey key)
429fd62bef4SShri Abhyankar {
430fd62bef4SShri Abhyankar   return pthread_getspecific(key);
431fd62bef4SShri Abhyankar }
432fd62bef4SShri Abhyankar 
433fd62bef4SShri Abhyankar /* Set the value for key */
434047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalSetValue(PetscThreadKey *key,void* value)
435fd62bef4SShri Abhyankar {
436bb9aae2fSBarry Smith   pthread_setspecific(*key,(void*)value);
437fd62bef4SShri Abhyankar }
438fd62bef4SShri Abhyankar 
439fd62bef4SShri Abhyankar /* Create pthread thread local key */
440047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalRegister(PetscThreadKey *key)
441fd62bef4SShri Abhyankar {
4420298fd71SBarry Smith   pthread_key_create(key,NULL);
443fd62bef4SShri Abhyankar }
444fd62bef4SShri Abhyankar 
445fd62bef4SShri Abhyankar /* Delete pthread thread local key */
446047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalDestroy(PetscThreadKey key)
447fd62bef4SShri Abhyankar {
448fd62bef4SShri Abhyankar   pthread_key_delete(key);
449fd62bef4SShri Abhyankar }
4502e43c059SShri Abhyankar #else
451047240e1SBarry Smith typedef void* PetscThreadKey;
452047240e1SBarry Smith PETSC_STATIC_INLINE void* PetscThreadLocalGetValue(PetscThreadKey key)
453fd62bef4SShri Abhyankar {
454fd3f9acdSBarry Smith   return key;
455fd62bef4SShri Abhyankar }
456fd62bef4SShri Abhyankar 
457047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalSetValue(PetscThreadKey *key,void* value)
458bb9aae2fSBarry Smith {
459bb9aae2fSBarry Smith   *key = value;
460bb9aae2fSBarry Smith }
461fd62bef4SShri Abhyankar 
462047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalRegister(PETSC_UNUSED PetscThreadKey *key)
463fd62bef4SShri Abhyankar {
464fd62bef4SShri Abhyankar }
465fd62bef4SShri Abhyankar 
466047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalDestroy(PETSC_UNUSED PetscThreadKey key)
467fd62bef4SShri Abhyankar {
468fd62bef4SShri Abhyankar }
4692e43c059SShri Abhyankar #endif
4702e43c059SShri Abhyankar 
4713a40ed3dSBarry Smith /*
4723a40ed3dSBarry Smith       Allows the code to build a stack frame as it runs
4733a40ed3dSBarry Smith */
4748bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG)
4753a40ed3dSBarry Smith 
47699cd645aSJed Brown #define PETSCSTACKSIZE 64
477184914b5SBarry Smith 
4783a40ed3dSBarry Smith typedef struct  {
4790e33f6ddSBarry Smith   const char      *function[PETSCSTACKSIZE];
4800e33f6ddSBarry Smith   const char      *file[PETSCSTACKSIZE];
481184914b5SBarry Smith         int       line[PETSCSTACKSIZE];
482a8d2bbe5SBarry Smith         PetscBool petscroutine[PETSCSTACKSIZE];
483184914b5SBarry Smith         int       currentsize;
4843a40ed3dSBarry Smith } PetscStack;
4853a40ed3dSBarry Smith 
48661d886c9SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES)
487997ce2baSShri Abhyankar #if defined(PETSC_PTHREAD_LOCAL)
488014dd563SJed Brown PETSC_EXTERN PETSC_PTHREAD_LOCAL PetscStack *petscstack;
489fe89fe5aSShri Abhyankar #else
490047240e1SBarry Smith PETSC_EXTERN PetscThreadKey petscstack;
491fe89fe5aSShri Abhyankar #endif
4921f46d60fSShri Abhyankar #elif defined(PETSC_HAVE_OPENMP)
4931f46d60fSShri Abhyankar PETSC_EXTERN PetscStack *petscstack;
4941f46d60fSShri Abhyankar #pragma omp threadprivate(petscstack)
49561d886c9SShri Abhyankar #else
496014dd563SJed Brown PETSC_EXTERN PetscStack *petscstack;
49761d886c9SShri Abhyankar #endif
49861d886c9SShri Abhyankar 
499014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackCopy(PetscStack*,PetscStack*);
500014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackPrint(PetscStack*,FILE* fp);
501184914b5SBarry Smith 
502dbf62e16SBarry Smith PETSC_STATIC_INLINE PetscBool PetscStackActive(void)
503dbf62e16SBarry Smith {
504dbf62e16SBarry Smith   return(PetscThreadLocalGetValue(petscstack) ? PETSC_TRUE : PETSC_FALSE);
505dbf62e16SBarry Smith }
5063a40ed3dSBarry Smith 
507441dd030SJed Brown /* Stack handling is based on the following two "NoCheck" macros.  These should only be called directly by other error
508441dd030SJed Brown  * handling macros.  We record the line of the call, which may or may not be the location of the definition.  But is at
509441dd030SJed Brown  * least more useful than "unknown" because it can distinguish multiple calls from the same function.
510441dd030SJed Brown  */
511441dd030SJed Brown 
512441dd030SJed Brown #define PetscStackPushNoCheck(funct,petsc_routine)                            \
513441dd030SJed Brown   do {                                                                        \
514441dd030SJed Brown     PetscStack* petscstackp;                                                  \
515441dd030SJed Brown     PetscStackAMSTakeAccess();                                                \
516441dd030SJed Brown     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);          \
517441dd030SJed Brown     if (petscstackp && (petscstackp->currentsize < PETSCSTACKSIZE)) {         \
518441dd030SJed Brown       petscstackp->function[petscstackp->currentsize]  = funct;               \
519441dd030SJed Brown       petscstackp->file[petscstackp->currentsize]      = __FILE__;            \
520441dd030SJed Brown       petscstackp->line[petscstackp->currentsize]      = __LINE__;            \
521441dd030SJed Brown       petscstackp->petscroutine[petscstackp->currentsize] = petsc_routine;    \
522441dd030SJed Brown       petscstackp->currentsize++;                                             \
523441dd030SJed Brown     }                                                                         \
524441dd030SJed Brown     PetscStackAMSGrantAccess();                                               \
525441dd030SJed Brown   } while (0)
526441dd030SJed Brown 
527441dd030SJed Brown #define PetscStackPopNoCheck                                            \
528441dd030SJed Brown   do {PetscStack* petscstackp;                                          \
529441dd030SJed Brown     PetscStackAMSTakeAccess();                                          \
530441dd030SJed Brown     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);    \
531441dd030SJed Brown     if (petscstackp && petscstackp->currentsize > 0) {                  \
532441dd030SJed Brown       petscstackp->currentsize--;                                       \
533441dd030SJed Brown       petscstackp->function[petscstackp->currentsize]  = 0;             \
534441dd030SJed Brown       petscstackp->file[petscstackp->currentsize]      = 0;             \
535441dd030SJed Brown       petscstackp->line[petscstackp->currentsize]      = 0;             \
536441dd030SJed Brown       petscstackp->petscroutine[petscstackp->currentsize] = PETSC_FALSE;\
537441dd030SJed Brown     }                                                                   \
538441dd030SJed Brown     PetscStackAMSGrantAccess();                                         \
539441dd030SJed Brown   } while (0)
540441dd030SJed Brown 
54130de9b25SBarry Smith /*MC
54230de9b25SBarry Smith    PetscFunctionBegin - First executable line of each PETSc function
54330de9b25SBarry Smith         used for error handling.
54430de9b25SBarry Smith 
54530de9b25SBarry Smith    Synopsis:
546f2ba6396SBarry Smith    #include "petscsys.h"
54730de9b25SBarry Smith    void PetscFunctionBegin;
54830de9b25SBarry Smith 
549eca87e8dSBarry Smith    Not Collective
550eca87e8dSBarry Smith 
55130de9b25SBarry Smith    Usage:
55230de9b25SBarry Smith .vb
55330de9b25SBarry Smith      int something;
55430de9b25SBarry Smith 
55530de9b25SBarry Smith      PetscFunctionBegin;
55630de9b25SBarry Smith .ve
55730de9b25SBarry Smith 
55830de9b25SBarry Smith    Notes:
55930de9b25SBarry Smith      Not available in Fortran
56030de9b25SBarry Smith 
56130de9b25SBarry Smith    Level: developer
56230de9b25SBarry Smith 
56330de9b25SBarry Smith .seealso: PetscFunctionReturn()
56430de9b25SBarry Smith 
56530de9b25SBarry Smith .keywords: traceback, error handling
56630de9b25SBarry Smith M*/
567441dd030SJed Brown #define PetscFunctionBegin do {                                   \
568441dd030SJed Brown     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_TRUE);        \
56953c77d0aSJed Brown     PetscCheck__FUNCT__();                                        \
5702d53ad75SBarry Smith     PetscRegister__FUNCT__();                                     \
57153c77d0aSJed Brown   } while (0)
57253c77d0aSJed Brown 
573a8d2bbe5SBarry Smith /*MC
574a8d2bbe5SBarry Smith    PetscFunctionBeginUser - First executable line of user provided PETSc routine
575a8d2bbe5SBarry Smith 
576a8d2bbe5SBarry Smith    Synopsis:
577f2ba6396SBarry Smith    #include "petscsys.h"
578a8d2bbe5SBarry Smith    void PetscFunctionBeginUser;
579a8d2bbe5SBarry Smith 
580a8d2bbe5SBarry Smith    Not Collective
581a8d2bbe5SBarry Smith 
582a8d2bbe5SBarry Smith    Usage:
583a8d2bbe5SBarry Smith .vb
584a8d2bbe5SBarry Smith      int something;
585a8d2bbe5SBarry Smith 
586a8d2bbe5SBarry Smith      PetscFunctionBegin;
587a8d2bbe5SBarry Smith .ve
588a8d2bbe5SBarry Smith 
589a8d2bbe5SBarry Smith    Notes:
590a8d2bbe5SBarry Smith      Not available in Fortran
591a8d2bbe5SBarry Smith 
592a8d2bbe5SBarry Smith    Level: developer
593a8d2bbe5SBarry Smith 
594a8d2bbe5SBarry Smith .seealso: PetscFunctionReturn()
595a8d2bbe5SBarry Smith 
596a8d2bbe5SBarry Smith .keywords: traceback, error handling
597a8d2bbe5SBarry Smith M*/
598a8d2bbe5SBarry Smith #define PetscFunctionBeginUser                                          \
599a8d2bbe5SBarry Smith   do {                                                                  \
600441dd030SJed Brown     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_FALSE);             \
601a8d2bbe5SBarry Smith     PetscCheck__FUNCT__();                                              \
602a8d2bbe5SBarry Smith     PetscRegister__FUNCT__();                                           \
603a8d2bbe5SBarry Smith   } while (0)
604a8d2bbe5SBarry Smith 
605a8d2bbe5SBarry Smith 
6062d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
6072d53ad75SBarry Smith #include <petsc-private/petscfptimpl.h>
6082d53ad75SBarry Smith /*
6092d53ad75SBarry Smith    Registers the current function into the global function pointer to function name table
6102d53ad75SBarry Smith 
6112d53ad75SBarry Smith    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
6122d53ad75SBarry Smith */
6132d53ad75SBarry Smith #define PetscRegister__FUNCT__() do { \
6142d53ad75SBarry Smith   static PetscBool __chked = PETSC_FALSE; \
6152d53ad75SBarry Smith   if (!__chked) {\
6160298fd71SBarry Smith   void *ptr; PetscDLSym(NULL,__FUNCT__,&ptr);\
6172d53ad75SBarry Smith   __chked = PETSC_TRUE;\
6182d53ad75SBarry Smith   }} while (0)
6192d53ad75SBarry Smith #else
6202d53ad75SBarry Smith #define PetscRegister__FUNCT__()
6212d53ad75SBarry Smith #endif
6222d53ad75SBarry Smith 
623573b0fb4SBarry Smith #define PetscCheck__FUNCT__() do { PetscBool _sc1,_sc2;                  \
624573b0fb4SBarry Smith     PetscStrcmpNoError(PETSC_FUNCTION_NAME,__FUNCT__,&_sc1);\
625573b0fb4SBarry Smith     PetscStrcmpNoError(__FUNCT__,"User provided function",&_sc2);\
626573b0fb4SBarry Smith     if (!_sc1 && !_sc2) { \
627e9f0693fSJed Brown       printf("%s:%d: __FUNCT__=\"%s\" does not agree with %s=\"%s\"\n",__FILE__,__LINE__,__FUNCT__,PetscStringize(PETSC_FUNCTION_NAME),PETSC_FUNCTION_NAME); \
62853c77d0aSJed Brown     }                                                                   \
62953c77d0aSJed Brown   } while (0)
6303a40ed3dSBarry Smith 
6315cd90555SBarry Smith #define PetscStackPush(n) \
63261d886c9SShri Abhyankar   do {                                                                  \
633441dd030SJed Brown     PetscStackPushNoCheck(n,PETSC_FALSE);                               \
63415681b3cSBarry Smith     CHKMEMQ;                                                            \
63515681b3cSBarry Smith   } while (0)
6363a40ed3dSBarry Smith 
637d64ed03dSBarry Smith #define PetscStackPop                           \
638441dd030SJed Brown     do {                                        \
639441dd030SJed Brown       CHKMEMQ;                                  \
640441dd030SJed Brown       PetscStackPopNoCheck;                     \
64115681b3cSBarry Smith     } while (0)
642d64ed03dSBarry Smith 
64330de9b25SBarry Smith /*MC
64430de9b25SBarry Smith    PetscFunctionReturn - Last executable line of each PETSc function
64530de9b25SBarry Smith         used for error handling. Replaces return()
64630de9b25SBarry Smith 
64730de9b25SBarry Smith    Synopsis:
648f2ba6396SBarry Smith    #include "petscsys.h"
64930de9b25SBarry Smith    void PetscFunctionReturn(0);
65030de9b25SBarry Smith 
651eca87e8dSBarry Smith    Not Collective
652eca87e8dSBarry Smith 
65330de9b25SBarry Smith    Usage:
65430de9b25SBarry Smith .vb
65530de9b25SBarry Smith     ....
65630de9b25SBarry Smith      PetscFunctionReturn(0);
65730de9b25SBarry Smith    }
65830de9b25SBarry Smith .ve
65930de9b25SBarry Smith 
66030de9b25SBarry Smith    Notes:
66130de9b25SBarry Smith      Not available in Fortran
66230de9b25SBarry Smith 
66330de9b25SBarry Smith    Level: developer
66430de9b25SBarry Smith 
66530de9b25SBarry Smith .seealso: PetscFunctionBegin()
66630de9b25SBarry Smith 
66730de9b25SBarry Smith .keywords: traceback, error handling
66830de9b25SBarry Smith M*/
6695cd90555SBarry Smith #define PetscFunctionReturn(a) \
6708246ba0dSJed Brown   do {                                                                \
671441dd030SJed Brown     PetscStackPopNoCheck;                                             \
6728246ba0dSJed Brown     return(a);} while (0)
673d64ed03dSBarry Smith 
674ff94ddecSSatish Balay #define PetscFunctionReturnVoid() \
6758246ba0dSJed Brown   do {                                                                \
676441dd030SJed Brown     PetscStackPopNoCheck;                                             \
6778246ba0dSJed Brown     return;} while (0)
67876386721SLisandro Dalcin 
6796d385327SIbrahima Ba #else
6806d385327SIbrahima Ba 
68176386721SLisandro Dalcin #if defined(PETSC_HAVE_PTHREADCLASSES)
68276386721SLisandro Dalcin #if defined(PETSC_PTHREAD_LOCAL)
68376386721SLisandro Dalcin PETSC_EXTERN PETSC_PTHREAD_LOCAL void *petscstack;
68476386721SLisandro Dalcin #else
68576386721SLisandro Dalcin PETSC_EXTERN PetscThreadKey petscstack;
68676386721SLisandro Dalcin #endif
68776386721SLisandro Dalcin #elif defined(PETSC_HAVE_OPENMP)
68876386721SLisandro Dalcin PETSC_EXTERN void *petscstack;
68976386721SLisandro Dalcin #pragma omp threadprivate(petscstack)
69076386721SLisandro Dalcin #else
69176386721SLisandro Dalcin PETSC_EXTERN void *petscstack;
69276386721SLisandro Dalcin #endif
69376386721SLisandro Dalcin 
694441dd030SJed Brown #define PetscStackPushNoCheck(funct,petsc_routine) do {} while (0)
695441dd030SJed Brown #define PetscStackPopNoCheck                       do {} while (0)
6963a40ed3dSBarry Smith #define PetscFunctionBegin
6970bdf7c52SPeter Brune #define PetscFunctionBeginUser
6983a40ed3dSBarry Smith #define PetscFunctionReturn(a)    return(a)
6995665465eSBarry Smith #define PetscFunctionReturnVoid() return
700812af9f3SBarry Smith #define PetscStackPop             CHKMEMQ
701812af9f3SBarry Smith #define PetscStackPush(f)         CHKMEMQ
702dbf62e16SBarry Smith #define PetscStackActive          PETSC_FALSE
7033a40ed3dSBarry Smith 
7043a40ed3dSBarry Smith #endif
7053a40ed3dSBarry Smith 
706eb6b5d47SBarry Smith /*
707eb6b5d47SBarry Smith     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
708eb6b5d47SBarry Smith 
709eb6b5d47SBarry Smith    Input Parameters:
710eb6b5d47SBarry Smith +   name - string that gives the name of the function being called
711fd3f9acdSBarry Smith -   routine - actual call to the routine, including ierr = and CHKERRQ(ierr);
712fd3f9acdSBarry Smith 
713dbf62e16SBarry Smith    Note: Often one should use PetscStackCallStandard() instead. This routine is intended for external library routines that DO NOT return error codes
714eb6b5d47SBarry Smith 
715eb6b5d47SBarry 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.
716eb6b5d47SBarry Smith 
717fd3f9acdSBarry Smith 
718fd3f9acdSBarry Smith 
719eb6b5d47SBarry Smith */
72030ecc5abSKarl Rupp #define PetscStackCall(name,routine) do { PetscStackPush(name);routine;PetscStackPop; } while(0)
721eb6b5d47SBarry Smith 
722fd3f9acdSBarry Smith /*
723fd3f9acdSBarry Smith     PetscStackCallStandard - Calls an external library routine after pushing the name of the routine on the stack.
724fd3f9acdSBarry Smith 
725fd3f9acdSBarry Smith    Input Parameters:
726fd3f9acdSBarry Smith +   func-  name of the routine
727fd3f9acdSBarry Smith -   args - arguments to the routine surrounded by ()
728fd3f9acdSBarry Smith 
729dbf62e16SBarry Smith    Notes: This is intended for external package routines that return error codes. Use PetscStackCall() for those that do not.
730dbf62e16SBarry Smith 
731dbf62e16SBarry 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.
732fd3f9acdSBarry Smith 
733fd3f9acdSBarry Smith */
734fd3f9acdSBarry Smith #define PetscStackCallStandard(func,args) do {                        \
735fd3f9acdSBarry Smith     PetscStackPush(#func);ierr = func args;PetscStackPop; if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s()",#func); \
736fd3f9acdSBarry Smith   } while (0)
737fd3f9acdSBarry Smith 
738014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackCreate(void);
739639ff905SBarry Smith PETSC_EXTERN PetscErrorCode PetscStackView(FILE*);
740014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackDestroy(void);
74106d1fe2cSBarry Smith 
74206d1fe2cSBarry Smith #endif
743