xref: /petsc/include/petscerror.h (revision 014dd563d73e9fc78d056590fa6cf997782bf92d)
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 */
5645d48df9SBarry Smith 
5745d48df9SBarry Smith #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
5845d48df9SBarry Smith #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
59a8c6a408SBarry Smith #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
6045d48df9SBarry Smith #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
6145d48df9SBarry Smith #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
624f227f7cSBarry Smith #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
634f227f7cSBarry Smith #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
646831982aSBarry Smith #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
65d252947aSBarry Smith #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
668cda6cd7SBarry Smith #define PETSC_ERR_ARG_TYPENOTSET   89   /* the type of the object has not yet been set */
67a8c6a408SBarry Smith #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
684482741eSBarry Smith #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
69958c9bccSBarry Smith #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
704f227f7cSBarry Smith 
714f227f7cSBarry Smith #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
724f227f7cSBarry Smith #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
734f227f7cSBarry Smith #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
74a8c6a408SBarry Smith #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
7545d48df9SBarry Smith 
76329ffe3dSLois Curfman McInnes #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
779e3b2f23SBarry Smith #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
7854a8ef01SBarry Smith 
793855c12bSBarry Smith #define PETSC_ERR_INT_OVERFLOW     84   /* should always be one less then the smallest value */
803855c12bSBarry Smith 
81bf3909cdSBarry Smith #define PETSC_ERR_FLOP_COUNT       90
82e113a28aSBarry Smith #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
83e113a28aSBarry Smith #define PETSC_ERR_MAX_VALUE        92  /* this is always the one more than the largest error code */
842a6744ebSBarry Smith 
8559aaf355SLisandro Dalcin #define PetscStringizeArg(a) #a
8659aaf355SLisandro Dalcin #define PetscStringize(a) PetscStringizeArg(a)
87330cf3c9SBarry Smith #define __SDIR__ PetscStringize(__INSDIR__)
88330cf3c9SBarry Smith 
89e8b7e333SSatish Balay #if defined(PETSC_USE_ERRORCHECKING)
90e8b7e333SSatish Balay 
9130de9b25SBarry Smith /*MC
9230de9b25SBarry Smith    SETERRQ - Macro that is called when an error has been detected,
9330de9b25SBarry Smith 
9430de9b25SBarry Smith    Synopsis:
95e7e72b3dSBarry Smith    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode errorcode,char *message)
9630de9b25SBarry Smith 
97eca87e8dSBarry Smith    Not Collective
9830de9b25SBarry Smith 
9930de9b25SBarry Smith    Input Parameters:
10030de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
10130de9b25SBarry Smith -  message - error message
10230de9b25SBarry Smith 
10330de9b25SBarry Smith   Level: beginner
10430de9b25SBarry Smith 
10530de9b25SBarry Smith    Notes:
10630de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
10730de9b25SBarry Smith 
10830de9b25SBarry Smith     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
10930de9b25SBarry Smith 
11058ebbce7SBarry Smith     In Fortran MPI_Abort() is always called
11130de9b25SBarry Smith 
11230de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
11330de9b25SBarry Smith 
11430de9b25SBarry Smith    Concepts: error^setting condition
11530de9b25SBarry Smith 
11691d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
11730de9b25SBarry Smith M*/
11853c77d0aSJed Brown #define SETERRQ(comm,n,s)              return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s)
11930de9b25SBarry Smith 
12030de9b25SBarry Smith /*MC
12130de9b25SBarry Smith    SETERRQ1 - Macro that is called when an error has been detected,
12230de9b25SBarry Smith 
12330de9b25SBarry Smith    Synopsis:
124e32f2f54SBarry Smith    PetscErrorCode SETERRQ1(MPI_Comm comm,PetscErrorCode errorcode,char *formatmessage,arg)
12530de9b25SBarry Smith 
126eca87e8dSBarry Smith    Not Collective
12730de9b25SBarry Smith 
12830de9b25SBarry Smith    Input Parameters:
12930de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
13030de9b25SBarry Smith .  message - error message in the printf format
13130de9b25SBarry Smith -  arg - argument (for example an integer, string or double)
13230de9b25SBarry Smith 
13330de9b25SBarry Smith   Level: beginner
13430de9b25SBarry Smith 
13530de9b25SBarry Smith    Notes:
13630de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
13730de9b25SBarry Smith 
13830de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
13930de9b25SBarry Smith 
14030de9b25SBarry Smith    Concepts: error^setting condition
14130de9b25SBarry Smith 
14291d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
14330de9b25SBarry Smith M*/
14453c77d0aSJed Brown #define SETERRQ1(comm,n,s,a1)          return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1)
14530de9b25SBarry Smith 
14630de9b25SBarry Smith /*MC
14730de9b25SBarry Smith    SETERRQ2 - Macro that is called when an error has been detected,
14830de9b25SBarry Smith 
14930de9b25SBarry Smith    Synopsis:
150f4442326SMatthew Knepley    PetscErrorCode SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
15130de9b25SBarry Smith 
152eca87e8dSBarry Smith    Not Collective
15330de9b25SBarry Smith 
15430de9b25SBarry Smith    Input Parameters:
15530de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
15630de9b25SBarry Smith .  message - error message in the printf format
15730de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
15830de9b25SBarry Smith -  arg2 - argument (for example an integer, string or double)
15930de9b25SBarry Smith 
16030de9b25SBarry Smith   Level: beginner
16130de9b25SBarry Smith 
16230de9b25SBarry Smith    Notes:
16330de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
16430de9b25SBarry Smith 
16530de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
16630de9b25SBarry Smith 
16730de9b25SBarry Smith    Concepts: error^setting condition
16830de9b25SBarry Smith 
1696024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
17030de9b25SBarry Smith M*/
17153c77d0aSJed Brown #define SETERRQ2(comm,n,s,a1,a2)       return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2)
17230de9b25SBarry Smith 
17330de9b25SBarry Smith /*MC
17430de9b25SBarry Smith    SETERRQ3 - Macro that is called when an error has been detected,
17530de9b25SBarry Smith 
17630de9b25SBarry Smith    Synopsis:
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:
214f4442326SMatthew Knepley    PetscErrorCode CHKERRQ(PetscErrorCode errorcode)
21530de9b25SBarry Smith 
216eca87e8dSBarry Smith    Not Collective
21730de9b25SBarry Smith 
21830de9b25SBarry Smith    Input Parameters:
21930de9b25SBarry Smith .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
22030de9b25SBarry Smith 
22130de9b25SBarry Smith   Level: beginner
22230de9b25SBarry Smith 
22330de9b25SBarry Smith    Notes:
22430de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
22530de9b25SBarry Smith 
22630de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
2275324ea47SKris Buschelman 
228fcecf507SKris Buschelman     CHKERRQ(n) is fundamentally a macro replacement for
2295324ea47SKris Buschelman          if (n) return(PetscError(...,n,...));
2305324ea47SKris Buschelman 
2315324ea47SKris Buschelman     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
2325324ea47SKris Buschelman     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
2335324ea47SKris Buschelman     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
23458ebbce7SBarry Smith     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
2355324ea47SKris Buschelman          if (n) {PetscError(....); return(YourReturnType);}
23658ebbce7SBarry Smith     where you may pass back a PETSC_NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
23758ebbce7SBarry Smith     MPI_Abort() returned immediately.
23858ebbce7SBarry Smith 
23958ebbce7SBarry Smith     In Fortran MPI_Abort() is always called
24030de9b25SBarry Smith 
24130de9b25SBarry Smith    Concepts: error^setting condition
24230de9b25SBarry Smith 
24391d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
24430de9b25SBarry Smith M*/
24553c77d0aSJed 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)
24630de9b25SBarry Smith 
24753c77d0aSJed 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)
24853c77d0aSJed 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)
24953c77d0aSJed Brown #define CHKERRCONTINUE(n)      do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");}} while (0)
25085614651SBarry Smith 
251fd705b32SMatthew Knepley #ifdef PETSC_CLANGUAGE_CXX
252fd705b32SMatthew Knepley 
253cc26af49SMatthew Knepley /*MC
254cc26af49SMatthew Knepley    CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
255cc26af49SMatthew Knepley 
256cc26af49SMatthew Knepley    Synopsis:
257cc26af49SMatthew Knepley    void CHKERRXX(PetscErrorCode errorcode)
258cc26af49SMatthew Knepley 
259eca87e8dSBarry Smith    Not Collective
260cc26af49SMatthew Knepley 
261cc26af49SMatthew Knepley    Input Parameters:
262cc26af49SMatthew Knepley .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
263cc26af49SMatthew Knepley 
264cc26af49SMatthew Knepley   Level: beginner
265cc26af49SMatthew Knepley 
266cc26af49SMatthew Knepley    Notes:
267cc26af49SMatthew Knepley     Once the error handler throws a ??? exception.
268cc26af49SMatthew Knepley 
269cc26af49SMatthew Knepley     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
270cc26af49SMatthew Knepley     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.
271cc26af49SMatthew Knepley 
272cc26af49SMatthew Knepley    Concepts: error^setting condition
273cc26af49SMatthew Knepley 
274cc26af49SMatthew Knepley .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKERRQ(), CHKMEMQ
275cc26af49SMatthew Knepley M*/
27653c77d0aSJed 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)
277fd705b32SMatthew Knepley 
278fd705b32SMatthew Knepley #endif
279fd705b32SMatthew Knepley 
28030de9b25SBarry Smith /*MC
28130de9b25SBarry Smith    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
28230de9b25SBarry Smith 
28330de9b25SBarry Smith    Synopsis:
28491d3bdf4SKris Buschelman    CHKMEMQ;
28530de9b25SBarry Smith 
286eca87e8dSBarry Smith    Not Collective
287eca87e8dSBarry Smith 
28830de9b25SBarry Smith   Level: beginner
28930de9b25SBarry Smith 
29030de9b25SBarry Smith    Notes:
291ff002950SBarry Smith     Must run with the option -malloc_debug to enable this option
29230de9b25SBarry Smith 
29330de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
29430de9b25SBarry Smith 
29530de9b25SBarry Smith     By defaults prints location where memory that is corrupted was allocated.
29630de9b25SBarry Smith 
297f621e05eSBarry Smith     Use CHKMEMA for functions that return void
298f621e05eSBarry Smith 
29930de9b25SBarry Smith    Concepts: memory corruption
30030de9b25SBarry Smith 
3016024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
302ff002950SBarry Smith           PetscMallocValidate()
30330de9b25SBarry Smith M*/
30453c77d0aSJed Brown #define CHKMEMQ do {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__);CHKERRQ(_7_ierr);} while(0)
30585614651SBarry Smith 
30653c77d0aSJed Brown #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__)
307e68848bdSBarry Smith 
308f8e50935SSatish Balay #else /* PETSC_USE_ERRORCHECKING */
309f621e05eSBarry Smith 
310f621e05eSBarry Smith /*
311e2e64c6bSBarry Smith     These are defined to be empty for when error checking is turned off, with ./configure --with-errorchecking=0
312f621e05eSBarry Smith */
313f621e05eSBarry Smith 
3144b209cf6SBarry Smith #define SETERRQ(c,n,s)
3154b209cf6SBarry Smith #define SETERRQ1(c,n,s,a1)
3164b209cf6SBarry Smith #define SETERRQ2(c,n,s,a1,a2)
3174b209cf6SBarry Smith #define SETERRQ3(c,n,s,a1,a2,a3)
3184b209cf6SBarry Smith #define SETERRQ4(c,n,s,a1,a2,a3,a4)
3194b209cf6SBarry Smith #define SETERRQ5(c,n,s,a1,a2,a3,a4,a5)
3204b209cf6SBarry Smith #define SETERRQ6(c,n,s,a1,a2,a3,a4,a5,a6)
3214ef524e9SStefano Zampini #define SETERRQ7(c,n,s,a1,a2,a3,a4,a5,a6,a7)
3224ef524e9SStefano Zampini #define SETERRQ8(c,n,s,a1,a2,a3,a4,a5,a6,a7,a8)
3234b209cf6SBarry Smith #define SETERRABORT(comm,n,s)
32485614651SBarry Smith 
3254f227f7cSBarry Smith #define CHKERRQ(n)     ;
3261ee4faa0SMatthew Knepley #define CHKERRABORT(comm,n) ;
3271ee4faa0SMatthew Knepley #define CHKERRCONTINUE(n) ;
32885614651SBarry Smith #define CHKMEMQ        ;
32985614651SBarry Smith 
3308cabf42eSMatthew G Knepley #ifdef PETSC_CLANGUAGE_CXX
3318cabf42eSMatthew G Knepley #define CHKERRXX(n) ;
3328cabf42eSMatthew G Knepley #endif
3338cabf42eSMatthew G Knepley 
334f8e50935SSatish Balay #endif /* PETSC_USE_ERRORCHECKING */
33554a8ef01SBarry Smith 
336668f157eSBarry Smith /*E
337668f157eSBarry Smith   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
338668f157eSBarry Smith 
339668f157eSBarry Smith   Level: advanced
340668f157eSBarry Smith 
341d736bfebSBarry Smith   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
342d736bfebSBarry Smith 
343668f157eSBarry Smith   Developer Notes: This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandling()
344668f157eSBarry Smith 
345d736bfebSBarry Smith .seealso: PetscError(), SETERRXX()
346668f157eSBarry Smith E*/
347d736bfebSBarry Smith typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
3484b209cf6SBarry Smith 
349*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
350*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
351*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
352*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
353*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
354*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
355*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
356*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
357*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
358*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...);
359*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
360*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
361*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscDefaultSignalHandler(int,void*);
362*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
363*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
364329f5518SBarry Smith 
365329f5518SBarry Smith typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
366*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
367*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
368*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
36954a8ef01SBarry Smith 
3704396dcddSShri Abhyankar /*  Linux functions CPU_SET and others don't work if sched.h is not included before
3714396dcddSShri Abhyankar     including pthread.h. Also, these functions are active only if either _GNU_SOURCE
3724396dcddSShri Abhyankar     or __USE_GNU is not set (see /usr/include/sched.h and /usr/include/features.h), hence
3734396dcddSShri Abhyankar     set these first.
3744396dcddSShri Abhyankar */
37561d886c9SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES)
3764396dcddSShri Abhyankar #if defined(PETSC_HAVE_SCHED_H)
3774396dcddSShri Abhyankar #ifndef _GNU_SOURCE
3784396dcddSShri Abhyankar #define _GNU_SOURCE
3794396dcddSShri Abhyankar #endif
3804396dcddSShri Abhyankar #include <sched.h>
3814396dcddSShri Abhyankar #endif
38261d886c9SShri Abhyankar #include <pthread.h>
38361d886c9SShri Abhyankar #endif
38461d886c9SShri Abhyankar 
3853a40ed3dSBarry Smith /*
3863a40ed3dSBarry Smith       Allows the code to build a stack frame as it runs
3873a40ed3dSBarry Smith */
3888bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG)
3893a40ed3dSBarry Smith 
39099cd645aSJed Brown #define PETSCSTACKSIZE 64
391184914b5SBarry Smith 
3923a40ed3dSBarry Smith typedef struct  {
3930e33f6ddSBarry Smith   const char *function[PETSCSTACKSIZE];
3940e33f6ddSBarry Smith   const char *file[PETSCSTACKSIZE];
3950e33f6ddSBarry Smith   const char *directory[PETSCSTACKSIZE];
396184914b5SBarry Smith         int  line[PETSCSTACKSIZE];
397184914b5SBarry Smith         int  currentsize;
3983a40ed3dSBarry Smith } PetscStack;
3993a40ed3dSBarry Smith 
40061d886c9SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES)
401997ce2baSShri Abhyankar #if defined(PETSC_PTHREAD_LOCAL)
402*014dd563SJed Brown PETSC_EXTERN PETSC_PTHREAD_LOCAL PetscStack *petscstack;
403fe89fe5aSShri Abhyankar #else
404*014dd563SJed Brown PETSC_EXTERN pthread_key_t petscstack_key;
405*014dd563SJed Brown PETSC_EXTERN PetscStack *petscstack;
406fe89fe5aSShri Abhyankar #endif
40761d886c9SShri Abhyankar #else
408*014dd563SJed Brown PETSC_EXTERN PetscStack *petscstack;
40961d886c9SShri Abhyankar #endif
41061d886c9SShri Abhyankar 
411*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackCopy(PetscStack*,PetscStack*);
412*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackPrint(PetscStack*,FILE* fp);
413184914b5SBarry Smith 
414184914b5SBarry Smith #define PetscStackActive (petscstack != 0)
4153a40ed3dSBarry Smith 
41661d886c9SShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES) && !defined(PETSC_PTHREAD_LOCAL)
41761d886c9SShri Abhyankar /* Get the value associated with name_key */
41861d886c9SShri Abhyankar #define PetscThreadLocalGetValue(name,type) ( (type)pthread_getspecific(name##_key))
41961d886c9SShri Abhyankar /* Set the value for name_key */
42061d886c9SShri Abhyankar #define PetscThreadLocalSetValue(name,value) ( pthread_setspecific(name##_key,(void*)value) )
42161d886c9SShri Abhyankar /* Create name_key */
42261d886c9SShri Abhyankar #define PetscThreadLocalRegister(name) ( pthread_key_create(&name##_key,NULL) )
42361d886c9SShri Abhyankar /* Destroy name_key */
42461d886c9SShri Abhyankar #define PetscThreadLocalDestroy(name) ( pthread_key_delete(name##_key) )
42561d886c9SShri Abhyankar #else
42661d886c9SShri Abhyankar #define PetscThreadLocalGetValue(name,type) ( (type)name )
42761d886c9SShri Abhyankar #define PetscThreadLocalSetValue(name,value)
42861d886c9SShri Abhyankar #define PetscThreadLocalRegister(name)
42961d886c9SShri Abhyankar #define PetscThreadLocalDestroy(name)
43061d886c9SShri Abhyankar #endif
43161d886c9SShri Abhyankar 
43230de9b25SBarry Smith /*MC
43330de9b25SBarry Smith    PetscFunctionBegin - First executable line of each PETSc function
43430de9b25SBarry Smith         used for error handling.
43530de9b25SBarry Smith 
43630de9b25SBarry Smith    Synopsis:
43730de9b25SBarry Smith    void PetscFunctionBegin;
43830de9b25SBarry Smith 
439eca87e8dSBarry Smith    Not Collective
440eca87e8dSBarry Smith 
44130de9b25SBarry Smith    Usage:
44230de9b25SBarry Smith .vb
44330de9b25SBarry Smith      int something;
44430de9b25SBarry Smith 
44530de9b25SBarry Smith      PetscFunctionBegin;
44630de9b25SBarry Smith .ve
44730de9b25SBarry Smith 
44830de9b25SBarry Smith    Notes:
44930de9b25SBarry Smith      Not available in Fortran
45030de9b25SBarry Smith 
45130de9b25SBarry Smith    Level: developer
45230de9b25SBarry Smith 
45330de9b25SBarry Smith .seealso: PetscFunctionReturn()
45430de9b25SBarry Smith 
45530de9b25SBarry Smith .keywords: traceback, error handling
45630de9b25SBarry Smith M*/
4573a40ed3dSBarry Smith #define PetscFunctionBegin \
4588246ba0dSJed Brown   do {									\
45961d886c9SShri Abhyankar     petscstack = PetscThreadLocalGetValue(petscstack,PetscStack*);	\
460184914b5SBarry Smith     if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {	\
46153c77d0aSJed Brown       petscstack->function[petscstack->currentsize]  = PETSC_FUNCTION_NAME; \
462184914b5SBarry Smith       petscstack->file[petscstack->currentsize]      = __FILE__;        \
463184914b5SBarry Smith       petscstack->directory[petscstack->currentsize] = __SDIR__;        \
464184914b5SBarry Smith       petscstack->line[petscstack->currentsize]      = __LINE__;        \
465184914b5SBarry Smith       petscstack->currentsize++;                                        \
46653c77d0aSJed Brown     }                                                                   \
46753c77d0aSJed Brown     PetscCheck__FUNCT__();						\
46853c77d0aSJed Brown   } while (0)
46953c77d0aSJed Brown 
47053c77d0aSJed Brown #define PetscCheck__FUNCT__() do { \
47153c77d0aSJed Brown     if (strcmp(PETSC_FUNCTION_NAME,__FUNCT__) && strcmp(__FUNCT__,"User provided function")) { \
47253c77d0aSJed 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); \
47353c77d0aSJed Brown     }                                                                   \
47453c77d0aSJed Brown   } while (0)
4753a40ed3dSBarry Smith 
4765cd90555SBarry Smith #define PetscStackPush(n) \
47761d886c9SShri Abhyankar   do {									\
47861d886c9SShri Abhyankar     petscstack = PetscThreadLocalGetValue(petscstack,PetscStack*);	\
47961d886c9SShri Abhyankar     if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {	\
480184914b5SBarry Smith       petscstack->function[petscstack->currentsize]  = n;		\
481184914b5SBarry Smith       petscstack->file[petscstack->currentsize]      = "unknown";	\
482184914b5SBarry Smith       petscstack->directory[petscstack->currentsize] = "unknown";	\
483184914b5SBarry Smith       petscstack->line[petscstack->currentsize]      = 0;		\
484184914b5SBarry Smith       petscstack->currentsize++;					\
485812af9f3SBarry Smith     } CHKMEMQ;} while (0)
4863a40ed3dSBarry Smith 
487d64ed03dSBarry Smith #define PetscStackPop \
48861d886c9SShri Abhyankar   do {CHKMEMQ;petscstack = PetscThreadLocalGetValue(petscstack,PetscStack*); \
48961d886c9SShri Abhyankar     if (petscstack && petscstack->currentsize > 0) {			\
490184914b5SBarry Smith       petscstack->currentsize--;					\
491184914b5SBarry Smith       petscstack->function[petscstack->currentsize]  = 0;		\
492184914b5SBarry Smith       petscstack->file[petscstack->currentsize]      = 0;		\
493184914b5SBarry Smith       petscstack->directory[petscstack->currentsize] = 0;		\
494184914b5SBarry Smith       petscstack->line[petscstack->currentsize]      = 0;		\
4958246ba0dSJed Brown     }} while (0)
496d64ed03dSBarry Smith 
49730de9b25SBarry Smith /*MC
49830de9b25SBarry Smith    PetscFunctionReturn - Last executable line of each PETSc function
49930de9b25SBarry Smith         used for error handling. Replaces return()
50030de9b25SBarry Smith 
50130de9b25SBarry Smith    Synopsis:
50230de9b25SBarry Smith    void PetscFunctionReturn(0);
50330de9b25SBarry Smith 
504eca87e8dSBarry Smith    Not Collective
505eca87e8dSBarry Smith 
50630de9b25SBarry Smith    Usage:
50730de9b25SBarry Smith .vb
50830de9b25SBarry Smith     ....
50930de9b25SBarry Smith      PetscFunctionReturn(0);
51030de9b25SBarry Smith    }
51130de9b25SBarry Smith .ve
51230de9b25SBarry Smith 
51330de9b25SBarry Smith    Notes:
51430de9b25SBarry Smith      Not available in Fortran
51530de9b25SBarry Smith 
51630de9b25SBarry Smith    Level: developer
51730de9b25SBarry Smith 
51830de9b25SBarry Smith .seealso: PetscFunctionBegin()
51930de9b25SBarry Smith 
52030de9b25SBarry Smith .keywords: traceback, error handling
52130de9b25SBarry Smith M*/
5225cd90555SBarry Smith #define PetscFunctionReturn(a) \
5238246ba0dSJed Brown   do {									\
52461d886c9SShri Abhyankar     petscstack = PetscThreadLocalGetValue(petscstack,PetscStack*);	\
525812af9f3SBarry Smith     if (petscstack && petscstack->currentsize > 0) {			\
526812af9f3SBarry Smith       petscstack->currentsize--;					\
527812af9f3SBarry Smith       petscstack->function[petscstack->currentsize]  = 0;		\
528812af9f3SBarry Smith       petscstack->file[petscstack->currentsize]      = 0;		\
529812af9f3SBarry Smith       petscstack->directory[petscstack->currentsize] = 0;		\
530812af9f3SBarry Smith       petscstack->line[petscstack->currentsize]      = 0;		\
531812af9f3SBarry Smith     }									\
5328246ba0dSJed Brown     return(a);} while (0)
533d64ed03dSBarry Smith 
534ff94ddecSSatish Balay #define PetscFunctionReturnVoid() \
5358246ba0dSJed Brown   do {							\
53661d886c9SShri Abhyankar     petscstack = PetscThreadLocalGetValue(petscstack,PetscStack*);	\
537812af9f3SBarry Smith     if (petscstack && petscstack->currentsize > 0) {			\
538812af9f3SBarry Smith       petscstack->currentsize--;					\
539812af9f3SBarry Smith       petscstack->function[petscstack->currentsize]  = 0;		\
540812af9f3SBarry Smith       petscstack->file[petscstack->currentsize]      = 0;		\
541812af9f3SBarry Smith       petscstack->directory[petscstack->currentsize] = 0;		\
542812af9f3SBarry Smith       petscstack->line[petscstack->currentsize]      = 0;		\
543812af9f3SBarry Smith     }									\
5448246ba0dSJed Brown     return;} while (0)
5456d385327SIbrahima Ba #else
5466d385327SIbrahima Ba 
5473a40ed3dSBarry Smith #define PetscFunctionBegin
5483a40ed3dSBarry Smith #define PetscFunctionReturn(a)  return(a)
5495665465eSBarry Smith #define PetscFunctionReturnVoid() return
550812af9f3SBarry Smith #define PetscStackPop     CHKMEMQ
551812af9f3SBarry Smith #define PetscStackPush(f) CHKMEMQ
552d64ed03dSBarry Smith #define PetscStackActive        0
5533a40ed3dSBarry Smith 
5543a40ed3dSBarry Smith #endif
5553a40ed3dSBarry Smith 
556eb6b5d47SBarry Smith /*
557eb6b5d47SBarry Smith     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
558eb6b5d47SBarry Smith 
559eb6b5d47SBarry Smith    Input Parameters:
560eb6b5d47SBarry Smith +   name - string that gives the name of the function being called
561eb6b5d47SBarry Smith -   routine - actual call to the routine
562eb6b5d47SBarry Smith 
563eb6b5d47SBarry 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.
564eb6b5d47SBarry Smith 
565eb6b5d47SBarry Smith */
566eb6b5d47SBarry Smith #define PetscStackCall(name,routine) PetscStackPush(name);routine;PetscStackPop;
567eb6b5d47SBarry Smith 
568*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackCreate(void);
569*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackView(PetscViewer);
570*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackDestroy(void);
571*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackPublish(void);
572*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscStackDepublish(void);
57306d1fe2cSBarry Smith 
57406d1fe2cSBarry Smith #endif
575