Exceptions for using global variables in parfor loops
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
tom3w
el 8 de Ag. de 2020
Comentada: tom3w
el 9 de Ag. de 2020
Hi,
I've read on Matlab central that Matlab does not allow the use of global variables in a parfor loop. Though, there are still some cases for which "global" variables would be of high interest at the individual worker level/parfor iteration.
Example: I'd like to execute a code for several independent entities (one entity per parfor iteration), and this code is rather complex, relying on nested functions and a large set of parameters that I store in global variables for the sake of easiness (much easier than passing all parameters as function inputs).
More specifically, I accept the fact that global variables defined at the worker level/for a given iteration could not be used afterwards at the client level or at another worker level/another iteration (it would be meaningless in the case of parallel computing), but I'm very embarassed by the fact that global variables can't be defined at the worker level within each given iteration, for a use only at that specific worker level/iteration. In other words, I'm talking here about "global variables" only valid at the worker level, which means that the 'global' terminology does not hold in the classic sense, but still holds regarding the way such variables can be used (e.g. defining and then calling them in separate functions).
Is it possible to force Matlab to allow this use? If not, is there already a patch allowing it or can it be foresee in a next release (hopefully soon)?
Thanks and cheers,
2 comentarios
Respuesta aceptada
Walter Roberson
el 8 de Ag. de 2020
Is it possible to force Matlab to allow this use?
NO
If not, is there already a patch allowing it or can it be foresee in a next release (hopefully soon)?
NO. This is unlikely to be supported any year soon.
The work-around is not difficult: put the reference to the global variables inside of functions that are called within the parfor body. As you indicated, you would still have to manage the fact that the global variables apply per-worker.
3 comentarios
Walter Roberson
el 8 de Ag. de 2020
Yes, correct, each worker will have the globals initialized to empty, no matter what value the variable might have had in the client.
You can use parfevalOnAll() to run code on each of the workers to initialize them. That might be most easily done by passing in a struct and setting the global variables according to the structure member names. Not generally recommended coding style, of course, but then using global is not recommended coding style either...
Más respuestas (0)
Ver también
Categorías
Más información sobre Parallel for-Loops (parfor) en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!