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.

3 Answers 3

9

You need to use the undocumented command

feature('numcores')

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

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. Nov 30, 2011 at 9:15
  • 2
    Hmm, dunno, wmic "works for me". Maybe add < nul to the end of the command?
    – Edric
    Nov 30, 2011 at 10:13
3

This will work

getenv('NUMBER_OF_PROCESSORS')
2
  • oh, I haven't try and I don't know. :)
    – Khoa
    Mar 13, 2014 at 12:10
  • This stopped working on my machine now. I think the answer above this one is better.
    – Khoa
    Jun 19, 2014 at 5:37
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
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.