- What happens if you run parfor with no parallel pool? (That gets the same code transformations - to do this, either turn off the parallel preference to auto-create a pool, or else use the special parfor (i = 1:N, 0) syntax)
- Is your for loop in a script or a function? If it is a script, try making it be a function - that changes the optimisations the MATLAB language can use
How do Matlab workers work?
16 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
So I have been working on optimizing/parallelising an existing piece of code as part of a project in my studies. And while doing so, I have encountered a rather strange problem, atleast to me.
The 'problem' occurs when I let the code run in a normal for-loop, versus a parfor-loop. According to the tic-toc command the parfor-loop's runtime using a single worker is about half the runtime it would take using a standard for-loop.
My problem with this is that according to my understanding standalone Matlab is single threaded. But using a single worker that also uses one thread is still faster, way faster even. And this is exactly where my understanding of the situation leaves me confused.
And yes, I have checked, there is actually only a single worker using a single thread.
On top of that I sort of need an explanation for this in my final papers for this project.
I really hope to find any explanation or even a hint at all to this, for me, strange behaviour.
4 Commenti
Risposte (1)
Tobias Brambier
il 19 Mag 2022
Modificato: Tobias Brambier
il 19 Mag 2022
1 Commento
Edric Ellis
il 23 Mag 2022
I don't have a definitive answer, but here's what I think is going on. The serial MATLAB profiler reveals that the expensive pieces of the computation are the increments to k_elem_gs, and the update of k_parallel. In particular, I think the updates to k_parallel get more expensive as the number of non-zero elements increases. This is significant, because a parfor loop reorders these additions. Even when using a single worker, parfor divides up the work of the loop into "subranges", which execute separately. Each of these subranges will start the addition of k_parallel from scratch - i.e. starting from "cheap" additions. So whereas the client for loop does this:
k = k + k + k + k + k + k + k;
the single-worker parfor does something more like this:
k = {k + k + k} + {k + k} + {k + k};
(where each k on the right-hand side is different of course). There are still the same number of additions, but not all of them are expensive additions.
Vedere anche
Categorie
Scopri di più su Parallel for-Loops (parfor) in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!