Skip to content

Distribute and run LLMs with a single file.

common.cpp: detect physical cores at runtime under cosmocc (fix #976) (#977)

Davide Eynardb2d21fd21507
diff --git a/llama.cpp.patches/patches/common_common.cpp.patch b/llama.cpp.patches/patches/common_common.cpp.patch
index dfbbcbfd..7e5a3001 100644
--- a/llama.cpp.patches/patches/common_common.cpp.patch
+++ b/llama.cpp.patches/patches/common_common.cpp.patch
@@ -1,7 +1,58 @@
diff --git a/common/common.cpp b/common/common.cpp
--- a/llama.cpp/common/common.cpp
+++ b/llama.cpp/common/common.cpp
-@@ -972,6 +972,16 @@ std::string fs_get_cache_directory() {
+@@ -33,6 +33,10 @@
+ #include <sys/sysctl.h>
+ #endif
+
++#if defined(COSMOCC)
++#include <cosmo.h> // for IsLinux() and sysctlbyname() declarations
++#endif
++
+ #if defined(_WIN32)
+ #define WIN32_LEAN_AND_MEAN
+ #ifndef NOMINMAX
+@@ -71,7 +75,38 @@ common_time_meas::~common_time_meas() {
+ //
+
+ int32_t common_cpu_get_num_physical_cores() {
+-#ifdef __linux__
++#if defined(COSMOCC)
++ // cosmocc cross-compiles for both Linux and macOS at once, so neither
++ // __linux__ nor __APPLE__ is defined here; detect the host OS at runtime.
++ if (IsLinux()) {
++ std::unordered_set<std::string> siblings;
++ for (uint32_t cpu=0; cpu < UINT32_MAX; ++cpu) {
++ std::ifstream thread_siblings("/sys/devices/system/cpu/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.empty()) {
++ return static_cast<int32_t>(siblings.size());
++ }
++ } else {
++ int32_t num_physical_cores;
++ size_t len = sizeof(num_physical_cores);
++ int result = sysctlbyname("hw.perflevel0.physicalcpu", &num_physical_cores, &len, NULL, 0);
++ if (result == 0) {
++ return num_physical_cores;
++ }
++ result = sysctlbyname("hw.physicalcpu", &num_physical_cores, &len, NULL, 0);
++ if (result == 0) {
++ return num_physical_cores;
++ }
++ }
++#elif defined(__linux__)
+ // 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) {
+@@ -972,6 +1007,16 @@ std::string fs_get_cache_directory() {
cache_directory = std::getenv("HOME") + std::string("/Library/Caches/");
#elif defined(_WIN32)
cache_directory = std::getenv("LOCALAPPDATA");
@@ -18,7 +69,7 @@ diff --git a/common/common.cpp b/common/common.cpp
#elif defined(__EMSCRIPTEN__)
GGML_ABORT("not implemented on this platform");
#else
-@@ -1148,10 +1158,31 @@ common_init_result::common_init_result(common_params & params) :
+@@ -1148,10 +1193,31 @@ common_init_result::common_init_result(common_params & params) :
if (params.fit_params) {
LOG_INF("%s: fitting params to device memory, for bugs during this step try to reproduce them with -fit off, or provide --verbose logs if the bug only occurs with -fit on\n", __func__);