Detecting the number of CPUs

Programming Topics (Computer Chess) and technical aspects as test techniques, book building, program tuning etc

Moderator: Andres Valverde

Detecting the number of CPUs

Postby Tord Romstad » 18 Nov 2007, 13:31

In Linux and Mac OS X, I use the following function to find the number of available CPUs:
Code: Select all
int cpu_count() {
  return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
}

This seems to work fine, but I also need a similar function for Windows. After some Googling, I came up with the following function, but because I don't have any Windows computers, I can't test if it works. Does the following look correct?
Code: Select all
int cpu_count() {
  SYSTEM_INFO s;
  GetSystemInfo(&s);
  return Min(s.dwNumberOfProcessors, 8);
}

The Min(<whatever>, 8) thing in the code is there simply because my program is currently limited to a maximum of 8 threads.

Tord
User avatar
Tord Romstad
 
Posts: 639
Joined: 09 Oct 2004, 12:49
Location: Oslo, Norway

Re: Detecting the number of CPUs

Postby Teemu Pudas » 18 Nov 2007, 14:31

It works here. The real test is whether it detects hyperthreading processors correctly, though.
Teemu Pudas
 
Posts: 124
Joined: 16 Apr 2007, 14:03

Re: Detecting the number of CPUs

Postby Tord Romstad » 18 Nov 2007, 18:58

Teemu Pudas wrote:It works here.

Thanks, great! :)

The real test is whether it detects hyperthreading processors correctly, though.

Probably not, I guess a hyperthreading processor will be incorrectly classified as multiple cores. Because hyperthreading processors are rare (are they even produced any longer?), I don't consider it to be a big problem. My program will be able to correctly detect the number of CPUs on most computers, and when it fails, it is always possible for the user to manually set the number of search threads.

Tord
User avatar
Tord Romstad
 
Posts: 639
Joined: 09 Oct 2004, 12:49
Location: Oslo, Norway

Re: Detecting the number of CPUs

Postby Tord Romstad » 18 Nov 2007, 18:59

Andrew Fan wrote:I use getenv("NUMBER_OF_PROCESSORS"). Works with my P4 HT and AMD X2.

How does it compare to my function on the P4 HT? Do they give the same result?

Tord
User avatar
Tord Romstad
 
Posts: 639
Joined: 09 Oct 2004, 12:49
Location: Oslo, Norway

Re: Detecting the number of CPUs

Postby H.G.Muller » 26 Dec 2007, 17:56

Why would classifying hyper-threading as two cores be incorrect? It seems to me that if an SMP program has the opportunity to hyper-thread, it should make use of it for a significant spead-up. Chess programs are very often branch-predict limited, so hyper-threading should help to make much better use of the execute units.
User avatar
H.G.Muller
 
Posts: 3453
Joined: 16 Nov 2005, 12:02
Location: Diemen, NL

Re: Detecting the number of CPUs

Postby Dann Corbit » 26 Dec 2007, 22:01

H.G.Muller wrote:Why would classifying hyper-threading as two cores be incorrect? It seems to me that if an SMP program has the opportunity to hyper-thread, it should make use of it for a significant spead-up. Chess programs are very often branch-predict limited, so hyper-threading should help to make much better use of the execute units.


From the measurements I have seen, using hyperthread CPUs as real ones has neither cost nor benefit for chess programs.

I think that the value gained by the free context switch is probably a wash with the battle for the *real* single register set.
Dann Corbit
 

Re: Detecting the number of CPUs

Postby bob » 26 Dec 2007, 23:52

Dann Corbit wrote:
H.G.Muller wrote:Why would classifying hyper-threading as two cores be incorrect? It seems to me that if an SMP program has the opportunity to hyper-thread, it should make use of it for a significant spead-up. Chess programs are very often branch-predict limited, so hyper-threading should help to make much better use of the execute units.


From the measurements I have seen, using hyperthread CPUs as real ones has neither cost nor benefit for chess programs.

I think that the value gained by the free context switch is probably a wash with the battle for the *real* single register set.


There isn't a "single real register set". But there _is_ a single real processor core. I found that enabling SMT produced a small NPS increase, but the overhead from the parallel search actually made performance (for crafty) worse. On my dual-xeon box, I have it disabled.

Some programs with excessive memory traffic might get a bit more from it, but once a program is tuned for Intel, hyperthreading generally hurts rather than helps. Thankfully it is a dead horse now...
User avatar
bob
 
Posts: 156
Joined: 10 May 2006, 17:59


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 47 guests