我刚刚开始使用 Thrust 库。我正在尝试在设备上制作一个长度为 5 的向量。她我只是设置第一个元素的成员vec[0]
#include<thrust/device_vector.h>
#include<iostream>
.
.
.
thrust::device_vector<uint2> vec(5);
vec[0]=make_uint2(4,5);
std::cout<<vec[0].x<<std::endl;
但是对于上面的代码,我得到了错误
error: class "thrust::device_reference<uint2>" has no member "x"
1 error detected in the compilation of "/tmp/tmpxft_000020dc_00000000-4_test.cpp1.ii".
我哪里错了?我认为访问 native CUDA 矢量数据类型的成员,例如
uint2
与 .x
和 .y
是正确的做法。
最佳答案
正如 talonmies 在他的评论中指出的那样,您不能直接访问 device_vector
拥有的元素的成员。 ,或任何包裹着 device_reference
的物体.但是,我想提供此答案以演示解决您的问题的替代方法。
即使 device_reference
不允许您访问包装对象的成员,它与 operator<<
兼容.此代码应按预期工作:
#include <thrust/device_vector.h>
#include <iostream>
// provide an overload for operator<<(ostream, uint2)
// as one is not provided otherwise
std::ostream &operator<<(std::ostream &os, const uint2 &x)
{
os << x.x << ", " << x.y;
return os;
}
int main()
{
thrust::device_vector<uint2> vec(5);
vec[0] = make_uint2(4,5);
std::cout << vec[0] << std::endl;
return 0;
}
关于cuda - uint2 : "has no member x" compiler error? 类型的推力向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8638839/