MATLAB: Global variables Pattern Search

global optimisation toolboxGlobal Optimization Toolboxglobal variableoutput functionParallel Computing Toolboxpattern search

Hi!
i'm using Pattern Search with a custom output function that at the end each iteration is updating a global variable. Is it true that if i'm activating the parallelisation of the PS i cannot use global variables?

Best Answer

No, that is not true. You can use global variables with parallelisation.
However, the initial value of the global variables will not be transferred to the workers, and the workers do not share the global variable with each other -- you cannot use global to communicate between the workers. Also, the value of the global variables will not be copied back from the workers to the client.
In order to initialize or retrieve the global variables, you end up needing to use parfevalOnAll.
global variables are global to their process, but are not shared between processes.
If you just need to initialize something on all workers and the value does not need to be changed, then consider using https://www.mathworks.com/help/parallel-computing/parallel.pool.constant.html