17d0a6c19SBarry Smith 2e5c89e4eSSatish Balay /* 3e5c89e4eSSatish Balay * IEEE error handler for all machines. Since each machine has 4e5c89e4eSSatish Balay * enough slight differences we have completely separate codes for each one. 5e5c89e4eSSatish Balay * 6e5c89e4eSSatish Balay */ 7b014e56cSJed Brown 8b014e56cSJed Brown /* 9b014e56cSJed Brown This feature test macro provides FE_NOMASK_ENV on GNU. It must be defined 10b014e56cSJed Brown at the top of the file because other headers may pull in fenv.h even when 11b014e56cSJed Brown not strictly necessary. Strictly speaking, we could include ONLY petscconf.h, 12b014e56cSJed Brown check PETSC_HAVE_FENV_H, and only define _GNU_SOURCE in that case, but such 13b014e56cSJed Brown shenanigans ought to be unnecessary. 14b014e56cSJed Brown */ 15519f805aSKarl Rupp #if !defined(_GNU_SOURCE) 16b014e56cSJed Brown #define _GNU_SOURCE 1776a6984eSJed Brown #endif 18b014e56cSJed Brown 19c6db04a5SJed Brown #include <petscsys.h> /*I "petscsys.h" I*/ 20e5c89e4eSSatish Balay #include <signal.h> 21e5c89e4eSSatish Balay 22670f3ff9SJed Brown struct PetscFPTrapLink { 23670f3ff9SJed Brown PetscFPTrap trapmode; 24670f3ff9SJed Brown struct PetscFPTrapLink *next; 25670f3ff9SJed Brown }; 26670f3ff9SJed Brown static PetscFPTrap _trapmode = PETSC_FP_TRAP_OFF; /* Current trapping mode */ 27670f3ff9SJed Brown static struct PetscFPTrapLink *_trapstack; /* Any pushed states of _trapmode */ 28670f3ff9SJed Brown 29670f3ff9SJed Brown #undef __FUNCT__ 30670f3ff9SJed Brown #define __FUNCT__ "PetscFPTrapPush" 31670f3ff9SJed Brown /*@ 32670f3ff9SJed Brown PetscFPTrapPush - push a floating point trapping mode, to be restored using PetscFPTrapPop() 33670f3ff9SJed Brown 34670f3ff9SJed Brown Not Collective 35670f3ff9SJed Brown 36670f3ff9SJed Brown Input Arguments: 37670f3ff9SJed Brown . trap - PETSC_FP_TRAP_ON or PETSC_FP_TRAP_OFF 38670f3ff9SJed Brown 39670f3ff9SJed Brown Level: advanced 40670f3ff9SJed Brown 41670f3ff9SJed Brown .seealso: PetscFPTrapPop(), PetscSetFPTrap() 42670f3ff9SJed Brown @*/ 43670f3ff9SJed Brown PetscErrorCode PetscFPTrapPush(PetscFPTrap trap) 44670f3ff9SJed Brown { 45670f3ff9SJed Brown PetscErrorCode ierr; 46670f3ff9SJed Brown struct PetscFPTrapLink *link; 47670f3ff9SJed Brown 48670f3ff9SJed Brown PetscFunctionBegin; 498caf3d72SBarry Smith ierr = PetscMalloc(sizeof(*link),&link);CHKERRQ(ierr); 50670f3ff9SJed Brown link->trapmode = _trapmode; 51670f3ff9SJed Brown link->next = _trapstack; 52670f3ff9SJed Brown _trapstack = link; 53f6883b22SJed Brown if (trap != _trapmode) {ierr = PetscSetFPTrap(trap);CHKERRQ(ierr);} 54670f3ff9SJed Brown PetscFunctionReturn(0); 55670f3ff9SJed Brown } 56670f3ff9SJed Brown 57670f3ff9SJed Brown #undef __FUNCT__ 58670f3ff9SJed Brown #define __FUNCT__ "PetscFPTrapPop" 59670f3ff9SJed Brown /*@ 60670f3ff9SJed Brown PetscFPTrapPop - push a floating point trapping mode, to be restored using PetscFPTrapPop() 61670f3ff9SJed Brown 62670f3ff9SJed Brown Not Collective 63670f3ff9SJed Brown 64670f3ff9SJed Brown Level: advanced 65670f3ff9SJed Brown 66670f3ff9SJed Brown .seealso: PetscFPTrapPush(), PetscSetFPTrap() 67670f3ff9SJed Brown @*/ 68670f3ff9SJed Brown PetscErrorCode PetscFPTrapPop(void) 69670f3ff9SJed Brown { 70670f3ff9SJed Brown PetscErrorCode ierr; 71670f3ff9SJed Brown struct PetscFPTrapLink *link; 72670f3ff9SJed Brown 73670f3ff9SJed Brown PetscFunctionBegin; 74f6883b22SJed Brown if (_trapstack->trapmode != _trapmode) {ierr = PetscSetFPTrap(_trapstack->trapmode);CHKERRQ(ierr);} 75670f3ff9SJed Brown link = _trapstack; 76670f3ff9SJed Brown _trapstack = _trapstack->next; 77670f3ff9SJed Brown ierr = PetscFree(link);CHKERRQ(ierr); 78670f3ff9SJed Brown PetscFunctionReturn(0); 79670f3ff9SJed Brown } 80670f3ff9SJed Brown 81e5c89e4eSSatish Balay /*--------------------------------------- ---------------------------------------------------*/ 82e5c89e4eSSatish Balay #if defined(PETSC_HAVE_SUN4_STYLE_FPTRAP) 83e5c89e4eSSatish Balay #include <floatingpoint.h> 84e5c89e4eSSatish Balay 85*8cc058d9SJed Brown PETSC_EXTERN PetscErrorCode ieee_flags(char*,char*,char*,char**); 86*8cc058d9SJed Brown PETSC_EXTERN PetscErrorCode ieee_handler(char*,char*,sigfpe_handler_type(int,int,struct sigcontext*,char*)); 87e5c89e4eSSatish Balay 88db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 89e5c89e4eSSatish Balay { FPE_INTDIV_TRAP ,"integer divide" }, 90e5c89e4eSSatish Balay { FPE_FLTOPERR_TRAP ,"IEEE operand error" }, 91e5c89e4eSSatish Balay { FPE_FLTOVF_TRAP ,"floating point overflow" }, 92e5c89e4eSSatish Balay { FPE_FLTUND_TRAP ,"floating point underflow" }, 93e5c89e4eSSatish Balay { FPE_FLTDIV_TRAP ,"floating pointing divide" }, 94e5c89e4eSSatish Balay { FPE_FLTINEX_TRAP ,"inexact floating point result" }, 95e5c89e4eSSatish Balay { 0 ,"unknown error" } 96e5c89e4eSSatish Balay }; 97e5c89e4eSSatish Balay #define SIGPC(scp) (scp->sc_pc) 98e5c89e4eSSatish Balay 99e5c89e4eSSatish Balay #undef __FUNCT__ 100e5c89e4eSSatish Balay #define __FUNCT__ "PetscDefaultFPTrap" 101e5c89e4eSSatish Balay sigfpe_handler_type PetscDefaultFPTrap(int sig,int code,struct sigcontext *scp,char *addr) 102e5c89e4eSSatish Balay { 103e5c89e4eSSatish Balay PetscErrorCode ierr; 104e5c89e4eSSatish Balay int err_ind = -1,j; 105e5c89e4eSSatish Balay 106e5c89e4eSSatish Balay PetscFunctionBegin; 107e5c89e4eSSatish Balay for (j = 0; error_codes[j].code_no; j++) { 108e5c89e4eSSatish Balay if (error_codes[j].code_no == code) err_ind = j; 109e5c89e4eSSatish Balay } 110e5c89e4eSSatish Balay 111a297a907SKarl Rupp if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred at pc=%X ***\n",error_codes[err_ind].name,SIGPC(scp)); 112a297a907SKarl Rupp else (*PetscErrorPrintf)("*** floating point error 0x%x occurred at pc=%X ***\n",code,SIGPC(scp)); 113a297a907SKarl Rupp 1145981cbfdSJed Brown ierr = PetscError(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided function","Unknown file","Unknown directory",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 115e5c89e4eSSatish Balay MPI_Abort(PETSC_COMM_WORLD,0); 116e5c89e4eSSatish Balay PetscFunctionReturn(0); 117e5c89e4eSSatish Balay } 118e5c89e4eSSatish Balay 119e5c89e4eSSatish Balay #undef __FUNCT__ 120e5c89e4eSSatish Balay #define __FUNCT__ "PetscSetFPTrap" 121e30d2299SSatish Balay /*@ 122e5c89e4eSSatish Balay PetscSetFPTrap - Enables traps/exceptions on common floating point errors. 123e5c89e4eSSatish Balay This option may not work on certain machines. 124e5c89e4eSSatish Balay 125e5c89e4eSSatish Balay Not Collective 126e5c89e4eSSatish Balay 127e5c89e4eSSatish Balay Input Parameters: 128e5c89e4eSSatish Balay . flag - PETSC_FP_TRAP_ON, PETSC_FP_TRAP_OFF. 129e5c89e4eSSatish Balay 130e5c89e4eSSatish Balay Options Database Keys: 131e5c89e4eSSatish Balay . -fp_trap - Activates floating point trapping 132e5c89e4eSSatish Balay 133e5c89e4eSSatish Balay Level: advanced 134e5c89e4eSSatish Balay 135e5c89e4eSSatish Balay Description: 136e5c89e4eSSatish Balay On systems that support it, this routine causes floating point 137e5c89e4eSSatish Balay overflow, divide-by-zero, and invalid-operand (e.g., a NaN) to 138e5c89e4eSSatish Balay cause a message to be printed and the program to exit. 139e5c89e4eSSatish Balay 1407d125cddSJed Brown Note: 1417d125cddSJed Brown On many common systems including x86 and x86-64 Linux, the floating 1427d125cddSJed Brown point exception state is not preserved from the location where the trap 1437d125cddSJed Brown occurred through to the signal handler. In this case, the signal handler 1447d125cddSJed Brown will just say that an unknown floating point exception occurred and which 1457d125cddSJed Brown function it occurred in. If you run with -fp_trap in a debugger, it will 1467d125cddSJed Brown break on the line where the error occurred. You can check which 1477d125cddSJed Brown exception occurred using fetestexcept(FE_ALL_EXCEPT). See fenv.h 1487d125cddSJed Brown (usually at /usr/include/bits/fenv.h) for the enum values on your system. 1497d125cddSJed Brown 150e5c89e4eSSatish Balay Caution: 151e5c89e4eSSatish Balay On certain machines, in particular the IBM rs6000, floating point 152e5c89e4eSSatish Balay trapping is VERY slow! 153e5c89e4eSSatish Balay 154e5c89e4eSSatish Balay Concepts: floating point exceptions^trapping 155e5c89e4eSSatish Balay Concepts: divide by zero 156e5c89e4eSSatish Balay 157670f3ff9SJed Brown .seealso: PetscFPTrapPush(), PetscFPTrapPop() 158e5c89e4eSSatish Balay @*/ 159e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 160e5c89e4eSSatish Balay { 161e5c89e4eSSatish Balay char *out; 162e5c89e4eSSatish Balay 163e5c89e4eSSatish Balay PetscFunctionBegin; 164e5c89e4eSSatish Balay /* Clear accumulated exceptions. Used to suppress meaningless messages from f77 programs */ 165e5c89e4eSSatish Balay (void) ieee_flags("clear","exception","all",&out); 166e5c89e4eSSatish Balay if (flag == PETSC_FP_TRAP_ON) { 167e5c89e4eSSatish Balay /* 168a297a907SKarl Rupp To trap more fp exceptions, including underflow, change the line below to 169e5c89e4eSSatish Balay if (ieee_handler("set","all",PetscDefaultFPTrap)) { 170e5c89e4eSSatish Balay */ 171a297a907SKarl Rupp if (ieee_handler("set","common",PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't set floatingpoint handler\n"); 172a297a907SKarl Rupp } else if (ieee_handler("clear","common",PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 173a297a907SKarl Rupp 174670f3ff9SJed Brown _trapmode = flag; 175e5c89e4eSSatish Balay PetscFunctionReturn(0); 176e5c89e4eSSatish Balay } 177e5c89e4eSSatish Balay 178e5c89e4eSSatish Balay /* -------------------------------------------------------------------------------------------*/ 179e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_SOLARIS_STYLE_FPTRAP) 180e5c89e4eSSatish Balay #include <sunmath.h> 181e5c89e4eSSatish Balay #include <floatingpoint.h> 182e5c89e4eSSatish Balay #include <siginfo.h> 183e5c89e4eSSatish Balay #include <ucontext.h> 184e5c89e4eSSatish Balay 185db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 186e5c89e4eSSatish Balay { FPE_FLTINV,"invalid floating point operand"}, 187e5c89e4eSSatish Balay { FPE_FLTRES,"inexact floating point result"}, 188e5c89e4eSSatish Balay { FPE_FLTDIV,"division-by-zero"}, 189e5c89e4eSSatish Balay { FPE_FLTUND,"floating point underflow"}, 190e5c89e4eSSatish Balay { FPE_FLTOVF,"floating point overflow"}, 191e5c89e4eSSatish Balay { 0, "unknown error"} 192e5c89e4eSSatish Balay }; 193e5c89e4eSSatish Balay #define SIGPC(scp) (scp->si_addr) 194e5c89e4eSSatish Balay 195e5c89e4eSSatish Balay #undef __FUNCT__ 196e5c89e4eSSatish Balay #define __FUNCT__ "PetscDefaultFPTrap" 197e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig,siginfo_t *scp,ucontext_t *uap) 198e5c89e4eSSatish Balay { 199e5c89e4eSSatish Balay int err_ind,j,code = scp->si_code; 200e5c89e4eSSatish Balay PetscErrorCode ierr; 201e5c89e4eSSatish Balay 202e5c89e4eSSatish Balay PetscFunctionBegin; 203e5c89e4eSSatish Balay err_ind = -1; 204e5c89e4eSSatish Balay for (j = 0; error_codes[j].code_no; j++) { 205e5c89e4eSSatish Balay if (error_codes[j].code_no == code) err_ind = j; 206e5c89e4eSSatish Balay } 207e5c89e4eSSatish Balay 208a297a907SKarl Rupp if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred at pc=%X ***\n",error_codes[err_ind].name,SIGPC(scp)); 209a297a907SKarl Rupp else (*PetscErrorPrintf)("*** floating point error 0x%x occurred at pc=%X ***\n",code,SIGPC(scp)); 210a297a907SKarl Rupp 2115981cbfdSJed Brown ierr = PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file","Unknown directory",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 212e5c89e4eSSatish Balay MPI_Abort(PETSC_COMM_WORLD,0); 213e5c89e4eSSatish Balay } 214e5c89e4eSSatish Balay 215e5c89e4eSSatish Balay #undef __FUNCT__ 216e5c89e4eSSatish Balay #define __FUNCT__ "PetscSetFPTrap" 217e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 218e5c89e4eSSatish Balay { 219e5c89e4eSSatish Balay char *out; 220e5c89e4eSSatish Balay 221e5c89e4eSSatish Balay PetscFunctionBegin; 222e5c89e4eSSatish Balay /* Clear accumulated exceptions. Used to suppress meaningless messages from f77 programs */ 223e5c89e4eSSatish Balay (void) ieee_flags("clear","exception","all",&out); 224e5c89e4eSSatish Balay if (flag == PETSC_FP_TRAP_ON) { 225a297a907SKarl Rupp if (ieee_handler("set","common",(sigfpe_handler_type)PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't set floating point handler\n"); 226a297a907SKarl Rupp } else if (ieee_handler("clear","common",(sigfpe_handler_type)PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 227670f3ff9SJed Brown _trapmode = flag; 228e5c89e4eSSatish Balay PetscFunctionReturn(0); 229e5c89e4eSSatish Balay } 230e5c89e4eSSatish Balay 231e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------------------*/ 232e5c89e4eSSatish Balay 233e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_IRIX_STYLE_FPTRAP) 234e5c89e4eSSatish Balay #include <sigfpe.h> 235db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 236e5c89e4eSSatish Balay { _INVALID ,"IEEE operand error" }, 237e5c89e4eSSatish Balay { _OVERFL ,"floating point overflow" }, 238e5c89e4eSSatish Balay { _UNDERFL ,"floating point underflow" }, 239e5c89e4eSSatish Balay { _DIVZERO ,"floating point divide" }, 240e5c89e4eSSatish Balay { 0 ,"unknown error" } 241e5c89e4eSSatish Balay } ; 242e5c89e4eSSatish Balay #undef __FUNCT__ 243e5c89e4eSSatish Balay #define __FUNCT__ "PetscDefaultFPTrap" 244e5c89e4eSSatish Balay void PetscDefaultFPTrap(unsigned exception[],int val[]) 245e5c89e4eSSatish Balay { 246e5c89e4eSSatish Balay int err_ind,j,code; 247e5c89e4eSSatish Balay 248e5c89e4eSSatish Balay PetscFunctionBegin; 249e5c89e4eSSatish Balay code = exception[0]; 250e5c89e4eSSatish Balay err_ind = -1; 251e5c89e4eSSatish Balay for (j = 0; error_codes[j].code_no; j++) { 252e5c89e4eSSatish Balay if (error_codes[j].code_no == code) err_ind = j; 253e5c89e4eSSatish Balay } 254a297a907SKarl Rupp if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred ***\n",error_codes[err_ind].name); 255a297a907SKarl Rupp else (*PetscErrorPrintf)("*** floating point error 0x%x occurred ***\n",code); 256a297a907SKarl Rupp 2575981cbfdSJed Brown PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file","Unknown directory",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 258e5c89e4eSSatish Balay MPI_Abort(PETSC_COMM_WORLD,0); 259e5c89e4eSSatish Balay } 260e5c89e4eSSatish Balay 261e5c89e4eSSatish Balay #undef __FUNCT__ 262e5c89e4eSSatish Balay #define __FUNCT__ "PetscSetFPTrap" 263e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 264e5c89e4eSSatish Balay { 265e5c89e4eSSatish Balay PetscFunctionBegin; 266a297a907SKarl Rupp if (flag == PETSC_FP_TRAP_ON) handle_sigfpes(_ON,_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,PetscDefaultFPTrap,_ABORT_ON_ERROR,0); 267a297a907SKarl Rupp else handle_sigfpes(_OFF,_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,0,_ABORT_ON_ERROR,0); 268a297a907SKarl Rupp 269670f3ff9SJed Brown _trapmode = flag; 270e5c89e4eSSatish Balay PetscFunctionReturn(0); 271e5c89e4eSSatish Balay } 272e5c89e4eSSatish Balay /*----------------------------------------------- --------------------------------------------*/ 273e5c89e4eSSatish Balay /* In "fast" mode, floating point traps are imprecise and ignored. 274e5c89e4eSSatish Balay This is the reason for the fptrap(FP_TRAP_SYNC) call */ 275e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_RS6000_STYLE_FPTRAP) 276e5c89e4eSSatish Balay struct sigcontext; 277e5c89e4eSSatish Balay #include <fpxcp.h> 278e5c89e4eSSatish Balay #include <fptrap.h> 279e5c89e4eSSatish Balay #define FPE_FLTOPERR_TRAP (fptrap_t)(0x20000000) 280e5c89e4eSSatish Balay #define FPE_FLTOVF_TRAP (fptrap_t)(0x10000000) 281e5c89e4eSSatish Balay #define FPE_FLTUND_TRAP (fptrap_t)(0x08000000) 282e5c89e4eSSatish Balay #define FPE_FLTDIV_TRAP (fptrap_t)(0x04000000) 283e5c89e4eSSatish Balay #define FPE_FLTINEX_TRAP (fptrap_t)(0x02000000) 284e5c89e4eSSatish Balay 285db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 286e5c89e4eSSatish Balay {FPE_FLTOPERR_TRAP ,"IEEE operand error" }, 287e5c89e4eSSatish Balay { FPE_FLTOVF_TRAP ,"floating point overflow" }, 288e5c89e4eSSatish Balay { FPE_FLTUND_TRAP ,"floating point underflow" }, 289e5c89e4eSSatish Balay { FPE_FLTDIV_TRAP ,"floating point divide" }, 290e5c89e4eSSatish Balay { FPE_FLTINEX_TRAP ,"inexact floating point result" }, 291e5c89e4eSSatish Balay { 0 ,"unknown error" } 292e5c89e4eSSatish Balay } ; 293e5c89e4eSSatish Balay #define SIGPC(scp) (0) /* Info MIGHT be in scp->sc_jmpbuf.jmp_context.iar */ 294e5c89e4eSSatish Balay /* 295e5c89e4eSSatish Balay For some reason, scp->sc_jmpbuf does not work on the RS6000, even though 296e5c89e4eSSatish Balay it looks like it should from the include definitions. It is probably 297e5c89e4eSSatish Balay some strange interaction with the "POSIX_SOURCE" that we require. 298e5c89e4eSSatish Balay */ 299e5c89e4eSSatish Balay 300e5c89e4eSSatish Balay #undef __FUNCT__ 301e5c89e4eSSatish Balay #define __FUNCT__ "PetscDefaultFPTrap" 302e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig,int code,struct sigcontext *scp) 303e5c89e4eSSatish Balay { 304e5c89e4eSSatish Balay PetscErrorCode ierr; 305e5c89e4eSSatish Balay int err_ind,j; 306e5c89e4eSSatish Balay fp_ctx_t flt_context; 307e5c89e4eSSatish Balay 308e5c89e4eSSatish Balay PetscFunctionBegin; 309e5c89e4eSSatish Balay fp_sh_trap_info(scp,&flt_context); 310e5c89e4eSSatish Balay 311e5c89e4eSSatish Balay err_ind = -1; 312e5c89e4eSSatish Balay for (j = 0; error_codes[j].code_no; j++) { 313e5c89e4eSSatish Balay if (error_codes[j].code_no == flt_context.trap) err_ind = j; 314e5c89e4eSSatish Balay } 315e5c89e4eSSatish Balay 316a297a907SKarl Rupp if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred ***\n",error_codes[err_ind].name); 317a297a907SKarl Rupp else (*PetscErrorPrintf)("*** floating point error 0x%x occurred ***\n",flt_context.trap); 318a297a907SKarl Rupp 3195981cbfdSJed Brown ierr = PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file","Unknown directory",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 320e5c89e4eSSatish Balay MPI_Abort(PETSC_COMM_WORLD,0); 321e5c89e4eSSatish Balay } 322e5c89e4eSSatish Balay 323e5c89e4eSSatish Balay #undef __FUNCT__ 324e5c89e4eSSatish Balay #define __FUNCT__ "PetscSetFPTrap" 325e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap on) 326e5c89e4eSSatish Balay { 327e5c89e4eSSatish Balay PetscFunctionBegin; 328e5c89e4eSSatish Balay if (on == PETSC_FP_TRAP_ON) { 329e5c89e4eSSatish Balay signal(SIGFPE,(void (*)(int))PetscDefaultFPTrap); 330e5c89e4eSSatish Balay fp_trap(FP_TRAP_SYNC); 331e5c89e4eSSatish Balay fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW); 332e5c89e4eSSatish Balay /* fp_enable(mask) for individual traps. Values are: 333e5c89e4eSSatish Balay TRP_INVALID 334e5c89e4eSSatish Balay TRP_DIV_BY_ZERO 335e5c89e4eSSatish Balay TRP_OVERFLOW 336e5c89e4eSSatish Balay TRP_UNDERFLOW 337e5c89e4eSSatish Balay TRP_INEXACT 338e5c89e4eSSatish Balay Can OR then together. 339e5c89e4eSSatish Balay fp_enable_all(); for all traps. 340e5c89e4eSSatish Balay */ 341e5c89e4eSSatish Balay } else { 342e5c89e4eSSatish Balay signal(SIGFPE,SIG_DFL); 343e5c89e4eSSatish Balay fp_disable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW); 344e5c89e4eSSatish Balay fp_trap(FP_TRAP_OFF); 345e5c89e4eSSatish Balay } 346670f3ff9SJed Brown _trapmode = on; 347e5c89e4eSSatish Balay PetscFunctionReturn(0); 348e5c89e4eSSatish Balay } 349e5c89e4eSSatish Balay 3509a2402e9SBarry Smith #elif defined(PETSC_HAVE_FENV_H) && !defined(__cplusplus) 351b014e56cSJed Brown /* 352b014e56cSJed Brown C99 style floating point environment. 353b014e56cSJed Brown 354b014e56cSJed Brown Note that C99 merely specifies how to save, restore, and clear the floating 355b014e56cSJed Brown point environment as well as defining an enumeration of exception codes. In 356b014e56cSJed Brown particular, C99 does not specify how to make floating point exceptions raise 357b014e56cSJed Brown a signal. Glibc offers this capability through FE_NOMASK_ENV (or with finer 358b014e56cSJed Brown granularity, feenableexcept()), xmmintrin.h offers _MM_SET_EXCEPTION_MASK(). 359b014e56cSJed Brown */ 360b014e56cSJed Brown #include <fenv.h> 361b014e56cSJed Brown typedef struct {int code; const char *name;} FPNode; 362b014e56cSJed Brown static const FPNode error_codes[] = { 363b014e56cSJed Brown {FE_DIVBYZERO,"divide by zero"}, 364b014e56cSJed Brown {FE_INEXACT, "inexact floating point result"}, 365b014e56cSJed Brown {FE_INVALID, "invalid floating point arguments (domain error)"}, 366b014e56cSJed Brown {FE_OVERFLOW, "floating point overflow"}, 367b014e56cSJed Brown {FE_UNDERFLOW,"floating point underflow"}, 368b014e56cSJed Brown {0 ,"unknown error"} 369b014e56cSJed Brown }; 37099e0435eSBarry Smith 371b014e56cSJed Brown #undef __FUNCT__ 372b014e56cSJed Brown #define __FUNCT__ "PetscDefaultFPTrap" 373b014e56cSJed Brown void PetscDefaultFPTrap(int sig) 374b014e56cSJed Brown { 375b014e56cSJed Brown const FPNode *node; 376b014e56cSJed Brown int code; 377ace3abfcSBarry Smith PetscBool matched = PETSC_FALSE; 378b014e56cSJed Brown 379b014e56cSJed Brown PetscFunctionBegin; 380b014e56cSJed Brown /* Note: While it is possible for the exception state to be preserved by the 381b014e56cSJed Brown * kernel, this seems to be rare which makes the following flag testing almost 382b014e56cSJed Brown * useless. But on a system where the flags can be preserved, it would provide 3837d125cddSJed Brown * more detail. 384b014e56cSJed Brown */ 385b014e56cSJed Brown code = fetestexcept(FE_ALL_EXCEPT); 386b014e56cSJed Brown for (node=&error_codes[0]; node->code; node++) { 387b014e56cSJed Brown if (code & node->code) { 388b014e56cSJed Brown matched = PETSC_TRUE; 389b014e56cSJed Brown (*PetscErrorPrintf)("*** floating point error \"%s\" occurred ***\n",node->name); 390b014e56cSJed Brown code &= ~node->code; /* Unset this flag since it has been processed */ 391b014e56cSJed Brown } 392b014e56cSJed Brown } 393b014e56cSJed Brown if (!matched || code) { /* If any remaining flags are set, or we didn't process any flags */ 394b014e56cSJed Brown (*PetscErrorPrintf)("*** unknown floating point error occurred ***\n"); 3957d125cddSJed Brown (*PetscErrorPrintf)("The specific exception can be determined by running in a debugger. When the\n"); 3967d125cddSJed Brown (*PetscErrorPrintf)("debugger traps the signal, the exception can be found with fetestexcept(0x%x)\n",FE_ALL_EXCEPT); 3977d125cddSJed Brown (*PetscErrorPrintf)("where the result is a bitwise OR of the following flags:\n"); 3987d125cddSJed Brown (*PetscErrorPrintf)("FE_INVALID=0x%x FE_DIVBYZERO=0x%x FE_OVERFLOW=0x%x FE_UNDERFLOW=0x%x FE_INEXACT=0x%x\n",FE_INVALID,FE_DIVBYZERO,FE_OVERFLOW,FE_UNDERFLOW,FE_INEXACT); 399b014e56cSJed Brown } 4007d125cddSJed Brown 4017d125cddSJed Brown (*PetscErrorPrintf)("Try option -start_in_debugger\n"); 4028bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG) 403dbf62e16SBarry Smith if (!PetscStackActive()) (*PetscErrorPrintf)(" or try option -log_stack\n"); 404a297a907SKarl Rupp else { 4057d125cddSJed Brown (*PetscErrorPrintf)("likely location of problem given in stack below\n"); 4067d125cddSJed Brown (*PetscErrorPrintf)("--------------------- Stack Frames ------------------------------------\n"); 407639ff905SBarry Smith PetscStackView(PETSC_STDOUT); 4087d125cddSJed Brown } 4097d125cddSJed Brown #endif 4107d125cddSJed Brown #if !defined(PETSC_USE_DEBUG) 4117d125cddSJed Brown (*PetscErrorPrintf)("configure using --with-debugging=yes, recompile, link, and run \n"); 4127d125cddSJed Brown (*PetscErrorPrintf)("with -start_in_debugger to get more information on the crash.\n"); 4137d125cddSJed Brown #endif 414668f157eSBarry Smith PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file","Unknown directory",PETSC_ERR_FP,PETSC_ERROR_INITIAL,"trapped floating point error"); 415b014e56cSJed Brown MPI_Abort(PETSC_COMM_WORLD,0); 416b014e56cSJed Brown } 417b014e56cSJed Brown 418b014e56cSJed Brown #undef __FUNCT__ 419b014e56cSJed Brown #define __FUNCT__ "PetscSetFPTrap" 4207087cfbeSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap on) 421b014e56cSJed Brown { 422b014e56cSJed Brown PetscFunctionBegin; 423b014e56cSJed Brown if (on == PETSC_FP_TRAP_ON) { 424b014e56cSJed Brown /* Clear any flags that are currently set so that activating trapping will not immediately call the signal handler. */ 425e32f2f54SBarry Smith if (feclearexcept(FE_ALL_EXCEPT)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot clear floating point exception flags\n"); 426b014e56cSJed Brown #if defined FE_NOMASK_ENV 427b014e56cSJed Brown /* We could use fesetenv(FE_NOMASK_ENV), but that causes spurious exceptions (like gettimeofday() -> PetscLogDouble). */ 428e32f2f54SBarry Smith if (feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW) == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot activate floating point exceptions\n"); 429b014e56cSJed Brown #elif defined PETSC_HAVE_XMMINTRIN_H 430b014e56cSJed Brown _MM_SET_EXCEPTION_MASK(_MM_MASK_INEXACT); 431b014e56cSJed Brown #else 432b014e56cSJed Brown /* C99 does not provide a way to modify the environment so there is no portable way to activate trapping. */ 433b014e56cSJed Brown #endif 434e32f2f54SBarry Smith if (SIG_ERR == signal(SIGFPE,PetscDefaultFPTrap)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't set floating point handler\n"); 435b014e56cSJed Brown } else { 436e32f2f54SBarry Smith if (fesetenv(FE_DFL_ENV)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot disable floating point exceptions"); 437e32f2f54SBarry Smith if (SIG_ERR == signal(SIGFPE,SIG_DFL)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't clear floating point handler\n"); 438b014e56cSJed Brown } 439670f3ff9SJed Brown _trapmode = on; 440b014e56cSJed Brown PetscFunctionReturn(0); 441b014e56cSJed Brown } 442b014e56cSJed Brown 443e5c89e4eSSatish Balay /* -------------------------Default -----------------------------------*/ 444e5c89e4eSSatish Balay #else 44599e0435eSBarry Smith 446e5c89e4eSSatish Balay #undef __FUNCT__ 447e5c89e4eSSatish Balay #define __FUNCT__ "PetscDefaultFPTrap" 448e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig) 449e5c89e4eSSatish Balay { 450e5c89e4eSSatish Balay PetscFunctionBegin; 451e5c89e4eSSatish Balay (*PetscErrorPrintf)("*** floating point error occurred ***\n"); 4520a78d522SHong Zhang PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file","Unknown directory",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 453e5c89e4eSSatish Balay MPI_Abort(PETSC_COMM_WORLD,0); 454e5c89e4eSSatish Balay } 45599e0435eSBarry Smith 456e5c89e4eSSatish Balay #undef __FUNCT__ 457e5c89e4eSSatish Balay #define __FUNCT__ "PetscSetFPTrap" 4587087cfbeSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap on) 459e5c89e4eSSatish Balay { 460e5c89e4eSSatish Balay PetscFunctionBegin; 461e5c89e4eSSatish Balay if (on == PETSC_FP_TRAP_ON) { 462a297a907SKarl Rupp if (SIG_ERR == signal(SIGFPE,PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't set floatingpoint handler\n"); 463a297a907SKarl Rupp } else if (SIG_ERR == signal(SIGFPE,SIG_DFL)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 464a297a907SKarl Rupp 465670f3ff9SJed Brown _trapmode = on; 466e5c89e4eSSatish Balay PetscFunctionReturn(0); 467e5c89e4eSSatish Balay } 468e5c89e4eSSatish Balay #endif 469e5c89e4eSSatish Balay 470e5c89e4eSSatish Balay 471e5c89e4eSSatish Balay 472