passing thrust::device_vector to a function by reference

僤鯓⒐⒋嵵緔 提交于 2019-11-29 12:20:34

Unfortunately, device_reference<T> cannot expose members of T, but it can convert to T.

To implement print, make a temporary copy of each element by converting it to a temporary temp:

void print(thrust::device_vector<point> &points, unsigned int index)
{
    point temp = points[index];
    std::cout << temp.y << temp.y << std::endl;
}

Each time you invoke print, it causes a transfer from GPU to system memory to create the temporary. If you need to print the entire collection of points at once, a more efficient method would copy the entire vector points en masse to a host_vector or std::vector (using thrust::copy) and then iterate through the collection as normal.

From http://thrust.googlecode.com/svn/tags/1.1.0/doc/html/structthrust_1_1device__reference.html :

device_reference acts as a reference to an object stored in device memory. device_reference is not intended to be used directly; rather, this type is the result of deferencing a device_ptr. Similarly, taking the address of a device_reference yields a device_ptr.

Maybe you need something like

(&points[index]).get()->x

instead of

points[index].x

It's a bit ugly, but CUDA needs a mechanism to transport data between RAM and GPU.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!