I'm trying to get access to vector elements in this manner
struct point
{
unsigned int x;
unsigned int y;
};
...
thrust::device_vector<point> devPoints(hPoints.begin(), hPoints.end());
for(thrust::device_vector<point>::iterator iter = devPoints.begin(); iter != devPoints.end(); iter++)
{
std::cout << iter->x << " " << iter->y << " " << std::endl; (1)
}
device_vector was initialized properly. I get following errors:
error: expression must have pointer type (at 1)
error: no suitable user-defined conversion from "const thrust::detail::normal_iterator<thrust::device_ptr<point>>" to "thrust::device_ptr<point>" exists
detected during instantiation of "Pointer thrust::experimental::iterator_facade<Derived, Pointer, Value, Space, Traversal, Reference, Difference>::operator->() const [with Derived=thrust::detail::normal_iterator<thrust::device_ptr<point>>, Pointer=thrust::device_ptr<point>, Value=point, Space=thrust::detail::cuda_device_space_tag, Traversal=thrust::random_access_traversal_tag, Reference=thrust::device_reference<point>, Difference=ptrdiff_t]"
What am I doing wrong?
Ok this one was a bit more complicated than I expected :)
Here are the results of my investigations:
Your problem comes from thrust's implementation. Thrust uses a type called
device_reference
which, as its documentation says: http://wiki.thrust.googlecode.com/hg/html/classthrust_1_1device__reference.htmlHowever, there are some cases when we are dealing implicitly with
device_reference
. For example, when a device_reference is passed as a parameter to functions waiting for POD (more or less what you are trying to do withoperator<<
), the following problem appears:Having said that, all you have to do is to cast your
device_reference
to the POD you're handling. In your case, you'd do:In my opinion, this is not the most elegant solution, I'd rather use the
std::copy
algorithm to print the content of yourpoint
class. Thus, I've written a small example file, using yourpoint
class and printing it using three different ways:Now, it's up to you to choose the one you prefer!
:))