Reliable way to programmatically get the number of cores in Windows

I try my best to find a reliable way to get the number of cores in Windows. I am running the 64-bit version of Windows 7 Professional SP1 on a machine with an Intel Xeon E5-2699 v3 @ 2.30GHz dual-core processor that summarizes 72 cores. I tried different methods to get the number of cores, and I found that only two of them seem to work exactly in a 32-bit or 64-bit process. Here are my results:

  + ------------------------------------------------ + ---------------- + ---------------- +
 |  Methods |  32-bit process |  64-bit process |
 + ------------------------------------------------ + ---------------- + ---------------- +
 |  GetSystemInfo-> dwNumberOfProcessors |  32 |  36 |
 |  GetNativeSystemInfo-> dwNumberOfProcessors |  36 |  36 |
 |  GetLogicalProcessorInformation |  36 |  36 |
 |  GetProcessAffinityMask.processAffinityMask |  32 |  32 |
 |  GetProcessAffinityMask.systemAffinityMask |  32 |  32 |
 |  omp_get_num_procs |  32 |  36 |
 |  getenv ("NUMBER_OF_PROCESSORS") |  36 |  36 |
 |  GetActiveProcessorCount (ALL_PROCESSOR_GROUPS) |  64 |  72 |
 |  GetMaximumProcessorCount (ALL_PROCESSOR_GROUPS) |  64 |  72 |
 |  boost :: thread :: hardware_concurrency () |  32 |  36 |
 |  Performance counter API |  36 |  36 |
 |  WMI |  72 |  72 |
 |  HARDWARE \ DESCRIPTION \ System \ CentralProcessor |  72 |  72 |
 + ------------------------------------------------ + ---------------- + ---------------- +

I will not explain why all these functions return different values. The only 2 methods that seem reliable to me are either using WMI (but rather complicated) or simply reading the following key in the Windows registry: HARDWARE \ DESCRIPTION \ System \ CentralProcessor.

What do you think? Do you confirm that the WMI and registry key methods are the only reliable methods?

Thanks in advance

+7
c ++ multithreading windows winapi
source share
2 answers

The API function you need is GetLogicalProcessorInformationEx . Since you have more than 64 processors, your processors are grouped. GetLogicalProcessorInformation reports only the processors in the processor group that are currently assigned by the thread. You must use GetLogicalProcessorInformationEx to overcome this limitation.

The documentation says:

On systems with more than 64 logical processors, the GetLogicalProcessorInformation function retrieves the logical processor information about the processors in the processor group to which the calling thread is currently assigned. Use the GetLogicalProcessorInformationEx function to get information about the processors in all processor groups in the system.

+7
source share

You can use the CPUID instruction to directly query the processor (platform independent, though, since you cannot do the built-in asm in MSVC anymore for some compilers, you will need to use different functions to access it). The only drawback is that several years ago, Intel and AMD handled this instruction differently, and you will need to do a lot of work to make sure that you are reading the information correctly. In fact, you can not only get the main account, but also get all the information about the processor topology. Not sure how this works in VM, but if you use this environment.

0
source share

All Articles