为什么数字运算程序启动时发散到NaN的运行很慢? [英] Why a number crunching program starts running much slower when diverges into NaNs?

查看:297
本文介绍了为什么数字运算程序启动时发散到NaN的运行很慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个程序重复了的数组双取值一些计算。然后不幸的事发生了,并且NaN获得生产......它开始运行后,该慢得多。

A program repeats some calculation over an array of doubles. Then something unfortunate happens and NaN get produced... It starts running much slower after this.

-ffast-数学不改变任何事情。

为什么它与 -ffast-数学发生呢?难道不应该prevent投掷浮点异常,只是进行,以同样的速度像往常一样的数字?

Why does it happen with -ffast-math? Shouldn't it prevent throwing floating-point exceptions and just proceed and churn out NaNs at the same rate as usual numbers?

简单的例子:

nan.c

#include <stdio.h>
#include <math.h>

int main() {
    long long int i;
    double a=-1,b=0,c=1;

    for(i=0; i<100000000; ++i) {
        a+=0.001*(b+c)/1000;
        b+=0.001*(a+c)/1000;
        c+=0.001*(a+b)/1000;
        if(i%1000000==0) { fprintf(stdout, "%g\n", a); fflush(stdout); }
        if(i==50000000) b=NAN;
    }
    return 0;
}

运行:

$ gcc -ffast-math -O3 nan.c -o nan && ./nan  | ts '%.s'
...
1389025567.070093 2.00392e+33
1389025567.085662 1.48071e+34
1389025567.100250 1.0941e+35
1389025567.115273 8.08439e+35
1389025567.129992 5.9736e+36
1389025568.261108 nan
1389025569.385904 nan
1389025570.515169 nan
1389025571.657104 nan
1389025572.805347 nan

更新:尝试过各种 -O3 -ffast-数学 -msse -msse3 - 不影响。 Hovewer当我试图构建64位,而不是通常的32位,它开始以最快的速度处理NaN的其它数字(除了一般的50%增速),即使没有任何优化选项。为什么NaN是在32位模式下这么慢与 -ffast-数学

Update: Tried various -O3, -ffast-math, -msse, -msse3 - no effect. Hovewer when I tried building for 64-bits instead of usual 32-bits, it started to process NaNs as fast as other numbers (in addition to general 50% speedup), even without any optimisation options. Why NaNs are so slow in 32-bit mode with -ffast-math?

推荐答案

您的编译器默认使用的x87产生32位可执行文件时(这将产生一个摆摊处理NaN的)。通 -mfpmath = SSE 来告诉它使用SSE(可在速度处理NaN的)来代替。

Your compiler defaults to using x87 (which incurs a stall for processing NaNs) when producing a 32-bit executable. Pass -mfpmath=sse to tell it to use SSE (which can handle NaNs at speed) instead.

这篇关于为什么数字运算程序启动时发散到NaN的运行很慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆