Checking for multi-core

Started by
6 comments, last by ddn3 15 years, 9 months ago
Is there a way to check for the presence of a multi-core cpu? I've searched google and there doesn't seem to be a consistent or easy way of doing this. Does anyone know of any code snippet which can do this? Thanks! -ddn
Advertisement
I use the GLFW function GetNumberOfProcessors to find this out. You could download the GLFW code-base and see how it's implemented.
If you are in windows:

SYSTEM_INFO cSysInfo;
GetSystemInfo(&cSysInfo);

cSysInfo.dwNumberOfProcessors == The number of physical cores.

see: http://msdn.microsoft.com/en-us/library/ms724381(VS.85).aspx
On *nix you can, like was mentioned, use a library such as GLFW... or you can go right ahead and steal their code (as long as you give them credit and follow any possibly stricter licensing issues).

GLFW's platform.h (X11):
Quote:
// We support two different ways for getting the number of processors in
// the system: sysconf (POSIX) and sysctl (BSD?)
#if defined( _GLFW_HAS_SYSCONF )

// Use a single constant for querying number of online processors using
// the sysconf function (e.g. SGI defines _SC_NPROC_ONLN instead of
// _SC_NPROCESSORS_ONLN)
#ifndef _SC_NPROCESSORS_ONLN
#ifdef _SC_NPROC_ONLN
#define _SC_NPROCESSORS_ONLN _SC_NPROC_ONLN
#else
#error POSIX constant _SC_NPROCESSORS_ONLN not defined!
#endif
#endif

// Macro for querying the number of processors
#define _glfw_numprocessors(n) n=(int)sysconf(_SC_NPROCESSORS_ONLN)

#elif defined( _GLFW_HAS_SYSCTL )

#include <sys/types.h>
#include <sys/sysctl.h>

// Macro for querying the number of processors
#define _glfw_numprocessors(n) { int mib[2], ncpu; size_t len = 1; mib[0] = CTL_HW; mib[1] = HW_NCPU; n = 1; if( sysctl( mib, 2, &ncpu, &len, NULL, 0 ) != -1 ) { if( len > 0 ) { n = ncpu; } } }

#else

// If neither sysconf nor sysctl is supported, assume single processor
// system
#define _glfw_numprocessors(n) n=1

#endif
I'll be a smart-ass. I wrote my own 64-bit, multicore operating system, LoseThos, and everything has kernel privilege. Read the Intel datasheets ;-)

You can see how I did it: http://www.losethos.com/multicore.html

I assume you mean for some specific operating system. Can't help you. If you manage to get kernel privilege you can use my code.
On Intel Software Network there is nice sample: Detecting Multi-Core Processor Topology in an IA-32 Platform
I doubt there is a consistent way and most likely involves some assembly as this article that took me a minute to google shows:

Detecting Multi-Core Processors


In this entry we will show you how to detect the topological relationships between physical package, processor core, and logical processors sharing the same core in a multi-processing platform with IA-32 processors. The algorithm described in this paper applies across many hardware multi-processing configurations, including single-socket and multi-socket platforms, IA-32 processors supporting Hyper-Threading Technology, dual-core and multiple cores.

[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
Thanks all! Lots of great info.

-ddn

This topic is closed to new replies.

Advertisement