parallel execution of loop

2 views (last 30 days)
D_coder
D_coder on 22 Mar 2019
Commented: Walter Roberson on 23 Mar 2019
I have the following structure
for i = 1:N
%some lines of code
for j = 1:M
%some lines of code and M<N
end
end
How do I perform following parallel execution : i = 1 perform j = 1, i = 2 perform j = 1, i = 3 perform j = 1 and then i = 2 perform j = 1 and so on ?
  2 Comments
Adam
Adam on 22 Mar 2019
I'm not sure I really get your description at the bottom. That basically just boils down to the for loops as you have shown them that you want parallelising?
If you have the Parallel Computing Toolbox then
doc parfor
can do this, depending what is in the body of the for loops. You would usually want to put the parfor on the outer loop. for obvious reasons you can't make both loops parfors.
Walter Roberson
Walter Roberson on 23 Mar 2019
Performing the iterations in the order you request would not be equivalent to doing them in serial.
A = 0;
for i = 1 : 10
B = 0;
for j = 1 : 5
A = A + 1;
B = B + 1;
end
end
When i = 2, j = 1, then A has to be 5 if you executed in serial mode. But if all of the j = 1 iterations for each possible i are executed first, as you request, then A would have been incremented to 10.

Sign in to comment.

Answers (0)

Categories

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

Community Treasure Hunt

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

Start Hunting!