Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

c++ - passing thrust::device_vector to a function by reference

I'm trying to pass device_vector of structures

struct point 
{
    unsigned int x;
    unsigned int y;
}

to a function in a following manner:

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

myvector was initialized properly

print(myvector, 0);

I get following errors:

error: class "thrust::device_reference<point>" has no member "x"
error: class "thrust::device_reference<point>" has no member "y"

What's wrong with it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...