How to run arrayfun with a GPU?

2 次查看(过去 30 天)
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