How to parallel two outer loops of a multiple for loop?
显示 更早的评论
Hi,
I have 4 for loops that two outer loops are independent while two inner loops are dependent. In fact the two outer loops stand for two variables which for any of them, I have to calculate 2*10e4 via two inner for loops.
load ('BJ.mat');
p= 0:0.001:1;
d=0:0.001:1;
for j=1:length(p)
for i=1:length(d)
s_m=0;
for m=1:1:10000
s_n=0;
for n= 1:1:10000
s_n =s_n + ((besselj(1,BJ(m,n)*p(j))).^2 * (besselj(m,BJ(m,n)*d(i))).^2)/((BJ(m,n)).^3 * (1 - (m/BJ(m,n)).^2 ) * (besselj(m,BJ(m,n))).^2);
end
s_m= s_m+ s_n;
end
EC(j,i)= s_m /p(j);
end
end
This is actually a very simple Sigma over n and m for two variables (p and d). Therefore, I wanted to use Parfor and I didn't vectorize it but the loops are dependent. The only solution for this number of calculations, seems to be parallelization but I have no experience in parallelization in Matlab. Anybody can help me? Thank you
3 个评论
Please post a question once only. I've deleted the duplicate thread. Thanks.
I have to calculate 2*10e4 via two inner for loops
I do not see, what "2*10e4" means here. There are (1e4)^2 iterations for the inner loop.
Did you pre-allocate the output EC?
al kian
2018-10-16
Jan
2018-10-16
Actually no because for parfor loop, the problem arises when
Actually no what? Which problem arises? You did not mention a problem yet. s_n and s_m do not need a pre-allocation, because they are scalars. But letting EC grow iteratively is a wast of time.
parfor would be applied to the output loops, preferably to the outermost one.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 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!