← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
ck_topology.c
Go to the documentation of this file.
1/**
2 * @file ck_topology.c
3 * @brief Generic CPU topology discovery (affinity-aware, capacity-based)
4 *
5 * CK-ENGINE KERNEL RULES:
6 * =======================
7 * 1. NO malloc/free - memory via bump allocator, pointers passed in
8 * 2. NO OpenMP - parallelization at orchestrator/codegen layer
9 * 3. API must define: inputs, outputs, workspace, and memory layouts
10 * 4. Pure computation - deterministic, no side effects
11 *
12 * Discovery is cached process-wide (pthread_once) because sysfs content and
13 * the process affinity mask are effectively fixed for the lifetime of an
14 * inference process, and dispatch paths query this on every GEMV.
15 */
16
17#ifndef _GNU_SOURCE
18#define _GNU_SOURCE
19#endif
20
21#include "ck_topology.h"
22
23#include <pthread.h>
24#include <stdio.h>
25#include <stdlib.h>
26
27#ifdef __linux__
28#include <sched.h>
29#include <unistd.h>
30#endif
31
32/* Bound on discovered CPUs; CPU_SETSIZE is 1024 on Linux. */
33#define CK_TOPOLOGY_MAX_CPUS 1024
34
35/* Capacity ratio above which the allowed set counts as heterogeneous
36 * (GB10 Cortex-X925 vs Cortex-A725 is ~1.4; SMT siblings are ~1.0). */
37#define CK_TOPOLOGY_HETEROGENEOUS_RATIO_X100 115
38
39/* A CPU belongs to the top tier when its capacity is within 90% of the max
40 * (per-core capacity values differ slightly between otherwise identical
41 * big cores, e.g. 997 vs 1024). */
42#define CK_TOPOLOGY_TOP_TIER_PCT 90
43
44typedef struct {
45 int n_allowed;
46 int heterogeneous;
47 int n_big;
48 int big_cpus[CK_TOPOLOGY_MAX_CPUS];
49} ck_topology_state_t;
50
51static ck_topology_state_t g_topology;
52static pthread_once_t g_topology_once = PTHREAD_ONCE_INIT;
53
54#ifdef __linux__
55static long read_cpu_capacity(int cpu)
56{
57 char path[128];
58 snprintf(path, sizeof(path),
59 "/sys/devices/system/cpu/cpu%d/cpu_capacity", cpu);
60 FILE *f = fopen(path, "r");
61 if (f) {
62 long v = -1;
63 if (fscanf(f, "%ld", &v) != 1) v = -1;
64 fclose(f);
65 if (v > 0) return v;
66 }
67 /* Fallback: cpufreq maximum frequency in kHz. */
68 snprintf(path, sizeof(path),
69 "/sys/devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", cpu);
70 f = fopen(path, "r");
71 if (!f) return -1;
72 long v = -1;
73 if (fscanf(f, "%ld", &v) != 1) v = -1;
74 fclose(f);
75 return v;
76}
77#endif
78
79static void topology_discover(void)
80{
81 ck_topology_state_t *t = &g_topology;
82 t->n_allowed = 1;
83 t->heterogeneous = 0;
84 t->n_big = 1;
85 t->big_cpus[0] = 0;
86
87#ifdef __linux__
88 cpu_set_t allowed;
89 CPU_ZERO(&allowed);
90 if (sched_getaffinity(0, sizeof(allowed), &allowed) != 0) {
91 return; /* keep the conservative single-CPU default */
92 }
93
94 long capacities[CK_TOPOLOGY_MAX_CPUS];
96 int n = 0;
97 long cap_min = 0, cap_max = 0;
98 for (int cpu = 0; cpu < CPU_SETSIZE && n < CK_TOPOLOGY_MAX_CPUS; ++cpu) {
99 if (!CPU_ISSET(cpu, &allowed)) continue;
100 ids[n] = cpu;
101 capacities[n] = read_cpu_capacity(cpu);
102 if (capacities[n] > 0) {
103 if (cap_max == 0 || capacities[n] > cap_max) cap_max = capacities[n];
104 if (cap_min == 0 || capacities[n] < cap_min) cap_min = capacities[n];
105 }
106 ++n;
107 }
108 if (n == 0) {
109 return;
110 }
111 t->n_allowed = n;
112
113 if (cap_min <= 0 || cap_max * 100 <= cap_min * CK_TOPOLOGY_HETEROGENEOUS_RATIO_X100) {
114 /* Homogeneous or capacity unknown: every allowed CPU is top tier. */
115 t->heterogeneous = 0;
116 t->n_big = n;
117 for (int i = 0; i < n; ++i) t->big_cpus[i] = ids[i];
118 return;
119 }
120
121 t->heterogeneous = 1;
122 int nb = 0;
123 for (int i = 0; i < n && nb < CK_TOPOLOGY_MAX_CPUS; ++i) {
124 if (capacities[i] > 0 && capacities[i] * 100 >= cap_max * CK_TOPOLOGY_TOP_TIER_PCT) {
125 t->big_cpus[nb++] = ids[i];
126 }
127 }
128 if (nb == 0) {
129 t->n_big = n;
130 for (int i = 0; i < n; ++i) t->big_cpus[i] = ids[i];
131 return;
132 }
133 t->n_big = nb;
134#endif
135}
136
137static void topology_init(void)
138{
140}
141
143{
144 pthread_once(&g_topology_once, topology_init);
145 return g_topology.n_allowed;
146}
147
149{
150 pthread_once(&g_topology_once, topology_init);
151 return g_topology.heterogeneous;
152}
153
154int ck_topology_big_cpus(int *cpus, int max_cpus)
155{
156 pthread_once(&g_topology_once, topology_init);
157 if (!cpus || max_cpus <= 0) return 0;
158 int n = g_topology.n_big;
159 if (n > max_cpus) n = max_cpus;
160 for (int i = 0; i < n; ++i) {
161 cpus[i] = g_topology.big_cpus[i];
162 }
163 return n;
164}
int ck_topology_heterogeneous(void)
#define CK_TOPOLOGY_HETEROGENEOUS_RATIO_X100
Definition ck_topology.c:37
int ck_topology_big_cpus(int *cpus, int max_cpus)
static pthread_once_t g_topology_once
Definition ck_topology.c:52
static void topology_discover(void)
Definition ck_topology.c:79
int ck_topology_allowed_cpus(void)
static ck_topology_state_t g_topology
Definition ck_topology.c:51
static void topology_init(void)
#define CK_TOPOLOGY_TOP_TIER_PCT
Definition ck_topology.c:42
#define CK_TOPOLOGY_MAX_CPUS
Definition ck_topology.c:33
const int32_t * ids
Definition tokenizer.h:447