3

我刚刚开始使用 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".

我哪里错了?我认为访问本机 CUDA 矢量数据类型的成员(例如uint2with.x.y)是正确的做法。

4

1 回答 1

4

正如 talonmies 在他的评论中指出的那样,您不能直接访问 a 拥有的元素的成员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;
}
于 2011-12-28T22:36:40.443 回答