Skip to content

Commit

Permalink
avoid adding new function
Browse files Browse the repository at this point in the history
  • Loading branch information
Septa2112 committed Aug 6, 2024
1 parent 1d1e6e8 commit 2e7243c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 37 deletions.
54 changes: 20 additions & 34 deletions common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,38 +80,6 @@ using json = nlohmann::ordered_json;
//
// CPU utils
//
#ifdef _WIN32
int32_t cpu_get_num_cores_win(bool print_physical_core_num) {
DWORD buffer_size = 0;
GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &buffer_size);
std::vector<char> buffer(buffer_size);
if (!GetLogicalProcessorInformationEx(RelationProcessorCore, reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(buffer.data()), &buffer_size)) {
return 0;
}

int num_cores_win = 0;
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(buffer.data());
while (buffer_size > 0) {
if (info->Relationship == RelationProcessorCore) {
if (print_physical_core_num) {
num_cores_win += info->Processor.GroupCount;
} else {
for (WORD i = 0; i < info->Processor.GroupCount; ++i) {
#ifdef _MSC_VER
num_cores_win += __popcnt64(info->Processor.GroupMask[i].Mask);
#else
num_cores_win += _popcnt64(info->Processor.GroupMask[i].Mask);
#endif
}
}
}
buffer_size -= info->Size;
info = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(reinterpret_cast<char*>(info) + info->Size);
}

return num_cores_win;
}
#endif

int32_t cpu_get_num_physical_cores() {
#ifdef __linux__
Expand Down Expand Up @@ -143,7 +111,24 @@ int32_t cpu_get_num_physical_cores() {
return num_physical_cores;
}
#elif defined(_WIN32)
return cpu_get_num_cores_win(true);
DWORD buffer_size = 0;
GetLogicalProcessorInformationEx(RelationProcessorCore, nullptr, &buffer_size);
std::vector<char> buffer(buffer_size);
if (!GetLogicalProcessorInformationEx(RelationProcessorCore, reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(buffer.data()), &buffer_size)) {
return 0;
}

int32_t num_physical_cores = 0;
PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX info = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(buffer.data());
while (buffer_size > 0) {
if (info->Relationship == RelationProcessorCore) {
num_physical_cores += info->Processor.GroupCount;
}
buffer_size -= info->Size;
info = reinterpret_cast<PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX>(reinterpret_cast<char*>(info) + info->Size);
}

return num_physical_cores;
#endif
unsigned int n_threads = std::thread::hardware_concurrency();
return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;
Expand Down Expand Up @@ -1749,7 +1734,8 @@ std::string gpt_params_get_system_info(const gpt_params & params) {
os << " (n_threads_batch = " << params.n_threads_batch << ")";
}
#ifdef _WIN32
os << " / " << cpu_get_num_cores_win(false) << " | " << llama_print_system_info();
DWORD logicalProcessorCount = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
os << " / " << logicalProcessorCount << " | " << llama_print_system_info();
#else
os << " / " << std::thread::hardware_concurrency() << " | " << llama_print_system_info();
#endif
Expand Down
3 changes: 0 additions & 3 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ struct llama_control_vector_load_info;
//
// CPU utils
//
#ifdef _WIN32
int32_t cpu_get_num_cores_win(bool print_physical_core_num);
#endif
int32_t cpu_get_num_physical_cores();
int32_t cpu_get_num_math();

Expand Down

0 comments on commit 2e7243c

Please sign in to comment.