Counting physical CPU

Discussion of chess software programming and technical issues.

Moderator: Ras

Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Counting physical CPU

Post by Sven »

mcostalba wrote:regarding getting the cpuid and the APIC vector you could do also in C with:

Code: Select all

#include <intrin.h>

int cpu_apic_id() {

  int CPUInfo[4] = {-1};
  __cpuid(CPUInfo, 0x00000001);

  return (CPUInfo[1] >> 24) & 0xff;
}
See http://msdn.microsoft.com/en-us/library ... 80%29.aspx

But I don't understand where you found the information to extract the # of cores. [...]
I assume you already know that CPUID and the __cpuid() intrinsics function are specific for Intel and MSVC (>= 2005). Furthermore, to get a correct result the documentation requires to query the maximum allowed value for the second parameter by calling __cpuid(CPUInfo, 0) first, and to call __cpuid(CPUInfo, x) for x > 0 only if the first call returned a value >= x in CPUInfo[0]. The # of cores is found by something like:

Code: Select all

__cpuid(CPUInfo, 4);
return CPUInfo[0] >> 26;
where masking the result with 0xff is redundant since there are at most 6 nonzero bits left after shifting.

But since this "# cores" value is not available for all Intel processors (that's why the first call with 0 is necessary), one might want to maintain two functions: one boolean function telling whether the information about # of cores is available, and the other one relying on availability and returning the correct number. One might of course be lazy and put that into one function, returning a special value like 0 or -1 for "not available".

EDIT: See also the Visual Studio 2008 page for __cpuid. The link above is for VS2005. I guess it may even be necessary to use VS2008 at least for # of cores since the VS2005 intrinsics implementation might not be capable of that, although I did not test.

Sven