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 Comments

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?
Thank you Jan for your reply. Yes you are right. That is (10e4)^2.
Actually no because for parfor loop, the problem arises when it nulls the (s_n) and (s_m) at the beginning of each loop. While this is Sigma of s_n for each m. And I can not pre-allocate s_n and s_m.
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.

Sign in to comment.

 Accepted Answer

Jan
Jan on 16 Oct 2018
Edited: Jan on 16 Oct 2018
Here a version of your code with some removed parentheses and a rand instead of a load (because I do not have the required MAT file) - in addition I reduced the data size, because your problem is really huge:
BJ = rand(10, 10); % load ('BJ.mat');
p = 0:0.01:1;
d = 0:0.01:1;
tic
EC = zeros(length(p), length(d));
for j = 1:length(p)
for i = 1:length(d)
s_m = 0;
for m = 1:10
s_n = 0;
for n = 1:10
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
toc
Now a vectorized version of the inner loop:
tic
EC2 = zeros(length(p), length(d));
for j = 1:length(p)
for i = 1:length(d)
s_m = 0;
for m = 1:10
BBJ = BJ(m, 1:10);
s_n = sum((besselj(1, BBJ * p(j)) .^2 .* ...
besselj(m, BBJ * d(i)) .^2) ./ ...
(BBJ .^3 .* (1 - (m ./ BBJ) .^2) .* ...
besselj(m, BBJ) .^2));
s_m = s_m + s_n;
end
EC2(j, i) = s_m / p(j);
end
end
toc
The original version needs 17 seconds, after vectorizing the inner loop only 3.3 seconds.
Remember, that this is a smaller version of your calculation with BJ is a 10x10 matrix instead of a 1e4x1e4 (by the way: not "10e4") and p and d have 101 elements instead of 10001. The most time is spent for the Bessel function, so you can expect that the runtime of the original problem is about 1e10 times longer. For the version without parfor this is more than 100'000 days, which is about 317 years. So think twice if your current approach is feasible at all.
Think of reducing the number of Bessel functions. E.g. besselj(m, BJ) in the last line of the equation is computed repeatedly for the same inputs. besselj(1, BBJ * p(j)) is the same value for all iterations of the for i loop. So think of a better way to accumulate the sums in the output matrix, which avoids the expensive calculation of the Bessel functions for the same values, because this wastes the most time.

1 Comment

Thank you Jan, I've modified the code and decreased the range of variables. Maybe in future I'll submit it on the cluster.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2017b

Asked:

on 16 Oct 2018

Commented:

on 18 Oct 2018

Community Treasure Hunt

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

Start Hunting!