xref: /petsc/include/petscerror.h (revision a8d2bbe5e2ec4f82cea34690032c2899b2c07c1b)
1 /*
2     Contains all error handling interfaces for PETSc.
3 */
4 #if !defined(__PETSCERROR_H)
5 #define __PETSCERROR_H
6 
7 #if defined(PETSC_HAVE_STRING_H)
8 #include <string.h> /* for strcmp */
9 #endif
10 
11 
12 /*
13    Defines the directory where the compiled source is located; used
14    in printing error messages. Each makefile has an entry
15    LOCDIR	  =  thedirectory
16    and bmake/common_variables includes in CCPPFLAGS -D__SDIR__=${LOCDIR}
17    which is a flag passed to the C/C++ compilers. This declaration below
18    is only needed if some code is compiled without the -D__SDIR__
19 */
20 #if !defined(__INSDIR__)
21 #define __INSDIR__ "unknowndirectory/"
22 #endif
23 
24 /*
25    Defines the function where the compiled source is located; used
26    in printing error messages. This is defined here in case the user
27    does not declare it.
28 */
29 #if !defined(__FUNCT__)
30 #define __FUNCT__ "User provided function"
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. The string versions are
36      at src/sys/error/err.c any changes here must also be made there
37      These are also define in include/finclude/petscerror.h any CHANGES here
38      must be also made there.
39 
40 */
41 #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
42 
43 #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
44 #define PETSC_ERR_SUP              56   /* no support for requested operation */
45 #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
46 #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
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 #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
54 #define PETSC_ERR_USER             83   /* user has not provided needed function */
55 #define PETSC_ERR_SYS              88   /* error in system call */
56 #define PETSC_ERR_POINTER          70   /* pointer does not point to valid address */
57 
58 #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
59 #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
60 #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
61 #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
62 #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
63 #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
64 #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
65 #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
66 #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
67 #define PETSC_ERR_ARG_TYPENOTSET   89   /* the type of the object has not yet been set */
68 #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
69 #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
70 #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
71 
72 #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
73 #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
74 #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
75 #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
76 
77 #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
78 #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
79 
80 #define PETSC_ERR_INT_OVERFLOW     84   /* should always be one less then the smallest value */
81 
82 #define PETSC_ERR_FLOP_COUNT       90
83 #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
84 #define PETSC_ERR_MAX_VALUE        92  /* this is always the one more than the largest error code */
85 
86 #define PetscStringizeArg(a) #a
87 #define PetscStringize(a) PetscStringizeArg(a)
88 #define __SDIR__ PetscStringize(__INSDIR__)
89 
90 #if defined(PETSC_USE_ERRORCHECKING)
91 
92 /*MC
93    SETERRQ - Macro that is called when an error has been detected,
94 
95    Synopsis:
96    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode errorcode,char *message)
97 
98    Not Collective
99 
100    Input Parameters:
101 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
102 -  message - error message
103 
104   Level: beginner
105 
106    Notes:
107     Once the error handler is called the calling function is then returned from with the given error code.
108 
109     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
110 
111     In Fortran MPI_Abort() is always called
112 
113     Experienced users can set the error handler with PetscPushErrorHandler().
114 
115    Concepts: error^setting condition
116 
117 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
118 M*/
119 #define SETERRQ(comm,n,s)              return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s)
120 
121 /*MC
122    SETERRQ1 - Macro that is called when an error has been detected,
123 
124    Synopsis:
125    PetscErrorCode SETERRQ1(MPI_Comm comm,PetscErrorCode errorcode,char *formatmessage,arg)
126 
127    Not Collective
128 
129    Input Parameters:
130 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
131 .  message - error message in the printf format
132 -  arg - argument (for example an integer, string or double)
133 
134   Level: beginner
135 
136    Notes:
137     Once the error handler is called the calling function is then returned from with the given error code.
138 
139    Experienced users can set the error handler with PetscPushErrorHandler().
140 
141    Concepts: error^setting condition
142 
143 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
144 M*/
145 #define SETERRQ1(comm,n,s,a1)          return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1)
146 
147 /*MC
148    SETERRQ2 - Macro that is called when an error has been detected,
149 
150    Synopsis:
151    PetscErrorCode SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
152 
153    Not Collective
154 
155    Input Parameters:
156 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
157 .  message - error message in the printf format
158 .  arg1 - argument (for example an integer, string or double)
159 -  arg2 - argument (for example an integer, string or double)
160 
161   Level: beginner
162 
163    Notes:
164     Once the error handler is called the calling function is then returned from with the given error code.
165 
166    Experienced users can set the error handler with PetscPushErrorHandler().
167 
168    Concepts: error^setting condition
169 
170 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
171 M*/
172 #define SETERRQ2(comm,n,s,a1,a2)       return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2)
173 
174 /*MC
175    SETERRQ3 - Macro that is called when an error has been detected,
176 
177    Synopsis:
178    PetscErrorCode SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
179 
180    Not Collective
181 
182    Input Parameters:
183 +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
184 .  message - error message in the printf format
185 .  arg1 - argument (for example an integer, string or double)
186 .  arg2 - argument (for example an integer, string or double)
187 -  arg3 - argument (for example an integer, string or double)
188 
189   Level: beginner
190 
191    Notes:
192     Once the error handler is called the calling function is then returned from with the given error code.
193 
194     There are also versions for 4, 5, 6 and 7 arguments.
195 
196    Experienced users can set the error handler with PetscPushErrorHandler().
197 
198    Concepts: error^setting condition
199 
200 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
201 M*/
202 #define SETERRQ3(comm,n,s,a1,a2,a3)    return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3)
203 
204 #define SETERRQ4(comm,n,s,a1,a2,a3,a4) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4)
205 #define SETERRQ5(comm,n,s,a1,a2,a3,a4,a5)       return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5)
206 #define SETERRQ6(comm,n,s,a1,a2,a3,a4,a5,a6)    return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6)
207 #define SETERRQ7(comm,n,s,a1,a2,a3,a4,a5,a6,a7) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6,a7)
208 #define SETERRQ8(comm,n,s,a1,a2,a3,a4,a5,a6,a7,a8) return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s,a1,a2,a3,a4,a5,a6,a7,a8)
209 #define SETERRABORT(comm,n,s)     do {PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_INITIAL,s);MPI_Abort(comm,n);} while (0)
210 
211 /*MC
212    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
213 
214    Synopsis:
215    PetscErrorCode CHKERRQ(PetscErrorCode errorcode)
216 
217    Not Collective
218 
219    Input Parameters:
220 .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
221 
222   Level: beginner
223 
224    Notes:
225     Once the error handler is called the calling function is then returned from with the given error code.
226 
227     Experienced users can set the error handler with PetscPushErrorHandler().
228 
229     CHKERRQ(n) is fundamentally a macro replacement for
230          if (n) return(PetscError(...,n,...));
231 
232     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
233     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
234     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
235     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
236          if (n) {PetscError(....); return(YourReturnType);}
237     where you may pass back a PETSC_NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
238     MPI_Abort() returned immediately.
239 
240     In Fortran MPI_Abort() is always called
241 
242    Concepts: error^setting condition
243 
244 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
245 M*/
246 #define CHKERRQ(n)             do {if (PetscUnlikely(n)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");} while (0)
247 
248 #define CHKERRV(n)             do {if (PetscUnlikely(n)) {n = PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");return;}} while(0)
249 #define CHKERRABORT(comm,n)    do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");MPI_Abort(comm,n);}} while (0)
250 #define CHKERRCONTINUE(n)      do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_REPEAT," ");}} while (0)
251 
252 #ifdef PETSC_CLANGUAGE_CXX
253 
254 /*MC
255    CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception
256 
257    Synopsis:
258    void CHKERRXX(PetscErrorCode errorcode)
259 
260    Not Collective
261 
262    Input Parameters:
263 .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
264 
265   Level: beginner
266 
267    Notes:
268     Once the error handler throws a ??? exception.
269 
270     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
271     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.
272 
273    Concepts: error^setting condition
274 
275 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKERRQ(), CHKMEMQ
276 M*/
277 #define CHKERRXX(n)            do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_IN_CXX,0);}} while(0)
278 
279 #endif
280 
281 /*MC
282    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
283 
284    Synopsis:
285    CHKMEMQ;
286 
287    Not Collective
288 
289   Level: beginner
290 
291    Notes:
292     Must run with the option -malloc_debug to enable this option
293 
294     Once the error handler is called the calling function is then returned from with the given error code.
295 
296     By defaults prints location where memory that is corrupted was allocated.
297 
298     Use CHKMEMA for functions that return void
299 
300    Concepts: memory corruption
301 
302 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
303           PetscMallocValidate()
304 M*/
305 #define CHKMEMQ do {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__);CHKERRQ(_7_ierr);} while(0)
306 
307 #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__)
308 
309 #else /* PETSC_USE_ERRORCHECKING */
310 
311 /*
312     These are defined to be empty for when error checking is turned off, with ./configure --with-errorchecking=0
313 */
314 
315 #define SETERRQ(c,n,s)
316 #define SETERRQ1(c,n,s,a1)
317 #define SETERRQ2(c,n,s,a1,a2)
318 #define SETERRQ3(c,n,s,a1,a2,a3)
319 #define SETERRQ4(c,n,s,a1,a2,a3,a4)
320 #define SETERRQ5(c,n,s,a1,a2,a3,a4,a5)
321 #define SETERRQ6(c,n,s,a1,a2,a3,a4,a5,a6)
322 #define SETERRQ7(c,n,s,a1,a2,a3,a4,a5,a6,a7)
323 #define SETERRQ8(c,n,s,a1,a2,a3,a4,a5,a6,a7,a8)
324 #define SETERRABORT(comm,n,s)
325 
326 #define CHKERRQ(n)     ;
327 #define CHKERRABORT(comm,n) ;
328 #define CHKERRCONTINUE(n) ;
329 #define CHKMEMQ        ;
330 
331 #ifdef PETSC_CLANGUAGE_CXX
332 #define CHKERRXX(n) ;
333 #endif
334 
335 #endif /* PETSC_USE_ERRORCHECKING */
336 
337 /*E
338   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers
339 
340   Level: advanced
341 
342   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated
343 
344   Developer Notes: This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandling()
345 
346 .seealso: PetscError(), SETERRXX()
347 E*/
348 typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;
349 
350 PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
351 PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
352 PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
353 PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
354 PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
355 PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
356 PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
357 PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
358 PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
359 PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...);
360 PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
361 PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
362 PETSC_EXTERN PetscErrorCode PetscDefaultSignalHandler(int,void*);
363 PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
364 PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);
365 
366 typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
367 PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
368 PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
369 PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);
370 
371 /*  Linux functions CPU_SET and others don't work if sched.h is not included before
372     including pthread.h. Also, these functions are active only if either _GNU_SOURCE
373     or __USE_GNU is not set (see /usr/include/sched.h and /usr/include/features.h), hence
374     set these first.
375 */
376 #if defined(PETSC_HAVE_PTHREADCLASSES)
377 #if defined(PETSC_HAVE_SCHED_H)
378 #ifndef _GNU_SOURCE
379 #define _GNU_SOURCE
380 #endif
381 #include <sched.h>
382 #endif
383 #include <pthread.h>
384 #endif
385 
386 #if defined(PETSC_HAVE_PTHREADCLASSES) && !defined(PETSC_PTHREAD_LOCAL)
387 /* Get the value associated with key */
388 PETSC_STATIC_INLINE void* PetscThreadLocalGetValue(pthread_key_t key)
389 {
390   return pthread_getspecific(key);
391 }
392 
393 /* Set the value for key */
394 PETSC_STATIC_INLINE void PetscThreadLocalSetValue(pthread_key_t key,void* value)
395 {
396   pthread_setspecific(key,(void*)value);
397 }
398 
399 /* Create pthread thread local key */
400 PETSC_STATIC_INLINE void PetscThreadLocalRegister(pthread_key_t *key)
401 {
402   pthread_key_create(key,PETSC_NULL);
403 }
404 
405 /* Delete pthread thread local key */
406 PETSC_STATIC_INLINE void PetscThreadLocalDestroy(pthread_key_t key)
407 {
408   pthread_key_delete(key);
409 }
410 #else
411 PETSC_STATIC_INLINE void* PetscThreadLocalGetValue(void* key)
412 {
413   return key;
414 }
415 
416 #define PetscThreadLocalSetValue(key,value) (key = value)
417 
418 PETSC_STATIC_INLINE void PetscThreadLocalRegister(PETSC_UNUSED void *key)
419 {
420 }
421 
422 PETSC_STATIC_INLINE void PetscThreadLocalDestroy(PETSC_UNUSED void *key)
423 {
424 }
425 #endif
426 
427 /*
428       Allows the code to build a stack frame as it runs
429 */
430 #if defined(PETSC_USE_DEBUG)
431 
432 #define PETSCSTACKSIZE 64
433 
434 typedef struct  {
435   const char      *function[PETSCSTACKSIZE];
436   const char      *file[PETSCSTACKSIZE];
437   const char      *directory[PETSCSTACKSIZE];
438         int       line[PETSCSTACKSIZE];
439         PetscBool petscroutine[PETSCSTACKSIZE];
440         int       currentsize;
441 } PetscStack;
442 
443 #if defined(PETSC_HAVE_PTHREADCLASSES)
444 #if defined(PETSC_PTHREAD_LOCAL)
445 PETSC_EXTERN PETSC_PTHREAD_LOCAL PetscStack *petscstack;
446 #else
447 PETSC_EXTERN pthread_key_t petscstack;
448 #endif
449 #elif defined(PETSC_HAVE_OPENMP)
450 PETSC_EXTERN PetscStack *petscstack;
451 #pragma omp threadprivate(petscstack)
452 #else
453 PETSC_EXTERN PetscStack *petscstack;
454 #endif
455 
456 PETSC_EXTERN PetscErrorCode PetscStackCopy(PetscStack*,PetscStack*);
457 PETSC_EXTERN PetscErrorCode PetscStackPrint(PetscStack*,FILE* fp);
458 
459 #define PetscStackActive (((PetscStack*)PetscThreadLocalGetValue(petscstack)) != 0)
460 
461 /*MC
462    PetscFunctionBegin - First executable line of each PETSc function
463         used for error handling.
464 
465    Synopsis:
466    void PetscFunctionBegin;
467 
468    Not Collective
469 
470    Usage:
471 .vb
472      int something;
473 
474      PetscFunctionBegin;
475 .ve
476 
477    Notes:
478      Not available in Fortran
479 
480    Level: developer
481 
482 .seealso: PetscFunctionReturn()
483 
484 .keywords: traceback, error handling
485 M*/
486 #define PetscFunctionBegin \
487   do {									\
488     PetscStack* petscstackp;                                            \
489     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);     \
490     if (petscstackp && (petscstackp->currentsize < PETSCSTACKSIZE)) {	\
491       petscstackp->function[petscstackp->currentsize]  = PETSC_FUNCTION_NAME; \
492       petscstackp->file[petscstackp->currentsize]      = __FILE__;        \
493       petscstackp->directory[petscstackp->currentsize] = __SDIR__;        \
494       petscstackp->line[petscstackp->currentsize]      = __LINE__;        \
495       petscstackp->petscroutine[petscstackp->currentsize] = PETSC_TRUE;  \
496       petscstackp->currentsize++;                                        \
497     }                                                                   \
498     PetscCheck__FUNCT__();						\
499     PetscRegister__FUNCT__();						\
500   } while (0)
501 
502 /*MC
503    PetscFunctionBeginUser - First executable line of user provided PETSc routine
504 
505    Synopsis:
506    void PetscFunctionBeginUser;
507 
508    Not Collective
509 
510    Usage:
511 .vb
512      int something;
513 
514      PetscFunctionBegin;
515 .ve
516 
517    Notes:
518      Not available in Fortran
519 
520    Level: developer
521 
522 .seealso: PetscFunctionReturn()
523 
524 .keywords: traceback, error handling
525 M*/
526 #define PetscFunctionBeginUser \
527   do {									\
528     PetscStack* petscstackp;                                            \
529     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);     \
530     if (petscstackp && (petscstackp->currentsize < PETSCSTACKSIZE)) {	\
531       petscstackp->function[petscstackp->currentsize]  = PETSC_FUNCTION_NAME; \
532       petscstackp->file[petscstackp->currentsize]      = __FILE__;        \
533       petscstackp->directory[petscstackp->currentsize] = __SDIR__;        \
534       petscstackp->line[petscstackp->currentsize]      = __LINE__;        \
535       petscstackp->petscroutine[petscstackp->currentsize] = PETSC_FALSE;  \
536       petscstackp->currentsize++;                                        \
537     }                                                                   \
538     PetscCheck__FUNCT__();						\
539     PetscRegister__FUNCT__();						\
540   } while (0)
541 
542 
543 #if defined(PETSC_SERIALIZE_FUNCTIONS)
544 #include <petsc-private/petscfptimpl.h>
545 /*
546    Registers the current function into the global function pointer to function name table
547 
548    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
549 */
550 #define PetscRegister__FUNCT__() do { \
551   static PetscBool __chked = PETSC_FALSE; \
552   if (!__chked) {\
553   void *ptr; PetscDLSym(PETSC_NULL,__FUNCT__,&ptr);\
554   __chked = PETSC_TRUE;\
555   }} while (0)
556 #else
557 #define PetscRegister__FUNCT__()
558 #endif
559 
560 #define PetscCheck__FUNCT__() do { \
561     if (strcmp(PETSC_FUNCTION_NAME,__FUNCT__) && strcmp(__FUNCT__,"User provided function")) { \
562       (*PetscErrorPrintf)("%s%s:%d: __FUNCT__=\"%s\" does not agree with %s=\"%s\"\n",__SDIR__,__FILE__,__LINE__,__FUNCT__,PetscStringize(PETSC_FUNCTION_NAME),PETSC_FUNCTION_NAME); \
563     }                                                                   \
564   } while (0)
565 
566 #define PetscStackPush(n) \
567   do {									\
568     PetscStack * petscstackp;                                           \
569     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);     \
570     if (petscstackp && (petscstackp->currentsize < PETSCSTACKSIZE)) {	\
571       petscstackp->function[petscstackp->currentsize]  = n;		\
572       petscstackp->file[petscstackp->currentsize]      = "unknown";	\
573       petscstackp->directory[petscstackp->currentsize] = "unknown";	\
574       petscstackp->line[petscstackp->currentsize]      = 0;		\
575       petscstackp->currentsize++;					\
576     } CHKMEMQ;} while (0)
577 
578 #define PetscStackPop \
579   do {PetscStack* petscstackp;CHKMEMQ;                                  \
580     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);     \
581     if (petscstackp && petscstackp->currentsize > 0) {			\
582       petscstackp->currentsize--;					\
583       petscstackp->function[petscstackp->currentsize]  = 0;		\
584       petscstackp->file[petscstackp->currentsize]      = 0;		\
585       petscstackp->directory[petscstackp->currentsize] = 0;		\
586       petscstackp->line[petscstackp->currentsize]      = 0;		\
587     }} while (0)
588 
589 /*MC
590    PetscFunctionReturn - Last executable line of each PETSc function
591         used for error handling. Replaces return()
592 
593    Synopsis:
594    void PetscFunctionReturn(0);
595 
596    Not Collective
597 
598    Usage:
599 .vb
600     ....
601      PetscFunctionReturn(0);
602    }
603 .ve
604 
605    Notes:
606      Not available in Fortran
607 
608    Level: developer
609 
610 .seealso: PetscFunctionBegin()
611 
612 .keywords: traceback, error handling
613 M*/
614 #define PetscFunctionReturn(a) \
615   do {									\
616     PetscStack* petscstackp;                                            \
617     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack); \
618     if (petscstackp && petscstackp->currentsize > 0) {			\
619       petscstackp->currentsize--;					\
620       petscstackp->function[petscstackp->currentsize]  = 0;		\
621       petscstackp->file[petscstackp->currentsize]      = 0;		\
622       petscstackp->directory[petscstackp->currentsize] = 0;		\
623       petscstackp->line[petscstackp->currentsize]      = 0;		\
624     }									\
625     return(a);} while (0)
626 
627 #define PetscFunctionReturnVoid() \
628   do {                                                              \
629     PetscStack* petscstackp;                                        \
630     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack); \
631     if (petscstackp && petscstackp->currentsize > 0) {			\
632       petscstackp->currentsize--;					\
633       petscstackp->function[petscstackp->currentsize]  = 0;		\
634       petscstackp->file[petscstackp->currentsize]      = 0;		\
635       petscstackp->directory[petscstackp->currentsize] = 0;		\
636       petscstackp->line[petscstackp->currentsize]      = 0;		\
637     }									\
638     return;} while (0)
639 #else
640 
641 #define PetscFunctionBegin
642 #define PetscFunctionReturn(a)  return(a)
643 #define PetscFunctionReturnVoid() return
644 #define PetscStackPop     CHKMEMQ
645 #define PetscStackPush(f) CHKMEMQ
646 #define PetscStackActive        0
647 
648 #endif
649 
650 /*
651     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.
652 
653    Input Parameters:
654 +   name - string that gives the name of the function being called
655 -   routine - actual call to the routine, including ierr = and CHKERRQ(ierr);
656 
657    Note: Often one should use PetscStackCallStandard() instead
658 
659    Developer Note: this is so that when a user or external library routine results in a crash or corrupts memory, they get blamed instead of PETSc.
660 
661 
662 
663 */
664 #define PetscStackCall(name,routine) PetscStackPush(name);routine;PetscStackPop;
665 
666 /*
667     PetscStackCallStandard - Calls an external library routine after pushing the name of the routine on the stack.
668 
669    Input Parameters:
670 +   func-  name of the routine
671 -   args - arguments to the routine surrounded by ()
672 
673    Developer Note: this is so that when a hypre routine results in a crash or corrupts memory, they get blamed instead of PETSc.
674 
675 */
676 #define PetscStackCallStandard(func,args) do {                        \
677     PetscStackPush(#func);ierr = func args;PetscStackPop; if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s()",#func); \
678   } while (0)
679 
680 PETSC_EXTERN PetscErrorCode PetscStackCreate(void);
681 PETSC_EXTERN PetscErrorCode PetscStackView(PetscViewer);
682 PETSC_EXTERN PetscErrorCode PetscStackDestroy(void);
683 PETSC_EXTERN PetscErrorCode PetscStackPublish(void);
684 PETSC_EXTERN PetscErrorCode PetscStackDepublish(void);
685 
686 #endif
687