Run parfor loop inside a function
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I am trying to parallelize my code (function dfdx presented below), but a warning message : ( The entire array or structure 'th','x' is a broadcast variable this might result in unnecessary communication overhead) appears under th(9) and x(5,i), I am confusing Why does it only under th(9) in df7dx7 and under x(5,i) in df8dx8.
the question : what is the origine of this warning, and how can I handle it.
function [Fx]=dfdx(x,th,gama,T0,a,StateDim,nCpts,dt)
Fx = zeros(StateDim,StateDim,nCpts);
parfor i=1:nCpts
df7dx7 = -x(7,i)^(1/a - 1)/(a*(th(9) + T0));
df8dx1 = (gama*(1/(exp(th(5)*(th(6) - x(1,i))) + 1) - 2))/(T0*(1/(exp(th(5)*(th(6) - 1)) + 1) - 2)*(gama + 1)) + (th(5)*gama*x(1,i)*exp(th(5)*(th(6) - x(1,i))))/(T0*(1/(exp(th(5)*(th(6) - 1)) + 1) - 2)*(exp(th(5)*(th(6) - x(1,i))) + 1)^2*(gama + 1));
df8dx5 = -(th(9)*x(8,i))/(T0*x(7,i)*(th(9) + T0));
df8dx7 = (x(8,i)*(th(9)*x(5,i) + T0*x(7,i)^(1/a)))/(T0*x(7,i)^2*(th(9) + T0)) - (x(7,i)^(1/a - 1)*x(8,i))/(a*x(7,i)*(th(9) + T0));
df8dx8 = -(th(9)*x(5,i) + T0*x(7,i)^(1/a))/(T0*x(7,i)*(th(9) + T0));
Fxi = [0 1 0 0 0 0 0 0;
-th(2)^2 -2*th(2) 0 0 0 0 0 0;
0 0 0 1 0 0 0 0;
0 0 -th(4)^2 -2*th(4) 0 0 0 0;
0 0 0 0 0 1 0 0;
0 0 0 0 -th(8)^2 -2*th(8) 0 0;
0 0 0 0 1/(th(9)+T0) 0 df7dx7 0;
df8dx1 0 1/(T0*(gama+1)) 0 df8dx5 0 df8dx7 df8dx8];
Fx(:,:,i) = eye(StateDim,StateDim) + dt*Fxi;
end
0 Kommentare
Antworten (1)
OCDER
am 31 Aug. 2017
The warning is just stating that the vector variables (th and x) will be copied to every worker for every iteration of the for loop. This could cause a lot of read/write delays, but is sometimes unavoidable like in your case. The editor doesn't know that x and th are actually small and easy to copy around, so it'll just throw a warning instead. To hide the warning, use temporary variables.
https://www.mathworks.com/help/distcomp/broadcast-variable.html
About why only certain th and x get the warning in the editor, the editor doesn't seem to mark all warnings at once - just the (first?) ones that it knows of.
Try this out:
function [Fx]=dfdx(x,th,gama,T0,a,StateDim,nCpts,dt)
Fx = zeros(StateDim,StateDim,nCpts);
parfor i=1:nCpts
TH = th; %Tells matlab that th is a real broadcast variable.
X = x; %Tells matlab that x is a real broadcast variable.
df7dx7 = -X(7,i)^(1/a - 1)/(a*(TH(9) + T0));
df8dx1 = (gama*(1/(exp(TH(5)*(TH(6) - X(1,i))) + 1) - 2))/(T0*(1/(exp(TH(5)*(TH(6) - 1)) + 1) - 2)*(gama + 1)) + (TH(5)*gama*X(1,i)*exp(TH(5)*(TH(6) - X(1,i))))/(T0*(1/(exp(TH(5)*(TH(6) - 1)) + 1) - 2)*(exp(TH(5)*(TH(6) - X(1,i))) + 1)^2*(gama + 1));
df8dx5 = -(TH(9)*X(8,i))/(T0*X(7,i)*(TH(9) + T0));
df8dx7 = (X(8,i)*(TH(9)*X(5,i) + T0*X(7,i)^(1/a)))/(T0*X(7,i)^2*(TH(9) + T0)) - (X(7,i)^(1/a - 1)*X(8,i))/(a*X(7,i)*(TH(9) + T0));
df8dx8 = -(TH(9)*X(5,i) + T0*X(7,i)^(1/a))/(T0*X(7,i)*(TH(9) + T0));
Fxi = [0 1 0 0 0 0 0 0;
-TH(2)^2 -2*TH(2) 0 0 0 0 0 0;
0 0 0 1 0 0 0 0;
0 0 -TH(4)^2 -2*TH(4) 0 0 0 0;
0 0 0 0 0 1 0 0;
0 0 0 0 -TH(8)^2 -2*TH(8) 0 0;
0 0 0 0 1/(TH(9)+T0) 0 df7dx7 0;
df8dx1 0 1/(T0*(gama+1)) 0 df8dx5 0 df8dx7 df8dx8];
Fx(:,:,i) = eye(StateDim,StateDim) + dt*Fxi;
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Dialog Boxes finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!