在 CUDA 中测试无穷大

发布于 2024-10-06 07:05:43 字数 550 浏览 3 评论 0原文

在 CUDA 程序中,我最近从使用

return x==INFINITY || x==-INFINITY;

来自 math.h 的 INFINITY 来测试无穷大,转换为使用来自 math.h 的无穷大测试,

return !isfinite(x);

并且非常惊讶地得到了不同的结果。 gnu.org 建议他们实际上应该表现得相似地。我错过了什么吗? CUDA内核中是否不允许使用INFINITY

编辑: 我刚刚发现了 isinf 并注意到使用检查

return isinf(x);

给出了与 INFINITY 检查相同的结果。为什么不是 isfinite(x)==!isinf(x)?

In a CUDA program, I recently switched from testing for inifinity using

return x==INFINITY || x==-INFINITY;

where INFINITY is from math.h, to

return !isfinite(x);

and was quite surprised to get different results. gnu.org suggests that they actually should behave similarly. Am I missing something? Is it not allowed to use INFINITY in a CUDA kernel?

Edit:
I just discovered isinf and noticed that checking using

return isinf(x);

gives the same result as the INFINITY check. Why isn't
isfinite(x)==!isinf(x)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

╰ゝ天使的微笑 2024-10-13 07:05:43

isfinite(a)!isnan(a) && 相同!isinf(a)。如果 x 为 NaN,则 isfinite(x) 和 isinf(x) 都为 false。

isfinite(a) is the same as !isnan(a) && !isinf(a). If x is NaN, then both isfinite(x) and isinf(x) are false.

去了角落 2024-10-13 07:05:43

isinf() 仅检查 +INFINITY-INFINITY

!isfinite() 检查 +INFINITY-INFINITYNaN

isinf() checks only for +INFINITY or -INFINITY.

!isfinite() checks for +INFINITY, -INFINITY or NaN.

暖风昔人 2024-10-13 07:05:43

浮点比较不一定有效。例如,可能是 (1.0f + 3.0f != 2.0f + 2.0f)。 isfinite 完全有可能将小于特定常量的值视为等于 INFINITE 或 -INFINITE,而你写的是字面意义上的平等。

Floating-point comparisons aren't necessarily valid. For example, it's possible that (1.0f + 3.0f != 2.0f + 2.0f).It's perfectly possible that isfinite considers values less than a specific constant apart to be equal to INFINITE or -INFINITE, whereas you wrote a literal equality.

初相遇 2024-10-13 07:05:43

许多 GPU 和 SIMD 单元并不完全符合 IEEE754,特别是对于无穷大和 NaN 的边缘情况。就在昨晚,我注意到我使用的一个特定矢量处理器声称 ∞+1 ≠ ∞ ,并且 x == x 即使对于 x ∈ NaN 也是如此。

Many GPUs and SIMD units are not totally IEEE754 compliant, especially for edge cases around infinities and NaNs. Just last night I noticed that a particular vector processor I worked with claimed that ∞+1 ≠ ∞ , and x == x even for x ∈ NaN.

魂归处 2024-10-13 07:05:43

在最近的帖子中 检查是否矩阵在 CUDA 中包含 nan 或无限值,Robert Crovella 建议使用 isinf() 来检查 CUDA 中的无限值。

下面我提供了一个使用 isinf() 并利用 CUDA Thrust 检查数组中无限值的示例。也许它可以为其他用户提供参考。下面的例子相当于Matlab的d_result=isinf(d_data);。它与我针对上面引用的问题发布的示例不同,因为当前一个检查每个单独元素是否是无限的,而另一个检查整个数组是否至少包含一个 NaN 并且相当于Matlab 的 sum(isnan(d_data));

#include <thrust/sequence.h>

#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust\device_vector.h>
#include <thrust\reduce.h>

#include <float.h>

// --- Operator for testing inf values
struct isinf_test { 
    __host__ __device__ bool operator()(const float a) const {
        return isinf(a);
    }
};

void main(){

    const int N = 10;

    thrust::host_vector<float> h_data(N);
    for (int i=0; i<N; i++)
        h_data[i] = rand()/RAND_MAX;

    h_data[0] = FLT_MAX/FLT_MIN;

    thrust::device_vector<float> d_data(h_data);
    thrust::device_vector<float> d_result(h_data);

    thrust::transform(d_data.begin(), d_data.end(), d_result.begin(), isinf_test());

    for (int i=0; i<N; i++) {
        float val = d_result[i];
        printf("Isinf test for element number %i equal to %f\n",i,val);
    }

    getchar();

}

In the recent post Checking if a matrix contains nans or infinite values in CUDA, Robert Crovella has suggested using isinf() to check for infinite values in CUDA.

Below I'm providing an example of checking for infinite values in an array using isinf() and by exploiting CUDA Thrust. Perhaps it could be useful as reference for other users. The example below is equivalent to Matlab's d_result=isinf(d_data);. It is different from the example I posted for the question quoted above in that the present one checks for each individual element is infinite while the other one checked if the whole array contained at least one NaN and was equivalent to Matlab's sum(isnan(d_data));.

#include <thrust/sequence.h>

#include <thrust/device_vector.h>
#include <thrust/host_vector.h>
#include <thrust\device_vector.h>
#include <thrust\reduce.h>

#include <float.h>

// --- Operator for testing inf values
struct isinf_test { 
    __host__ __device__ bool operator()(const float a) const {
        return isinf(a);
    }
};

void main(){

    const int N = 10;

    thrust::host_vector<float> h_data(N);
    for (int i=0; i<N; i++)
        h_data[i] = rand()/RAND_MAX;

    h_data[0] = FLT_MAX/FLT_MIN;

    thrust::device_vector<float> d_data(h_data);
    thrust::device_vector<float> d_result(h_data);

    thrust::transform(d_data.begin(), d_data.end(), d_result.begin(), isinf_test());

    for (int i=0; i<N; i++) {
        float val = d_result[i];
        printf("Isinf test for element number %i equal to %f\n",i,val);
    }

    getchar();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文