Improving parfor tasks distribution

8 views (last 30 days)
Hello,
I have the feeling that the way parfor distributes tasks over the workers may be suboptimal in some cases. Here is a dummy example to explain the problem I encountered: Let say I have 22 tasks to perform in parallel and 10 cores available. Now, for some external reasons (not related to Matlab in any way), I had to stop the code while only 8 tasks were completed. It gets a bit more tricky when I want to start things over.
listTask=[T1,..,..,T22];
parfor iTask=1:numel(listTask)
if listTaks(iTask) not performed yet
perform listTask(iTask)
end
end
So when I restart the program, Matlab naturally starts tasks T9 and T10... but not T11 to T18. It works on only two cores while 8 more are doing nothing. 80% of my computer ressources is idling. To be able to start Matlab over the 10 cores again, the only way I found is to directly edit the listTask variable such as
listTask=[T9,..,..,T22];
So in conclusion, if I restart the program from the 9th task, Matlab will take 3 batches (T9 to T10, then T11 to T20, and finally T20 to T22) to finish the job while 2 would be enough! Is there a way to force Matlab to use all cores available?
Thanks!

Accepted Answer

Edric Ellis
Edric Ellis on 16 Feb 2018
parfor scheduling is designed to work best when iterations are roughly similar in execution time. There is some consideration given to unequal workloads, but in your case it sounds like the workloads can be of drastically different durations (i.e. because they might actually already be complete).
In your case, you might be better served using parfeval as this allows you to specify the scheduling yourself. (Each parfeval request is sent separately to a free worker for processing).
  1 Comment
Antoine Thiboult
Antoine Thiboult on 16 Feb 2018
Thanks Edric. Here a small code that proves that you are right.
I've 16 cores on my machine and I build a 160x1 list of tasks but only 16 of them are to be computed. The tasks consist of waiting 5 seconds... So it should take a little more than 5 seconds to be performed.
clear
clc
% Seed
rng(1)
% Create a list of tasks (if 1 compute taks, 0 don't)
listTasks=zeros(1,16*10);
idTask=floor(rand(1,16)*numel(listTasks))+1;
listTasks(idTask)=1;
% Perform and time tasks
tic
parfor iTask=1:numel(listTasks)
if listTasks(iTask)==1
pause(5)
end
end
t=toc;
disp(evalc('feature(''numcores'')'));
fprintf('%i tasks performed in %0.3f s\n',sum(listTasks), t)
>>
MATLAB detected: 16 physical cores.
MATLAB detected: 32 logical cores.
MATLAB was assigned: 32 logical cores by the OS.
MATLAB is using: 16 logical cores.
MATLAB is not using all logical cores because hyper-threading is enabled.
ans =
16
16 tasks performed in 15.119 s

Sign in to comment.

More Answers (0)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!