xref: /petsc/include/petscerror.h (revision 29bbc08cd5461c366aba6645924e8ff42acd1de0)
1 /* $Id: petscerror.h,v 1.49 2000/09/28 19:17:44 bsmith Exp bsmith $ */
2 /*
3     Contains all error handling code for PETSc.
4 */
5 #if !defined(__PETSCERROR_H)
6 #define __PETSCERROR_H
7 
8 #include "petsc.h"
9 
10 #if defined(PETSC_HAVE_AMS)
11 #include "ams.h"
12 #endif
13 
14 /*
15    Defines the directory where the compiled source is located; used
16    in printing error messages. Each makefile has an entry
17    LOCDIR	  =  thedirectory
18    and bmake/common_variables includes in CCPPFLAGS -D__SDIR__='"${LOCDIR}"'
19    which is a flag passed to the C/C++ compilers.
20 */
21 #if !defined(__SDIR__)
22 #define __SDIR__ "unknowndirectory/"
23 #endif
24 
25 /*
26    Defines the function where the compiled source is located; used
27    in printing error messages.
28 */
29 #if !defined(__FUNC__)
30 #define __FUNC__ "unknownfunction"
31 #endif
32 
33 /*
34      These are the generic error codes. These error codes are used
35      many different places in the PETSc source code.
36 
37 */
38 #define PETSC_ERR_MEM             55   /* unable to allocate requested memory */
39 #define PETSC_ERR_SUP             56   /* no support for requested operation */
40 #define PETSC_ERR_SIG             59   /* signal received */
41 #define PETSC_ERR_FP              72   /* floating point exception */
42 #define PETSC_ERR_COR             74   /* corrupted PETSc object */
43 #define PETSC_ERR_LIB             76   /* error in library called by PETSc */
44 #define PETSC_ERR_PLIB            77   /* PETSc library generated inconsistent data */
45 #define PETSC_ERR_MEMC            78   /* memory corruption */
46 
47 #define PETSC_ERR_ARG_SIZ         60   /* nonconforming object sizes used in operation */
48 #define PETSC_ERR_ARG_IDN         61   /* two arguments not allowed to be the same */
49 #define PETSC_ERR_ARG_WRONG       62   /* wrong argument (but object probably ok) */
50 #define PETSC_ERR_ARG_CORRUPT     64   /* null or corrupted PETSc object as argument */
51 #define PETSC_ERR_ARG_OUTOFRANGE  63   /* input argument, out of range */
52 #define PETSC_ERR_ARG_BADPTR      68   /* invalid pointer argument */
53 #define PETSC_ERR_ARG_NOTSAMETYPE 69   /* two args must be same object type */
54 #define PETSC_ERR_ARG_NOTSAMECOMM 80   /* two args must be same communicators */
55 #define PETSC_ERR_ARG_WRONGSTATE  73   /* object in argument is in wrong state, e.g. unassembled mat */
56 #define PETSC_ERR_ARG_INCOMP      75   /* two arguments are incompatible */
57 
58 #define PETSC_ERR_FILE_OPEN       65   /* unable to open file */
59 #define PETSC_ERR_FILE_READ       66   /* unable to read from file */
60 #define PETSC_ERR_FILE_WRITE      67   /* unable to write to file */
61 #define PETSC_ERR_FILE_UNEXPECTED 79   /* unexpected data in file */
62 
63 #define PETSC_ERR_KSP_BRKDWN      70   /* break down in a Krylov method */
64 
65 #define PETSC_ERR_MAT_LU_ZRPVT    71   /* detected a zero pivot during LU factorization */
66 #define PETSC_ERR_MAT_CH_ZRPVT    81   /* detected a zero pivot during Cholesky factorization */
67 
68 #if defined(PETSC_USE_DEBUG)
69 #define SETERRA(n,s)       {int _ierr = PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,1,s);\
70                            MPI_Abort(PETSC_COMM_WORLD,_ierr);}
71 #define SETERRA1(n,s,a1)   {int _ierr = PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,1,s,a1);\
72                            MPI_Abort(PETSC_COMM_WORLD,_ierr);}
73 #define SETERRQ(n,s)              {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,1,s);}
74 #define SETERRQ1(n,s,a1)          {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,1,s,a1);}
75 #define SETERRQ2(n,s,a1,a2)       {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,1,s,a1,a2);}
76 #define SETERRQ3(n,s,a1,a2,a3)    {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,1,s,a1,a2,a3);}
77 #define SETERRQ4(n,s,a1,a2,a3,a4) {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4);}
78 
79 #define CHKERRQ(n)     if (n) {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,0,0);}
80 #define CHKERRA(n)     if (n) {int _ierr = PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,0,0);\
81                            MPI_Abort(PETSC_COMM_WORLD,_ierr);}
82 #define CHKPTRQ(p)     if (!p) {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,PETSC_ERR_MEM,0,0);}
83 #define CHKPTRA(p)     if (!p) {int _ierr = PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,PETSC_ERR_MEM,0,0);\
84                                 MPI_Abort(PETSC_COMM_WORLD,_ierr);}
85 
86 #define CHKMEMQ {int __ierr = PetscTrValid(__LINE__,__FUNC__,__FILE__,__SDIR__);CHKERRQ(__ierr);}
87 #define CHKMEMA {int __ierr = PetscTrValid(__LINE__,__FUNC__,__FILE__,__SDIR__);CHKERRA(__ierr);}
88 
89 #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
90 extern  int __gierr;
91 #define _   __gierr =
92 #define ___  CHKERRA(__gierr);
93 #define ____ CHKERRQ(__gierr);
94 #endif
95 
96 #else
97 #define SETERRA(n,p,s) ;
98 #define SETERRA1(n,p,s,b) ;
99 #define SETERRQ(n,p,s) ;
100 #define SETERRQ1(n,p,s,a1) ;
101 #define SETERRQ2(n,p,s,a1,a2) ;
102 #define SETERRQ3(n,p,s,a1,a2,a3) ;
103 #define SETERRQ4(n,p,s,a1,a2,a3,a4) ;
104 
105 #define CHKERRQ(n)     ;
106 #define CHKERRA(n)     ;
107 #define CHKPTRQ(p)     ;
108 #define CHKPTRA(p)     ;
109 
110 #define CHKMEMQ        ;
111 #define CHKMEMA        ;
112 
113 #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
114 #define _
115 #define ___
116 #define ____
117 #endif
118 
119 #endif
120 
121 EXTERN int PetscErrorMessage(int,char**);
122 EXTERN int PetscTraceBackErrorHandler(int,char*,char*,char*,int,int,char*,void*);
123 EXTERN int PetscEmacsClientErrorHandler(int,char*,char*,char*,int,int,char*,void*);
124 EXTERN int PetscStopErrorHandler(int,char*,char*,char*,int,int,char*,void*);
125 EXTERN int PetscAbortErrorHandler(int,char*,char*,char*,int,int,char*,void*);
126 EXTERN int PetscAttachDebuggerErrorHandler(int,char*,char*,char*,int,int,char*,void*);
127 EXTERN int PetscError(int,char*,char*,char*,int,int,char*,...);
128 EXTERN int PetscPushErrorHandler(int (*handler)(int,char*,char*,char*,int,int,char*,void*),void*);
129 EXTERN int PetscPopErrorHandler(void);
130 
131 EXTERN int PetscDefaultSignalHandler(int,void*);
132 EXTERN int PetscPushSignalHandler(int (*)(int,void *),void*);
133 EXTERN int PetscPopSignalHandler(void);
134 
135 typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
136 EXTERN int PetscSetFPTrap(PetscFPTrap);
137 
138 /*
139       Allows the code to build a stack frame as it runs
140 */
141 #if defined(PETSC_USE_STACK)
142 
143 #define PETSCSTACKSIZE 15
144 
145 typedef struct  {
146   char *function[PETSCSTACKSIZE];
147   char *file[PETSCSTACKSIZE];
148   char *directory[PETSCSTACKSIZE];
149   int  line[PETSCSTACKSIZE];
150   int  currentsize;
151 } PetscStack;
152 
153 extern PetscStack *petscstack;
154 EXTERN int PetscStackCopy(PetscStack*,PetscStack*);
155 EXTERN int PetscStackPrint(PetscStack*,FILE* fp);
156 
157 #define PetscStackActive (petscstack != 0)
158 
159 #if !defined(PETSC_HAVE_AMS)
160 
161 #define PetscFunctionBegin \
162   {\
163    if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
164     petscstack->function[petscstack->currentsize]  = __FUNC__; \
165     petscstack->file[petscstack->currentsize]      = __FILE__; \
166     petscstack->directory[petscstack->currentsize] = __SDIR__; \
167     petscstack->line[petscstack->currentsize]      = __LINE__; \
168     petscstack->currentsize++; \
169   }}
170 
171 #define PetscStackPush(n) \
172   {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
173     petscstack->function[petscstack->currentsize]  = n; \
174     petscstack->file[petscstack->currentsize]      = "unknown"; \
175     petscstack->directory[petscstack->currentsize] = "unknown"; \
176     petscstack->line[petscstack->currentsize]      = 0; \
177     petscstack->currentsize++; \
178   }}
179 
180 #define PetscStackPop \
181   {if (petscstack && petscstack->currentsize > 0) {     \
182     petscstack->currentsize--; \
183     petscstack->function[petscstack->currentsize]  = 0; \
184     petscstack->file[petscstack->currentsize]      = 0; \
185     petscstack->directory[petscstack->currentsize] = 0; \
186     petscstack->line[petscstack->currentsize]      = 0; \
187   }};
188 
189 #define PetscFunctionReturn(a) \
190   {\
191   PetscStackPop; \
192   return(a);}
193 
194 #define PetscFunctionReturnVoid() \
195   {\
196   PetscStackPop;}
197 
198 #else
199 
200 /*
201     Duplicate Code for when the ALICE Memory Snooper (AMS)
202   is being used. When PETSC_HAVE_AMS is defined.
203 
204      stack_mem is the AMS memory that contains fields for the
205                number of stack frames and names of the stack frames
206 */
207 
208 extern AMS_Memory stack_mem;
209 extern int        stack_err;
210 
211 #define PetscFunctionBegin \
212   {\
213    if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
214     if (!(stack_mem < 0)) stack_err = AMS_Memory_take_access(stack_mem);\
215     petscstack->function[petscstack->currentsize]  = __FUNC__; \
216     petscstack->file[petscstack->currentsize]      = __FILE__; \
217     petscstack->directory[petscstack->currentsize] = __SDIR__; \
218     petscstack->line[petscstack->currentsize]      = __LINE__; \
219     petscstack->currentsize++; \
220     if (!(stack_mem < 0)) stack_err = AMS_Memory_grant_access(stack_mem);\
221   }}
222 
223 #define PetscStackPush(n) \
224   {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
225     if (!(stack_mem < 0)) stack_err = AMS_Memory_take_access(stack_mem);\
226     petscstack->function[petscstack->currentsize]  = n; \
227     petscstack->file[petscstack->currentsize]      = "unknown"; \
228     petscstack->directory[petscstack->currentsize] = "unknown"; \
229     petscstack->line[petscstack->currentsize]      = 0; \
230     petscstack->currentsize++; \
231     if (!(stack_mem < 0)) stack_err = AMS_Memory_grant_access(stack_mem);\
232   }}
233 
234 #define PetscStackPop \
235   {if (petscstack && petscstack->currentsize > 0) {     \
236     if (!(stack_mem < 0)) stack_err = AMS_Memory_take_access(stack_mem);\
237     petscstack->currentsize--; \
238     petscstack->function[petscstack->currentsize]  = 0; \
239     petscstack->file[petscstack->currentsize]      = 0; \
240     petscstack->directory[petscstack->currentsize] = 0; \
241     petscstack->line[petscstack->currentsize]      = 0; \
242     if (!(stack_mem < 0)) stack_err = AMS_Memory_grant_access(stack_mem);\
243   }};
244 
245 #define PetscFunctionReturn(a) \
246   {\
247   PetscStackPop; \
248   return(a);}
249 
250 #define PetscFunctionReturnVoid() \
251   {\
252   PetscStackPop;}
253 
254 
255 #endif
256 
257 #else
258 
259 #define PetscFunctionBegin
260 #define PetscFunctionReturn(a)  return(a)
261 #define PetscFunctionReturnVoid()
262 #define PetscStackPop
263 #define PetscStackPush(f)
264 #define PetscStackActive        0
265 
266 #endif
267 
268 EXTERN int PetscStackCreate(void);
269 EXTERN int PetscStackView(Viewer);
270 EXTERN int PetscStackDestroy(void);
271 EXTERN int PetscStackPublish(void);
272 EXTERN int PetscStackDepublish(void);
273 
274 
275 #endif
276 
277 
278