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

5 次查看(过去 30 天)
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 个评论
Edric Ellis
Edric Ellis 2024-5-30
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.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by