Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reliable way to programmatically get the number of hardware threads on Windows

I'm struggling to find a reliable way to get the number of hardware threads on Windows. I am running a Windows 7 Professional SP1 64-bit on a machine with dual CPU Intel Xeon E5-2699 v3 @ 2.30GHz totalizing 36 cores and 72 threads. I have tried different methods to get the number of cores, and I have found that only two of them seem to work accurately 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 do not explain why all these functions return different values. The only 2 methods which seem reliable to me is either using WMI (but fairly complicated) or simply to read in the Windows registry the following key: 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

like image 993
ben Avatar asked Jul 03 '15 14:07

ben


People also ask

How do I know how many threads to use?

The optimal number of threads should equal the number of cores, in which situation the computation capacity of each core will be fully utilized, if the computation on each element is independently.

How do I check my cores and threads in Windows 10?

Press Ctrl + Shift + Esc to open Task Manager. Select the Performance tab to see how many cores and logical processors your PC has.

How do you define number of threads?

A single CPU core can have up-to 2 threads per core. For example, if a CPU is dual core (i.e., 2 cores) it will have 4 threads.


1 Answers

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

The documentation says:

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

like image 86
David Heffernan Avatar answered Nov 15 '22 19:11

David Heffernan