What determines the increase in speed for parfor vs. for?

4 views (last 30 days)
The question is simply that. I know that for some loops with a lot of overhead, it can be slower. In some cases, it can be faster, due to multiple calculations being done in parallel.
The reason I ask this is because my parfor loop runs much faster than I'd expect. When I use
tic
for run = 1:n_runs
function_to_call
end
toc
The code takes around 140-145 seconds to run for n_runs=12. My parallel pool has 12 threads, which makes me think it should run 12 times faster, plus/minus a little bit to account for parfor overhead. What actually happens when I run
tic
parfor run = 1:n_runs
function_to_call
end
toc
is that the code takes about 1 second to run.
What is the explanation here? Am I missing something obvious, or is it a something deeper?
  7 Comments
Sindar
Sindar on 15 Dec 2020
roughly what does function_to_call do? I could imagine some cases where Matlab might do some trickery only with parfor, but not for.
For example, if the effect of the function is redundant (overwriting the same variables or files), for might assume that you have some reason to do it in sequence, whereas parfor explicitly knows that order doesn't matter, so it can just skip to the last iteration. (A human could see this; I doubt Matlab could)
Perhaps more realistically, some aspect of memory management might be similarly sped up. ("oh, order doesn't matter, so I can load this array in once and share it to each thread" or "let's just keep this file stream open")
[caveat: I know just enough about parallelization to be dangerous; I expect you'll explain the function and I'll have no clue why it's faster, but someone else might]
Daniel Pollard
Daniel Pollard on 15 Dec 2020
Essentially what it's doing is running through a binary vector of length N (N>1e5), creating subvectors of length n (n=10-100) and counting how many 1s and 0s there are in each subvector. The binary vectors are randomly generated and I'm interested in averages, so it's being run n_run times as a Monte Carlo simulation to generate these averages.

Sign in to comment.

Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!