Communicating between parfor loop iterations
5 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Edric Ellis
on 16 Apr 2012
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
0 Comments
More Answers (2)
Eric
on 7 Sep 2013
Hi Brian,
I am struggling with a similar issue of desiring a very small amount of communication in a parfor loop. Could you share which system commands you used that were able to create lock files without any race conditions?
Thanks! Eric
0 Comments
See Also
Categories
Find more on Parallel Computing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!