# How to run independent for loops altogether

2 views (last 30 days)
Ahmad Zueter on 7 Apr 2021
Answered: Gargi Patil on 16 Apr 2021
Hello all,
So I have a code that has one large for loop inside which three for loops are nested. The three loops do calculations which are indepndent from each other. The outpuf of each loop is evntually used at the end of the large for loop. The code looks as the following
kmax = 1000;
T(1:100,1:100,1:kmax)=0;
T(1:100,1:100,1)=270;
for o=2:omax %large for loop
% in the real code, there are lot of calculations inside each for loop
% including calculation of an inverse of a large matrix (size is 5000);
% however, for clarification purposes, the code is simplified as
% follows
for i=1:100 %first inner loop
x1(i,1:100)=T(i,1:100,o-1)+5;
end
for i=1:100 %first inner loop
x2(i,1:100)=T(i,1:100,o-1)-3;
end
for j=1:100 %first inner loop
y1(1:100,j)=T(1:100,j,o-1)-10;
end
for j=1:100 %first inner loop
y2(1:100,j)=T(1:100,j,o-1)+6;
end
%after the end of all four loops, their outcome is added as follows
T(:,:,o)=T(:,:,o-1)+x1+x2+y1+y2;
end
Now, my question is, how can I run the four different for loops in parallel using different solvers, noting that they are independent from each other?

Gargi Patil on 16 Apr 2021
Hi,
You can refer to this thread which introduces parallel computing and parfor to run for loops in parallel.