How to run arrayfun with a GPU?

2 views (last 30 days)
Greg
Greg on 1 Jun 2012
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

Accepted Answer

Narfi
Narfi on 2 Jun 2012
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 Comment
Greg
Greg on 4 Jun 2012
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!