Actual source code: petscerror.h

petsc-dev 2014-08-19
Report Typos and Errors
  1: /*
  2:     Contains all error handling interfaces for PETSc.
  3: */

  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: */
 14: #endif

 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/finclude/petscerror.h any CHANGES here
 21:      must be also made there.

 23: */
 24: #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */

 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 */

 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 */

 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 */

 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 */

 63: #define PETSC_ERR_INT_OVERFLOW     84

 65: #define PETSC_ERR_FLOP_COUNT       90
 66: #define PETSC_ERR_NOT_CONVERGED    91  /* solver did not converge */
 67: #define PETSC_ERR_MAX_VALUE        92  /* this is always the one more than the largest error code */

 69: #define PetscStringizeArg(a) #a
 70: #define PetscStringize(a) PetscStringizeArg(a)

 72: #if defined(PETSC_USE_ERRORCHECKING)

 74: /*MC
 75:    SETERRQ - Macro to be called when an error has been detected,

 77:    Synopsis:
 78:    #include <petscsys.h>
 79:    PetscErrorCode SETERRQ(MPI_Comm comm,PetscErrorCode errorcode,char *message)

 81:    Not Collective

 83:    Input Parameters:
 84: +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
 85: -  message - error message

 87:   Level: beginner

 89:    Notes:
 90:     Once the error handler is called the calling function is then returned from with the given error code.

 92:     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments

 94:     In Fortran MPI_Abort() is always called

 96:     Experienced users can set the error handler with PetscPushErrorHandler().

 98:    Concepts: error^setting condition

100: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
101: M*/
102: #define SETERRQ(comm,n,s)              return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s)

104: /*MC
105:    SETERRQ1 - Macro that is called when an error has been detected,

107:    Synopsis:
108:    #include <petscsys.h>
109:    PetscErrorCode SETERRQ1(MPI_Comm comm,PetscErrorCode errorcode,char *formatmessage,arg)

111:    Not Collective

113:    Input Parameters:
114: +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
115: .  message - error message in the printf format
116: -  arg - argument (for example an integer, string or double)

118:   Level: beginner

120:    Notes:
121:     Once the error handler is called the calling function is then returned from with the given error code.

123:    Experienced users can set the error handler with PetscPushErrorHandler().

125:    Concepts: error^setting condition

127: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
128: M*/
129: #define SETERRQ1(comm,n,s,a1)          return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s,a1)

131: /*MC
132:    SETERRQ2 - Macro that is called when an error has been detected,

134:    Synopsis:
135:    #include <petscsys.h>
136:    PetscErrorCode SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)

138:    Not Collective

140:    Input Parameters:
141: +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
142: .  message - error message in the printf format
143: .  arg1 - argument (for example an integer, string or double)
144: -  arg2 - argument (for example an integer, string or double)

146:   Level: beginner

148:    Notes:
149:     Once the error handler is called the calling function is then returned from with the given error code.

151:    Experienced users can set the error handler with PetscPushErrorHandler().

153:    Concepts: error^setting condition

155: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
156: M*/
157: #define SETERRQ2(comm,n,s,a1,a2)       return PetscError(comm,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_INITIAL,s,a1,a2)

159: /*MC
160:    SETERRQ3 - Macro that is called when an error has been detected,

162:    Synopsis:
163:    #include <petscsys.h>
164:    PetscErrorCode SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)

166:    Not Collective

168:    Input Parameters:
169: +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
170: .  message - error message in the printf format
171: .  arg1 - argument (for example an integer, string or double)
172: .  arg2 - argument (for example an integer, string or double)
173: -  arg3 - argument (for example an integer, string or double)

175:   Level: beginner

177:    Notes:
178:     Once the error handler is called the calling function is then returned from with the given error code.

180:     There are also versions for 4, 5, 6 and 7 arguments.

182:    Experienced users can set the error handler with PetscPushErrorHandler().

184:    Concepts: error^setting condition

186: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
187: M*/
188: #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: #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)
191: #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)
192: #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)
193: #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)
194: #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)
195: #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: /*MC
198:    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns

200:    Synopsis:
201:    #include <petscsys.h>
202:    PetscErrorCode CHKERRQ(PetscErrorCode errorcode)

204:    Not Collective

206:    Input Parameters:
207: .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h

209:   Level: beginner

211:    Notes:
212:     Once the error handler is called the calling function is then returned from with the given error code.

214:     Experienced users can set the error handler with PetscPushErrorHandler().

216:     CHKERRQ(n) is fundamentally a macro replacement for
217:          if (n) return(PetscError(...,n,...));

219:     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
220:     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
221:     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
222:     you can use CHKERRV() which returns without an error code (bad idea since the error is ignored or
223:          if (n) {PetscError(....); return(YourReturnType);}
224:     where you may pass back a NULL to indicate an error. You can also call CHKERRABORT(comm,n) to have
225:     MPI_Abort() returned immediately.

227:     In Fortran MPI_Abort() is always called

229:    Concepts: error^setting condition

231: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
232: M*/
233: #define CHKERRQ(n)             do {if (PetscUnlikely(n)) return PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_REPEAT," ");} while (0)

235: #define CHKERRV(n)             do {if (PetscUnlikely(n)) {n = PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_REPEAT," ");return;}} while(0)
236: #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)
237: #define CHKERRCONTINUE(n)      do {if (PetscUnlikely(n)) {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,n,PETSC_ERROR_REPEAT," ");}} while (0)

239: #ifdef PETSC_CLANGUAGE_CXX

241: /*MC
242:    CHKERRXX - Checks error code, if non-zero it calls the C++ error handler which throws an exception

244:    Synopsis:
245:    #include <petscsys.h>
246:    void CHKERRXX(PetscErrorCode errorcode)

248:    Not Collective

250:    Input Parameters:
251: .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h

253:   Level: beginner

255:    Notes:
256:     Once the error handler throws a ??? exception.

258:     You can use CHKERRV() which returns without an error code (bad idea since the error is ignored)
259:     or CHKERRABORT(comm,n) to have MPI_Abort() returned immediately.

261:    Concepts: error^setting condition

263: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKERRQ(), CHKMEMQ
264: M*/
265: #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: #endif

269: /*MC
270:    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected

272:    Synopsis:
273:    #include <petscsys.h>
274:    CHKMEMQ;

276:    Not Collective

278:   Level: beginner

280:    Notes:
281:     We highly recommend using valgrind http://www.mcs.anl.gov/petsc/documentation/faq.html#valgrind for finding memory problems. This is useful
282:     on systems that do not have valgrind, but much much less useful.

284:     Must run with the option -malloc_debug to enable this option

286:     Once the error handler is called the calling function is then returned from with the given error code.

288:     By defaults prints location where memory that is corrupted was allocated.

290:     Use CHKMEMA for functions that return void

292:    Concepts: memory corruption

294: .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
295:           PetscMallocValidate()
296: M*/
297: #define CHKMEMQ do {PetscErrorCode _7_PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__);CHKERRQ(_7_ierr);} while(0)

299: #define CHKMEMA PetscMallocValidate(__LINE__,PETSC_FUNCTION_NAME,__FILE__)

301: #else /* PETSC_USE_ERRORCHECKING */

303: /*
304:     These are defined to be empty for when error checking is turned off, with ./configure --with-errorchecking=0
305: */

307: #define SETERRQ(c,n,s)
308: #define SETERRQ1(c,n,s,a1)
309: #define SETERRQ2(c,n,s,a1,a2)
310: #define SETERRQ3(c,n,s,a1,a2,a3)
311: #define SETERRQ4(c,n,s,a1,a2,a3,a4)
312: #define SETERRQ5(c,n,s,a1,a2,a3,a4,a5)
313: #define SETERRQ6(c,n,s,a1,a2,a3,a4,a5,a6)
314: #define SETERRQ7(c,n,s,a1,a2,a3,a4,a5,a6,a7)
315: #define SETERRQ8(c,n,s,a1,a2,a3,a4,a5,a6,a7,a8)
316: #define SETERRABORT(comm,n,s)

318: #define CHKERRQ(n)     ;
319: #define CHKERRABORT(comm,n) ;
320: #define CHKERRCONTINUE(n) ;
321: #define CHKMEMQ        ;

323: #ifdef PETSC_CLANGUAGE_CXX
324: #define CHKERRXX(n) ;
325: #endif

327: #endif /* PETSC_USE_ERRORCHECKING */

329: /*E
330:   PetscErrorType - passed to the PETSc error handling routines indicating if this is the first or a later call to the error handlers

332:   Level: advanced

334:   PETSC_ERROR_IN_CXX indicates the error was detected in C++ and an exception should be generated

336:   Developer Notes: This is currently used to decide when to print the detailed information about the run in PetscTraceBackErrorHandling()

338: .seealso: PetscError(), SETERRXX()
339: E*/
340: typedef enum {PETSC_ERROR_INITIAL=0,PETSC_ERROR_REPEAT=1,PETSC_ERROR_IN_CXX = 2} PetscErrorType;

342: PETSC_EXTERN PetscErrorCode PetscErrorPrintfInitialize(void);
343: PETSC_EXTERN PetscErrorCode PetscErrorMessage(int,const char*[],char **);
344: PETSC_EXTERN PetscErrorCode PetscTraceBackErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
345: PETSC_EXTERN PetscErrorCode PetscIgnoreErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
346: PETSC_EXTERN PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
347: PETSC_EXTERN PetscErrorCode PetscMPIAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
348: PETSC_EXTERN PetscErrorCode PetscAbortErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
349: PETSC_EXTERN PetscErrorCode PetscAttachDebuggerErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
350: PETSC_EXTERN PetscErrorCode PetscReturnErrorHandler(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*);
351: PETSC_EXTERN PetscErrorCode PetscError(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,...);
352: PETSC_EXTERN PetscErrorCode PetscPushErrorHandler(PetscErrorCode (*handler)(MPI_Comm,int,const char*,const char*,PetscErrorCode,PetscErrorType,const char*,void*),void*);
353: PETSC_EXTERN PetscErrorCode PetscPopErrorHandler(void);
354: PETSC_EXTERN PetscErrorCode PetscSignalHandlerDefault(int,void*);
355: PETSC_EXTERN PetscErrorCode PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
356: PETSC_EXTERN PetscErrorCode PetscPopSignalHandler(void);

359: /*MC
360:     PetscErrorPrintf - Prints error messages.

362:    Synopsis:
363:     #include <petscsys.h>
364:      PetscErrorCode (*PetscErrorPrintf)(const char format[],...);

366:     Not Collective

368:     Input Parameters:
369: .   format - the usual printf() format string

371:    Options Database Keys:
372: +    -error_output_stdout - cause error messages to be printed to stdout instead of the  (default) stderr
373: -    -error_output_none to turn off all printing of error messages (does not change the way the error is handled.)

375:    Notes: Use
376: $     PetscErrorPrintf = PetscErrorPrintfNone; to turn off all printing of error messages (does not change the way the
377: $                        error is handled.) and
378: $     PetscErrorPrintf = PetscErrorPrintfDefault; to turn it back on or you can use your own function

380:           Use
381:      PETSC_STDERR = FILE* obtained from a file open etc. to have stderr printed to the file.
382:      PETSC_STDOUT = FILE* obtained from a file open etc. to have stdout printed to the file.

384:           Use
385:       PetscPushErrorHandler() to provide your own error handler that determines what kind of messages to print

387:    Level: developer

389:     Fortran Note:
390:     This routine is not supported in Fortran.

392:     Concepts: error messages^printing
393:     Concepts: printing^error messages

395: .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscHelpPrintf(), PetscPrintf(), PetscErrorHandlerPush(), PetscVFPrintf(), PetscHelpPrintf()
396: M*/
397: PETSC_EXTERN PetscErrorCode (*PetscErrorPrintf)(const char[],...);

399: typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
400: PETSC_EXTERN PetscErrorCode PetscSetFPTrap(PetscFPTrap);
401: PETSC_EXTERN PetscErrorCode PetscFPTrapPush(PetscFPTrap);
402: PETSC_EXTERN PetscErrorCode PetscFPTrapPop(void);

404: /*  Linux functions CPU_SET and others don't work if sched.h is not included before
405:     including pthread.h. Also, these functions are active only if either _GNU_SOURCE
406:     or __USE_GNU is not set (see /usr/include/sched.h and /usr/include/features.h), hence
407:     set these first.
408: */
409: #if defined(PETSC_HAVE_PTHREADCLASSES) || defined (PETSC_HAVE_OPENMP)
410: #if defined(PETSC_HAVE_SCHED_H)
411: #ifndef _GNU_SOURCE
412: #define _GNU_SOURCE
413: #endif
414: #include <sched.h>
415: #endif
416: #include <pthread.h>
417: #endif

419: /*
420:      This code is for managing thread local global variables. Each of Linux, Microsoft WINDOWS, OpenMP, and Apple OS X have
421:    different ways to indicate this. On OS X each thread local global is accessed by using a pthread_key_t for that variable.
422:    Thus we have functions for creating destroying and using the keys. Except for OS X these access functions merely directly 
423:    acess the thread local variable.
424: */

426: #if defined(PETSC_HAVE_PTHREADCLASSES) && !defined(PETSC_PTHREAD_LOCAL)
427: typedef pthread_key_t PetscThreadKey;
428: /* Get the value associated with key */
429: PETSC_STATIC_INLINE void* PetscThreadLocalGetValue(PetscThreadKey key)
430: {
431:   return pthread_getspecific(key);
432: }

434: /* Set the value for key */
435: PETSC_STATIC_INLINE void PetscThreadLocalSetValue(PetscThreadKey *key,void* value)
436: {
437:   pthread_setspecific(*key,(void*)value);
438: }

440: /* Create pthread thread local key */
441: PETSC_STATIC_INLINE void PetscThreadLocalRegister(PetscThreadKey *key)
442: {
443:   pthread_key_create(key,NULL);
444: }

446: /* Delete pthread thread local key */
447: PETSC_STATIC_INLINE void PetscThreadLocalDestroy(PetscThreadKey key)
448: {
449:   pthread_key_delete(key);
450: }
451: #else
452: typedef void* PetscThreadKey;
453: PETSC_STATIC_INLINE void* PetscThreadLocalGetValue(PetscThreadKey key)
454: {
455:   return key;
456: }

458: PETSC_STATIC_INLINE void PetscThreadLocalSetValue(PetscThreadKey *key,void* value)
459: {
460:   *key = value;
461: }

463: PETSC_STATIC_INLINE void PetscThreadLocalRegister(PETSC_UNUSED PetscThreadKey *key)
464: {
465: }

467: PETSC_STATIC_INLINE void PetscThreadLocalDestroy(PETSC_UNUSED PetscThreadKey key)
468: {
469: }
470: #endif

472: /*
473:       Allows the code to build a stack frame as it runs
474: */

476: #define PETSCSTACKSIZE 64

478: typedef struct  {
479:   const char      *function[PETSCSTACKSIZE];
480:   const char      *file[PETSCSTACKSIZE];
481:         int       line[PETSCSTACKSIZE];
482:         PetscBool petscroutine[PETSCSTACKSIZE];
483:         int       currentsize;
484:         int       hotdepth;
485: } PetscStack;

487: #if defined(PETSC_HAVE_PTHREADCLASSES)
488: #if defined(PETSC_PTHREAD_LOCAL)
489: PETSC_EXTERN PETSC_PTHREAD_LOCAL PetscStack *petscstack;
490: #else
491: PETSC_EXTERN PetscThreadKey petscstack;
492: #endif
493: #elif defined(PETSC_HAVE_OPENMP)
494: PETSC_EXTERN PetscStack *petscstack;
495: #pragma omp threadprivate(petscstack)
496: #else
497: PETSC_EXTERN PetscStack *petscstack;
498: #endif

500: PETSC_EXTERN PetscErrorCode PetscStackCopy(PetscStack*,PetscStack*);
501: PETSC_EXTERN PetscErrorCode PetscStackPrint(PetscStack*,FILE* fp);

503: #if defined(PETSC_USE_DEBUG)
504: PETSC_STATIC_INLINE PetscBool PetscStackActive(void)
505: {
506:   return(PetscThreadLocalGetValue(petscstack) ? PETSC_TRUE : PETSC_FALSE);
507: }

509: /* Stack handling is based on the following two "NoCheck" macros.  These should only be called directly by other error
510:  * handling macros.  We record the line of the call, which may or may not be the location of the definition.  But is at
511:  * least more useful than "unknown" because it can distinguish multiple calls from the same function.
512:  */

514: #define PetscStackPushNoCheck(funct,petsc_routine,hot)                        \
515:   do {                                                                        \
516:     PetscStack* petscstackp;                                                  \
517:     PetscStackSAWsTakeAccess();                                                \
518:     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);          \
519:     if (petscstackp && (petscstackp->currentsize < PETSCSTACKSIZE)) {         \
520:       petscstackp->function[petscstackp->currentsize]  = funct;               \
521:       petscstackp->file[petscstackp->currentsize]      = __FILE__;            \
522:       petscstackp->line[petscstackp->currentsize]      = __LINE__;            \
523:       petscstackp->petscroutine[petscstackp->currentsize] = petsc_routine;    \
524:       petscstackp->currentsize++;                                             \
525:     }                                                                         \
526:     if (petscstackp) {                                                        \
527:       petscstackp->hotdepth += (hot || petscstackp->hotdepth);                \
528:     }                                                                         \
529:     PetscStackSAWsGrantAccess();                                               \
530:   } while (0)

532: #define PetscStackPopNoCheck                                            \
533:   do {PetscStack* petscstackp;                                          \
534:     PetscStackSAWsTakeAccess();                                          \
535:     petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);    \
536:     if (petscstackp && petscstackp->currentsize > 0) {                  \
537:       petscstackp->currentsize--;                                       \
538:       petscstackp->function[petscstackp->currentsize]  = 0;             \
539:       petscstackp->file[petscstackp->currentsize]      = 0;             \
540:       petscstackp->line[petscstackp->currentsize]      = 0;             \
541:       petscstackp->petscroutine[petscstackp->currentsize] = PETSC_FALSE;\
542:     }                                                                   \
543:     if (petscstackp) {                                                  \
544:       petscstackp->hotdepth = PetscMax(petscstackp->hotdepth-1,0);      \
545:     }                                                                   \
546:     PetscStackSAWsGrantAccess();                                         \
547:   } while (0)

549: /*MC
551:       line of PETSc functions should be return(0);

553:    Synopsis:
554:    #include <petscsys.h>

557:    Not Collective

559:    Usage:
560: .vb
561:      int something;

564: .ve

566:    Notes:

569:      Not available in Fortran

571:    Level: developer


575: .keywords: traceback, error handling
576: M*/
578:     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_TRUE,PETSC_FALSE); \
580:     PetscRegister__FUNCT__();                                          \
581:   } while (0)

583: /*MC
585:    performance-critical circumstances.  Use of this function allows for lighter profiling by default.

587:    Synopsis:
588:    #include <petscsys.h>

591:    Not Collective

593:    Usage:
594: .vb
595:      int something;

598: .ve

600:    Notes:
601:      Not available in Fortran

603:    Level: developer


607: .keywords: traceback, error handling
608: M*/
610:     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_TRUE,PETSC_TRUE);  \
612:     PetscRegister__FUNCT__();                                          \
613:   } while (0)

615: /*MC

618:    Synopsis:
619:    #include <petscsys.h>

622:    Not Collective

624:    Usage:
625: .vb
626:      int something;

629: .ve

631:    Notes:
632:       Final line of PETSc functions should be return(0) except for main().

634:       Not available in Fortran

636:    Level: intermediate


640: .keywords: traceback, error handling
641: M*/
643:   do {                                                                  \
644:     PetscStackPushNoCheck(PETSC_FUNCTION_NAME,PETSC_FALSE,PETSC_FALSE); \
646:     PetscRegister__FUNCT__();                                           \
647:   } while (0)


650: #if defined(PETSC_SERIALIZE_FUNCTIONS)
651: #include <petsc-private/petscfptimpl.h>
652: /*
653:    Registers the current function into the global function pointer to function name table

655:    Have to fix this to handle errors but cannot return error since used in PETSC_VIEWER_DRAW_() etc
656: */
657: #define PetscRegister__FUNCT__() do { \
658:   static PetscBool __chked = PETSC_FALSE; \
659:   if (!__chked) {\
660:   void *ptr; PetscDLSym(NULL,__FUNCT__,&ptr);\
661:   __chked = PETSC_TRUE;\
662:   }} while (0)
663: #else
664: #define PetscRegister__FUNCT__()
665: #endif

668:     PetscStrcmpNoError(PETSC_FUNCTION_NAME,__FUNCT__,&_sc1);\
669:     PetscStrcmpNoError(__FUNCT__,"User provided function",&_sc2);\
670:     if (!_sc1 && !_sc2) { \
671:       printf("%s:%d: __FUNCT__=\"%s\" does not agree with %s=\"%s\"\n",__FILE__,__LINE__,__FUNCT__,PetscStringize(PETSC_FUNCTION_NAME),PETSC_FUNCTION_NAME); \
672:     }                                                                   \
673:   } while (0)

675: #define PetscStackPush(n) \
676:   do {                                                                  \
677:     PetscStackPushNoCheck(n,PETSC_FALSE,PETSC_FALSE);                   \
678:     CHKMEMQ;                                                            \
679:   } while (0)

681: #define PetscStackPop                           \
682:     do {                                        \
683:       CHKMEMQ;                                  \
684:       PetscStackPopNoCheck;                     \
685:     } while (0)

687: /*MC
688:    PetscFunctionReturn - Last executable line of each PETSc function
689:         used for error handling. Replaces return()

691:    Synopsis:
692:    #include <petscsys.h>
693:    void return(0);

695:    Not Collective

697:    Usage:
698: .vb
699:     ....
700:      return(0);
701:    }
702: .ve

704:    Notes:
705:      Not available in Fortran

707:    Level: developer


711: .keywords: traceback, error handling
712: M*/
713: #define PetscFunctionReturn(a) \
714:   do {                                                                \
715:     PetscStackPopNoCheck;                                             \
716:     return(a);} while (0)

718: #define PetscFunctionReturnVoid() \
719:   do {                                                                \
720:     PetscStackPopNoCheck;                                             \
721:     return;} while (0)

723: #else

725: PETSC_STATIC_INLINE PetscBool PetscStackActive(void) {return PETSC_FALSE;}
726: #define PetscStackPushNoCheck(funct,petsc_routine,hot) do {} while (0)
727: #define PetscStackPopNoCheck                           do {} while (0)
731: #define PetscFunctionReturn(a)    return(a)
732: #define PetscFunctionReturnVoid() return
733: #define PetscStackPop             CHKMEMQ
734: #define PetscStackPush(f)         CHKMEMQ

736: #endif

738: /*
739:     PetscStackCall - Calls an external library routine or user function after pushing the name of the routine on the stack.

741:    Input Parameters:
742: +   name - string that gives the name of the function being called
743: -   routine - actual call to the routine, including and 

745:    Note: Often one should use PetscStackCallStandard() instead. This routine is intended for external library routines that DO NOT return error codes

747:    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.



751: */
752: #define PetscStackCall(name,routine) do { PetscStackPush(name);routine;PetscStackPop; } while(0)

754: /*
755:     PetscStackCallStandard - Calls an external library routine after pushing the name of the routine on the stack.

757:    Input Parameters:
758: +   func-  name of the routine
759: -   args - arguments to the routine surrounded by ()

761:    Notes: This is intended for external package routines that return error codes. Use PetscStackCall() for those that do not.

763:    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: #define PetscStackCallStandard(func,args) do {                        \
767:     PetscStackPush(#func);func args;PetscStackPop; if (ierr) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error in %s()",#func); \
768:   } while (0)

770: PETSC_EXTERN PetscErrorCode PetscStackCreate(void);
771: PETSC_EXTERN PetscErrorCode PetscStackView(FILE*);
772: PETSC_EXTERN PetscErrorCode PetscStackDestroy(void);

774: #endif