Visual Studio 尝试在需要在主机代码中查询其返回类型的上下文中使用扩展的_device_ lambda

yhxst69z  于 2023-04-22  发布在  其他
关注(0)|答案(1)|浏览(74)

我收到编译器错误

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>不起作用。

shyt4zoc

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类型,即

__host__ __device__ __forceinline__
float2 operator+(float2 left, float2 right) noexcept {
    return float2{left.x + right.x,
                  left.y + right.y};
}

将允许您使用thrust::plus<float2>

相关问题