xref: /petsc/include/petscerror.h (revision ac2a4f0d24b3b6a4ee93edbcad41f4bb9e923944)
1 /* $Id: petscerror.h,v 1.38 1999/10/13 20:39:18 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 #else
93 #define SETERRQ(n,p,s) ;
94 #define SETERRQ1(n,p,s,a1) ;
95 #define SETERRQ2(n,p,s,a1,a2) ;
96 #define SETERRQ3(n,p,s,a1,a2,a3) ;
97 #define SETERRQ4(n,p,s,a1,a2,a3,a4) ;
98 #define SETERRA(n,p,s) ;
99 
100 #define CHKERRQ(n)     ;
101 #define CHKERRA(n)     ;
102 #define CHKPTRQ(p)     ;
103 #define CHKPTRA(p)     ;
104 
105 #define CHKMEMQ        ;
106 #define CHKMEMA        ;
107 
108 #endif
109 
110 extern int PetscTraceBackErrorHandler(int,char*,char*,char*,int,int,char*,void*);
111 extern int PetscStopErrorHandler(int,char*,char*,char*,int,int,char*,void*);
112 extern int PetscAbortErrorHandler(int,char*,char*,char*,int,int,char*,void* );
113 extern int PetscAttachDebuggerErrorHandler(int,char*,char*,char*,int,int,char*,void*);
114 extern int PetscError(int,char*,char*,char*,int,int,char*,...);
115 extern int PetscPushErrorHandler(int (*handler)(int,char*,char*,char*,int,int,char*,void*),void*);
116 extern int PetscPopErrorHandler(void);
117 
118 extern int PetscDefaultSignalHandler(int,void*);
119 extern int PetscPushSignalHandler(int (*)(int,void *),void*);
120 extern int PetscPopSignalHandler(void);
121 #define PETSC_FP_TRAP_OFF    0
122 #define PETSC_FP_TRAP_ON     1
123 extern int PetscSetFPTrap(int);
124 
125 /*
126       Allows the code to build a stack frame as it runs
127 */
128 #if defined(PETSC_USE_STACK)
129 
130 #define PETSCSTACKSIZE 15
131 
132 typedef struct  {
133   char *function[PETSCSTACKSIZE];
134   char *file[PETSCSTACKSIZE];
135   char *directory[PETSCSTACKSIZE];
136   int  line[PETSCSTACKSIZE];
137   int  currentsize;
138 } PetscStack;
139 
140 extern PetscStack *petscstack;
141 extern int PetscStackCopy(PetscStack*,PetscStack*);
142 extern int PetscStackPrint(PetscStack*,FILE* fp);
143 
144 #define PetscStackActive (petscstack != 0)
145 
146 #if !defined(PETSC_HAVE_AMS)
147 
148 #define PetscFunctionBegin \
149   {\
150    if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
151     petscstack->function[petscstack->currentsize]  = __FUNC__; \
152     petscstack->file[petscstack->currentsize]      = __FILE__; \
153     petscstack->directory[petscstack->currentsize] = __SDIR__; \
154     petscstack->line[petscstack->currentsize]      = __LINE__; \
155     petscstack->currentsize++; \
156   }}
157 
158 #define PetscStackPush(n) \
159   {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
160     petscstack->function[petscstack->currentsize]  = n; \
161     petscstack->file[petscstack->currentsize]      = "unknown"; \
162     petscstack->directory[petscstack->currentsize] = "unknown"; \
163     petscstack->line[petscstack->currentsize]      = 0; \
164     petscstack->currentsize++; \
165   }}
166 
167 #define PetscStackPop \
168   {if (petscstack && petscstack->currentsize > 0) {     \
169     petscstack->currentsize--; \
170     petscstack->function[petscstack->currentsize]  = 0; \
171     petscstack->file[petscstack->currentsize]      = 0; \
172     petscstack->directory[petscstack->currentsize] = 0; \
173     petscstack->line[petscstack->currentsize]      = 0; \
174   }};
175 
176 #define PetscFunctionReturn(a) \
177   {\
178   PetscStackPop; \
179   return(a);}
180 
181 #else
182 
183 /*
184     Duplicate Code for when the ALICE Memory Snooper (AMS)
185   is being used. When PETSC_HAVE_AMS is defined.
186 
187      stack_mem is the AMS memory that contains fields for the
188                number of stack frames and names of the stack frames
189 */
190 
191 extern AMS_Memory stack_mem;
192 extern int        stack_err;
193 
194 #define PetscFunctionBegin \
195   {\
196    if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
197     if (!(stack_mem < 0)) stack_err = AMS_Memory_take_access(stack_mem);\
198     petscstack->function[petscstack->currentsize]  = __FUNC__; \
199     petscstack->file[petscstack->currentsize]      = __FILE__; \
200     petscstack->directory[petscstack->currentsize] = __SDIR__; \
201     petscstack->line[petscstack->currentsize]      = __LINE__; \
202     petscstack->currentsize++; \
203     if (!(stack_mem < 0)) stack_err = AMS_Memory_grant_access(stack_mem);\
204   }}
205 
206 #define PetscStackPush(n) \
207   {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
208     if (!(stack_mem < 0)) stack_err = AMS_Memory_take_access(stack_mem);\
209     petscstack->function[petscstack->currentsize]  = n; \
210     petscstack->file[petscstack->currentsize]      = "unknown"; \
211     petscstack->directory[petscstack->currentsize] = "unknown"; \
212     petscstack->line[petscstack->currentsize]      = 0; \
213     petscstack->currentsize++; \
214     if (!(stack_mem < 0)) stack_err = AMS_Memory_grant_access(stack_mem);\
215   }}
216 
217 #define PetscStackPop \
218   {if (petscstack && petscstack->currentsize > 0) {     \
219     if (!(stack_mem < 0)) stack_err = AMS_Memory_take_access(stack_mem);\
220     petscstack->currentsize--; \
221     petscstack->function[petscstack->currentsize]  = 0; \
222     petscstack->file[petscstack->currentsize]      = 0; \
223     petscstack->directory[petscstack->currentsize] = 0; \
224     petscstack->line[petscstack->currentsize]      = 0; \
225     if (!(stack_mem < 0)) stack_err = AMS_Memory_grant_access(stack_mem);\
226   }};
227 
228 #define PetscFunctionReturn(a) \
229   {\
230   PetscStackPop; \
231   return(a);}
232 
233 #endif
234 
235 #else
236 
237 #define PetscFunctionBegin
238 #define PetscFunctionReturn(a)  return(a)
239 #define PetscStackPop
240 #define PetscStackPush(f)
241 #define PetscStackActive        0
242 
243 #endif
244 
245 extern int PetscStackCreate(void);
246 extern int PetscStackView(Viewer);
247 extern int PetscStackDestroy(void);
248 extern int PetscStackPublish(void);
249 extern int PetscStackDepublish(void);
250 
251 /*
252           For locking, unlocking and destroying AMS memories associated with
253     PETSc objects
254 */
255 
256 #if defined(PETSC_HAVE_AMS)
257 #define PetscObjectTakeAccess(obj)   \
258     ((((PetscObject)(obj))->amem == -1) ? 0 : AMS_Memory_take_access(((PetscObject)(obj))->amem))
259 #define PetscObjectGrantAccess(obj)  \
260     ((((PetscObject)(obj))->amem == -1) ? 0 : AMS_Memory_grant_access(((PetscObject)(obj))->amem))
261 #define PetscObjectDepublish(obj)  \
262     ((((PetscObject)(obj))->amem == -1) ? 0 : AMS_Memory_destroy(((PetscObject)(obj))->amem)); \
263     ((PetscObject)(obj))->amem = -1;
264 #else
265 #define PetscObjectTakeAccess(obj)   0
266 #define PetscObjectGrantAccess(obj)  0
267 #define PetscObjectDepublish(obj)      0
268 #endif
269 
270 #endif
271 
272