c++ - 通过另一个 vector 推力访问 vector 元素

标签 c++ vector cuda thrust

Thrust我有三个设备 vector

device_vector<int> ID(N)
device_vector<float> F(M),Y(N)

通常在哪里 M<<N (但这应该无关紧要)。 ID 的所有值在 0...M-1 范围内.

我现在想分配 Y[i] = F[ID[i]]对于所有 i=0...N-1 .如果我正确理解 Thrust,我不能用 for_each 做到这一点和一些仿函数。有没有什么方法可以用 thrust::transform 来实现呢?或类似的东西?

最佳答案

您可以使用 thrust:gather 执行此操作,类似于:

device_vector<int> ID(N);
device_vector<float> F(M),Y(N);

thrust::gather(ID.begin(), ID.end(), F.begin(), Y.begin());

在调用中,ID 是用于收集F 中的值并将它们写入Y 的映射。

[标准免责声明:用浏览器编写,未经测试,使用风险自负]

关于c++ - 通过另一个 vector 推力访问 vector 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31605149/

相关文章:

c++ - Box2D 不寻常的错误。为什么 +0 可以防止错误?

c++ - 使用 vector 插入 map 的值

c++ - 理解以下 C++ 代码时遇到问题。 (寻求)

python - C++ 等价于 Python 的带有比较的数组访问 (array[condition] = value)

c++ - Arduino 串口解析

math - 带中心的旋转矩阵

c++ std::sort() 任何可比较成员仅使用一个比较函数的对象 vector

c++ - 了解 CUDA shfl 指令

c++ - 为什么不可能重载 CUDA C++ 类的主机/设备成员函数

cuda - 在 centos 6.2 上设置 cuda 5.0