Actual source code: fp.c
1: /*
2: IEEE error handler for all machines. Since each OS has
3: enough slight differences we have completely separate codes for each one.
4: */
6: /*
7: This feature test macro provides FE_NOMASK_ENV on GNU. It must be defined
8: at the top of the file because other headers may pull in fenv.h even when
9: not strictly necessary. Strictly speaking, we could include ONLY petscconf.h,
10: check PETSC_HAVE_FENV_H, and only define _GNU_SOURCE in that case, but such
11: shenanigans ought to be unnecessary.
12: */
13: #if !defined(_GNU_SOURCE)
14: #define _GNU_SOURCE
15: #endif
17: #include <petsc/private/petscimpl.h>
18: #include <signal.h>
19: #if PetscDefined(HAVE_XMMINTRIN_H)
20: #include <xmmintrin.h>
21: #endif
23: struct PetscFPTrapLink {
24: PetscFPTrap trapmode;
25: struct PetscFPTrapLink *next;
26: };
27: static PetscFPTrap _trapmode = PETSC_FP_TRAP_OFF; /* Current trapping mode; see PetscDetermineInitialFPTrap() */
28: static struct PetscFPTrapLink *_trapstack; /* Any pushed states of _trapmode */
30: /*@
31: PetscFPTrapPush - push a floating point trapping mode, restored using `PetscFPTrapPop()`
33: Not Collective
35: Input Parameter:
36: . trap - `PETSC_FP_TRAP_ON` or `PETSC_FP_TRAP_OFF` or any of the values passable to `PetscSetFPTrap()`
38: Level: advanced
40: Notes:
41: This only changes the trapping if the new mode is different than the current mode.
43: This routine is called to turn off trapping for certain LAPACK routines that assume that dividing
44: by zero is acceptable. In particular the routine ieeeck().
46: Most systems by default have all trapping turned off, but certain Fortran compilers have
47: link flags that turn on trapping before the program begins.
48: .vb
49: gfortran -ffpe-trap=invalid,zero,overflow,underflow,denormal
50: ifort -fpe0
51: .ve
53: .seealso: `PetscFPTrapPop()`, `PetscSetFPTrap()`, `PetscDetermineInitialFPTrap()`
54: @*/
55: PetscErrorCode PetscFPTrapPush(PetscFPTrap trap)
56: {
57: struct PetscFPTrapLink *link;
59: PetscFunctionBegin;
60: PetscCall(PetscNew(&link));
61: #if PetscDefined(HAVE_THREADSAFETY) && PetscDefined(HAVE_OPENMP)
62: PetscPragmaOMP(critical)
63: #endif
64: {
65: link->trapmode = _trapmode;
66: link->next = _trapstack;
67: _trapstack = link;
68: }
69: if (trap != _trapmode) PetscCall(PetscSetFPTrap(trap));
70: PetscFunctionReturn(PETSC_SUCCESS);
71: }
73: /*@
74: PetscFPTrapPop - push a floating point trapping mode, to be restored using `PetscFPTrapPop()`
76: Not Collective
78: Level: advanced
80: .seealso: `PetscFPTrapPush()`, `PetscSetFPTrap()`, `PetscDetermineInitialFPTrap()`
81: @*/
82: PetscErrorCode PetscFPTrapPop(void)
83: {
84: struct PetscFPTrapLink *link;
86: PetscFunctionBegin;
87: if (_trapstack->trapmode != _trapmode) PetscCall(PetscSetFPTrap(_trapstack->trapmode));
88: #if PetscDefined(HAVE_THREADSAFETY) && PetscDefined(HAVE_OPENMP)
89: PetscPragmaOMP(critical)
90: #endif
91: {
92: link = _trapstack;
93: _trapstack = _trapstack->next;
94: }
95: PetscCall(PetscFree(link));
96: PetscFunctionReturn(PETSC_SUCCESS);
97: }
99: #if PetscDefined(HAVE_WINDOWS_COMPILERS)
100: #include <float.h>
101: void PetscDefaultFPTrap(int sig)
102: {
103: PetscFunctionBegin;
104: (void)(*PetscErrorPrintf)("*** floating point error occurred ***\n");
105: (void)PetscError(PETSC_COMM_SELF, 0, NULL, NULL, PETSC_ERR_FP, PETSC_ERROR_REPEAT, "floating point error");
106: PETSCABORT(MPI_COMM_WORLD, PETSC_ERR_FP);
107: }
109: /*@
110: PetscSetFPTrap - Enables traps/exceptions on common floating point errors. This option may not work on certain systems or only a
111: subset of exceptions may be trapable.
113: Not Collective
115: Input Parameter:
116: . flag - values are
117: .vb
118: PETSC_FP_TRAP_OFF - do not trap any exceptions
119: PETSC_FP_TRAP_ON - all exceptions that are possible on the system except underflow
120: PETSC_FP_TRAP_INDIV - integer divide by zero
121: PETSC_FP_TRAP_FLTOPERR - improper argument to function, for example with real numbers, the square root of a negative number
122: PETSC_FP_TRAP_FLTOVF - overflow
123: PETSC_FP_TRAP_FLTUND - underflow - not trapped by default on most systems
124: PETSC_FP_TRAP_FLTDIV - floating point divide by zero
125: PETSC_FP_TRAP_FLTINEX - inexact floating point result
126: .ve
128: Options Database Key:
129: . -fp_trap (true|false) - turn on or off trapping of floating point exceptions
131: Level: advanced
133: Notes:
134: Preferred usage is `PetscFPTrapPush()` and `PetscFPTrapPop()` instead of this routine
136: Currently only `PETSC_FP_TRAP_OFF` and `PETSC_FP_TRAP_ON` are handled. All others are treated as `PETSC_FP_TRAP_ON`.
138: The values are bit values and may be |ed together in the function call
140: On systems that support it this routine causes floating point
141: overflow, divide-by-zero, and invalid-operand (e.g., a NaN), but not underflow, to
142: cause a message to be printed and the program to exit.
144: On many common systems, the floating
145: point exception state is not preserved from the location where the trap
146: occurred through to the signal handler. In this case, the signal handler
147: will just say that an unknown floating point exception occurred and which
148: function it occurred in. If you run with -fp_trap in a debugger, it will
149: break on the line where the error occurred. On systems that support C99
150: floating point exception handling You can check which
151: exception occurred using fetestexcept(FE_ALL_EXCEPT). See fenv.h
152: (usually at /usr/include/bits/fenv.h) for the enum values on your system.
154: .seealso: `PetscFPTrapPush()`, `PetscFPTrapPop()`, `PetscDetermineInitialFPTrap()`
155: @*/
156: PetscErrorCode PetscSetFPTrap(PetscFPTrap flag)
157: {
158: unsigned int cw;
160: PetscFunctionBegin;
161: if (flag == PETSC_FP_TRAP_ON) {
162: /* cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW | _EM_UNDERFLOW; */
163: cw = _EM_INVALID | _EM_ZERODIVIDE | _EM_OVERFLOW;
164: PetscCheck(SIG_ERR != signal(SIGFPE, PetscDefaultFPTrap), PETSC_COMM_SELF, PETSC_ERR_LIB, "Can't set floating point handler");
165: } else {
166: cw = 0;
167: PetscCheck(SIG_ERR != signal(SIGFPE, SIG_DFL), PETSC_COMM_SELF, PETSC_ERR_LIB, "Can't clear floating point handler");
168: }
169: (void)_controlfp(0, cw);
170: _trapmode = flag;
171: PetscCall(PetscInfo(NULL, "Using PETSC_HAVE_WINDOWS_COMPILERS FPTRAP\n"));
172: PetscFunctionReturn(PETSC_SUCCESS);
173: }
175: /*@
176: PetscDetermineInitialFPTrap - Attempts to determine the floating point trapping that exists when `PetscInitialize()` is called
178: Not Collective
180: Note:
181: Currently only supported on Linux and macOS. Checks if divide by zero is enable and if so declares that trapping is on.
183: Level: advanced
185: .seealso: `PetscFPTrapPush()`, `PetscFPTrapPop()`, `PetscDetermineInitialFPTrap()`
186: @*/
187: PetscErrorCode PetscDetermineInitialFPTrap(void)
188: {
189: PetscFunctionBegin;
190: PetscCall(PetscInfo(NULL, "Unable to determine initial floating point trapping. Assuming it is off\n"));
191: PetscFunctionReturn(PETSC_SUCCESS);
192: }
194: #elif PetscDefined(HAVE_FENV_H) && !defined(__cplusplus)
195: /*
196: C99 style floating point environment.
198: Note that C99 merely specifies how to save, restore, and clear the floating
199: point environment as well as defining an enumeration of exception codes. In
200: particular, C99 does not specify how to make floating point exceptions raise
201: a signal. Glibc offers this capability through FE_NOMASK_ENV (or with finer
202: granularity, feenableexcept()), xmmintrin.h offers _MM_SET_EXCEPTION_MASK().
203: */
204: #include <fenv.h>
205: #if PetscDefined(HAVE_FE_VALUES)
206: typedef struct {
207: int code;
208: const char *name;
209: } FPNode;
210: static const FPNode error_codes[] = {
211: {FE_DIVBYZERO, "divide by zero" },
212: {FE_INEXACT, "inexact floating point result" },
213: {FE_INVALID, "invalid floating point arguments (domain error)"},
214: {FE_OVERFLOW, "floating point overflow" },
215: {FE_UNDERFLOW, "floating point underflow" },
216: {0, "unknown error" }
217: };
218: #endif
220: void PetscDefaultFPTrap(int sig)
221: {
222: #if PetscDefined(HAVE_FE_VALUES)
223: const FPNode *node;
224: int code;
225: PetscBool matched = PETSC_FALSE;
226: #endif
228: PetscFunctionBegin;
229: /* Note: While it is possible for the exception state to be preserved by the
230: * kernel, this seems to be rare which makes the following flag testing almost
231: * useless. But on a system where the flags can be preserved, it would provide
232: * more detail.
233: */
234: #if PetscDefined(HAVE_FE_VALUES)
235: code = fetestexcept(FE_ALL_EXCEPT);
236: for (node = &error_codes[0]; node->code; node++) {
237: if (code & node->code) {
238: matched = PETSC_TRUE;
239: (void)(*PetscErrorPrintf)("*** floating point error \"%s\" occurred ***\n", node->name);
240: code &= ~node->code; /* Unset this flag since it has been processed */
241: }
242: }
243: if (!matched || code) { /* If any remaining flags are set, or we didn't process any flags */
244: (void)(*PetscErrorPrintf)("*** unknown floating point error occurred ***\n");
245: (void)(*PetscErrorPrintf)("The specific exception can be determined by running in a debugger. When the\n");
246: (void)(*PetscErrorPrintf)("debugger traps the signal, the exception can be found with fetestexcept(0x%x)\n", FE_ALL_EXCEPT);
247: (void)(*PetscErrorPrintf)("where the result is a bitwise OR of the following flags:\n");
248: (void)(*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);
249: }
250: #else
251: (void)(*PetscErrorPrintf)("*** unknown floating point error occurred ***\n");
252: #endif
254: (void)(*PetscErrorPrintf)("Try option -start_in_debugger\n");
255: if (PetscDefined(USE_DEBUG)) {
256: (void)(*PetscErrorPrintf)("likely location of problem given in stack below\n");
257: (void)(*PetscErrorPrintf)("--------------------- Stack Frames ------------------------------------\n");
258: (void)PetscStackView(PETSC_STDOUT);
259: } else {
260: (void)(*PetscErrorPrintf)("configure using --with-debugging=yes, recompile, link, and run \n");
261: (void)(*PetscErrorPrintf)("with -start_in_debugger to get more information on the crash.\n");
262: }
263: (void)PetscError(PETSC_COMM_SELF, 0, NULL, NULL, PETSC_ERR_FP, PETSC_ERROR_INITIAL, "trapped floating point error");
264: PETSCABORT(MPI_COMM_WORLD, PETSC_ERR_FP);
265: }
267: PetscErrorCode PetscSetFPTrap(PetscFPTrap flag)
268: {
269: PetscFunctionBegin;
270: if (flag == PETSC_FP_TRAP_ON) {
271: /* Clear any flags that are currently set so that activating trapping will not immediately call the signal handler. */
272: PetscCheck(!feclearexcept(FE_ALL_EXCEPT), PETSC_COMM_SELF, PETSC_ERR_LIB, "Cannot clear floating point exception flags");
273: #if defined(FE_NOMASK_ENV) && PetscDefined(HAVE_FE_VALUES)
274: /* Could use fesetenv(FE_NOMASK_ENV), but that causes spurious exceptions (like gettimeofday() -> PetscLogDouble). */
275: /* PetscCheck(feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW) != -1,PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot activate floating point exceptions"); */
276: /* Doesn't work on AArch64 targets. There's a known hardware limitation. Need to detect hardware at configure time? */
277: PetscCheck(feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW) != -1, PETSC_COMM_SELF, PETSC_ERR_LIB, "Cannot activate floating point exceptions");
278: PetscCall(PetscInfo(NULL, "Using PETSC_HAVE_FENV_H FPTRAP with FE_NOMASK_ENV\n"));
279: #elif defined PETSC_HAVE_XMMINTRIN_H
280: _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_DIV_ZERO);
281: /* _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_UNDERFLOW); */
282: _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_OVERFLOW);
283: _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_INVALID);
284: PetscCall(PetscInfo(NULL, "Using PETSC_HAVE_FENV_H FPTRAP with PETSC_HAVE_XMMINTRIN_H\n"));
285: #else
286: /* C99 does not provide a way to modify the environment so there is no portable way to activate trapping. */
287: PetscCall(PetscInfo(NULL, "Using PETSC_HAVE_FENV_H FPTRAP\n"));
288: #endif
289: PetscCheck(SIG_ERR != signal(SIGFPE, PetscDefaultFPTrap), PETSC_COMM_SELF, PETSC_ERR_LIB, "Can't set floating point handler");
290: } else {
291: PetscCheck(!fesetenv(FE_DFL_ENV), PETSC_COMM_SELF, PETSC_ERR_LIB, "Cannot disable floating point exceptions");
292: /* can use _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() | _MM_MASK_UNDERFLOW); if PETSC_HAVE_XMMINTRIN_H exists */
293: PetscCheck(SIG_ERR != signal(SIGFPE, SIG_DFL), PETSC_COMM_SELF, PETSC_ERR_LIB, "Can't clear floating point handler");
294: }
295: _trapmode = flag;
296: PetscFunctionReturn(PETSC_SUCCESS);
297: }
299: PetscErrorCode PetscDetermineInitialFPTrap(void)
300: {
301: #if defined(FE_NOMASK_ENV) || defined PETSC_HAVE_XMMINTRIN_H
302: unsigned int flags;
303: #endif
305: PetscFunctionBegin;
306: #if defined(FE_NOMASK_ENV)
307: flags = fegetexcept();
308: if (flags & FE_DIVBYZERO) {
309: #elif defined PETSC_HAVE_XMMINTRIN_H
310: flags = _MM_GET_EXCEPTION_MASK();
311: if (!(flags & _MM_MASK_DIV_ZERO)) {
312: #else
313: PetscCall(PetscInfo(NULL, "Floating point trapping unknown, assuming off\n"));
314: PetscFunctionReturn(PETSC_SUCCESS);
315: #endif
316: #if defined(FE_NOMASK_ENV) || defined PETSC_HAVE_XMMINTRIN_H
317: _trapmode = PETSC_FP_TRAP_ON;
318: PetscCall(PetscInfo(NULL, "Floating point trapping is on by default %d\n", flags));
319: } else {
320: _trapmode = PETSC_FP_TRAP_OFF;
321: PetscCall(PetscInfo(NULL, "Floating point trapping is off by default %d\n", flags));
322: }
323: PetscFunctionReturn(PETSC_SUCCESS);
324: #endif
325: }
327: #elif PetscDefined(HAVE_IEEEFP_H)
328: #include <ieeefp.h>
329: void PetscDefaultFPTrap(int sig)
330: {
331: PetscFunctionBegin;
332: (void)(*PetscErrorPrintf)("*** floating point error occurred ***\n");
333: (void)PetscError(PETSC_COMM_SELF, 0, NULL, NULL, PETSC_ERR_FP, PETSC_ERROR_REPEAT, "floating point error");
334: PETSCABORT(MPI_COMM_WORLD, PETSC_ERR_FP);
335: }
337: PetscErrorCode PetscSetFPTrap(PetscFPTrap flag)
338: {
339: PetscFunctionBegin;
340: if (flag == PETSC_FP_TRAP_ON) {
341: #if PetscDefined(HAVE_FPRESETSTICKY)
342: fpresetsticky(fpgetsticky());
343: #elif PetscDefined(HAVE_FPSETSTICKY)
344: fpsetsticky(fpgetsticky());
345: #endif
346: fpsetmask(FP_X_INV | FP_X_DZ | FP_X_OFL | FP_X_OFL);
347: PetscCheck(SIG_ERR != signal(SIGFPE, PetscDefaultFPTrap), PETSC_COMM_SELF, PETSC_ERR_LIB, "Can't set floating point handler");
348: } else {
349: #if PetscDefined(HAVE_FPRESETSTICKY)
350: fpresetsticky(fpgetsticky());
351: #elif PetscDefined(HAVE_FPSETSTICKY)
352: fpsetsticky(fpgetsticky());
353: #endif
354: fpsetmask(0);
355: PetscCheck(SIG_ERR != signal(SIGFPE, SIG_DFL), PETSC_COMM_SELF, PETSC_ERR_LIB, "Can't clear floating point handler");
356: }
357: _trapmode = flag;
358: PetscCall(PetscInfo(NULL, "Using PETSC_HAVE_IEEEFP_H FPTRAP\n"));
359: PetscFunctionReturn(PETSC_SUCCESS);
360: }
362: PetscErrorCode PetscDetermineInitialFPTrap(void)
363: {
364: PetscFunctionBegin;
365: PetscCall(PetscInfo(NULL, "Unable to determine initial floating point trapping. Assuming it is off\n"));
366: PetscFunctionReturn(PETSC_SUCCESS);
367: }
369: /* Default */
370: #else
372: static void PetscDefaultFPTrap(int sig)
373: {
374: PetscFunctionBegin;
375: (void)(*PetscErrorPrintf)("*** floating point error occurred ***\n");
376: (void)PetscError(PETSC_COMM_SELF, 0, NULL, NULL, PETSC_ERR_FP, PETSC_ERROR_REPEAT, "floating point error");
377: PETSCABORT(MPI_COMM_WORLD, PETSC_ERR_FP);
378: }
380: PetscErrorCode PetscSetFPTrap(PetscFPTrap flag)
381: {
382: PetscFunctionBegin;
383: if (flag == PETSC_FP_TRAP_ON) {
384: if (SIG_ERR == signal(SIGFPE, PetscDefaultFPTrap)) PetscCall((*PetscErrorPrintf)("Can't set floatingpoint handler\n"));
385: } else if (SIG_ERR == signal(SIGFPE, SIG_DFL)) PetscCall((*PetscErrorPrintf)("Can't clear floatingpoint handler\n"));
387: _trapmode = flag;
388: PetscCall(PetscInfo(NULL, "Using default FPTRAP\n"));
389: PetscFunctionReturn(PETSC_SUCCESS);
390: }
392: PetscErrorCode PetscDetermineInitialFPTrap(void)
393: {
394: PetscFunctionBegin;
395: PetscCall(PetscInfo(NULL, "Unable to determine initial floating point trapping. Assuming it is off\n"));
396: PetscFunctionReturn(PETSC_SUCCESS);
397: }
398: #endif