xref: /petsc/src/sys/error/fp.c (revision 41e02c4d8c59de663c4988f33479b3e4bb91dfe1)
17d0a6c19SBarry Smith 
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay *   IEEE error handler for all machines. Since each machine has
4e5c89e4eSSatish Balay *   enough slight differences we have completely separate codes for each one.
5e5c89e4eSSatish Balay *
6e5c89e4eSSatish Balay */
7b014e56cSJed Brown 
8b014e56cSJed Brown /*
9b014e56cSJed Brown   This feature test macro provides FE_NOMASK_ENV on GNU.  It must be defined
10b014e56cSJed Brown   at the top of the file because other headers may pull in fenv.h even when
11b014e56cSJed Brown   not strictly necessary.  Strictly speaking, we could include ONLY petscconf.h,
12b014e56cSJed Brown   check PETSC_HAVE_FENV_H, and only define _GNU_SOURCE in that case, but such
13b014e56cSJed Brown   shenanigans ought to be unnecessary.
14b014e56cSJed Brown */
15519f805aSKarl Rupp #if !defined(_GNU_SOURCE)
16b014e56cSJed Brown #define _GNU_SOURCE
1776a6984eSJed Brown #endif
18b014e56cSJed Brown 
19c6db04a5SJed Brown #include <petscsys.h>           /*I  "petscsys.h"  I*/
20e5c89e4eSSatish Balay #include <signal.h>
21e5c89e4eSSatish Balay 
22670f3ff9SJed Brown struct PetscFPTrapLink {
23670f3ff9SJed Brown   PetscFPTrap            trapmode;
24670f3ff9SJed Brown   struct PetscFPTrapLink *next;
25670f3ff9SJed Brown };
26670f3ff9SJed Brown static PetscFPTrap            _trapmode = PETSC_FP_TRAP_OFF; /* Current trapping mode */
27670f3ff9SJed Brown static struct PetscFPTrapLink *_trapstack;                   /* Any pushed states of _trapmode */
28670f3ff9SJed Brown 
29670f3ff9SJed Brown /*@
30670f3ff9SJed Brown    PetscFPTrapPush - push a floating point trapping mode, to be restored using PetscFPTrapPop()
31670f3ff9SJed Brown 
32670f3ff9SJed Brown    Not Collective
33670f3ff9SJed Brown 
34670f3ff9SJed Brown    Input Arguments:
35670f3ff9SJed Brown . trap - PETSC_FP_TRAP_ON or PETSC_FP_TRAP_OFF
36670f3ff9SJed Brown 
37670f3ff9SJed Brown    Level: advanced
38670f3ff9SJed Brown 
39670f3ff9SJed Brown .seealso: PetscFPTrapPop(), PetscSetFPTrap()
40670f3ff9SJed Brown @*/
41670f3ff9SJed Brown PetscErrorCode PetscFPTrapPush(PetscFPTrap trap)
42670f3ff9SJed Brown {
43670f3ff9SJed Brown   PetscErrorCode         ierr;
44670f3ff9SJed Brown   struct PetscFPTrapLink *link;
45670f3ff9SJed Brown 
46670f3ff9SJed Brown   PetscFunctionBegin;
47854ce69bSBarry Smith   ierr           = PetscNew(&link);CHKERRQ(ierr);
48670f3ff9SJed Brown   link->trapmode = _trapmode;
49670f3ff9SJed Brown   link->next     = _trapstack;
50670f3ff9SJed Brown   _trapstack     = link;
51f6883b22SJed Brown   if (trap != _trapmode) {ierr = PetscSetFPTrap(trap);CHKERRQ(ierr);}
52670f3ff9SJed Brown   PetscFunctionReturn(0);
53670f3ff9SJed Brown }
54670f3ff9SJed Brown 
55670f3ff9SJed Brown /*@
56670f3ff9SJed Brown    PetscFPTrapPop - push a floating point trapping mode, to be restored using PetscFPTrapPop()
57670f3ff9SJed Brown 
58670f3ff9SJed Brown    Not Collective
59670f3ff9SJed Brown 
60670f3ff9SJed Brown    Level: advanced
61670f3ff9SJed Brown 
62670f3ff9SJed Brown .seealso: PetscFPTrapPush(), PetscSetFPTrap()
63670f3ff9SJed Brown @*/
64670f3ff9SJed Brown PetscErrorCode PetscFPTrapPop(void)
65670f3ff9SJed Brown {
66670f3ff9SJed Brown   PetscErrorCode         ierr;
67670f3ff9SJed Brown   struct PetscFPTrapLink *link;
68670f3ff9SJed Brown 
69670f3ff9SJed Brown   PetscFunctionBegin;
70f6883b22SJed Brown   if (_trapstack->trapmode != _trapmode) {ierr = PetscSetFPTrap(_trapstack->trapmode);CHKERRQ(ierr);}
71670f3ff9SJed Brown   link       = _trapstack;
72670f3ff9SJed Brown   _trapstack = _trapstack->next;
73670f3ff9SJed Brown   ierr       = PetscFree(link);CHKERRQ(ierr);
74670f3ff9SJed Brown   PetscFunctionReturn(0);
75670f3ff9SJed Brown }
76670f3ff9SJed Brown 
77e5c89e4eSSatish Balay /*--------------------------------------- ---------------------------------------------------*/
78e5c89e4eSSatish Balay #if defined(PETSC_HAVE_SUN4_STYLE_FPTRAP)
79e5c89e4eSSatish Balay #include <floatingpoint.h>
80e5c89e4eSSatish Balay 
818cc058d9SJed Brown PETSC_EXTERN PetscErrorCode ieee_flags(char*,char*,char*,char**);
828cc058d9SJed Brown PETSC_EXTERN PetscErrorCode ieee_handler(char*,char*,sigfpe_handler_type(int,int,struct sigcontext*,char*));
83e5c89e4eSSatish Balay 
84db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = {
85e5c89e4eSSatish Balay   { FPE_INTDIV_TRAP    ,"integer divide" },
86e5c89e4eSSatish Balay   { FPE_FLTOPERR_TRAP  ,"IEEE operand error" },
87e5c89e4eSSatish Balay   { FPE_FLTOVF_TRAP    ,"floating point overflow" },
88e5c89e4eSSatish Balay   { FPE_FLTUND_TRAP    ,"floating point underflow" },
89e5c89e4eSSatish Balay   { FPE_FLTDIV_TRAP    ,"floating pointing divide" },
90e5c89e4eSSatish Balay   { FPE_FLTINEX_TRAP   ,"inexact floating point result" },
91e5c89e4eSSatish Balay   { 0                  ,"unknown error" }
92e5c89e4eSSatish Balay };
93e5c89e4eSSatish Balay #define SIGPC(scp) (scp->sc_pc)
94e5c89e4eSSatish Balay 
95e5c89e4eSSatish Balay sigfpe_handler_type PetscDefaultFPTrap(int sig,int code,struct sigcontext *scp,char *addr)
96e5c89e4eSSatish Balay {
97e5c89e4eSSatish Balay   PetscErrorCode ierr;
98e5c89e4eSSatish Balay   int            err_ind = -1,j;
99e5c89e4eSSatish Balay 
100e5c89e4eSSatish Balay   PetscFunctionBegin;
101e5c89e4eSSatish Balay   for (j = 0; error_codes[j].code_no; j++) {
102e5c89e4eSSatish Balay     if (error_codes[j].code_no == code) err_ind = j;
103e5c89e4eSSatish Balay   }
104e5c89e4eSSatish Balay 
105a297a907SKarl Rupp   if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred at pc=%X ***\n",error_codes[err_ind].name,SIGPC(scp));
106a297a907SKarl Rupp   else              (*PetscErrorPrintf)("*** floating point error 0x%x occurred at pc=%X ***\n",code,SIGPC(scp));
107a297a907SKarl Rupp 
108efca3c55SSatish Balay   ierr = PetscError(PETSC_COMM_SELF,PETSC_ERR_FP,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error");
109*41e02c4dSJunchao Zhang   PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP);
110e5c89e4eSSatish Balay   PetscFunctionReturn(0);
111e5c89e4eSSatish Balay }
112e5c89e4eSSatish Balay 
113e30d2299SSatish Balay /*@
114e5c89e4eSSatish Balay    PetscSetFPTrap - Enables traps/exceptions on common floating point errors.
115e5c89e4eSSatish Balay                     This option may not work on certain machines.
116e5c89e4eSSatish Balay 
117e5c89e4eSSatish Balay    Not Collective
118e5c89e4eSSatish Balay 
119e5c89e4eSSatish Balay    Input Parameters:
120e5c89e4eSSatish Balay .  flag - PETSC_FP_TRAP_ON, PETSC_FP_TRAP_OFF.
121e5c89e4eSSatish Balay 
122e5c89e4eSSatish Balay    Options Database Keys:
123e5c89e4eSSatish Balay .  -fp_trap - Activates floating point trapping
124e5c89e4eSSatish Balay 
125e5c89e4eSSatish Balay    Level: advanced
126e5c89e4eSSatish Balay 
127e5c89e4eSSatish Balay    Description:
128e5c89e4eSSatish Balay    On systems that support it, this routine causes floating point
129e5c89e4eSSatish Balay    overflow, divide-by-zero, and invalid-operand (e.g., a NaN) to
130e5c89e4eSSatish Balay    cause a message to be printed and the program to exit.
131e5c89e4eSSatish Balay 
1327d125cddSJed Brown    Note:
1337d125cddSJed Brown    On many common systems including x86 and x86-64 Linux, the floating
1347d125cddSJed Brown    point exception state is not preserved from the location where the trap
1357d125cddSJed Brown    occurred through to the signal handler.  In this case, the signal handler
1367d125cddSJed Brown    will just say that an unknown floating point exception occurred and which
1377d125cddSJed Brown    function it occurred in.  If you run with -fp_trap in a debugger, it will
1387d125cddSJed Brown    break on the line where the error occurred.  You can check which
1397d125cddSJed Brown    exception occurred using fetestexcept(FE_ALL_EXCEPT).  See fenv.h
1407d125cddSJed Brown    (usually at /usr/include/bits/fenv.h) for the enum values on your system.
1417d125cddSJed Brown 
142e5c89e4eSSatish Balay    Caution:
143e5c89e4eSSatish Balay    On certain machines, in particular the IBM rs6000, floating point
144e5c89e4eSSatish Balay    trapping is VERY slow!
145e5c89e4eSSatish Balay 
146e5c89e4eSSatish Balay 
147670f3ff9SJed Brown .seealso: PetscFPTrapPush(), PetscFPTrapPop()
148e5c89e4eSSatish Balay @*/
149e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag)
150e5c89e4eSSatish Balay {
151e5c89e4eSSatish Balay   char *out;
152e5c89e4eSSatish Balay 
153e5c89e4eSSatish Balay   PetscFunctionBegin;
154e5c89e4eSSatish Balay   /* Clear accumulated exceptions.  Used to suppress meaningless messages from f77 programs */
155e5c89e4eSSatish Balay   (void) ieee_flags("clear","exception","all",&out);
156e5c89e4eSSatish Balay   if (flag == PETSC_FP_TRAP_ON) {
157e5c89e4eSSatish Balay     /*
158a297a907SKarl Rupp       To trap more fp exceptions, including underflow, change the line below to
159e5c89e4eSSatish Balay       if (ieee_handler("set","all",PetscDefaultFPTrap)) {
160e5c89e4eSSatish Balay     */
161a297a907SKarl Rupp     if (ieee_handler("set","common",PetscDefaultFPTrap))        (*PetscErrorPrintf)("Can't set floatingpoint handler\n");
162a297a907SKarl Rupp   } else if (ieee_handler("clear","common",PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n");
163a297a907SKarl Rupp 
164670f3ff9SJed Brown   _trapmode = flag;
165e5c89e4eSSatish Balay   PetscFunctionReturn(0);
166e5c89e4eSSatish Balay }
167e5c89e4eSSatish Balay 
168e5c89e4eSSatish Balay /* -------------------------------------------------------------------------------------------*/
169e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_SOLARIS_STYLE_FPTRAP)
170e5c89e4eSSatish Balay #include <sunmath.h>
171e5c89e4eSSatish Balay #include <floatingpoint.h>
172e5c89e4eSSatish Balay #include <siginfo.h>
173e5c89e4eSSatish Balay #include <ucontext.h>
174e5c89e4eSSatish Balay 
175db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = {
176e5c89e4eSSatish Balay   { FPE_FLTINV,"invalid floating point operand"},
177e5c89e4eSSatish Balay   { FPE_FLTRES,"inexact floating point result"},
178e5c89e4eSSatish Balay   { FPE_FLTDIV,"division-by-zero"},
179e5c89e4eSSatish Balay   { FPE_FLTUND,"floating point underflow"},
180e5c89e4eSSatish Balay   { FPE_FLTOVF,"floating point overflow"},
181e5c89e4eSSatish Balay   { 0,         "unknown error"}
182e5c89e4eSSatish Balay };
183e5c89e4eSSatish Balay #define SIGPC(scp) (scp->si_addr)
184e5c89e4eSSatish Balay 
185e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig,siginfo_t *scp,ucontext_t *uap)
186e5c89e4eSSatish Balay {
187e5c89e4eSSatish Balay   int            err_ind,j,code = scp->si_code;
188e5c89e4eSSatish Balay   PetscErrorCode ierr;
189e5c89e4eSSatish Balay 
190e5c89e4eSSatish Balay   PetscFunctionBegin;
191e5c89e4eSSatish Balay   err_ind = -1;
192e5c89e4eSSatish Balay   for (j = 0; error_codes[j].code_no; j++) {
193e5c89e4eSSatish Balay     if (error_codes[j].code_no == code) err_ind = j;
194e5c89e4eSSatish Balay   }
195e5c89e4eSSatish Balay 
196a297a907SKarl Rupp   if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred at pc=%X ***\n",error_codes[err_ind].name,SIGPC(scp));
197a297a907SKarl Rupp   else              (*PetscErrorPrintf)("*** floating point error 0x%x occurred at pc=%X ***\n",code,SIGPC(scp));
198a297a907SKarl Rupp 
199efca3c55SSatish Balay   ierr = PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error");
200*41e02c4dSJunchao Zhang   PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP);
201e5c89e4eSSatish Balay }
202e5c89e4eSSatish Balay 
203e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag)
204e5c89e4eSSatish Balay {
205e5c89e4eSSatish Balay   char *out;
206e5c89e4eSSatish Balay 
207e5c89e4eSSatish Balay   PetscFunctionBegin;
208e5c89e4eSSatish Balay   /* Clear accumulated exceptions.  Used to suppress meaningless messages from f77 programs */
209e5c89e4eSSatish Balay   (void) ieee_flags("clear","exception","all",&out);
210e5c89e4eSSatish Balay   if (flag == PETSC_FP_TRAP_ON) {
211a297a907SKarl Rupp     if (ieee_handler("set","common",(sigfpe_handler_type)PetscDefaultFPTrap))        (*PetscErrorPrintf)("Can't set floating point handler\n");
212a297a907SKarl Rupp   } else if (ieee_handler("clear","common",(sigfpe_handler_type)PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't clear floatingpoint handler\n");
213670f3ff9SJed Brown   _trapmode = flag;
214e5c89e4eSSatish Balay   PetscFunctionReturn(0);
215e5c89e4eSSatish Balay }
216e5c89e4eSSatish Balay 
217e5c89e4eSSatish Balay /* ------------------------------------------------------------------------------------------*/
218e5c89e4eSSatish Balay 
219e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_IRIX_STYLE_FPTRAP)
220e5c89e4eSSatish Balay #include <sigfpe.h>
221db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = {
222e5c89e4eSSatish Balay   { _INVALID   ,"IEEE operand error" },
223e5c89e4eSSatish Balay   { _OVERFL    ,"floating point overflow" },
224e5c89e4eSSatish Balay   { _UNDERFL   ,"floating point underflow" },
225e5c89e4eSSatish Balay   { _DIVZERO   ,"floating point divide" },
226e5c89e4eSSatish Balay   { 0          ,"unknown error" }
227e5c89e4eSSatish Balay } ;
228e5c89e4eSSatish Balay void PetscDefaultFPTrap(unsigned exception[],int val[])
229e5c89e4eSSatish Balay {
230e5c89e4eSSatish Balay   int err_ind,j,code;
231e5c89e4eSSatish Balay 
232e5c89e4eSSatish Balay   PetscFunctionBegin;
233e5c89e4eSSatish Balay   code    = exception[0];
234e5c89e4eSSatish Balay   err_ind = -1;
235e5c89e4eSSatish Balay   for (j = 0; error_codes[j].code_no; j++) {
236e5c89e4eSSatish Balay     if (error_codes[j].code_no == code) err_ind = j;
237e5c89e4eSSatish Balay   }
238a297a907SKarl Rupp   if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred ***\n",error_codes[err_ind].name);
239a297a907SKarl Rupp   else              (*PetscErrorPrintf)("*** floating point error 0x%x occurred ***\n",code);
240a297a907SKarl Rupp 
241efca3c55SSatish Balay   PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error");
242*41e02c4dSJunchao Zhang   PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP);
243e5c89e4eSSatish Balay }
244e5c89e4eSSatish Balay 
245e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap flag)
246e5c89e4eSSatish Balay {
247e5c89e4eSSatish Balay   PetscFunctionBegin;
248a297a907SKarl Rupp   if (flag == PETSC_FP_TRAP_ON) handle_sigfpes(_ON,_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,PetscDefaultFPTrap,_ABORT_ON_ERROR,0);
249a297a907SKarl Rupp   else                          handle_sigfpes(_OFF,_EN_OVERFL|_EN_DIVZERO|_EN_INVALID,0,_ABORT_ON_ERROR,0);
250a297a907SKarl Rupp 
251670f3ff9SJed Brown   _trapmode = flag;
252e5c89e4eSSatish Balay   PetscFunctionReturn(0);
253e5c89e4eSSatish Balay }
254e5c89e4eSSatish Balay /*----------------------------------------------- --------------------------------------------*/
255e5c89e4eSSatish Balay /* In "fast" mode, floating point traps are imprecise and ignored.
256e5c89e4eSSatish Balay    This is the reason for the fptrap(FP_TRAP_SYNC) call */
257e5c89e4eSSatish Balay #elif defined(PETSC_HAVE_RS6000_STYLE_FPTRAP)
258e5c89e4eSSatish Balay struct sigcontext;
259e5c89e4eSSatish Balay #include <fpxcp.h>
260e5c89e4eSSatish Balay #include <fptrap.h>
261e5c89e4eSSatish Balay #define FPE_FLTOPERR_TRAP (fptrap_t)(0x20000000)
262e5c89e4eSSatish Balay #define FPE_FLTOVF_TRAP   (fptrap_t)(0x10000000)
263e5c89e4eSSatish Balay #define FPE_FLTUND_TRAP   (fptrap_t)(0x08000000)
264e5c89e4eSSatish Balay #define FPE_FLTDIV_TRAP   (fptrap_t)(0x04000000)
265e5c89e4eSSatish Balay #define FPE_FLTINEX_TRAP  (fptrap_t)(0x02000000)
266e5c89e4eSSatish Balay 
267db32a245SJed Brown static struct { int code_no; char *name; } error_codes[] = {
268e5c89e4eSSatish Balay   {FPE_FLTOPERR_TRAP   ,"IEEE operand error" },
269e5c89e4eSSatish Balay   { FPE_FLTOVF_TRAP    ,"floating point overflow" },
270e5c89e4eSSatish Balay   { FPE_FLTUND_TRAP    ,"floating point underflow" },
271e5c89e4eSSatish Balay   { FPE_FLTDIV_TRAP    ,"floating point divide" },
272e5c89e4eSSatish Balay   { FPE_FLTINEX_TRAP   ,"inexact floating point result" },
273e5c89e4eSSatish Balay   { 0                  ,"unknown error" }
274e5c89e4eSSatish Balay } ;
275e5c89e4eSSatish Balay #define SIGPC(scp) (0) /* Info MIGHT be in scp->sc_jmpbuf.jmp_context.iar */
276e5c89e4eSSatish Balay /*
277e5c89e4eSSatish Balay    For some reason, scp->sc_jmpbuf does not work on the RS6000, even though
278e5c89e4eSSatish Balay    it looks like it should from the include definitions.  It is probably
279e5c89e4eSSatish Balay    some strange interaction with the "POSIX_SOURCE" that we require.
280e5c89e4eSSatish Balay */
281e5c89e4eSSatish Balay 
282e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig,int code,struct sigcontext *scp)
283e5c89e4eSSatish Balay {
284e5c89e4eSSatish Balay   PetscErrorCode ierr;
285e5c89e4eSSatish Balay   int            err_ind,j;
286e5c89e4eSSatish Balay   fp_ctx_t       flt_context;
287e5c89e4eSSatish Balay 
288e5c89e4eSSatish Balay   PetscFunctionBegin;
289e5c89e4eSSatish Balay   fp_sh_trap_info(scp,&flt_context);
290e5c89e4eSSatish Balay 
291e5c89e4eSSatish Balay   err_ind = -1;
292e5c89e4eSSatish Balay   for (j = 0; error_codes[j].code_no; j++) {
293e5c89e4eSSatish Balay     if (error_codes[j].code_no == flt_context.trap) err_ind = j;
294e5c89e4eSSatish Balay   }
295e5c89e4eSSatish Balay 
296a297a907SKarl Rupp   if (err_ind >= 0) (*PetscErrorPrintf)("*** %s occurred ***\n",error_codes[err_ind].name);
297a297a907SKarl Rupp   else              (*PetscErrorPrintf)("*** floating point error 0x%x occurred ***\n",flt_context.trap);
298a297a907SKarl Rupp 
299efca3c55SSatish Balay   ierr = PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error");
300*41e02c4dSJunchao Zhang   PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP);
301e5c89e4eSSatish Balay }
302e5c89e4eSSatish Balay 
303e5c89e4eSSatish Balay PetscErrorCode PetscSetFPTrap(PetscFPTrap on)
304e5c89e4eSSatish Balay {
305e5c89e4eSSatish Balay   PetscFunctionBegin;
306e5c89e4eSSatish Balay   if (on == PETSC_FP_TRAP_ON) {
307e5c89e4eSSatish Balay     signal(SIGFPE,(void (*)(int))PetscDefaultFPTrap);
308e5c89e4eSSatish Balay     fp_trap(FP_TRAP_SYNC);
309e5c89e4eSSatish Balay     fp_enable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW);
310e5c89e4eSSatish Balay     /* fp_enable(mask) for individual traps.  Values are:
311e5c89e4eSSatish Balay        TRP_INVALID
312e5c89e4eSSatish Balay        TRP_DIV_BY_ZERO
313e5c89e4eSSatish Balay        TRP_OVERFLOW
314e5c89e4eSSatish Balay        TRP_UNDERFLOW
315e5c89e4eSSatish Balay        TRP_INEXACT
316e5c89e4eSSatish Balay        Can OR then together.
317e5c89e4eSSatish Balay        fp_enable_all(); for all traps.
318e5c89e4eSSatish Balay     */
319e5c89e4eSSatish Balay   } else {
320e5c89e4eSSatish Balay     signal(SIGFPE,SIG_DFL);
321e5c89e4eSSatish Balay     fp_disable(TRP_INVALID | TRP_DIV_BY_ZERO | TRP_OVERFLOW);
322e5c89e4eSSatish Balay     fp_trap(FP_TRAP_OFF);
323e5c89e4eSSatish Balay   }
324670f3ff9SJed Brown   _trapmode = on;
325e5c89e4eSSatish Balay   PetscFunctionReturn(0);
326e5c89e4eSSatish Balay }
327e5c89e4eSSatish Balay 
3289a2402e9SBarry Smith #elif defined(PETSC_HAVE_FENV_H) && !defined(__cplusplus)
329b014e56cSJed Brown /*
330b014e56cSJed Brown    C99 style floating point environment.
331b014e56cSJed Brown 
332b014e56cSJed Brown    Note that C99 merely specifies how to save, restore, and clear the floating
333b014e56cSJed Brown    point environment as well as defining an enumeration of exception codes.  In
334b014e56cSJed Brown    particular, C99 does not specify how to make floating point exceptions raise
335b014e56cSJed Brown    a signal.  Glibc offers this capability through FE_NOMASK_ENV (or with finer
336b014e56cSJed Brown    granularity, feenableexcept()), xmmintrin.h offers _MM_SET_EXCEPTION_MASK().
337b014e56cSJed Brown */
338b014e56cSJed Brown #include <fenv.h>
339b014e56cSJed Brown typedef struct {int code; const char *name;} FPNode;
340b014e56cSJed Brown static const FPNode error_codes[] = {
341b014e56cSJed Brown   {FE_DIVBYZERO,"divide by zero"},
342b014e56cSJed Brown   {FE_INEXACT,  "inexact floating point result"},
343b014e56cSJed Brown   {FE_INVALID,  "invalid floating point arguments (domain error)"},
344b014e56cSJed Brown   {FE_OVERFLOW, "floating point overflow"},
345b014e56cSJed Brown   {FE_UNDERFLOW,"floating point underflow"},
346b014e56cSJed Brown   {0           ,"unknown error"}
347b014e56cSJed Brown };
34899e0435eSBarry Smith 
349b014e56cSJed Brown void PetscDefaultFPTrap(int sig)
350b014e56cSJed Brown {
351b014e56cSJed Brown   const FPNode *node;
352b014e56cSJed Brown   int          code;
353ace3abfcSBarry Smith   PetscBool    matched = PETSC_FALSE;
354b014e56cSJed Brown 
355b014e56cSJed Brown   PetscFunctionBegin;
356b014e56cSJed Brown   /* Note: While it is possible for the exception state to be preserved by the
357b014e56cSJed Brown    * kernel, this seems to be rare which makes the following flag testing almost
358b014e56cSJed Brown    * useless.  But on a system where the flags can be preserved, it would provide
3597d125cddSJed Brown    * more detail.
360b014e56cSJed Brown    */
361b014e56cSJed Brown   code = fetestexcept(FE_ALL_EXCEPT);
362b014e56cSJed Brown   for (node=&error_codes[0]; node->code; node++) {
363b014e56cSJed Brown     if (code & node->code) {
364b014e56cSJed Brown       matched = PETSC_TRUE;
365b014e56cSJed Brown       (*PetscErrorPrintf)("*** floating point error \"%s\" occurred ***\n",node->name);
366b014e56cSJed Brown       code &= ~node->code; /* Unset this flag since it has been processed */
367b014e56cSJed Brown     }
368b014e56cSJed Brown   }
369b014e56cSJed Brown   if (!matched || code) { /* If any remaining flags are set, or we didn't process any flags */
370b014e56cSJed Brown     (*PetscErrorPrintf)("*** unknown floating point error occurred ***\n");
3717d125cddSJed Brown     (*PetscErrorPrintf)("The specific exception can be determined by running in a debugger.  When the\n");
3727d125cddSJed Brown     (*PetscErrorPrintf)("debugger traps the signal, the exception can be found with fetestexcept(0x%x)\n",FE_ALL_EXCEPT);
3737d125cddSJed Brown     (*PetscErrorPrintf)("where the result is a bitwise OR of the following flags:\n");
3747d125cddSJed 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);
375b014e56cSJed Brown   }
3767d125cddSJed Brown 
3777d125cddSJed Brown   (*PetscErrorPrintf)("Try option -start_in_debugger\n");
3788bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG)
379dbf62e16SBarry Smith   if (!PetscStackActive()) (*PetscErrorPrintf)("  or try option -log_stack\n");
380a297a907SKarl Rupp   else {
3817d125cddSJed Brown     (*PetscErrorPrintf)("likely location of problem given in stack below\n");
3827d125cddSJed Brown     (*PetscErrorPrintf)("---------------------  Stack Frames ------------------------------------\n");
383639ff905SBarry Smith     PetscStackView(PETSC_STDOUT);
3847d125cddSJed Brown   }
3857d125cddSJed Brown #endif
3867d125cddSJed Brown #if !defined(PETSC_USE_DEBUG)
3877d125cddSJed Brown   (*PetscErrorPrintf)("configure using --with-debugging=yes, recompile, link, and run \n");
3887d125cddSJed Brown   (*PetscErrorPrintf)("with -start_in_debugger to get more information on the crash.\n");
3897d125cddSJed Brown #endif
390efca3c55SSatish Balay   PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_INITIAL,"trapped floating point error");
391*41e02c4dSJunchao Zhang   PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP);
392b014e56cSJed Brown }
393b014e56cSJed Brown 
3947087cfbeSBarry Smith PetscErrorCode  PetscSetFPTrap(PetscFPTrap on)
395b014e56cSJed Brown {
396b014e56cSJed Brown   PetscFunctionBegin;
397b014e56cSJed Brown   if (on == PETSC_FP_TRAP_ON) {
398b014e56cSJed Brown     /* Clear any flags that are currently set so that activating trapping will not immediately call the signal handler. */
399e32f2f54SBarry Smith     if (feclearexcept(FE_ALL_EXCEPT)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot clear floating point exception flags\n");
400b014e56cSJed Brown #if defined FE_NOMASK_ENV
401b014e56cSJed Brown     /* We could use fesetenv(FE_NOMASK_ENV), but that causes spurious exceptions (like gettimeofday() -> PetscLogDouble). */
402e32f2f54SBarry Smith     if (feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW) == -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot activate floating point exceptions\n");
403b014e56cSJed Brown #elif defined PETSC_HAVE_XMMINTRIN_H
404da407576SBarry Smith     _MM_SET_EXCEPTION_MASK(_MM_MASK_INEXACT | _MM_MASK_UNDERFLOW);
405b014e56cSJed Brown #else
406b014e56cSJed Brown     /* C99 does not provide a way to modify the environment so there is no portable way to activate trapping. */
407b014e56cSJed Brown #endif
408e32f2f54SBarry Smith     if (SIG_ERR == signal(SIGFPE,PetscDefaultFPTrap)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't set floating point handler\n");
409b014e56cSJed Brown   } else {
410e32f2f54SBarry Smith     if (fesetenv(FE_DFL_ENV)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot disable floating point exceptions");
411e32f2f54SBarry Smith     if (SIG_ERR == signal(SIGFPE,SIG_DFL)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Can't clear floating point handler\n");
412b014e56cSJed Brown   }
413670f3ff9SJed Brown   _trapmode = on;
414b014e56cSJed Brown   PetscFunctionReturn(0);
415b014e56cSJed Brown }
416b014e56cSJed Brown 
417e5c89e4eSSatish Balay /* -------------------------Default -----------------------------------*/
418e5c89e4eSSatish Balay #else
41999e0435eSBarry Smith 
420e5c89e4eSSatish Balay void PetscDefaultFPTrap(int sig)
421e5c89e4eSSatish Balay {
422e5c89e4eSSatish Balay   PetscFunctionBegin;
423e5c89e4eSSatish Balay   (*PetscErrorPrintf)("*** floating point error occurred ***\n");
424efca3c55SSatish Balay   PetscError(PETSC_COMM_SELF,0,"User provided function","Unknown file",PETSC_ERR_FP,PETSC_ERROR_REPEAT,"floating point error");
425*41e02c4dSJunchao Zhang   PETSCABORT(MPI_COMM_WORLD,PETSC_ERR_FP);
426e5c89e4eSSatish Balay }
42799e0435eSBarry Smith 
4287087cfbeSBarry Smith PetscErrorCode  PetscSetFPTrap(PetscFPTrap on)
429e5c89e4eSSatish Balay {
430e5c89e4eSSatish Balay   PetscFunctionBegin;
431e5c89e4eSSatish Balay   if (on == PETSC_FP_TRAP_ON) {
432a297a907SKarl Rupp     if (SIG_ERR == signal(SIGFPE,PetscDefaultFPTrap)) (*PetscErrorPrintf)("Can't set floatingpoint handler\n");
433a297a907SKarl Rupp   } else if (SIG_ERR == signal(SIGFPE,SIG_DFL))       (*PetscErrorPrintf)("Can't clear floatingpoint handler\n");
434a297a907SKarl Rupp 
435670f3ff9SJed Brown   _trapmode = on;
436e5c89e4eSSatish Balay   PetscFunctionReturn(0);
437e5c89e4eSSatish Balay }
438e5c89e4eSSatish Balay #endif
439e5c89e4eSSatish Balay 
440e5c89e4eSSatish Balay 
441e5c89e4eSSatish Balay 
442