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
Counting physical CPU
Moderators: hgm, Rebel, chrisw
-
- Posts: 373
- Joined: Thu Aug 14, 2008 3:21 am
- Location: Albuquerque, NM
Re: Counting physical CPU
I found this answer: http://stackoverflow.com/questions/1503 ... -a-machinemcostalba 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 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.
-
- Posts: 20943
- Joined: Mon Feb 27, 2006 7:30 pm
- Location: Birmingham, AL
Re: Counting physical CPU
Look up the intel "CPUID" instruction...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
-
- Posts: 2684
- Joined: Sat Jun 14, 2008 9:17 pm
Re: Counting physical CPU
yes, thanks, and more precisely the APIC ID vector...but it's a messbob wrote:Look up the intel "CPUID" instruction...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
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.
-
- Posts: 2684
- Joined: Sat Jun 14, 2008 9:17 pm
Re: Counting physical CPU
Thanks, but sysinfo.dwNumberOfProcessors return number of logical processors, I know it because is what used currenlty in SF, see cpu_count() in misc.cppbnemias wrote:I found this answer: http://stackoverflow.com/questions/1503 ... -a-machinemcostalba 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 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.
-
- Posts: 373
- Joined: Thu Aug 14, 2008 3:21 am
- Location: Albuquerque, NM
Re: Counting physical CPU
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.mcostalba wrote:Thanks, but sysinfo.dwNumberOfProcessors return number of logical processors, I know it because is what used currenlty in SF, see cpu_count() in misc.cppbnemias wrote:See SYSTEM_INFO structure.
I just assumed the MS docs were correct. That they aren't is nothing new. Heh.
-
- Posts: 266
- Joined: Tue Dec 08, 2009 1:37 pm
- Location: Milan, Italy
- Full name: Alex Brunetti
Re: Counting physical CPU
I wrote these functions (Delphi, easy to port to C) that seem to work:
Alex
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;
-
- Posts: 2684
- Joined: Sat Jun 14, 2008 9:17 pm
Re: Counting physical CPU
Thanks Alex,Brunetti wrote:I wrote these functions (Delphi, easy to port to C) that seem to work:
AlexCode: Select all
function cpu3:longword;//# of cores asm mov eax,4 cpuid shr eax,26 inc eax end;
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;
}
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
-
- Posts: 266
- Joined: Tue Dec 08, 2009 1:37 pm
- Location: Milan, Italy
- Full name: Alex Brunetti
Re: Counting physical CPU
Sure: http://www.developers.net/filestore2/download/579mcostalba 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 ?
Alex