Manipulating arrays within GPU arrayfun
조회 수: 20 (최근 30일)
이전 댓글 표시
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?
댓글 수: 0
채택된 답변
Joss Knight
2016년 4월 28일
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.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 GPU Computing in MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!