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