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

18 views (last 30 days)
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 Comment
Roberto
Roberto on 27 Apr 2014
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

Sign in to comment.

Accepted Answer

Edric Ellis
Edric Ellis on 16 Mar 2012
Edited: John Kelly on 29 Sep 2017
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 Comments
Roberto
Roberto on 27 Apr 2014
Edited: Roberto on 27 Apr 2014
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 on 22 Sep 2017
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

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!