17d0a6c19SBarry Smith 2e5c89e4eSSatish Balay /* 30e3d61c9SBarry Smith IEEE error handler for all machines. Since each OS has 40e3d61c9SBarry Smith enough slight differences we have completely separate codes for each one. 5e5c89e4eSSatish Balay */ 6b014e56cSJed Brown 7b014e56cSJed Brown /* 8b014e56cSJed Brown This feature test macro provides FE_NOMASK_ENV on GNU. It must be defined 9b014e56cSJed Brown at the top of the file because other headers may pull in fenv.h even when 10b014e56cSJed Brown not strictly necessary. Strictly speaking, we could include ONLY petscconf.h, 11b014e56cSJed Brown check PETSC_HAVE_FENV_H, and only define _GNU_SOURCE in that case, but such 12b014e56cSJed Brown shenanigans ought to be unnecessary. 13b014e56cSJed Brown */ 14519f805aSKarl Rupp #if !defined(_GNU_SOURCE) 15b014e56cSJed Brown #define _GNU_SOURCE 1676a6984eSJed Brown #endif 17b014e56cSJed Brown 1827104ee2SJacob Faibussowitsch #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/ 19e5c89e4eSSatish Balay #include <signal.h> 20e5c89e4eSSatish Balay 21670f3ff9SJed Brown struct PetscFPTrapLink { 22670f3ff9SJed Brown PetscFPTrap trapmode; 23670f3ff9SJed Brown struct PetscFPTrapLink *next; 24670f3ff9SJed Brown }; 25aba4c478SBarry Smith static PetscFPTrap _trapmode = PETSC_FP_TRAP_OFF; /* Current trapping mode; see PetscDetermineInitialFPTrap() */ 26670f3ff9SJed Brown static struct PetscFPTrapLink *_trapstack; /* Any pushed states of _trapmode */ 27670f3ff9SJed Brown 28670f3ff9SJed Brown /*@ 29cc9df77eSBarry Smith PetscFPTrapPush - push a floating point trapping mode, restored using PetscFPTrapPop() 30670f3ff9SJed Brown 31670f3ff9SJed Brown Not Collective 32670f3ff9SJed Brown 334165533cSJose E. Roman Input Parameter: 34*cf0818bdSBarry Smith . trap - PETSC_FP_TRAP_ON or PETSC_FP_TRAP_OFF or any of the values passable to `PetscSetFPTrap()` 35670f3ff9SJed Brown 36670f3ff9SJed Brown Level: advanced 37670f3ff9SJed Brown 38cc9df77eSBarry Smith Notes: 39cc9df77eSBarry Smith This only changes the trapping if the new mode is different than the current mode. 40cc9df77eSBarry Smith 41cc9df77eSBarry Smith This routine is called to turn off trapping for certain LAPACK routines that assume that dividing 42cc9df77eSBarry Smith by zero is acceptable. In particular the routine ieeeck(). 43cc9df77eSBarry Smith 44cc9df77eSBarry Smith Most systems by default have all trapping turned off, but certain Fortran compilers have 45cc9df77eSBarry Smith link flags that turn on trapping before the program begins. 46cc9df77eSBarry Smith $ gfortran -ffpe-trap=invalid,zero,overflow,underflow,denormal 47cc9df77eSBarry Smith $ ifort -fpe0 48cc9df77eSBarry Smith 49db781477SPatrick Sanan .seealso: `PetscFPTrapPop()`, `PetscSetFPTrap()`, `PetscDetermineInitialFPTrap()` 50670f3ff9SJed Brown @*/ 51670f3ff9SJed Brown PetscErrorCode PetscFPTrapPush(PetscFPTrap trap) 52670f3ff9SJed Brown { 53670f3ff9SJed Brown struct PetscFPTrapLink *link; 54670f3ff9SJed Brown 55670f3ff9SJed Brown PetscFunctionBegin; 569566063dSJacob Faibussowitsch PetscCall(PetscNew(&link)); 57670f3ff9SJed Brown link->trapmode = _trapmode; 58670f3ff9SJed Brown link->next = _trapstack; 59670f3ff9SJed Brown _trapstack = link; 609566063dSJacob Faibussowitsch if (trap != _trapmode) PetscCall(PetscSetFPTrap(trap)); 61670f3ff9SJed Brown PetscFunctionReturn(0); 62670f3ff9SJed Brown } 63670f3ff9SJed Brown 64670f3ff9SJed Brown /*@ 65670f3ff9SJed Brown PetscFPTrapPop - push a floating point trapping mode, to be restored using PetscFPTrapPop() 66670f3ff9SJed Brown 67670f3ff9SJed Brown Not Collective 68670f3ff9SJed Brown 69670f3ff9SJed Brown Level: advanced 70670f3ff9SJed Brown 71db781477SPatrick Sanan .seealso: `PetscFPTrapPush()`, `PetscSetFPTrap()`, `PetscDetermineInitialFPTrap()` 72670f3ff9SJed Brown @*/ 73670f3ff9SJed Brown PetscErrorCode PetscFPTrapPop(void) 74670f3ff9SJed Brown { 75670f3ff9SJed Brown struct PetscFPTrapLink *link; 76670f3ff9SJed Brown 77670f3ff9SJed Brown PetscFunctionBegin; 789566063dSJacob Faibussowitsch if (_trapstack->trapmode != _trapmode) PetscCall(PetscSetFPTrap(_trapstack->trapmode)); 79670f3ff9SJed Brown link = _trapstack; 80670f3ff9SJed Brown _trapstack = _trapstack->next; 819566063dSJacob Faibussowitsch PetscCall(PetscFree(link)); 82670f3ff9SJed Brown PetscFunctionReturn(0); 83670f3ff9SJed Brown } 84670f3ff9SJed Brown 85e5c89e4eSSatish Balay /*--------------------------------------- ---------------------------------------------------*/ 86e5c89e4eSSatish Balay #if defined(PETSC_HAVE_SUN4_STYLE_FPTRAP) 87e5c89e4eSSatish Balay #include <floatingpoint.h> 88e5c89e4eSSatish Balay 898cc058d9SJed Brown PETSC_EXTERN PetscErrorCode ieee_flags(char*,char*,char*,char**); 908cc058d9SJed Brown PETSC_EXTERN PetscErrorCode ieee_handler(char*,char*,sigfpe_handler_type(int,int,struct sigcontext*,char*)); 91e5c89e4eSSatish Balay 92db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 93e5c89e4eSSatish Balay { FPE_INTDIV_TRAP ,"integer divide" }, 94e5c89e4eSSatish Balay { FPE_FLTOPERR_TRAP ,"IEEE operand error" }, 95e5c89e4eSSatish Balay { FPE_FLTOVF_TRAP ,"floating point overflow" }, 96e5c89e4eSSatish Balay { FPE_FLTUND_TRAP ,"floating point underflow" }, 97e5c89e4eSSatish Balay { FPE_FLTDIV_TRAP ,"floating pointing divide" }, 98e5c89e4eSSatish Balay { FPE_FLTINEX_TRAP ,"inexact floating point result" }, 99e5c89e4eSSatish Balay { 0 ,"unknown error" } 100e5c89e4eSSatish Balay }; 101e5c89e4eSSatish Balay #define SIGPC(scp) (scp->sc_pc) 102e5c89e4eSSatish Balay 103*cf0818bdSBarry Smith /* this function gets called if a trap has occurred and been caught */ 104e5c89e4eSSatish Balay sigfpe_handler_type PetscDefaultFPTrap(int sig,int code,struct sigcontext *scp,char *addr) 105e5c89e4eSSatish Balay { 1065f80ce2aSJacob Faibussowitsch int err_ind = -1; 107e5c89e4eSSatish Balay 108e5c89e4eSSatish Balay PetscFunctionBegin; 1095f80ce2aSJacob Faibussowitsch for (int j = 0; error_codes[j].code_no; j++) { 110e5c89e4eSSatish Balay if (error_codes[j].code_no == code) err_ind = j; 111e5c89e4eSSatish Balay } 112e5c89e4eSSatish Balay 113a297a907SKarl Rupp if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred at pc=%X ***\n",error_codes[err_ind].name,SIGPC(scp)); 114a297a907SKarl Rupp else (*PetscErrorPrintf)("*** floating point error 0x%x occurred at pc=%X ***\n",code,SIGPC(scp)); 115a297a907SKarl Rupp 11649c86fc7SBarry Smith (void)PetscError(PETSC_COMM_SELF,PETSC_ERR_FP,NULL,NULL,PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 11741e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 118e5c89e4eSSatish Balay PetscFunctionReturn(0); 119e5c89e4eSSatish Balay } 120e5c89e4eSSatish Balay 121e30d2299SSatish Balay /*@ 122*cf0818bdSBarry Smith PetscSetFPTrap - Enables traps/exceptions on common floating point errors. This option may not work on certain systems or only a 123*cf0818bdSBarry Smith subset of exceptions may be trapable. 124e5c89e4eSSatish Balay 125e5c89e4eSSatish Balay Not Collective 126e5c89e4eSSatish Balay 127e5c89e4eSSatish Balay Input Parameters: 128*cf0818bdSBarry Smith . flag - values are 129*cf0818bdSBarry Smith .vb 130*cf0818bdSBarry Smith PETSC_FP_TRAP_OFF - do not trap any exceptions 131*cf0818bdSBarry Smith PETSC_FP_TRAP_ON - all exceptions that are possible on the system except underflow 132*cf0818bdSBarry Smith PETSC_FP_TRAP_INDIV - integer divide by zero 133*cf0818bdSBarry Smith PETSC_FP_TRAP_FLTOPERR - improper argument to function, for example with real numbers, the square root of a negative number 134*cf0818bdSBarry Smith PETSC_FP_TRAP_FLTOVF - overflow 135*cf0818bdSBarry Smith PETSC_FP_TRAP_FLTUND - underflow - not trapped by default on most systems 136*cf0818bdSBarry Smith PETSC_FP_TRAP_FLTDIV - floating point divide by zero 137*cf0818bdSBarry Smith PETSC_FP_TRAP_FLTINEX - inexact floating point result 138*cf0818bdSBarry Smith .ve 139e5c89e4eSSatish Balay 140e5c89e4eSSatish Balay Options Database Keys: 141*cf0818bdSBarry Smith . -fp_trap <off,on> - turn on or off trapping of floating point exceptions 142e5c89e4eSSatish Balay 143e5c89e4eSSatish Balay Level: advanced 144e5c89e4eSSatish Balay 145*cf0818bdSBarry Smith Notes: 146*cf0818bdSBarry Smith Currently only PETSC_FP_TRAP_OFF and PETSC_FP_TRAP_ON are handled. All others are treated as PETSC_FP_TRAP_ON. 147*cf0818bdSBarry Smith 148*cf0818bdSBarry Smith The values are bit values and may be |ed together in the function call 149*cf0818bdSBarry Smith 150*cf0818bdSBarry Smith On systems that support it this routine causes floating point 151*cf0818bdSBarry Smith overflow, divide-by-zero, and invalid-operand (e.g., a NaN), but not underflow, to 152e5c89e4eSSatish Balay cause a message to be printed and the program to exit. 153e5c89e4eSSatish Balay 154cc9df77eSBarry Smith On many common systems, the floating 1557d125cddSJed Brown point exception state is not preserved from the location where the trap 1567d125cddSJed Brown occurred through to the signal handler. In this case, the signal handler 1577d125cddSJed Brown will just say that an unknown floating point exception occurred and which 1587d125cddSJed Brown function it occurred in. If you run with -fp_trap in a debugger, it will 159cc9df77eSBarry Smith break on the line where the error occurred. On systems that support C99 160cc9df77eSBarry Smith floating point exception handling You can check which 1617d125cddSJed Brown exception occurred using fetestexcept(FE_ALL_EXCEPT). See fenv.h 1627d125cddSJed Brown (usually at /usr/include/bits/fenv.h) for the enum values on your system. 1637d125cddSJed Brown 164e5c89e4eSSatish Balay Caution: 165cc9df77eSBarry Smith On certain machines, in particular the IBM PowerPC, floating point 166cc9df77eSBarry Smith trapping may be VERY slow! 167e5c89e4eSSatish Balay 168db781477SPatrick Sanan .seealso: `PetscFPTrapPush()`, `PetscFPTrapPop()`, `PetscDetermineInitialFPTrap()` 169e5c89e4eSSatish Balay @*/ 170e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 171e5c89e4eSSatish Balay { 172e5c89e4eSSatish Balay char *out; 173e5c89e4eSSatish Balay 174e5c89e4eSSatish Balay PetscFunctionBegin; 175e5c89e4eSSatish Balay /* Clear accumulated exceptions. Used to suppress meaningless messages from f77 programs */ 176e5c89e4eSSatish Balay (void) ieee_flags("clear","exception","all",&out); 177*cf0818bdSBarry Smith if (flag) { 178e5c89e4eSSatish Balay /* 179a297a907SKarl Rupp To trap more fp exceptions, including underflow, change the line below to 180e5c89e4eSSatish Balay if (ieee_handler("set","all",PetscDefaultFPTrap)) { 181e5c89e4eSSatish Balay */ 182a297a907SKarl Rupp if (ieee_handler("set","common",PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't set floatingpoint handler\n"); 183a297a907SKarl Rupp } else if (ieee_handler("clear","common",PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 184a297a907SKarl Rupp 185670f3ff9SJed Brown _trapmode = flag; 186e5c89e4eSSatish Balay PetscFunctionReturn(0); 187e5c89e4eSSatish Balay } 188e5c89e4eSSatish Balay 189cc9df77eSBarry Smith /*@ 190aba4c478SBarry Smith PetscDetermineInitialFPTrap - Attempts to determine the floating point trapping that exists when PetscInitialize() is called 191cc9df77eSBarry Smith 192cc9df77eSBarry Smith Not Collective 193cc9df77eSBarry Smith 194cc9df77eSBarry Smith Notes: 195cc9df77eSBarry Smith Currently only supported on Linux and MacOS. Checks if divide by zero is enable and if so declares that trapping is on. 196cc9df77eSBarry Smith 197ee300463SSatish Balay Level: advanced 198cc9df77eSBarry Smith 199db781477SPatrick Sanan .seealso: `PetscFPTrapPush()`, `PetscFPTrapPop()`, `PetscDetermineInitialFPTrap()` 200cc9df77eSBarry Smith @*/ 201aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 202cc9df77eSBarry Smith { 203cc9df77eSBarry Smith PetscFunctionBegin; 2049566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 205cc9df77eSBarry Smith PetscFunctionReturn(0); 206cc9df77eSBarry Smith } 207cc9df77eSBarry Smith 208e5c89e4eSSatish Balay /* -------------------------------------------------------------------------------------------*/ 209e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_SOLARIS_STYLE_FPTRAP) 210e5c89e4eSSatish Balay #include <sunmath.h> 211e5c89e4eSSatish Balay #include <floatingpoint.h> 212e5c89e4eSSatish Balay #include <siginfo.h> 213e5c89e4eSSatish Balay #include <ucontext.h> 214e5c89e4eSSatish Balay 215db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 216e5c89e4eSSatish Balay { FPE_FLTINV,"invalid floating point operand"}, 217e5c89e4eSSatish Balay { FPE_FLTRES,"inexact floating point result"}, 218e5c89e4eSSatish Balay { FPE_FLTDIV,"division-by-zero"}, 219e5c89e4eSSatish Balay { FPE_FLTUND,"floating point underflow"}, 220e5c89e4eSSatish Balay { FPE_FLTOVF,"floating point overflow"}, 221e5c89e4eSSatish Balay { 0, "unknown error"} 222e5c89e4eSSatish Balay }; 223e5c89e4eSSatish Balay #define SIGPC(scp) (scp->si_addr) 224e5c89e4eSSatish Balay 225e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig,siginfo_t *scp,ucontext_t *uap) 226e5c89e4eSSatish Balay { 2275f80ce2aSJacob Faibussowitsch int err_ind = -1,code = scp->si_code; 228e5c89e4eSSatish Balay 229e5c89e4eSSatish Balay PetscFunctionBegin; 2305f80ce2aSJacob Faibussowitsch for (int j = 0; error_codes[j].code_no; j++) { 231e5c89e4eSSatish Balay if (error_codes[j].code_no == code) err_ind = j; 232e5c89e4eSSatish Balay } 233e5c89e4eSSatish Balay 234a297a907SKarl Rupp if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred at pc=%X ***\n",error_codes[err_ind].name,SIGPC(scp)); 235a297a907SKarl Rupp else (*PetscErrorPrintf)("*** floating point error 0x%x occurred at pc=%X ***\n",code,SIGPC(scp)); 236a297a907SKarl Rupp 23749c86fc7SBarry Smith (void)PetscError(PETSC_COMM_SELF,0,NULL,NULL,PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 23841e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 239e5c89e4eSSatish Balay } 240e5c89e4eSSatish Balay 241e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 242e5c89e4eSSatish Balay { 243e5c89e4eSSatish Balay char *out; 244e5c89e4eSSatish Balay 245e5c89e4eSSatish Balay PetscFunctionBegin; 246e5c89e4eSSatish Balay /* Clear accumulated exceptions. Used to suppress meaningless messages from f77 programs */ 247e5c89e4eSSatish Balay (void) ieee_flags("clear","exception","all",&out); 248e5c89e4eSSatish Balay if (flag == PETSC_FP_TRAP_ON) { 249a297a907SKarl Rupp if (ieee_handler("set","common",(sigfpe_handler_type)PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't set floating point handler\n"); 250cc9df77eSBarry Smith } else { 251cc9df77eSBarry Smith if (ieee_handler("clear","common",(sigfpe_handler_type)PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 252cc9df77eSBarry Smith } 253670f3ff9SJed Brown _trapmode = flag; 254e5c89e4eSSatish Balay PetscFunctionReturn(0); 255e5c89e4eSSatish Balay } 256e5c89e4eSSatish Balay 257aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 258cc9df77eSBarry Smith { 259cc9df77eSBarry Smith PetscFunctionBegin; 2609566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 261cc9df77eSBarry Smith PetscFunctionReturn(0); 262cc9df77eSBarry Smith } 263cc9df77eSBarry Smith 264cc9df77eSBarry Smith /* ------------------------------------------------------------------------------------------*/ 265e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_IRIX_STYLE_FPTRAP) 266e5c89e4eSSatish Balay #include <sigfpe.h> 267db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 268e5c89e4eSSatish Balay { _INVALID ,"IEEE operand error" }, 269e5c89e4eSSatish Balay { _OVERFL ,"floating point overflow" }, 270e5c89e4eSSatish Balay { _UNDERFL ,"floating point underflow" }, 271e5c89e4eSSatish Balay { _DIVZERO ,"floating point divide" }, 272e5c89e4eSSatish Balay { 0 ,"unknown error" } 273e5c89e4eSSatish Balay } ; 274e5c89e4eSSatish Balay void PetscDefaultFPTrap(unsigned exception[],int val[]) 275e5c89e4eSSatish Balay { 2765f80ce2aSJacob Faibussowitsch int err_ind = -1,code = exception[0]; 277e5c89e4eSSatish Balay 278e5c89e4eSSatish Balay PetscFunctionBegin; 2795f80ce2aSJacob Faibussowitsch for (int j = 0; error_codes[j].code_no; j++) { 280e5c89e4eSSatish Balay if (error_codes[j].code_no == code) err_ind = j; 281e5c89e4eSSatish Balay } 282a297a907SKarl Rupp if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred ***\n",error_codes[err_ind].name); 283a297a907SKarl Rupp else (*PetscErrorPrintf)("*** floating point error 0x%x occurred ***\n",code); 284a297a907SKarl Rupp 28549c86fc7SBarry Smith (void)PetscError(PETSC_COMM_SELF,0,NULL,NULL,PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 28641e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 287e5c89e4eSSatish Balay } 288e5c89e4eSSatish Balay 289e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 290e5c89e4eSSatish Balay { 291e5c89e4eSSatish Balay PetscFunctionBegin; 292*cf0818bdSBarry Smith if (flag) handle_sigfpes(_ON,,_EN_UNDERFL|_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,PetscDefaultFPTrap,_ABORT_ON_ERROR,0); 293cc9df77eSBarry Smith else handle_sigfpes(_OFF,_EN_UNDERFL|_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,0,_ABORT_ON_ERROR,0); 294670f3ff9SJed Brown _trapmode = flag; 295e5c89e4eSSatish Balay PetscFunctionReturn(0); 296e5c89e4eSSatish Balay } 297cc9df77eSBarry Smith 298aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 299cc9df77eSBarry Smith { 300cc9df77eSBarry Smith PetscFunctionBegin; 3019566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 302cc9df77eSBarry Smith PetscFunctionReturn(0); 303cc9df77eSBarry Smith } 304cc9df77eSBarry Smith 305e5c89e4eSSatish Balay /* -------------------------------------------------------------------------------------------*/ 306cc9df77eSBarry Smith #elif defined(PETSC_HAVE_SOLARIS_STYLE_FPTRAP) 307cc9df77eSBarry Smith #include <sunmath.h> 308cc9df77eSBarry Smith #include <floatingpoint.h> 309cc9df77eSBarry Smith #include <siginfo.h> 310cc9df77eSBarry Smith #include <ucontext.h> 311cc9df77eSBarry Smith 312cc9df77eSBarry Smith static struct { int code_no; char *name; } error_codes[] = { 313cc9df77eSBarry Smith { FPE_FLTINV,"invalid floating point operand"}, 314cc9df77eSBarry Smith { FPE_FLTRES,"inexact floating point result"}, 315cc9df77eSBarry Smith { FPE_FLTDIV,"division-by-zero"}, 316cc9df77eSBarry Smith { FPE_FLTUND,"floating point underflow"}, 317cc9df77eSBarry Smith { FPE_FLTOVF,"floating point overflow"}, 318cc9df77eSBarry Smith { 0, "unknown error"} 319cc9df77eSBarry Smith }; 320cc9df77eSBarry Smith #define SIGPC(scp) (scp->si_addr) 321cc9df77eSBarry Smith 322cc9df77eSBarry Smith void PetscDefaultFPTrap(int sig,siginfo_t *scp,ucontext_t *uap) 323cc9df77eSBarry Smith { 3245f80ce2aSJacob Faibussowitsch int err_ind = -1,code = scp->si_code; 325cc9df77eSBarry Smith 326cc9df77eSBarry Smith PetscFunctionBegin; 327cc9df77eSBarry Smith err_ind = -1; 3285f80ce2aSJacob Faibussowitsch for (int j = 0; error_codes[j].code_no; j++) { 329cc9df77eSBarry Smith if (error_codes[j].code_no == code) err_ind = j; 330cc9df77eSBarry Smith } 331cc9df77eSBarry Smith 332cc9df77eSBarry Smith if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred at pc=%X ***\n",error_codes[err_ind].name,SIGPC(scp)); 333cc9df77eSBarry Smith else (*PetscErrorPrintf)("*** floating point error 0x%x occurred at pc=%X ***\n",code,SIGPC(scp)); 334cc9df77eSBarry Smith 33549c86fc7SBarry Smith (void)PetscError(PETSC_COMM_SELF,0,NULL,NULL,PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 336cc9df77eSBarry Smith PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 337cc9df77eSBarry Smith } 338cc9df77eSBarry Smith 339cc9df77eSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 340cc9df77eSBarry Smith { 341cc9df77eSBarry Smith char *out; 342cc9df77eSBarry Smith 343cc9df77eSBarry Smith PetscFunctionBegin; 344cc9df77eSBarry Smith /* Clear accumulated exceptions. Used to suppress meaningless messages from f77 programs */ 345cc9df77eSBarry Smith (void) ieee_flags("clear","exception","all",&out); 346*cf0818bdSBarry Smith if (flag) { 347cc9df77eSBarry Smith if (ieee_handler("set","common",(sigfpe_handler_type)PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't set floating point handler\n"); 348cc9df77eSBarry Smith } else { 349cc9df77eSBarry Smith if (ieee_handler("clear","common",(sigfpe_handler_type)PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 350cc9df77eSBarry Smith } 351cc9df77eSBarry Smith _trapmode = flag; 352cc9df77eSBarry Smith PetscFunctionReturn(0); 353cc9df77eSBarry Smith } 354cc9df77eSBarry Smith 355aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 356cc9df77eSBarry Smith { 357cc9df77eSBarry Smith PetscFunctionBegin; 3589566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 359cc9df77eSBarry Smith PetscFunctionReturn(0); 360cc9df77eSBarry Smith } 361cc9df77eSBarry Smith 362cc9df77eSBarry Smith /*----------------------------------------------- --------------------------------------------*/ 363cc9df77eSBarry Smith #elif defined(PETSC_HAVE_RS6000_STYLE_FPTRAP) 364e5c89e4eSSatish Balay /* In "fast" mode, floating point traps are imprecise and ignored. 365e5c89e4eSSatish Balay This is the reason for the fptrap(FP_TRAP_SYNC) call */ 366e5c89e4eSSatish Balay struct sigcontext; 367e5c89e4eSSatish Balay #include <fpxcp.h> 368e5c89e4eSSatish Balay #include <fptrap.h> 369e5c89e4eSSatish Balay #define FPE_FLTOPERR_TRAP (fptrap_t)(0x20000000) 370e5c89e4eSSatish Balay #define FPE_FLTOVF_TRAP (fptrap_t)(0x10000000) 371e5c89e4eSSatish Balay #define FPE_FLTUND_TRAP (fptrap_t)(0x08000000) 372e5c89e4eSSatish Balay #define FPE_FLTDIV_TRAP (fptrap_t)(0x04000000) 373e5c89e4eSSatish Balay #define FPE_FLTINEX_TRAP (fptrap_t)(0x02000000) 374e5c89e4eSSatish Balay 375db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 376e5c89e4eSSatish Balay {FPE_FLTOPERR_TRAP ,"IEEE operand error" }, 377e5c89e4eSSatish Balay { FPE_FLTOVF_TRAP ,"floating point overflow" }, 378e5c89e4eSSatish Balay { FPE_FLTUND_TRAP ,"floating point underflow" }, 379e5c89e4eSSatish Balay { FPE_FLTDIV_TRAP ,"floating point divide" }, 380e5c89e4eSSatish Balay { FPE_FLTINEX_TRAP ,"inexact floating point result" }, 381e5c89e4eSSatish Balay { 0 ,"unknown error" } 382e5c89e4eSSatish Balay } ; 383e5c89e4eSSatish Balay #define SIGPC(scp) (0) /* Info MIGHT be in scp->sc_jmpbuf.jmp_context.iar */ 384e5c89e4eSSatish Balay /* 385e5c89e4eSSatish Balay For some reason, scp->sc_jmpbuf does not work on the RS6000, even though 386e5c89e4eSSatish Balay it looks like it should from the include definitions. It is probably 387e5c89e4eSSatish Balay some strange interaction with the "POSIX_SOURCE" that we require. 388e5c89e4eSSatish Balay */ 389e5c89e4eSSatish Balay 390e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig,int code,struct sigcontext *scp) 391e5c89e4eSSatish Balay { 392e5c89e4eSSatish Balay int err_ind,j; 393e5c89e4eSSatish Balay fp_ctx_t flt_context; 394e5c89e4eSSatish Balay 395e5c89e4eSSatish Balay PetscFunctionBegin; 396e5c89e4eSSatish Balay fp_sh_trap_info(scp,&flt_context); 397e5c89e4eSSatish Balay 398e5c89e4eSSatish Balay err_ind = -1; 399e5c89e4eSSatish Balay for (j = 0; error_codes[j].code_no; j++) { 400e5c89e4eSSatish Balay if (error_codes[j].code_no == flt_context.trap) err_ind = j; 401e5c89e4eSSatish Balay } 402e5c89e4eSSatish Balay 403a297a907SKarl Rupp if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred ***\n",error_codes[err_ind].name); 404a297a907SKarl Rupp else (*PetscErrorPrintf)("*** floating point error 0x%x occurred ***\n",flt_context.trap); 405a297a907SKarl Rupp 40649c86fc7SBarry Smith (void)PetscError(PETSC_COMM_SELF,0,NULL,NULL,PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 40741e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 408e5c89e4eSSatish Balay } 409e5c89e4eSSatish Balay 410*cf0818bdSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 411e5c89e4eSSatish Balay { 412e5c89e4eSSatish Balay PetscFunctionBegin; 413*cf0818bdSBarry Smith if (flag) { 414e5c89e4eSSatish Balay signal(SIGFPE,(void (*)(int))PetscDefaultFPTrap); 415e5c89e4eSSatish Balay fp_trap(FP_TRAP_SYNC); 416cc9df77eSBarry Smith fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW | TRP_UNDERFLOW); 417e5c89e4eSSatish Balay /* fp_enable(mask) for individual traps. Values are: 418e5c89e4eSSatish Balay TRP_INVALID 419e5c89e4eSSatish Balay TRP_DIV_BY_ZERO 420e5c89e4eSSatish Balay TRP_OVERFLOW 421e5c89e4eSSatish Balay TRP_UNDERFLOW 422e5c89e4eSSatish Balay TRP_INEXACT 423e5c89e4eSSatish Balay Can OR then together. 424e5c89e4eSSatish Balay fp_enable_all(); for all traps. 425e5c89e4eSSatish Balay */ 426e5c89e4eSSatish Balay } else { 427e5c89e4eSSatish Balay signal(SIGFPE,SIG_DFL); 428cc9df77eSBarry Smith fp_disable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW | TRP_UNDERFLOW); 429e5c89e4eSSatish Balay fp_trap(FP_TRAP_OFF); 430e5c89e4eSSatish Balay } 431*cf0818bdSBarry Smith _trapmode = flag; 432e5c89e4eSSatish Balay PetscFunctionReturn(0); 433e5c89e4eSSatish Balay } 434e5c89e4eSSatish Balay 435aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 436cc9df77eSBarry Smith { 437cc9df77eSBarry Smith PetscFunctionBegin; 4389566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 439cc9df77eSBarry Smith PetscFunctionReturn(0); 440cc9df77eSBarry Smith } 441cc9df77eSBarry Smith 442cc9df77eSBarry Smith /* ------------------------------------------------------------*/ 443cc9df77eSBarry Smith #elif defined(PETSC_HAVE_WINDOWS_COMPILERS) 444cc9df77eSBarry Smith #include <float.h> 445cc9df77eSBarry Smith void PetscDefaultFPTrap(int sig) 446cc9df77eSBarry Smith { 447cc9df77eSBarry Smith PetscFunctionBegin; 448cc9df77eSBarry Smith (*PetscErrorPrintf)("*** floating point error occurred ***\n"); 44949c86fc7SBarry Smith PetscError(PETSC_COMM_SELF,0,NULL,NULL,PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 450cc9df77eSBarry Smith PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 451cc9df77eSBarry Smith } 452cc9df77eSBarry Smith 453*cf0818bdSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 454cc9df77eSBarry Smith { 455cc9df77eSBarry Smith unsigned int cw; 456cc9df77eSBarry Smith 457cc9df77eSBarry Smith PetscFunctionBegin; 458*cf0818bdSBarry Smith if (flag) { 459cc9df77eSBarry Smith cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW; 46008401ef6SPierre Jolivet PetscCheck(SIG_ERR != signal(SIGFPE,PetscDefaultFPTrap),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't set floating point handler"); 461cc9df77eSBarry Smith } else { 462cc9df77eSBarry Smith cw = 0; 46308401ef6SPierre Jolivet PetscCheck(SIG_ERR != signal(SIGFPE,SIG_DFL),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't clear floating point handler"); 464cc9df77eSBarry Smith } 465cc9df77eSBarry Smith (void)_controlfp(0, cw); 466*cf0818bdSBarry Smith _trapmode = flag; 467cc9df77eSBarry Smith PetscFunctionReturn(0); 468cc9df77eSBarry Smith } 469cc9df77eSBarry Smith 470aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 471cc9df77eSBarry Smith { 472cc9df77eSBarry Smith PetscFunctionBegin; 4739566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 474cc9df77eSBarry Smith PetscFunctionReturn(0); 475cc9df77eSBarry Smith } 476cc9df77eSBarry Smith 477cc9df77eSBarry Smith /* ------------------------------------------------------------*/ 4789a2402e9SBarry Smith #elif defined(PETSC_HAVE_FENV_H) && !defined(__cplusplus) 479b014e56cSJed Brown /* 480b014e56cSJed Brown C99 style floating point environment. 481b014e56cSJed Brown 482b014e56cSJed Brown Note that C99 merely specifies how to save, restore, and clear the floating 483b014e56cSJed Brown point environment as well as defining an enumeration of exception codes. In 484b014e56cSJed Brown particular, C99 does not specify how to make floating point exceptions raise 485b014e56cSJed Brown a signal. Glibc offers this capability through FE_NOMASK_ENV (or with finer 486b014e56cSJed Brown granularity, feenableexcept()), xmmintrin.h offers _MM_SET_EXCEPTION_MASK(). 487b014e56cSJed Brown */ 488b014e56cSJed Brown #include <fenv.h> 489b014e56cSJed Brown typedef struct {int code; const char *name;} FPNode; 490b014e56cSJed Brown static const FPNode error_codes[] = { 491b014e56cSJed Brown {FE_DIVBYZERO,"divide by zero"}, 492b014e56cSJed Brown {FE_INEXACT, "inexact floating point result"}, 493b014e56cSJed Brown {FE_INVALID, "invalid floating point arguments (domain error)"}, 494b014e56cSJed Brown {FE_OVERFLOW, "floating point overflow"}, 495b014e56cSJed Brown {FE_UNDERFLOW,"floating point underflow"}, 496b014e56cSJed Brown {0 ,"unknown error"} 497b014e56cSJed Brown }; 49899e0435eSBarry Smith 499b014e56cSJed Brown void PetscDefaultFPTrap(int sig) 500b014e56cSJed Brown { 501b014e56cSJed Brown const FPNode *node; 502b014e56cSJed Brown int code; 503ace3abfcSBarry Smith PetscBool matched = PETSC_FALSE; 504b014e56cSJed Brown 505b014e56cSJed Brown PetscFunctionBegin; 506b014e56cSJed Brown /* Note: While it is possible for the exception state to be preserved by the 507b014e56cSJed Brown * kernel, this seems to be rare which makes the following flag testing almost 508b014e56cSJed Brown * useless. But on a system where the flags can be preserved, it would provide 5097d125cddSJed Brown * more detail. 510b014e56cSJed Brown */ 511b014e56cSJed Brown code = fetestexcept(FE_ALL_EXCEPT); 512b014e56cSJed Brown for (node=&error_codes[0]; node->code; node++) { 513b014e56cSJed Brown if (code & node->code) { 514b014e56cSJed Brown matched = PETSC_TRUE; 515b014e56cSJed Brown (*PetscErrorPrintf)("*** floating point error \"%s\" occurred ***\n",node->name); 516b014e56cSJed Brown code &= ~node->code; /* Unset this flag since it has been processed */ 517b014e56cSJed Brown } 518b014e56cSJed Brown } 519b014e56cSJed Brown if (!matched || code) { /* If any remaining flags are set, or we didn't process any flags */ 520b014e56cSJed Brown (*PetscErrorPrintf)("*** unknown floating point error occurred ***\n"); 5217d125cddSJed Brown (*PetscErrorPrintf)("The specific exception can be determined by running in a debugger. When the\n"); 5227d125cddSJed Brown (*PetscErrorPrintf)("debugger traps the signal, the exception can be found with fetestexcept(0x%x)\n",FE_ALL_EXCEPT); 5237d125cddSJed Brown (*PetscErrorPrintf)("where the result is a bitwise OR of the following flags:\n"); 5247d125cddSJed 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); 525b014e56cSJed Brown } 5267d125cddSJed Brown 5277d125cddSJed Brown (*PetscErrorPrintf)("Try option -start_in_debugger\n"); 52827104ee2SJacob Faibussowitsch #if PetscDefined(USE_DEBUG) 5297d125cddSJed Brown (*PetscErrorPrintf)("likely location of problem given in stack below\n"); 5307d125cddSJed Brown (*PetscErrorPrintf)("--------------------- Stack Frames ------------------------------------\n"); 531639ff905SBarry Smith PetscStackView(PETSC_STDOUT); 53227104ee2SJacob Faibussowitsch #else 5337d125cddSJed Brown (*PetscErrorPrintf)("configure using --with-debugging=yes, recompile, link, and run \n"); 5347d125cddSJed Brown (*PetscErrorPrintf)("with -start_in_debugger to get more information on the crash.\n"); 53527104ee2SJacob Faibussowitsch #endif 53649c86fc7SBarry Smith PetscError(PETSC_COMM_SELF,0,NULL,NULL,PETSC_ERR_FP,PETSC_ERROR_INITIAL,"trapped floating point error"); 53741e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 538b014e56cSJed Brown } 539b014e56cSJed Brown 540*cf0818bdSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 541b014e56cSJed Brown { 542b014e56cSJed Brown PetscFunctionBegin; 543*cf0818bdSBarry Smith if (flag) { 544b014e56cSJed Brown /* Clear any flags that are currently set so that activating trapping will not immediately call the signal handler. */ 545cc73adaaSBarry Smith PetscCheck(!feclearexcept(FE_ALL_EXCEPT),PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot clear floating point exception flags"); 546cc9df77eSBarry Smith #if defined(FE_NOMASK_ENV) 547cc9df77eSBarry Smith /* Could use fesetenv(FE_NOMASK_ENV), but that causes spurious exceptions (like gettimeofday() -> PetscLogDouble). */ 548*cf0818bdSBarry Smith /* PetscCheck(feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW) != -1,PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot activate floating point exceptions"); */ 549*cf0818bdSBarry Smith PetscCheck(feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW) != -1,PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot activate floating point exceptions"); 550b014e56cSJed Brown #elif defined PETSC_HAVE_XMMINTRIN_H 551cc9df77eSBarry Smith _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_DIV_ZERO); 552cc9df77eSBarry Smith _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_UNDERFLOW); 553cc9df77eSBarry Smith _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_OVERFLOW); 554cc9df77eSBarry Smith _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_INVALID); 555b014e56cSJed Brown #else 556b014e56cSJed Brown /* C99 does not provide a way to modify the environment so there is no portable way to activate trapping. */ 557b014e56cSJed Brown #endif 55808401ef6SPierre Jolivet PetscCheck(SIG_ERR != signal(SIGFPE,PetscDefaultFPTrap),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't set floating point handler"); 559b014e56cSJed Brown } else { 560cc73adaaSBarry Smith PetscCheck(!fesetenv(FE_DFL_ENV),PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot disable floating point exceptions"); 561cc9df77eSBarry Smith /* can use _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() | _MM_MASK_UNDERFLOW); if PETSC_HAVE_XMMINTRIN_H exists */ 56208401ef6SPierre Jolivet PetscCheck(SIG_ERR != signal(SIGFPE,SIG_DFL),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't clear floating point handler"); 563b014e56cSJed Brown } 564*cf0818bdSBarry Smith _trapmode = flag; 565b014e56cSJed Brown PetscFunctionReturn(0); 566b014e56cSJed Brown } 567b014e56cSJed Brown 568aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 569cc9df77eSBarry Smith { 570cc9df77eSBarry Smith #if defined(FE_NOMASK_ENV) || defined PETSC_HAVE_XMMINTRIN_H 571cc9df77eSBarry Smith unsigned int flags; 572cc9df77eSBarry Smith #endif 573cc9df77eSBarry Smith 574cc9df77eSBarry Smith PetscFunctionBegin; 575cc9df77eSBarry Smith #if defined(FE_NOMASK_ENV) 576cc9df77eSBarry Smith flags = fegetexcept(); 577cc9df77eSBarry Smith if (flags & FE_DIVBYZERO) { 578cc9df77eSBarry Smith #elif defined PETSC_HAVE_XMMINTRIN_H 579cc9df77eSBarry Smith flags = _MM_GET_EXCEPTION_MASK(); 580cc9df77eSBarry Smith if (!(flags & _MM_MASK_DIV_ZERO)) { 581cc9df77eSBarry Smith #else 5829566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Floating point trapping unknown, assuming off\n")); 583cc9df77eSBarry Smith PetscFunctionReturn(0); 584cc9df77eSBarry Smith #endif 585cc9df77eSBarry Smith #if defined(FE_NOMASK_ENV) || defined PETSC_HAVE_XMMINTRIN_H 586cc9df77eSBarry Smith _trapmode = PETSC_FP_TRAP_ON; 5879566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Floating point trapping is on by default %d\n",flags)); 588cc9df77eSBarry Smith } else { 589cc9df77eSBarry Smith _trapmode = PETSC_FP_TRAP_OFF; 5909566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Floating point trapping is off by default %d\n",flags)); 591cc9df77eSBarry Smith } 592cc9df77eSBarry Smith PetscFunctionReturn(0); 593cc9df77eSBarry Smith #endif 594cc9df77eSBarry Smith } 595cc9df77eSBarry Smith 596cc9df77eSBarry Smith /* ------------------------------------------------------------*/ 597cc9df77eSBarry Smith #elif defined(PETSC_HAVE_IEEEFP_H) 598cc9df77eSBarry Smith #include <ieeefp.h> 599cc9df77eSBarry Smith void PetscDefaultFPTrap(int sig) 600cc9df77eSBarry Smith { 601cc9df77eSBarry Smith PetscFunctionBegin; 602cc9df77eSBarry Smith (*PetscErrorPrintf)("*** floating point error occurred ***\n"); 60349c86fc7SBarry Smith PetscError(PETSC_COMM_SELF,0,NULL,NULL,PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 604cc9df77eSBarry Smith PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 605cc9df77eSBarry Smith } 606cc9df77eSBarry Smith 607*cf0818bdSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 608cc9df77eSBarry Smith { 609cc9df77eSBarry Smith PetscFunctionBegin; 610*cf0818bdSBarry Smith if (flag == PETSC_FP_TRAP_ON) { 611cc9df77eSBarry Smith #if defined(PETSC_HAVE_FPPRESETSTICKY) 612cc9df77eSBarry Smith fpresetsticky(fpgetsticky()); 613cc9df77eSBarry Smith #elif defined(PETSC_HAVE_FPSETSTICKY) 614cc9df77eSBarry Smith fpsetsticky(fpgetsticky()); 615cc9df77eSBarry Smith #endif 616cc9df77eSBarry Smith fpsetmask(FP_X_INV | FP_X_DZ | FP_X_OFL | FP_X_OFL); 61708401ef6SPierre Jolivet PetscCheck(SIG_ERR != signal(SIGFPE,PetscDefaultFPTrap),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't set floating point handler"); 618cc9df77eSBarry Smith } else { 619cc9df77eSBarry Smith #if defined(PETSC_HAVE_FPPRESETSTICKY) 620cc9df77eSBarry Smith fpresetsticky(fpgetsticky()); 621cc9df77eSBarry Smith #elif defined(PETSC_HAVE_FPSETSTICKY) 622cc9df77eSBarry Smith fpsetsticky(fpgetsticky()); 623cc9df77eSBarry Smith #endif 624cc9df77eSBarry Smith fpsetmask(0); 62508401ef6SPierre Jolivet PetscCheck(SIG_ERR != signal(SIGFPE,SIG_DFL),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't clear floating point handler"); 626cc9df77eSBarry Smith } 627*cf0818bdSBarry Smith _trapmode = flag; 628cc9df77eSBarry Smith PetscFunctionReturn(0); 629cc9df77eSBarry Smith } 630cc9df77eSBarry Smith 631aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 632cc9df77eSBarry Smith { 633cc9df77eSBarry Smith PetscFunctionBegin; 6349566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 635cc9df77eSBarry Smith PetscFunctionReturn(0); 636cc9df77eSBarry Smith } 637cc9df77eSBarry Smith 638e5c89e4eSSatish Balay /* -------------------------Default -----------------------------------*/ 639e5c89e4eSSatish Balay #else 64099e0435eSBarry Smith 641e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig) 642e5c89e4eSSatish Balay { 643e5c89e4eSSatish Balay PetscFunctionBegin; 644e5c89e4eSSatish Balay (*PetscErrorPrintf)("*** floating point error occurred ***\n"); 64549c86fc7SBarry Smith PetscError(PETSC_COMM_SELF,0,NULL,NULL,PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 64641e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 647e5c89e4eSSatish Balay } 64899e0435eSBarry Smith 649*cf0818bdSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 650e5c89e4eSSatish Balay { 651e5c89e4eSSatish Balay PetscFunctionBegin; 652*cf0818bdSBarry Smith if (flag) { 653a297a907SKarl Rupp if (SIG_ERR == signal(SIGFPE,PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't set floatingpoint handler\n"); 654a297a907SKarl Rupp } else if (SIG_ERR == signal(SIGFPE,SIG_DFL)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 655a297a907SKarl Rupp 656*cf0818bdSBarry Smith _trapmode = flag; 657e5c89e4eSSatish Balay PetscFunctionReturn(0); 658e5c89e4eSSatish Balay } 659cc9df77eSBarry Smith 660aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 661cc9df77eSBarry Smith { 662cc9df77eSBarry Smith PetscFunctionBegin; 6639566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 664cc9df77eSBarry Smith PetscFunctionReturn(0); 665cc9df77eSBarry Smith } 666e5c89e4eSSatish Balay #endif 667