Thursday, May 15, 2008

How to find the number of processors in a system.

Here is some useful code I wrote:

[Note: I included hyper-threaded cores as two processores (if enabled).]

int getNumberOfProcessors()
{
int rval = 1;
SYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer[maxCPUs];
DWORD len = sizeof(buffer);

BOOL success = GetLogicalProcessorInformation(buffer,&len);

if (success)
{
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION ptr;

int processorCount = 0;
DWORD byteOffset = 0;
ptr = buffer;
while (byteOffset < len)
{
switch (ptr->Relationship)
{
case RelationNumaNode:
case RelationProcessorCore:
processorCount++;
break;
default:
break;
}
byteOffset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
ptr++;
}
rval = processorCount;
}
if (rval > maxCPUs)
rval = maxCPUs;

return rval;
}

No comments:

chris' shared items

Twitter Updates

Official blog of Chris Lee Runyan

Fastest C++ in the west.