global variables - call function - parfor

조회 수: 2 (최근 30일)
Hugo
Hugo 2014년 5월 15일
댓글: Edric Ellis 2014년 5월 15일
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.

답변 (3개)

Edric Ellis
Edric Ellis 2014년 5월 15일
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).

Hugo
Hugo 2014년 5월 15일
편집: Hugo 2014년 5월 15일
Hi,
Thank you for your answer. At the beginning of the code I calcul a huge matrix then use its coefficients in my differential equation. The problem is that calculate the matrix takes time however it will remain a constant during the for loop. This is why a global variable would be great...!
Any idea ? I was wondering if I can put more than t and y as parameter in my equation ?
Thank you,
Hugo
  댓글 수: 1
Edric Ellis
Edric Ellis 2014년 5월 15일
You can avoid transferring the data multiple times by taking advantage of the Worker Object Wrapper.

댓글을 달려면 로그인하십시오.


Hugo
Hugo 2014년 5월 15일
Yeah it works, I can put my matrix as a variable of my ode.
thank you,

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by