xref: /petsc/include/petscerror.h (revision 28559dc8e3b01649f1bb28782e463c7c8cc5bdac)
154a8ef01SBarry Smith /*
2f621e05eSBarry Smith     Contains all error handling interfaces for PETSc.
354a8ef01SBarry Smith */
445d48df9SBarry Smith #if !defined(__PETSCERROR_H)
545d48df9SBarry Smith #define __PETSCERROR_H
66c7e564aSBarry Smith 
754a8ef01SBarry Smith /*
854a8ef01SBarry Smith    Defines the directory where the compiled source is located; used
95e97870eSBarry Smith    in printing error messages. Each makefile has an entry
105e97870eSBarry Smith    LOCDIR  =  thedirectory
11330cf3c9SBarry Smith    and bmake/common_variables includes in CCPPFLAGS -D__SDIR__=${LOCDIR}
12f621e05eSBarry Smith    which is a flag passed to the C/C++ compilers. This declaration below
13f621e05eSBarry Smith    is only needed if some code is compiled without the -D__SDIR__
1454a8ef01SBarry Smith */
15330cf3c9SBarry Smith #if !defined(__INSDIR__)
16330cf3c9SBarry Smith #define __INSDIR__ "unknowndirectory/"
1754a8ef01SBarry Smith #endif
1854a8ef01SBarry Smith 
1954a8ef01SBarry Smith /*
204f227f7cSBarry Smith    Defines the function where the compiled source is located; used
21f621e05eSBarry Smith    in printing error messages. This is defined here in case the user
22f621e05eSBarry Smith    does not declare it.
234f227f7cSBarry Smith */
244a2ae208SSatish Balay #if !defined(__FUNCT__)
25da9b6338SBarry Smith #define __FUNCT__ "User provided function"
264f227f7cSBarry Smith #endif
274f227f7cSBarry Smith 
284f227f7cSBarry Smith /*
29329ffe3dSLois Curfman McInnes      These are the generic error codes. These error codes are used
30e2d1d2b7SBarry Smith      many different places in the PETSc source code. The string versions are
310e5e90baSSatish Balay      at src/sys/error/err.c any changes here must also be made there
320f9cf654SBarry Smith      These are also define in include/finclude/petscerror.h any CHANGES here
330f9cf654SBarry Smith      must be also made there.
3445d48df9SBarry Smith 
3554a8ef01SBarry Smith */
362a6744ebSBarry Smith #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
372a6744ebSBarry Smith 
3845d48df9SBarry Smith #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
3947794344SBarry Smith #define PETSC_ERR_SUP              56   /* no support for requested operation */
40e2d1d2b7SBarry Smith #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
41e2d1d2b7SBarry Smith #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
4245d48df9SBarry Smith #define PETSC_ERR_SIG              59   /* signal received */
43f1caa5a4SBarry Smith #define PETSC_ERR_FP               72   /* floating point exception */
44a8c6a408SBarry Smith #define PETSC_ERR_COR              74   /* corrupted PETSc object */
45a8c6a408SBarry Smith #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
46329ffe3dSLois Curfman McInnes #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
47329ffe3dSLois Curfman McInnes #define PETSC_ERR_MEMC             78   /* memory corruption */
48b3cc6726SBarry Smith #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
491302d50aSBarry Smith #define PETSC_ERR_USER             83   /* user has not provided needed function */
504e2ffeddSBarry Smith #define PETSC_ERR_SYS              88   /* error in system call */
51a8b45ee7SBarry Smith #define PETSC_ERR_POINTER          70   /* pointer does not point to valid address */
5245d48df9SBarry Smith 
5345d48df9SBarry Smith #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
5445d48df9SBarry Smith #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
55a8c6a408SBarry Smith #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
5645d48df9SBarry Smith #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
5745d48df9SBarry Smith #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
584f227f7cSBarry Smith #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
594f227f7cSBarry Smith #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
606831982aSBarry Smith #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
61d252947aSBarry Smith #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
628cda6cd7SBarry Smith #define PETSC_ERR_ARG_TYPENOTSET   89   /* the type of the object has not yet been set */
63a8c6a408SBarry Smith #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
644482741eSBarry Smith #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
65958c9bccSBarry Smith #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
664f227f7cSBarry Smith 
674f227f7cSBarry Smith #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
684f227f7cSBarry Smith #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
694f227f7cSBarry Smith #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
70a8c6a408SBarry Smith #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
7145d48df9SBarry Smith 
72329ffe3dSLois Curfman McInnes #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
739e3b2f23SBarry Smith #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
7454a8ef01SBarry Smith 
753855c12bSBarry Smith #define PETSC_ERR_INT_OVERFLOW     84   /* should always be one less then the smallest value */
763855c12bSBarry Smith 
77bf3909cdSBarry Smith #define PETSC_ERR_FLOP_COUNT       90
78e113a28aSBarry Smith #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
79e113a28aSBarry Smith #define PETSC_ERR_MAX_VALUE        92  /* this is always the one more than the largest error code */
802a6744ebSBarry Smith 
8159aaf355SLisandro Dalcin #define PetscStringizeArg(a) #a
8259aaf355SLisandro Dalcin #define PetscStringize(a) PetscStringizeArg(a)
83330cf3c9SBarry Smith #define __SDIR__ PetscStringize(__INSDIR__)
84330cf3c9SBarry Smith 
85e8b7e333SSatish Balay #if defined(PETSC_USE_ERRORCHECKING)
86e8b7e333SSatish Balay 
8730de9b25SBarry Smith /*MC
8830de9b25SBarry Smith    SETERRQ - Macro that is called when an error has been detected,
8930de9b25SBarry Smith 
9030de9b25SBarry Smith    Synopsis:
91f2ba6396SBarry Smith    #include "petscsys.h"
92e7e72b3dSBarry Smith    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode errorcode,char *message)
9330de9b25SBarry Smith 
94eca87e8dSBarry Smith    Not Collective
9530de9b25SBarry Smith 
9630de9b25SBarry Smith    Input Parameters:
9730de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
9830de9b25SBarry Smith -  message - error message
9930de9b25SBarry Smith 
10030de9b25SBarry Smith   Level: beginner
10130de9b25SBarry Smith 
10230de9b25SBarry Smith    Notes:
10330de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
10430de9b25SBarry Smith 
10530de9b25SBarry Smith     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
10630de9b25SBarry Smith 
10758ebbce7SBarry Smith     In Fortran MPI_Abort() is always called
10830de9b25SBarry Smith 
10930de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
11030de9b25SBarry Smith 
11130de9b25SBarry Smith    Concepts: error^setting condition
11230de9b25SBarry Smith 
11391d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
11430de9b25SBarry Smith M*/
11553c77d0aSJed Brown #define SETERRQ(comm,n,s)              return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s)
11630de9b25SBarry Smith 
11730de9b25SBarry Smith /*MC
11830de9b25SBarry Smith    SETERRQ1 - Macro that is called when an error has been detected,
11930de9b25SBarry Smith 
12030de9b25SBarry Smith    Synopsis:
121f2ba6396SBarry Smith    #include "petscsys.h"
122e32f2f54SBarry Smith    PetscErrorCode SETERRQ1(MPI_Comm comm,PetscErrorCode errorcode,char *formatmessage,arg)
12330de9b25SBarry Smith 
124eca87e8dSBarry Smith    Not Collective
12530de9b25SBarry Smith 
12630de9b25SBarry Smith    Input Parameters:
12730de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
12830de9b25SBarry Smith .  message - error message in the printf format
12930de9b25SBarry Smith -  arg - argument (for example an integer, string or double)
13030de9b25SBarry Smith 
13130de9b25SBarry Smith   Level: beginner
13230de9b25SBarry Smith 
13330de9b25SBarry Smith    Notes:
13430de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
13530de9b25SBarry Smith 
13630de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
13730de9b25SBarry Smith 
13830de9b25SBarry Smith    Concepts: error^setting condition
13930de9b25SBarry Smith 
14091d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
14130de9b25SBarry Smith M*/
14253c77d0aSJed Brown #define SETERRQ1(comm,n,s,a1)          return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1)
14330de9b25SBarry Smith 
14430de9b25SBarry Smith /*MC
14530de9b25SBarry Smith    SETERRQ2 - Macro that is called when an error has been detected,
14630de9b25SBarry Smith 
14730de9b25SBarry Smith    Synopsis:
148f2ba6396SBarry Smith    #include "petscsys.h"
149f4442326SMatthew Knepley    PetscErrorCode SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
15030de9b25SBarry Smith 
151eca87e8dSBarry Smith    Not Collective
15230de9b25SBarry Smith 
15330de9b25SBarry Smith    Input Parameters:
15430de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
15530de9b25SBarry Smith .  message - error message in the printf format
15630de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
15730de9b25SBarry Smith -  arg2 - argument (for example an integer, string or double)
15830de9b25SBarry Smith 
15930de9b25SBarry Smith   Level: beginner
16030de9b25SBarry Smith 
16130de9b25SBarry Smith    Notes:
16230de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
16330de9b25SBarry Smith 
16430de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
16530de9b25SBarry Smith 
16630de9b25SBarry Smith    Concepts: error^setting condition
16730de9b25SBarry Smith 
1686024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
16930de9b25SBarry Smith M*/
17053c77d0aSJed Brown #define SETERRQ2(comm,n,s,a1,a2)       return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2)
17130de9b25SBarry Smith 
17230de9b25SBarry Smith /*MC
17330de9b25SBarry Smith    SETERRQ3 - Macro that is called when an error has been detected,
17430de9b25SBarry Smith 
17530de9b25SBarry Smith    Synopsis:
176f2ba6396SBarry Smith    #include "petscsys.h"
177f4442326SMatthew Knepley    PetscErrorCode SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
17830de9b25SBarry Smith 
179eca87e8dSBarry Smith    Not Collective
18030de9b25SBarry Smith 
18130de9b25SBarry Smith    Input Parameters:
18230de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
18330de9b25SBarry Smith .  message - error message in the printf format
18430de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
18530de9b25SBarry Smith .  arg2 - argument (for example an integer, string or double)
18630de9b25SBarry Smith -  arg3 - argument (for example an integer, string or double)
18730de9b25SBarry Smith 
18830de9b25SBarry Smith   Level: beginner
18930de9b25SBarry Smith 
19030de9b25SBarry Smith    Notes:
19130de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
19230de9b25SBarry Smith 
193f621e05eSBarry Smith     There are also versions for 4, 5, 6 and 7 arguments.
194f621e05eSBarry Smith 
19530de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
19630de9b25SBarry Smith 
19730de9b25SBarry Smith    Concepts: error^setting condition
19830de9b25SBarry Smith 
1996024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
20030de9b25SBarry Smith M*/
20153c77d0aSJed 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)
20230de9b25SBarry Smith 
20353c77d0aSJed 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)
20453c77d0aSJed 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)
20553c77d0aSJed 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)
20653c77d0aSJed 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)
2072f3d0e1cSMatthew 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)
20853c77d0aSJed 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)
2099a00fa46SSatish Balay 
21030de9b25SBarry Smith /*MC
21130de9b25SBarry Smith    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
21230de9b25SBarry Smith 
21330de9b25SBarry Smith    Synopsis:
214f2ba6396SBarry Smith    #include "petscsys.h"
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);}
2370298fd71SBarry Smith     where you may pass back a 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:
258f2ba6396SBarry Smith    #include "petscsys.h"
259cc26af49SMatthew Knepley    void CHKERRXX(PetscErrorCode errorcode)
260cc26af49SMatthew Knepley 
261eca87e8dSBarry Smith    Not Collective
262cc26af49SMatthew Knepley 
263cc26af49SMatthew Knepley    Input Parameters:
264cc26af49SMatthew Knepley .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
265cc26af49SMatthew Knepley 
266cc26af49SMatthew Knepley   Level: beginner
267cc26af49SMatthew Knepley 
268cc26af49SMatthew Knepley    Notes:
269cc26af49SMatthew Knepley     Once the error handler throws a ??? exception.
270cc26af49SMatthew Knepley 
271cc26af49SMatthew Knepley     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
272cc26af49SMatthew Knepley     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.
273cc26af49SMatthew Knepley 
274cc26af49SMatthew Knepley    Concepts: error^setting condition
275cc26af49SMatthew Knepley 
276cc26af49SMatthew Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKERRQ(), CHKMEMQ
277cc26af49SMatthew Knepley M*/
27853c77d0aSJed 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)
279fd705b32SMatthew Knepley 
280fd705b32SMatthew Knepley #endif
281fd705b32SMatthew Knepley 
28230de9b25SBarry Smith /*MC
28330de9b25SBarry Smith    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
28430de9b25SBarry Smith 
28530de9b25SBarry Smith    Synopsis:
286f2ba6396SBarry Smith    #include "petscsys.h"
28791d3bdf4SKris Buschelman    CHKMEMQ;
28830de9b25SBarry Smith 
289eca87e8dSBarry Smith    Not Collective
290eca87e8dSBarry Smith 
29130de9b25SBarry Smith   Level: beginner
29230de9b25SBarry Smith 
29330de9b25SBarry Smith    Notes:
294ff002950SBarry Smith     Must run with the option -malloc_debug to enable this option
29530de9b25SBarry Smith 
29630de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
29730de9b25SBarry Smith 
29830de9b25SBarry Smith     By defaults prints location where memory that is corrupted was allocated.
29930de9b25SBarry Smith 
300f621e05eSBarry Smith     Use CHKMEMA for functions that return void
301f621e05eSBarry Smith 
30230de9b25SBarry Smith    Concepts: memory corruption
30330de9b25SBarry Smith 
3046024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
305ff002950SBarry Smith           PetscMallocValidate()
30630de9b25SBarry Smith M*/
30753c77d0aSJed Brown #define CHKMEMQ do {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__);CHKERRQ(_7_ierr);} while(0)
30885614651SBarry Smith 
30953c77d0aSJed Brown #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__)
310e68848bdSBarry Smith 
311f8e50935SSatish Balay #else /* PETSC_USE_ERRORCHECKING */
312f621e05eSBarry Smith 
313f621e05eSBarry Smith /*
314e2e64c6bSBarry Smith     These are defined to be empty for when error checking is turned off, with ./configure --with-errorchecking=0
315f621e05eSBarry Smith */
316f621e05eSBarry Smith 
3174b209cf6SBarry Smith #define SETERRQ(c,n,s)
3184b209cf6SBarry Smith #define SETERRQ1(c,n,s,a1)
3194b209cf6SBarry Smith #define SETERRQ2(c,n,s,a1,a2)
3204b209cf6SBarry Smith #define SETERRQ3(c,n,s,a1,a2,a3)
3214b209cf6SBarry Smith #define SETERRQ4(c,n,s,a1,a2,a3,a4)
3224b209cf6SBarry Smith #define SETERRQ5(c,n,s,a1,a2,a3,a4,a5)
3234b209cf6SBarry Smith #define SETERRQ6(c,n,s,a1,a2,a3,a4,a5,a6)
3244ef524e9SStefano Zampini #define SETERRQ7(c,n,s,a1,a2,a3,a4,a5,a6,a7)
3254ef524e9SStefano Zampini #define SETERRQ8(c,n,s,a1,a2,a3,a4,a5,a6,a7,a8)
3264b209cf6SBarry Smith #define SETERRABORT(comm,n,s)
32785614651SBarry Smith 
3284f227f7cSBarry Smith #define CHKERRQ(n)     ;
3291ee4faa0SMatthew Knepley #define CHKERRABORT(comm,n) ;
3301ee4faa0SMatthew Knepley #define CHKERRCONTINUE(n) ;
33185614651SBarry Smith #define CHKMEMQ        ;
33285614651SBarry Smith 
3338cabf42eSMatthew G Knepley #ifdef PETSC_CLANGUAGE_CXX
3348cabf42eSMatthew G Knepley #define CHKERRXX(n) ;
3358cabf42eSMatthew G Knepley #endif
3368cabf42eSMatthew G Knepley 
337f8e50935SSatish Balay #endif /* PETSC_USE_ERRORCHECKING */
33854a8ef01SBarry Smith 
339668f157eSBarry Smith /*E
340668f157eSBarry Smith   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
341668f157eSBarry Smith 
342668f157eSBarry Smith   Level: advanced
343668f157eSBarry Smith 
344d736bfebSBarry Smith   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
345d736bfebSBarry Smith 
346668f157eSBarry Smith   Developer Notes: This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandling()
347668f157eSBarry Smith 
348d736bfebSBarry Smith .seealso: PetscError(), SETERRXX()
349668f157eSBarry Smith E*/
350d736bfebSBarry Smith typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
3514b209cf6SBarry Smith 
352014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
353014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
354014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
355014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
356014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
357014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
358014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
359014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
360014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
361014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...);
362014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
363014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
3648d359177SBarry Smith PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int,void*);
365014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
366014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
367*28559dc8SJed Brown PETSC_EXTERN PetscErrorCode PetscCheckPointerSetIntensity(PetscInt);
368329f5518SBarry Smith 
369639ff905SBarry Smith /*MC
370639ff905SBarry Smith     PetscErrorPrintf - Prints error messages.
371639ff905SBarry Smith 
372639ff905SBarry Smith    Synopsis:
373639ff905SBarry Smith     #include "petscsys.h"
374639ff905SBarry Smith      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);
375639ff905SBarry Smith 
376639ff905SBarry Smith     Not Collective
377639ff905SBarry Smith 
378639ff905SBarry Smith     Input Parameters:
379639ff905SBarry Smith .   format - the usual printf() format string
380639ff905SBarry Smith 
381639ff905SBarry Smith    Options Database Keys:
382639ff905SBarry Smith +    -error_output_stdout - cause error messages to be printed to stdout instead of the
383639ff905SBarry Smith          (default) stderr
384639ff905SBarry Smith -    -error_output_none to turn off all printing of error messages (does not change the way the
385639ff905SBarry Smith           error is handled.)
386639ff905SBarry Smith 
387639ff905SBarry Smith    Notes: Use
388639ff905SBarry Smith $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
389639ff905SBarry Smith $                        error is handled.) and
390639ff905SBarry Smith $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on
391639ff905SBarry Smith $        of you can use your own function
392639ff905SBarry Smith 
393639ff905SBarry Smith           Use
394639ff905SBarry Smith      PETSC_STDERR = FILE* obtained from a file open etc. to have stderr printed to the file.
395639ff905SBarry Smith      PETSC_STDOUT = FILE* obtained from a file open etc. to have stdout printed to the file.
396639ff905SBarry Smith 
397639ff905SBarry Smith           Use
398639ff905SBarry Smith       PetscPushErrorHandler() to provide your own error handler that determines what kind of messages to print
399639ff905SBarry Smith 
400639ff905SBarry Smith    Level: developer
401639ff905SBarry Smith 
402639ff905SBarry Smith     Fortran Note:
403639ff905SBarry Smith     This routine is not supported in Fortran.
404639ff905SBarry Smith 
405639ff905SBarry Smith     Concepts: error messages^printing
406639ff905SBarry Smith     Concepts: printing^error messages
407639ff905SBarry Smith 
408639ff905SBarry Smith .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscHelpPrintf(), PetscPrintf(), PetscErrorHandlerPush(), PetscVFPrintf(), PetscHelpPrintf()
409639ff905SBarry Smith M*/
410639ff905SBarry Smith PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[],...);
411639ff905SBarry Smith 
412329f5518SBarry Smith typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
413014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
414014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
415014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
41654a8ef01SBarry Smith 
4174396dcddSShri Abhyankar /*  Linux functions CPU_SET and others don't work if sched.h is not included before
4184396dcddSShri Abhyankar     including pthread.h. Also, these functions are active only if either _GNU_SOURCE
4194396dcddSShri Abhyankar     or __USE_GNU is not set (see /usr/include/sched.h and /usr/include/features.h), hence
4204396dcddSShri Abhyankar     set these first.
4214396dcddSShri Abhyankar */
42261d886c9SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES)
4234396dcddSShri Abhyankar #if defined(PETSC_HAVE_SCHED_H)
4244396dcddSShri Abhyankar #ifndef _GNU_SOURCE
4254396dcddSShri Abhyankar #define _GNU_SOURCE
4264396dcddSShri Abhyankar #endif
4274396dcddSShri Abhyankar #include <sched.h>
4284396dcddSShri Abhyankar #endif
42961d886c9SShri Abhyankar #include <pthread.h>
43061d886c9SShri Abhyankar #endif
43161d886c9SShri Abhyankar 
432bb9aae2fSBarry Smith /*
433bb9aae2fSBarry Smith      This code is for managing thread local global variables. Each of Linux, Microsoft WINDOWS, OpenMP, and Apple OS X have
434bb9aae2fSBarry Smith    different ways to indicate this. On OS X each thread local global is accessed by using a pthread_key_t for that variable.
435bb9aae2fSBarry Smith    Thus we have functions for creating destroying and using the keys. Except for OS X these access functions merely directly
436bb9aae2fSBarry Smith    acess the thread local variable.
437bb9aae2fSBarry Smith */
438bb9aae2fSBarry Smith 
4392e43c059SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES) && !defined(PETSC_PTHREAD_LOCAL)
440047240e1SBarry Smith typedef pthread_key_t PetscThreadKey;
441fd62bef4SShri Abhyankar /* Get the value associated with key */
442047240e1SBarry Smith PETSC_STATIC_INLINE void* PetscThreadLocalGetValue(PetscThreadKey key)
443fd62bef4SShri Abhyankar {
444fd62bef4SShri Abhyankar   return pthread_getspecific(key);
445fd62bef4SShri Abhyankar }
446fd62bef4SShri Abhyankar 
447fd62bef4SShri Abhyankar /* Set the value for key */
448047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalSetValue(PetscThreadKey *key,void* value)
449fd62bef4SShri Abhyankar {
450bb9aae2fSBarry Smith   pthread_setspecific(*key,(void*)value);
451fd62bef4SShri Abhyankar }
452fd62bef4SShri Abhyankar 
453fd62bef4SShri Abhyankar /* Create pthread thread local key */
454047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalRegister(PetscThreadKey *key)
455fd62bef4SShri Abhyankar {
4560298fd71SBarry Smith   pthread_key_create(key,NULL);
457fd62bef4SShri Abhyankar }
458fd62bef4SShri Abhyankar 
459fd62bef4SShri Abhyankar /* Delete pthread thread local key */
460047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalDestroy(PetscThreadKey key)
461fd62bef4SShri Abhyankar {
462fd62bef4SShri Abhyankar   pthread_key_delete(key);
463fd62bef4SShri Abhyankar }
4642e43c059SShri Abhyankar #else
465047240e1SBarry Smith typedef void* PetscThreadKey;
466047240e1SBarry Smith PETSC_STATIC_INLINE void* PetscThreadLocalGetValue(PetscThreadKey key)
467fd62bef4SShri Abhyankar {
468fd3f9acdSBarry Smith   return key;
469fd62bef4SShri Abhyankar }
470fd62bef4SShri Abhyankar 
471047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalSetValue(PetscThreadKey *key,void* value)
472bb9aae2fSBarry Smith {
473bb9aae2fSBarry Smith   *key = value;
474bb9aae2fSBarry Smith }
475fd62bef4SShri Abhyankar 
476047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalRegister(PETSC_UNUSED PetscThreadKey *key)
477fd62bef4SShri Abhyankar {
478fd62bef4SShri Abhyankar }
479fd62bef4SShri Abhyankar 
480047240e1SBarry Smith PETSC_STATIC_INLINE void PetscThreadLocalDestroy(PETSC_UNUSED PetscThreadKey key)
481fd62bef4SShri Abhyankar {
482fd62bef4SShri Abhyankar }
4832e43c059SShri Abhyankar #endif
4842e43c059SShri Abhyankar 
4853a40ed3dSBarry Smith /*
4863a40ed3dSBarry Smith       Allows the code to build a stack frame as it runs
4873a40ed3dSBarry Smith */
4883a40ed3dSBarry Smith 
48999cd645aSJed Brown #define PETSCSTACKSIZE 64
490184914b5SBarry Smith 
4913a40ed3dSBarry Smith typedef struct  {
4920e33f6ddSBarry Smith   const char      *function[PETSCSTACKSIZE];
4930e33f6ddSBarry Smith   const char      *file[PETSCSTACKSIZE];
4940e33f6ddSBarry Smith   const char      *directory[PETSCSTACKSIZE];
495184914b5SBarry Smith         int       line[PETSCSTACKSIZE];
496a8d2bbe5SBarry Smith         PetscBool petscroutine[PETSCSTACKSIZE];
497184914b5SBarry Smith         int       currentsize;
498a2f94806SJed Brown         int       hotdepth;
4993a40ed3dSBarry Smith } PetscStack;
5003a40ed3dSBarry Smith 
50161d886c9SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES)
502997ce2baSShri Abhyankar #if defined(PETSC_PTHREAD_LOCAL)
503014dd563SJed Brown PETSC_EXTERN PETSC_PTHREAD_LOCAL PetscStack *petscstack;
504fe89fe5aSShri Abhyankar #else
505047240e1SBarry Smith PETSC_EXTERN PetscThreadKey petscstack;
506fe89fe5aSShri Abhyankar #endif
5071f46d60fSShri Abhyankar #elif defined(PETSC_HAVE_OPENMP)
5081f46d60fSShri Abhyankar PETSC_EXTERN PetscStack *petscstack;
5091f46d60fSShri Abhyankar #pragma omp threadprivate(petscstack)
51061d886c9SShri Abhyankar #else
511014dd563SJed Brown PETSC_EXTERN PetscStack *petscstack;
51261d886c9SShri Abhyankar #endif
51361d886c9SShri Abhyankar 
514014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackCopy(PetscStack*,PetscStack*);
515014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackPrint(PetscStack*,FILE* fp);
516184914b5SBarry Smith 
5172d7c6352SJed Brown #if defined(PETSC_USE_DEBUG)
518dbf62e16SBarry Smith PETSC_STATIC_INLINE PetscBool PetscStackActive(void)
519dbf62e16SBarry Smith {
520dbf62e16SBarry Smith   return(PetscThreadLocalGetValue(petscstack) ? PETSC_TRUE : PETSC_FALSE);
521dbf62e16SBarry Smith }
5223a40ed3dSBarry Smith 
523441dd030SJed Brown /* Stack handling is based on the following two "NoCheck" macros.  These should only be called directly by other error
524441dd030SJed Brown  * handling macros.  We record the line of the call, which may or may not be the location of the definition.  But is at
525441dd030SJed Brown  * least more useful than "unknown" because it can distinguish multiple calls from the same function.
526441dd030SJed Brown  */
527441dd030SJed Brown 
528a2f94806SJed Brown #define PetscStackPushNoCheck(funct,petsc_routine,hot)                        \
529441dd030SJed Brown   do {                                                                        \
530441dd030SJed Brown     PetscStack* petscstackp;                                                  \
531441dd030SJed Brown     PetscStackAMSTakeAccess();                                                \
532441dd030SJed Brown     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);          \
533441dd030SJed Brown     if (petscstackp && (petscstackp->currentsize < PETSCSTACKSIZE)) {         \
534441dd030SJed Brown       petscstackp->function[petscstackp->currentsize]  = funct;               \
535441dd030SJed Brown       petscstackp->file[petscstackp->currentsize]      = __FILE__;            \
536441dd030SJed Brown       petscstackp->directory[petscstackp->currentsize] = __SDIR__;            \
537441dd030SJed Brown       petscstackp->line[petscstackp->currentsize]      = __LINE__;            \
538441dd030SJed Brown       petscstackp->petscroutine[petscstackp->currentsize] = petsc_routine;    \
539441dd030SJed Brown       petscstackp->currentsize++;                                             \
540441dd030SJed Brown     }                                                                         \
541a2f94806SJed Brown     if (petscstackp) {                                                        \
542a2f94806SJed Brown       petscstackp->hotdepth += (hot || petscstackp->hotdepth);                \
543a2f94806SJed Brown     }                                                                         \
544441dd030SJed Brown     PetscStackAMSGrantAccess();                                               \
545441dd030SJed Brown   } while (0)
546441dd030SJed Brown 
547441dd030SJed Brown #define PetscStackPopNoCheck                                            \
548441dd030SJed Brown   do {PetscStack* petscstackp;                                          \
549441dd030SJed Brown     PetscStackAMSTakeAccess();                                          \
550441dd030SJed Brown     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);    \
551441dd030SJed Brown     if (petscstackp && petscstackp->currentsize > 0) {                  \
552441dd030SJed Brown       petscstackp->currentsize--;                                       \
553441dd030SJed Brown       petscstackp->function[petscstackp->currentsize]  = 0;             \
554441dd030SJed Brown       petscstackp->file[petscstackp->currentsize]      = 0;             \
555441dd030SJed Brown       petscstackp->directory[petscstackp->currentsize] = 0;             \
556441dd030SJed Brown       petscstackp->line[petscstackp->currentsize]      = 0;             \
557441dd030SJed Brown       petscstackp->petscroutine[petscstackp->currentsize] = PETSC_FALSE;\
558441dd030SJed Brown     }                                                                   \
559a2f94806SJed Brown     if (petscstackp) {                                                  \
560a2f94806SJed Brown       petscstackp->hotdepth = PetscMax(petscstackp->hotdepth-1,0);      \
561a2f94806SJed Brown     }                                                                   \
562441dd030SJed Brown     PetscStackAMSGrantAccess();                                         \
563441dd030SJed Brown   } while (0)
564441dd030SJed Brown 
56530de9b25SBarry Smith /*MC
56630de9b25SBarry Smith    PetscFunctionBegin - First executable line of each PETSc function
56730de9b25SBarry Smith         used for error handling.
56830de9b25SBarry Smith 
56930de9b25SBarry Smith    Synopsis:
570f2ba6396SBarry Smith    #include "petscsys.h"
57130de9b25SBarry Smith    void PetscFunctionBegin;
57230de9b25SBarry Smith 
573eca87e8dSBarry Smith    Not Collective
574eca87e8dSBarry Smith 
57530de9b25SBarry Smith    Usage:
57630de9b25SBarry Smith .vb
57730de9b25SBarry Smith      int something;
57830de9b25SBarry Smith 
57930de9b25SBarry Smith      PetscFunctionBegin;
58030de9b25SBarry Smith .ve
58130de9b25SBarry Smith 
58230de9b25SBarry Smith    Notes:
58330de9b25SBarry Smith      Not available in Fortran
58430de9b25SBarry Smith 
58530de9b25SBarry Smith    Level: developer
58630de9b25SBarry Smith 
58730de9b25SBarry Smith .seealso: PetscFunctionReturn()
58830de9b25SBarry Smith 
58930de9b25SBarry Smith .keywords: traceback, error handling
59030de9b25SBarry Smith M*/
591441dd030SJed Brown #define PetscFunctionBegin do {                                        \
592a2f94806SJed Brown     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_TRUE,PETSC_FALSE); \
593a2f94806SJed Brown     PetscCheck__FUNCT__();                                             \
594a2f94806SJed Brown     PetscRegister__FUNCT__();                                          \
595a2f94806SJed Brown   } while (0)
596a2f94806SJed Brown 
597a2f94806SJed Brown /*MC
598a2f94806SJed Brown    PetscFunctionBeginHot - Substitute for PetscFunctionBegin to be used in functions that are called in
599a2f94806SJed Brown    performance-critical circumstances.  Use of this function allows for lighter profiling by default.
600a2f94806SJed Brown 
601a2f94806SJed Brown    Synopsis:
602a2f94806SJed Brown    #include "petscsys.h"
603a2f94806SJed Brown    void PetscFunctionBeginHot;
604a2f94806SJed Brown 
605a2f94806SJed Brown    Not Collective
606a2f94806SJed Brown 
607a2f94806SJed Brown    Usage:
608a2f94806SJed Brown .vb
609a2f94806SJed Brown      int something;
610a2f94806SJed Brown 
611a2f94806SJed Brown      PetscFunctionBeginHot;
612a2f94806SJed Brown .ve
613a2f94806SJed Brown 
614a2f94806SJed Brown    Notes:
615a2f94806SJed Brown      Not available in Fortran
616a2f94806SJed Brown 
617a2f94806SJed Brown    Level: developer
618a2f94806SJed Brown 
619a2f94806SJed Brown .seealso: PetscFunctionBegin, PetscFunctionReturn()
620a2f94806SJed Brown 
621a2f94806SJed Brown .keywords: traceback, error handling
622a2f94806SJed Brown M*/
623a2f94806SJed Brown #define PetscFunctionBeginHot do {                                     \
624a2f94806SJed Brown     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_TRUE,PETSC_TRUE);  \
62553c77d0aSJed Brown     PetscCheck__FUNCT__();                                             \
6262d53ad75SBarry Smith     PetscRegister__FUNCT__();                                          \
62753c77d0aSJed Brown   } while (0)
62853c77d0aSJed Brown 
629a8d2bbe5SBarry Smith /*MC
630a8d2bbe5SBarry Smith    PetscFunctionBeginUser - First executable line of user provided PETSc routine
631a8d2bbe5SBarry Smith 
632a8d2bbe5SBarry Smith    Synopsis:
633f2ba6396SBarry Smith    #include "petscsys.h"
634a8d2bbe5SBarry Smith    void PetscFunctionBeginUser;
635a8d2bbe5SBarry Smith 
636a8d2bbe5SBarry Smith    Not Collective
637a8d2bbe5SBarry Smith 
638a8d2bbe5SBarry Smith    Usage:
639a8d2bbe5SBarry Smith .vb
640a8d2bbe5SBarry Smith      int something;
641a8d2bbe5SBarry Smith 
642a8d2bbe5SBarry Smith      PetscFunctionBegin;
643a8d2bbe5SBarry Smith .ve
644a8d2bbe5SBarry Smith 
645a8d2bbe5SBarry Smith    Notes:
646a8d2bbe5SBarry Smith      Not available in Fortran
647a8d2bbe5SBarry Smith 
648a2f94806SJed Brown    Level: intermediate
649a8d2bbe5SBarry Smith 
650a2f94806SJed Brown .seealso: PetscFunctionReturn(), PetscFunctionBegin, PetscFunctionBeginHot
651a8d2bbe5SBarry Smith 
652a8d2bbe5SBarry Smith .keywords: traceback, error handling
653a8d2bbe5SBarry Smith M*/
654a8d2bbe5SBarry Smith #define PetscFunctionBeginUser                                          \
655a8d2bbe5SBarry Smith   do {                                                                  \
656a2f94806SJed Brown     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_FALSE,PETSC_FALSE); \
657a8d2bbe5SBarry Smith     PetscCheck__FUNCT__();                                              \
658a8d2bbe5SBarry Smith     PetscRegister__FUNCT__();                                           \
659a8d2bbe5SBarry Smith   } while (0)
660a8d2bbe5SBarry Smith 
661a8d2bbe5SBarry Smith 
6622d53ad75SBarry Smith #if defined(PETSC_SERIALIZE_FUNCTIONS)
6632d53ad75SBarry Smith #include <petsc-private/petscfptimpl.h>
6642d53ad75SBarry Smith /*
6652d53ad75SBarry Smith    Registers the current function into the global function pointer to function name table
6662d53ad75SBarry Smith 
6672d53ad75SBarry Smith    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
6682d53ad75SBarry Smith */
6692d53ad75SBarry Smith #define PetscRegister__FUNCT__() do { \
6702d53ad75SBarry Smith   static PetscBool __chked = PETSC_FALSE; \
6712d53ad75SBarry Smith   if (!__chked) {\
6720298fd71SBarry Smith   void *ptr; PetscDLSym(NULL,__FUNCT__,&ptr);\
6732d53ad75SBarry Smith   __chked = PETSC_TRUE;\
6742d53ad75SBarry Smith   }} while (0)
6752d53ad75SBarry Smith #else
6762d53ad75SBarry Smith #define PetscRegister__FUNCT__()
6772d53ad75SBarry Smith #endif
6782d53ad75SBarry Smith 
679573b0fb4SBarry Smith #define PetscCheck__FUNCT__() do { PetscBool _sc1,_sc2;                  \
680573b0fb4SBarry Smith     PetscStrcmpNoError(PETSC_FUNCTION_NAME,__FUNCT__,&_sc1);\
681573b0fb4SBarry Smith     PetscStrcmpNoError(__FUNCT__,"User provided function",&_sc2);\
682573b0fb4SBarry Smith     if (!_sc1 && !_sc2) { \
683573b0fb4SBarry Smith       printf("%s%s:%d: __FUNCT__=\"%s\" does not agree with %s=\"%s\"\n",__SDIR__,__FILE__,__LINE__,__FUNCT__,PetscStringize(PETSC_FUNCTION_NAME),PETSC_FUNCTION_NAME); \
68453c77d0aSJed Brown     }                                                                   \
68553c77d0aSJed Brown   } while (0)
6863a40ed3dSBarry Smith 
6875cd90555SBarry Smith #define PetscStackPush(n) \
68861d886c9SShri Abhyankar   do {                                                                  \
689a2f94806SJed Brown     PetscStackPushNoCheck(n,PETSC_FALSE,PETSC_FALSE);                   \
69015681b3cSBarry Smith     CHKMEMQ;                                                            \
69115681b3cSBarry Smith   } while (0)
6923a40ed3dSBarry Smith 
693d64ed03dSBarry Smith #define PetscStackPop                           \
694441dd030SJed Brown     do {                                        \
695441dd030SJed Brown       CHKMEMQ;                                  \
696441dd030SJed Brown       PetscStackPopNoCheck;                     \
69715681b3cSBarry Smith     } while (0)
698d64ed03dSBarry Smith 
69930de9b25SBarry Smith /*MC
70030de9b25SBarry Smith    PetscFunctionReturn - Last executable line of each PETSc function
70130de9b25SBarry Smith         used for error handling. Replaces return()
70230de9b25SBarry Smith 
70330de9b25SBarry Smith    Synopsis:
704f2ba6396SBarry Smith    #include "petscsys.h"
70530de9b25SBarry Smith    void PetscFunctionReturn(0);
70630de9b25SBarry Smith 
707eca87e8dSBarry Smith    Not Collective
708eca87e8dSBarry Smith 
70930de9b25SBarry Smith    Usage:
71030de9b25SBarry Smith .vb
71130de9b25SBarry Smith     ....
71230de9b25SBarry Smith      PetscFunctionReturn(0);
71330de9b25SBarry Smith    }
71430de9b25SBarry Smith .ve
71530de9b25SBarry Smith 
71630de9b25SBarry Smith    Notes:
71730de9b25SBarry Smith      Not available in Fortran
71830de9b25SBarry Smith 
71930de9b25SBarry Smith    Level: developer
72030de9b25SBarry Smith 
72130de9b25SBarry Smith .seealso: PetscFunctionBegin()
72230de9b25SBarry Smith 
72330de9b25SBarry Smith .keywords: traceback, error handling
72430de9b25SBarry Smith M*/
7255cd90555SBarry Smith #define PetscFunctionReturn(a) \
7268246ba0dSJed Brown   do {                                                                \
727441dd030SJed Brown     PetscStackPopNoCheck;                                             \
7288246ba0dSJed Brown     return(a);} while (0)
729d64ed03dSBarry Smith 
730ff94ddecSSatish Balay #define PetscFunctionReturnVoid() \
7318246ba0dSJed Brown   do {                                                                \
732441dd030SJed Brown     PetscStackPopNoCheck;                                             \
7338246ba0dSJed Brown     return;} while (0)
73476386721SLisandro Dalcin 
7356d385327SIbrahima Ba #else
7366d385327SIbrahima Ba 
737c82b4e47SJed Brown PETSC_STATIC_INLINE PetscBool PetscStackActive(void) {return PETSC_FALSE;}
738a2f94806SJed Brown #define PetscStackPushNoCheck(funct,petsc_routine,hot) do {} while (0)
739441dd030SJed Brown #define PetscStackPopNoCheck                           do {} while (0)
7403a40ed3dSBarry Smith #define PetscFunctionBegin
7410bdf7c52SPeter Brune #define PetscFunctionBeginUser
742a2f94806SJed Brown #define PetscFunctionBeginHot
7433a40ed3dSBarry Smith #define PetscFunctionReturn(a)    return(a)
7445665465eSBarry Smith #define PetscFunctionReturnVoid() return
745812af9f3SBarry Smith #define PetscStackPop             CHKMEMQ
746812af9f3SBarry Smith #define PetscStackPush(f)         CHKMEMQ
7473a40ed3dSBarry Smith 
7483a40ed3dSBarry Smith #endif
7493a40ed3dSBarry Smith 
750eb6b5d47SBarry Smith /*
751eb6b5d47SBarry Smith     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
752eb6b5d47SBarry Smith 
753eb6b5d47SBarry Smith    Input Parameters:
754eb6b5d47SBarry Smith +   name - string that gives the name of the function being called
755fd3f9acdSBarry Smith -   routine - actual call to the routine, including ierr = and CHKERRQ(ierr);
756fd3f9acdSBarry Smith 
757dbf62e16SBarry Smith    Note: Often one should use PetscStackCallStandard() instead. This routine is intended for external library routines that DO NOT return error codes
758eb6b5d47SBarry Smith 
759eb6b5d47SBarry 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.
760eb6b5d47SBarry Smith 
761fd3f9acdSBarry Smith 
762fd3f9acdSBarry Smith 
763eb6b5d47SBarry Smith */
76430ecc5abSKarl Rupp #define PetscStackCall(name,routine) do { PetscStackPush(name);routine;PetscStackPop; } while(0)
765eb6b5d47SBarry Smith 
766fd3f9acdSBarry Smith /*
767fd3f9acdSBarry Smith     PetscStackCallStandard - Calls an external library routine after pushing the name of the routine on the stack.
768fd3f9acdSBarry Smith 
769fd3f9acdSBarry Smith    Input Parameters:
770fd3f9acdSBarry Smith +   func-  name of the routine
771fd3f9acdSBarry Smith -   args - arguments to the routine surrounded by ()
772fd3f9acdSBarry Smith 
773dbf62e16SBarry Smith    Notes: This is intended for external package routines that return error codes. Use PetscStackCall() for those that do not.
774dbf62e16SBarry Smith 
775dbf62e16SBarry 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.
776fd3f9acdSBarry Smith 
777fd3f9acdSBarry Smith */
778fd3f9acdSBarry Smith #define PetscStackCallStandard(func,args) do {                        \
779fd3f9acdSBarry Smith     PetscStackPush(#func);ierr = func args;PetscStackPop; if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s()",#func); \
780fd3f9acdSBarry Smith   } while (0)
781fd3f9acdSBarry Smith 
782014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackCreate(void);
783639ff905SBarry Smith PETSC_EXTERN PetscErrorCode PetscStackView(FILE*);
784014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackDestroy(void);
78506d1fe2cSBarry Smith 
78606d1fe2cSBarry Smith #endif
787