在 CUDA 中测试无穷大
在 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'tisfinite(x)==!isinf(x)
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
isfinite(a)
与!isnan(a) && 相同!isinf(a)
。如果 x 为 NaN,则 isfinite(x) 和 isinf(x) 都为 false。isfinite(a)
is the same as!isnan(a) && !isinf(a)
. Ifx
is NaN, then bothisfinite(x)
andisinf(x)
are false.isinf()
仅检查+INFINITY
或-INFINITY
。!isfinite()
检查+INFINITY
、-INFINITY
或NaN
。isinf()
checks only for+INFINITY
or-INFINITY
.!isfinite()
checks for+INFINITY
,-INFINITY
orNaN
.浮点比较不一定有效。例如,可能是
(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.许多 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.
在最近的帖子中 检查是否矩阵在 CUDA 中包含 nan 或无限值,Robert Crovella 建议使用
isinf()
来检查 CUDA 中的无限值。下面我提供了一个使用
isinf()
并利用 CUDA Thrust 检查数组中无限值的示例。也许它可以为其他用户提供参考。下面的例子相当于Matlab的d_result=isinf(d_data);
。它与我针对上面引用的问题发布的示例不同,因为当前一个检查每个单独元素是否是无限的,而另一个检查整个数组是否至少包含一个NaN
并且相当于Matlab 的sum(isnan(d_data));
。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'sd_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 oneNaN
and was equivalent to Matlab'ssum(isnan(d_data));
.