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

조회 수: 3 (최근 30일)
Kevin Johnson
Kevin Johnson 2024년 5월 29일
댓글: Edric Ellis 2024년 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