设备存储器上的推力减小结果

Org*_*rim 4 reduce cuda thrust

是否可以在设备分配的内存中保留推力::减少操作的返回值?如果是这样,是否就像将值分配给cudaMalloc的区域一样容易,还是我应该使用推力:: device_ptr?

Rob*_*lla 5

是否可以在设备分配的内存中保留推力::减少操作的返回值?

最简洁的答案是不。

推力减小返回一个量,即减小的结果。此数量必须存储在宿主常驻变量中

以reduce为例,它是同步的,并且始终将其结果返回给CPU:

template<typename Iterator, typename T> 
T reduce(Iterator first, Iterator last, T init); 
Run Code Online (Sandbox Code Playgroud)

将操作结果返回到CPU后,可以根据需要将其复制到GPU:

#include <iostream>
#include <thrust/device_vector.h>
#include <thrust/reduce.h>

int main(){

    thrust::device_vector<int> data(256, 1);
    thrust::device_vector<int> result(1);
    result[0] = thrust::reduce(data.begin(), data.end());
    std::cout << "result = " << result[0] << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

另一种可能的替代方法是使用thrust::reduce_by_key它将缩减结果返回到设备内存,而不是复制到主机内存。如果您对整个阵列使用单个键,则最终结果将是单个输出,类似于thrust::reduce


小智 5

是的,通过使用thrust::reduce_by_key代替为键提供的thrust::constant_iterator应该是可能的。