Parfor becomes serial half-way through computation

조회 수: 6 (최근 30일)
Dio
Dio 2019년 6월 23일
답변: Edric Ellis 2019년 6월 24일
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.

답변 (1개)

Edric Ellis
Edric Ellis 2019년 6월 24일
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

카테고리

Help CenterFile Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by