3

maxNumCompThreads is deprecated, but is it still working with R2014a?

I tried to force a script to use a single computational thread, but it uses 2 logical cores:

maxNumCompThreads(1); % limit MATLAB to a single computational thread. 
signal = rand(1, 1000000);
for i=1:100
    cwt(signal,1:10,'sym2');   
    i
end

Any idea why?

Setting the -singleCompThread option when starting MATLAB does work fine (the script then uses one core only).

Note that my computer has hyperthreading, so 2 logical cores is actually only 1 physical core but usually Matlab count with logical cores, not physical ones (e.g. when setting the number of cores in a parallel pool).

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
  • actually by default, MATLAB sets the maximum number of computational threads equal to the number of *physical* cores, not logical ones. So are you sure that your machine is not in fact dual-core? (hyper-threading will make it appear as 4 cores). Most modern CPUs are dual or quad these days.. For instance `maxNumCompThreads` reports `4` on my quad-core machine, even though I have hyper-threading (OS task manager shows 8 logical cores) – Amro Jun 24 '14 at 23:32
  • Besides `maxNumCompThreads(1)` set the max number of computational threads to one, that doesn't mean this thread is restricted to one CPU only (the thread could jump around between CPUs assigned by the scheduler). If you want that, you'll have to set the affinity of the MATLAB process. – Amro Jun 24 '14 at 23:39
  • @Amro Same here, `maxNumCompThreads` reports 4 on my quad-core machine, even though I have hyper-threading (OS task manager shows 8 logical cores). In the parallel pool the default number of workers is also the number of physical cores, but one worker only uses one logical core, so I thought maxNumCompThreads might also uses logical cores as unit. – Franck Dernoncourt Jun 24 '14 at 23:42
  • @Amro The fact that the code snippet I put in the question only uses around 2 logical cores (actually, slightly less, like 80% of 2 logical cores) might be due to the fact that `cwt(signal,1:10,'sym2');` only makes use of at most two logical cores: do you know by any chance on the top of your head some Matlab code for which Matlab would use more than two logical cores? – Franck Dernoncourt Jun 24 '14 at 23:43
  • 1
    @Amro Indeed in theory setting that the maximum number of thread is different than setting the maximum number of logical or physical core. But `-singleCompThread` does limits Matlab to using one logical core at most, so I thought `maxNumCompThreads(1)` would do the same. – Franck Dernoncourt Jun 24 '14 at 23:44
  • `eig(rand(5000));` :) – Amro Jun 24 '14 at 23:46
  • Like I said you could set the [affinity](http://stackoverflow.com/q/9778287/97160) of the MATLAB process (for instance on Windows, start MATLAB from the command line as: `start /affinity 1 matlab.exe`, or use the Task Manager) – Amro Jun 24 '14 at 23:48
  • @Amro Thanks, indeed I could change the affinity but I would prefer to use some Matlab command and was trying to understand this `maxNumCompThreads`. That's interesting: with `eig(rand(2500));`, `maxNumCompThreads(1);` does strictly limit Matlab to one logical core, while for some reason `maxNumCompThreads(1);` fails with `cwt(signal,1:10,'sym2')`; (whereas `-singleCompThread` does work). – Franck Dernoncourt Jun 24 '14 at 23:56
  • Operating systems are optimized to make full use of available processing power, so even if an application is single-threaded, it may be executed on different cores during its lifetime. Each time a thread is [interrupted](https://en.wikipedia.org/wiki/Preemption_%28computing%29), it may later be assigned to a different CPU by the OS scheduler. The only way to control this assignment is to tell the OS about the process/thread [affinity](https://en.wikipedia.org/wiki/Processor_affinity).. – Amro Jun 25 '14 at 00:00
  • @Amro Thanks, I agree with you but I am surprised to see that `maxNumCompThreads(1);` differs from `-singleCompThread`. – Franck Dernoncourt Jun 25 '14 at 00:30

0 Answers0