xref: /petsc/include/petscerror.h (revision 047240e14af00aad1ef65e96f6fface8924f7f7e)
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:
96f2ba6396SBarry Smith    #include "petscsys.h"
97e7e72b3dSBarry Smith    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode errorcode,char *message)
9830de9b25SBarry Smith 
99eca87e8dSBarry Smith    Not Collective
10030de9b25SBarry Smith 
10130de9b25SBarry Smith    Input Parameters:
10230de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
10330de9b25SBarry Smith -  message - error message
10430de9b25SBarry Smith 
10530de9b25SBarry Smith   Level: beginner
10630de9b25SBarry Smith 
10730de9b25SBarry Smith    Notes:
10830de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
10930de9b25SBarry Smith 
11030de9b25SBarry Smith     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
11130de9b25SBarry Smith 
11258ebbce7SBarry Smith     In Fortran MPI_Abort() is always called
11330de9b25SBarry Smith 
11430de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
11530de9b25SBarry Smith 
11630de9b25SBarry Smith    Concepts: error^setting condition
11730de9b25SBarry Smith 
11891d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
11930de9b25SBarry Smith M*/
12053c77d0aSJed Brown #define SETERRQ(comm,n,s)              return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s)
12130de9b25SBarry Smith 
12230de9b25SBarry Smith /*MC
12330de9b25SBarry Smith    SETERRQ1 - Macro that is called when an error has been detected,
12430de9b25SBarry Smith 
12530de9b25SBarry Smith    Synopsis:
126f2ba6396SBarry Smith    #include "petscsys.h"
127e32f2f54SBarry Smith    PetscErrorCode SETERRQ1(MPI_Comm comm,PetscErrorCode errorcode,char *formatmessage,arg)
12830de9b25SBarry Smith 
129eca87e8dSBarry Smith    Not Collective
13030de9b25SBarry Smith 
13130de9b25SBarry Smith    Input Parameters:
13230de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
13330de9b25SBarry Smith .  message - error message in the printf format
13430de9b25SBarry Smith -  arg - argument (for example an integer, string or double)
13530de9b25SBarry Smith 
13630de9b25SBarry Smith   Level: beginner
13730de9b25SBarry Smith 
13830de9b25SBarry Smith    Notes:
13930de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
14030de9b25SBarry Smith 
14130de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
14230de9b25SBarry Smith 
14330de9b25SBarry Smith    Concepts: error^setting condition
14430de9b25SBarry Smith 
14591d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
14630de9b25SBarry Smith M*/
14753c77d0aSJed Brown #define SETERRQ1(comm,n,s,a1)          return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1)
14830de9b25SBarry Smith 
14930de9b25SBarry Smith /*MC
15030de9b25SBarry Smith    SETERRQ2 - Macro that is called when an error has been detected,
15130de9b25SBarry Smith 
15230de9b25SBarry Smith    Synopsis:
153f2ba6396SBarry Smith    #include "petscsys.h"
154f4442326SMatthew Knepley    PetscErrorCode SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
15530de9b25SBarry Smith 
156eca87e8dSBarry Smith    Not Collective
15730de9b25SBarry Smith 
15830de9b25SBarry Smith    Input Parameters:
15930de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
16030de9b25SBarry Smith .  message - error message in the printf format
16130de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
16230de9b25SBarry Smith -  arg2 - argument (for example an integer, string or double)
16330de9b25SBarry Smith 
16430de9b25SBarry Smith   Level: beginner
16530de9b25SBarry Smith 
16630de9b25SBarry Smith    Notes:
16730de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
16830de9b25SBarry Smith 
16930de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
17030de9b25SBarry Smith 
17130de9b25SBarry Smith    Concepts: error^setting condition
17230de9b25SBarry Smith 
1736024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
17430de9b25SBarry Smith M*/
17553c77d0aSJed Brown #define SETERRQ2(comm,n,s,a1,a2)       return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2)
17630de9b25SBarry Smith 
17730de9b25SBarry Smith /*MC
17830de9b25SBarry Smith    SETERRQ3 - Macro that is called when an error has been detected,
17930de9b25SBarry Smith 
18030de9b25SBarry Smith    Synopsis:
181f2ba6396SBarry Smith    #include "petscsys.h"
182f4442326SMatthew Knepley    PetscErrorCode SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
18330de9b25SBarry Smith 
184eca87e8dSBarry Smith    Not Collective
18530de9b25SBarry Smith 
18630de9b25SBarry Smith    Input Parameters:
18730de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
18830de9b25SBarry Smith .  message - error message in the printf format
18930de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
19030de9b25SBarry Smith .  arg2 - argument (for example an integer, string or double)
19130de9b25SBarry Smith -  arg3 - argument (for example an integer, string or double)
19230de9b25SBarry Smith 
19330de9b25SBarry Smith   Level: beginner
19430de9b25SBarry Smith 
19530de9b25SBarry Smith    Notes:
19630de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
19730de9b25SBarry Smith 
198f621e05eSBarry Smith     There are also versions for 4, 5, 6 and 7 arguments.
199f621e05eSBarry Smith 
20030de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
20130de9b25SBarry Smith 
20230de9b25SBarry Smith    Concepts: error^setting condition
20330de9b25SBarry Smith 
2046024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
20530de9b25SBarry Smith M*/
20653c77d0aSJed 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)
20730de9b25SBarry Smith 
20853c77d0aSJed 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)
20953c77d0aSJed 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)
21053c77d0aSJed 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)
21153c77d0aSJed 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)
2122f3d0e1cSMatthew 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)
21353c77d0aSJed 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)
2149a00fa46SSatish Balay 
21530de9b25SBarry Smith /*MC
21630de9b25SBarry Smith    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
21730de9b25SBarry Smith 
21830de9b25SBarry Smith    Synopsis:
219f2ba6396SBarry Smith    #include "petscsys.h"
220f4442326SMatthew Knepley    PetscErrorCode CHKERRQ(PetscErrorCode errorcode)
22130de9b25SBarry Smith 
222eca87e8dSBarry Smith    Not Collective
22330de9b25SBarry Smith 
22430de9b25SBarry Smith    Input Parameters:
22530de9b25SBarry Smith .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
22630de9b25SBarry Smith 
22730de9b25SBarry Smith   Level: beginner
22830de9b25SBarry Smith 
22930de9b25SBarry Smith    Notes:
23030de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
23130de9b25SBarry Smith 
23230de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
2335324ea47SKris Buschelman 
234fcecf507SKris Buschelman     CHKERRQ(n) is fundamentally a macro replacement for
2355324ea47SKris Buschelman          if (n) return(PetscError(...,n,...));
2365324ea47SKris Buschelman 
2375324ea47SKris Buschelman     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
2385324ea47SKris Buschelman     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
2395324ea47SKris Buschelman     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
24058ebbce7SBarry Smith     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
2415324ea47SKris Buschelman          if (n) {PetscError(....); return(YourReturnType);}
24258ebbce7SBarry Smith     where you may pass back a PETSC_NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
24358ebbce7SBarry Smith     MPI_Abort() returned immediately.
24458ebbce7SBarry Smith 
24558ebbce7SBarry Smith     In Fortran MPI_Abort() is always called
24630de9b25SBarry Smith 
24730de9b25SBarry Smith    Concepts: error^setting condition
24830de9b25SBarry Smith 
24991d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
25030de9b25SBarry Smith M*/
25153c77d0aSJed 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)
25230de9b25SBarry Smith 
25353c77d0aSJed 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)
25453c77d0aSJed 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)
25553c77d0aSJed Brown #define CHKERRCONTINUE(n)      do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");}} while (0)
25685614651SBarry Smith 
257fd705b32SMatthew Knepley #ifdef PETSC_CLANGUAGE_CXX
258fd705b32SMatthew Knepley 
259cc26af49SMatthew Knepley /*MC
260cc26af49SMatthew Knepley    CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
261cc26af49SMatthew Knepley 
262cc26af49SMatthew Knepley    Synopsis:
263f2ba6396SBarry Smith    #include "petscsys.h"
264cc26af49SMatthew Knepley    void CHKERRXX(PetscErrorCode errorcode)
265cc26af49SMatthew Knepley 
266eca87e8dSBarry Smith    Not Collective
267cc26af49SMatthew Knepley 
268cc26af49SMatthew Knepley    Input Parameters:
269cc26af49SMatthew Knepley .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
270cc26af49SMatthew Knepley 
271cc26af49SMatthew Knepley   Level: beginner
272cc26af49SMatthew Knepley 
273cc26af49SMatthew Knepley    Notes:
274cc26af49SMatthew Knepley     Once the error handler throws a ??? exception.
275cc26af49SMatthew Knepley 
276cc26af49SMatthew Knepley     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
277cc26af49SMatthew Knepley     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.
278cc26af49SMatthew Knepley 
279cc26af49SMatthew Knepley    Concepts: error^setting condition
280cc26af49SMatthew Knepley 
281cc26af49SMatthew Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKERRQ(), CHKMEMQ
282cc26af49SMatthew Knepley M*/
28353c77d0aSJed 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)
284fd705b32SMatthew Knepley 
285fd705b32SMatthew Knepley #endif
286fd705b32SMatthew Knepley 
28730de9b25SBarry Smith /*MC
28830de9b25SBarry Smith    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
28930de9b25SBarry Smith 
29030de9b25SBarry Smith    Synopsis:
291f2ba6396SBarry Smith    #include "petscsys.h"
29291d3bdf4SKris Buschelman    CHKMEMQ;
29330de9b25SBarry Smith 
294eca87e8dSBarry Smith    Not Collective
295eca87e8dSBarry Smith 
29630de9b25SBarry Smith   Level: beginner
29730de9b25SBarry Smith 
29830de9b25SBarry Smith    Notes:
299ff002950SBarry Smith     Must run with the option -malloc_debug to enable this option
30030de9b25SBarry Smith 
30130de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
30230de9b25SBarry Smith 
30330de9b25SBarry Smith     By defaults prints location where memory that is corrupted was allocated.
30430de9b25SBarry Smith 
305f621e05eSBarry Smith     Use CHKMEMA for functions that return void
306f621e05eSBarry Smith 
30730de9b25SBarry Smith    Concepts: memory corruption
30830de9b25SBarry Smith 
3096024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
310ff002950SBarry Smith           PetscMallocValidate()
31130de9b25SBarry Smith M*/
31253c77d0aSJed Brown #define CHKMEMQ do {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__);CHKERRQ(_7_ierr);} while(0)
31385614651SBarry Smith 
31453c77d0aSJed Brown #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__)
315e68848bdSBarry Smith 
316f8e50935SSatish Balay #else /* PETSC_USE_ERRORCHECKING */
317f621e05eSBarry Smith 
318f621e05eSBarry Smith /*
319e2e64c6bSBarry Smith     These are defined to be empty for when error checking is turned off, with ./configure --with-errorchecking=0
320f621e05eSBarry Smith */
321f621e05eSBarry Smith 
3224b209cf6SBarry Smith #define SETERRQ(c,n,s)
3234b209cf6SBarry Smith #define SETERRQ1(c,n,s,a1)
3244b209cf6SBarry Smith #define SETERRQ2(c,n,s,a1,a2)
3254b209cf6SBarry Smith #define SETERRQ3(c,n,s,a1,a2,a3)
3264b209cf6SBarry Smith #define SETERRQ4(c,n,s,a1,a2,a3,a4)
3274b209cf6SBarry Smith #define SETERRQ5(c,n,s,a1,a2,a3,a4,a5)
3284b209cf6SBarry Smith #define SETERRQ6(c,n,s,a1,a2,a3,a4,a5,a6)
3294ef524e9SStefano Zampini #define SETERRQ7(c,n,s,a1,a2,a3,a4,a5,a6,a7)
3304ef524e9SStefano Zampini #define SETERRQ8(c,n,s,a1,a2,a3,a4,a5,a6,a7,a8)
3314b209cf6SBarry Smith #define SETERRABORT(comm,n,s)
33285614651SBarry Smith 
3334f227f7cSBarry Smith #define CHKERRQ(n)     ;
3341ee4faa0SMatthew Knepley #define CHKERRABORT(comm,n) ;
3351ee4faa0SMatthew Knepley #define CHKERRCONTINUE(n) ;
33685614651SBarry Smith #define CHKMEMQ        ;
33785614651SBarry Smith 
3388cabf42eSMatthew G Knepley #ifdef PETSC_CLANGUAGE_CXX
3398cabf42eSMatthew G Knepley #define CHKERRXX(n) ;
3408cabf42eSMatthew G Knepley #endif
3418cabf42eSMatthew G Knepley 
342f8e50935SSatish Balay #endif /* PETSC_USE_ERRORCHECKING */
34354a8ef01SBarry Smith 
344668f157eSBarry Smith /*E
345668f157eSBarry Smith   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
346668f157eSBarry Smith 
347668f157eSBarry Smith   Level: advanced
348668f157eSBarry Smith 
349d736bfebSBarry Smith   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
350d736bfebSBarry Smith 
351668f157eSBarry Smith   Developer Notes: This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandling()
352668f157eSBarry Smith 
353d736bfebSBarry Smith .seealso: PetscError(), SETERRXX()
354668f157eSBarry Smith E*/
355d736bfebSBarry Smith typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
3564b209cf6SBarry Smith 
357014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
358014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
359014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
360014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
361014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
362014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
363014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
364014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
365014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
366014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...);
367014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
368014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
369014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDefaultSignalHandler(int,void*);
370014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
371014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
372329f5518SBarry Smith 
373639ff905SBarry Smith 
374639ff905SBarry Smith /*MC
375639ff905SBarry Smith     PetscErrorPrintf - Prints error messages.
376639ff905SBarry Smith 
377639ff905SBarry Smith    Synopsis:
378639ff905SBarry Smith     #include "petscsys.h"
379639ff905SBarry Smith      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
380639ff905SBarry Smith 
381639ff905SBarry Smith     Not Collective
382639ff905SBarry Smith 
383639ff905SBarry Smith     Input Parameters:
384639ff905SBarry Smith .   format - the usual printf() format string
385639ff905SBarry Smith 
386639ff905SBarry Smith    Options Database Keys:
387639ff905SBarry Smith +    -error_output_stdout - cause error messages to be printed to stdout instead of the
388639ff905SBarry Smith          (default) stderr
389639ff905SBarry Smith -    -error_output_none to turn off all printing of error messages (does not change the way the
390639ff905SBarry Smith           error is handled.)
391639ff905SBarry Smith 
392639ff905SBarry Smith    Notes: Use
393639ff905SBarry Smith $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
394639ff905SBarry Smith $                        error is handled.) and
395639ff905SBarry Smith $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on
396639ff905SBarry Smith $        of you can use your own function
397639ff905SBarry Smith 
398639ff905SBarry Smith           Use
399639ff905SBarry Smith      PETSC_STDERR = FILE* obtained from a file open etc. to have stderr printed to the file.
400639ff905SBarry Smith      PETSC_STDOUT = FILE* obtained from a file open etc. to have stdout printed to the file.
401639ff905SBarry Smith 
402639ff905SBarry Smith           Use
403639ff905SBarry Smith       PetscPushErrorHandler() to provide your own error handler that determines what kind of messages to print
404639ff905SBarry Smith 
405639ff905SBarry Smith    Level: developer
406639ff905SBarry Smith 
407639ff905SBarry Smith     Fortran Note:
408639ff905SBarry Smith     This routine is not supported in Fortran.
409639ff905SBarry Smith 
410639ff905SBarry Smith     Concepts: error messages^printing
411639ff905SBarry Smith     Concepts: printing^error messages
412639ff905SBarry Smith 
413639ff905SBarry Smith .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscHelpPrintf(), PetscPrintf(), PetscErrorHandlerPush(), PetscVFPrintf(), PetscHelpPrintf()
414639ff905SBarry Smith M*/
415639ff905SBarry Smith PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[],...);
416639ff905SBarry Smith 
417329f5518SBarry Smith typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
418014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
419014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
420014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
42154a8ef01SBarry Smith 
4224396dcddSShri Abhyankar /*  Linux functions CPU_SET and others don't work if sched.h is not included before
4234396dcddSShri Abhyankar     including pthread.h. Also, these functions are active only if either _GNU_SOURCE
4244396dcddSShri Abhyankar     or __USE_GNU is not set (see /usr/include/sched.h and /usr/include/features.h), hence
4254396dcddSShri Abhyankar     set these first.
4264396dcddSShri Abhyankar */
42761d886c9SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES)
4284396dcddSShri Abhyankar #if defined(PETSC_HAVE_SCHED_H)
4294396dcddSShri Abhyankar #ifndef _GNU_SOURCE
4304396dcddSShri Abhyankar #define _GNU_SOURCE
4314396dcddSShri Abhyankar #endif
4324396dcddSShri Abhyankar #include <sched.h>
4334396dcddSShri Abhyankar #endif
43461d886c9SShri Abhyankar #include <pthread.h>
43561d886c9SShri Abhyankar #endif
43661d886c9SShri Abhyankar 
437bb9aae2fSBarry Smith /*
438bb9aae2fSBarry Smith      This code is for managing thread local global variables. Each of Linux, Microsoft WINDOWS, OpenMP, and Apple OS X have
439bb9aae2fSBarry Smith    different ways to indicate this. On OS X each thread local global is accessed by using a pthread_key_t for that variable.
440bb9aae2fSBarry Smith    Thus we have functions for creating destroying and using the keys. Except for OS X these access functions merely directly
441bb9aae2fSBarry Smith    acess the thread local variable.
442bb9aae2fSBarry Smith */
443bb9aae2fSBarry Smith 
4442e43c059SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES) && !defined(PETSC_PTHREAD_LOCAL)
445*047240e1SBarry Smith typedef pthread_key_t PetscThreadKey;
446fd62bef4SShri Abhyankar /* Get the value associated with key */
447*047240e1SBarry Smith PETSC_STATIC_INLINE void* PetscThreadLocalGetValue(PetscThreadKey key)
448fd62bef4SShri Abhyankar {
449fd62bef4SShri Abhyankar   return pthread_getspecific(key);
450fd62bef4SShri Abhyankar }
451fd62bef4SShri Abhyankar 
452fd62bef4SShri Abhyankar /* Set the value for key */
453*047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalSetValue(PetscThreadKey *key,void* value)
454fd62bef4SShri Abhyankar {
455bb9aae2fSBarry Smith   pthread_setspecific(*key,(void*)value);
456fd62bef4SShri Abhyankar }
457fd62bef4SShri Abhyankar 
458fd62bef4SShri Abhyankar /* Create pthread thread local key */
459*047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalRegister(PetscThreadKey *key)
460fd62bef4SShri Abhyankar {
461fd3f9acdSBarry Smith   pthread_key_create(key,PETSC_NULL);
462fd62bef4SShri Abhyankar }
463fd62bef4SShri Abhyankar 
464fd62bef4SShri Abhyankar /* Delete pthread thread local key */
465*047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalDestroy(PetscThreadKey key)
466fd62bef4SShri Abhyankar {
467fd62bef4SShri Abhyankar   pthread_key_delete(key);
468fd62bef4SShri Abhyankar }
4692e43c059SShri Abhyankar #else
470*047240e1SBarry Smith typedef void* PetscThreadKey;
471*047240e1SBarry Smith PETSC_STATIC_INLINE void* PetscThreadLocalGetValue(PetscThreadKey key)
472fd62bef4SShri Abhyankar {
473fd3f9acdSBarry Smith   return key;
474fd62bef4SShri Abhyankar }
475fd62bef4SShri Abhyankar 
476*047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalSetValue(PetscThreadKey *key,void* value)
477bb9aae2fSBarry Smith {
478bb9aae2fSBarry Smith   *key = value;
479bb9aae2fSBarry Smith }
480fd62bef4SShri Abhyankar 
481*047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalRegister(PETSC_UNUSED PetscThreadKey *key)
482fd62bef4SShri Abhyankar {
483fd62bef4SShri Abhyankar }
484fd62bef4SShri Abhyankar 
485*047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalDestroy(PETSC_UNUSED PetscThreadKey key)
486fd62bef4SShri Abhyankar {
487fd62bef4SShri Abhyankar }
4882e43c059SShri Abhyankar #endif
4892e43c059SShri Abhyankar 
4903a40ed3dSBarry Smith /*
4913a40ed3dSBarry Smith       Allows the code to build a stack frame as it runs
4923a40ed3dSBarry Smith */
4938bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG)
4943a40ed3dSBarry Smith 
49599cd645aSJed Brown #define PETSCSTACKSIZE 64
496184914b5SBarry Smith 
4973a40ed3dSBarry Smith typedef struct  {
4980e33f6ddSBarry Smith   const char      *function[PETSCSTACKSIZE];
4990e33f6ddSBarry Smith   const char      *file[PETSCSTACKSIZE];
5000e33f6ddSBarry Smith   const char      *directory[PETSCSTACKSIZE];
501184914b5SBarry Smith         int       line[PETSCSTACKSIZE];
502a8d2bbe5SBarry Smith         PetscBool petscroutine[PETSCSTACKSIZE];
503184914b5SBarry Smith         int       currentsize;
5043a40ed3dSBarry Smith } PetscStack;
5053a40ed3dSBarry Smith 
50661d886c9SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES)
507997ce2baSShri Abhyankar #if defined(PETSC_PTHREAD_LOCAL)
508014dd563SJed Brown PETSC_EXTERN PETSC_PTHREAD_LOCAL PetscStack *petscstack;
509fe89fe5aSShri Abhyankar #else
510*047240e1SBarry Smith PETSC_EXTERN PetscThreadKey petscstack;
511fe89fe5aSShri Abhyankar #endif
5121f46d60fSShri Abhyankar #elif defined(PETSC_HAVE_OPENMP)
5131f46d60fSShri Abhyankar PETSC_EXTERN PetscStack *petscstack;
5141f46d60fSShri Abhyankar #pragma omp threadprivate(petscstack)
51561d886c9SShri Abhyankar #else
516014dd563SJed Brown PETSC_EXTERN PetscStack *petscstack;
51761d886c9SShri Abhyankar #endif
51861d886c9SShri Abhyankar 
519014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackCopy(PetscStack*,PetscStack*);
520014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackPrint(PetscStack*,FILE* fp);
521184914b5SBarry Smith 
522dbf62e16SBarry Smith PETSC_STATIC_INLINE PetscBool PetscStackActive(void)
523dbf62e16SBarry Smith {
524dbf62e16SBarry Smith   return(PetscThreadLocalGetValue(petscstack) ? PETSC_TRUE : PETSC_FALSE);
525dbf62e16SBarry Smith }
5263a40ed3dSBarry Smith 
52730de9b25SBarry Smith /*MC
52830de9b25SBarry Smith    PetscFunctionBegin - First executable line of each PETSc function
52930de9b25SBarry Smith         used for error handling.
53030de9b25SBarry Smith 
53130de9b25SBarry Smith    Synopsis:
532f2ba6396SBarry Smith    #include "petscsys.h"
53330de9b25SBarry Smith    void PetscFunctionBegin;
53430de9b25SBarry Smith 
535eca87e8dSBarry Smith    Not Collective
536eca87e8dSBarry Smith 
53730de9b25SBarry Smith    Usage:
53830de9b25SBarry Smith .vb
53930de9b25SBarry Smith      int something;
54030de9b25SBarry Smith 
54130de9b25SBarry Smith      PetscFunctionBegin;
54230de9b25SBarry Smith .ve
54330de9b25SBarry Smith 
54430de9b25SBarry Smith    Notes:
54530de9b25SBarry Smith      Not available in Fortran
54630de9b25SBarry Smith 
54730de9b25SBarry Smith    Level: developer
54830de9b25SBarry Smith 
54930de9b25SBarry Smith .seealso: PetscFunctionReturn()
55030de9b25SBarry Smith 
55130de9b25SBarry Smith .keywords: traceback, error handling
55230de9b25SBarry Smith M*/
5533a40ed3dSBarry Smith #define PetscFunctionBegin \
5548246ba0dSJed Brown   do {                                                                        \
5551f46d60fSShri Abhyankar     PetscStack* petscstackp;                                                  \
5561f46d60fSShri Abhyankar     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);          \
5571f46d60fSShri Abhyankar     if (petscstackp && (petscstackp->currentsize < PETSCSTACKSIZE)) {         \
5581f46d60fSShri Abhyankar       petscstackp->function[petscstackp->currentsize]  = PETSC_FUNCTION_NAME; \
5591f46d60fSShri Abhyankar       petscstackp->file[petscstackp->currentsize]      = __FILE__;            \
5601f46d60fSShri Abhyankar       petscstackp->directory[petscstackp->currentsize] = __SDIR__;            \
5611f46d60fSShri Abhyankar       petscstackp->line[petscstackp->currentsize]      = __LINE__;            \
562a8d2bbe5SBarry Smith       petscstackp->petscroutine[petscstackp->currentsize] = PETSC_TRUE;       \
5631f46d60fSShri Abhyankar       petscstackp->currentsize++;                                             \
56453c77d0aSJed Brown     }                                                                         \
56553c77d0aSJed Brown     PetscCheck__FUNCT__();                                                    \
5662d53ad75SBarry Smith     PetscRegister__FUNCT__();                                                 \
56753c77d0aSJed Brown   } while (0)
56853c77d0aSJed Brown 
569a8d2bbe5SBarry Smith /*MC
570a8d2bbe5SBarry Smith    PetscFunctionBeginUser - First executable line of user provided PETSc routine
571a8d2bbe5SBarry Smith 
572a8d2bbe5SBarry Smith    Synopsis:
573f2ba6396SBarry Smith    #include "petscsys.h"
574a8d2bbe5SBarry Smith    void PetscFunctionBeginUser;
575a8d2bbe5SBarry Smith 
576a8d2bbe5SBarry Smith    Not Collective
577a8d2bbe5SBarry Smith 
578a8d2bbe5SBarry Smith    Usage:
579a8d2bbe5SBarry Smith .vb
580a8d2bbe5SBarry Smith      int something;
581a8d2bbe5SBarry Smith 
582a8d2bbe5SBarry Smith      PetscFunctionBegin;
583a8d2bbe5SBarry Smith .ve
584a8d2bbe5SBarry Smith 
585a8d2bbe5SBarry Smith    Notes:
586a8d2bbe5SBarry Smith      Not available in Fortran
587a8d2bbe5SBarry Smith 
588a8d2bbe5SBarry Smith    Level: developer
589a8d2bbe5SBarry Smith 
590a8d2bbe5SBarry Smith .seealso: PetscFunctionReturn()
591a8d2bbe5SBarry Smith 
592a8d2bbe5SBarry Smith .keywords: traceback, error handling
593a8d2bbe5SBarry Smith M*/
594a8d2bbe5SBarry Smith #define PetscFunctionBeginUser \
595a8d2bbe5SBarry Smith   do {                                                                        \
596a8d2bbe5SBarry Smith     PetscStack* petscstackp;                                                  \
597a8d2bbe5SBarry Smith     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);          \
598a8d2bbe5SBarry Smith     if (petscstackp && (petscstackp->currentsize < PETSCSTACKSIZE)) {         \
599a8d2bbe5SBarry Smith       petscstackp->function[petscstackp->currentsize]  = PETSC_FUNCTION_NAME; \
600a8d2bbe5SBarry Smith       petscstackp->file[petscstackp->currentsize]      = __FILE__;            \
601a8d2bbe5SBarry Smith       petscstackp->directory[petscstackp->currentsize] = __SDIR__;            \
602a8d2bbe5SBarry Smith       petscstackp->line[petscstackp->currentsize]      = __LINE__;            \
603a8d2bbe5SBarry Smith       petscstackp->petscroutine[petscstackp->currentsize] = PETSC_FALSE;      \
604a8d2bbe5SBarry Smith       petscstackp->currentsize++;                                             \
605a8d2bbe5SBarry Smith     }                                                                         \
606a8d2bbe5SBarry Smith     PetscCheck__FUNCT__();                                                    \
607a8d2bbe5SBarry Smith     PetscRegister__FUNCT__();                                                 \
608a8d2bbe5SBarry Smith   } while (0)
609a8d2bbe5SBarry Smith 
610a8d2bbe5SBarry Smith 
6112d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
6122d53ad75SBarry Smith #include <petsc-private/petscfptimpl.h>
6132d53ad75SBarry Smith /*
6142d53ad75SBarry Smith    Registers the current function into the global function pointer to function name table
6152d53ad75SBarry Smith 
6162d53ad75SBarry Smith    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
6172d53ad75SBarry Smith */
6182d53ad75SBarry Smith #define PetscRegister__FUNCT__() do { \
6192d53ad75SBarry Smith   static PetscBool __chked = PETSC_FALSE; \
6202d53ad75SBarry Smith   if (!__chked) {\
6212d53ad75SBarry Smith   void *ptr; PetscDLSym(PETSC_NULL,__FUNCT__,&ptr);\
6222d53ad75SBarry Smith   __chked = PETSC_TRUE;\
6232d53ad75SBarry Smith   }} while (0)
6242d53ad75SBarry Smith #else
6252d53ad75SBarry Smith #define PetscRegister__FUNCT__()
6262d53ad75SBarry Smith #endif
6272d53ad75SBarry Smith 
62853c77d0aSJed Brown #define PetscCheck__FUNCT__() do { \
62953c77d0aSJed Brown     if (strcmp(PETSC_FUNCTION_NAME,__FUNCT__) && strcmp(__FUNCT__,"User provided function")) { \
63053c77d0aSJed 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); \
63153c77d0aSJed Brown     }                                                                   \
63253c77d0aSJed Brown   } while (0)
6333a40ed3dSBarry Smith 
6345cd90555SBarry Smith #define PetscStackPush(n) \
63561d886c9SShri Abhyankar   do {                                                                  \
6361f46d60fSShri Abhyankar     PetscStack * petscstackp;                                           \
6371f46d60fSShri Abhyankar     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);    \
6381f46d60fSShri Abhyankar     if (petscstackp && (petscstackp->currentsize < PETSCSTACKSIZE)) {   \
6391f46d60fSShri Abhyankar       petscstackp->function[petscstackp->currentsize]  = n;             \
6401f46d60fSShri Abhyankar       petscstackp->file[petscstackp->currentsize]      = "unknown";     \
6411f46d60fSShri Abhyankar       petscstackp->directory[petscstackp->currentsize] = "unknown";     \
6421f46d60fSShri Abhyankar       petscstackp->line[petscstackp->currentsize]      = 0;             \
6431f46d60fSShri Abhyankar       petscstackp->currentsize++;                                       \
644812af9f3SBarry Smith     } CHKMEMQ;} while (0)
6453a40ed3dSBarry Smith 
646d64ed03dSBarry Smith #define PetscStackPop \
6472f0c533eSSatish Balay   do {PetscStack* petscstackp;CHKMEMQ;                                  \
6481f46d60fSShri Abhyankar     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);    \
6491f46d60fSShri Abhyankar     if (petscstackp && petscstackp->currentsize > 0) {                  \
6501f46d60fSShri Abhyankar       petscstackp->currentsize--;                                       \
6511f46d60fSShri Abhyankar       petscstackp->function[petscstackp->currentsize]  = 0;             \
6521f46d60fSShri Abhyankar       petscstackp->file[petscstackp->currentsize]      = 0;             \
6531f46d60fSShri Abhyankar       petscstackp->directory[petscstackp->currentsize] = 0;             \
6541f46d60fSShri Abhyankar       petscstackp->line[petscstackp->currentsize]      = 0;             \
6558246ba0dSJed Brown     }} while (0)
656d64ed03dSBarry Smith 
65730de9b25SBarry Smith /*MC
65830de9b25SBarry Smith    PetscFunctionReturn - Last executable line of each PETSc function
65930de9b25SBarry Smith         used for error handling. Replaces return()
66030de9b25SBarry Smith 
66130de9b25SBarry Smith    Synopsis:
662f2ba6396SBarry Smith    #include "petscsys.h"
66330de9b25SBarry Smith    void PetscFunctionReturn(0);
66430de9b25SBarry Smith 
665eca87e8dSBarry Smith    Not Collective
666eca87e8dSBarry Smith 
66730de9b25SBarry Smith    Usage:
66830de9b25SBarry Smith .vb
66930de9b25SBarry Smith     ....
67030de9b25SBarry Smith      PetscFunctionReturn(0);
67130de9b25SBarry Smith    }
67230de9b25SBarry Smith .ve
67330de9b25SBarry Smith 
67430de9b25SBarry Smith    Notes:
67530de9b25SBarry Smith      Not available in Fortran
67630de9b25SBarry Smith 
67730de9b25SBarry Smith    Level: developer
67830de9b25SBarry Smith 
67930de9b25SBarry Smith .seealso: PetscFunctionBegin()
68030de9b25SBarry Smith 
68130de9b25SBarry Smith .keywords: traceback, error handling
68230de9b25SBarry Smith M*/
6835cd90555SBarry Smith #define PetscFunctionReturn(a) \
6848246ba0dSJed Brown   do {                                                                \
6851f46d60fSShri Abhyankar     PetscStack* petscstackp;                                          \
6861f46d60fSShri Abhyankar     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);  \
6871f46d60fSShri Abhyankar     if (petscstackp && petscstackp->currentsize > 0) {                \
6881f46d60fSShri Abhyankar       petscstackp->currentsize--;                                     \
6891f46d60fSShri Abhyankar       petscstackp->function[petscstackp->currentsize]  = 0;           \
6901f46d60fSShri Abhyankar       petscstackp->file[petscstackp->currentsize]      = 0;           \
6911f46d60fSShri Abhyankar       petscstackp->directory[petscstackp->currentsize] = 0;           \
6921f46d60fSShri Abhyankar       petscstackp->line[petscstackp->currentsize]      = 0;           \
693812af9f3SBarry Smith     }                                                                 \
6948246ba0dSJed Brown     return(a);} while (0)
695d64ed03dSBarry Smith 
696ff94ddecSSatish Balay #define PetscFunctionReturnVoid() \
6978246ba0dSJed Brown   do {                                                                \
6981f46d60fSShri Abhyankar     PetscStack* petscstackp;                                          \
6991f46d60fSShri Abhyankar     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);  \
7001f46d60fSShri Abhyankar     if (petscstackp && petscstackp->currentsize > 0) {                \
7011f46d60fSShri Abhyankar       petscstackp->currentsize--;                                     \
7021f46d60fSShri Abhyankar       petscstackp->function[petscstackp->currentsize]  = 0;           \
7031f46d60fSShri Abhyankar       petscstackp->file[petscstackp->currentsize]      = 0;           \
7041f46d60fSShri Abhyankar       petscstackp->directory[petscstackp->currentsize] = 0;           \
7051f46d60fSShri Abhyankar       petscstackp->line[petscstackp->currentsize]      = 0;           \
706812af9f3SBarry Smith     }                                                                 \
7078246ba0dSJed Brown     return;} while (0)
7086d385327SIbrahima Ba #else
7096d385327SIbrahima Ba 
7103a40ed3dSBarry Smith #define PetscFunctionBegin
7110bdf7c52SPeter Brune #define PetscFunctionBeginUser
7123a40ed3dSBarry Smith #define PetscFunctionReturn(a)  return(a)
7135665465eSBarry Smith #define PetscFunctionReturnVoid() return
714812af9f3SBarry Smith #define PetscStackPop     CHKMEMQ
715812af9f3SBarry Smith #define PetscStackPush(f) CHKMEMQ
716dbf62e16SBarry Smith #define PetscStackActive        PETSC_FALSE
7173a40ed3dSBarry Smith 
7183a40ed3dSBarry Smith #endif
7193a40ed3dSBarry Smith 
720eb6b5d47SBarry Smith /*
721eb6b5d47SBarry Smith     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
722eb6b5d47SBarry Smith 
723eb6b5d47SBarry Smith    Input Parameters:
724eb6b5d47SBarry Smith +   name - string that gives the name of the function being called
725fd3f9acdSBarry Smith -   routine - actual call to the routine, including ierr = and CHKERRQ(ierr);
726fd3f9acdSBarry Smith 
727dbf62e16SBarry Smith    Note: Often one should use PetscStackCallStandard() instead. This routine is intended for external library routines that DO NOT return error codes
728eb6b5d47SBarry Smith 
729eb6b5d47SBarry 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.
730eb6b5d47SBarry Smith 
731fd3f9acdSBarry Smith 
732fd3f9acdSBarry Smith 
733eb6b5d47SBarry Smith */
73430ecc5abSKarl Rupp #define PetscStackCall(name,routine) do { PetscStackPush(name);routine;PetscStackPop; } while(0)
735eb6b5d47SBarry Smith 
736fd3f9acdSBarry Smith /*
737fd3f9acdSBarry Smith     PetscStackCallStandard - Calls an external library routine after pushing the name of the routine on the stack.
738fd3f9acdSBarry Smith 
739fd3f9acdSBarry Smith    Input Parameters:
740fd3f9acdSBarry Smith +   func-  name of the routine
741fd3f9acdSBarry Smith -   args - arguments to the routine surrounded by ()
742fd3f9acdSBarry Smith 
743dbf62e16SBarry Smith    Notes: This is intended for external package routines that return error codes. Use PetscStackCall() for those that do not.
744dbf62e16SBarry Smith 
745dbf62e16SBarry 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.
746fd3f9acdSBarry Smith 
747fd3f9acdSBarry Smith */
748fd3f9acdSBarry Smith #define PetscStackCallStandard(func,args) do {                        \
749fd3f9acdSBarry Smith     PetscStackPush(#func);ierr = func args;PetscStackPop; if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s()",#func); \
750fd3f9acdSBarry Smith   } while (0)
751fd3f9acdSBarry Smith 
752014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackCreate(void);
753639ff905SBarry Smith PETSC_EXTERN PetscErrorCode PetscStackView(FILE*);
754014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackDestroy(void);
755014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackPublish(void);
756014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackDepublish(void);
75706d1fe2cSBarry Smith 
75806d1fe2cSBarry Smith #endif
759