-
Notifications
You must be signed in to change notification settings - Fork 11.8k
fix get_num_physical_cores() #1436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ | |||||
#include <iterator> | ||||||
#include <algorithm> | ||||||
#include <sstream> | ||||||
#include <unordered_set> | ||||||
|
||||||
#if defined(__APPLE__) && defined(__MACH__) | ||||||
#include <sys/types.h> | ||||||
|
@@ -28,21 +29,21 @@ | |||||
|
||||||
int32_t get_num_physical_cores() { | ||||||
#ifdef __linux__ | ||||||
std::ifstream cpuinfo("/proc/cpuinfo"); | ||||||
std::string line; | ||||||
while (std::getline(cpuinfo, line)) { | ||||||
std::size_t pos = line.find("cpu cores"); | ||||||
if (pos != std::string::npos) { | ||||||
pos = line.find(": ", pos); | ||||||
if (pos != std::string::npos) { | ||||||
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 | ||||||
} | ||||||
} | ||||||
// enumerate the set of thread siblings, num entries is num cores | ||||||
std::unordered_set<std::string> siblings; | ||||||
for (uint32_t cpu=0; cpu < UINT32_MAX; ++cpu) { | ||||||
std::ifstream thread_siblings("/sys/devices/system/cpu" | ||||||
+ std::to_string(cpu) + "/topology/thread_siblings"); | ||||||
if (!thread_siblings.is_open()) { | ||||||
break; // no more cpus | ||||||
} | ||||||
std::string line; | ||||||
if (std::getline(thread_siblings, line)) { | ||||||
siblings.insert(line); | ||||||
} | ||||||
} | ||||||
if (siblings.size() > 0) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
Suggested change
Additional context/usr/include/c++/11/bits/unordered_set.h:298: method 'unordered_set'::empty() defined here empty() const noexcept
^ |
||||||
return static_cast<int32_t>(siblings.size()); | ||||||
} | ||||||
#elif defined(__APPLE__) && defined(__MACH__) | ||||||
int32_t num_physical_cores; | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be
"/sys/devices/system/cpu/cpu" + std::to_string(cpu) + "/topology/thread_siblings")
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, do you want to open a PR with fix?