Detecting Dual-Core from C++

Started by
9 comments, last by _Madman_ 17 years, 7 months ago
What is the best way to determine the number of cores a system has from C++? (dual core/quad core/more core) I know how to get the make and clock of a single core machine by checking the keys in HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\SYSTEM\CentralProcessor\0 Do all multicore machines have HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\SYSTEM\CentralProcessor\1 HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\SYSTEM\CentralProcessor\2 ... HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\SYSTEM\CentralProcessor\n I assume that in all multicore machines the clock speed of the CPUs are the same and will be in the foreseeable future? I'm trying to profile the user's machine on startup so I know how much detail I can run my game at and still get good performance (obviously I consider RAM and some other things too...)

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

Advertisement
Be real careful with what you're doing. An Athlon64 will report much lower clockspeeds than a Pentium -- mine runs at 2 GHz and can easily outperform a P4 3.2 HT at gaming tasks. Come to think of it, watch out for HT too, because that will report dual processors but can be deceptive perf-wise.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
Mucking around random places in the registry is almost always a bad thing to do. If all you want to do is to get a (logical) processor count then you can use GetProcessAffinityMask. You can get much more detailed information via GetLogicalProcessorInformation but that's only available on Server 2003 and higher. I imagine WMI would have this sort of information also but I don't really know.

As for profiling the system I would suggest you go with the standard "lets see how long it takes to run through this quick benchmark" rather than trying to extrapolate from raw numbers like CPU speed. That will give you a much better measure of how the system performs as a whole.
-Mike
Check this code sample from Intel: Detecting Multi-Core Processor Topology in an IA-32 Platform - Intel® Software Network
I need compatibility back to win 2000, 98 would be better. I care about CPU make, clock, and #cores. Using IA-specific hacks is not a great solution. I hadn't thought about HT - I'm guessing there may be no way to determine how many real cores there are in a cpu without vendor-specific hax. Damn.

My fallback is to do a benchmark, but that is overkill for what I'm doing right now, which is mainly gathering anonymous user platform data.

Shedletsky's Bits: A Blog | ROBLOX | Twitter
Time held me green and dying
Though I sang in my chains like the sea...

Quote:Original post by Telamon
I hadn't thought about HT - I'm guessing there may be no way to determine how many real cores there are in a cpu without vendor-specific hax. Damn.
You could just check for the sub strings "Pentium" and "HT" in the processor name string and slice the core count in half.

Sure it's a tremendous hack, but there you go.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
It may not be appropriate to care how many cores the processor has. Going multi-threaded is a very fundamental decision. Are you actually going to drop to just one thread if the system's only got the one core? Design your system to use N threads instead (You'll probably find you have a minimum) and allow the user to scale N as they like. If I had a 4 core system, I might even set your application to use 3, just so it can't starve other processes no matter what.
I belive invoking CPUID will allow you to check, which I assume what the aforementioned Intel sample code does (as posted by bubu).
Latest project: Sideways Racing on the iPad
Those registry keys are ok, I dont know why some people get so melodramatic. The keys are populated at startup and hyperthreading or multicore will occupy additional keys.
The intel and AMD official solutions require using the assembly CPUID function, as you can see in the intel solution posted above.
For 64 bit windows you can use getlogicalprocessorinformation.

I agree with what Deyja posted. If you can split your code into N threads without a problem, then there is 'probably' not much to worry about. If anything use the assembly instructions as they are more portable then using the widows registry.

Edit...

I was reading some random physics books in the school library today and ran across a book by Richard Feynman It has a very interesting chapter on some parellel hacks they implemented using one of the very first IBM puch card systems. Chapter 3 in this book Pleasure

Edit...

For those interested.

link

Search for IBM if you don't want to read the chapter.

[Edited by - smc on September 14, 2006 12:50:01 AM]
∫Mc

This topic is closed to new replies.

Advertisement