通过设备中的阵列并行减少CUDA

Hla*_*son 1 cuda reduction thrust

我需要执行并行缩减以在CUDA设备上找到数组的最小值或最大值.我找到了一个很好的图书馆,称为Thrust.您似乎只能在主机内存中对阵列执行并行缩减.我的数据在设备内存中.是否可以减少设备内存中的数据?我无法想象如何做到这一点.以下是Thrust的文档:http://code.google.com/p/thrust/wiki/QuickStartGuide#Reductions.谢谢大家.

tal*_*ies 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];
Run Code Online (Sandbox Code Playgroud)

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

int * dres = thrust::raw_pointer_cast(dresptr); 
Run Code Online (Sandbox Code Playgroud)