How do I count CPU cores?
-
Hi together, I'd like to determine the number of cores a CPU has using a C++ program. I found a solution at http://www.intel.com on how to do it for Intel CPUs. But I didn't find out how to do it for AMD CPUs (i.e., Athlon X2). Moreover, the Intel solution states that it might give wrong results for CPUs with a certain stepping if it's not compiled on that CPU type :( Therefore I'd like to ask if anybody has a robust solution for counting the cores of Intel and AMD CPUs. Thank you for any ideas in advance! Regards, Marcus.
-
Hi together, I'd like to determine the number of cores a CPU has using a C++ program. I found a solution at http://www.intel.com on how to do it for Intel CPUs. But I didn't find out how to do it for AMD CPUs (i.e., Athlon X2). Moreover, the Intel solution states that it might give wrong results for CPUs with a certain stepping if it's not compiled on that CPU type :( Therefore I'd like to ask if anybody has a robust solution for counting the cores of Intel and AMD CPUs. Thank you for any ideas in advance! Regards, Marcus.
Use the
Win32_Processor
class.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
Use the
Win32_Processor
class.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
Hi David, thank you for the hint. I managed to access the
Win32_Processor
members. However, the membersNumberOfCores
andNumberOfLogicalProcessors
are not valid. To get them MSDN suggests to useWin32_ComputerSystemProcessor
. This class contains a reference toWin32_Processor
which should contain the data I'm looking for. Now my question: How do I follow the reference fromWin32_ComputerSystemProcessor
toWin32_Processor
in C++? In the first run I usedExecQuery()
to selectWin32_Processor
and accessed its data usingNext()
andGet()
. Now I guess I have to useExecQuery()
to selectWin32_ComputerSystemProcessor
, but how do I continue? Thank you for any help you can give! Regards, Marcus. -
Hi David, thank you for the hint. I managed to access the
Win32_Processor
members. However, the membersNumberOfCores
andNumberOfLogicalProcessors
are not valid. To get them MSDN suggests to useWin32_ComputerSystemProcessor
. This class contains a reference toWin32_Processor
which should contain the data I'm looking for. Now my question: How do I follow the reference fromWin32_ComputerSystemProcessor
toWin32_Processor
in C++? In the first run I usedExecQuery()
to selectWin32_Processor
and accessed its data usingNext()
andGet()
. Now I guess I have to useExecQuery()
to selectWin32_ComputerSystemProcessor
, but how do I continue? Thank you for any help you can give! Regards, Marcus.khb wrote:
I managed to access the Win32_Processor members. However, the members NumberOfCores and NumberOfLogicalProcessors are not valid. To get them MSDN suggests to use Win32_ComputerSystemProcessor. This class contains a reference to Win32_Processor which should contain the data I'm looking for.
This makes no sense. If the members of
Win32_Processor
are not valid, why would you then state that they should contain the data you're looking for?khb wrote:
However, the members NumberOfCores and NumberOfLogicalProcessors are not valid.
How are you verifying this?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
khb wrote:
I managed to access the Win32_Processor members. However, the members NumberOfCores and NumberOfLogicalProcessors are not valid. To get them MSDN suggests to use Win32_ComputerSystemProcessor. This class contains a reference to Win32_Processor which should contain the data I'm looking for.
This makes no sense. If the members of
Win32_Processor
are not valid, why would you then state that they should contain the data you're looking for?khb wrote:
However, the members NumberOfCores and NumberOfLogicalProcessors are not valid.
How are you verifying this?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
DavidCrow wrote:
This makes no sense. If the members of Win32_Processor are not valid, why would you then state that they should contain the data you're looking for?
Maybe I understood wrong what MSDN says. I just read: "To determine the total number of processor instances associated with a computer system object, use the Win32_ComputerSystemProcessor association class."
DavidCrow wrote:
How are you verifying this?
Maybe I'm doing something wrong. I compiled the code with VC6 on my single core P4 with HT enabled (Win XP). I also run the program on a dual core Pentium D with HT enabled (also Win XP). In both cases
NumberOfCores
was not found andNumberOfLogicalProcessors
was 0. I guess that the code works, as I can retrieve other members like, e.g.,Name
correctly. However, I can post the code if you like. Regards, Marcus. -
DavidCrow wrote:
This makes no sense. If the members of Win32_Processor are not valid, why would you then state that they should contain the data you're looking for?
Maybe I understood wrong what MSDN says. I just read: "To determine the total number of processor instances associated with a computer system object, use the Win32_ComputerSystemProcessor association class."
DavidCrow wrote:
How are you verifying this?
Maybe I'm doing something wrong. I compiled the code with VC6 on my single core P4 with HT enabled (Win XP). I also run the program on a dual core Pentium D with HT enabled (also Win XP). In both cases
NumberOfCores
was not found andNumberOfLogicalProcessors
was 0. I guess that the code works, as I can retrieve other members like, e.g.,Name
correctly. However, I can post the code if you like. Regards, Marcus.khb wrote:
In both cases NumberOfCores was not found and NumberOfLogicalProcessors was 0.
These were added for Vista. http://www.microsoft.com/technet/scriptcenter/topics/vista/wmi1.mspx[^] http://windowssdk.msdn.microsoft.com/en-us/library/ms738352.aspx[^]
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
khb wrote:
In both cases NumberOfCores was not found and NumberOfLogicalProcessors was 0.
These were added for Vista. http://www.microsoft.com/technet/scriptcenter/topics/vista/wmi1.mspx[^] http://windowssdk.msdn.microsoft.com/en-us/library/ms738352.aspx[^]
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
DavidCrow wrote:
These were added for Vista.
Whoops, I somehow missed that :-O Well, this brings me right back to my initial question: Is there any chance to get the processor specs on non-Vista systems? Regards, Marcus.
khb wrote:
Is there any chance to get the processor specs on non-Vista systems?
Yes, I've done it with both
Win32_ComputerSystem
andWin32_Processor
.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
khb wrote:
Is there any chance to get the processor specs on non-Vista systems?
Yes, I've done it with both
Win32_ComputerSystem
andWin32_Processor
.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
Now I'm getting confused ;) I just tried
Win32_ComputerSystem
. For example, I can getNumberOfProcessors
correctly. But I can't getNumberOfLogicalProcessors
, it's not in the query result. But this is alright according to MSDN which says aboutNumberOfLogicalProcessors
: Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0, and Windows Me/98/95: This property is not available. Then I triedWin32_Processor
. For example, I can getName
correctly. But I get neitherNumberOfLogicalProcessors
norNumberOfCores
, they're both not in the query result. But again this is alright, because MSDN states for both properties what I already cited above. Or did I get something completely wrong? Thank you for your patience, David! Regards, Marcus. -
Now I'm getting confused ;) I just tried
Win32_ComputerSystem
. For example, I can getNumberOfProcessors
correctly. But I can't getNumberOfLogicalProcessors
, it's not in the query result. But this is alright according to MSDN which says aboutNumberOfLogicalProcessors
: Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0, and Windows Me/98/95: This property is not available. Then I triedWin32_Processor
. For example, I can getName
correctly. But I get neitherNumberOfLogicalProcessors
norNumberOfCores
, they're both not in the query result. But again this is alright, because MSDN states for both properties what I already cited above. Or did I get something completely wrong? Thank you for your patience, David! Regards, Marcus.::GetSystemInfo (which is available all the way back to Win95) will get you the number of processors. http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp[^] I don't know how it would interpret a hyper-threaded machine. For that you might try the cpuid instruction, which is available as a compiler intrinsic as __cpuid (see http://msdn2.microsoft.com/en-us/library/hskdteyh.aspx[^]) Mike
-
::GetSystemInfo (which is available all the way back to Win95) will get you the number of processors. http://msdn.microsoft.com/library/en-us/sysinfo/base/getsysteminfo.asp[^] I don't know how it would interpret a hyper-threaded machine. For that you might try the cpuid instruction, which is available as a compiler intrinsic as __cpuid (see http://msdn2.microsoft.com/en-us/library/hskdteyh.aspx[^]) Mike
Hi Mike, thanks for your answer. Currently I'm using the code from the article Counting Physical and Logical Processors here on CP. It makes also use of the functions you mentioned. I split up the code on did some tests on the following 2 PCs (I was wrong before: Pentium D has no HT):
- Pentium 4, 3.06GHz, Single core, HT
- Pentium D 950, 3.40GHz, Dual core, No HT
I'd like to calculate the following values:
-
CPUs: 1, #Cores: 1, #Log. CPUs per Core: 2
-
CPUs: 1, #Cores: 2, #Log. CPUs per Core: 1
Now the problem: The functions you mentioned return the following values for both(!) CPUs:
GetSystemInfo.dwNumberOfProcessors: 2
cpuid HTT bit: Set
cpuid # logical CPUs per physical CPU: 2This shows that it's not possible to distinguish between HT and dual core :(( Regards, Marcus.