fix get_num_physical_cores()
had been broken on complex topologies because "cpu cores" in /proc/cpuinfo is per-"physical id"
This commit is contained in:
parent
08737ef720
commit
4561838719
1 changed files with 15 additions and 14 deletions
|
@ -8,6 +8,7 @@
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
#if defined(__APPLE__) && defined(__MACH__)
|
#if defined(__APPLE__) && defined(__MACH__)
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
@ -28,21 +29,21 @@
|
||||||
|
|
||||||
int32_t get_num_physical_cores() {
|
int32_t get_num_physical_cores() {
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
std::ifstream cpuinfo("/proc/cpuinfo");
|
// enumerate the set of thread siblings, num entries is num cores
|
||||||
std::string line;
|
std::unordered_set<std::string> siblings;
|
||||||
while (std::getline(cpuinfo, line)) {
|
for(uint32_t cpu=0; cpu < UINT32_MAX; ++cpu) {
|
||||||
std::size_t pos = line.find("cpu cores");
|
std::ifstream thread_siblings("/sys/devices/system/cpu"
|
||||||
if (pos != std::string::npos) {
|
+ std::to_string(cpu) + "/topology/thread_siblings");
|
||||||
pos = line.find(": ", pos);
|
if(!thread_siblings.is_open()) {
|
||||||
if (pos != std::string::npos) {
|
break; // no more cpus
|
||||||
try {
|
|
||||||
// Extract the number and return it
|
|
||||||
return static_cast<int32_t>(std::stoul(line.substr(pos + 2)));
|
|
||||||
} catch (const std::invalid_argument &) {
|
|
||||||
// Ignore if we could not parse
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
std::string line;
|
||||||
|
if(std::getline(thread_siblings, line)) {
|
||||||
|
siblings.insert(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(siblings.size() > 0) {
|
||||||
|
return static_cast<int32_t>(siblings.size());
|
||||||
}
|
}
|
||||||
#elif defined(__APPLE__) && defined(__MACH__)
|
#elif defined(__APPLE__) && defined(__MACH__)
|
||||||
int32_t num_physical_cores;
|
int32_t num_physical_cores;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue