推力减小结果在设备存储器 [英] thrust reduction result on device memory
问题描述
是否可以在设备分配的内存中保留thrust :: reduce操作的返回值?如果是,是否就像将值分配给cudaMalloc区域一样简单,或者应该使用thrust :: device_ptr?
Is it possible to leave the return value of a thrust::reduce operation in device-allocated memory? In case it is, is it just as easy as assigning the value to a cudaMalloc'ed area, or should I use a thrust::device_ptr?
推荐答案
是否可以在设备分配的内存中保留thrust :: reduce操作的返回值?
Is it possible to leave the return value of a thrust::reduce operation in device-allocated memory?
简短的答案是否定的。
thrust减少返回一个数量,减少的结果。此数量必须存储在主机常驻变量中:
thrust reduce returns a quantity, the result of the reduction. This quantity must be deposited in a host resident variable:
例如,reduce是同步的,
总是将结果返回给CPU:
Take for example reduce, which is synchronous and always returns its result to the CPU:
template<typename Iterator, typename T>
T reduce(Iterator first, Iterator last, T init);
一旦操作结果返回到CPU,就可以将其复制到GPU if您喜欢:
Once the result of the operation has been returned to the CPU, you can copy it to the GPU if you like:
#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;
}
这篇关于推力减小结果在设备存储器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!