Best way to nest parfoor in for loops?

2 views (last 30 days)
Tristan
Tristan on 8 Oct 2014
Edited: Iain on 8 Oct 2014
I'm running some code that requires time-consuming function evaluation within 4-5 nested loops. I want to make one of the loops into a parfor loop to improve speed.
What criteria should I be considering when deciding which of the for loops to replace with parfor? Does it matter? I've read that because parfor requires some overhead, it should be the outer-most loop, but does this make a big difference? What else should I consider?
Thanks in advance.

Accepted Answer

Iain
Iain on 8 Oct 2014
Edited: Iain on 8 Oct 2014
There are a few things to consider -
1. Is each loop iteration entirely independent of the prior ones? - If not, you can't turn it into a parfor unless you do really fancy stuff.
2. Is there a better optimisation? - For example, don't recalculate stuff if you already have the answer.
3. How long does each loop take? If you do a loop twice, and the first time it's slow, but the second etc. are faster, then theres not necessarily much to be gained making it parallel.
4. What is the cause of the time taken? If it's disk accesses, then you'll probably struggle to make any gains unless the disk accesses are in the parfor.
5. How do the loops interact? - This might make it hard to put some loops in the parfor.

More 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!