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

How to get number of logical cores on Windows

bharatipingale
Beginner
3,018 Views

Hi all,

Does anybody know how to get number of logical processors on windows?

I tried command GetSystemInfo(), but it gives number of physical processors.

and GetLogicalProcessorInfo() is supported 2003 SP3 onwards.

Thanks in advance,

BharatiPingale

0 Kudos
15 Replies
TimP
Honored Contributor III
3,018 Views

http://stackoverflow.com/questions/188503/detecting-the-number-of-processors

Should work on all versions of Windows with threading support, including some which didn't have adequate support for HyperThreading.

0 Kudos
Dmitry_Vyukov
Valued Contributor I
3,018 Views
Quoting - bharatipingale

Hi all,

Does anybody know how to get number of logical processors on windows?

I tried command GetSystemInfo(), but it gives number of physical processors.

and GetLogicalProcessorInfo() is supported 2003 SP3 onwards.

Thanks in advance,

I believe that GetSystemInfo() must return number of LOGICAL processors (i.e. HT processor -> 2 processors).

Unfortunately I am unable to check this right now. Are you sure that it returns number of physical procesors (i.e. HT processor -> 1 processor)?

0 Kudos
Emmanuel_W_
New Contributor I
3,017 Views
Quoting - Dmitriy Vyukov

I believe that GetSystemInfo() must return number of LOGICAL processors (i.e. HT processor -> 2 processors).

Unfortunately I am unable to check this right now. Are you sure that it returns number of physical procesors (i.e. HT processor -> 1 processor)?

If this helps GetSystemInfo does return the number of logical processors on all my systems.

0 Kudos
Eric_P_Intel
Employee
3,017 Views
Quoting - tim18

http://stackoverflow.com/questions/188503/detecting-the-number-of-processors

Should work on all versions of Windows with threading support, including some which didn't have adequate support for HyperThreading.


If you just want the number of logical processors (currently booted/in-use by Windows), using GetSystemInfo will work. If you need to know more about the topology (packages, cores, HyperThreading), the "official" method can be found here:

http://software.intel.com/en-us/articles/intel-64-architecture-processor-topology-enumeration

Be wary of other implementations that use cpuid directly. (Using OS-provided info

i should

be ok).

- Eric

0 Kudos
Dmitry_Vyukov
Valued Contributor I
3,017 Views


If you just want the number of logical processors (currently booted/in-use by Windows), using GetSystemInfo will work. If you need to know more about the topology (packages, cores, HyperThreading), the "official" method can be found here:

http://software.intel.com/en-us/articles/intel-64-architecture-processor-topology-enumeration

Be wary of other implementations that use cpuid directly. (Using OS-provided info

i should

be ok).

One must understand that this way is not "how to do in on Windows", it's "how to do it on x86". This won't work for Itanium. So, if one's system is otherwise portable to all Windows hardware, after employing such thick it will become x86 specific.

For Linux it is a much more serious problem, because Linux also supports SPARC, PPC, ARM, etc.

However the good news about CPUID is that it will work both on Windows and Linux (while you are on x86, and figure out how to transform asm/intrinsic syntax).

0 Kudos
gaston-hillar
Valued Contributor I
3,018 Views
Hi bharatipingale,

I don't know which programming language you are using. However, here are some tips for three programming languages:

Java:
Runtime.getRuntime().availableProcessors();

C#:
Environment.ProcessorCount;

Visual Basic:
Environment.ProcessorCount

These return the number of logical cores on Windows and in other operating systems.
For example, if you run these lines on a computer with a quad-core Core i7 supporting Hyper-Threading, it will return 8.

Cheers,

Gastn Hillar
0 Kudos
pvonkaenel
New Contributor III
3,018 Views
Quoting - Dmitriy Vyukov

I believe that GetSystemInfo() must return number of LOGICAL processors (i.e. HT processor -> 2 processors).

Unfortunately I am unable to check this right now. Are you sure that it returns number of physical procesors (i.e. HT processor -> 1 processor)?


I agree: I think GetSystemInfo() does return the number of logical processors. I wrote some code a few years ago to return various pieces of processor information which may help. As has already been mentioned, this code is x86 specific:

[cpp]//****************************************************************************
// FUNCTION: ProcCtrl::IsIntel()
// ACTION: Returns true if this is an Intel processor, otherwise false.
// EXPORT: Public
// HISTORY:
//  Date       Author              Description
// 10/25/2004   Peter von Kaenel    created
//****************************************************************************
bool ProcCtrl::IsIntel()
{
	unsigned int reg[5];
	char *stamp;

	// Make sure this is an Intel Processor
	__asm {
		mov eax, 00h
		cpuid
		mov reg[0], eax
		mov reg[4], ebx
		mov reg[8], edx
		mov reg[12], ecx
	}
	stamp = (char*)[1];
	stamp[12] = '�';
	if (strcmp("GenuineIntel", stamp) == 0)
		return true;

	return false;
}

//****************************************************************************
// FUNCTION: ProcCtrl::NumProcs()
// ACTION: Return the total number of processors recognized by the OS.  If
//         hyper-threading is enabled, then a single physical processor will
//         be identified as multiple processors.
// EXPORT: Public
// HISTORY:
//  Date       Author              Description
// 10/25/2004   Peter von Kaenel    created
//****************************************************************************
I32 ProcCtrl::NumProcs()
{
	// Get the total number of processoes in the computer
	SYSTEM_INFO sysInfo;
	GetSystemInfo(&sysInfo);
	return sysInfo.dwNumberOfProcessors;
}

//****************************************************************************
// FUNCTION: ProcCtrl::NumPhysicalProcs()
// ACTION: Returns the number of physical processors in the computer.
// EXPORT: Public
// HISTORY:
//  Date       Author              Description
// 10/25/2004   Peter von Kaenel    created
//****************************************************************************
I32 ProcCtrl::NumPhysicalProcs()
{
	// Get the number of logical processors per physical processor: this is
	// >1 if hyper-threading is enabled.
	I32 numLogicalProcs = NumLogicalProcs();

	// Get the total number of processoes on the system and devide it by
	// the number of logical processors per physical processor.
	return (NumProcs() / numLogicalProcs);
}

//****************************************************************************
// FUNCTION: ProcCtrl::NumLogicalProcs()
// ACTION: Return the number of logical processors per physical processor.
//         This will be >1 if hyper-threading is enabled.  Note that this does
//         not return the total number of processors, just the number of logical
//         processors per physical processor.  Call NumProcs() to get the
//         total number of processors.
// EXPORT: Public
// HISTORY:
//  Date       Author              Description
// 10/25/2004   Peter von Kaenel    created
//****************************************************************************
I32 ProcCtrl::NumLogicalProcs()
{
	// If this is not an Intel processor, there is no need to check further
	if (IsIntel() == false)
		return 1;

	// Determine if hyper-threading is available and enabled.  If it is, then
	// probe the number of logical processors.
	U32 regEdx, regEbx;
 	__asm {
		mov eax, 01h
		cpuid
		mov regEdx, edx
		mov regEbx, ebx
	}
	if (regEdx & BIT(28))
		return (regEbx & 0xff0000) >> 16;

	return 1;
}

[/cpp]

0 Kudos
TimP
Honored Contributor III
3,018 Views
Intel C++ is supposed to support standard C++ OpenMP syntax at the next major release, allowing use of omp_get_num_procs in a num_threads specification:

#ifdef _OPENMP
#include
#endif

........

#pragma omp parallel for num_threads(min(4,omp_get_num_procs()))

For now, you can write it out, C style. The Fortran equivalent to the above works, even in (linux) gfortran now.

I'm not very excited about the recommendations to use non-standard C++ syntax with processor and compiler dependent asm.
0 Kudos
Dmitry_Vyukov
Valued Contributor I
3,018 Views
Quoting - tim18
I'm not very excited about the recommendations to use non-standard C++ syntax with processor and compiler dependent asm.

Non-standard C++ syntax or non-standard C++ functions and extensions, what's the difference? Results is exactly the same - non-portable code. Is it possible to get standard way to get number of processors from the language which does not support multi-threading? All bets are on C++09, when ICC will support C++09 threading extensions? So that we will be able to use std::thread::hardware_concurrency().

0 Kudos
gaston-hillar
Valued Contributor I
3,018 Views
can also help you. It uses C++ and a library to detect logical and physical cores.

I think you have a lot of options to work with!

Cheers,

Gastn
0 Kudos
anandakumaryg
Beginner
3,018 Views
Quoting - pvonkaenel

I agree: I think GetSystemInfo() does return the number of logical processors. I wrote some code a few years ago to return various pieces of processor information which may help. As has already been mentioned, this code is x86 specific:

[cpp]//****************************************************************************
// FUNCTION: ProcCtrl::IsIntel()
// ACTION: Returns true if this is an Intel processor, otherwise false.
// EXPORT: Public
// HISTORY:
//  Date       Author              Description
// 10/25/2004   Peter von Kaenel    created
//****************************************************************************
bool ProcCtrl::IsIntel()
{
	unsigned int reg[5];
	char *stamp;

	// Make sure this is an Intel Processor
	__asm {
		mov eax, 00h
		cpuid
		mov reg[0], eax
		mov reg[4], ebx
		mov reg[8], edx
		mov reg[12], ecx
	}
	stamp = (char*)[1];
	stamp[12] = '�';
	if (strcmp("GenuineIntel", stamp) == 0)
		return true;

	return false;
}

//****************************************************************************
// FUNCTION: ProcCtrl::NumProcs()
// ACTION: Return the total number of processors recognized by the OS.  If
//         hyper-threading is enabled, then a single physical processor will
//         be identified as multiple processors.
// EXPORT: Public
// HISTORY:
//  Date       Author              Description
// 10/25/2004   Peter von Kaenel    created
//****************************************************************************
I32 ProcCtrl::NumProcs()
{
	// Get the total number of processoes in the computer
	SYSTEM_INFO sysInfo;
	GetSystemInfo(&sysInfo);
	return sysInfo.dwNumberOfProcessors;
}

//****************************************************************************
// FUNCTION: ProcCtrl::NumPhysicalProcs()
// ACTION: Returns the number of physical processors in the computer.
// EXPORT: Public
// HISTORY:
//  Date       Author              Description
// 10/25/2004   Peter von Kaenel    created
//****************************************************************************
I32 ProcCtrl::NumPhysicalProcs()
{
	// Get the number of logical processors per physical processor: this is
	// >1 if hyper-threading is enabled.
	I32 numLogicalProcs = NumLogicalProcs();

	// Get the total number of processoes on the system and devide it by
	// the number of logical processors per physical processor.
	return (NumProcs() / numLogicalProcs);
}

//****************************************************************************
// FUNCTION: ProcCtrl::NumLogicalProcs()
// ACTION: Return the number of logical processors per physical processor.
//         This will be >1 if hyper-threading is enabled.  Note that this does
//         not return the total number of processors, just the number of logical
//         processors per physical processor.  Call NumProcs() to get the
//         total number of processors.
// EXPORT: Public
// HISTORY:
//  Date       Author              Description
// 10/25/2004   Peter von Kaenel    created
//****************************************************************************
I32 ProcCtrl::NumLogicalProcs()
{
	// If this is not an Intel processor, there is no need to check further
	if (IsIntel() == false)
		return 1;

	// Determine if hyper-threading is available and enabled.  If it is, then
	// probe the number of logical processors.
	U32 regEdx, regEbx;
 	__asm {
		mov eax, 01h
		cpuid
		mov regEdx, edx
		mov regEbx, ebx
	}
	if (regEdx & BIT(28))
		return (regEbx & 0xff0000) >> 16;

	return 1;
}

[/cpp]


0 Kudos
anandakumaryg
Beginner
3,018 Views
Quoting - anandakumaryg


I have tried the above sample to get the number of processor available in my PC. Now I have two processor with 8 cores per processor. My problem is m_dwNumberofprocessor and logical processor are same, so when i divide the numberofprocessor/logicalprocessor, I am getting 1. But I should get 2.

I followedd the logic given above.

Please help me out to solve this issue.
0 Kudos
pvonkaenel
New Contributor III
3,018 Views
Quoting - anandakumaryg

I have tried the above sample to get the number of processor available in my PC. Now I have two processor with 8 cores per processor. My problem is m_dwNumberofprocessor and logical processor are same, so when i divide the numberofprocessor/logicalprocessor, I am getting 1. But I should get 2.

I followedd the logic given above.

Please help me out to solve this issue.

I'm afraid I no longer have a multi-die computer to test this out on, but let's see what we can figure out. What exactly is being returned in sysInfo.dwNumberOfProcessors? Also, what is ProcCtrl::NumLogicalProcs() returning? I'm wondering if ProcCtrl::IsIntel() is not identifying your machine as an Intel based machine.

Peter
0 Kudos
anandakumaryg
Beginner
3,018 Views
Quoting - pvonkaenel

I'm afraid I no longer have a multi-die computer to test this out on, but let's see what we can figure out. What exactly is being returned in sysInfo.dwNumberOfProcessors? Also, what is ProcCtrl::NumLogicalProcs() returning? I'm wondering if ProcCtrl::IsIntel() is not identifying your machine as an Intel based machine.

Peter

Hi pvonkaenel,
Thanks for the reply.

sysInfo.dwNumberOfProcessors returns number of logical processor not physical processor count. ProcCtrl::IsIntel() is identifying the processor as Intel.

Could you suggest a simple way to get the physical processor count.

Thanks.




0 Kudos
pvonkaenel
New Contributor III
3,018 Views
Quoting - anandakumaryg

Hi pvonkaenel,
Thanks for the reply.

sysInfo.dwNumberOfProcessors returns number of logical processor not physical processor count. ProcCtrl::IsIntel() is identifying the processor as Intel.

Could you suggest a simple way to get the physical processor count.

Thanks.





I'm afraid that if ProcCtrl::NumLogicalProcs() is not returning the number of cores per die, then I'm not sure. Although, if you have the IPP library available, you could replace the call to ProcCtrl::NumLogicalProcs() with ippGetNumCoresOnDie().

Peter
0 Kudos
Reply