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: 34670f3ff9SJed Brown . trap - PETSC_FP_TRAP_ON or PETSC_FP_TRAP_OFF 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 49aba4c478SBarry Smith .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 71aba4c478SBarry Smith .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 103e5c89e4eSSatish Balay sigfpe_handler_type PetscDefaultFPTrap(int sig,int code,struct sigcontext *scp,char *addr) 104e5c89e4eSSatish Balay { 1055f80ce2aSJacob Faibussowitsch int err_ind = -1; 106e5c89e4eSSatish Balay 107e5c89e4eSSatish Balay PetscFunctionBegin; 1085f80ce2aSJacob Faibussowitsch for (int j = 0; error_codes[j].code_no; j++) { 109e5c89e4eSSatish Balay if (error_codes[j].code_no == code) err_ind = j; 110e5c89e4eSSatish Balay } 111e5c89e4eSSatish Balay 112a297a907SKarl Rupp if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred at pc=%X ***\n",error_codes[err_ind].name,SIGPC(scp)); 113a297a907SKarl Rupp else (*PetscErrorPrintf)("*** floating point error 0x%x occurred at pc=%X ***\n",code,SIGPC(scp)); 114a297a907SKarl Rupp 1155f80ce2aSJacob Faibussowitsch (void)PetscError(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 11641e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 117e5c89e4eSSatish Balay PetscFunctionReturn(0); 118e5c89e4eSSatish Balay } 119e5c89e4eSSatish Balay 120e30d2299SSatish Balay /*@ 121e5c89e4eSSatish Balay PetscSetFPTrap - Enables traps/exceptions on common floating point errors. 122cc9df77eSBarry Smith This option may not work on certain systems. 123e5c89e4eSSatish Balay 124e5c89e4eSSatish Balay Not Collective 125e5c89e4eSSatish Balay 126e5c89e4eSSatish Balay Input Parameters: 127e5c89e4eSSatish Balay . flag - PETSC_FP_TRAP_ON, PETSC_FP_TRAP_OFF. 128e5c89e4eSSatish Balay 129e5c89e4eSSatish Balay Options Database Keys: 130e5c89e4eSSatish Balay . -fp_trap - Activates floating point trapping 131e5c89e4eSSatish Balay 132e5c89e4eSSatish Balay Level: advanced 133e5c89e4eSSatish Balay 134e5c89e4eSSatish Balay Description: 135cc9df77eSBarry Smith On systems that support it, when called with PETSC_FP_TRAP_ON this routine causes floating point 136cc9df77eSBarry Smith underflow, overflow, divide-by-zero, and invalid-operand (e.g., a NaN) to 137e5c89e4eSSatish Balay cause a message to be printed and the program to exit. 138e5c89e4eSSatish Balay 1397d125cddSJed Brown Note: 140cc9df77eSBarry Smith On many common systems, the floating 1417d125cddSJed Brown point exception state is not preserved from the location where the trap 1427d125cddSJed Brown occurred through to the signal handler. In this case, the signal handler 1437d125cddSJed Brown will just say that an unknown floating point exception occurred and which 1447d125cddSJed Brown function it occurred in. If you run with -fp_trap in a debugger, it will 145cc9df77eSBarry Smith break on the line where the error occurred. On systems that support C99 146cc9df77eSBarry Smith floating point exception handling You can check which 1477d125cddSJed Brown exception occurred using fetestexcept(FE_ALL_EXCEPT). See fenv.h 1487d125cddSJed Brown (usually at /usr/include/bits/fenv.h) for the enum values on your system. 1497d125cddSJed Brown 150e5c89e4eSSatish Balay Caution: 151cc9df77eSBarry Smith On certain machines, in particular the IBM PowerPC, floating point 152cc9df77eSBarry Smith trapping may be VERY slow! 153e5c89e4eSSatish Balay 154aba4c478SBarry Smith .seealso: PetscFPTrapPush(), PetscFPTrapPop(), PetscDetermineInitialFPTrap() 155e5c89e4eSSatish Balay @*/ 156e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 157e5c89e4eSSatish Balay { 158e5c89e4eSSatish Balay char *out; 159e5c89e4eSSatish Balay 160e5c89e4eSSatish Balay PetscFunctionBegin; 161e5c89e4eSSatish Balay /* Clear accumulated exceptions. Used to suppress meaningless messages from f77 programs */ 162e5c89e4eSSatish Balay (void) ieee_flags("clear","exception","all",&out); 163e5c89e4eSSatish Balay if (flag == PETSC_FP_TRAP_ON) { 164e5c89e4eSSatish Balay /* 165a297a907SKarl Rupp To trap more fp exceptions, including underflow, change the line below to 166e5c89e4eSSatish Balay if (ieee_handler("set","all",PetscDefaultFPTrap)) { 167e5c89e4eSSatish Balay */ 168a297a907SKarl Rupp if (ieee_handler("set","common",PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't set floatingpoint handler\n"); 169a297a907SKarl Rupp } else if (ieee_handler("clear","common",PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 170a297a907SKarl Rupp 171670f3ff9SJed Brown _trapmode = flag; 172e5c89e4eSSatish Balay PetscFunctionReturn(0); 173e5c89e4eSSatish Balay } 174e5c89e4eSSatish Balay 175cc9df77eSBarry Smith /*@ 176aba4c478SBarry Smith PetscDetermineInitialFPTrap - Attempts to determine the floating point trapping that exists when PetscInitialize() is called 177cc9df77eSBarry Smith 178cc9df77eSBarry Smith Not Collective 179cc9df77eSBarry Smith 180cc9df77eSBarry Smith Notes: 181cc9df77eSBarry Smith Currently only supported on Linux and MacOS. Checks if divide by zero is enable and if so declares that trapping is on. 182cc9df77eSBarry Smith 183ee300463SSatish Balay Level: advanced 184cc9df77eSBarry Smith 185aba4c478SBarry Smith .seealso: PetscFPTrapPush(), PetscFPTrapPop(), PetscDetermineInitialFPTrap() 186cc9df77eSBarry Smith @*/ 187aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 188cc9df77eSBarry Smith { 189cc9df77eSBarry Smith PetscFunctionBegin; 1909566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 191cc9df77eSBarry Smith PetscFunctionReturn(0); 192cc9df77eSBarry Smith } 193cc9df77eSBarry Smith 194e5c89e4eSSatish Balay /* -------------------------------------------------------------------------------------------*/ 195e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_SOLARIS_STYLE_FPTRAP) 196e5c89e4eSSatish Balay #include <sunmath.h> 197e5c89e4eSSatish Balay #include <floatingpoint.h> 198e5c89e4eSSatish Balay #include <siginfo.h> 199e5c89e4eSSatish Balay #include <ucontext.h> 200e5c89e4eSSatish Balay 201db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 202e5c89e4eSSatish Balay { FPE_FLTINV,"invalid floating point operand"}, 203e5c89e4eSSatish Balay { FPE_FLTRES,"inexact floating point result"}, 204e5c89e4eSSatish Balay { FPE_FLTDIV,"division-by-zero"}, 205e5c89e4eSSatish Balay { FPE_FLTUND,"floating point underflow"}, 206e5c89e4eSSatish Balay { FPE_FLTOVF,"floating point overflow"}, 207e5c89e4eSSatish Balay { 0, "unknown error"} 208e5c89e4eSSatish Balay }; 209e5c89e4eSSatish Balay #define SIGPC(scp) (scp->si_addr) 210e5c89e4eSSatish Balay 211e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig,siginfo_t *scp,ucontext_t *uap) 212e5c89e4eSSatish Balay { 2135f80ce2aSJacob Faibussowitsch int err_ind = -1,code = scp->si_code; 214e5c89e4eSSatish Balay 215e5c89e4eSSatish Balay PetscFunctionBegin; 2165f80ce2aSJacob Faibussowitsch for (int j = 0; error_codes[j].code_no; j++) { 217e5c89e4eSSatish Balay if (error_codes[j].code_no == code) err_ind = j; 218e5c89e4eSSatish Balay } 219e5c89e4eSSatish Balay 220a297a907SKarl Rupp if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred at pc=%X ***\n",error_codes[err_ind].name,SIGPC(scp)); 221a297a907SKarl Rupp else (*PetscErrorPrintf)("*** floating point error 0x%x occurred at pc=%X ***\n",code,SIGPC(scp)); 222a297a907SKarl Rupp 2235f80ce2aSJacob Faibussowitsch (void)PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 22441e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 225e5c89e4eSSatish Balay } 226e5c89e4eSSatish Balay 227e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 228e5c89e4eSSatish Balay { 229e5c89e4eSSatish Balay char *out; 230e5c89e4eSSatish Balay 231e5c89e4eSSatish Balay PetscFunctionBegin; 232e5c89e4eSSatish Balay /* Clear accumulated exceptions. Used to suppress meaningless messages from f77 programs */ 233e5c89e4eSSatish Balay (void) ieee_flags("clear","exception","all",&out); 234e5c89e4eSSatish Balay if (flag == PETSC_FP_TRAP_ON) { 235a297a907SKarl Rupp if (ieee_handler("set","common",(sigfpe_handler_type)PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't set floating point handler\n"); 236cc9df77eSBarry Smith } else { 237cc9df77eSBarry Smith if (ieee_handler("clear","common",(sigfpe_handler_type)PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 238cc9df77eSBarry Smith } 239670f3ff9SJed Brown _trapmode = flag; 240e5c89e4eSSatish Balay PetscFunctionReturn(0); 241e5c89e4eSSatish Balay } 242e5c89e4eSSatish Balay 243aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 244cc9df77eSBarry Smith { 245cc9df77eSBarry Smith PetscFunctionBegin; 2469566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 247cc9df77eSBarry Smith PetscFunctionReturn(0); 248cc9df77eSBarry Smith } 249cc9df77eSBarry Smith 250cc9df77eSBarry Smith /* ------------------------------------------------------------------------------------------*/ 251e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_IRIX_STYLE_FPTRAP) 252e5c89e4eSSatish Balay #include <sigfpe.h> 253db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 254e5c89e4eSSatish Balay { _INVALID ,"IEEE operand error" }, 255e5c89e4eSSatish Balay { _OVERFL ,"floating point overflow" }, 256e5c89e4eSSatish Balay { _UNDERFL ,"floating point underflow" }, 257e5c89e4eSSatish Balay { _DIVZERO ,"floating point divide" }, 258e5c89e4eSSatish Balay { 0 ,"unknown error" } 259e5c89e4eSSatish Balay } ; 260e5c89e4eSSatish Balay void PetscDefaultFPTrap(unsigned exception[],int val[]) 261e5c89e4eSSatish Balay { 2625f80ce2aSJacob Faibussowitsch int err_ind = -1,code = exception[0]; 263e5c89e4eSSatish Balay 264e5c89e4eSSatish Balay PetscFunctionBegin; 2655f80ce2aSJacob Faibussowitsch for (int j = 0; error_codes[j].code_no; j++) { 266e5c89e4eSSatish Balay if (error_codes[j].code_no == code) err_ind = j; 267e5c89e4eSSatish Balay } 268a297a907SKarl Rupp if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred ***\n",error_codes[err_ind].name); 269a297a907SKarl Rupp else (*PetscErrorPrintf)("*** floating point error 0x%x occurred ***\n",code); 270a297a907SKarl Rupp 2715f80ce2aSJacob Faibussowitsch (void)PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 27241e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 273e5c89e4eSSatish Balay } 274e5c89e4eSSatish Balay 275e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 276e5c89e4eSSatish Balay { 277e5c89e4eSSatish Balay PetscFunctionBegin; 278cc9df77eSBarry Smith if (flag == PETSC_FP_TRAP_ON) handle_sigfpes(_ON,,_EN_UNDERFL|_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,PetscDefaultFPTrap,_ABORT_ON_ERROR,0); 279cc9df77eSBarry Smith else handle_sigfpes(_OFF,_EN_UNDERFL|_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,0,_ABORT_ON_ERROR,0); 280670f3ff9SJed Brown _trapmode = flag; 281e5c89e4eSSatish Balay PetscFunctionReturn(0); 282e5c89e4eSSatish Balay } 283cc9df77eSBarry Smith 284aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 285cc9df77eSBarry Smith { 286cc9df77eSBarry Smith PetscFunctionBegin; 2879566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 288cc9df77eSBarry Smith PetscFunctionReturn(0); 289cc9df77eSBarry Smith } 290cc9df77eSBarry Smith 291e5c89e4eSSatish Balay /* -------------------------------------------------------------------------------------------*/ 292cc9df77eSBarry Smith #elif defined(PETSC_HAVE_SOLARIS_STYLE_FPTRAP) 293cc9df77eSBarry Smith #include <sunmath.h> 294cc9df77eSBarry Smith #include <floatingpoint.h> 295cc9df77eSBarry Smith #include <siginfo.h> 296cc9df77eSBarry Smith #include <ucontext.h> 297cc9df77eSBarry Smith 298cc9df77eSBarry Smith static struct { int code_no; char *name; } error_codes[] = { 299cc9df77eSBarry Smith { FPE_FLTINV,"invalid floating point operand"}, 300cc9df77eSBarry Smith { FPE_FLTRES,"inexact floating point result"}, 301cc9df77eSBarry Smith { FPE_FLTDIV,"division-by-zero"}, 302cc9df77eSBarry Smith { FPE_FLTUND,"floating point underflow"}, 303cc9df77eSBarry Smith { FPE_FLTOVF,"floating point overflow"}, 304cc9df77eSBarry Smith { 0, "unknown error"} 305cc9df77eSBarry Smith }; 306cc9df77eSBarry Smith #define SIGPC(scp) (scp->si_addr) 307cc9df77eSBarry Smith 308cc9df77eSBarry Smith void PetscDefaultFPTrap(int sig,siginfo_t *scp,ucontext_t *uap) 309cc9df77eSBarry Smith { 3105f80ce2aSJacob Faibussowitsch int err_ind = -1,code = scp->si_code; 311cc9df77eSBarry Smith 312cc9df77eSBarry Smith PetscFunctionBegin; 313cc9df77eSBarry Smith err_ind = -1; 3145f80ce2aSJacob Faibussowitsch for (int j = 0; error_codes[j].code_no; j++) { 315cc9df77eSBarry Smith if (error_codes[j].code_no == code) err_ind = j; 316cc9df77eSBarry Smith } 317cc9df77eSBarry Smith 318cc9df77eSBarry Smith if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred at pc=%X ***\n",error_codes[err_ind].name,SIGPC(scp)); 319cc9df77eSBarry Smith else (*PetscErrorPrintf)("*** floating point error 0x%x occurred at pc=%X ***\n",code,SIGPC(scp)); 320cc9df77eSBarry Smith 3215f80ce2aSJacob Faibussowitsch (void)PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 322cc9df77eSBarry Smith PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 323cc9df77eSBarry Smith } 324cc9df77eSBarry Smith 325cc9df77eSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap flag) 326cc9df77eSBarry Smith { 327cc9df77eSBarry Smith char *out; 328cc9df77eSBarry Smith 329cc9df77eSBarry Smith PetscFunctionBegin; 330cc9df77eSBarry Smith /* Clear accumulated exceptions. Used to suppress meaningless messages from f77 programs */ 331cc9df77eSBarry Smith (void) ieee_flags("clear","exception","all",&out); 332cc9df77eSBarry Smith if (flag == PETSC_FP_TRAP_ON) { 333cc9df77eSBarry Smith if (ieee_handler("set","common",(sigfpe_handler_type)PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't set floating point handler\n"); 334cc9df77eSBarry Smith } else { 335cc9df77eSBarry Smith if (ieee_handler("clear","common",(sigfpe_handler_type)PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 336cc9df77eSBarry Smith } 337cc9df77eSBarry Smith _trapmode = flag; 338cc9df77eSBarry Smith PetscFunctionReturn(0); 339cc9df77eSBarry Smith } 340cc9df77eSBarry Smith 341aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 342cc9df77eSBarry Smith { 343cc9df77eSBarry Smith PetscFunctionBegin; 3449566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 345cc9df77eSBarry Smith PetscFunctionReturn(0); 346cc9df77eSBarry Smith } 347cc9df77eSBarry Smith 348cc9df77eSBarry Smith /*----------------------------------------------- --------------------------------------------*/ 349cc9df77eSBarry Smith #elif defined(PETSC_HAVE_RS6000_STYLE_FPTRAP) 350e5c89e4eSSatish Balay /* In "fast" mode, floating point traps are imprecise and ignored. 351e5c89e4eSSatish Balay This is the reason for the fptrap(FP_TRAP_SYNC) call */ 352e5c89e4eSSatish Balay struct sigcontext; 353e5c89e4eSSatish Balay #include <fpxcp.h> 354e5c89e4eSSatish Balay #include <fptrap.h> 355e5c89e4eSSatish Balay #define FPE_FLTOPERR_TRAP (fptrap_t)(0x20000000) 356e5c89e4eSSatish Balay #define FPE_FLTOVF_TRAP (fptrap_t)(0x10000000) 357e5c89e4eSSatish Balay #define FPE_FLTUND_TRAP (fptrap_t)(0x08000000) 358e5c89e4eSSatish Balay #define FPE_FLTDIV_TRAP (fptrap_t)(0x04000000) 359e5c89e4eSSatish Balay #define FPE_FLTINEX_TRAP (fptrap_t)(0x02000000) 360e5c89e4eSSatish Balay 361db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = { 362e5c89e4eSSatish Balay {FPE_FLTOPERR_TRAP ,"IEEE operand error" }, 363e5c89e4eSSatish Balay { FPE_FLTOVF_TRAP ,"floating point overflow" }, 364e5c89e4eSSatish Balay { FPE_FLTUND_TRAP ,"floating point underflow" }, 365e5c89e4eSSatish Balay { FPE_FLTDIV_TRAP ,"floating point divide" }, 366e5c89e4eSSatish Balay { FPE_FLTINEX_TRAP ,"inexact floating point result" }, 367e5c89e4eSSatish Balay { 0 ,"unknown error" } 368e5c89e4eSSatish Balay } ; 369e5c89e4eSSatish Balay #define SIGPC(scp) (0) /* Info MIGHT be in scp->sc_jmpbuf.jmp_context.iar */ 370e5c89e4eSSatish Balay /* 371e5c89e4eSSatish Balay For some reason, scp->sc_jmpbuf does not work on the RS6000, even though 372e5c89e4eSSatish Balay it looks like it should from the include definitions. It is probably 373e5c89e4eSSatish Balay some strange interaction with the "POSIX_SOURCE" that we require. 374e5c89e4eSSatish Balay */ 375e5c89e4eSSatish Balay 376e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig,int code,struct sigcontext *scp) 377e5c89e4eSSatish Balay { 378e5c89e4eSSatish Balay PetscErrorCode ierr; 379e5c89e4eSSatish Balay int err_ind,j; 380e5c89e4eSSatish Balay fp_ctx_t flt_context; 381e5c89e4eSSatish Balay 382e5c89e4eSSatish Balay PetscFunctionBegin; 383e5c89e4eSSatish Balay fp_sh_trap_info(scp,&flt_context); 384e5c89e4eSSatish Balay 385e5c89e4eSSatish Balay err_ind = -1; 386e5c89e4eSSatish Balay for (j = 0; error_codes[j].code_no; j++) { 387e5c89e4eSSatish Balay if (error_codes[j].code_no == flt_context.trap) err_ind = j; 388e5c89e4eSSatish Balay } 389e5c89e4eSSatish Balay 390a297a907SKarl Rupp if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred ***\n",error_codes[err_ind].name); 391a297a907SKarl Rupp else (*PetscErrorPrintf)("*** floating point error 0x%x occurred ***\n",flt_context.trap); 392a297a907SKarl Rupp 393efca3c55SSatish Balay ierr = PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 39441e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 395e5c89e4eSSatish Balay } 396e5c89e4eSSatish Balay 397e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap on) 398e5c89e4eSSatish Balay { 399e5c89e4eSSatish Balay PetscFunctionBegin; 400e5c89e4eSSatish Balay if (on == PETSC_FP_TRAP_ON) { 401e5c89e4eSSatish Balay signal(SIGFPE,(void (*)(int))PetscDefaultFPTrap); 402e5c89e4eSSatish Balay fp_trap(FP_TRAP_SYNC); 403cc9df77eSBarry Smith fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW | TRP_UNDERFLOW); 404e5c89e4eSSatish Balay /* fp_enable(mask) for individual traps. Values are: 405e5c89e4eSSatish Balay TRP_INVALID 406e5c89e4eSSatish Balay TRP_DIV_BY_ZERO 407e5c89e4eSSatish Balay TRP_OVERFLOW 408e5c89e4eSSatish Balay TRP_UNDERFLOW 409e5c89e4eSSatish Balay TRP_INEXACT 410e5c89e4eSSatish Balay Can OR then together. 411e5c89e4eSSatish Balay fp_enable_all(); for all traps. 412e5c89e4eSSatish Balay */ 413e5c89e4eSSatish Balay } else { 414e5c89e4eSSatish Balay signal(SIGFPE,SIG_DFL); 415cc9df77eSBarry Smith fp_disable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW | TRP_UNDERFLOW); 416e5c89e4eSSatish Balay fp_trap(FP_TRAP_OFF); 417e5c89e4eSSatish Balay } 418670f3ff9SJed Brown _trapmode = on; 419e5c89e4eSSatish Balay PetscFunctionReturn(0); 420e5c89e4eSSatish Balay } 421e5c89e4eSSatish Balay 422aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 423cc9df77eSBarry Smith { 424cc9df77eSBarry Smith PetscFunctionBegin; 4259566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 426cc9df77eSBarry Smith PetscFunctionReturn(0); 427cc9df77eSBarry Smith } 428cc9df77eSBarry Smith 429cc9df77eSBarry Smith /* ------------------------------------------------------------*/ 430cc9df77eSBarry Smith #elif defined(PETSC_HAVE_WINDOWS_COMPILERS) 431cc9df77eSBarry Smith #include <float.h> 432cc9df77eSBarry Smith void PetscDefaultFPTrap(int sig) 433cc9df77eSBarry Smith { 434cc9df77eSBarry Smith PetscFunctionBegin; 435cc9df77eSBarry Smith (*PetscErrorPrintf)("*** floating point error occurred ***\n"); 436cc9df77eSBarry Smith PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 437cc9df77eSBarry Smith PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 438cc9df77eSBarry Smith } 439cc9df77eSBarry Smith 440cc9df77eSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap on) 441cc9df77eSBarry Smith { 442cc9df77eSBarry Smith unsigned int cw; 443cc9df77eSBarry Smith 444cc9df77eSBarry Smith PetscFunctionBegin; 445cc9df77eSBarry Smith if (on == PETSC_FP_TRAP_ON) { 446cc9df77eSBarry Smith cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW; 447*08401ef6SPierre Jolivet PetscCheck(SIG_ERR != signal(SIGFPE,PetscDefaultFPTrap),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't set floating point handler"); 448cc9df77eSBarry Smith } else { 449cc9df77eSBarry Smith cw = 0; 450*08401ef6SPierre Jolivet PetscCheck(SIG_ERR != signal(SIGFPE,SIG_DFL),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't clear floating point handler"); 451cc9df77eSBarry Smith } 452cc9df77eSBarry Smith (void)_controlfp(0, cw); 453cc9df77eSBarry Smith _trapmode = on; 454cc9df77eSBarry Smith PetscFunctionReturn(0); 455cc9df77eSBarry Smith } 456cc9df77eSBarry Smith 457aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 458cc9df77eSBarry Smith { 459cc9df77eSBarry Smith PetscFunctionBegin; 4609566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 461cc9df77eSBarry Smith PetscFunctionReturn(0); 462cc9df77eSBarry Smith } 463cc9df77eSBarry Smith 464cc9df77eSBarry Smith /* ------------------------------------------------------------*/ 4659a2402e9SBarry Smith #elif defined(PETSC_HAVE_FENV_H) && !defined(__cplusplus) 466b014e56cSJed Brown /* 467b014e56cSJed Brown C99 style floating point environment. 468b014e56cSJed Brown 469b014e56cSJed Brown Note that C99 merely specifies how to save, restore, and clear the floating 470b014e56cSJed Brown point environment as well as defining an enumeration of exception codes. In 471b014e56cSJed Brown particular, C99 does not specify how to make floating point exceptions raise 472b014e56cSJed Brown a signal. Glibc offers this capability through FE_NOMASK_ENV (or with finer 473b014e56cSJed Brown granularity, feenableexcept()), xmmintrin.h offers _MM_SET_EXCEPTION_MASK(). 474b014e56cSJed Brown */ 475b014e56cSJed Brown #include <fenv.h> 476b014e56cSJed Brown typedef struct {int code; const char *name;} FPNode; 477b014e56cSJed Brown static const FPNode error_codes[] = { 478b014e56cSJed Brown {FE_DIVBYZERO,"divide by zero"}, 479b014e56cSJed Brown {FE_INEXACT, "inexact floating point result"}, 480b014e56cSJed Brown {FE_INVALID, "invalid floating point arguments (domain error)"}, 481b014e56cSJed Brown {FE_OVERFLOW, "floating point overflow"}, 482b014e56cSJed Brown {FE_UNDERFLOW,"floating point underflow"}, 483b014e56cSJed Brown {0 ,"unknown error"} 484b014e56cSJed Brown }; 48599e0435eSBarry Smith 486b014e56cSJed Brown void PetscDefaultFPTrap(int sig) 487b014e56cSJed Brown { 488b014e56cSJed Brown const FPNode *node; 489b014e56cSJed Brown int code; 490ace3abfcSBarry Smith PetscBool matched = PETSC_FALSE; 491b014e56cSJed Brown 492b014e56cSJed Brown PetscFunctionBegin; 493b014e56cSJed Brown /* Note: While it is possible for the exception state to be preserved by the 494b014e56cSJed Brown * kernel, this seems to be rare which makes the following flag testing almost 495b014e56cSJed Brown * useless. But on a system where the flags can be preserved, it would provide 4967d125cddSJed Brown * more detail. 497b014e56cSJed Brown */ 498b014e56cSJed Brown code = fetestexcept(FE_ALL_EXCEPT); 499b014e56cSJed Brown for (node=&error_codes[0]; node->code; node++) { 500b014e56cSJed Brown if (code & node->code) { 501b014e56cSJed Brown matched = PETSC_TRUE; 502b014e56cSJed Brown (*PetscErrorPrintf)("*** floating point error \"%s\" occurred ***\n",node->name); 503b014e56cSJed Brown code &= ~node->code; /* Unset this flag since it has been processed */ 504b014e56cSJed Brown } 505b014e56cSJed Brown } 506b014e56cSJed Brown if (!matched || code) { /* If any remaining flags are set, or we didn't process any flags */ 507b014e56cSJed Brown (*PetscErrorPrintf)("*** unknown floating point error occurred ***\n"); 5087d125cddSJed Brown (*PetscErrorPrintf)("The specific exception can be determined by running in a debugger. When the\n"); 5097d125cddSJed Brown (*PetscErrorPrintf)("debugger traps the signal, the exception can be found with fetestexcept(0x%x)\n",FE_ALL_EXCEPT); 5107d125cddSJed Brown (*PetscErrorPrintf)("where the result is a bitwise OR of the following flags:\n"); 5117d125cddSJed 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); 512b014e56cSJed Brown } 5137d125cddSJed Brown 5147d125cddSJed Brown (*PetscErrorPrintf)("Try option -start_in_debugger\n"); 51527104ee2SJacob Faibussowitsch #if PetscDefined(USE_DEBUG) 5167d125cddSJed Brown (*PetscErrorPrintf)("likely location of problem given in stack below\n"); 5177d125cddSJed Brown (*PetscErrorPrintf)("--------------------- Stack Frames ------------------------------------\n"); 518639ff905SBarry Smith PetscStackView(PETSC_STDOUT); 51927104ee2SJacob Faibussowitsch #else 5207d125cddSJed Brown (*PetscErrorPrintf)("configure using --with-debugging=yes, recompile, link, and run \n"); 5217d125cddSJed Brown (*PetscErrorPrintf)("with -start_in_debugger to get more information on the crash.\n"); 52227104ee2SJacob Faibussowitsch #endif 523efca3c55SSatish Balay PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_INITIAL,"trapped floating point error"); 52441e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 525b014e56cSJed Brown } 526b014e56cSJed Brown 5277087cfbeSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap on) 528b014e56cSJed Brown { 529b014e56cSJed Brown PetscFunctionBegin; 530b014e56cSJed Brown if (on == PETSC_FP_TRAP_ON) { 531b014e56cSJed Brown /* Clear any flags that are currently set so that activating trapping will not immediately call the signal handler. */ 5322c71b3e2SJacob Faibussowitsch PetscCheckFalse(feclearexcept(FE_ALL_EXCEPT),PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot clear floating point exception flags"); 533cc9df77eSBarry Smith #if defined(FE_NOMASK_ENV) 534cc9df77eSBarry Smith /* Could use fesetenv(FE_NOMASK_ENV), but that causes spurious exceptions (like gettimeofday() -> PetscLogDouble). */ 535*08401ef6SPierre Jolivet PetscCheck(feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW) != -1,PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot activate floating point exceptions"); 536b014e56cSJed Brown #elif defined PETSC_HAVE_XMMINTRIN_H 537cc9df77eSBarry Smith _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_DIV_ZERO); 538cc9df77eSBarry Smith _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_UNDERFLOW); 539cc9df77eSBarry Smith _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_OVERFLOW); 540cc9df77eSBarry Smith _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_INVALID); 541b014e56cSJed Brown #else 542b014e56cSJed Brown /* C99 does not provide a way to modify the environment so there is no portable way to activate trapping. */ 543b014e56cSJed Brown #endif 544*08401ef6SPierre Jolivet PetscCheck(SIG_ERR != signal(SIGFPE,PetscDefaultFPTrap),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't set floating point handler"); 545b014e56cSJed Brown } else { 5462c71b3e2SJacob Faibussowitsch PetscCheckFalse(fesetenv(FE_DFL_ENV),PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot disable floating point exceptions"); 547cc9df77eSBarry Smith /* can use _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() | _MM_MASK_UNDERFLOW); if PETSC_HAVE_XMMINTRIN_H exists */ 548*08401ef6SPierre Jolivet PetscCheck(SIG_ERR != signal(SIGFPE,SIG_DFL),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't clear floating point handler"); 549b014e56cSJed Brown } 550670f3ff9SJed Brown _trapmode = on; 551b014e56cSJed Brown PetscFunctionReturn(0); 552b014e56cSJed Brown } 553b014e56cSJed Brown 554aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 555cc9df77eSBarry Smith { 556cc9df77eSBarry Smith #if defined(FE_NOMASK_ENV) || defined PETSC_HAVE_XMMINTRIN_H 557cc9df77eSBarry Smith unsigned int flags; 558cc9df77eSBarry Smith #endif 559cc9df77eSBarry Smith 560cc9df77eSBarry Smith PetscFunctionBegin; 561cc9df77eSBarry Smith #if defined(FE_NOMASK_ENV) 562cc9df77eSBarry Smith flags = fegetexcept(); 563cc9df77eSBarry Smith if (flags & FE_DIVBYZERO) { 564cc9df77eSBarry Smith #elif defined PETSC_HAVE_XMMINTRIN_H 565cc9df77eSBarry Smith flags = _MM_GET_EXCEPTION_MASK(); 566cc9df77eSBarry Smith if (!(flags & _MM_MASK_DIV_ZERO)) { 567cc9df77eSBarry Smith #else 5689566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Floating point trapping unknown, assuming off\n")); 569cc9df77eSBarry Smith PetscFunctionReturn(0); 570cc9df77eSBarry Smith #endif 571cc9df77eSBarry Smith #if defined(FE_NOMASK_ENV) || defined PETSC_HAVE_XMMINTRIN_H 572cc9df77eSBarry Smith _trapmode = PETSC_FP_TRAP_ON; 5739566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Floating point trapping is on by default %d\n",flags)); 574cc9df77eSBarry Smith } else { 575cc9df77eSBarry Smith _trapmode = PETSC_FP_TRAP_OFF; 5769566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Floating point trapping is off by default %d\n",flags)); 577cc9df77eSBarry Smith } 578cc9df77eSBarry Smith PetscFunctionReturn(0); 579cc9df77eSBarry Smith #endif 580cc9df77eSBarry Smith } 581cc9df77eSBarry Smith 582cc9df77eSBarry Smith /* ------------------------------------------------------------*/ 583cc9df77eSBarry Smith #elif defined(PETSC_HAVE_IEEEFP_H) 584cc9df77eSBarry Smith #include <ieeefp.h> 585cc9df77eSBarry Smith void PetscDefaultFPTrap(int sig) 586cc9df77eSBarry Smith { 587cc9df77eSBarry Smith PetscFunctionBegin; 588cc9df77eSBarry Smith (*PetscErrorPrintf)("*** floating point error occurred ***\n"); 589cc9df77eSBarry Smith PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 590cc9df77eSBarry Smith PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 591cc9df77eSBarry Smith } 592cc9df77eSBarry Smith 593cc9df77eSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap on) 594cc9df77eSBarry Smith { 595cc9df77eSBarry Smith PetscFunctionBegin; 596cc9df77eSBarry Smith if (on == PETSC_FP_TRAP_ON) { 597cc9df77eSBarry Smith #if defined(PETSC_HAVE_FPPRESETSTICKY) 598cc9df77eSBarry Smith fpresetsticky(fpgetsticky()); 599cc9df77eSBarry Smith #elif defined(PETSC_HAVE_FPSETSTICKY) 600cc9df77eSBarry Smith fpsetsticky(fpgetsticky()); 601cc9df77eSBarry Smith #endif 602cc9df77eSBarry Smith fpsetmask(FP_X_INV | FP_X_DZ | FP_X_OFL | FP_X_OFL); 603*08401ef6SPierre Jolivet PetscCheck(SIG_ERR != signal(SIGFPE,PetscDefaultFPTrap),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't set floating point handler"); 604cc9df77eSBarry Smith } else { 605cc9df77eSBarry Smith #if defined(PETSC_HAVE_FPPRESETSTICKY) 606cc9df77eSBarry Smith fpresetsticky(fpgetsticky()); 607cc9df77eSBarry Smith #elif defined(PETSC_HAVE_FPSETSTICKY) 608cc9df77eSBarry Smith fpsetsticky(fpgetsticky()); 609cc9df77eSBarry Smith #endif 610cc9df77eSBarry Smith fpsetmask(0); 611*08401ef6SPierre Jolivet PetscCheck(SIG_ERR != signal(SIGFPE,SIG_DFL),PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't clear floating point handler"); 612cc9df77eSBarry Smith } 613cc9df77eSBarry Smith _trapmode = on; 614cc9df77eSBarry Smith PetscFunctionReturn(0); 615cc9df77eSBarry Smith } 616cc9df77eSBarry Smith 617aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 618cc9df77eSBarry Smith { 619cc9df77eSBarry Smith PetscFunctionBegin; 6209566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 621cc9df77eSBarry Smith PetscFunctionReturn(0); 622cc9df77eSBarry Smith } 623cc9df77eSBarry Smith 624e5c89e4eSSatish Balay /* -------------------------Default -----------------------------------*/ 625e5c89e4eSSatish Balay #else 62699e0435eSBarry Smith 627e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig) 628e5c89e4eSSatish Balay { 629e5c89e4eSSatish Balay PetscFunctionBegin; 630e5c89e4eSSatish Balay (*PetscErrorPrintf)("*** floating point error occurred ***\n"); 631efca3c55SSatish Balay PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error"); 63241e02c4dSJunchao Zhang PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP); 633e5c89e4eSSatish Balay } 63499e0435eSBarry Smith 6357087cfbeSBarry Smith PetscErrorCode PetscSetFPTrap(PetscFPTrap on) 636e5c89e4eSSatish Balay { 637e5c89e4eSSatish Balay PetscFunctionBegin; 638e5c89e4eSSatish Balay if (on == PETSC_FP_TRAP_ON) { 639a297a907SKarl Rupp if (SIG_ERR == signal(SIGFPE,PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't set floatingpoint handler\n"); 640a297a907SKarl Rupp } else if (SIG_ERR == signal(SIGFPE,SIG_DFL)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n"); 641a297a907SKarl Rupp 642670f3ff9SJed Brown _trapmode = on; 643e5c89e4eSSatish Balay PetscFunctionReturn(0); 644e5c89e4eSSatish Balay } 645cc9df77eSBarry Smith 646aba4c478SBarry Smith PetscErrorCode PetscDetermineInitialFPTrap(void) 647cc9df77eSBarry Smith { 648cc9df77eSBarry Smith PetscFunctionBegin; 6499566063dSJacob Faibussowitsch PetscCall(PetscInfo(NULL,"Unable to determine initial floating point trapping. Assuming it is off\n")); 650cc9df77eSBarry Smith PetscFunctionReturn(0); 651cc9df77eSBarry Smith } 652e5c89e4eSSatish Balay #endif 653