global variables - call function - parfor
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi all,
I am trying to parallelize my code which basically solve odes with different boundary condition. The problem is that my equations need global variable which seems to be reset when calling a parfor loop. I use matlab R2014a and here is an example of code that (I think) should work :
Main program :
global a
a=8;
b=zeros(10,1);
parfor i=1:10
b(i,1)=pluss(i);
end
b
function pluss :
function val=pluss(x)
global a
whos a
val=x+a;
It works fine with a for loop however with parfor the
who's a
gives as an answer
Name Size Bytes Class Attributes
a 0x0 0 double global
so the program stop running.
Does somebody has an idea how could I make this variable 'a' a 'real' global variable ?
thank you in advance,
Hugo P.
0 comentarios
Respuestas (3)
Edric Ellis
el 15 de Mayo de 2014
Global variables are not synchronized between the desktop MATLAB and the workers, as described in the documentation. You should aim to refactor your code to remove the need for globals. (Perhaps you could post a simple reproduction showing why you currently need to use globals).
0 comentarios
Hugo
el 15 de Mayo de 2014
Editada: Hugo
el 15 de Mayo de 2014
1 comentario
Edric Ellis
el 15 de Mayo de 2014
You can avoid transferring the data multiple times by taking advantage of the Worker Object Wrapper.
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!