Which loop to run in parallel if I have three of them?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Alex
am 17 Mär. 2014
Beantwortet: Kevin Claytor
am 17 Mär. 2014
Assuming I have a code like this:
for ii=1:51
do something
for jj=1:100
do something
for kk=1:200
do something
end
end
end
I have parallel programming toolbox and want to run the code on all processors available. Which loop should I change to parfor? Or maybe all three?
Please, give a detailed explanation.
0 Kommentare
Akzeptierte Antwort
Kevin Claytor
am 17 Mär. 2014
It's going to depend.
Specifically, where is the heavy lifting done? If you have code that runs fast on a single CPU, it may be anti-productive to wrap that in a parfor loop as you'll have to distribute and re-gather the data every time.
I would suggest coming up with a test case and using tic and toc to time how long it takes for parfor in each of the cases.
tic;
parfor ii=1:51
do something
for jj=1:100
do something
for kk=1:200
do something
end
end
end
a = toc;
tic;
for ii=1:51
do something
parfor jj=1:100
do something
for kk=1:200
do something
end
end
end
b = toc;
tic;
for ii=1:51
do something
for jj=1:100
do something
parfor kk=1:200
do something
end
end
end
c = toc;
fprintf('parfor in 1st loop: %ds\nparfor in 2nd loop: %ds\nparfor in 3rd loop: %ds\n',a,b,c);
Also have a look at the matlab profiler to see which functions take longer to run, and where you should be spending your time optimizing your code.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements 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!