The following program prints the logical and physical cores of a windows machine.
#define STRICT
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <omp.h>
template<typename T>
T *AdvanceBytes(T *p, SIZE_T cb)
{
return reinterpret_cast<T*>(reinterpret_cast<BYTE *>(p) + cb);
}
class EnumLogicalProcessorInformation
{
public:
EnumLogicalProcessorInformation(LOGICAL_PROCESSOR_RELATIONSHIP Relationship)
: m_pinfoBase(nullptr), m_pinfoCurrent(nullptr), m_cbRemaining(0)
{
DWORD cb = 0;
if (GetLogicalProcessorInformationEx(Relationship,
nullptr, &cb)) return;
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) return;
m_pinfoBase =
reinterpret_cast<SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *>
(LocalAlloc(LMEM_FIXED, cb));
if (!m_pinfoBase) return;
if (!GetLogicalProcessorInformationEx(Relationship,
m_pinfoBase, &cb)) return;
m_pinfoCurrent = m_pinfoBase;
m_cbRemaining = cb;
}
~EnumLogicalProcessorInformation() { LocalFree(m_pinfoBase); }
void MoveNext()
{
if (m_pinfoCurrent) {
m_cbRemaining -= m_pinfoCurrent->Size;
if (m_cbRemaining) {
m_pinfoCurrent = AdvanceBytes(m_pinfoCurrent,
m_pinfoCurrent->Size);
} else {
m_pinfoCurrent = nullptr;
}
}
}
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *Current()
{ return m_pinfoCurrent; }
private:
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *m_pinfoBase;
SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *m_pinfoCurrent;
DWORD m_cbRemaining;
};
int __cdecl main(int argc, char **argv)
{
int numLogicalCore = 0;
int numPhysicalCore = 0;
for (EnumLogicalProcessorInformation enumInfo(RelationProcessorCore);
auto pinfo = enumInfo.Current(); enumInfo.MoveNext())
{
int numThreadPerCore = (pinfo->Processor.Flags == LTP_PC_SMT) ? 2 : 1;
// std::cout << "thread per core: "<< numThreadPerCore << std::endl;
numLogicalCore += numThreadPerCore;
numPhysicalCore += 1;
}
printf ("Number of physical core = %d , Number of Logical core = %d \n", numPhysicalCore, numLogicalCore );
char c = getchar(); /* just to wait on to see the results in the command prompt */
return 0;
}
/*
I tested with Intel Xeon four cores with hyper threading and here is the result
Number of physical core = 4 , Number of Logical core = 8
*/