negfrequency
negfrequency

Reputation: 1897

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

My process I am trying to accomplish in Matlab:

  1. Send a 3D array to the GPU
  2. Distribute each slice or 'page' (:,:,i) to a processor on the GPU
  3. Output a vector of these summations
  4. Return the vector to the 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

This throws an error that pagefun does not like the summation function.

On the CPU, a similar process works just fine. It works in a FOR loop as well on the GPU, but this does not vectorize the process for ideal speed. Would a CUDA kernel be more able to do something like this? Is there a better way to do this? Is this more suitable for a cluster rather than a GPU?

Help appreciated, Will

Setup: ASUS i7 quadcore, GTX Geforce 960 running CUDA driver

Upvotes: 1

Views: 100

Answers (3)

Edric
Edric

Reputation: 25160

Here's my answer that I also posted on MATLAB answers.

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

Upvotes: 0

IESTIMATED
IESTIMATED

Reputation: 1

I found the solution and posted it to the matlab forums here

Upvotes: 0

Pavan Yalamanchili
Pavan Yalamanchili

Reputation: 12109

Well, you could try out the uglier way:

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

Upvotes: 2

Related Questions