Exceptions for using global variables in parfor loops

3 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2020-8-8
Put the parameters in one structure and pass that as an input.
tom3w
tom3w 2020-8-8
Thanks, this option will indeed definitively work. I'm still examining if it's still possible to use global variables for this purpose; if this is not the case, I'll probably implement this.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2020-8-8
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 个评论
Walter Roberson
Walter Roberson 2020-8-8
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...
tom3w
tom3w 2020-8-9
Thank you Walter for the suggestions. I'll probably use input structures to replace global variables initialized before the parfor loop and, as a complement, initialize "global" variables at the worker level for a local use at that level.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by