20 #include <sys/sysinfo.h>
22 #include <sys/sysctl.h>
23 #include <sys/types.h>
40 std::ifstream proc_cpuinfo(
"/proc/cpuinfo");
41 const std::string physical_id(
"physical id");
42 const std::string core_id(
"core id");
45 using CoreEntry = std::pair<int, int>;
46 std::set<CoreEntry> cores;
47 CoreEntry current_core_entry;
50 while (std::getline(proc_cpuinfo, line)) {
54 std::vector<std::string> key_val =
56 if (key_val.size() != 2) {
57 return std::thread::hardware_concurrency();
61 if (key == physical_id) {
62 current_core_entry.first = std::stoi(value);
66 current_core_entry.second = std::stoi(value);
67 cores.insert(current_core_entry);
73 return cores.size() != 0 ? cores.size()
74 : std::thread::hardware_concurrency();
79 return sysctlbyname(
"hw.physicalcpu", &
count, &
size,
NULL, 0) ? 0
84 const BOOL result_first = GetLogicalProcessorInformationEx(
85 RelationProcessorCore,
nullptr, &
length);
86 assert(GetLastError() == ERROR_INSUFFICIENT_BUFFER);
88 std::unique_ptr<uint8_t[]> buffer(
new uint8_t[
length]);
89 const PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info =
90 reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX
>(
92 const BOOL result_second = GetLogicalProcessorInformationEx(
93 RelationProcessorCore, info, &
length);
94 assert(result_second !=
FALSE);
96 int nb_physical_cores = 0;
99 const PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX current_info =
100 reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX
>(
102 offset += current_info->Size;
105 return nb_physical_cores;
107 return std::thread::hardware_concurrency();
110 return std::thread::hardware_concurrency();
117 std::ifstream proc_cpuinfo(
"/proc/cpuinfo");
119 while (std::getline(proc_cpuinfo, line)) {
120 if (line.find(
"model name") == 0) {
121 size_t colon_pos = line.find(
':');
122 if (colon_pos != std::string::npos) {
123 std::string model_name = line.substr(colon_pos + 1);
130 char brand_string[256];
131 size_t size =
sizeof(brand_string);
132 if (sysctlbyname(
"machdep.cpu.brand_string", brand_string, &
size,
NULL,
134 return std::string(brand_string);
139 int cpuInfo[4] = {-1};
140 char brand_string[0x40] = {0};
143 __cpuid(cpuInfo, 0x80000000);
144 unsigned int nExIds = cpuInfo[0];
146 if (nExIds >= 0x80000004) {
148 for (
unsigned int i = 0x80000002; i <= 0x80000004; ++i) {
150 memcpy(brand_string + (i - 0x80000002) * 16, cpuInfo,
153 std::string
result(brand_string);
155 size_t first =
result.find_first_not_of(
" \t\n\r");
156 size_t last =
result.find_last_not_of(
" \t\n\r");
157 if (first != std::string::npos && last != std::string::npos) {
158 return result.substr(first, last - first + 1);
168 CPUInfo::CPUInfo() : impl_(new CPUInfo::Impl()) {
170 impl_->num_threads_ = std::thread::hardware_concurrency();
179 int CPUInfo::NumCores()
const {
return impl_->num_cores_; }
181 int CPUInfo::NumThreads()
const {
return impl_->num_threads_; }
183 const std::string& CPUInfo::ModelName()
const {
return impl_->model_name_; }
185 void CPUInfo::Print()
const {
186 if (!impl_->model_name_.empty()) {
188 impl_->model_name_, NumCores(), NumThreads());
__host__ __device__ float length(float2 v)
Helper functions for the ml ops.
ccGuiPythonInstance * GetInstance() noexcept
void SplitString(std::vector< std::string > &tokens, const std::string &str, const std::string &delimiters=" ", bool trim_empty_str=true)
std::string & StripString(std::string &str, const std::string &chars="\t\n\v\f\r ")
static std::string GetCPUModelName()
Returns the CPU model name/brand string.
static int PhysicalConcurrency()
Returns the number of physical CPU cores.
Generic file read and write utility for python interface.