xref: /petsc/include/petscerror.h (revision f1af5d2ffeae1f5fc391a89939f4818e47770ae3)
1 /* $Id: petscerror.h,v 1.39 1999/10/23 00:02:04 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 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 SETERRQ(n,p,s)              {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,p,s);}
79 #define SETERRQ1(n,p,s,a1)          {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,p,s,a1);}
80 #define SETERRQ2(n,p,s,a1,a2)       {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,p,s,a1,a2);}
81 #define SETERRQ3(n,p,s,a1,a2,a3)    {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,p,s,a1,a2,a3);}
82 #define SETERRQ4(n,p,s,a1,a2,a3,a4) {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,p,s,a1,a2,a3,a4);}
83 
84 #define CHKERRQ(n)     {if (n) SETERRQ(n,0,(char *)0);}
85 #define CHKERRA(n)     {if (n) SETERRA(n,0,(char *)0);}
86 #define CHKPTRQ(p)     if (!p) SETERRQ(PETSC_ERR_MEM,0,(char*)0);
87 #define CHKPTRA(p)     if (!p) SETERRA(PETSC_ERR_MEM,0,(char*)0);
88 
89 #define CHKMEMQ {int __ierr = PetscTrValid(__LINE__,__FUNC__,__FILE__,__SDIR__); CHKERRQ(__ierr);}
90 #define CHKMEMA {int __ierr = PetscTrValid(__LINE__,__FUNC__,__FILE__,__SDIR__); CHKERRA(__ierr);}
91 
92 #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
93 extern  int __gierr;
94 #define _   __gierr =
95 #define ___  CHKERRA(__gierr);
96 #define ____ CHKERRQ(__gierr);
97 #endif
98 
99 #else
100 #define SETERRQ(n,p,s) ;
101 #define SETERRQ1(n,p,s,a1) ;
102 #define SETERRQ2(n,p,s,a1,a2) ;
103 #define SETERRQ3(n,p,s,a1,a2,a3) ;
104 #define SETERRQ4(n,p,s,a1,a2,a3,a4) ;
105 #define SETERRA(n,p,s) ;
106 
107 #define CHKERRQ(n)     ;
108 #define CHKERRA(n)     ;
109 #define CHKPTRQ(p)     ;
110 #define CHKPTRA(p)     ;
111 
112 #define CHKMEMQ        ;
113 #define CHKMEMA        ;
114 
115 #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
116 #define _
117 #define ___
118 #define ____
119 #endif
120 
121 #endif
122 
123 extern int PetscTraceBackErrorHandler(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 #define PETSC_FP_TRAP_OFF    0
135 #define PETSC_FP_TRAP_ON     1
136 extern int PetscSetFPTrap(int);
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 #else
195 
196 /*
197     Duplicate Code for when the ALICE Memory Snooper (AMS)
198   is being used. When PETSC_HAVE_AMS is defined.
199 
200      stack_mem is the AMS memory that contains fields for the
201                number of stack frames and names of the stack frames
202 */
203 
204 extern AMS_Memory stack_mem;
205 extern int        stack_err;
206 
207 #define PetscFunctionBegin \
208   {\
209    if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
210     if (!(stack_mem < 0)) stack_err = AMS_Memory_take_access(stack_mem);\
211     petscstack->function[petscstack->currentsize]  = __FUNC__; \
212     petscstack->file[petscstack->currentsize]      = __FILE__; \
213     petscstack->directory[petscstack->currentsize] = __SDIR__; \
214     petscstack->line[petscstack->currentsize]      = __LINE__; \
215     petscstack->currentsize++; \
216     if (!(stack_mem < 0)) stack_err = AMS_Memory_grant_access(stack_mem);\
217   }}
218 
219 #define PetscStackPush(n) \
220   {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
221     if (!(stack_mem < 0)) stack_err = AMS_Memory_take_access(stack_mem);\
222     petscstack->function[petscstack->currentsize]  = n; \
223     petscstack->file[petscstack->currentsize]      = "unknown"; \
224     petscstack->directory[petscstack->currentsize] = "unknown"; \
225     petscstack->line[petscstack->currentsize]      = 0; \
226     petscstack->currentsize++; \
227     if (!(stack_mem < 0)) stack_err = AMS_Memory_grant_access(stack_mem);\
228   }}
229 
230 #define PetscStackPop \
231   {if (petscstack && petscstack->currentsize > 0) {     \
232     if (!(stack_mem < 0)) stack_err = AMS_Memory_take_access(stack_mem);\
233     petscstack->currentsize--; \
234     petscstack->function[petscstack->currentsize]  = 0; \
235     petscstack->file[petscstack->currentsize]      = 0; \
236     petscstack->directory[petscstack->currentsize] = 0; \
237     petscstack->line[petscstack->currentsize]      = 0; \
238     if (!(stack_mem < 0)) stack_err = AMS_Memory_grant_access(stack_mem);\
239   }};
240 
241 #define PetscFunctionReturn(a) \
242   {\
243   PetscStackPop; \
244   return(a);}
245 
246 #endif
247 
248 #else
249 
250 #define PetscFunctionBegin
251 #define PetscFunctionReturn(a)  return(a)
252 #define PetscStackPop
253 #define PetscStackPush(f)
254 #define PetscStackActive        0
255 
256 #endif
257 
258 extern int PetscStackCreate(void);
259 extern int PetscStackView(Viewer);
260 extern int PetscStackDestroy(void);
261 extern int PetscStackPublish(void);
262 extern int PetscStackDepublish(void);
263 
264 /*
265           For locking, unlocking and destroying AMS memories associated with
266     PETSc objects
267 */
268 
269 #if defined(PETSC_HAVE_AMS)
270 #define PetscObjectTakeAccess(obj)   \
271     ((((PetscObject)(obj))->amem == -1) ? 0 : AMS_Memory_take_access(((PetscObject)(obj))->amem))
272 #define PetscObjectGrantAccess(obj)  \
273     ((((PetscObject)(obj))->amem == -1) ? 0 : AMS_Memory_grant_access(((PetscObject)(obj))->amem))
274 #define PetscObjectDepublish(obj)  \
275     ((((PetscObject)(obj))->amem == -1) ? 0 : AMS_Memory_destroy(((PetscObject)(obj))->amem)); \
276     ((PetscObject)(obj))->amem = -1;
277 #else
278 #define PetscObjectTakeAccess(obj)   0
279 #define PetscObjectGrantAccess(obj)  0
280 #define PetscObjectDepublish(obj)      0
281 #endif
282 
283 #endif
284 
285