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
0 个评论
采纳的回答
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
更多回答(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!