How to find the number of CPUs?

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

Moderator: Andres Valverde

How to find the number of CPUs?

Postby Tord Romstad » 29 Mar 2006, 20:58

Hi all,

Is there any simple way for a C/C++ program to find the number of CPUs on the computer it is running? If possible, I would prefer a cross-platform solution, but if the code is not too messy I would also be happy with separate solutions for Unix and Windows.

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

Re: How to find the number of CPUs?

Postby Sven Schüle » 29 Mar 2006, 22:42

Hi Tord,

I'm afraid there is no cross-platform solution. AFAIK even on different UNIX systems you may need different code (no POSIX standard yet?), although for both Linux and Solaris (o.k., the latter is not a chess platform) probably the same code should work (I did not try):
Code: Select all
#include <unistd.h>
int ncpus = sysconf(_SC_NPROCESSORS_ONLN);

retrieves the number of active ("online") CPUs; this should be less than or equal to the number of CPUs present on the motherboard, which in turn could be retrieved by 'sysconf(_SC_NPROCESSORS_CONF)'.

For Windows, I simply don't know.

Sven
User avatar
Sven Schüle
 
Posts: 240
Joined: 26 Sep 2004, 20:19
Location: Berlin, Germany

Re: How to find the number of CPUs?

Postby Alessandro Scotti » 29 Mar 2006, 23:06

Hi Tord,
there is no simple solution that I know. Although you can find the proper API call for each system, I think the real problem is understanding what information it is actually reporting.
For example if you run Windows, Linux or Solaris on a Intel single-core processor with hyperthreading, you will find the operating system reports 2 processors, which is not what you want in a chess engine.
Likewise, Solaris on the new 8-core Ultra SPARC T1 is reporting 32 processors (each core has 4-way SMT) but I'm not sure it's a good idea to use each of them for searching.
BTW you can use GetSystemInfo() on Windows, and take a look at /proc/cpuinfo on Linux/x86 (which is less clean than sysconf but has more info e.g. about hyperthreading).
Also, a while ago I recompiled and tested Glaurung for Solaris/SPARC and it worked fine IIRC.

P.S. This is going to be off-topic but... got the new Mac, got Quartz Composer, got hooked... Hamsters' in danger!!!
User avatar
Alessandro Scotti
 
Posts: 306
Joined: 20 Nov 2004, 00:10
Location: Rome, Italy


Re: How to find the number of CPUs?

Postby Tord Romstad » 30 Mar 2006, 20:45

Hi all,

Thanks for the replies!

Alessandro Scotti wrote:Hi Tord,
there is no simple solution that I know. Although you can find the proper API call for each system, I think the real problem is understanding what information it is actually reporting.
For example if you run Windows, Linux or Solaris on a Intel single-core processor with hyperthreading, you will find the operating system reports 2 processors, which is not what you want in a chess engine.


That's a pity. I suppose I will just have to trust that the users will be smart enough to figure out how many threads they should use, then.

P.S. This is going to be off-topic but... got the new Mac, got Quartz Composer, got hooked... Hamsters' in danger!!!


I don't even know what Quartz Composer is, although I have noticed that there is a program with such a name. Thanks for the warning - I'll try to resist the temptation to have a look at it. :)

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


Return to Programming and Technical Discussions

Who is online

Users browsing this forum: No registered users and 24 guests