通过另一个向量推力访问向量元素

Thrust access vector elements by another vector

Thrust中我有三个设备向量

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

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

我现在想为所有 i=0...N-1 分配 Y[i] = F[ID[i]]。如果我正确理解 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的映射。

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