How can I request the number of physical cores from MATLAB?

Does anyone know how to query the number of physical cores from MATLAB? I would like to get the number of physical, not logical, cores (which may vary when the hyper-thread is turned on).

I need a method for cross-platform (Windows and Linux, not caring about Mac), but I would be happy to use two separate methods with an operator switchbased on output computer.

So far I have tried:

  • java.lang.Runtime.getRuntime().availableProcessors
  • System.Environment.ProcessorCount
  • !wmic cpu get NumberOfCoresand !wmic cpu get NumberOfLogicalProcessors.

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

2 - Windows only, and also returns logical, not physical processors.

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

+5
source share
3 answers

You need to use an undocumented command

feature('numcores')

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

+6
source

It will work

getenv('NUMBER_OF_PROCESSORS')
+1
source

maxNumCompThreads. . 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
0

All Articles