MATLAB: Is it possible to determine the cpu capacity of each parfor worker

parallel computingParallel Computing Toolbox

Hello:
Is it possible to determine the cpu capacity of each parfor worker?
The reason of my question is the following:
I have a code with 100 iterations. In each iteration a lot of simulations must be performed, for which I am using a parfor loop in order to distribut the simulations among the parfor workers, so that I can reduce the total iteration time. My problem is that as iterations passes, the number of simulations that must be performed on each iteration decreases and maybe some workers does not have to perform a simulation in that iteration. Therefore, I would like to transfer the cpu capacity of the workers that are sleeping to those who are not.
The summary of my code is bellow. My problem is that while in the first iterations the "total_simulations_to_perfor_in_the_iteration" are higher than the number of workers, in the previous iterions this number reduces and some workers are not used.
for iteration_index=1:100
total_simulations_to_perfor_in_the_iteration=compute_no_of_simulations_required (input_data);
parfor simulation_index=1:total_simulations_to_perfor_in_the_iteration
%code for running the simulations
end
end
Thanks in advance,
David

Best Answer

parfor() breaks up the iteration range into 3 groups. In the first pass, it assigns each worker an equal share of the last 2/3 of the possible iterations (later iteration numbers are done first as an optimization on array allocation.) As each worker finishes, it is assigned a chunk which is an equal share of roughly the middle 1/4-ish of possible iterations. When those run out, each worker is assigned a chunk which is a small number of iterations. Somewhere in Answers I posted a more detailed analysis of the allocation algorithm in practice, as then implemented... ah, it was https://www.mathworks.com/matlabcentral/answers/341778-how-matlab-divide-the-number-of-iterations-of-parfor-on-the-workers-of-one-computer#comment_456761
In cases where this does not load balance well enough for your purposes, see https://www.mathworks.com/matlabcentral/answers/65350-why-does-parfor-execute-loops-in-a-random-order#comment_133329 or use parfeval()