Sam Roberts
Sam Roberts

Reputation: 24127

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

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.

Upvotes: 7

Views: 7330

Answers (3)

Khoa
Khoa

Reputation: 185

This will work

getenv('NUMBER_OF_PROCESSORS')

Upvotes: 3

Edric
Edric

Reputation: 25140

You need to use the undocumented command

feature('numcores')

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

Upvotes: 9

Oli
Oli

Reputation: 16045

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

Upvotes: 1

Related Questions