13

I have Intel i7-2600K quadcore, with hyperthreading enabled on Ubuntu 12.04. I know that I can find out how many cores I have in Python with import multiprocessing; multiprocessing.cpu_count(), but that gives me 8 because I have hyperthreading enabled on 4 physical cores. I'm rather interested in finding out how many physical cores I have. Is there a way to do that in Python? Alternatively, is there a way of finding out in Python whether hyperthreading is enabled? Thank you in advance for your help!

Ray
  • 7,833
  • 13
  • 57
  • 91
  • 1
    possible duplicate of [Python: Cross-platform solution to detect physical non-HT CPUs?](http://stackoverflow.com/questions/10152173/python-cross-platform-solution-to-detect-physical-non-ht-cpus) – Blender Oct 15 '12 at 18:48
  • are you on linux or windows? The answer will be different, I don't think there exists a platform independent solution – pyCthon Oct 15 '12 at 19:26

4 Answers4

8

To get hyperthreading information on the mac, you can use:

os.popen('sysctl hw').readlines()[1:20]

and compare the 'hw.activecpu' value to the 'hw.physicalcpu' value, the former being the number of cpus including hyperthreading. Or you could just compare 'hw.physicalcpu' with the value returned from multiprocessing.cpu_count().

On linux you could do something similar using:

os.popen('lscpu').readlines()

and multiply the 'Socket(s)' value and the 'Core(s) per socket' value to get the number of physical cpus. Again, you could check this number against multiprocessing.cpu_count().

I dont use Windows, so I cant help you there, but it seems like others have some ideas in this direction

An example related to this can be found here hardware_info().

Paul Nation
  • 384
  • 2
  • 6
3

According to http://archive.richweb.com/cpu_info, determining a cpu hyperthreading a bit complicated, but still useful.

Note that method is linux-specific.

Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
mechmind
  • 1,732
  • 13
  • 11
1

On windows, to see if hyperthreading is enbabled you can do some WMI magic with pywin32:

from __future__ import print_function
from win32com.client import GetObject

winmgmts_root = GetObject("winmgmts:root\cimv2")
cpus = winmgmts_root.ExecQuery("Select * from Win32_Processor")
for cpu in cpus:
    print('on "{}", hyperthreading is '.format(cpu.DeviceID), end='') 
    if cpu.NumberOfCores < cpu.NumberOfLogicalProcessors:
        print('active')
    else:
        print('inactive')

On my machine, the output is:

on "CPU0", hyperthreading is active

See more info at msdn describing what can be fetched from the Win32_Processor class.

David
  • 9,635
  • 5
  • 62
  • 68
0

On windows, to see if hyperthreading is enbabled you can do some WMI magic with pywin32:

Wrong idea! On my Core2Due (without any hyperthrating) it gives same output:

<on "CPU0", hyperthreading is active>

I think you must change string in code to:

if cpu.NumberOfCores < cpu.NumberOfLogicalProcessors

(no LowerOrEqual just Lower)

samlev
  • 5,852
  • 1
  • 26
  • 38