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