Execute parfor iterations in order!

21 ビュー (過去 30 日間)
Sina Gharebaghi
Sina Gharebaghi 2021 年 11 月 22 日
コメント済み: Sina Gharebaghi 2021 年 11 月 23 日
Hi,
I want to use Matlab parallelization to do this parfor in order like 1, 2, 3, ... , and I have limitted number of workers/cores, say 4.
parfor cnt = 1:20
do_something(cnt)
end
This is important for me to run cnt = 1:4 first, and once one of them finished, I need to start running next one which will be 5. I would appreciate your suggestions.
Thanks!
  2 件のコメント
James Tursa
James Tursa 2021 年 11 月 22 日
Why do they need to be run in order? Does the result of the 1:4 iterations serve as inputs to the 5:8 iterations, etc.?
Sina Gharebaghi
Sina Gharebaghi 2021 年 11 月 22 日
1- Actually, results of 1:4 does not serve as inputs of 5:8. However, based on results of 1:4, I may decide that running 5:8 is not required. I just need to stop running 5:end to decrease runtime.
2- If I want to use results of 1:4 as inputs of 5:8, how can I do that?
Thank you!

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2021 年 11 月 23 日
This is not possible with parfor(), and it is unlikely that Mathworks will ever implement an option to support this.
parfor divides the available range up into chunks depending on the number of cores, and starts some of the chunks going, leaving other chunks in the pool to be allocated to whichever workers finish fastest. parfor also leaves a number of individual iterations to the end, to be allocated one-by-one as workers finish.
Furthermore, when parfor starts allocating the initial (larger) chunks, the first chunk it starts is the last iterations. This gives some advantages in pre-allocating arrays.
In your situation, instead of using parfor, you should use parfeval() . That allows you to control exactly which work gets sent out, in which order, and allows you to cancel chunks that have not finished yet.
  3 件のコメント
Steven Lord
Steven Lord 2021 年 11 月 23 日
You may be able to use two parfor loops, one after the other. For the first one:
parfor k = 1:4
% body of parfor
end
If after evaluating the results of that parfor loop you decide you want to run the rest of the iterations:
parfor k = 5:20
% body of parfor
end
Sina Gharebaghi
Sina Gharebaghi 2021 年 11 月 23 日
Thank you, Steven!

サインインしてコメントする。

その他の回答 (1 件)

Jaya
Jaya 2021 年 11 月 22 日
編集済み: Jaya 2021 年 11 月 23 日
You need not run 4 times first and then start with 5. You can directly do the parfor for 1:20. Assuming yours is a quad core, then each core will take the task of doing 5 iterations. How it splits that is not a thing for us to worry.
I asked this similar question some time ago.Here is the link.
  5 件のコメント
Jaya
Jaya 2021 年 11 月 22 日
Then I don't really think or don't know if or how can you still use parfor for this case. Since I understand your situation as something that requires simulation stopping based on some result you get in between the iterations. And as you might know, parfor cannot use a break statement.
Sina Gharebaghi
Sina Gharebaghi 2021 年 11 月 22 日
Thanks for your help. You are right. Probably, I will not be able to use "parfor".

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeParallel Computing Fundamentals についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by