cuda - uint2 : "has no member x" compiler error? 类型的推力向量

标签 cuda thrust

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

相关文章:

Cuda C 线程与 printf 或其他函数同步

pointers - 将结构中的双指针传递给 CUDA

c++ - 如何通过推力将主机 vector 复制到设备 vector

c++ - 将 CUDA 分配 char * 到对象的 device_vector

c++ - Thrust:对另一个数组索引的数组元素求和【Matlab的语法 sum(x(indices))】

c++ - CUDA 内核函数似乎显示竞争条件,尽管 racecheck 显示 0 竞争条件

cuda - 如何在一个方法内调用一个函数两次来编译cuda代码?

cuda - 在哪里可以找到有关 CUDA 4.0 中统一虚拟寻址的信息?

c++ - 如何使用推力和 CUDA 流将内存从主机异步复制到设备

cuda - 将 Thrust 设备迭代器转换为原始指针