How do I get the core count?

I was wondering if someone knew how t get the core count of a the running machine.

1 Like

Hi,

on windows you can do it with WinApi GetLogicalProcessorInformation function (sysinfoapi.h) - Win32 apps | Microsoft Learn

Does epic not have a cross platform solution implemented?

cross platform solution implementation not really about epic and UE4 itself, it’s more between C++ developers, different OS and hardware developers

i don’t know how else software can get access to hardware except drivers and in case drives OS dependant i don’t sure C++ can offer natively same way for every OS+hardware combination, because it really too much depend on them

you may try dig into CPUID way descibed here c++ - How to Detect the Number of Physical Processors / Cores on Windows, Mac and Linux - Stack Overflow , but people there arguing about how correct result is

little question, what for you want know number of cores?

you should know that “one thread designed code” won’t work faster on multicore CPU because code logic can’t be executed separatly and in case of code written on “events” in UE4 probably would be auto managed over all CPU because of C++ compilier and OS optimisation for multycore CPU itself

better result you probably may get only in case you’re a hardcore geek and know too much about inner OS, drives and hardware implementations, guess it’s not about you and me :slight_smile:

Epic has a solution implemented but it doesn’t seem to be working correctly.

After more hours that I would like to to admit of searching the source, I came across this:
FGenericPlatformMisc::NumberOfCoresIncludingHyperthreads

It detects my cores but despite “IncludingHyperthreads”, it’s not detecting them.

::EDIT::

I posted a bug report with a possible fix HERE.

In the meantime FPlatformMisc::NumberOfCoresIncludingHyperthreads() is a valid solution.

i’m lack knowledges on core optimisation, but guess, it’s WinAPI GetLogicalProcessorInformation function (sysinfoapi.h) - Win32 apps | Microsoft Learn
GetLogicalProcessorInformation

int32 FWindowsPlatformMisc::NumberOfCores()
{
	static int32 CoreCount = 0;
	if (CoreCount == 0)
	{
		if (FParse::Param(FCommandLine::Get(), TEXT("usehyperthreading")))
		{
			CoreCount = NumberOfCoresIncludingHyperthreads();
		}
		else
		{
			// Get only physical cores
			PSYSTEM_LOGICAL_PROCESSOR_INFORMATION InfoBuffer = NULL;
			::DWORD BufferSize = 0;

			// Get the size of the buffer to hold processor information.
			::BOOL Result = GetLogicalProcessorInformation(InfoBuffer, &BufferSize);
			check(!Result && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
			check(BufferSize > 0);

			// Allocate the buffer to hold the processor info.
			InfoBuffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)FMemory::Malloc(BufferSize);
			check(InfoBuffer);

			// Get the actual information.
			Result = GetLogicalProcessorInformation(InfoBuffer, &BufferSize);
			check(Result);

			// Count physical cores
			const int32 InfoCount = (int32)(BufferSize / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION));
			for (int32 Index = 0; Index < InfoCount; ++Index)
			{
				SYSTEM_LOGICAL_PROCESSOR_INFORMATION* Info = &InfoBuffer[Index];
				if (Info->Relationship ==  RelationProcessorCore)
				{
					CoreCount++;
				}
			}
			FMemory::Free(InfoBuffer);
		}
	}
	return CoreCount;
}

i don’t sure code sample from WinAPI will give you different result, the only way check it - experiment, so compile it and see if result same or not, if result same, then maybe you misunderstood something in difinitions of logical/physical cores and what actually matter or can be used, also you have to note that maybe there’s no need to manually drive cores, because OS have own optimisation and split CPU load on cores, the only thing you need to do is allow multythreading so separate threads can be executed on other cores, the only way achieve it i know is FTickFunction and it’s property bRunOnAnyThread FTickFunction | Unreal Engine Documentation but have no example of use

I did do a test using the proper implimentation adn got the results I expected. I dug through the UE4 source some more and found the issue. I believe it to be a bug and posted a bug report.

since you did investigation and sure it’s bug, advice you report it on forum https://forums.unrealengine.com/forumdisplay.php?24-Feedback-for-Epic just to be sure Epic Staff see it

and i really interested what for you want know number of cores on device? are you about to manage them manually, want calculate maximum load CPU capasity?

You should use our generic API wrappers to do this so the code works on all platforms:

FPlatformMisc::NumberOfCores()
FPlatformMisc::NumberOfCoresIncludingHyperthreads()

Also don’t use the FGenericPlatform* directly since that means you’ll get the wrong version of that code when it’s different for a given platform. These aren’t virtual functions. We use typecasting to make it appear as if they are. We do this to avoid the virtual function overhead.

I figured that out and posted it in my answer along with a post on what I believe is a bug.