필터 지우기
필터 지우기

How to run arrayfun with a GPU?

조회 수: 1 (최근 30일)
Greg
Greg 2012년 6월 1일
Using MATLAB version 7.14.0.739 (R2012a) and the Parallel Computing Toolkit ver 6.0, I've been unable to get a simple test case to run.
The file "getHighRandValue.m" has the code
function [highvalue] = getHighRandValue(outerbound)
dist=randn(outerbound,1);
% dist=parallel.gpu.GPUArray.randn(outerbound,1);
highvalue=max(dist);
end
and the file "testarrayfun.m" has the code
outerbound = parallel.gpu.GPUArray.colon(1,10);
for i=1:length(outerbound)
outerbound(i)=2*i;
end
[highvalue]=arrayfun(@getHighRandValue, outerbound);
highvalue=gather(highvalue)
Running this script gives the message
Error using parallel.gpu.GPUArray/arrayfun
Too many arguments supplied to: 'randn'. error at line: 2
Error in testarrayfun (line 5)
[highvalue]=arrayfun(@getHighRandValue, outerbound);
I don't understand this error messages in this context. The Help file seems to support this usage of randn. Anyway, replacing line #2 in "getHighRandValue.m" (by toggling the comments) with
dist=parallel.gpu.GPUArray.randn(outerbound,1);
Gives a different error message:
Error using parallel.gpu.GPUArray/arrayfun
Undefined function or variable 'outerbound'.
Error in testarrayfun (line 5)
[highvalue]=arrayfun(@getHighRandValue, outerbound);
As before, I don't understand the error message in this context. Any help getting this simple example to work would be most appreciated.
Greg

채택된 답변

Narfi
Narfi 2012년 6월 2일
Inside arrayfun, one can only perform computations with scalars, so vector and matrix computations are not supported. In particular, one cannot create random matrices.
Having said that, you can easily write loops, create multiple random numbers, etc., so the equivalent functionality of the example code would be achieved via:
function highvalue = getHighRandValue(outerbound)
highvalue = -inf;
for i = 1:outerbound
highvalue = max(highvalue, randn());
end
end
Best,
Narfi
  댓글 수: 1
Greg
Greg 2012년 6월 4일
Thank you. It helps a lot to understand.
The implications of "arrayfun can perform computations only with scalars" don't bode well for my real application, and it's distressing that I don't see this restriction documented with the PCT. But even if it's not what I wanted to hear, your explanation is most helpful.
Regards.
Greg

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 GPU Computing in MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by