4

I have tried sysconf(_SC_NPROCESSORS_ONLN) and sysconf(_SC_NPROCESSORS_CONF), but they both return total number of (as Intel calls it in their CPU documentation) Threads (as in: hyper-threading threads), not physical cores (called Core on mentioned Intel site).

Is there a way to get number of physical cores, instead of logical? Counting entries in /proc/cpuinfo gives 8, similarly to calling sysconf, and my processor is the one linked above.

I'm interested in answers working on Linux and BSDs, preferably in form of C API.

Griwes
  • 8,805
  • 2
  • 43
  • 70

3 Answers3

7

A different solution is to use hwloc. Here's a simple example:

#include <hwloc.h>
#include <stdio.h>

int main(){

  // Allocate, initialize, and perform topology detection
  hwloc_topology_t topology;
  hwloc_topology_init(&topology);
  hwloc_topology_load(topology);

  // Try to get the number of CPU cores from topology
  int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_CORE);
  if(depth == HWLOC_TYPE_DEPTH_UNKNOWN)
    printf("*** The number of cores is unknown\n");
  else
    printf("*** %u core(s)\n", hwloc_get_nbobjs_by_depth(topology, depth));

  // Destroy topology object and return
  hwloc_topology_destroy(topology);
  return 0;
}

I tested this on a Linux box running Red Hat 4.1.2-48 with GCC 4.1.2, and also on an Apple running OS X 10.8.1 with GCC 4.2.1

Douglas B. Staple
  • 10,510
  • 8
  • 31
  • 58
  • 1
    On Ubuntu 12.04, `apt-get install libhwloc-dev`, and link with `-lhwloc`. This code gave the right answers for a core2duo e8400 and an i7-3770, 2 and 4 cores respectively. – Camille Goudeseune Feb 26 '14 at 20:14
  • On AMD Piledriver 6380, hwloc reports 16 cores, which is correct from AMD's perspective as there are 16 integer pipelines, but only 8 floating point pipelines: [AMD architecture](http://linustechtips.com/main/topic/48571-intel-amd-architectural-discussion-how-far-ahead-is-intel/). Counting unique (physical id, core id) combinations in /proc/cpuinfo, as suggested by @liori, yields 8. Slightly ambiguous what to call a "core" on that architecture. – Mark Gates Apr 01 '14 at 18:49
  • @MarkGates Yes, I find what AMD calls a 'core' totally misleading. It threw me for a loop on this question: http://stackoverflow.com/questions/19780554/what-limits-scaling-in-this-simple-openmp-program – Douglas B. Staple Apr 01 '14 at 23:58
4

It is not a C API, and it probably works only on Linux. But this is all I know, maybe you'll find this useful.

/proc/cpuinfo's CPU descriptions have few fields: physical id, which is a physical CPU identifier and core id, which is the physical core identifier. If you calculate number of unique (physical id, core id) pairs, you'll get what you want.

You can also check the cpu cores field for each physical CPU listed.

liori
  • 40,917
  • 13
  • 78
  • 105
  • 1
    Right, counting that should work without problems. Yet, I'm going to wait with accepting this, in case someone comes with some less "stupid-file-parsing"ish solution within reasonable amount of time. – Griwes Sep 18 '12 at 19:24
2

The files in /sys/devices/system/cpu/cpu<n> are much easier to parse for this sort of information, and include additional information about topology. It's still not a pre-wrapped API, but if all you're looking for is CPU count and possibly which threads belong to which cores belong to which chips, writing something to parse this would be not too bad. I know there are some libraries (e.g. the cgroups stuff) that already parse this, so there are reference points to find good ways to go about it, and there might even be a way to just use parts of those libraries if you want.

twalberg
  • 59,951
  • 11
  • 89
  • 84
  • Yay, counting `.../cpu` and filtering it based on `.../cpu/topology/thread_siblings_list`s seems much easier (or less "I'm writing that again"ish) than parsing `/proc/cpuinfo`. One question: I'm currently on Linux, so I can see that it's supported fine. Is it also supported on BSDs? – Griwes Sep 18 '12 at 21:01
  • The `/sys` file system is something that's generated by the Linux kernel, so, if it's not running a Linux kernel, or if specific options weren't included in the kernel when it was compiled, that info won't be there. I don't personally have any experience with the BSDs (at least recently), so I can't say for sure... – twalberg Sep 18 '12 at 21:11