Enable multi-threading on a parfor loop

Hi everyone,
I have a read a lot of stuff on multi-threading on Matlab but i am a bit lost between all the versions/revisions on this topic. I have a code that mostly runs a parfor loop. This sends to my workers work to do, in single-threaded mode as i can see on my task manager. My question is : despite the overlapping issues that may arise, is there a way in Matlab 2012b and higher to force the multi-thread mode on this parfor loop ?
Thanks in advance,
Alex

 Risposta accettata

Edric Ellis
Edric Ellis il 8 Lug 2014
You can use the maxNumCompThreads function (from within an SPMD block for example) to control the number of threads used by the workers. (Yes, this function is deprecated, but unfortunately there is currently no alternative).

5 Commenti

Thanks for your answer. In the maxNumCompThreads documentation, it is said that :
Currently, the maximum number of computational threads is equal to the number of computational cores on your machine.
I have 4 physical cores, so I get an expected '4' when I ask for maxNumCompThreads.
But I have 8 logical cores as each physical core can execute 2 threads at the same time. Is there a way to go around the limitation quoted above, and get 8 logical workers ? Therefore 100% of my CPU could be used instead of 50%...
Thanks !
Logical cores are more of a programming abstraction, the processor smartly manages two threads, say if one is waiting to fetch data then executes the other on the physical core and switches back and forth. If you have a single process taking all of the physical core's advantage, you are all set.
I see. But then why my task manager shows only 50% use when i run my code ? If it was fetching and computing at the same time, i should see 100%, 50% fetching and 50% computing, right ?
José-Luis
José-Luis il 8 Lug 2014
Modificato: José-Luis il 8 Lug 2014
In a perfect world maybe. But the chances that fetching/computing from different threads will perfectly mesh are very small. In fact, in some cases hyperthreading can make performance worse. And Matlab doesn't really allow you to do the fine tuning necessary to affect this performance.
I am not sure it works as it should or maybe I am misinterpreting something. I want to perform a large number of LU factorizations on big sparse matrices. I have access to 16 thread machines (Xeon E5-2670) with 64GB RAM / machine. If I use simple parfor with 16 workers it uses 16x100% cpu power but I run out of memory due to the matrix sizes. If I use 4 or 8 workers I am not utilizing my whole computer's CPU power. So the idea is to use M threads on each of the N workers where NxM=16.
If I set the number of workers to 1 and the number of threads to 4, I can see one matlab process using 400% CPU and doing the job on 4 threads. However, if I now use 4 workers and 4 threads, only 4x100%. 4 workers, 16 threads, still 4x100%. As if doing parfor would stop the LU factorization to run on 4 threads.
Any idea how to resolve the issue, so that I can use 4 workers, each of which is using 4 threads to do the LU factorization?

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by