Clear Filters
Clear Filters

Final few iterations of a parfor loop slow down by a factor of 10

5 views (last 30 days)
I have a regular for loop which calls a function 44 times. Each iteration takes about 32 seconds to execute in this ordinary for loop. If I use a parfor loop instead, each iteration takes about 8 seconds. However, the final 2 iterations take 10 times as long, 75 and 82 seconds. For example, iteration 22 took 15 seconds in an ordinary for loop, and 75 seconds in the parfor loop, because it happened to be executed as the last iteration. This problem only occurs with the final couple of iterations, never in the middle of the rest of the computation. It occurs with differing datasets. Again, those same iterations , if run in a regular for loop, do not take an inordinate amount of time.
I have preallocated the variables, and don't use global variables.
What is going on? How can I avoid this?
  4 Comments
Edric Ellis
Edric Ellis on 30 May 2024
There's something intriguingly strange going on here. The fact that individual iterations get 4x faster (from 32 seconds down to 8 seconds) when run in parfor indicates definitely something odd - it's much more common for individual operations to get slower when running inside parfor as they no longer run using MATLAB's intrinsic multithreading (workers effectively run in single-computational-thread mode).
  • Is your parfor loop inside a script or a function file? (If it's a script - try using a function)
  • Can you post some minimal reproduction steps that demonstrate the problem?
  • Have you tried using a thread-based pool (i.e. parpool("Threads")) - if it's compatible.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!