Global variables in parfor loop

I am using global variables in parfor loop and I get following m-lint
The code uses a global variable in a parfor loop. Because parfor loops run on several different machines simultaneously, the global workspace might not be the same on each machine. Therefore, using a global variable in a parfor loop could have unpredictable or unexpected results and Code Analyzer flags it as an error.
This is my code:
r=cell(size(sample,1),1);
parfor samp=1:size(sample,1)
theta_opt=theta_opt_array(samp,:);
Cp_opt=diag(Cp_opt_array(samp,:));
beta_opt=beta_opt_array(samp);
sigmaobs2_opt=sigmaobs2_opt_array(samp);
mu=strm(:,samp);
n=10000;
Ct_opt=CovarianceMatrixEstimation(Cp_opt,theta_opt,sigmaobs2_opt,...
GLOBAL_DATA,GEOMORPH); % these two are global variables
r{samp}=mulgennormrnd(n,mu,Ct_opt,beta_opt);
end
Any suggestions on how to fix it? I don't understand the suggested fix by MATLAB. The suggestion by MATLAB is:
Make local copies of the global variables that you want to use within the parfor loop before beginning the parfor loop
Also,I am not changing my global variables in parfor loop.

 Respuesta aceptada

Adam Danz
Adam Danz el 29 de Jun. de 2018
Editada: Adam Danz el 29 de Jun. de 2018

0 votos

What was the suggestion by Matlab? Have you read through the plethora of answers to this topic?
There are lots of reasons not to use global variables at all and this is one of them. As the error message indicates, parfor() is executed in parallel potentially between >1 machine. Global variable are not accessible between machines. So you'll have to initialize them independently within your code.

6 comentarios

Abhinav
Abhinav el 29 de Jun. de 2018
All the existing answers are about not using global variables. I need to use global variables. The suggestion by matlab:
Make local copies of the global variables that you want to use within the parfor loop before beginning the parfor loop.
Adam Danz
Adam Danz el 29 de Jun. de 2018
Editada: Adam Danz el 29 de Jun. de 2018
Sometimes global variables are necessary. Matlab's suggestion matches the many suggestions of other people who had this problem (if you search this forum of 'global variables in parfor' you'll see).
As matlab suggests, you need to make a local copy of the global variable. Local means within the code that calls parfor().
If your global variable is named 'GEOMORPH' ....
geoMorph = GEOMORPH; %reassign global to local without re-writing it.
parfor samp=1:size(sample,1)
x = myfunc(...,geoMorph,...)
end
Alternative solutions might exist in this forum and you're encouraged to read through the solutions found by others. Check out the link I provided in my answer.
Steven Lord
Steven Lord el 29 de Jun. de 2018
I need to use global variables.
Why?
If you're doing it to let each iteration update those common global variables (to pass along information to the next) you're violating one of the key requirements of parfor, loop iteration independence. In that case, just use for.
Abhinav
Abhinav el 29 de Jun. de 2018
I am not updating global variables in the loop. I have two scripts which need to interact with each other.
Steven Lord
Steven Lord el 29 de Jun. de 2018
So your mulgennormrnd function needs information from the CovarianceMatrixEstimation call that took place earlier in the same loop iteration (not cross-iteration)?
The simplest approach, and the one that makes it clear that you're not trying to share data across iterations, would be to have your CovarianceMatrixEstimation function return that information as additional outputs and have your mulgennormrnd function accept that information as additional inputs.
Abhinav
Abhinav el 29 de Jun. de 2018
yes, the output of covarianceMatrixEstimation is used in same iteration, not cross-iteration. Thanks a lot for your suggestion!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 29 de Jun. de 2018

Comentada:

el 29 de Jun. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by