我收到编译器错误
static_assert failed: 'Attempt to use an extended __device__ lambda in a context that requires querying its return type in host code. Use a named function object, a __host__ __device__ lambda, or cuda::proclaim_return_type instead.'
编译此代码时:
thrust::device_vector<float2> a;
thrust::device_vector<float> b;
float param1, param2;
float2 param3;
thrust::transform_reduce(
thrust::make_zip_iterator(thrust::make_tuple(a.begin(), b.begin())),
thrust::make_zip_iterator(thrust::make_tuple(a.end(), b.end())),
[param1, param2, param3] __device__ (thrust::tuple<float2, float> const& tuple)
{
/* do something and return a float2 */
},
float2{},
[] __device__ (float2 const& first, float2 const& second)
{
float2 result{};
result.x = first.x + second.x;
result.y = first.y + second.y;
return result;
});
我如何重写这段代码,使它编译?我是新的CUDA/推力和它的相当模糊,我需要如何处理这个问题。
(BTW,我真的需要这里复杂的“转换”功能吗?thrust::plus<float2>
不起作用。
1条答案
按热度按时间shyt4zoc1#
我怎样重写这段代码才能让它编译?
您在问题中发布的Assert消息中似乎指出了一个可能的解决方案:
static_assert失败:'尝试在需要在主机代码中查询其返回类型的上下文中使用扩展
__device__
lambda。请改用命名函数对象a__host__ __device__
lambda或cuda::proclaim_return_type。'当我将每个lambda都改为用
__host__ __device__
而不是__device__
修饰时,那么the code compiles for me on CUDA 12.1(BTW,我真的需要复杂的“转换”功能吗?thrust::plus不起作用。)
CUDA不为CUDA提供的向量类型提供算术运算符,AFAIK thrust也不提供:
则x+y必须被定义
因此,您需要提供自己的定义来添加
float2
类型,即将允许您使用
thrust::plus<float2>
。