Skip to content

Commit 079ac82

Browse files
committed
Add support for cpu_get_num_phsical_cores() on Windows
1 parent c887d8b commit 079ac82

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

common/common.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,34 @@ using json = nlohmann::ordered_json;
8080
//
8181
// CPU utils
8282
//
83+
#ifdef _WIN32
84+
int32_t cpu_get_num_cores_win(bool print_physical_core_num) {
85+
DWORD buffer_size = 0;
86+
GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &buffer_size);
87+
std::vector<char> buffer(buffer_size);
88+
if (!GetLogicalProcessorInformationEx(RelationProcessorCore, reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(buffer.data()), &buffer_size)) {
89+
return 0;
90+
}
91+
92+
int num_cores_win = 0;
93+
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(buffer.data());
94+
while (buffer_size > 0) {
95+
if (info->Relationship == RelationProcessorCore) {
96+
if (print_physical_core_num) {
97+
num_cores_win += info->Processor.GroupCount;
98+
} else {
99+
for (WORD i = 0; i < info->Processor.GroupCount; ++i) {
100+
num_cores_win += __popcnt64(info->Processor.GroupMask[i].Mask);
101+
}
102+
}
103+
}
104+
buffer_size -= info->Size;
105+
info = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(reinterpret_cast<char*>(info) + info->Size);
106+
}
107+
108+
return num_cores_win;
109+
}
110+
#endif
83111

84112
int32_t cpu_get_num_physical_cores() {
85113
#ifdef __linux__
@@ -111,7 +139,7 @@ int32_t cpu_get_num_physical_cores() {
111139
return num_physical_cores;
112140
}
113141
#elif defined(_WIN32)
114-
//TODO: Implement
142+
return cpu_get_num_cores_win(true);
115143
#endif
116144
unsigned int n_threads = std::thread::hardware_concurrency();
117145
return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;
@@ -1716,7 +1744,11 @@ std::string gpt_params_get_system_info(const gpt_params & params) {
17161744
if (params.n_threads_batch != -1) {
17171745
os << " (n_threads_batch = " << params.n_threads_batch << ")";
17181746
}
1747+
#ifdef _WIN32
1748+
os << " / " << cpu_get_num_cores_win(false) << " | " << llama_print_system_info();
1749+
#else
17191750
os << " / " << std::thread::hardware_concurrency() << " | " << llama_print_system_info();
1751+
#endif
17201752

17211753
return os.str();
17221754
}

0 commit comments

Comments
 (0)