gfortran警告浮点异常 [英] gfortran warn on floating point exception
问题描述
-ffpe-trap = 0,overflow,invalid
试图追捕一些错误。这使我的程序立即停止执行。在某些情况下,FPE可能是正常的,所以如下所示的标志:
-ffpe-warn =零,溢出,无效
将非常有用。 gfortran(或任何其他编译器)是否提供这样的东西?如果没有,是否有任何解决方法?我目前的想法是创建一个C函数来注册一个信号处理程序来写出警告,尽管我不知道该怎么做。
我不知道遇到浮点异常的一种警告方式。但是,gfortran和ifort都有信号处理程序。参见例如信号和
英特尔Fortran编译器用户和参考指南(警告:大型PDF)(见第410页)
您可以为呼叫
信号
的信号建立以下操作之一:
- 忽略指定的信号(由号码标识)。
- 对指定的信号使用默认操作,可以重置先前建立的操作。
- 将控制从指定的信号转移到接收信号的过程,由名称指定。
在您的情况下,您会想要在发生浮点异常时(例如打印文件名/行号)写一个函数来执行某些操作,并使用上面列表中的第三个选项。
不幸的是这不是很便携:看看此页面,了解各种编译器的信号处理示例。如果你想要
- 编译多个编译器
- ,可以在预处理宏中包装一些代码如果设置了一些预处理标志,请使用信号处理例程(参见
-NDEBUG
)
更新:根据High Performance Mark的建议,最终, ieee_exceptions
内在模块的异常处理工具将是便携式的方法。
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.
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.
这篇关于gfortran警告浮点异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!