MATLAB: How are iterations assigned to workers in parfor

MATLABParallel Computing Toolboxparfor

I am currently using parfor to process multiple raw data files, in the statement, it first checks if the raw file have already been processed, and only process if it does not see an existing output, like this:
RawDatalist=dir(fullfile(RawDataFolder,'*.txt'));
NumRawData=length(RawDatalist);
parfor i =1:NumRawData
if %output for RawDatalist(i).name already exist
Execute=false;
else
Execute=true;
end
if Execute
%Process RawDatalist(i).name
end
end
Obviously some iterations will take less time than others because there is no calculation involved. I am just wondering if iterations are 1)devided among works at the start of parfor, or 2)handed out one by one once a worker become available? If it's the first case then some workers will be just sitting idle while some others busy working, and I need to move the existance check out of the parfor loop.

Best Answer

As @Mohammad already commented, the parfor implementation automatically divides up the iterations of the loop onto the workers. Since R2019a, you can have some control over this division using parforOptions. The default division works well in most situations, even when the loop iterations do not take equal amounts of time. However, if there is a large imbalance, the division might not work well, and it may indeed be worth pre-computing which iterations need real work to be done.