'parfor' slower than 'for' for simple tutorial example
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Fabio
am 16 Mär. 2012
Bearbeitet: John Kelly
am 29 Sep. 2017
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 Kommentar
Roberto
am 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
Akzeptierte Antwort
Edric Ellis
am 16 Mär. 2012
Bearbeitet: John Kelly
am 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 Kommentare
Roberto
am 27 Apr. 2014
Bearbeitet: Roberto
am 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
am 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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Parallel Computing Toolbox finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!