error with arrayfun and GPU computing part 2.
조회 수: 1 (최근 30일)
이전 댓글 표시
I am tring to compute simple example on GPU:
function C=myf(A,B,N)
for i=1:N
C(:,:,i)=(A(:,:,i)*B)^10;
end
end
With CPU it works:
M = 100;
K = 100;
N = 1000;
A=rand(M,K,N);
B=rand(K,M);
C=myf(A,B,N);
With GPU:
if true
Aongpu=gpuArray(A);
Bongpu=gpuArray(B);
C=arrayfun(@myf,Aongpu,Bongpu,N);
end
It doesn't:
Error using parallel.gpu.GPUArray/arrayfun Matrix dimensions must agree.
Why?? Thanks in advance
댓글 수: 1
Geoff Hayes
2014년 10월 31일
Mikhail - I'm not sure if using arrayfun in this manner is appropriate/correct. The documentation indicates
Nonsingleton dimensions of input arrays must match each other. In other words, the corresponding dimensions of arguments B, C, etc., must be equal to each other, or equal to one. Whenever a dimension of an input array is singleton (equal to 1), arrayfun uses singleton expansion to virtually replicate the array along that dimension to match the largest of the other arrays in that dimension…
Given the examples (from the above link), this seems to suggest that your B and N would be expanded to MxKxN arrays, and your myf function would be then applied to each element within these identically sized inputs. (This is similar to the non-GPU version of arrayfun where each array input must be of the same dimension.)
채택된 답변
Edric Ellis
2014년 10월 31일
When working with gpuArray data, the function that you pass to the arrayfun will be called with scalar values. In other words, your myf needs to accept scalars and return scalars, like so:
M = 100;
K = 100;
N = 1000;
A=rand(M,K,N);
B=rand(K,M);
myf = @(a, b, n) a + b + n % or something more appropriate
arrayfun(myf, gpuArray(A), gpuArray(B), N);
추가 답변 (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!