Parfor becomes serial half-way through computation
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!