7

Does anyone know of a way to query the number of physical cores from MATLAB? I would specifically like to get the number of physical rather than logical cores (which can differ when hyperthreading is enabled).

I need the method to be cross-platform (Windows and Linux, don't care about Mac), but I'd be happy to use two separate methods with a switch statement based on the output of computer.

So far I've tried:

  1. java.lang.Runtime.getRuntime().availableProcessors
  2. System.Environment.ProcessorCount
  3. !wmic cpu get NumberOfCores and !wmic cpu get NumberOfLogicalProcessors.

1 is cross-platform, but returns the number of logical rather than physical processors.

2 is Windows only, and also returns logical rather than physical processors.

3 gives both physical and logical processors, but is also Windows only, and although I can use it successfully from the DOS command window, for some reason it seems to hang for an eternity when run from MATLAB.

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64

3 Answers3

9

You need to use the undocumented command

feature('numcores')

as explained here: http://undocumentedmatlab.com/blog/undocumented-feature-function/

Edric
  • 23,676
  • 2
  • 38
  • 40
  • Ah - there's a bit of undocumented secret sauce, perfect. Thanks @Edric. Do you have any idea why the system call to `wmic` might have been hanging? There are lots of other things that could be useful for if it worked. – Sam Roberts Nov 30 '11 at 09:15
  • 2
    Hmm, dunno, `wmic` "works for me". Maybe add `< nul` to the end of the command? – Edric Nov 30 '11 at 10:13
3

This will work

getenv('NUMBER_OF_PROCESSORS')
Khoa
  • 175
  • 1
  • 8
1

You can use the function maxNumCompThreads. However it's deprecated. Still it works on Matlab 2011a.

maxNumCompThreads
Warning: maxNumCompThreads will be removed in a future release. Please remove any
instances of this function from your code. 
> In maxNumCompThreads at 27

ans =

     4
Oli
  • 15,935
  • 7
  • 50
  • 66