Manipulating arrays within GPU arrayfun

4 views (last 30 days)
I am trying to use GPU arrayfun to perform computations on various subsets of a large array. However it appears that manipulating arrays is not supported by GPU arrayfun. The following minimal example gives the error "Function passed as first input argument contains unsupported or unknown function 'colon'".
function test()
bigarray = rand(5,5);
param = [1 2 3 4];
param = gpuArray(param); %CPU version gives no error
function res=func(p)
subarray = bigarray(p:p+1,1:5);
res = sum(sum(subarray));
end
arrayfun(@func,param)
end
Is there a workaround to the problem?

Accepted Answer

Joss Knight
Joss Knight on 28 Apr 2016
The general rule is that you cannot manipulate matrices or vectors inside a gpuArray arrayfun function, only scalar operations are supported - that rules out colon, and sum I'm afraid. The point of gpuArray arrayfun is to represent an element-wise kernel, so the function represents the operations one GPU thread is carrying out on one element of your input. You can do indexing, on upvalue variables, but only to index a single element. In this sense it is very different from the CPU version - the CPU version is just a syntactical convenience function that avoids having to write loops.
Usually there is a vectorised version of whatever it is you're trying to do that avoids the need for complex operations inside arrayfun. In your case, you're trying to get hold of the sum of the data in pairwise columns. Reductions are not good candidates for arrayfun. You want something like:
sumRows = sum(bigarray);
res = sumRows(1:end-1) + sumRows(2:end);
If you really really need to do the CPU-like thing on a gpuArray, then just write out the nested for-loops - it will perform just the same.

More Answers (0)

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!