What you can do is run a whole batch, accumulate each pass's independent cc
indicator in an array, and then combine the results outside the parfor
to see if you need to continue. This keeps each pass of the loop independent of the results of other passes, so it can be done in parallel.
n = 1000;
cc = 0;
isDone = 1;
while ~isDone
ccs = zeros(1, n);
parfor i=1:n
if (abs(i) <= gamma)
% ...etc..
ccs(i) = 1;
end
end
cc = cc + sum(ccs);
isDone = cc >= 100;
end
This will waste some work. You can adjust n
to change the batch size and control the tradeoff between wasted work (due to "fragmentation" in the last batch where cc
would have made it to 100 with a smaller batch) vs. the overhead of parallelization.