1 /* 2 Contains all error handling code for PETSc. 3 */ 4 #if !defined(__PETSCERROR_H) 5 #define __PETSCERROR_H 6 #include "petsc.h" 7 PETSC_EXTERN_CXX_BEGIN 8 9 /* 10 Defines the directory where the compiled source is located; used 11 in printing error messages. Each makefile has an entry 12 LOCDIR = thedirectory 13 and bmake/common_variables includes in CCPPFLAGS -D__SDIR__='"${LOCDIR}"' 14 which is a flag passed to the C/C++ compilers. 15 */ 16 #if !defined(__SDIR__) 17 #define __SDIR__ "unknowndirectory/" 18 #endif 19 20 /* 21 Defines the function where the compiled source is located; used 22 in printing error messages. 23 */ 24 #if !defined(__FUNCT__) 25 #define __FUNCT__ "User provided function" 26 #endif 27 28 /* 29 These are the generic error codes. These error codes are used 30 many different places in the PETSc source code. The string versions are 31 at src/sys/src/error/err.c any changes here must also be made there 32 33 */ 34 #define PETSC_ERR_MEM 55 /* unable to allocate requested memory */ 35 #define PETSC_ERR_MEM_MALLOC_0 85 /* cannot malloc zero size */ 36 #define PETSC_ERR_SUP 56 /* no support for requested operation */ 37 #define PETSC_ERR_SUP_SYS 57 /* no support for requested operation on this computer system */ 38 #define PETSC_ERR_ORDER 58 /* operation done in wrong order */ 39 #define PETSC_ERR_SIG 59 /* signal received */ 40 #define PETSC_ERR_FP 72 /* floating point exception */ 41 #define PETSC_ERR_COR 74 /* corrupted PETSc object */ 42 #define PETSC_ERR_LIB 76 /* error in library called by PETSc */ 43 #define PETSC_ERR_PLIB 77 /* PETSc library generated inconsistent data */ 44 #define PETSC_ERR_MEMC 78 /* memory corruption */ 45 #define PETSC_ERR_CONV_FAILED 82 /* iterative method (KSP or SNES) failed */ 46 #define PETSC_ERR_USER 83 /* user has not provided needed function */ 47 48 #define PETSC_ERR_ARG_SIZ 60 /* nonconforming object sizes used in operation */ 49 #define PETSC_ERR_ARG_IDN 61 /* two arguments not allowed to be the same */ 50 #define PETSC_ERR_ARG_WRONG 62 /* wrong argument (but object probably ok) */ 51 #define PETSC_ERR_ARG_CORRUPT 64 /* null or corrupted PETSc object as argument */ 52 #define PETSC_ERR_ARG_OUTOFRANGE 63 /* input argument, out of range */ 53 #define PETSC_ERR_ARG_BADPTR 68 /* invalid pointer argument */ 54 #define PETSC_ERR_ARG_NOTSAMETYPE 69 /* two args must be same object type */ 55 #define PETSC_ERR_ARG_NOTSAMECOMM 80 /* two args must be same communicators */ 56 #define PETSC_ERR_ARG_WRONGSTATE 73 /* object in argument is in wrong state, e.g. unassembled mat */ 57 #define PETSC_ERR_ARG_INCOMP 75 /* two arguments are incompatible */ 58 #define PETSC_ERR_ARG_NULL 85 /* argument is null that should not be */ 59 #define PETSC_ERR_ARG_UNKNOWN_TYPE 86 /* type name doesn't match any registered type */ 60 #define PETSC_ERR_ARG_DOMAIN 87 /* argument is not in domain of function */ 61 62 #define PETSC_ERR_FILE_OPEN 65 /* unable to open file */ 63 #define PETSC_ERR_FILE_READ 66 /* unable to read from file */ 64 #define PETSC_ERR_FILE_WRITE 67 /* unable to write to file */ 65 #define PETSC_ERR_FILE_UNEXPECTED 79 /* unexpected data in file */ 66 67 #define PETSC_ERR_MAT_LU_ZRPVT 71 /* detected a zero pivot during LU factorization */ 68 #define PETSC_ERR_MAT_CH_ZRPVT 81 /* detected a zero pivot during Cholesky factorization */ 69 70 #if defined(PETSC_USE_ERRORCHECKING) 71 72 /*MC 73 SETERRQ - Macro that is called when an error has been detected, 74 75 Not Collective 76 77 Synopsis: 78 void SETERRQ(PetscErrorCode errorcode,char *message) 79 80 81 Input Parameters: 82 + errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h 83 - message - error message 84 85 Level: beginner 86 87 Notes: 88 Once the error handler is called the calling function is then returned from with the given error code. 89 90 See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments 91 92 93 Experienced users can set the error handler with PetscPushErrorHandler(). 94 95 Concepts: error^setting condition 96 97 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3() 98 M*/ 99 #define SETERRQ(n,s) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);} 100 101 /*MC 102 SETERRQ1 - Macro that is called when an error has been detected, 103 104 Not Collective 105 106 Synopsis: 107 void SETERRQ1(PetscErrorCode errorcode,char *formatmessage,arg) 108 109 110 Input Parameters: 111 + errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h 112 . message - error message in the printf format 113 - arg - argument (for example an integer, string or double) 114 115 Level: beginner 116 117 Notes: 118 Once the error handler is called the calling function is then returned from with the given error code. 119 120 Experienced users can set the error handler with PetscPushErrorHandler(). 121 122 Concepts: error^setting condition 123 124 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3() 125 M*/ 126 #define SETERRQ1(n,s,a1) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1);} 127 128 /*MC 129 SETERRQ2 - Macro that is called when an error has been detected, 130 131 Not Collective 132 133 Synopsis: 134 void SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2) 135 136 137 Input Parameters: 138 + errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h 139 . message - error message in the printf format 140 . arg1 - argument (for example an integer, string or double) 141 - arg2 - argument (for example an integer, string or double) 142 143 Level: beginner 144 145 Notes: 146 Once the error handler is called the calling function is then returned from with the given error code. 147 148 Experienced users can set the error handler with PetscPushErrorHandler(). 149 150 Concepts: error^setting condition 151 152 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3() 153 M*/ 154 #define SETERRQ2(n,s,a1,a2) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2);} 155 156 /*MC 157 SETERRQ3 - Macro that is called when an error has been detected, 158 159 Not Collective 160 161 Synopsis: 162 void SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3) 163 164 165 Input Parameters: 166 + errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h 167 . message - error message in the printf format 168 . arg1 - argument (for example an integer, string or double) 169 . arg2 - argument (for example an integer, string or double) 170 - arg3 - argument (for example an integer, string or double) 171 172 Level: beginner 173 174 Notes: 175 Once the error handler is called the calling function is then returned from with the given error code. 176 177 Experienced users can set the error handler with PetscPushErrorHandler(). 178 179 Concepts: error^setting condition 180 181 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2() 182 M*/ 183 #define SETERRQ3(n,s,a1,a2,a3) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3);} 184 185 #define SETERRQ4(n,s,a1,a2,a3,a4) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4);} 186 #define SETERRQ5(n,s,a1,a2,a3,a4,a5) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5);} 187 #define SETERRQ6(n,s,a1,a2,a3,a4,a5,a6) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5,a6);} 188 #define SETERRQ7(n,s,a1,a2,a3,a4,a5,a6,a7) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5,a6,a7);} 189 #define SETERRABORT(comm,n,s) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);MPI_Abort(comm,n);} 190 191 /*MC 192 CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns 193 194 Not Collective 195 196 Synopsis: 197 void CHKERRQ(PetscErrorCode errorcode) 198 199 200 Input Parameters: 201 . errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h 202 203 Level: beginner 204 205 Notes: 206 Once the error handler is called the calling function is then returned from with the given error code. 207 208 Experienced users can set the error handler with PetscPushErrorHandler(). 209 210 CHKERRQ(n) is fundamentally a macro replacement for 211 if (n) return(PetscError(...,n,...)); 212 213 Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is 214 highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular, 215 it cannot be used in functions which return(void) or any other datatype. In these types of functions, 216 a more appropriate construct for using PETSc Error Handling would be 217 if (n) {PetscError(....); return(YourReturnType);} 218 219 Concepts: error^setting condition 220 221 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2() 222 M*/ 223 #define CHKERRQ(n) if (n) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");} 224 225 #define CHKERRABORT(comm,n) if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");MPI_Abort(comm,n);} 226 #define CHKERRCONTINUE(n) if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");} 227 228 /*MC 229 CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected 230 231 Not Collective 232 233 Synopsis: 234 CHKMEMQ; 235 236 Level: beginner 237 238 Notes: 239 Must run with the option -malloc_debug to enable this option 240 241 Once the error handler is called the calling function is then returned from with the given error code. 242 243 By defaults prints location where memory that is corrupted was allocated. 244 245 Concepts: memory corruption 246 247 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(), 248 PetscMallocValidate() 249 M*/ 250 #define CHKMEMQ {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);CHKERRQ(_7_ierr);} 251 252 #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR) 253 extern PetscErrorCode __gierr; 254 #define _ __gierr = 255 #define ___ CHKERRQ(__gierr); 256 #endif 257 258 #define PETSC_EXCEPTIONS_MAX 256 259 extern PetscErrorCode PetscErrorUncatchable[PETSC_EXCEPTIONS_MAX]; 260 extern PetscInt PetscErrorUncatchableCount; 261 extern PetscErrorCode PetscExceptions[PETSC_EXCEPTIONS_MAX]; 262 extern PetscInt PetscExceptionsCount; 263 264 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscExceptionPush(PetscErrorCode); 265 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorSetCatchable(PetscErrorCode,PetscTruth); 266 EXTERN void PETSC_DLLEXPORT PetscExceptionPop(PetscErrorCode); 267 268 static inline PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr) { 269 PetscInt i; 270 if (xierr != zierr) return PETSC_FALSE; 271 for (i=0; i<PetscErrorUncatchableCount; i++) { 272 if (PetscErrorUncatchable[i] == zierr) { 273 return PETSC_FALSE; 274 } 275 } 276 return PETSC_TRUE; 277 } 278 279 280 static inline PetscTruth PetscExceptionValue(PetscErrorCode zierr) { 281 PetscInt i; 282 for (i=0; i<PetscExceptionsCount; i++) { 283 if (PetscExceptions[i] == zierr) { 284 return PETSC_TRUE; 285 } 286 } 287 return PETSC_FALSE; 288 } 289 290 /*MC 291 PetscExceptionTry1 - Runs the routine, causing a particular error code to be treated as an exception, 292 rather than an error. That is if that error code is treated the program returns to this level, 293 but does not call the error handlers 294 295 Not Collective 296 297 Synopsis: 298 PetscExceptionTry1(PetscErrorCode routine(....),PetscErrorCode); 299 300 Level: advanced 301 302 Notes: 303 PETSc must not be configured using the option --with-errorchecking=0 for this to work 304 305 306 Concepts: exceptions, exception hanlding 307 308 .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(), 309 CHKERRQ() 310 M*/ 311 extern PetscErrorCode PetscExceptionTmp; 312 #define PetscExceptionTry1(a,b) (PetscExceptionTmp = PetscExceptionPush(b)) ? PetscExceptionTmp : (PetscExceptionTmp = a , PetscExceptionPop(b),PetscExceptionTmp) 313 314 #else 315 #define SETERRQ(n,s) ; 316 #define SETERRQ1(n,s,a1) ; 317 #define SETERRQ2(n,s,a1,a2) ; 318 #define SETERRQ3(n,s,a1,a2,a3) ; 319 #define SETERRQ4(n,s,a1,a2,a3,a4) ; 320 #define SETERRQ5(n,s,a1,a2,a3,a4,a5) ; 321 #define SETERRQ6(n,s,a1,a2,a3,a4,a5,a6) ; 322 #define SETERRABORT(comm,n,s) ; 323 324 #define CHKERRQ(n) ; 325 #define CHKERRABORT(comm,n) ; 326 #define CHKERRCONTINUE(n) ; 327 328 #define CHKMEMQ ; 329 330 #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR) 331 #define _ 332 #define ___ 333 #endif 334 335 #define PetscExceptionTry1(a,b) a 336 #endif 337 338 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfInitialize(void); 339 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorMessage(int,const char*[],char **); 340 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscTraceBackErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*); 341 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscIgnoreErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*); 342 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscEmacsClientErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*); 343 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscStopErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*); 344 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscAbortErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*); 345 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscAttachDebuggerErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*); 346 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscError(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,...) PETSC_PRINTF_FORMAT_CHECK(7,8); 347 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPushErrorHandler(PetscErrorCode (*handler)(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*),void*); 348 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPopErrorHandler(void); 349 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscDefaultSignalHandler(int,void*); 350 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*); 351 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPopSignalHandler(void); 352 353 typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap; 354 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscSetFPTrap(PetscFPTrap); 355 356 /* 357 Allows the code to build a stack frame as it runs 358 */ 359 #if defined(PETSC_USE_DEBUG) 360 361 #define PETSCSTACKSIZE 15 362 363 typedef struct { 364 const char *function[PETSCSTACKSIZE]; 365 const char *file[PETSCSTACKSIZE]; 366 const char *directory[PETSCSTACKSIZE]; 367 int line[PETSCSTACKSIZE]; 368 int currentsize; 369 } PetscStack; 370 371 extern PETSC_DLLEXPORT PetscStack *petscstack; 372 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscStackCopy(PetscStack*,PetscStack*); 373 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscStackPrint(PetscStack*,FILE* fp); 374 375 #define PetscStackActive (petscstack != 0) 376 377 378 /*MC 379 PetscFunctionBegin - First executable line of each PETSc function 380 used for error handling. 381 382 Synopsis: 383 void PetscFunctionBegin; 384 385 Usage: 386 .vb 387 int something; 388 389 PetscFunctionBegin; 390 .ve 391 392 Notes: 393 Not available in Fortran 394 395 Level: developer 396 397 .seealso: PetscFunctionReturn() 398 399 .keywords: traceback, error handling 400 M*/ 401 #define PetscFunctionBegin \ 402 {\ 403 if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) { \ 404 petscstack->function[petscstack->currentsize] = __FUNCT__; \ 405 petscstack->file[petscstack->currentsize] = __FILE__; \ 406 petscstack->directory[petscstack->currentsize] = __SDIR__; \ 407 petscstack->line[petscstack->currentsize] = __LINE__; \ 408 petscstack->currentsize++; \ 409 }} 410 411 #define PetscStackPush(n) \ 412 {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) { \ 413 petscstack->function[petscstack->currentsize] = n; \ 414 petscstack->file[petscstack->currentsize] = "unknown"; \ 415 petscstack->directory[petscstack->currentsize] = "unknown"; \ 416 petscstack->line[petscstack->currentsize] = 0; \ 417 petscstack->currentsize++; \ 418 }} 419 420 #define PetscStackPop \ 421 {if (petscstack && petscstack->currentsize > 0) { \ 422 petscstack->currentsize--; \ 423 petscstack->function[petscstack->currentsize] = 0; \ 424 petscstack->file[petscstack->currentsize] = 0; \ 425 petscstack->directory[petscstack->currentsize] = 0; \ 426 petscstack->line[petscstack->currentsize] = 0; \ 427 }}; 428 429 /*MC 430 PetscFunctionReturn - Last executable line of each PETSc function 431 used for error handling. Replaces return() 432 433 Synopsis: 434 void PetscFunctionReturn(0); 435 436 Usage: 437 .vb 438 .... 439 PetscFunctionReturn(0); 440 } 441 .ve 442 443 Notes: 444 Not available in Fortran 445 446 Level: developer 447 448 .seealso: PetscFunctionBegin() 449 450 .keywords: traceback, error handling 451 M*/ 452 #define PetscFunctionReturn(a) \ 453 {\ 454 PetscStackPop; \ 455 return(a);} 456 457 #define PetscFunctionReturnVoid() \ 458 {\ 459 PetscStackPop; \ 460 return;} 461 462 463 #else 464 465 #define PetscFunctionBegin 466 #define PetscFunctionReturn(a) return(a) 467 #define PetscFunctionReturnVoid() return 468 #define PetscStackPop 469 #define PetscStackPush(f) 470 #define PetscStackActive 0 471 472 #endif 473 474 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscStackCreate(void); 475 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscStackView(PetscViewer); 476 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscStackDestroy(void); 477 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscStackPublish(void); 478 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscStackDepublish(void); 479 480 481 PETSC_EXTERN_CXX_END 482 #endif 483