Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 534
Sam Roberts Avatar asked Nov 29 '11 13:11

Sam Roberts


People also ask

How many cores does MATLAB use by default?

MATLAB is using: 3 logical cores. MATLAB is not using all logical cores because hyper-threading is enabled. why matlab detect just juste 3 physical cores ! and how can i use all logical and physical cores for parallel computing. thanks.

How many logical cores does a physical core have?

The number of logical processors you will have will be 8. This value is a product of the number of physical cores (8), and the number of threads they can handle (1). But what if your CPU is capable of hyperthreading? Well then each core can handle two threads, so an 8 core CPU will have 8 * 2 = 16 logical processors.

Does MATLAB support multiple cores?

MATLAB® provides two main ways to take advantage of multicore and multiprocessor computers. By using the full computational power of your machine, you can run your MATLAB applications faster and more efficiently.

Is MATLAB single core or multi core?

Only certain functions are optimized to take advantage of multiple core processor. More generally matlab remains a single threaded application and you have to use parallel computing toolbox to take full advantage of multi core processors.


2 Answers

You need to use the undocumented command

feature('numcores')

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

like image 75
Edric Avatar answered Oct 05 '22 20:10

Edric


This will work

getenv('NUMBER_OF_PROCESSORS')
like image 37
Khoa Avatar answered Oct 05 '22 20:10

Khoa