Final few iterations of a parfor loop slow down by a factor of 10
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?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? 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? parfor, slow MATLAB Answers — New Questions