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 */ 15e547316fSJed Brown #ifndef _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 #if defined(PETSC_HAVE_STDLIB_H) 22e5c89e4eSSatish Balay #include <stdlib.h> 23e5c89e4eSSatish Balay #endif 24e5c89e4eSSatish Balay 25*670f3ff9SJed Brown struct PetscFPTrapLink { 26*670f3ff9SJed Brown PetscFPTrap trapmode; 27*670f3ff9SJed Brown struct PetscFPTrapLink *next; 28*670f3ff9SJed Brown }; 29*670f3ff9SJed Brown static PetscFPTrap _trapmode = PETSC_FP_TRAP_OFF; /* Current trapping mode */ 30*670f3ff9SJed Brown static struct PetscFPTrapLink *_trapstack; /* Any pushed states of _trapmode */ 31*670f3ff9SJed Brown 32*670f3ff9SJed Brown #undef __FUNCT__ 33*670f3ff9SJed Brown #define __FUNCT__ "PetscFPTrapPush" 34*670f3ff9SJed Brown /*@ 35*670f3ff9SJed Brown PetscFPTrapPush - push a floating point trapping mode, to be restored using PetscFPTrapPop() 36*670f3ff9SJed Brown 37*670f3ff9SJed Brown Not Collective 38*670f3ff9SJed Brown 39*670f3ff9SJed Brown Input Arguments: 40*670f3ff9SJed Brown . trap - PETSC_FP_TRAP_ON or PETSC_FP_TRAP_OFF 41*670f3ff9SJed Brown 42*670f3ff9SJed Brown Level: advanced 43*670f3ff9SJed Brown 44*670f3ff9SJed Brown .seealso: PetscFPTrapPop(), PetscSetFPTrap() 45*670f3ff9SJed Brown @*/ 46*670f3ff9SJed Brown PetscErrorCode PetscFPTrapPush(PetscFPTrap trap) 47*670f3ff9SJed Brown { 48*670f3ff9SJed Brown PetscErrorCode ierr; 49*670f3ff9SJed Brown struct PetscFPTrapLink *link; 50*670f3ff9SJed Brown 51*670f3ff9SJed Brown PetscFunctionBegin; 52*670f3ff9SJed Brown ierr = PetscMalloc(sizeof *link,&link);CHKERRQ(ierr); 53*670f3ff9SJed Brown link->trapmode = _trapmode; 54*670f3ff9SJed Brown link->next = _trapstack; 55*670f3ff9SJed Brown _trapstack = link; 56*670f3ff9SJed Brown ierr = PetscSetFPTrap(trap);CHKERRQ(ierr); 57*670f3ff9SJed Brown PetscFunctionReturn(0); 58*670f3ff9SJed Brown } 59*670f3ff9SJed Brown 60*670f3ff9SJed Brown #undef __FUNCT__ 61*670f3ff9SJed Brown #define __FUNCT__ "PetscFPTrapPop" 62*670f3ff9SJed Brown /*@ 63*670f3ff9SJed Brown PetscFPTrapPop - push a floating point trapping mode, to be restored using PetscFPTrapPop() 64*670f3ff9SJed Brown 65*670f3ff9SJed Brown Not Collective 66*670f3ff9SJed Brown 67*670f3ff9SJed Brown Level: advanced 68*670f3ff9SJed Brown 69*670f3ff9SJed Brown .seealso: PetscFPTrapPush(), PetscSetFPTrap() 70*670f3ff9SJed Brown @*/ 71*670f3ff9SJed Brown PetscErrorCode PetscFPTrapPop(void) 72*670f3ff9SJed Brown { 73*670f3ff9SJed Brown PetscErrorCode ierr; 74*670f3ff9SJed Brown struct PetscFPTrapLink *link; 75*670f3ff9SJed Brown 76*670f3ff9SJed Brown PetscFunctionBegin; 77*670f3ff9SJed Brown ierr = PetscSetFPTrap(_trapstack->trapmode);CHKERRQ(ierr); 78*670f3ff9SJed Brown link = _trapstack; 79*670f3ff9SJed Brown _trapstack = _trapstack->next; 80*670f3ff9SJed Brown ierr = PetscFree(link);CHKERRQ(ierr); 81*670f3ff9SJed Brown PetscFunctionReturn(0); 82*670f3ff9SJed Brown } 83*670f3ff9SJed Brown 84e5c89e4eSSatish Balay /*--------------------------------------- ---------------------------------------------------*/ 85e5c89e4eSSatish Balay #if defined(PETSC_HAVE_SUN4_STYLE_FPTRAP) 86e5c89e4eSSatish Balay #include <floatingpoint.h> 87e5c89e4eSSatish Balay 88e5c89e4eSSatish Balay EXTERN_C_BEGIN 89e5c89e4eSSatish Balay PetscErrorCode ieee_flags(char*,char*,char*,char**); 90e5c89e4eSSatish Balay PetscErrorCode ieee_handler(char *,char *,sigfpe_handler_type(int,int,struct sigcontext*,char *)); 91e5c89e4eSSatish Balay EXTERN_C_END 92e5c89e4eSSatish Balay 93db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 94e5c89e4eSSatish Balay { FPE_INTDIV_TRAP ,"integer divide" }, 95e5c89e4eSSatish Balay { FPE_FLTOPERR_TRAP ,"IEEE operand error" }, 96e5c89e4eSSatish Balay { FPE_FLTOVF_TRAP ,"floating point overflow" }, 97e5c89e4eSSatish Balay { FPE_FLTUND_TRAP ,"floating point underflow" }, 98e5c89e4eSSatish Balay { FPE_FLTDIV_TRAP ,"floating pointing divide" }, 99e5c89e4eSSatish Balay { FPE_FLTINEX_TRAP ,"inexact floating point result" }, 100e5c89e4eSSatish Balay { 0 ,"unknown error" } 101e5c89e4eSSatish Balay } ; 102e5c89e4eSSatish Balay #define SIGPC(scp) (scp->sc_pc) 103e5c89e4eSSatish Balay 104e5c89e4eSSatish Balay #undef __FUNCT__ 105e5c89e4eSSatish Balay #define __FUNCT__ "PetscDefaultFPTrap" 106e5c89e4eSSatish Balay sigfpe_handler_type PetscDefaultFPTrap(int sig,int code,struct sigcontext *scp,char *addr) 107e5c89e4eSSatish Balay { 108e5c89e4eSSatish Balay PetscErrorCode ierr; 109e5c89e4eSSatish Balay int err_ind = -1,j; 110e5c89e4eSSatish Balay 111e5c89e4eSSatish Balay PetscFunctionBegin; 112e5c89e4eSSatish Balay for (j = 0 ; error_codes[j].code_no ; j++) { 113e5c89e4eSSatish Balay if (error_codes[j].code_no == code) err_ind = j; 114e5c89e4eSSatish Balay } 115e5c89e4eSSatish Balay 116e5c89e4eSSatish Balay if (err_ind >= 0) { 117e5c89e4eSSatish Balay (*PetscErrorPrintf)("*** %s occurred at pc=%X ***\n",error_codes[err_ind].name,SIGPC(scp)); 118e5c89e4eSSatish Balay } else { 119e5c89e4eSSatish Balay (*PetscErrorPrintf)("*** floating point error 0x%x occurred at pc=%X ***\n",code,SIGPC(scp)); 120e5c89e4eSSatish Balay } 1215981cbfdSJed 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"); 122e5c89e4eSSatish Balay MPI_Abort(PETSC_COMM_WORLD,0); 123e5c89e4eSSatish Balay PetscFunctionReturn(0); 124e5c89e4eSSatish Balay } 125e5c89e4eSSatish Balay 126e5c89e4eSSatish Balay #undef __FUNCT__ 127e5c89e4eSSatish Balay #define __FUNCT__ "PetscSetFPTrap" 128e30d2299SSatish Balay /*@ 129e5c89e4eSSatish Balay PetscSetFPTrap - Enables traps/exceptions on common floating point errors. 130e5c89e4eSSatish Balay This option may not work on certain machines. 131e5c89e4eSSatish Balay 132e5c89e4eSSatish Balay Not Collective 133e5c89e4eSSatish Balay 134e5c89e4eSSatish Balay Input Parameters: 135e5c89e4eSSatish Balay . flag - PETSC_FP_TRAP_ON, PETSC_FP_TRAP_OFF. 136e5c89e4eSSatish Balay 137e5c89e4eSSatish Balay Options Database Keys: 138e5c89e4eSSatish Balay . -fp_trap - Activates floating point trapping 139e5c89e4eSSatish Balay 140e5c89e4eSSatish Balay Level: advanced 141e5c89e4eSSatish Balay 142e5c89e4eSSatish Balay Description: 143e5c89e4eSSatish Balay On systems that support it, this routine causes floating point 144e5c89e4eSSatish Balay overflow, divide-by-zero, and invalid-operand (e.g., a NaN) to 145e5c89e4eSSatish Balay cause a message to be printed and the program to exit. 146e5c89e4eSSatish Balay 1477d125cddSJed Brown Note: 1487d125cddSJed Brown On many common systems including x86 and x86-64 Linux, the floating 1497d125cddSJed Brown point exception state is not preserved from the location where the trap 1507d125cddSJed Brown occurred through to the signal handler. In this case, the signal handler 1517d125cddSJed Brown will just say that an unknown floating point exception occurred and which 1527d125cddSJed Brown function it occurred in. If you run with -fp_trap in a debugger, it will 1537d125cddSJed Brown break on the line where the error occurred. You can check which 1547d125cddSJed Brown exception occurred using fetestexcept(FE_ALL_EXCEPT). See fenv.h 1557d125cddSJed Brown (usually at /usr/include/bits/fenv.h) for the enum values on your system. 1567d125cddSJed Brown 157e5c89e4eSSatish Balay Caution: 158e5c89e4eSSatish Balay On certain machines, in particular the IBM rs6000, floating point 159e5c89e4eSSatish Balay trapping is VERY slow! 160e5c89e4eSSatish Balay 161e5c89e4eSSatish Balay Concepts: floating point exceptions^trapping 162e5c89e4eSSatish Balay Concepts: divide by zero 163e5c89e4eSSatish Balay 164*670f3ff9SJed Brown .seealso: PetscFPTrapPush(), PetscFPTrapPop() 165e5c89e4eSSatish Balay @*/ 166e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 167e5c89e4eSSatish Balay { 168e5c89e4eSSatish Balay char *out; 169e5c89e4eSSatish Balay 170e5c89e4eSSatish Balay PetscFunctionBegin; 171e5c89e4eSSatish Balay /* Clear accumulated exceptions. Used to suppress meaningless messages from f77 programs */ 172e5c89e4eSSatish Balay (void) ieee_flags("clear","exception","all",&out); 173e5c89e4eSSatish Balay if (flag == PETSC_FP_TRAP_ON) { 174e5c89e4eSSatish Balay if (ieee_handler("set","common",PetscDefaultFPTrap)) { 175e5c89e4eSSatish Balay /* 176e5c89e4eSSatish Balay To trap more fp exceptions, including undrflow, change the above line to 177e5c89e4eSSatish Balay if (ieee_handler("set","all",PetscDefaultFPTrap)) { 178e5c89e4eSSatish Balay */ 179e5c89e4eSSatish Balay (*PetscErrorPrintf)("Can't set floatingpoint handler\n"); 180e5c89e4eSSatish Balay } 181e5c89e4eSSatish Balay } else { 182e5c89e4eSSatish Balay if (ieee_handler("clear","common",PetscDefaultFPTrap)) { 183e5c89e4eSSatish Balay (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 184e5c89e4eSSatish Balay } 185e5c89e4eSSatish Balay } 186*670f3ff9SJed Brown _trapmode = flag; 187e5c89e4eSSatish Balay PetscFunctionReturn(0); 188e5c89e4eSSatish Balay } 189e5c89e4eSSatish Balay 190e5c89e4eSSatish Balay /* -------------------------------------------------------------------------------------------*/ 191e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_SOLARIS_STYLE_FPTRAP) 192e5c89e4eSSatish Balay #include <sunmath.h> 193e5c89e4eSSatish Balay #include <floatingpoint.h> 194e5c89e4eSSatish Balay #include <siginfo.h> 195e5c89e4eSSatish Balay #include <ucontext.h> 196e5c89e4eSSatish Balay 197db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 198e5c89e4eSSatish Balay { FPE_FLTINV,"invalid floating point operand"}, 199e5c89e4eSSatish Balay { FPE_FLTRES,"inexact floating point result"}, 200e5c89e4eSSatish Balay { FPE_FLTDIV,"division-by-zero"}, 201e5c89e4eSSatish Balay { FPE_FLTUND,"floating point underflow"}, 202e5c89e4eSSatish Balay { FPE_FLTOVF,"floating point overflow"}, 203e5c89e4eSSatish Balay { 0, "unknown error"} 204e5c89e4eSSatish Balay }; 205e5c89e4eSSatish Balay #define SIGPC(scp) (scp->si_addr) 206e5c89e4eSSatish Balay 207e5c89e4eSSatish Balay #undef __FUNCT__ 208e5c89e4eSSatish Balay #define __FUNCT__ "PetscDefaultFPTrap" 209e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig,siginfo_t *scp,ucontext_t *uap) 210e5c89e4eSSatish Balay { 211e5c89e4eSSatish Balay int err_ind,j,code = scp->si_code; 212e5c89e4eSSatish Balay PetscErrorCode ierr; 213e5c89e4eSSatish Balay 214e5c89e4eSSatish Balay PetscFunctionBegin; 215e5c89e4eSSatish Balay err_ind = -1 ; 216e5c89e4eSSatish Balay for (j = 0 ; error_codes[j].code_no ; j++) { 217e5c89e4eSSatish Balay if (error_codes[j].code_no == code) err_ind = j; 218e5c89e4eSSatish Balay } 219e5c89e4eSSatish Balay 220e5c89e4eSSatish Balay if (err_ind >= 0) { 221e5c89e4eSSatish Balay (*PetscErrorPrintf)("*** %s occurred at pc=%X ***\n",error_codes[err_ind].name,SIGPC(scp)); 222e5c89e4eSSatish Balay } else { 223e5c89e4eSSatish Balay (*PetscErrorPrintf)("*** floating point error 0x%x occurred at pc=%X ***\n",code,SIGPC(scp)); 224e5c89e4eSSatish Balay } 2255981cbfdSJed Brown ierr = PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file","Unknown directory",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 226e5c89e4eSSatish Balay MPI_Abort(PETSC_COMM_WORLD,0); 227e5c89e4eSSatish Balay } 228e5c89e4eSSatish Balay 229e5c89e4eSSatish Balay #undef __FUNCT__ 230e5c89e4eSSatish Balay #define __FUNCT__ "PetscSetFPTrap" 231e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 232e5c89e4eSSatish Balay { 233e5c89e4eSSatish Balay char *out; 234e5c89e4eSSatish Balay 235e5c89e4eSSatish Balay PetscFunctionBegin; 236e5c89e4eSSatish Balay /* Clear accumulated exceptions. Used to suppress meaningless messages from f77 programs */ 237e5c89e4eSSatish Balay (void) ieee_flags("clear","exception","all",&out); 238e5c89e4eSSatish Balay if (flag == PETSC_FP_TRAP_ON) { 239e5c89e4eSSatish Balay if (ieee_handler("set","common",(sigfpe_handler_type)PetscDefaultFPTrap)) { 240e5c89e4eSSatish Balay (*PetscErrorPrintf)("Can't set floating point handler\n"); 241e5c89e4eSSatish Balay } 242e5c89e4eSSatish Balay } else { 243e5c89e4eSSatish Balay if (ieee_handler("clear","common",(sigfpe_handler_type)PetscDefaultFPTrap)) { 244e5c89e4eSSatish Balay (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 245e5c89e4eSSatish Balay } 246e5c89e4eSSatish Balay } 247*670f3ff9SJed Brown _trapmode = flag; 248e5c89e4eSSatish Balay PetscFunctionReturn(0); 249e5c89e4eSSatish Balay } 250e5c89e4eSSatish Balay 251e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------------------*/ 252e5c89e4eSSatish Balay 253e5c89e4eSSatish Balay #elif defined (PETSC_HAVE_IRIX_STYLE_FPTRAP) 254e5c89e4eSSatish Balay #include <sigfpe.h> 255db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 256e5c89e4eSSatish Balay { _INVALID ,"IEEE operand error" }, 257e5c89e4eSSatish Balay { _OVERFL ,"floating point overflow" }, 258e5c89e4eSSatish Balay { _UNDERFL ,"floating point underflow" }, 259e5c89e4eSSatish Balay { _DIVZERO ,"floating point divide" }, 260e5c89e4eSSatish Balay { 0 ,"unknown error" } 261e5c89e4eSSatish Balay } ; 262e5c89e4eSSatish Balay #undef __FUNCT__ 263e5c89e4eSSatish Balay #define __FUNCT__ "PetscDefaultFPTrap" 264e5c89e4eSSatish Balay void PetscDefaultFPTrap(unsigned exception[],int val[]) 265e5c89e4eSSatish Balay { 266e5c89e4eSSatish Balay int err_ind,j,code; 267e5c89e4eSSatish Balay 268e5c89e4eSSatish Balay PetscFunctionBegin; 269e5c89e4eSSatish Balay code = exception[0]; 270e5c89e4eSSatish Balay err_ind = -1 ; 271e5c89e4eSSatish Balay for (j = 0 ; error_codes[j].code_no ; j++){ 272e5c89e4eSSatish Balay if (error_codes[j].code_no == code) err_ind = j; 273e5c89e4eSSatish Balay } 274e5c89e4eSSatish Balay if (err_ind >= 0){ 275e5c89e4eSSatish Balay (*PetscErrorPrintf)("*** %s occurred ***\n",error_codes[err_ind].name); 276e5c89e4eSSatish Balay } else{ 277e5c89e4eSSatish Balay (*PetscErrorPrintf)("*** floating point error 0x%x occurred ***\n",code); 278e5c89e4eSSatish Balay } 2795981cbfdSJed Brown PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file","Unknown directory",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 280e5c89e4eSSatish Balay MPI_Abort(PETSC_COMM_WORLD,0); 281e5c89e4eSSatish Balay } 282e5c89e4eSSatish Balay 283e5c89e4eSSatish Balay #undef __FUNCT__ 284e5c89e4eSSatish Balay #define __FUNCT__ "PetscSetFPTrap" 285e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 286e5c89e4eSSatish Balay { 287e5c89e4eSSatish Balay PetscFunctionBegin; 288e5c89e4eSSatish Balay if (flag == PETSC_FP_TRAP_ON) { 289e5c89e4eSSatish Balay handle_sigfpes(_ON,_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,PetscDefaultFPTrap,_ABORT_ON_ERROR,0); 290e5c89e4eSSatish Balay } else { 291e5c89e4eSSatish Balay handle_sigfpes(_OFF,_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,0,_ABORT_ON_ERROR,0); 292e5c89e4eSSatish Balay } 293*670f3ff9SJed Brown _trapmode = flag; 294e5c89e4eSSatish Balay PetscFunctionReturn(0); 295e5c89e4eSSatish Balay } 296e5c89e4eSSatish Balay /*----------------------------------------------- --------------------------------------------*/ 297e5c89e4eSSatish Balay /* In "fast" mode, floating point traps are imprecise and ignored. 298e5c89e4eSSatish Balay This is the reason for the fptrap(FP_TRAP_SYNC) call */ 299e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_RS6000_STYLE_FPTRAP) 300e5c89e4eSSatish Balay struct sigcontext; 301e5c89e4eSSatish Balay #include <fpxcp.h> 302e5c89e4eSSatish Balay #include <fptrap.h> 303e5c89e4eSSatish Balay #include <stdlib.h> 304e5c89e4eSSatish Balay #define FPE_FLTOPERR_TRAP (fptrap_t)(0x20000000) 305e5c89e4eSSatish Balay #define FPE_FLTOVF_TRAP (fptrap_t)(0x10000000) 306e5c89e4eSSatish Balay #define FPE_FLTUND_TRAP (fptrap_t)(0x08000000) 307e5c89e4eSSatish Balay #define FPE_FLTDIV_TRAP (fptrap_t)(0x04000000) 308e5c89e4eSSatish Balay #define FPE_FLTINEX_TRAP (fptrap_t)(0x02000000) 309e5c89e4eSSatish Balay 310db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 311e5c89e4eSSatish Balay {FPE_FLTOPERR_TRAP ,"IEEE operand error" }, 312e5c89e4eSSatish Balay { FPE_FLTOVF_TRAP ,"floating point overflow" }, 313e5c89e4eSSatish Balay { FPE_FLTUND_TRAP ,"floating point underflow" }, 314e5c89e4eSSatish Balay { FPE_FLTDIV_TRAP ,"floating point divide" }, 315e5c89e4eSSatish Balay { FPE_FLTINEX_TRAP ,"inexact floating point result" }, 316e5c89e4eSSatish Balay { 0 ,"unknown error" } 317e5c89e4eSSatish Balay } ; 318e5c89e4eSSatish Balay #define SIGPC(scp) (0) /* Info MIGHT be in scp->sc_jmpbuf.jmp_context.iar */ 319e5c89e4eSSatish Balay /* 320e5c89e4eSSatish Balay For some reason, scp->sc_jmpbuf does not work on the RS6000, even though 321e5c89e4eSSatish Balay it looks like it should from the include definitions. It is probably 322e5c89e4eSSatish Balay some strange interaction with the "POSIX_SOURCE" that we require. 323e5c89e4eSSatish Balay */ 324e5c89e4eSSatish Balay 325e5c89e4eSSatish Balay #undef __FUNCT__ 326e5c89e4eSSatish Balay #define __FUNCT__ "PetscDefaultFPTrap" 327e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig,int code,struct sigcontext *scp) 328e5c89e4eSSatish Balay { 329e5c89e4eSSatish Balay PetscErrorCode ierr; 330e5c89e4eSSatish Balay int err_ind,j; 331e5c89e4eSSatish Balay fp_ctx_t flt_context; 332e5c89e4eSSatish Balay 333e5c89e4eSSatish Balay PetscFunctionBegin; 334e5c89e4eSSatish Balay fp_sh_trap_info(scp,&flt_context); 335e5c89e4eSSatish Balay 336e5c89e4eSSatish Balay err_ind = -1 ; 337e5c89e4eSSatish Balay for (j = 0 ; error_codes[j].code_no ; j++) { 338e5c89e4eSSatish Balay if (error_codes[j].code_no == flt_context.trap) err_ind = j; 339e5c89e4eSSatish Balay } 340e5c89e4eSSatish Balay 341e5c89e4eSSatish Balay if (err_ind >= 0){ 342e5c89e4eSSatish Balay (*PetscErrorPrintf)("*** %s occurred ***\n",error_codes[err_ind].name); 343e5c89e4eSSatish Balay } else{ 344e5c89e4eSSatish Balay (*PetscErrorPrintf)("*** floating point error 0x%x occurred ***\n",flt_context.trap); 345e5c89e4eSSatish Balay } 3465981cbfdSJed Brown ierr = PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file","Unknown directory",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 347e5c89e4eSSatish Balay MPI_Abort(PETSC_COMM_WORLD,0); 348e5c89e4eSSatish Balay } 349e5c89e4eSSatish Balay 350e5c89e4eSSatish Balay #undef __FUNCT__ 351e5c89e4eSSatish Balay #define __FUNCT__ "PetscSetFPTrap" 352e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap on) 353e5c89e4eSSatish Balay { 354e5c89e4eSSatish Balay PetscFunctionBegin; 355e5c89e4eSSatish Balay if (on == PETSC_FP_TRAP_ON) { 356e5c89e4eSSatish Balay signal(SIGFPE,(void (*)(int))PetscDefaultFPTrap); 357e5c89e4eSSatish Balay fp_trap(FP_TRAP_SYNC); 358e5c89e4eSSatish Balay fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW); 359e5c89e4eSSatish Balay /* fp_enable(mask) for individual traps. Values are: 360e5c89e4eSSatish Balay TRP_INVALID 361e5c89e4eSSatish Balay TRP_DIV_BY_ZERO 362e5c89e4eSSatish Balay TRP_OVERFLOW 363e5c89e4eSSatish Balay TRP_UNDERFLOW 364e5c89e4eSSatish Balay TRP_INEXACT 365e5c89e4eSSatish Balay Can OR then together. 366e5c89e4eSSatish Balay fp_enable_all(); for all traps. 367e5c89e4eSSatish Balay */ 368e5c89e4eSSatish Balay } else { 369e5c89e4eSSatish Balay signal(SIGFPE,SIG_DFL); 370e5c89e4eSSatish Balay fp_disable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW); 371e5c89e4eSSatish Balay fp_trap(FP_TRAP_OFF); 372e5c89e4eSSatish Balay } 373*670f3ff9SJed Brown _trapmode = on; 374e5c89e4eSSatish Balay PetscFunctionReturn(0); 375e5c89e4eSSatish Balay } 376e5c89e4eSSatish Balay 3779a2402e9SBarry Smith #elif defined(PETSC_HAVE_FENV_H) && !defined(__cplusplus) 378b014e56cSJed Brown /* 379b014e56cSJed Brown C99 style floating point environment. 380b014e56cSJed Brown 381b014e56cSJed Brown Note that C99 merely specifies how to save, restore, and clear the floating 382b014e56cSJed Brown point environment as well as defining an enumeration of exception codes. In 383b014e56cSJed Brown particular, C99 does not specify how to make floating point exceptions raise 384b014e56cSJed Brown a signal. Glibc offers this capability through FE_NOMASK_ENV (or with finer 385b014e56cSJed Brown granularity, feenableexcept()), xmmintrin.h offers _MM_SET_EXCEPTION_MASK(). 386b014e56cSJed Brown */ 387b014e56cSJed Brown #include <fenv.h> 388b014e56cSJed Brown typedef struct {int code; const char *name;} FPNode; 389b014e56cSJed Brown static const FPNode error_codes[] = { 390b014e56cSJed Brown {FE_DIVBYZERO,"divide by zero"}, 391b014e56cSJed Brown {FE_INEXACT, "inexact floating point result"}, 392b014e56cSJed Brown {FE_INVALID, "invalid floating point arguments (domain error)"}, 393b014e56cSJed Brown {FE_OVERFLOW, "floating point overflow"}, 394b014e56cSJed Brown {FE_UNDERFLOW,"floating point underflow"}, 395b014e56cSJed Brown {0 ,"unknown error"} 396b014e56cSJed Brown }; 397b014e56cSJed Brown EXTERN_C_BEGIN 398b014e56cSJed Brown #undef __FUNCT__ 399b014e56cSJed Brown #define __FUNCT__ "PetscDefaultFPTrap" 400b014e56cSJed Brown void PetscDefaultFPTrap(int sig) 401b014e56cSJed Brown { 402b014e56cSJed Brown const FPNode *node; 403b014e56cSJed Brown int code; 404ace3abfcSBarry Smith PetscBool matched = PETSC_FALSE; 405b014e56cSJed Brown 406b014e56cSJed Brown PetscFunctionBegin; 407b014e56cSJed Brown /* Note: While it is possible for the exception state to be preserved by the 408b014e56cSJed Brown * kernel, this seems to be rare which makes the following flag testing almost 409b014e56cSJed Brown * useless. But on a system where the flags can be preserved, it would provide 4107d125cddSJed Brown * more detail. 411b014e56cSJed Brown */ 412b014e56cSJed Brown code = fetestexcept(FE_ALL_EXCEPT); 413b014e56cSJed Brown for (node=&error_codes[0]; node->code; node++) { 414b014e56cSJed Brown if (code & node->code) { 415b014e56cSJed Brown matched = PETSC_TRUE; 416b014e56cSJed Brown (*PetscErrorPrintf)("*** floating point error \"%s\" occurred ***\n",node->name); 417b014e56cSJed Brown code &= ~node->code; /* Unset this flag since it has been processed */ 418b014e56cSJed Brown } 419b014e56cSJed Brown } 420b014e56cSJed Brown if (!matched || code) { /* If any remaining flags are set, or we didn't process any flags */ 421b014e56cSJed Brown (*PetscErrorPrintf)("*** unknown floating point error occurred ***\n"); 4227d125cddSJed Brown (*PetscErrorPrintf)("The specific exception can be determined by running in a debugger. When the\n"); 4237d125cddSJed Brown (*PetscErrorPrintf)("debugger traps the signal, the exception can be found with fetestexcept(0x%x)\n",FE_ALL_EXCEPT); 4247d125cddSJed Brown (*PetscErrorPrintf)("where the result is a bitwise OR of the following flags:\n"); 4257d125cddSJed 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); 426b014e56cSJed Brown } 4277d125cddSJed Brown 4287d125cddSJed Brown (*PetscErrorPrintf)("Try option -start_in_debugger\n"); 42963d6bff0SBarry Smith #if defined(PETSC_USE_DEBUG) && !defined(PETSC_USE_PTHREAD) 4307d125cddSJed Brown if (!PetscStackActive) { 4317d125cddSJed Brown (*PetscErrorPrintf)(" or try option -log_stack\n"); 4327d125cddSJed Brown } else { 4337d125cddSJed Brown (*PetscErrorPrintf)("likely location of problem given in stack below\n"); 4347d125cddSJed Brown (*PetscErrorPrintf)("--------------------- Stack Frames ------------------------------------\n"); 4357d125cddSJed Brown PetscStackView(PETSC_VIEWER_STDOUT_SELF); 4367d125cddSJed Brown } 4377d125cddSJed Brown #endif 4387d125cddSJed Brown #if !defined(PETSC_USE_DEBUG) 4397d125cddSJed Brown (*PetscErrorPrintf)("configure using --with-debugging=yes, recompile, link, and run \n"); 4407d125cddSJed Brown (*PetscErrorPrintf)("with -start_in_debugger to get more information on the crash.\n"); 4417d125cddSJed Brown #endif 442668f157eSBarry Smith PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file","Unknown directory",PETSC_ERR_FP,PETSC_ERROR_INITIAL,"trapped floating point error"); 443b014e56cSJed Brown MPI_Abort(PETSC_COMM_WORLD,0); 444b014e56cSJed Brown } 445b014e56cSJed Brown EXTERN_C_END 446b014e56cSJed Brown 447b014e56cSJed Brown #undef __FUNCT__ 448b014e56cSJed Brown #define __FUNCT__ "PetscSetFPTrap" 4497087cfbeSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap on) 450b014e56cSJed Brown { 451b014e56cSJed Brown PetscFunctionBegin; 452b014e56cSJed Brown if (on == PETSC_FP_TRAP_ON) { 453b014e56cSJed Brown /* Clear any flags that are currently set so that activating trapping will not immediately call the signal handler. */ 454e32f2f54SBarry Smith if (feclearexcept(FE_ALL_EXCEPT)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot clear floating point exception flags\n"); 455b014e56cSJed Brown #if defined FE_NOMASK_ENV 456b014e56cSJed Brown /* We could use fesetenv(FE_NOMASK_ENV), but that causes spurious exceptions (like gettimeofday() -> PetscLogDouble). */ 457e32f2f54SBarry 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"); 458b014e56cSJed Brown #elif defined PETSC_HAVE_XMMINTRIN_H 459b014e56cSJed Brown _MM_SET_EXCEPTION_MASK(_MM_MASK_INEXACT); 460b014e56cSJed Brown #else 461b014e56cSJed Brown /* C99 does not provide a way to modify the environment so there is no portable way to activate trapping. */ 462b014e56cSJed Brown #endif 463e32f2f54SBarry Smith if (SIG_ERR == signal(SIGFPE,PetscDefaultFPTrap)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't set floating point handler\n"); 464b014e56cSJed Brown } else { 465e32f2f54SBarry Smith if (fesetenv(FE_DFL_ENV)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot disable floating point exceptions"); 466e32f2f54SBarry Smith if (SIG_ERR == signal(SIGFPE,SIG_DFL)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't clear floating point handler\n"); 467b014e56cSJed Brown } 468*670f3ff9SJed Brown _trapmode = on; 469b014e56cSJed Brown PetscFunctionReturn(0); 470b014e56cSJed Brown } 471b014e56cSJed Brown 472e5c89e4eSSatish Balay /* -------------------------Default -----------------------------------*/ 473e5c89e4eSSatish Balay #else 474e5c89e4eSSatish Balay EXTERN_C_BEGIN 475e5c89e4eSSatish Balay #undef __FUNCT__ 476e5c89e4eSSatish Balay #define __FUNCT__ "PetscDefaultFPTrap" 477e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig) 478e5c89e4eSSatish Balay { 479e5c89e4eSSatish Balay PetscFunctionBegin; 480e5c89e4eSSatish Balay (*PetscErrorPrintf)("*** floating point error occurred ***\n"); 4810a78d522SHong Zhang PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file","Unknown directory",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 482e5c89e4eSSatish Balay MPI_Abort(PETSC_COMM_WORLD,0); 483e5c89e4eSSatish Balay } 484e5c89e4eSSatish Balay EXTERN_C_END 485e5c89e4eSSatish Balay #undef __FUNCT__ 486e5c89e4eSSatish Balay #define __FUNCT__ "PetscSetFPTrap" 4877087cfbeSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap on) 488e5c89e4eSSatish Balay { 489e5c89e4eSSatish Balay PetscFunctionBegin; 490e5c89e4eSSatish Balay if (on == PETSC_FP_TRAP_ON) { 491e5c89e4eSSatish Balay if (SIG_ERR == signal(SIGFPE,PetscDefaultFPTrap)) { 492e5c89e4eSSatish Balay (*PetscErrorPrintf)("Can't set floatingpoint handler\n"); 493e5c89e4eSSatish Balay } 494e5c89e4eSSatish Balay } else { 495e5c89e4eSSatish Balay if (SIG_ERR == signal(SIGFPE,SIG_DFL)) { 496e5c89e4eSSatish Balay (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 497e5c89e4eSSatish Balay } 498e5c89e4eSSatish Balay } 499*670f3ff9SJed Brown _trapmode = on; 500e5c89e4eSSatish Balay PetscFunctionReturn(0); 501e5c89e4eSSatish Balay } 502e5c89e4eSSatish Balay #endif 503e5c89e4eSSatish Balay 504e5c89e4eSSatish Balay 505e5c89e4eSSatish Balay 506