MATLAB: Improving parfor tasks distribution

parfor

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!

Best Answer

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).