如何从 GPU 上的 3D 数组输出一个向量,该向量是每个切片或页面的总和

How to output a vector that is the sum of each slice or page from a 3D array on GPU

我试图在 Matlab 中完成的过程:

  1. 向 GPU 发送 3D 数组
  2. 将每个切片或 'page' (:,:,i) 分配给 GPU 上的处理器
  3. 输出这些求和的向量
  4. Return 指向 CPU

    的向量

    % My stab at it: Array = gpuArray(ones(3,3,5)); Array = pagefun(@sum,array); Array = gather(Array); % Desired output: Array = 1x1x5 vector of 9's

这会抛出pagefun不喜欢求和函数的错误。

在 CPU 上,类似的过程工作得很好。它也适用于 GPU 上的 FOR 循环,但这并没有将过程矢量化以获得理想的速度。 CUDA 内核会更能做这样的事情吗?有一个更好的方法吗?这是否更适合集群而不是 GPU?

感谢帮助,Will

设置:ASUS i7 四核,GTX Geforce 960 运行 CUDA 驱动程序

好吧,你可以尝试更丑陋的方式:

Array = reshape(sum(Array(:,:)), 1, 1, k); %k is whatever third dimension is 

我找到了解决方案并将其发布到 matlab 论坛 here

这是我在 MATLAB answers 上发布的答案。

array = ones(3, 3, 5, 'gpuArray');
result = sum(reshape(array, [], size(array, 3)));
result = gather(reshape(result, 1, 1, []));