1

我需要执行并行缩减以找到 CUDA 设备上数组的最小值或最大值。我为此找到了一个很好的库,称为 Thrust。您似乎只能对主机内存中的数组执行并行缩减。我的数据在设备内存中。是否可以减少设备内存中的数据?我不知道该怎么做。这是 Thrust 的文档:http ://code.google.com/p/thrust/wiki/QuickStartGuide#Reductions 。谢谢大家。

4

2 回答 2

7

您可以减少已经在设备内存中的阵列的推力。您需要做的就是将设备指针包装在thrust::device_pointer容器中,并调用其中一个缩减过程,正如您链接到的 wiki 中所示:

// assume this is a valid device allocation holding N words of data
int * dmem;

// Wrap raw device pointer 
thrust::device_ptr<int> dptr(dmem);

// use max_element for reduction
thrust::device_ptr<int> dresptr = thrust::max_element(dptr, dptr+N);

// retrieve result from device (if required)
int max_value = dresptr[0];

请注意,返回值也是 a device_ptr,因此您可以直接在其他内核中使用它thrust::raw_pointer_cast

int * dres = thrust::raw_pointer_cast(dresptr); 
于 2012-04-12T13:59:05.290 回答
2

如果推力或任何其他库没有为您提供这样的服务,您仍然可以自己创建该内核。

Mark Harris 有一个关于并行减少及其在 cuda 上的优化的很棒的教程。按照他的幻灯片,根据您的需要实施和修改它并不难。

于 2012-04-12T13:49:59.797 回答