nVidia推力:device_ptr常数正确性 [英] nVidia Thrust: device_ptr Const-Correctness

查看:371
本文介绍了nVidia推力:device_ptr常数正确性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我广泛使用nVidia CUDA的项目中,我有时使用Thrust来做非常非常好的事情。减少 是在该库中特别好地实现的一种算法,并且 reduce 的一个使用是通过将每个元素除以所有

In my project which makes extensive use of nVidia CUDA, I sometimes use Thrust for things that it does very, very well. Reduce is one algorithm that is particularly well implemented in that library and one use of reduce is to normalise a vector of non-negative elements by dividing each element by the sum of all elements.

template <typename T>
void normalise(T const* const d_input, const unsigned int size, T* d_output)
{
    const thrust::device_ptr<T> X = thrust::device_pointer_cast(const_cast<T*>(d_input));
    T sum = thrust::reduce(X, X + size);

    thrust::constant_iterator<T> denominator(sum);
    thrust::device_ptr<T> Y = thrust::device_pointer_cast(d_output);
    thrust::transform(X, X + size, denominator, Y, thrust::divides<T>());
}

T 通常 float double

不想依赖于Thrust在我的整个代码库,所以我尝试确保像上面的示例函数只接受原始CUDA设备指针。这意味着一旦它们被NVCC编译,我可以将它们静态链接到没有NVCC的其他代码。

In general, I don't want to depend on Thrust throughout my entire code base so I try to make sure that functions like the above example accept only raw CUDA device pointers. This means that once they are compiled by NVCC, I can link them statically into other code without NVCC.

然而这个代码让我担心。我想要的函数是const正确,但我似乎找不到 const 版本 thrust :: device_pointer_cast(...) / code> - 这样的事情存在吗?在这个版本的代码中,我使用了 const_cast ,以便在函数签名中使用 const 让我很伤心。

This code worries me, however. I want the function to be const-correct but I can't seem to find a const version of thrust::device_pointer_cast(...) - Does such a thing exist? In this version of the code, I have resorted to a const_cast so that I use const in the function signature and that makes me sad.

在旁注中,将 reduce 的结果复制到主机只是发送回设备下一步。有没有更好的方法来做到这一点?

On a side note, it feels odd to copy the result of reduce to the host only to send it back to the device for the next step. Is there a better way to do this?

推荐答案

如果你想const正确性,你需要const正确无处不在。 输入是指向 const T 的指针,因此应该是 X

If you want const-correctness, you need to be const-correct everywhere. input is a pointer to const T, therefore so should be X:

const thrust::device_ptr<const T> X = thrust::device_pointer_cast(d_input);

这篇关于nVidia推力:device_ptr常数正确性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆