← Back to C-Kernel-Engine Docs Doxygen Source Documentation
Loading...
Searching...
No Matches
ck_topology.c File Reference

Generic CPU topology discovery (affinity-aware, capacity-based). More...

#include "ck_topology.h"
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>

Go to the source code of this file.

Macros

#define _GNU_SOURCE
#define CK_TOPOLOGY_HETEROGENEOUS_RATIO_X100   115
#define CK_TOPOLOGY_MAX_CPUS   1024
#define CK_TOPOLOGY_TOP_TIER_PCT   90

Functions

int ck_topology_allowed_cpus (void)
int ck_topology_big_cpus (int *cpus, int max_cpus)
int ck_topology_heterogeneous (void)
static void topology_discover (void)
static void topology_init (void)

Variables

static ck_topology_state_t g_topology
static pthread_once_t g_topology_once = PTHREAD_ONCE_INIT

Detailed Description

Generic CPU topology discovery (affinity-aware, capacity-based).

CK-ENGINE KERNEL RULES:

  1. NO malloc/free - memory via bump allocator, pointers passed in
  2. NO OpenMP - parallelization at orchestrator/codegen layer
  3. API must define: inputs, outputs, workspace, and memory layouts
  4. Pure computation - deterministic, no side effects

Discovery is cached process-wide (pthread_once) because sysfs content and the process affinity mask are effectively fixed for the lifetime of an inference process, and dispatch paths query this on every GEMV.

Definition in file ck_topology.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE

Definition at line 18 of file ck_topology.c.

◆ CK_TOPOLOGY_HETEROGENEOUS_RATIO_X100

#define CK_TOPOLOGY_HETEROGENEOUS_RATIO_X100   115

Definition at line 37 of file ck_topology.c.

Referenced by topology_discover().

◆ CK_TOPOLOGY_MAX_CPUS

#define CK_TOPOLOGY_MAX_CPUS   1024

Definition at line 33 of file ck_topology.c.

Referenced by topology_discover().

◆ CK_TOPOLOGY_TOP_TIER_PCT

#define CK_TOPOLOGY_TOP_TIER_PCT   90

Definition at line 42 of file ck_topology.c.

Referenced by topology_discover().

Function Documentation

◆ ck_topology_allowed_cpus()

int ck_topology_allowed_cpus ( void )

Definition at line 142 of file ck_topology.c.

143{
144 pthread_once(&g_topology_once, topology_init);
145 return g_topology.n_allowed;
146}
static pthread_once_t g_topology_once
Definition ck_topology.c:52
static ck_topology_state_t g_topology
Definition ck_topology.c:51
static void topology_init(void)

References g_topology, g_topology_once, and topology_init().

◆ ck_topology_big_cpus()

int ck_topology_big_cpus ( int * cpus,
int max_cpus )

Definition at line 154 of file ck_topology.c.

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}

References g_topology, g_topology_once, and topology_init().

Referenced by ck_threadpool_maybe_pin_big_cores().

◆ ck_topology_heterogeneous()

int ck_topology_heterogeneous ( void )

Definition at line 148 of file ck_topology.c.

149{
150 pthread_once(&g_topology_once, topology_init);
151 return g_topology.heterogeneous;
152}

References g_topology, g_topology_once, and topology_init().

Referenced by ck_threadpool_maybe_pin_big_cores().

◆ topology_discover()

void topology_discover ( void )
static

Definition at line 79 of file ck_topology.c.

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}
#define CK_TOPOLOGY_HETEROGENEOUS_RATIO_X100
Definition ck_topology.c:37
#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

References CK_TOPOLOGY_HETEROGENEOUS_RATIO_X100, CK_TOPOLOGY_MAX_CPUS, CK_TOPOLOGY_TOP_TIER_PCT, g_topology, and ids.

Referenced by main(), and topology_init().

◆ topology_init()

void topology_init ( void )
static

Definition at line 137 of file ck_topology.c.

138{
140}
static void topology_discover(void)
Definition ck_topology.c:79

References topology_discover().

Referenced by ck_topology_allowed_cpus(), ck_topology_big_cpus(), and ck_topology_heterogeneous().

Variable Documentation

◆ g_topology

ck_topology_state_t g_topology
static

◆ g_topology_once

pthread_once_t g_topology_once = PTHREAD_ONCE_INIT
static