xref: /petsc/include/petscerror.h (revision a8d2bbe5e2ec4f82cea34690032c2899b2c07c1b)
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 
7fcfd50ebSBarry Smith #if defined(PETSC_HAVE_STRING_H)
8fcfd50ebSBarry Smith #include <string.h> /* for strcmp */
9fcfd50ebSBarry Smith #endif
10fcfd50ebSBarry Smith 
11c22c1629SBarry Smith 
1254a8ef01SBarry Smith /*
1354a8ef01SBarry Smith    Defines the directory where the compiled source is located; used
145e97870eSBarry Smith    in printing error messages. Each makefile has an entry
155e97870eSBarry Smith    LOCDIR	  =  thedirectory
16330cf3c9SBarry Smith    and bmake/common_variables includes in CCPPFLAGS -D__SDIR__=${LOCDIR}
17f621e05eSBarry Smith    which is a flag passed to the C/C++ compilers. This declaration below
18f621e05eSBarry Smith    is only needed if some code is compiled without the -D__SDIR__
1954a8ef01SBarry Smith */
20330cf3c9SBarry Smith #if !defined(__INSDIR__)
21330cf3c9SBarry Smith #define __INSDIR__ "unknowndirectory/"
2254a8ef01SBarry Smith #endif
2354a8ef01SBarry Smith 
2454a8ef01SBarry Smith /*
254f227f7cSBarry Smith    Defines the function where the compiled source is located; used
26f621e05eSBarry Smith    in printing error messages. This is defined here in case the user
27f621e05eSBarry Smith    does not declare it.
284f227f7cSBarry Smith */
294a2ae208SSatish Balay #if !defined(__FUNCT__)
30da9b6338SBarry Smith #define __FUNCT__ "User provided function"
314f227f7cSBarry Smith #endif
324f227f7cSBarry Smith 
334f227f7cSBarry Smith /*
34329ffe3dSLois Curfman McInnes      These are the generic error codes. These error codes are used
35e2d1d2b7SBarry Smith      many different places in the PETSc source code. The string versions are
360e5e90baSSatish Balay      at src/sys/error/err.c any changes here must also be made there
370f9cf654SBarry Smith      These are also define in include/finclude/petscerror.h any CHANGES here
380f9cf654SBarry Smith      must be also made there.
3945d48df9SBarry Smith 
4054a8ef01SBarry Smith */
412a6744ebSBarry Smith #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
422a6744ebSBarry Smith 
4345d48df9SBarry Smith #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
4447794344SBarry Smith #define PETSC_ERR_SUP              56   /* no support for requested operation */
45e2d1d2b7SBarry Smith #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
46e2d1d2b7SBarry Smith #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
4745d48df9SBarry Smith #define PETSC_ERR_SIG              59   /* signal received */
48f1caa5a4SBarry Smith #define PETSC_ERR_FP               72   /* floating point exception */
49a8c6a408SBarry Smith #define PETSC_ERR_COR              74   /* corrupted PETSc object */
50a8c6a408SBarry Smith #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
51329ffe3dSLois Curfman McInnes #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
52329ffe3dSLois Curfman McInnes #define PETSC_ERR_MEMC             78   /* memory corruption */
53b3cc6726SBarry Smith #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
541302d50aSBarry Smith #define PETSC_ERR_USER             83   /* user has not provided needed function */
554e2ffeddSBarry Smith #define PETSC_ERR_SYS              88   /* error in system call */
56a8b45ee7SBarry Smith #define PETSC_ERR_POINTER          70   /* pointer does not point to valid address */
5745d48df9SBarry Smith 
5845d48df9SBarry Smith #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
5945d48df9SBarry Smith #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
60a8c6a408SBarry Smith #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
6145d48df9SBarry Smith #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
6245d48df9SBarry Smith #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
634f227f7cSBarry Smith #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
644f227f7cSBarry Smith #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
656831982aSBarry Smith #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
66d252947aSBarry Smith #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
678cda6cd7SBarry Smith #define PETSC_ERR_ARG_TYPENOTSET   89   /* the type of the object has not yet been set */
68a8c6a408SBarry Smith #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
694482741eSBarry Smith #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
70958c9bccSBarry Smith #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
714f227f7cSBarry Smith 
724f227f7cSBarry Smith #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
734f227f7cSBarry Smith #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
744f227f7cSBarry Smith #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
75a8c6a408SBarry Smith #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
7645d48df9SBarry Smith 
77329ffe3dSLois Curfman McInnes #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
789e3b2f23SBarry Smith #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
7954a8ef01SBarry Smith 
803855c12bSBarry Smith #define PETSC_ERR_INT_OVERFLOW     84   /* should always be one less then the smallest value */
813855c12bSBarry Smith 
82bf3909cdSBarry Smith #define PETSC_ERR_FLOP_COUNT       90
83e113a28aSBarry Smith #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
84e113a28aSBarry Smith #define PETSC_ERR_MAX_VALUE        92  /* this is always the one more than the largest error code */
852a6744ebSBarry Smith 
8659aaf355SLisandro Dalcin #define PetscStringizeArg(a) #a
8759aaf355SLisandro Dalcin #define PetscStringize(a) PetscStringizeArg(a)
88330cf3c9SBarry Smith #define __SDIR__ PetscStringize(__INSDIR__)
89330cf3c9SBarry Smith 
90e8b7e333SSatish Balay #if defined(PETSC_USE_ERRORCHECKING)
91e8b7e333SSatish Balay 
9230de9b25SBarry Smith /*MC
9330de9b25SBarry Smith    SETERRQ - Macro that is called when an error has been detected,
9430de9b25SBarry Smith 
9530de9b25SBarry Smith    Synopsis:
96e7e72b3dSBarry Smith    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode errorcode,char *message)
9730de9b25SBarry Smith 
98eca87e8dSBarry Smith    Not Collective
9930de9b25SBarry Smith 
10030de9b25SBarry Smith    Input Parameters:
10130de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
10230de9b25SBarry Smith -  message - error message
10330de9b25SBarry Smith 
10430de9b25SBarry Smith   Level: beginner
10530de9b25SBarry Smith 
10630de9b25SBarry Smith    Notes:
10730de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
10830de9b25SBarry Smith 
10930de9b25SBarry Smith     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
11030de9b25SBarry Smith 
11158ebbce7SBarry Smith     In Fortran MPI_Abort() is always called
11230de9b25SBarry Smith 
11330de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
11430de9b25SBarry Smith 
11530de9b25SBarry Smith    Concepts: error^setting condition
11630de9b25SBarry Smith 
11791d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
11830de9b25SBarry Smith M*/
11953c77d0aSJed Brown #define SETERRQ(comm,n,s)              return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s)
12030de9b25SBarry Smith 
12130de9b25SBarry Smith /*MC
12230de9b25SBarry Smith    SETERRQ1 - Macro that is called when an error has been detected,
12330de9b25SBarry Smith 
12430de9b25SBarry Smith    Synopsis:
125e32f2f54SBarry Smith    PetscErrorCode SETERRQ1(MPI_Comm comm,PetscErrorCode errorcode,char *formatmessage,arg)
12630de9b25SBarry Smith 
127eca87e8dSBarry Smith    Not Collective
12830de9b25SBarry Smith 
12930de9b25SBarry Smith    Input Parameters:
13030de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
13130de9b25SBarry Smith .  message - error message in the printf format
13230de9b25SBarry Smith -  arg - argument (for example an integer, string or double)
13330de9b25SBarry Smith 
13430de9b25SBarry Smith   Level: beginner
13530de9b25SBarry Smith 
13630de9b25SBarry Smith    Notes:
13730de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
13830de9b25SBarry Smith 
13930de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
14030de9b25SBarry Smith 
14130de9b25SBarry Smith    Concepts: error^setting condition
14230de9b25SBarry Smith 
14391d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
14430de9b25SBarry Smith M*/
14553c77d0aSJed Brown #define SETERRQ1(comm,n,s,a1)          return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1)
14630de9b25SBarry Smith 
14730de9b25SBarry Smith /*MC
14830de9b25SBarry Smith    SETERRQ2 - Macro that is called when an error has been detected,
14930de9b25SBarry Smith 
15030de9b25SBarry Smith    Synopsis:
151f4442326SMatthew Knepley    PetscErrorCode SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
15230de9b25SBarry Smith 
153eca87e8dSBarry Smith    Not Collective
15430de9b25SBarry Smith 
15530de9b25SBarry Smith    Input Parameters:
15630de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
15730de9b25SBarry Smith .  message - error message in the printf format
15830de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
15930de9b25SBarry Smith -  arg2 - argument (for example an integer, string or double)
16030de9b25SBarry Smith 
16130de9b25SBarry Smith   Level: beginner
16230de9b25SBarry Smith 
16330de9b25SBarry Smith    Notes:
16430de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
16530de9b25SBarry Smith 
16630de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
16730de9b25SBarry Smith 
16830de9b25SBarry Smith    Concepts: error^setting condition
16930de9b25SBarry Smith 
1706024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
17130de9b25SBarry Smith M*/
17253c77d0aSJed Brown #define SETERRQ2(comm,n,s,a1,a2)       return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2)
17330de9b25SBarry Smith 
17430de9b25SBarry Smith /*MC
17530de9b25SBarry Smith    SETERRQ3 - Macro that is called when an error has been detected,
17630de9b25SBarry Smith 
17730de9b25SBarry Smith    Synopsis:
178f4442326SMatthew Knepley    PetscErrorCode SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
17930de9b25SBarry Smith 
180eca87e8dSBarry Smith    Not Collective
18130de9b25SBarry Smith 
18230de9b25SBarry Smith    Input Parameters:
18330de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
18430de9b25SBarry Smith .  message - error message in the printf format
18530de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
18630de9b25SBarry Smith .  arg2 - argument (for example an integer, string or double)
18730de9b25SBarry Smith -  arg3 - argument (for example an integer, string or double)
18830de9b25SBarry Smith 
18930de9b25SBarry Smith   Level: beginner
19030de9b25SBarry Smith 
19130de9b25SBarry Smith    Notes:
19230de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
19330de9b25SBarry Smith 
194f621e05eSBarry Smith     There are also versions for 4, 5, 6 and 7 arguments.
195f621e05eSBarry Smith 
19630de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
19730de9b25SBarry Smith 
19830de9b25SBarry Smith    Concepts: error^setting condition
19930de9b25SBarry Smith 
2006024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
20130de9b25SBarry Smith M*/
20253c77d0aSJed Brown #define SETERRQ3(comm,n,s,a1,a2,a3)    return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3)
20330de9b25SBarry Smith 
20453c77d0aSJed Brown #define SETERRQ4(comm,n,s,a1,a2,a3,a4) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4)
20553c77d0aSJed Brown #define SETERRQ5(comm,n,s,a1,a2,a3,a4,a5)       return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5)
20653c77d0aSJed Brown #define SETERRQ6(comm,n,s,a1,a2,a3,a4,a5,a6)    return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6)
20753c77d0aSJed Brown #define SETERRQ7(comm,n,s,a1,a2,a3,a4,a5,a6,a7) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6,a7)
2082f3d0e1cSMatthew G Knepley #define SETERRQ8(comm,n,s,a1,a2,a3,a4,a5,a6,a7,a8) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6,a7,a8)
20953c77d0aSJed Brown #define SETERRABORT(comm,n,s)     do {PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s);MPI_Abort(comm,n);} while (0)
2109a00fa46SSatish Balay 
21130de9b25SBarry Smith /*MC
21230de9b25SBarry Smith    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
21330de9b25SBarry Smith 
21430de9b25SBarry Smith    Synopsis:
215f4442326SMatthew Knepley    PetscErrorCode CHKERRQ(PetscErrorCode errorcode)
21630de9b25SBarry Smith 
217eca87e8dSBarry Smith    Not Collective
21830de9b25SBarry Smith 
21930de9b25SBarry Smith    Input Parameters:
22030de9b25SBarry Smith .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
22130de9b25SBarry Smith 
22230de9b25SBarry Smith   Level: beginner
22330de9b25SBarry Smith 
22430de9b25SBarry Smith    Notes:
22530de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
22630de9b25SBarry Smith 
22730de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
2285324ea47SKris Buschelman 
229fcecf507SKris Buschelman     CHKERRQ(n) is fundamentally a macro replacement for
2305324ea47SKris Buschelman          if (n) return(PetscError(...,n,...));
2315324ea47SKris Buschelman 
2325324ea47SKris Buschelman     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
2335324ea47SKris Buschelman     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
2345324ea47SKris Buschelman     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
23558ebbce7SBarry Smith     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
2365324ea47SKris Buschelman          if (n) {PetscError(....); return(YourReturnType);}
23758ebbce7SBarry Smith     where you may pass back a PETSC_NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
23858ebbce7SBarry Smith     MPI_Abort() returned immediately.
23958ebbce7SBarry Smith 
24058ebbce7SBarry Smith     In Fortran MPI_Abort() is always called
24130de9b25SBarry Smith 
24230de9b25SBarry Smith    Concepts: error^setting condition
24330de9b25SBarry Smith 
24491d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
24530de9b25SBarry Smith M*/
24653c77d0aSJed Brown #define CHKERRQ(n)             do {if (PetscUnlikely(n)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");} while (0)
24730de9b25SBarry Smith 
24853c77d0aSJed Brown #define CHKERRV(n)             do {if (PetscUnlikely(n)) {n = PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");return;}} while(0)
24953c77d0aSJed Brown #define CHKERRABORT(comm,n)    do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");MPI_Abort(comm,n);}} while (0)
25053c77d0aSJed Brown #define CHKERRCONTINUE(n)      do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");}} while (0)
25185614651SBarry Smith 
252fd705b32SMatthew Knepley #ifdef PETSC_CLANGUAGE_CXX
253fd705b32SMatthew Knepley 
254cc26af49SMatthew Knepley /*MC
255cc26af49SMatthew Knepley    CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
256cc26af49SMatthew Knepley 
257cc26af49SMatthew Knepley    Synopsis:
258cc26af49SMatthew Knepley    void CHKERRXX(PetscErrorCode errorcode)
259cc26af49SMatthew Knepley 
260eca87e8dSBarry Smith    Not Collective
261cc26af49SMatthew Knepley 
262cc26af49SMatthew Knepley    Input Parameters:
263cc26af49SMatthew Knepley .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
264cc26af49SMatthew Knepley 
265cc26af49SMatthew Knepley   Level: beginner
266cc26af49SMatthew Knepley 
267cc26af49SMatthew Knepley    Notes:
268cc26af49SMatthew Knepley     Once the error handler throws a ??? exception.
269cc26af49SMatthew Knepley 
270cc26af49SMatthew Knepley     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
271cc26af49SMatthew Knepley     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.
272cc26af49SMatthew Knepley 
273cc26af49SMatthew Knepley    Concepts: error^setting condition
274cc26af49SMatthew Knepley 
275cc26af49SMatthew Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKERRQ(), CHKMEMQ
276cc26af49SMatthew Knepley M*/
27753c77d0aSJed Brown #define CHKERRXX(n)            do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_IN_CXX,0);}} while(0)
278fd705b32SMatthew Knepley 
279fd705b32SMatthew Knepley #endif
280fd705b32SMatthew Knepley 
28130de9b25SBarry Smith /*MC
28230de9b25SBarry Smith    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
28330de9b25SBarry Smith 
28430de9b25SBarry Smith    Synopsis:
28591d3bdf4SKris Buschelman    CHKMEMQ;
28630de9b25SBarry Smith 
287eca87e8dSBarry Smith    Not Collective
288eca87e8dSBarry Smith 
28930de9b25SBarry Smith   Level: beginner
29030de9b25SBarry Smith 
29130de9b25SBarry Smith    Notes:
292ff002950SBarry Smith     Must run with the option -malloc_debug to enable this option
29330de9b25SBarry Smith 
29430de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
29530de9b25SBarry Smith 
29630de9b25SBarry Smith     By defaults prints location where memory that is corrupted was allocated.
29730de9b25SBarry Smith 
298f621e05eSBarry Smith     Use CHKMEMA for functions that return void
299f621e05eSBarry Smith 
30030de9b25SBarry Smith    Concepts: memory corruption
30130de9b25SBarry Smith 
3026024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
303ff002950SBarry Smith           PetscMallocValidate()
30430de9b25SBarry Smith M*/
30553c77d0aSJed Brown #define CHKMEMQ do {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__);CHKERRQ(_7_ierr);} while(0)
30685614651SBarry Smith 
30753c77d0aSJed Brown #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__)
308e68848bdSBarry Smith 
309f8e50935SSatish Balay #else /* PETSC_USE_ERRORCHECKING */
310f621e05eSBarry Smith 
311f621e05eSBarry Smith /*
312e2e64c6bSBarry Smith     These are defined to be empty for when error checking is turned off, with ./configure --with-errorchecking=0
313f621e05eSBarry Smith */
314f621e05eSBarry Smith 
3154b209cf6SBarry Smith #define SETERRQ(c,n,s)
3164b209cf6SBarry Smith #define SETERRQ1(c,n,s,a1)
3174b209cf6SBarry Smith #define SETERRQ2(c,n,s,a1,a2)
3184b209cf6SBarry Smith #define SETERRQ3(c,n,s,a1,a2,a3)
3194b209cf6SBarry Smith #define SETERRQ4(c,n,s,a1,a2,a3,a4)
3204b209cf6SBarry Smith #define SETERRQ5(c,n,s,a1,a2,a3,a4,a5)
3214b209cf6SBarry Smith #define SETERRQ6(c,n,s,a1,a2,a3,a4,a5,a6)
3224ef524e9SStefano Zampini #define SETERRQ7(c,n,s,a1,a2,a3,a4,a5,a6,a7)
3234ef524e9SStefano Zampini #define SETERRQ8(c,n,s,a1,a2,a3,a4,a5,a6,a7,a8)
3244b209cf6SBarry Smith #define SETERRABORT(comm,n,s)
32585614651SBarry Smith 
3264f227f7cSBarry Smith #define CHKERRQ(n)     ;
3271ee4faa0SMatthew Knepley #define CHKERRABORT(comm,n) ;
3281ee4faa0SMatthew Knepley #define CHKERRCONTINUE(n) ;
32985614651SBarry Smith #define CHKMEMQ        ;
33085614651SBarry Smith 
3318cabf42eSMatthew G Knepley #ifdef PETSC_CLANGUAGE_CXX
3328cabf42eSMatthew G Knepley #define CHKERRXX(n) ;
3338cabf42eSMatthew G Knepley #endif
3348cabf42eSMatthew G Knepley 
335f8e50935SSatish Balay #endif /* PETSC_USE_ERRORCHECKING */
33654a8ef01SBarry Smith 
337668f157eSBarry Smith /*E
338668f157eSBarry Smith   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
339668f157eSBarry Smith 
340668f157eSBarry Smith   Level: advanced
341668f157eSBarry Smith 
342d736bfebSBarry Smith   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
343d736bfebSBarry Smith 
344668f157eSBarry Smith   Developer Notes: This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandling()
345668f157eSBarry Smith 
346d736bfebSBarry Smith .seealso: PetscError(), SETERRXX()
347668f157eSBarry Smith E*/
348d736bfebSBarry Smith typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
3494b209cf6SBarry Smith 
350014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
351014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
352014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
353014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
354014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
355014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
356014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
357014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
358014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
359014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...);
360014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
361014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
362014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDefaultSignalHandler(int,void*);
363014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
364014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
365329f5518SBarry Smith 
366329f5518SBarry Smith typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
367014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
368014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
369014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
37054a8ef01SBarry Smith 
3714396dcddSShri Abhyankar /*  Linux functions CPU_SET and others don't work if sched.h is not included before
3724396dcddSShri Abhyankar     including pthread.h. Also, these functions are active only if either _GNU_SOURCE
3734396dcddSShri Abhyankar     or __USE_GNU is not set (see /usr/include/sched.h and /usr/include/features.h), hence
3744396dcddSShri Abhyankar     set these first.
3754396dcddSShri Abhyankar */
37661d886c9SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES)
3774396dcddSShri Abhyankar #if defined(PETSC_HAVE_SCHED_H)
3784396dcddSShri Abhyankar #ifndef _GNU_SOURCE
3794396dcddSShri Abhyankar #define _GNU_SOURCE
3804396dcddSShri Abhyankar #endif
3814396dcddSShri Abhyankar #include <sched.h>
3824396dcddSShri Abhyankar #endif
38361d886c9SShri Abhyankar #include <pthread.h>
38461d886c9SShri Abhyankar #endif
38561d886c9SShri Abhyankar 
3862e43c059SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES) && !defined(PETSC_PTHREAD_LOCAL)
387fd62bef4SShri Abhyankar /* Get the value associated with key */
388fd62bef4SShri Abhyankar PETSC_STATIC_INLINE void* PetscThreadLocalGetValue(pthread_key_t key)
389fd62bef4SShri Abhyankar {
390fd62bef4SShri Abhyankar   return pthread_getspecific(key);
391fd62bef4SShri Abhyankar }
392fd62bef4SShri Abhyankar 
393fd62bef4SShri Abhyankar /* Set the value for key */
394fd62bef4SShri Abhyankar PETSC_STATIC_INLINE void PetscThreadLocalSetValue(pthread_key_t key,void* value)
395fd62bef4SShri Abhyankar {
396fd62bef4SShri Abhyankar   pthread_setspecific(key,(void*)value);
397fd62bef4SShri Abhyankar }
398fd62bef4SShri Abhyankar 
399fd62bef4SShri Abhyankar /* Create pthread thread local key */
40093471ed9SShri Abhyankar PETSC_STATIC_INLINE void PetscThreadLocalRegister(pthread_key_t *key)
401fd62bef4SShri Abhyankar {
402fd3f9acdSBarry Smith   pthread_key_create(key,PETSC_NULL);
403fd62bef4SShri Abhyankar }
404fd62bef4SShri Abhyankar 
405fd62bef4SShri Abhyankar /* Delete pthread thread local key */
406fd62bef4SShri Abhyankar PETSC_STATIC_INLINE void PetscThreadLocalDestroy(pthread_key_t key)
407fd62bef4SShri Abhyankar {
408fd62bef4SShri Abhyankar   pthread_key_delete(key);
409fd62bef4SShri Abhyankar }
4102e43c059SShri Abhyankar #else
411fd3f9acdSBarry Smith PETSC_STATIC_INLINE void* PetscThreadLocalGetValue(void* key)
412fd62bef4SShri Abhyankar {
413fd3f9acdSBarry Smith   return key;
414fd62bef4SShri Abhyankar }
415fd62bef4SShri Abhyankar 
416fd3f9acdSBarry Smith #define PetscThreadLocalSetValue(key,value) (key = value)
417fd62bef4SShri Abhyankar 
418fd3f9acdSBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalRegister(PETSC_UNUSED void *key)
419fd62bef4SShri Abhyankar {
420fd62bef4SShri Abhyankar }
421fd62bef4SShri Abhyankar 
422fd3f9acdSBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalDestroy(PETSC_UNUSED void *key)
423fd62bef4SShri Abhyankar {
424fd62bef4SShri Abhyankar }
4252e43c059SShri Abhyankar #endif
4262e43c059SShri Abhyankar 
4273a40ed3dSBarry Smith /*
4283a40ed3dSBarry Smith       Allows the code to build a stack frame as it runs
4293a40ed3dSBarry Smith */
4308bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG)
4313a40ed3dSBarry Smith 
43299cd645aSJed Brown #define PETSCSTACKSIZE 64
433184914b5SBarry Smith 
4343a40ed3dSBarry Smith typedef struct  {
4350e33f6ddSBarry Smith   const char      *function[PETSCSTACKSIZE];
4360e33f6ddSBarry Smith   const char      *file[PETSCSTACKSIZE];
4370e33f6ddSBarry Smith   const char      *directory[PETSCSTACKSIZE];
438184914b5SBarry Smith         int       line[PETSCSTACKSIZE];
439*a8d2bbe5SBarry Smith         PetscBool petscroutine[PETSCSTACKSIZE];
440184914b5SBarry Smith         int       currentsize;
4413a40ed3dSBarry Smith } PetscStack;
4423a40ed3dSBarry Smith 
44361d886c9SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES)
444997ce2baSShri Abhyankar #if defined(PETSC_PTHREAD_LOCAL)
445014dd563SJed Brown PETSC_EXTERN PETSC_PTHREAD_LOCAL PetscStack *petscstack;
446fe89fe5aSShri Abhyankar #else
447f5dc6901SShri Abhyankar PETSC_EXTERN pthread_key_t petscstack;
448fe89fe5aSShri Abhyankar #endif
4491f46d60fSShri Abhyankar #elif defined(PETSC_HAVE_OPENMP)
4501f46d60fSShri Abhyankar PETSC_EXTERN PetscStack *petscstack;
4511f46d60fSShri Abhyankar #pragma omp threadprivate(petscstack)
45261d886c9SShri Abhyankar #else
453014dd563SJed Brown PETSC_EXTERN PetscStack *petscstack;
45461d886c9SShri Abhyankar #endif
45561d886c9SShri Abhyankar 
456014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackCopy(PetscStack*,PetscStack*);
457014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackPrint(PetscStack*,FILE* fp);
458184914b5SBarry Smith 
4591f46d60fSShri Abhyankar #define PetscStackActive (((PetscStack*)PetscThreadLocalGetValue(petscstack)) != 0)
4603a40ed3dSBarry Smith 
46130de9b25SBarry Smith /*MC
46230de9b25SBarry Smith    PetscFunctionBegin - First executable line of each PETSc function
46330de9b25SBarry Smith         used for error handling.
46430de9b25SBarry Smith 
46530de9b25SBarry Smith    Synopsis:
46630de9b25SBarry Smith    void PetscFunctionBegin;
46730de9b25SBarry Smith 
468eca87e8dSBarry Smith    Not Collective
469eca87e8dSBarry Smith 
47030de9b25SBarry Smith    Usage:
47130de9b25SBarry Smith .vb
47230de9b25SBarry Smith      int something;
47330de9b25SBarry Smith 
47430de9b25SBarry Smith      PetscFunctionBegin;
47530de9b25SBarry Smith .ve
47630de9b25SBarry Smith 
47730de9b25SBarry Smith    Notes:
47830de9b25SBarry Smith      Not available in Fortran
47930de9b25SBarry Smith 
48030de9b25SBarry Smith    Level: developer
48130de9b25SBarry Smith 
48230de9b25SBarry Smith .seealso: PetscFunctionReturn()
48330de9b25SBarry Smith 
48430de9b25SBarry Smith .keywords: traceback, error handling
48530de9b25SBarry Smith M*/
4863a40ed3dSBarry Smith #define PetscFunctionBegin \
4878246ba0dSJed Brown   do {									\
4881f46d60fSShri Abhyankar     PetscStack* petscstackp;                                            \
4891f46d60fSShri Abhyankar     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);     \
4901f46d60fSShri Abhyankar     if (petscstackp && (petscstackp->currentsize < PETSCSTACKSIZE)) {	\
4911f46d60fSShri Abhyankar       petscstackp->function[petscstackp->currentsize]  = PETSC_FUNCTION_NAME; \
4921f46d60fSShri Abhyankar       petscstackp->file[petscstackp->currentsize]      = __FILE__;        \
4931f46d60fSShri Abhyankar       petscstackp->directory[petscstackp->currentsize] = __SDIR__;        \
4941f46d60fSShri Abhyankar       petscstackp->line[petscstackp->currentsize]      = __LINE__;        \
495*a8d2bbe5SBarry Smith       petscstackp->petscroutine[petscstackp->currentsize] = PETSC_TRUE;  \
4961f46d60fSShri Abhyankar       petscstackp->currentsize++;                                        \
49753c77d0aSJed Brown     }                                                                   \
49853c77d0aSJed Brown     PetscCheck__FUNCT__();						\
4992d53ad75SBarry Smith     PetscRegister__FUNCT__();						\
50053c77d0aSJed Brown   } while (0)
50153c77d0aSJed Brown 
502*a8d2bbe5SBarry Smith /*MC
503*a8d2bbe5SBarry Smith    PetscFunctionBeginUser - First executable line of user provided PETSc routine
504*a8d2bbe5SBarry Smith 
505*a8d2bbe5SBarry Smith    Synopsis:
506*a8d2bbe5SBarry Smith    void PetscFunctionBeginUser;
507*a8d2bbe5SBarry Smith 
508*a8d2bbe5SBarry Smith    Not Collective
509*a8d2bbe5SBarry Smith 
510*a8d2bbe5SBarry Smith    Usage:
511*a8d2bbe5SBarry Smith .vb
512*a8d2bbe5SBarry Smith      int something;
513*a8d2bbe5SBarry Smith 
514*a8d2bbe5SBarry Smith      PetscFunctionBegin;
515*a8d2bbe5SBarry Smith .ve
516*a8d2bbe5SBarry Smith 
517*a8d2bbe5SBarry Smith    Notes:
518*a8d2bbe5SBarry Smith      Not available in Fortran
519*a8d2bbe5SBarry Smith 
520*a8d2bbe5SBarry Smith    Level: developer
521*a8d2bbe5SBarry Smith 
522*a8d2bbe5SBarry Smith .seealso: PetscFunctionReturn()
523*a8d2bbe5SBarry Smith 
524*a8d2bbe5SBarry Smith .keywords: traceback, error handling
525*a8d2bbe5SBarry Smith M*/
526*a8d2bbe5SBarry Smith #define PetscFunctionBeginUser \
527*a8d2bbe5SBarry Smith   do {									\
528*a8d2bbe5SBarry Smith     PetscStack* petscstackp;                                            \
529*a8d2bbe5SBarry Smith     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);     \
530*a8d2bbe5SBarry Smith     if (petscstackp && (petscstackp->currentsize < PETSCSTACKSIZE)) {	\
531*a8d2bbe5SBarry Smith       petscstackp->function[petscstackp->currentsize]  = PETSC_FUNCTION_NAME; \
532*a8d2bbe5SBarry Smith       petscstackp->file[petscstackp->currentsize]      = __FILE__;        \
533*a8d2bbe5SBarry Smith       petscstackp->directory[petscstackp->currentsize] = __SDIR__;        \
534*a8d2bbe5SBarry Smith       petscstackp->line[petscstackp->currentsize]      = __LINE__;        \
535*a8d2bbe5SBarry Smith       petscstackp->petscroutine[petscstackp->currentsize] = PETSC_FALSE;  \
536*a8d2bbe5SBarry Smith       petscstackp->currentsize++;                                        \
537*a8d2bbe5SBarry Smith     }                                                                   \
538*a8d2bbe5SBarry Smith     PetscCheck__FUNCT__();						\
539*a8d2bbe5SBarry Smith     PetscRegister__FUNCT__();						\
540*a8d2bbe5SBarry Smith   } while (0)
541*a8d2bbe5SBarry Smith 
542*a8d2bbe5SBarry Smith 
5432d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
5442d53ad75SBarry Smith #include <petsc-private/petscfptimpl.h>
5452d53ad75SBarry Smith /*
5462d53ad75SBarry Smith    Registers the current function into the global function pointer to function name table
5472d53ad75SBarry Smith 
5482d53ad75SBarry Smith    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
5492d53ad75SBarry Smith */
5502d53ad75SBarry Smith #define PetscRegister__FUNCT__() do { \
5512d53ad75SBarry Smith   static PetscBool __chked = PETSC_FALSE; \
5522d53ad75SBarry Smith   if (!__chked) {\
5532d53ad75SBarry Smith   void *ptr; PetscDLSym(PETSC_NULL,__FUNCT__,&ptr);\
5542d53ad75SBarry Smith   __chked = PETSC_TRUE;\
5552d53ad75SBarry Smith   }} while (0)
5562d53ad75SBarry Smith #else
5572d53ad75SBarry Smith #define PetscRegister__FUNCT__()
5582d53ad75SBarry Smith #endif
5592d53ad75SBarry Smith 
56053c77d0aSJed Brown #define PetscCheck__FUNCT__() do { \
56153c77d0aSJed Brown     if (strcmp(PETSC_FUNCTION_NAME,__FUNCT__) && strcmp(__FUNCT__,"User provided function")) { \
56253c77d0aSJed Brown       (*PetscErrorPrintf)("%s%s:%d: __FUNCT__=\"%s\" does not agree with %s=\"%s\"\n",__SDIR__,__FILE__,__LINE__,__FUNCT__,PetscStringize(PETSC_FUNCTION_NAME),PETSC_FUNCTION_NAME); \
56353c77d0aSJed Brown     }                                                                   \
56453c77d0aSJed Brown   } while (0)
5653a40ed3dSBarry Smith 
5665cd90555SBarry Smith #define PetscStackPush(n) \
56761d886c9SShri Abhyankar   do {									\
5681f46d60fSShri Abhyankar     PetscStack * petscstackp;                                           \
5691f46d60fSShri Abhyankar     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);     \
5701f46d60fSShri Abhyankar     if (petscstackp && (petscstackp->currentsize < PETSCSTACKSIZE)) {	\
5711f46d60fSShri Abhyankar       petscstackp->function[petscstackp->currentsize]  = n;		\
5721f46d60fSShri Abhyankar       petscstackp->file[petscstackp->currentsize]      = "unknown";	\
5731f46d60fSShri Abhyankar       petscstackp->directory[petscstackp->currentsize] = "unknown";	\
5741f46d60fSShri Abhyankar       petscstackp->line[petscstackp->currentsize]      = 0;		\
5751f46d60fSShri Abhyankar       petscstackp->currentsize++;					\
576812af9f3SBarry Smith     } CHKMEMQ;} while (0)
5773a40ed3dSBarry Smith 
578d64ed03dSBarry Smith #define PetscStackPop \
5792f0c533eSSatish Balay   do {PetscStack* petscstackp;CHKMEMQ;                                  \
5801f46d60fSShri Abhyankar     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);     \
5811f46d60fSShri Abhyankar     if (petscstackp && petscstackp->currentsize > 0) {			\
5821f46d60fSShri Abhyankar       petscstackp->currentsize--;					\
5831f46d60fSShri Abhyankar       petscstackp->function[petscstackp->currentsize]  = 0;		\
5841f46d60fSShri Abhyankar       petscstackp->file[petscstackp->currentsize]      = 0;		\
5851f46d60fSShri Abhyankar       petscstackp->directory[petscstackp->currentsize] = 0;		\
5861f46d60fSShri Abhyankar       petscstackp->line[petscstackp->currentsize]      = 0;		\
5878246ba0dSJed Brown     }} while (0)
588d64ed03dSBarry Smith 
58930de9b25SBarry Smith /*MC
59030de9b25SBarry Smith    PetscFunctionReturn - Last executable line of each PETSc function
59130de9b25SBarry Smith         used for error handling. Replaces return()
59230de9b25SBarry Smith 
59330de9b25SBarry Smith    Synopsis:
59430de9b25SBarry Smith    void PetscFunctionReturn(0);
59530de9b25SBarry Smith 
596eca87e8dSBarry Smith    Not Collective
597eca87e8dSBarry Smith 
59830de9b25SBarry Smith    Usage:
59930de9b25SBarry Smith .vb
60030de9b25SBarry Smith     ....
60130de9b25SBarry Smith      PetscFunctionReturn(0);
60230de9b25SBarry Smith    }
60330de9b25SBarry Smith .ve
60430de9b25SBarry Smith 
60530de9b25SBarry Smith    Notes:
60630de9b25SBarry Smith      Not available in Fortran
60730de9b25SBarry Smith 
60830de9b25SBarry Smith    Level: developer
60930de9b25SBarry Smith 
61030de9b25SBarry Smith .seealso: PetscFunctionBegin()
61130de9b25SBarry Smith 
61230de9b25SBarry Smith .keywords: traceback, error handling
61330de9b25SBarry Smith M*/
6145cd90555SBarry Smith #define PetscFunctionReturn(a) \
6158246ba0dSJed Brown   do {									\
6161f46d60fSShri Abhyankar     PetscStack* petscstackp;                                            \
6171f46d60fSShri Abhyankar     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack); \
6181f46d60fSShri Abhyankar     if (petscstackp && petscstackp->currentsize > 0) {			\
6191f46d60fSShri Abhyankar       petscstackp->currentsize--;					\
6201f46d60fSShri Abhyankar       petscstackp->function[petscstackp->currentsize]  = 0;		\
6211f46d60fSShri Abhyankar       petscstackp->file[petscstackp->currentsize]      = 0;		\
6221f46d60fSShri Abhyankar       petscstackp->directory[petscstackp->currentsize] = 0;		\
6231f46d60fSShri Abhyankar       petscstackp->line[petscstackp->currentsize]      = 0;		\
624812af9f3SBarry Smith     }									\
6258246ba0dSJed Brown     return(a);} while (0)
626d64ed03dSBarry Smith 
627ff94ddecSSatish Balay #define PetscFunctionReturnVoid() \
6288246ba0dSJed Brown   do {                                                              \
6291f46d60fSShri Abhyankar     PetscStack* petscstackp;                                        \
6301f46d60fSShri Abhyankar     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack); \
6311f46d60fSShri Abhyankar     if (petscstackp && petscstackp->currentsize > 0) {			\
6321f46d60fSShri Abhyankar       petscstackp->currentsize--;					\
6331f46d60fSShri Abhyankar       petscstackp->function[petscstackp->currentsize]  = 0;		\
6341f46d60fSShri Abhyankar       petscstackp->file[petscstackp->currentsize]      = 0;		\
6351f46d60fSShri Abhyankar       petscstackp->directory[petscstackp->currentsize] = 0;		\
6361f46d60fSShri Abhyankar       petscstackp->line[petscstackp->currentsize]      = 0;		\
637812af9f3SBarry Smith     }									\
6388246ba0dSJed Brown     return;} while (0)
6396d385327SIbrahima Ba #else
6406d385327SIbrahima Ba 
6413a40ed3dSBarry Smith #define PetscFunctionBegin
6423a40ed3dSBarry Smith #define PetscFunctionReturn(a)  return(a)
6435665465eSBarry Smith #define PetscFunctionReturnVoid() return
644812af9f3SBarry Smith #define PetscStackPop     CHKMEMQ
645812af9f3SBarry Smith #define PetscStackPush(f) CHKMEMQ
646d64ed03dSBarry Smith #define PetscStackActive        0
6473a40ed3dSBarry Smith 
6483a40ed3dSBarry Smith #endif
6493a40ed3dSBarry Smith 
650eb6b5d47SBarry Smith /*
651eb6b5d47SBarry Smith     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
652eb6b5d47SBarry Smith 
653eb6b5d47SBarry Smith    Input Parameters:
654eb6b5d47SBarry Smith +   name - string that gives the name of the function being called
655fd3f9acdSBarry Smith -   routine - actual call to the routine, including ierr = and CHKERRQ(ierr);
656fd3f9acdSBarry Smith 
657fd3f9acdSBarry Smith    Note: Often one should use PetscStackCallStandard() instead
658eb6b5d47SBarry Smith 
659eb6b5d47SBarry 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.
660eb6b5d47SBarry Smith 
661fd3f9acdSBarry Smith 
662fd3f9acdSBarry Smith 
663eb6b5d47SBarry Smith */
664eb6b5d47SBarry Smith #define PetscStackCall(name,routine) PetscStackPush(name);routine;PetscStackPop;
665eb6b5d47SBarry Smith 
666fd3f9acdSBarry Smith /*
667fd3f9acdSBarry Smith     PetscStackCallStandard - Calls an external library routine after pushing the name of the routine on the stack.
668fd3f9acdSBarry Smith 
669fd3f9acdSBarry Smith    Input Parameters:
670fd3f9acdSBarry Smith +   func-  name of the routine
671fd3f9acdSBarry Smith -   args - arguments to the routine surrounded by ()
672fd3f9acdSBarry Smith 
673fd3f9acdSBarry Smith    Developer Note: this is so that when a hypre routine results in a crash or corrupts memory, they get blamed instead of PETSc.
674fd3f9acdSBarry Smith 
675fd3f9acdSBarry Smith */
676fd3f9acdSBarry Smith #define PetscStackCallStandard(func,args) do {                        \
677fd3f9acdSBarry Smith     PetscStackPush(#func);ierr = func args;PetscStackPop; if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s()",#func); \
678fd3f9acdSBarry Smith   } while (0)
679fd3f9acdSBarry Smith 
680014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackCreate(void);
681014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackView(PetscViewer);
682014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackDestroy(void);
683014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackPublish(void);
684014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackDepublish(void);
68506d1fe2cSBarry Smith 
68606d1fe2cSBarry Smith #endif
687