How do Matlab workers work?
古いコメントを表示
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 件のコメント
Edric Ellis
2022 年 5 月 11 日
Are you able to narrow this down to a simple reproduction that you can share? Without that, we can only speculate as to what the cause might be. Providing you're using parpool("local") (and not "threads"), then yes, the MATLAB workers are single-threaded (by default). Your desktop MATLAB can use multiple threads for some operations (things like large matrix calculations) - usually this leads to computations being faster on the desktop than on a single worker.
One possibility is that the code transformations used by parfor might end up with more efficient code for the worker to execute. Because parfor enforces certain constraints on the loop body, the transformed code can end up being more efficient. Here's a couple of things I'd try (if you can't easily come up with a concise reproduction):
- 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
Walter Roberson
2022 年 5 月 11 日
Also try experimenting with setting numcompthread to 1.
Tobias Brambier
2022 年 5 月 11 日
Walter Roberson
2022 年 5 月 11 日
https://www.mathworks.com/help/matlab/ref/maxnumcompthreads.html
回答 (1 件)
Tobias Brambier
2022 年 5 月 19 日
編集済み: Tobias Brambier
2022 年 5 月 19 日
1 件のコメント
Edric Ellis
2022 年 5 月 23 日
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.
カテゴリ
ヘルプ センター および File Exchange で Parallel for-Loops (parfor) についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!