简体   繁体   English

计算具有推力的设备阵列的归约和

[英]Compute reduction sum of a device array with thrust

I know we can compute sum of a CPU(host) array with thrust like this.我知道我们可以用这样的推力计算 CPU(主机)数组的总和。

int data[6] = {1, 0, 2, 2, 1, 3};
int result = thrust::reduce(data, data + 6, 0);

Can we find sum of GPU array with thrust without cudaMemcpy to CPU array?我们可以在没有cudaMemcpy的情况下找到 GPU 阵列的总和到 CPU 阵列吗?
Suppose I have a device array created using cudaMalloc like this,假设我有一个像这样使用cudaMalloc创建的设备数组,

cudaMalloc(&gpuspeed, n* sizeof(int));  

and did modifications to gpuspeed with some kernels.并使用一些内核对gpuspeed进行了修改。 Now can I find sum of that with thrust?现在我可以找到推力的总和吗? If we can, what changes I have to make?如果可以,我必须做出哪些改变?

Yes, you can do that with thrust.是的,你可以用推力做到这一点。

You can pass device pointers to thrust, and thrust will do the right thing if you specify explicitly the device execution path, using thrust execution policies.您可以将设备指针传递给推力,如果您使用推力执行策略明确指定设备执行路径,推力将做正确的事情。

Alternatively, you can use thrust::device_ptr to refer to your data, and thrust will also do the right thing, even without explicitly specifying the device execution path.或者,您可以使用thrust::device_ptr来引用您的数据,推力也会做正确的事情,即使没有明确指定设备执行路径。

This answer covers both approaches, albeit with inclusive_scan . 这个答案涵盖了这两种方法,尽管使用了inclusive_scan

Here is an example:这是一个例子:

$ cat t137.cu
#include <thrust/reduce.h>
#include <thrust/device_ptr.h>
#include <thrust/execution_policy.h>
#include <iostream>

__global__ void k(int *d, int n){
  int idx = threadIdx.x+blockDim.x*blockIdx.x;
  if (idx < n)
    d[idx] = idx;
}
const int ds = 10;
const int nTPB = 256;
int main(){

  int *d, r1, r2;
  cudaMalloc(&d, ds*sizeof(d[0]));
  k<<<(ds+nTPB-1)/nTPB,nTPB>>>(d, ds);
  thrust::device_ptr<int> tdp = thrust::device_pointer_cast(d);
  r1 = thrust::reduce(tdp, tdp+ds);
  r2 = thrust::reduce(thrust::device, d, d+ds);
  std::cout << "r1: "  << r1 << " r2: " << r2 << std::endl;
}
$ nvcc -std=c++14 -o t137 t137.cu
$ ./t137
r1: 45 r2: 45
$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM