Counting physical CPU

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Counting physical CPU

Post by mcostalba »

I have googled around to find some code that counts the number of physical CPU, not the logical ones, normally returned, i.e. is able to detect hyperthreading is on and is not fooled by it, but returns 2 if you are on a dual core also if HT is switched on.

Unfortunately it seems does not exists an easy solution, or at least I have not found one. I have found a little program that does it, but is not simple.

The reason I would need is because when you have 2 cores you may want to set the number of default threads to 2, not 4 as it happens when HT is on.

Does anyone has some reference on this subject ?

Thanks
Marco
bnemias
Posts: 373
Joined: Thu Aug 14, 2008 3:21 am
Location: Albuquerque, NM

Re: Counting physical CPU

Post by bnemias »

mcostalba wrote:I have googled around to find some code that counts the number of physical CPU, not the logical ones, normally returned, i.e. is able to detect hyperthreading is on and is not fooled by it, but returns 2 if you are on a dual core also if HT is switched on.

Unfortunately it seems does not exists an easy solution, or at least I have not found one. I have found a little program that does it, but is not simple.

The reason I would need is because when you have 2 cores you may want to set the number of default threads to 2, not 4 as it happens when HT is on.
I found this answer: http://stackoverflow.com/questions/1503 ... -a-machine

I didn't exhaustively search to see that all solutions give physical processor count, but it appears that for Windows, the result is physical.

See SYSTEM_INFO structure.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Counting physical CPU

Post by bob »

mcostalba wrote:I have googled around to find some code that counts the number of physical CPU, not the logical ones, normally returned, i.e. is able to detect hyperthreading is on and is not fooled by it, but returns 2 if you are on a dual core also if HT is switched on.

Unfortunately it seems does not exists an easy solution, or at least I have not found one. I have found a little program that does it, but is not simple.

The reason I would need is because when you have 2 cores you may want to set the number of default threads to 2, not 4 as it happens when HT is on.

Does anyone has some reference on this subject ?

Thanks
Marco
Look up the intel "CPUID" instruction...
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Counting physical CPU

Post by mcostalba »

bob wrote:
mcostalba wrote:I have googled around to find some code that counts the number of physical CPU, not the logical ones, normally returned, i.e. is able to detect hyperthreading is on and is not fooled by it, but returns 2 if you are on a dual core also if HT is switched on.

Unfortunately it seems does not exists an easy solution, or at least I have not found one. I have found a little program that does it, but is not simple.

The reason I would need is because when you have 2 cores you may want to set the number of default threads to 2, not 4 as it happens when HT is on.

Does anyone has some reference on this subject ?

Thanks
Marco
Look up the intel "CPUID" instruction...
yes, thanks, :-) and more precisely the APIC ID vector...but it's a mess :-(

It is not a single bit, but first you have to find how many bits are reserved for coding HT, then how many bits are used to coding MC (multicore) capability, because they depend on the processor family, then do some masking work then finally get that info.

There is a video on Intel site where a guy explain this...but is not trivial to get it right, so I was wondering if there is some already invented code around that does that.
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Counting physical CPU

Post by mcostalba »

bnemias wrote:
mcostalba wrote:I have googled around to find some code that counts the number of physical CPU, not the logical ones, normally returned, i.e. is able to detect hyperthreading is on and is not fooled by it, but returns 2 if you are on a dual core also if HT is switched on.

Unfortunately it seems does not exists an easy solution, or at least I have not found one. I have found a little program that does it, but is not simple.

The reason I would need is because when you have 2 cores you may want to set the number of default threads to 2, not 4 as it happens when HT is on.
I found this answer: http://stackoverflow.com/questions/1503 ... -a-machine

I didn't exhaustively search to see that all solutions give physical processor count, but it appears that for Windows, the result is physical.

See SYSTEM_INFO structure.
Thanks, but sysinfo.dwNumberOfProcessors return number of logical processors, I know it because is what used currenlty in SF, see cpu_count() in misc.cpp
bnemias
Posts: 373
Joined: Thu Aug 14, 2008 3:21 am
Location: Albuquerque, NM

Re: Counting physical CPU

Post by bnemias »

mcostalba wrote:
bnemias wrote:See SYSTEM_INFO structure.
Thanks, but sysinfo.dwNumberOfProcessors return number of logical processors, I know it because is what used currenlty in SF, see cpu_count() in misc.cpp
I didn't expect that. That's why I included the link to the SYSTEM_INFO structure-- it specifically says there that to get logical processors instead of physical ones, to use a different call.

I just assumed the MS docs were correct. That they aren't is nothing new. Heh.
User avatar
Brunetti
Posts: 266
Joined: Tue Dec 08, 2009 1:37 pm
Location: Milan, Italy
Full name: Alex Brunetti

Re: Counting physical CPU

Post by Brunetti »

I wrote these functions (Delphi, easy to port to C) that seem to work:

Code: Select all

function cpu0:longword;//max cpuid parameter accepted
asm
  mov eax,0
	cpuid
end;

function cpu1:longword;//HT is enabled
asm
  mov eax,1
	cpuid
  mov eax,edx
  and eax,$10000000
  shr eax,28
end;

function cpu2:longword;//# of logical processore
asm
  mov eax,1
	cpuid
  mov eax,ebx
  and eax,$00FF0000
  shr eax,16
end;

function cpu3:longword;//# of cores
asm
  mov eax,4
	cpuid
  shr eax,26
  inc eax
end;
Alex
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Counting physical CPU

Post by mcostalba »

Brunetti wrote:I wrote these functions (Delphi, easy to port to C) that seem to work:

Code: Select all


function cpu3:longword;//# of cores
asm
  mov eax,4
	cpuid
  shr eax,26
  inc eax
end;
Alex
Thanks Alex,

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&#40;) &#123;

  int CPUInfo&#91;4&#93; = &#123;-1&#125;;
  __cpuid&#40;CPUInfo, 0x00000001&#41;;

  return &#40;CPUInfo&#91;1&#93; >> 24&#41; & 0xff;
&#125;
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. Care to post a link to the corresponding documentation ?

Thanks
Marco
User avatar
Brunetti
Posts: 266
Joined: Tue Dec 08, 2009 1:37 pm
Location: Milan, Italy
Full name: Alex Brunetti

Re: Counting physical CPU

Post by Brunetti »

mcostalba wrote:But I don't understand where you found the information to extract the # of cores. Care to post a link to the corresponding documentation ?
Sure: http://www.developers.net/filestore2/download/579

Alex
mcostalba
Posts: 2684
Joined: Sat Jun 14, 2008 9:17 pm

Re: Counting physical CPU

Post by mcostalba »

Thanks Alex :D