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

3

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.

Pasia answered 29/11, 2011 at 13:21 Comment(0)
A
9

You need to use the undocumented command

feature('numcores')

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

Argentum answered 30/11, 2011 at 7:11 Comment(2)
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.Pasia
Hmm, dunno, wmic "works for me". Maybe add < nul to the end of the command?Argentum
H
3

This will work

getenv('NUMBER_OF_PROCESSORS')
Hygroscope answered 6/3, 2014 at 12:37 Comment(2)
oh, I haven't try and I don't know. :)Hygroscope
This stopped working on my machine now. I think the answer above this one is better.Hygroscope
R
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
Rambort answered 29/11, 2011 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.