parallel execution of loop

1 次查看(过去 30 天)
D_coder
D_coder 2019-3-22
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 个评论
Adam
Adam 2019-3-22
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 2019-3-23
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by