我想知道 CUDA 是否提供了类似于
std::floating_point
的概念,但包括所有 IEE754 类型,例如__一半。我在下面提供了一个示例代码,用于测试 __half 模板函数是否无法使用 std::floating_point
进行编译。
#include <concepts>
#include <iostream>
#include <cuda_fp16.h>
template<std::floating_point T>
__host__ __device__ bool use_floating_point() {
return True;
}
int main() {
std::cout << "Is float a floating-point type?" << use_floating_point<float>() << '\n';
std::cout << "Is __half a floating-point type?" << use_floating_point<__half>() << '\n'; // Compilation error
return 0;
}
std::is_iec559
,因为 is_floating_point
仅涵盖一组有限的类型,而 is_iec559
,据我了解应该匹配任何实现 IEE754 的类型。
然而,正如@Artyer 在评论中指出的那样。
std::is_iec559<__half>
是false
。
如果您只需要
std::is_flaoting_point
加上__half
,您可以编写您的自定义概念
template <typename T>
concept floating_or_half = std::floating_point<T> || std::same_as<T,__half>;
template<floating_or_half T>
bool use_floating_point() {
return true;
}