gpt4 book ai didi

c++ - 推力结构 vector 的迭代器

转载 作者:太空狗 更新时间:2023-10-29 23:47:15 24 4
gpt4 key购买 nike

我正在尝试以这种方式访问​​ vector 元素

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 已正确初始化。我收到以下错误:

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]"

我做错了什么?

最佳答案

好吧,这个比我预期的要复杂一点:)
以下是我的调查结果:

您的问题来自推力的实现。 Thrust 使用一种名为 device_reference 的类型正如其文档所述:http://wiki.thrust.googlecode.com/hg/html/classthrust_1_1device__reference.html

device_reference acts as a reference-like object 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.

但是,在某些情况下我们隐式处理 device_reference .例如,当将 device_reference 作为参数传递给等待 POD 的函数(或多或少是您尝试使用 operator<< 执行的操作)时,会出现以下问题:

Another common case where a device_reference cannot directly be used in place of its referent object occurs when passing them as parameters to functions like printf which have varargs parameters. Because varargs parameters must be Plain Old Data, a device_reference to a POD type requires a cast when passed to printf:

话虽如此,您所要做的就是转换您的 device_reference到您正在处理的 POD。在你的情况下,你会这样做:

for(thrust::device_vector<point>::iterator iter = devPoints.begin(); iter != devPoints.end(); iter++)  {
std::cout << (static_cast<point>(*iter)).x << " " << (static_cast<point>(*iter)).y << std::endl;
}

在我看来,这不是最优雅的解决方案,我宁愿使用 std::copy打印你的内容的算法 point类(class)。因此,我使用您的 point 编写了一个小示例文件。类并使用三种不同的方式打印它:

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <cstdlib>
#include <algorithm>
#include <iostream>

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

__host__
point getRandomPoint() {
point p;

p.x = rand();
p.y = rand();

return p;
}

__host__
std::ostream& operator<< (std::ostream& os, const point& p) {
os << "[ " << p.x << " ; " << p.y << " ]";
return os;
}

int main() {
// fill the host_vector with random points
thrust::host_vector<point> hPoints(512);
thrust::generate(hPoints.begin(), hPoints.end(), getRandomPoint);

// copy hPoints content to device memory
thrust::device_vector<point> devPoints(hPoints.begin(), hPoints.end());

// first way
for(thrust::device_vector<point>::iterator iter = devPoints.begin(); iter != devPoints.end(); iter++) {
std::cout << (static_cast<point>(*iter)).x << " " << (static_cast<point>(*iter)).y << std::endl;
}

// second way
for(thrust::device_vector<point>::iterator iter = devPoints.begin(); iter != devPoints.end(); iter++)
{
std::cout << *iter << std::endl;
}

// third way
std::copy(devPoints.begin(), devPoints.end(), std::ostream_iterator< point >(std::cout, " $ ") );

return 0;
}

现在,您可以选择自己喜欢的一款!

关于c++ - 推力结构 vector 的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6585614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com