parallel execution of loop

1 回表示 (過去 30 日間)
D_coder
D_coder 2019 年 3 月 22 日
コメント済み: Walter Roberson 2019 年 3 月 23 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by