MATLAB: Communicating between parfor loop iterations

global optimizationmulti-startparallel computing

I am using a parfor loop (Matlab parallel tool box) as part of a multi-start technique to look for a global minimum to a derivative free optimization problem. The problem I am facing is that I need some small communication between the loop iterations, to determine if an iteration is converging to a local minimum that is higher than another so that it can cease what it is doing and move on to another starting point. I know that all of the iterations are independent, so I what I want is for each loop iteration to read/write to a common file so that at a certain point in the algorithm, the each individual loop will open the file, compare what it's function value is to what is already in the file, and either proceed or terminate. Ideally, the final result in the file will be the minimum point found from all of the iterations, but what I actually get is the best point of the last iteration to finish. It seems that each loop iteration is writing over the previous information, or that with every loop, the file re-initiallizes and is a blank canvas for the taking, so to speak.
(I have already built in a 'lock' type function that should prevent two loops from opening the file at the same time to eliminate any race conditions.)
Any help would be greatly appreciated and thanks in advance.

Best Answer

I don't think there's a reliable way to achieve this with PARFOR. I would be tempted to investigate SPMD which does allow communication. For example, you could do something like this:
spmd
while ~done
myResult = fcn(...);
% use GCAT to get results from all labs onto
% each lab - after this, 'allResults' is a vector
% of length NUMLABS containing all 'myResult' values
allResults = gcat( myResult );
if myResult == max( allResults ) % I am worst
% do something else
end
% Note that all labs compute the same result
% for 'done'
done = any( abs( allResults ) < tolerance );
end
end
Related Question