This is more of a curiosity question than something that needs actual solving, but is there a way to determine how many cores a machine has from C++ in a platform-independent way? If no such thing exists, what about determining it per-platform (Windows/*nix/Mac)?
Windows Server 2003 and later lets you leverage the GetLogicalProcessorInformation function
On Linux, you can read the /proc/cpuinfo file and count the cores.
You probably won't be able to get it in a platform independent way. Windows you get get number of processors.
The Boost.Thread library has a function called thread::hardware_concurrency() which returns the number of hardware threads available on a system based on number of cpus, cores or hyperthreading units.
On linux the best programmatic way as far as I know is to use sysconf(_SC_NPROCESSORS_CONF) or sysconf(_SC_NPROCESSORS_ONLN).
These aren't standard, but are in my man page for Linux.
If you have assembly-language access, you can use the CPUID instruction to get all sorts of information about the CPU. It's portable between operating systems, though you'll need to use manufacturer-specific information to determine how to find the number of cores. Here's a document that describes how to do it on Intel chips, and page 11 of this one describes the AMD specification. If you need some example code for it, just ask. :-)
Win32:
SYSTEM_INFO sysinfo;
GetSystemInfo( &sysinfo );
numCPU = sysinfo.dwNumberOfProcessors;
Linux, Solaris, & AIX (per comments):
numCPU = sysconf( _SC_NPROCESSORS_ONLN );
FreeBSD, macosx, NetBSD, OpenBSD, etc.:
nt mib[4];
size_t len;
/* set the mib for hw.ncpu */
mib[0] = CTL_HW;
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
/* get the number of CPUs from the system */
sysctl(mib, 2, &numCPU, &len, NULL, 0);
if( numCPU < 1 )
{
mib[1] = HW_NCPU;
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
if( numCPU < 1 )
{
numCPU = 1;
}
}
HPUX:
numCPU = mpctl(MPC_GETNUMSPUS, NULL, NULL);
IRIX:
numCPU = sysconf( _SC_NPROC_ONLN );
Mac OS X using Objective-C++:
NSUInteger a = [[NSProcessInfo processInfo] processorCount];
NSUInteger b = [[NSProcessInfo processInfo] activeProcessorCount];
Note that "number of cores" might not be a particularly useful number, you might have to qualify it a bit more. How do you want to count multithreaded CPUs such as Intel HT, IBM Power5 and Power6, and most famously, Sun's Niagara/UltraSparc T1 and T2? Or even more interesting, the MIPS 1004k with its two levels of hardware threading (supervisor AND user-level)... Not to mention what happens when you move into hypervisor-supported systems where the hardware might have tens of CPUs but your particular OS only sees a few.
The best you can hope is to tell the number of logical processing units that you have in your local OS partition, you can really forget about seeing the true machine unless you are a hypervisor. The only exception to this rule today is in x86 land, but the end of non-virtual machines is coming fast...
OpenMP is supported on many platforms (including Visual Studio 2005) and it offers a
int omp_get_num_procs();
function that returns the number of processors/cores available at the time of call.
One more Windows recipe: use system-wide environment variable NUMBER_OF_PROCESSORS
:
printf("%d\n", atoi(getenv("NUMBER_OF_PROCESSORS")));
In Linux, you can checkout dmesg and filter the lines where ACPI initializes the CPUs, something like:
dmesg | grep 'ACPI: Processor
'
Other possibility is to use dmidecode to filter out the processor information.
OS X alternative: The solution described earlier based on [[NSProcessInfo processInfo] processorCount] is only available on OS X 10.5.0, according to the docs. For earlier versions of OS X, use the Carbon function MPProcessors().
If you're a Cocoa programmer, don't be freaked out by the fact that this is Carbon. You just need to need to add the Carbon framework to your Xcode project and MPProcessors() will be available.
(Almost) Platform Independent function in c-code
#ifdef _WIN32
#include <windows.h>
#elif MACOS
#include <sys/param.h>
#include <sys/sysctl.h>
#else
#include <unistd.h>
#endif
int getNumCores() {
#ifdef WIN32
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#elif MACOS
int nm[2];
size_t len = 4;
uint32_t count;
nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
sysctl(nm, 2, &count, &len, NULL, 0);
if(count < 1) {
nm[1] = HW_NCPU;
sysctl(nm, 2, &count, &len, NULL, 0);
if(count < 1) { count = 1; }
}
return count;
#else
return sysconf(_SC_NPROCESSORS_ONLN);
#endif
}