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