1

I made a function (see below) which detects if a CPU core has Hyper-threading. When I disable Hyper-threading in the BIOS CPUID still reports that the core has Hyper-threading. How can I do this properly to find out if Hyper-threading is enabled?

// input:  eax = functionnumber, ecx = 0
// output: eax = output[0], ebx = output[1], ecx = output[2], edx = output[3]
//static inline void cpuid (int output[4], int functionnumber)  

bool hasHyperThreading() {
    int abcd[4];
    cpuid(abcd,1);
    return (1<<28) & abcd[3];
}
Z boson
  • 32,619
  • 11
  • 123
  • 226
  • Short answer: no single cpuid bit does what you need. The bit your code looks at returns true if the processor supports HT, but HT is disabled in BIOS. Here is something to look at: http://stackoverflow.com/questions/2901694/programatically-detect-number-of-physical-processors-cores-or-if-hyper-threading. –  Jul 17 '14 at 17:48
  • @ScottD, that's exactly the kind of answer I am looking forward. I made this question to hastily. I think I know what to do now. – Z boson Jul 18 '14 at 07:20
  • @ScottD, I have looked into a bit. It's more complicated than I thought. The link you posted to is obsolete. It reports 32 logical processors and 16 physical cores on my system (it should be 8 and 4). This explains it https://stackoverflow.com/questions/1647190/cpuid-on-intel-i7-processors. – Z boson Jul 18 '14 at 08:24
  • @ScottD, I found some source code by AMD which gives the correct results on my Intel system: 8 logical cores, 4 physical cores with Hyper-threading enabled and 4 logical cores, 4 physical cores with it disabled. I assume this will give the correct result on AMD as well http://developer.amd.com/resources/documentation-articles/articles-whitepapers/processor-and-core-enumeration-using-cpuid/#one – Z boson Jul 18 '14 at 08:38
  • @ScottD, hmm...just tested the AMD code on my four socket server. It gives the wrong number of physical cores. It gets the total wrong and the cores per socket wrong so it's useless. Maybe it's time I use a library to do this. – Z boson Jul 18 '14 at 09:31
  • @ScottD, okay, I found some intel source code which gives the right answer on my server (80 logical cores, 40 physical ones, 4 sockets). https://software.intel.com/en-us/articles/intel-64-architecture-processor-topology-enumeration/ – Z boson Jul 18 '14 at 11:46
  • @Z boson, for a simple question this one sure has a difficult answer. It is good you have some multi-socket Intel and AMD systems to test with. –  Jul 18 '14 at 12:38
  • @ScottD, I added an answer to the link you gave https://stackoverflow.com/questions/2901694/programatically-detect-number-of-physical-processors-cores-or-if-hyper-threading/24824554#24824554 – Z boson Jul 18 '14 at 13:39

1 Answers1

2

The answer to the first part of your question is "for historical reasons", i.e. that bit is still on even for a processor that is only multi-core; the details are over here.

To figure out if you have HT enabled you need enumerate the active logical processors; it's OS-dependent how to do that. Then for every active logical processor get its x2APIC id (you need to run on it to get that, so you need to able to set the thread-to-processor affinity) and test the last bit. If no HT is enabled, then this bit will not be set for any logical processor. Good source code for doing this is provided by Intel. I see Z boson has provided an alternative, OpenMP-based method, which however needs a little teak to actually answer the question as asked (see my comment there.)

Also, as far as I know AMD currently has no hyperthreading for any of their processors.

Community
  • 1
  • 1
Fizz
  • 4,782
  • 1
  • 24
  • 51
  • My question was a [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What I really wanted to know was the number of physical cores and I was using hyper-threading to figure that out. But you're correct that with a small modification to my solution for finding the number of cores one could find the out if hyper-threading existed and was enabled in the BIOS so I'll give you plus one in case anyone ever wants to really know this. – Z boson Aug 13 '14 at 07:31