Parfor becomes serial half-way through computation

1 view (last 30 days)
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.

Answers (1)

Edric Ellis
Edric Ellis on 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

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!