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