Execute parfor iterations in order!
Show older comments
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 Comments
James Tursa
on 22 Nov 2021
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
on 22 Nov 2021
Accepted Answer
More Answers (1)
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 Comments
Sina Gharebaghi
on 22 Nov 2021
By 'run them in order', do you mean all the operations inside the for loop aren't independent?
From my knowledge, if output of one iteration is required for next iteration then I don't think parfor can be used for that. Since, logically, the cores working at the 'same' time cannot get the previous output of another core that performed (say) 1st iteration.
If this isn't the case for you then maybe you can elaborate on that 1-4 thing in your next comment and I or someone else can better answer then.
Sina Gharebaghi
on 22 Nov 2021
Jaya
on 22 Nov 2021
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
on 22 Nov 2021
Categories
Find more on Parallel Computing Fundamentals 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!