'parfor' slower than 'for' for simple tutorial example

18 次查看(过去 30 天)
Hi everybody, I'm a phd student in physics and I just started learning something about parallel programming in matlab. Unfortunately from the very beginning I've encountered some problems in running parallelized code on my computer. Just to have a try I used this simple code to run some test:
parfor ii=1:1000
A(ii)=ii;
end
A
If I compare the time elapsed for this calculation to the one of the same code with the normal for loop it appears evident that the parfor loop is much slower. I've tried to use matlabpool with default settings and also varying the number of workers (I’ve also tried to completely close the pool), and i've also tried to combine this with gpuArray preallocating A on the GPU, but simple for loop remains the best choice. Is it an hardware incompatibility? I'm currently using Matlab 2011b with Parallel Computing Toolbox 5.2. In my computer I've an INTEL i7 processor and a NVIDIA GEFORCE 540M. Does it matter?
  1 个评论
Roberto
Roberto 2014-4-27
I've encountered the same issue; here is what I've tried:
n = 50000;
x = randn(1,n) ;
y = zeros(3,n);
tic
for i = 1 : n
y(1,i) = std(x(1:i));
end
fprintf('\n For normal: %f secs\n',toc);
tic
for i = drange(1 : n)
y(2,i) = std(x(1:i));
end
fprintf('\n For drange: %f secs\n',toc);
tic
parfor i = 1 : n
y(3,i) = std(x(1:i));
end
fprintf('\n parFor: %f secs\n',toc);
and here are the results:
For normal: 11.337090 secs
For drange: 11.569059 secs
parFor: 11.552776 secs

请先登录,再进行评论。

采纳的回答

Edric Ellis
Edric Ellis 2012-3-16
编辑:John Kelly 2017-9-29
The problem with your loop is that there just isn't enough work happening in the body. To get the sort of speed-up you might expect, you need each iteration to be much more computationally intensive than that.
The reason for this is that PARFOR works by shipping the body of the loop across to the workers, which are separate MATLAB processes; and then collecting the results. There is overhead to sending the work out, receiving the results, and piecing the overall answers (in your case 'A') back together.
  2 个评论
Roberto
Roberto 2014-4-27
编辑:Roberto 2014-4-27
The problem was solved just 'putting workers to work' with matlabpool
n = 50000;
x = randn(1,n) ;
y = zeros(1,n);
matlabpool('open','10');
tic
for i = 1 : n
y(i) = std(x(1:i));
end
fprintf('\n Normal for: %f secs',toc);
tic
parfor i = 1 : n
y(i) = std(x(1:i));
end
fprintf('\n parFor: %f secs\n\n',toc);
matlabpool('close');
RESULT:
Starting matlabpool using the 'local' configuration ... connected to 8 labs.
Normal for: 11.007981 secs
parFor: 3.672066 secs
Sending a stop signal to all the labs ... stopped.
Walter Roberson
Walter Roberson 2017-9-22
The parallel_demo_parfor_bench does not appear to exist anymore, but there are several parallel benchmark examples at https://www.mathworks.com/help/distcomp/examples.html

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by