我已经使用普通的CUDA代码编写了内核,这些代码不使用推力设备矢量。内核输出一些存储在设备上的数组中的结果,比如数组X。现在我想对X进行一次约简。有没有一种方法可以在不先将X复制到thrust::device_vector变量中的情况下使用into::device函数?
发布于 2016-05-08 14:02:26
传统的方法是将设备指针封装到thrust::device_ptr中,并将其传递给推力算法。由于调用中提供的输入序列的类型,推力中基于标签的模板模型将确保设备执行结果。
#include <thrust/device_ptr.h>
#include <thrust/reduce.h>
int* X;
cudaMalloc((void **)&X, sizeof(int) * size_t(N));
// Do stuff with X
thrust::device_ptr X_ptr(X);
int result = thrust::reduce(X_ptr, X_ptr+N);从推力1.7开始,引入了执行策略的概念。这样就不再需要使用device_ptr显式包装设备地址。因此,您可以使用to thrust::device策略来指示输入迭代器在设备上,并执行以下操作
#include <thrust/reduce.h>
#include <thrust/execution_policy.h>
int* X;
cudaMalloc((void **)&X, sizeof(int) * size_t(N));
// Do stuff with X
int result = thrust::reduce(thrust::device, X, X+N);你选择哪种方式来做这件事,应该根据你拥有的推力版本和你喜欢的代码风格来决定。
https://stackoverflow.com/questions/37095883
复制相似问题