简体   繁体   English

gfortran警告浮点异常

[英]gfortran warn on floating point exception

I'm using gfortran for some code. 我正在使用gfortran编写一些代码。 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: 在某些情况下,FPE可能会正常运行,因此会出现如下标记:

-ffpe-warn=zero,overflow,invalid

would be very useful. 会非常有用。 Does gfortran (or any other compiler) provide anything like this? gfortran(或任何其他编译器)是否提供类似的功能? 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. 我目前的想法是创建一个C函数来注册信号处理程序以写出警告,尽管我不知道该怎么做。

I don't know of a way of warning on encountering a floating point exception. 我不知道遇到浮点异常的警告方式。 But both gfortran and ifort have signal handling routines. 但是gfortran和ifort都有信号处理例程。 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). 例如,请参见信号的gfortran文档以及《 Intel Fortran编译器用户和参考指南》(警告:大PDF) (请参阅病房第410页)。

You can establish one of the following actions for a signal with a call to signal : 您可以通过调用signal建立以下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 (eg 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 ) 仅在设置了某些预处理器标志的情况下才使用信号处理例程(参见-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. 更新:最终,如High Performance Mark所建议的那样, ieee_exceptions内部模块的异常处理工具将是可移植的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM