Why is Arrayfun so much faster than a for loop when using a GPU?

Can anyone say why Arrayfun is much faster than a for loop on a GPU? (not on the CPU, actually the for loop is faster on the processor)

Arrayfun:

x = parallel.gpu.GPUArray(rand(512,512,64));
count = arrayfun(@(x) x^2, x);

And the equivalent of For loop:

for i=1:size(x,1)*size(x,2)*size(x,3)
  z(i)=x(i).^2;        
end

Perhaps this is because the For loop is not multithreaded on the GPU? Thank.

+3
source share
2 answers

I do not think your loops are equivalent. It seems you are combining each element into an array with a processor implementation, but doing some sort of count for arrayfun.

Despite this, I think the explanation you are looking for is as follows:

GPU - - . , i [cell_i]^2 . , S-, S - , GPU. . , .

, , : * array_fun * , , . , , , . , .

+3

http://www.mathworks.co.uk/help/toolbox/distcomp/arrayfun.html, MATLAB, , , GPU ". for , - ​​ arrayfun - GPU.

+1

All Articles