Parfor loops

1 次查看(过去 30 天)
Thomas Humphrey
Thomas Humphrey 2012-2-17
Hi, Am trying to get par for loops running for the first time, and converting my for loops so hopefully my code will run faster.
I've already got the matlabpool open and 8 workers assigned to it, but I was wondering if someone could point out what's wrong exactly in my code.
w = 1;
A = (M_d*C_pd*T_d)+(M_b*C_pb*T_b);
D = (M_d*C_pd + M_b*C_pb);
T = [];
for z = 1:10
disp('loop')
disp(z)
disp('number of possibilites')
disp(size(T,2))
w = errorcheck(w,T,z);
disp('w')
disp(w)
g = 1/10^w;
parfor T_f = k:e:l
C_pf = Specific_Heat(T_f,X_f);
a = M_f*C_pf*(T_f-T_cw);
for T_o = m:f:n
b = A-D*T_o;
if abs(a-b)<g
T(1,i) = T_f;
T(2,i) = T_o;
i=i+1;
end
end
end
k = min(T(1,:));
l = max(T(1,:));
m = min(T(2,:));
n = max(T(2,:));
e = e/10;
f = f/10;
end
I haven't used the par for loop on the outside loop as I'm not sure this would be best + it's not where most of the calculations are occurring anyway.
Thanks in advance
Tom

回答(1 个)

B.V. Vijay
B.V. Vijay 2012-2-17
I haven't used parfor too extensively, so there could likely be better answers than mine. Anyway, what I notice in your code are these:
1. The parfor loop range does not seem correctly declared. I believe you cannot define your own increment like the e in k: e :l. Anyway, if that is not your error, look at the following other possibility:
2. Parameter X_f is not known to any worker, as I see it appearing for the first time inside the parfor loop here:
C_pf = Specific_Heat(T_f,X_f);
My way would be to create a function called RETURNXF that take X1,X2, ...XN as inputs and returns X_f, and call RETURNXF with a handle like this:
w = 1;
A = (M_d*C_pd*T_d)+(M_b*C_pb*T_b);
D = (M_d*C_pd + M_b*C_pb);
T = [];
function X_F = RETURNXF(X1,X2,..XN)
...
...
Statements to yield X_F
...
X_F=...
end
RTXF = @RETURNXF;
Then modify your parfor loop like this:
parfor (T_F=1:LIMIT)
...
Construct here the New_Loop_Index in place of T_f to be used in the Specific_Heat function, based on whatever loop increment you want to give.
...
Following are variables needed as input to RETURNXF function
X1=..;
X2=..;
..
XN=..;
X_f=RTXF(X1,X2,..XN)
C_pf = Specific_Heat(New_Loop_Index,X_f)
..
Rest of your code
Hope that works. Good luck!

类别

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