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