6-For-Loops: possibility to use parfor or something similar?!
2 次查看(过去 30 天)
显示 更早的评论
Hello Guys. i have a operation with 6-For-Loops. Is it possible to use parallel Computing like parfor or something similar. This is a very large and powerful iteration and it needs a lot of time. How the code must look like to get this operation/code faster. Thanks
Buff = zeros(numel(1350:5:1450)*numel(-150:5:-50)*numel(825:5:925)*numel(1225:5:1325),5);
Ctr = 1;
tic
for step1 = 1350:5:1450
for step2=-150:5:-50
for step3=825:5:925
for step4=1225:5:1325
x_g=step1;
y_g=step2;
PC=step3;
EG=step4;
OP=sqrt((x_p-x_o)^2+(y_p-y_o)^2);
OE=sqrt((x_e-x_o)^2+(y_e-y_o)^2);
OG=sqrt((x_g-x_o)^2+(y_g-y_o)^2);
OC=sqrt((x_c-x_o)^2+(y_c-y_o)^2);
OD=sqrt((x_d-x_o)^2+(y_d-y_o)^2);
OH=sqrt((x_h-x_o)^2+(y_h-y_o)^2);
OJ=sqrt((x_j-x_o)^2+(y_j-y_o)^2);
OA=sqrt((x_a-x_o)^2+(y_a-y_o)^2);
DC=sqrt((x_d-x_c)^2+(y_d-y_c)^2);
GD=sqrt((x_g-x_d)^2+(y_g-y_d)^2);
DA=sqrt((x_d-x_a)^2+(y_d-y_a)^2);
HG=sqrt((x_h-x_g)^2+(y_h-y_g)^2);
HD=sqrt((x_h-x_d)^2+(y_h-y_d)^2);
JA=sqrt((x_j-x_a)^2+(y_j-y_a)^2);
JH=sqrt((x_j-x_h)^2+(y_j-y_h)^2);
JS=sqrt((x_j-x_s)^2+(y_j-y_s)^2);
AS=sqrt((x_a-x_s)^2+(y_a-y_s)^2);
Main; % Calculation of other variables...
Buff(Ctr, 1) = step1;
Buff(Ctr, 2) = step2;
Buff(Ctr, 3) = step3;
Buff(Ctr, 4) = step4;
Buff(Ctr, 5) = J_Bolzen;
Ctr = Ctr+1;
end
end
end
end
toc
0 个评论
回答(4 个)
Jan
2012-12-27
At first it is essential for the efficiency, that all code is moved outside the loops, if it does not depend on the loops, e.g.:
OP = sqrt((x_p-x_o)^2 + (y_p-y_o)^2);
There is a lot of code, which is calculated repeatedly instead of using a temporary variable.
0 个评论
Trier87
2012-12-27
编辑:Trier87
2012-12-27
1 个评论
Greg Heath
2012-12-28
Parfor loops cannot be nested.
Use Parfor in the loop that does the largest amount of heavy duty computing.
Walter Roberson
2012-12-28
Rewrite
step1_vals = 1350:5:1450;
step2_vals = -150:5:-50;
step3_vals = 825:5:925;
step4_vals = 1225:5:1325;
Buff = zeros( length(step1_vals), length(step2_vals), length(step3_vals), length(step4_vals) );
for step4_idx = 1 : length(step4_vals)
step4 = step4_vals(step4_idx);
for step3_idx = 1 : length(step3_vals)
step3 = step3_vals(step3_idx);
for step2_idx = 1 : length(step2_vals)
step2 = step2_vals(step2_idx);
for step1_idx = 1 : length(step1_vals);
step1 = step1_vals(step1_idx);
..... bunch of code here ....
Buff(step1_idx, step2_idx, step3_idx, step4_idx) = J_Bolzen;
With no need to explicitly store step1, step2, step3, step4 into Buff because they are now completely implicit for Buff(I,J,K,L) as being step1_vals(I), step2_vals(J), step3_vals(K), step4_vals(L)
You might notice that I reversed the order of the loops. This was done to put columns as the fastest varying index, as that is the order MATLAB stores multidimensional arrays, and it is fastest to access in order of increasing memory.
With this code arrangement, you might find opportunities to vectorize your operations -- even if you are only able to make one level of vectorization practical, that will help.
If you do parfor, I would suggest that you calculate at least two for-loops-worth inside a single parfor iteration, so that each iteration has sufficient work to make the overhead of starting the workers worth-while. e.g., "for" step4 and "for" step3, but "parfor" step2 and inside that "for" step1; that should get you (21 * 21) calculations in one chunk. It might be worth "for" step4 and "parfor" step3 and "for" step2 and "for" step1 inside the "parfor".
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!