Added set thread affinity for Linux

This commit is contained in:
mann1x 2024-04-26 15:09:17 +02:00
parent a3e75fe481
commit f7d2c0a5cd
2 changed files with 35 additions and 2 deletions

View file

@ -231,6 +231,8 @@ int32_t setCpuAffinity(std::bitset<64> cpuMask) {
if (cpuMask[i] == 1) {
CPUSET_PRINT_DEBUG("Setting CPU %d\n", i);
CPU_SET(i, &mask);
} else {
CPU_CLR(i, &mask);
}
}
@ -328,7 +330,10 @@ int32_t get_num_physical_cores() {
is_hybrid_core = false;
if (is_hybrid) {
if (pin_cpu(cpu) == 0) {
if (is_running_on_efficiency_core()) is_hybrid_core = true;
if (is_running_on_efficiency_core()) {
is_hybrid_core = true;
CPUSET_PRINT_DEBUG("Logical CPU is Hybrid: %d\n", cpu);
}
}
}
numLogicalCores++;
@ -365,6 +370,7 @@ int32_t get_num_physical_cores() {
CPU_SET(cpu, &mask);
CPUSET_PRINT_DEBUG("CPU %u is physical, siblings: %s\n", cpu, line.c_str());
} else {
CPU_CLR(cpu, &mask);
cpuset_smt = true;
}
}
@ -378,7 +384,7 @@ int32_t get_num_physical_cores() {
if (pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) == -1) {
CPUSET_PRINT_DEBUG("pthread_setaffinity_np error\n");
}
fprintf(stderr, "get_num_physical_cores Physical CPU count: %li\n", siblings.size());
CPUSET_PRINT_DEBUG("get_num_physical_cores Physical CPU count: %li\n", siblings.size());
cpuset_best = cpuset;
cpuset_worst = cpuset;

27
ggml.c
View file

@ -18708,6 +18708,33 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl
};
const int rc = ggml_thread_create(&workers[j].thrd, NULL, ggml_graph_compute_thread, &workers[j]);
#if defined(__x86_64__) && defined(__linux__)
cpu_set_t procMask;
cpu_set_t threadMask;
//fprintf(stdout, "sched_getaffinity init\n");
if (sched_getaffinity(0, sizeof(cpu_set_t), &procMask) == -1) {
fprintf(stderr, "ggml_thread_create sched_getaffinity error\n");
} else {
int result = pthread_setaffinity_np(workers[j].thrd, sizeof(cpu_set_t), &procMask);
if (result !=0) fprintf(stderr, "ggml_thread_create pthread_setaffinity_np: %d", result);
//printf("Set returned by sched_getaffinity() contained:\n");
//for (size_t k = 0; k < CPU_SETSIZE; k++)
//if (CPU_ISSET(k, &procMask))
//printf(" CPU %zu\n", k);
}
/*
int s;
s = pthread_getaffinity_np(workers[j].thrd, sizeof(threadMask), &threadMask);
if (s != 0) {
fprintf(stderr, "ggml_thread_create pthread_getaffinity_np: %d\n", s);
} else {
printf("Set returned by pthread_getaffinity_np() contained:\n");
for (size_t l = 0; l < CPU_SETSIZE; l++)
if (CPU_ISSET(l, &threadMask))
printf(" CPU %zu\n", l);
}
*/
#endif
GGML_ASSERT(rc == 0);
UNUSED(rc);
}