2

I'm using gfortran for some code. For a while now, I've been compiling with

-ffpe-trap=zero,overflow,invalid

in an attempt to hunt down some bugs. This causes my program to cease execution immediately. There are some cases where the FPE might be OK and so a flag like:

-ffpe-warn=zero,overflow,invalid

would be very useful. Does gfortran (or any other compiler) provide anything like this? If not, are there any workarounds? My current thought is to create a C function to register a signal handler to write out the warning, although I have no idea how to go about doing that.

Chris
  • 44,602
  • 16
  • 137
  • 156
mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 1
    How would this putative runtime warning mechanism work? – David Heffernan Apr 18 '12 at 13:50
  • 3
    Does gfortran provide the (relatively new) ieee_arithmetic intrinsic module ? If it does, that would give you the tools you need to do your own f-p 'error' trapping in Fortran. – High Performance Mark Apr 18 '12 at 13:55
  • @DavidHefferman When the runtime exception is raised, the execution stops, a message is printed about where the exception occured and the program exits. I would just like to be notified when/where the exception occured (a message to stderr maybe?) and allow execution to proceed. Maybe that's not possible ... I don't know too much about how these things work ... – mgilson Apr 18 '12 at 13:56
  • @HighPerformanceMark I don't know. The code is not actually my code, and needs to be portable to a large number of different compilers -- I just ask about gfortran since that is the compiler that I typically use in my development. – mgilson Apr 18 '12 at 13:58

1 Answers1

4

I don't know of a way of warning on encountering a floating point exception. But both gfortran and ifort have signal handling routines. See for example the gfortran documentation of signal and the Intel Fortran Compiler User and Reference Guides (warning: large PDF) (see page 410 on wards).

You can establish one of the following actions for a signal with a call to signal:

  • Ignore the specified signal (identified by number).
  • Use the default action for the specified signal, which can reset a previously established action.
  • Transfer control from the specified signal to a procedure to receive the signal, specified by name.

In your case, you would want to write a function to do something when a floating point exception occurs (e.g. print file name/line number), and use the third option in the above list.

Unfortunately this is not very portable: take a look at this page for examples of signal handling for various compilers. You could wrap some code in preprocessor macros if you want to

  • compile with multiple compilers
  • only use the signal handling routines if some preprocessor flag is set (cf. -NDEBUG)

Update: Ultimately the exception handling facilities of the ieee_exceptions intrinsic module would be the portable way to do this, as suggested by High Performance Mark.

Community
  • 1
  • 1
Chris
  • 44,602
  • 16
  • 137
  • 156
  • Good to know about Fortran's signal handling -- Though I would probably do that part in C since it is a little more standardized. I still have no idea how to get the line number/filename/stacktrace where the exception occured -- googling around a bit makes me think it would be quite messy business. – mgilson Apr 18 '12 at 14:25