Suppressing Floating Point Exceptions

Programs can be made to generate floating point exceptions in response to conditions that arise when numbers cannot be computed due to overflow, underflow, invalid conditions, etc. Programs that enable floating point exceptions will generate SIGFPE signals and terminate. In scientific computation, this is often a desirable behavior since it can give developers an opportunity to debug the generated core file.

When integrating various codes together, as in the case of in situ, it is sometimes not desirable to have a blanket response to floating point exceptions. For example, when the VisIt runtime library is integrated into simulations, certain operations may create conditions that would generate a floating point exception. However, many of these cases are not terribly important and do not warrant the entire application terminating. It is important to be able to selectively suppress floating point exceptions.

Host Code

This program demonstrates code that would result in an overflow of the double precision floating point number, causing a floating point exception. The calls to savesigfpe() and restoresigfpe() will suspend and then restore the program's capacity to generate floating point exceptions. The intent is that floating point exceptions can be disabled around less important code (or VisIt in situ code), and then re-enabled afterward.

! gfortran-4.6.1 -O3 -ffpe-trap=invalid,zero,overflow -o fpe fpe.f sigfpe.o
      program test_signal
      implicit none
      integer i
      real*8 x

! Call into a C routine to disable FPE's
      call savesigfpe()

      x=1.
      do i=1,310
         x = 10. * x
         if (i .gt. 300) then
             write (6,*) 'x=',x
         endif
      end do

! Call into C routine to restore FPE's
      call restoresigfpe()

      x=1.
      do i=1,310
         x = 10. * x
         if (i .gt. 300) then
             write (6,*) 'pass2: x=',x
         endif
      end do

      end program test_signal

Suppression Code

The following C functions can be compiled and called from the Fortran host code to disable and re-enable floating point exceptions.

#define _GNU_SOURCE
#include <fenv.h>

static int fpeflags = 0;

void
savesigfpe_(void)
{
    /* Save the FPE flags. */
    fpeflags = fegetexcept();

    /* Disable floating point exceptions. */
    fedisableexcept(FE_ALL_EXCEPT);
}

void
restoresigfpe_(void)
{
    /* Restore the previous FPE flags*/
    feenableexcept(fpeflags);
}

Related VisIt Enhancement Tickets

Just wanted to add a link VisIt issue tickets that deal with some of these issues also.