GetGlobal() and parfor loop
Ältere Kommentare anzeigen
Hello everyone,
I am using the setGlobal() and getGlobal() functions in a code that worked pretty well initially but doesn't work now that I'm trying to parrallelize it.
I noticed that the functions setGlobal() and getGlobal() struggle to work with the parfor loop. or example, when I run
setGlobalF([2 3 4])
% j = getGlobalF();
parfor i = 1:4
j = getGlobalF()
end
the j value is not assigned to [2 3 4].
Have you guys any ideas of why and how should I proceed ? Thanks !
Antworten (1)
Walter Roberson
am 13 Jul. 2022
1 Stimme
Global and persistent variables are never copied to workers.
You need to do one of two things:
- run the setGlobalF command on each worker, such as using parfevalOnAll
- use parallel.pool.Constant to push a constant value into the workers, that you can then access.
Note that any setGlobalF that you do on a worker will not affect the other workers. If you need to be able to affect the other workers, then you should either use spmd with labSend and labReceive or else use parallel.pool.DataQueue or parallel.pool.PollableDataQueue to send data from the workers to the client and then the client would send the updated values to the other workers.
Kategorien
Mehr zu Parallel for-Loops (parfor) finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!