Intel® Moderncode for Parallel Architectures
Support for developing parallel programming applications on Intel® Architecture.

How to find the number of processors

sreedhar_reddy
Beginner
1,871 Views

Hi, I am trying to find the number of Physical processors and logical processors(HT) on a server. I was told that the server is a quad HT CPU machine. I have tried using winmsd and the results shows that it has 4 processors. If I look at the task manager, I see that there are 4 graphs representing 4 CPU's. So far so good. However, when I do the same things on my work station, which is a single HT CPU, winmsd tells me that my machine has two processors and the task manager shows two CPU graphs.Which is correct.

I want to be able to determine whether via code or manually how many logical processors and how many physical processors are on the server and whether HT is eanabled.

0 Kudos
14 Replies
jimdempseyatthecove
Honored Contributor III
1,871 Views

In Fortran something like the following

integer :: NumberOfAvailableProcessors
integer(PVOID) :: ProcessAffinityMask, SystemAffinityMask


integer
(HANDLE) :: hProcess
integer
(BOOL) :: bRetGetProcessAffinityMask

hProcess = GetCurrentProcess()
! returns pseudo handle of -1
bRetGetProcessAffinityMask = GetProcessAffinityMask(hProcess, &
& ProcessAffinityMask, SystemAffinityMask)

NumberOfAvailableProcessors = 0

do i=0,BIT_SIZE(ProcessAffinityMask)-1
if
(BTEST(QuickThreadProcessAffinityMask, i)) then
NumberOfAvailableProcessors = NumberOfAvailableProcessors + 1
e
ndif
end do

Note, the number of processors on the system may be more than the number of processors your application (Process) is permitted to use. External factors to the application can be used by the operating system to restrict the number of processors, and/or which processors the application is permitted to use.

Jim Dempsey

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,871 Views

Oops, remove "QuickThread" from the BTEST line, I ment to delete that from the text.

QuickThread is a project I am working on.

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
1,871 Views

Jim's code won't distinguish between cores and sockets - on my single socket, dual-core, non-HT system it returns a count of 2. I think it will count Hyper-Threaded logical processors too.

See also Detecting Multi-Core Processor Topology in an IA-32 Platform

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,871 Views

And Steve's suggestion works only for the processor on which the code is execuited.

A more generalized approach would be for the application to query the operating system.Operating systems generaly have performed a survey of the hardware platform and typicaly have system calls to query the results of the surve. For Windows platforms use GetLogicalProcessorInformation. This returns a table of system logical processor information structures. Depending on the version of Windows this returns more or less information. See the following link for additional information

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/getnumaproximitynode.asp

Next to GetLogicalProcessorInformation check out the GetNuma... function calls.

Linux should have a similar set of function calls.

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
1,871 Views

Jim, I don't understand your first comment. The code (which is not mine) enumerates all the enabled processors on the system.

I agree that the OS should do this, and yes, GetLogicalProcessorInformation is great for Windows, but unfortunately it is new in Windows Server 2003 and is not supported in XP and earlier. Perhaps Windows 2000 and NT we can ignore, but XP will be around for a long time.

From what I can tell, Linux does not have an API for this.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,871 Views

CPUID, when executed by an application, returns the CPU ID register of the processor which executed the CPUID instruction. i.e. It won't tell you the CPU IDs of the other processors in the system. It would be unwise to assume that all processors are identical within a system. It may be true to assume this most of the time, but it won't be true all of the time. On a large NUMA system with multiple motherboards it would not be unusual for each motherboard to have different model lines of processors.

Since you pointed out presense or lack of function support depending on O/S, the recommendation I make to my customers is remove the auto configure for affinity and simply provide environment variables to specify the preferred affinity usage. A simple test program could determine a suggested preferred affinity setting.

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
1,871 Views

Shihjong Kuo, one of the authors of the paper I referenced, asked me to post the following comment:

To help application deal with affinity binding and processor topology in scheduling resources, newer OSes provide APIs for applications to identify logical processors in relation to physical package, core, and HyperThreading Technology. In addition to GetLogicalProcessorInformation API (SYSTEM_LOGICAL_PROCESSOR_INFORMATION data structure) in newer Window OSes, newer Linux kernels have updated its /proc/cpuinfo system file to include processor topology information. The topology-related information available from /proc/cpuinfo now includes: "processor", "vendor id", "physical id", "siblings", "core id" and "cpu cores", and other fields. Of course, the /proc/cpuinfo file in older Linux does not have all of these fields.

Because API-based processor topology information are only available to newer OSes, we provided the white paper and link to source code to demonstrate how to enumerate processor topology and for situations that older OSes may not provide adequate information on processor topology.

Also, we worked hard to make sure that processor topology information from different OSes (different API, different terminology) are based on a consistent algorithm to enumerate processor topology, which you can get from our white paper and reference code.

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,871 Views

Thanks for the reference.

The CPUID (if you are permitted to execute it) will return the bonified info for the running processor.

The GetLogicalProcessorInformation will return what the OS wants you to know (assuming GetLogicalProcessorInformation is supported by the OS).

The /proc/cpuinfo file (or registry setting under Windows) is not much different than an environment variable. Text stored in a convienent place for retrieval. The registry might be more secure than a text file or environment variable.

Jim

0 Kudos
Steven_L_Intel1
Employee
1,871 Views

Jim,

I gather that you're not that familiar with Linux (I certainly am not). However, /proc/cpuinfo is not a "file", even though it looks like one. It's actually a "device" in Linux which, when you read from it, presents current data about the system based on what the running Linux can see. In that sense it's like the Win32 API routine but with a different interface. It's a bit strange to those of us used to routine-based APIs, but it works.

If you study the code that was attached to the article, you will see that it attempts to ensure that the CPUID instruction is executed on each logical processor through the manipulation of the affinity mask. Also, as the article states, the method described assumes homogenous processors. You mentioned NUMA which is not relevant here since the processors are all the same (at least in any supported system - good luck finding a SMP IA system that allows for mismatched processors.)

0 Kudos
richardbroadhurst
1,871 Views
It is supported by XP sp3, but doesn't seem to work for i7, reporting 1 core with 8 threads although it does work for Atom, 1 core 2 threads
0 Kudos
ngs_kec
Beginner
1,871 Views
SYSTEM_INFO sysinfo;
GetSystemInfo( &sysinfo );
int numCPU = sysinfo.dwNumberOfProcessors;
printf("\nThe number of Core's are %d\n", numCPU);
This small code will work fine on windows compiles(VC++). Also it will count multithreaded processors as two.
0 Kudos
Dave_Thompson
Beginner
1,871 Views
Did you figure out if GetLogicalProcessorInformation works on an iCore i7? I have a customer that reports its not working on an Microsoft Windows XP Service Pack 3 using Intel Core2 CPU 6400 @ 2.13GHz.

On this machine GetLogicalProcessorInformation returned 1 package, 1 core, 2 threads and hyper-threading.
0 Kudos
Grant_H_Intel
Employee
1,871 Views
[bash][/bash]
If you have any of the Intel compilers for Windows, you can try the following in a compiler command prompt window:

> set KMP_AFFINITY=verbose,none
> /Qopenmp
> .exe

The file can be as simple as this C code (as long as it contains the call to omp_get_num_procs):
get_num_procs.c:

[cpp]#include 
#include 

main () {
   printf("%dn", omp_get_num_procs());
}[/cpp]


It should use CPUID information that is available and print something like the following:

[plain]OMP: Info #204: KMP_AFFINITY: decoding cpuid leaf 11 APIC ids.

OMP: Info #202: KMP_AFFINITY: Affinity capable, using global cpuid leaf 11 info

OMP: Info #154: KMP_AFFINITY: Initial OS proc set respected: {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63} OMP: Info #156: KMP_AFFINITY: 64 available OS procs

OMP: Info #157: KMP_AFFINITY: Uniform topology

OMP: Info #179: KMP_AFFINITY: 4 packages x 8 cores/pkg x 2 threads/core (32 total cores)

OMP: Info #206: KMP_AFFINITY: OS proc to physical thread map:

OMP: Info #171: KMP_AFFINITY: OS proc 0 maps to package 0 core 0 thread 0

OMP: Info #171: KMP_AFFINITY: OS proc 32 maps to package 0 core 0 thread 1

...
[/plain]


The line that tells the total number of cores above will tell you whether hyperthreading is enabled. If it says "1 thread/core" it is disabled (or not present). If it says 2 or more threads per core it is enabled. The first lines will tell you where it is getting the information from. This is a handy utility to have around even if you don't use OpenMP programming for multi-threading.

Full documentation for this functionality is here:
http://software.intel.com/sites/products/documentation/studio/composer/en-us/2009/compiler_c/index.htm



0 Kudos
Roman_D_Intel
Employee
1,871 Views
According to thisdocumentation it looks like the GetLogicalProcessorInformation method to identify HT coresworks correctly only on Windows Vista and later MS OSes:
Windows Server 2003 and Windows XP Professional x64 Edition: [...] Therefore, to determine whether the processor supports multiple cores or hyperthreading on systems prior to Windows Vista, use the CPUID instruction.
Roman
0 Kudos
Reply