The existance of any parfeval job blocks the execution of a parfor loop. This occurs even when available pool workers are available, and should be free to execute the parfor loop.
On my machine, with 32 cores, running this:
function testParallelBlockingnIter = 3;pool = gcp;j = parfeval(pool,@backgroundFcn,1);for i = 1:nIter forLoopTime(i) = now;endparfor i = 1:nIter parforLoopTime(i) = now;enddisp(['Parfeval completed at ' datestr(j.fetchOutputs,'HH:MM:SS:FFF')]);for i = 1:nIter disp(['For loop completed at ' datestr(forLoopTime(nIter),'HH:MM:SS:FFF')]);endfor i = 1:nIter disp(['Parfor loop completed at ' datestr(parforLoopTime(nIter),'HH:MM:SS:FFF')]);endendfunction out = backgroundFcnpause(5);out = now;end
produces this output:
Parfeval completed at 15:15:25:244For loop completed at 15:15:20:232For loop completed at 15:15:20:232For loop completed at 15:15:20:232Parfor loop completed at 15:15:25:269Parfor loop completed at 15:15:25:269Parfor loop completed at 15:15:25:269
showing that while the rest of the code continues to run, the parfor loop does not begin until the parfeval job has completed.
Is there any way to have the parfor loop execute while the parfeval job is also running in the background? I have tried limiting the number of parfeval workers by changing
parfor i = 1:nIter
to
parfor (i = 1:nIter,nWorkers)
where nWorkers < (number of cores – number of pending parfeval jobs) but it does not change the behavior.
Thanks for your help.
Best Answer