Parfor becomes serial half-way through computation

1 Ansicht (letzte 30 Tage)
Dio
Dio am 23 Jun. 2019
Beantwortet: Edric Ellis am 24 Jun. 2019
I am running a parfor loop with 9 iterations. My laptop has 6 physical cores.
At first it runs 6 jobs in parallel, as expected. Once that is done, it begins to serially run the remaining 3 jobs 1-by-1 rather than running them on 3 cores in parallel (while idling the other 3).
Why is it not running the last 3 jobs in parallel on 3 cores?
Thank you.

Antworten (1)

Edric Ellis
Edric Ellis am 24 Jun. 2019
parfor uses heuristics to try and divide up loop execution into sub-ranges to maximise worker utilisation, but it sounds like the default strategy isn't working well for you. This can happen when you have a smallish number of loop iterations. Here's what I tried in R2019a on my 6-core machine, both in default mode and using parforOptions (new in R2019a) to explicitly control the subrange selection:
%% Get or create a parallel pool
pool = gcp();
if isempty(pool)
pool = parpool('local', 6);
end
%% Run a |parfor| loop, and time it.
% Here we're simply using the default loop division strategy.
t = tic();
parfor idx = 1:9
pause(1);
end
elapsedDefault = toc(t)
%% Use |parforOptions|
% Here we're forcing the loop to be divided into exactly 9 subranges
% each of 1 iteration
t = tic();
opts = parforOptions(pool', 'RangePartitionMethod', 'fixed', ...
'SubrangeSize', 1);
parfor (idx = 1:9, opts)
pause(1);
end
elapsedFixed = toc(t)
For me, I got the following results showing that in fact the default strategy seems to work OK...
elapsedDefault =
2.0460
elapsedFixed =
2.0275

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by