xref: /petsc/include/petscerror.h (revision 3f1db9ec2fd39765c6c3a00831044586630c4cca)
1 /* $Id: petscerror.h,v 1.30 1998/12/10 22:58:04 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(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_WRONGSTATE  73   /* object in argument is in wrong state, e.g. unassembled mat */
62 #define PETSC_ERR_ARG_INCOMP      75   /* two arguments are incompatible */
63 
64 #define PETSC_ERR_FILE_OPEN       65   /* unable to open file */
65 #define PETSC_ERR_FILE_READ       66   /* unable to read from file */
66 #define PETSC_ERR_FILE_WRITE      67   /* unable to write to file */
67 #define PETSC_ERR_FILE_UNEXPECTED 79   /* unexpected data in file */
68 
69 #define PETSC_ERR_KSP_BRKDWN      70   /* break down in a Krylov method */
70 
71 #define PETSC_ERR_MAT_LU_ZRPVT    71   /* detected a zero pivot during LU factorization */
72 #define PETSC_ERR_MAT_CH_ZRPVT    71   /* detected a zero pivot during Cholesky factorization */
73 
74 #if defined(USE_PETSC_DEBUG)
75 #define SETERRA(n,p,s) {int _ierr = PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,p,s);\
76                           MPI_Abort(PETSC_COMM_WORLD,_ierr);}
77 #define SETERRQ(n,p,s)              {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,p,s);}
78 #define SETERRQ1(n,p,s,a1)          {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,p,s,a1);}
79 #define SETERRQ2(n,p,s,a1,a2)       {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,p,s,a1,a2);}
80 #define SETERRQ3(n,p,s,a1,a2,a3)    {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,p,s,a1,a2,a3);}
81 #define SETERRQ4(n,p,s,a1,a2,a3,a4) {return PetscError(__LINE__,__FUNC__,__FILE__,__SDIR__,n,p,s,a1,a2,a3,a4);}
82 
83 #define CHKERRQ(n)     {if (n) SETERRQ(n,0,(char *)0);}
84 #define CHKERRA(n)     {if (n) SETERRA(n,0,(char *)0);}
85 #define CHKPTRQ(p)     if (!p) SETERRQ(PETSC_ERR_MEM,0,(char*)0);
86 #define CHKPTRA(p)     if (!p) SETERRA(PETSC_ERR_MEM,0,(char*)0);
87 #else
88 #define SETERRQ(n,p,s) ;
89 #define SETERRQ1(n,p,s,a1) ;
90 #define SETERRQ2(n,p,s,a1,a2) ;
91 #define SETERRQ3(n,p,s,a1,a2,a3) ;
92 #define SETERRQ4(n,p,s,a1,a2,a3,a4) ;
93 #define SETERRA(n,p,s) ;
94 #define CHKERRQ(n)     ;
95 #define CHKERRA(n)     ;
96 #define CHKPTRQ(p)     ;
97 #define CHKPTRA(p)     ;
98 #endif
99 
100 extern int PetscTraceBackErrorHandler(int,char*,char*,char*,int,int,char*,void*);
101 extern int PetscStopErrorHandler(int,char*,char*,char*,int,int,char*,void*);
102 extern int PetscAbortErrorHandler(int,char*,char*,char*,int,int,char*,void* );
103 extern int PetscAttachDebuggerErrorHandler(int,char*,char*,char*,int,int,char*,void*);
104 extern int PetscError(int,char*,char*,char*,int,int,char*,...);
105 extern int PetscPushErrorHandler(int (*handler)(int,char*,char*,char*,int,int,char*,void*),void*);
106 extern int PetscPopErrorHandler(void);
107 
108 extern int PetscDefaultSignalHandler(int,void*);
109 extern int PetscPushSignalHandler(int (*)(int,void *),void*);
110 extern int PetscPopSignalHandler(void);
111 #define PETSC_FP_TRAP_OFF    0
112 #define PETSC_FP_TRAP_ON     1
113 extern int PetscSetFPTrap(int);
114 extern int PetscInitializeNans(Scalar*,int);
115 extern int PetscInitializeLargeInts(int *,int);
116 
117 /*
118       Allows the code to build a stack frame as it runs
119 */
120 #if defined(USE_PETSC_STACK)
121 
122 typedef struct  {
123   char **function;
124   char **file;
125   char **directory;
126   int  *line;
127 } PetscStack;
128 
129 extern int        petscstacksize_max;
130 extern int        petscstacksize;
131 extern PetscStack *petscstack;
132 
133 #if !defined(HAVE_AMS)
134 
135 #define PetscFunctionBegin \
136   {\
137    if (petscstack && (petscstacksize < petscstacksize_max)) {    \
138     petscstack->function[petscstacksize]  = __FUNC__; \
139     petscstack->file[petscstacksize]      = __FILE__; \
140     petscstack->directory[petscstacksize] = __SDIR__; \
141     petscstack->line[petscstacksize]      = __LINE__; \
142     petscstacksize++; \
143   }}
144 
145 #define PetscStackPush(n) \
146   {if (petscstack && (petscstacksize < petscstacksize_max)) {    \
147     petscstack->function[petscstacksize]  = n; \
148     petscstack->file[petscstacksize]      = "unknown"; \
149     petscstack->directory[petscstacksize] = "unknown"; \
150     petscstack->line[petscstacksize]      = 0; \
151     petscstacksize++; \
152   }}
153 
154 #define PetscStackPop \
155   {if (petscstack && petscstacksize > 0) {     \
156     petscstacksize--; \
157     petscstack->function[petscstacksize]  = 0; \
158     petscstack->file[petscstacksize]      = 0; \
159     petscstack->directory[petscstacksize] = 0; \
160     petscstack->line[petscstacksize]      = 0; \
161   }};
162 
163 #define PetscFunctionReturn(a) \
164   {\
165   PetscStackPop; \
166   return(a);}
167 
168 #define PetscStackActive (petscstack != 0)
169 
170 #else
171 
172 /*
173     Duplicate Code for when the ALICE Memory Snooper (AMS)
174   is being used. When HAVE_AMS is defined.
175 
176      stack_mem is the AMS memory that contains fields for the
177                number of stack frames and names of the stack frames
178 */
179 
180 extern AMS_Memory stack_mem;
181 extern int        stack_err;
182 
183 #define PetscFunctionBegin \
184   {\
185    if (petscstack && (petscstacksize < petscstacksize_max)) {    \
186     if (!(stack_mem < 0)) stack_err = AMS_Memory_take_access(stack_mem);\
187     petscstack->function[petscstacksize]  = __FUNC__; \
188     petscstack->file[petscstacksize]      = __FILE__; \
189     petscstack->directory[petscstacksize] = __SDIR__; \
190     petscstack->line[petscstacksize]      = __LINE__; \
191     petscstacksize++; \
192     if (!(stack_mem < 0)) stack_err = AMS_Memory_grant_access(stack_mem);\
193   }}
194 
195 #define PetscStackPush(n) \
196   {if (petscstack && (petscstacksize < petscstacksize_max)) {    \
197     if (!(stack_mem < 0)) stack_err = AMS_Memory_take_access(stack_mem);\
198     petscstack->function[petscstacksize]  = n; \
199     petscstack->file[petscstacksize]      = "unknown"; \
200     petscstack->directory[petscstacksize] = "unknown"; \
201     petscstack->line[petscstacksize]      = 0; \
202     petscstacksize++; \
203     if (!(stack_mem < 0)) stack_err = AMS_Memory_grant_access(stack_mem);\
204   }}
205 
206 #define PetscStackPop \
207   {if (petscstack && petscstacksize > 0) {     \
208     if (!(stack_mem < 0)) stack_err = AMS_Memory_take_access(stack_mem);\
209     petscstacksize--; \
210     petscstack->function[petscstacksize]  = 0; \
211     petscstack->file[petscstacksize]      = 0; \
212     petscstack->directory[petscstacksize] = 0; \
213     petscstack->line[petscstacksize]      = 0; \
214     if (!(stack_mem < 0)) stack_err = AMS_Memory_grant_access(stack_mem);\
215   }};
216 
217 #define PetscFunctionReturn(a) \
218   {\
219   PetscStackPop; \
220   return(a);}
221 
222 #define PetscStackActive (petscstack != 0)
223 
224 #endif
225 
226 #else
227 
228 #define PetscFunctionBegin
229 #define PetscFunctionReturn(a)  return(a)
230 #define PetscStackPop
231 #define PetscStackPush(f)
232 #define PetscStackActive        0
233 
234 #endif
235 
236 extern int PetscStackCreate(int);
237 extern int PetscStackView(Viewer);
238 extern int PetscStackDestroy(void);
239 extern int PetscStackPublish(void);
240 extern int PetscStackDepublish(void);
241 
242 /*
243           For locking and unlocking AMS memories associated with
244     PETSc objects
245 */
246 
247 #if defined(HAVE_AMS)
248 #define PetscAMSTakeAccess(obj)   \
249     ((((PetscObject)(obj))->amem == -1) ? 0 : AMS_Memory_take_access(((PetscObject)(obj))->amem));
250 #define PetscAMSGrantAccess(obj)  \
251     ((((PetscObject)(obj))->amem == -1) ? 0 : AMS_Memory_grant_access(((PetscObject)(obj))->amem));
252 #else
253 #define PetscAMSTakeAccess(obj)
254 #define PetscAMSGrantAccess(obj)
255 #endif
256 
257 #endif
258 
259