Read/Write Limitations with Parallel Computing Toolbox (parfor)
Show older comments
I am working on implementing Parallel Computing Toolbox (specifically parfor) to a pre-existing program, but I am noticing some potential concerns and cannot find many resources online regarding it. Do any of these functions risk read collisions? fileparts, isempty, eval (when calling a MATLAB file directly)
Additionally, with diary, if you start diary before the parfor loop, will all workers log? And if you start a log in the worker (with a custom name between all workers), would each worker log individually?
Accepted Answer
More Answers (1)
Walter Roberson
on 16 May 2023
0 votes
parfor does not use shared memory, at least not classically. It uses java queuing operations to transfer data between workers, with it being unspecified (at least at the MATLAB level) exactly how Java does its transporting.
The model is that each worker gets a read-only copy of any variable that is not detected as being an output-only or input-output object. The only supported output is by indexing by the loop control index, or sometimes by appending (see reduction variables.) The loop control index operations are examined at parse time to ensure that the indexed segments cannot overlap.
fileparts and isempty would be operating on sliced or distributed variables, or on local variables, with updates from other workers not possible. (And if you use a dataqueue to send values between client and worker then you are getting an unshared copy of the data.)
eval() is not permitted directly in parfor. eval() is permitted within a function invoked from within a parfor loop -- and since it is inside a function it would not have access to any variables that had not been passed into or created in the function (though I would have to think more about how shared variables work in this context.)
Categories
Find more on Parallel for-Loops (parfor) 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!