← Back to C-Kernel-Engine Docs Doxygen Source Documentation
system_topology.h
Go to the documentation of this file.
1 /*
2  * system_topology.h - System hardware topology discovery for distributed training
3  *
4  * Probes CPU, memory, NUMA, PCIe, and network configuration to provide
5  * recommendations for optimal distributed training setup.
6  */
7 
8 #ifndef SYSTEM_TOPOLOGY_H
9 #define SYSTEM_TOPOLOGY_H
10 
11 #include <stdint.h>
12 #include <stdbool.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 // ═══════════════════════════════════════════════════════════════════════════════
19 // Constants
20 // ═══════════════════════════════════════════════════════════════════════════════
21 
22 #define MAX_NUMA_NODES 8
23 #define MAX_CPUS 256
24 #define MAX_CACHE_LEVELS 4
25 #define MAX_NICS 8
26 #define MAX_PCIE_DEVICES 32
27 #define MAX_MEMORY_SLOTS 16
28 #define MAX_STR_LEN 256
29 
30 // ═══════════════════════════════════════════════════════════════════════════════
31 // CPU Information
32 // ═══════════════════════════════════════════════════════════════════════════════
33 
34 typedef struct {
35  char model_name[MAX_STR_LEN];
36  char vendor[64];
37  int family;
38  int model;
39  int stepping;
40 
43  int sockets;
46 
48  float max_freq_mhz;
49 
50  // SIMD capabilities
51  bool has_sse4_2;
52  bool has_avx;
53  bool has_avx2;
57  bool has_avx512_bf16; // AVX-512 BF16 instructions
58  bool has_amx; // Intel AMX (any variant)
59  bool has_amx_tile; // AMX tile operations
60  bool has_amx_int8; // AMX INT8 matrix multiply
61  bool has_amx_bf16; // AMX BF16 matrix multiply
62  bool has_vnni; // Vector Neural Network Instructions
63 
64  // PCIe lanes (from CPU)
67 } CPUInfo;
68 
69 // ═══════════════════════════════════════════════════════════════════════════════
70 // Cache Information
71 // ═══════════════════════════════════════════════════════════════════════════════
72 
73 typedef struct {
74  int level; // L1, L2, L3
75  char type[16]; // "Data", "Instruction", "Unified"
76  int size_kb;
79  int shared_by_cores; // How many cores share this cache
80 } CacheInfo;
81 
82 typedef struct {
85  int l3_total_kb; // Total L3 across all sockets
87 
88 // ═══════════════════════════════════════════════════════════════════════════════
89 // NUMA Information
90 // ═══════════════════════════════════════════════════════════════════════════════
91 
92 typedef struct {
93  int node_id;
94  uint64_t memory_total_mb;
95  uint64_t memory_free_mb;
96  int cpu_list[MAX_CPUS];
97  int num_cpus;
98 } NUMANode;
99 
100 typedef struct {
103  // Inter-node distances (for NUMA-aware allocation)
105 } NUMATopology;
106 
107 // ═══════════════════════════════════════════════════════════════════════════════
108 // Memory Information
109 // ═══════════════════════════════════════════════════════════════════════════════
110 
111 typedef struct {
113  char locator[64]; // "DIMM_A1", "P1-DIMMA1", etc.
114  bool populated;
115  uint64_t size_mb;
117  char type[32]; // "DDR4", "DDR5"
118  int rank;
120 } MemorySlot;
121 
122 typedef struct {
123  uint64_t total_mb;
124  uint64_t available_mb;
125  uint64_t cached_mb;
126 
127  // DIMM configuration (requires dmidecode/root)
131 
132  // Channel configuration
135  char channel_config[64]; // "Dual-channel", "Quad-channel", etc.
136 
137  // Theoretical bandwidth
139  float measured_bandwidth_gbs; // From actual benchmark
140  int estimated_channels; // Inferred from measured bandwidth
141 
142  // Benchmark details (for transparency)
143  int bw_test_numa_node; // Which NUMA node was tested
144  int bw_test_num_threads; // How many threads used
145 
146  // Memory type detected
147  char memory_type[32];
149 } MemoryInfo;
150 
151 // ═══════════════════════════════════════════════════════════════════════════════
152 // PCIe Information
153 // ═══════════════════════════════════════════════════════════════════════════════
154 
155 typedef struct {
156  char slot_id[32];
157  char device_name[MAX_STR_LEN];
158  char vendor[64];
159  int domain;
160  int bus;
161  int device;
162  int function;
163 
164  int link_width; // x1, x4, x8, x16
165  int link_width_max; // Maximum supported
166  int link_speed; // PCIe generation (3, 4, 5, 6)
168 
169  float bandwidth_gbs; // Current bandwidth
170  float bandwidth_max_gbs; // Maximum possible
171 
172  bool is_gpu;
173  bool is_nic;
174  bool is_nvme;
175 } PCIeDevice;
176 
177 typedef struct {
180 
181  // Summary
187 } PCIeTopology;
188 
189 // ═══════════════════════════════════════════════════════════════════════════════
190 // Network Information
191 // ═══════════════════════════════════════════════════════════════════════════════
192 
193 typedef struct {
194  char name[32]; // eth0, enp3s0, ib0
195  char driver[64];
196  char pci_address[32];
197 
198  uint64_t speed_mbps; // Link speed in Mbps
199  bool is_up;
200  bool has_link;
201 
202  // Capabilities
203  bool supports_rdma; // RoCE or InfiniBand
206  int ib_port;
207 
208  char mac_address[24];
209  int mtu;
210 
211  // PCIe info for this NIC
213  int pcie_gen;
216 
217 typedef struct {
220 
221  // Best interface for distributed training
224  bool has_rdma;
226 
227 // ═══════════════════════════════════════════════════════════════════════════════
228 // OpenMP / Affinity Information
229 // ═══════════════════════════════════════════════════════════════════════════════
230 
231 typedef struct {
233  char omp_proc_bind[32]; // "close", "spread", "master", etc.
234  char omp_places[64]; // "cores", "threads", "sockets"
236 
237  // Current process affinity
238  int affinity_cpus[MAX_CPUS];
240 } AffinityInfo;
241 
242 // ═══════════════════════════════════════════════════════════════════════════════
243 // Complete System Topology
244 // ═══════════════════════════════════════════════════════════════════════════════
245 
246 typedef struct {
254 
255  // Flags
257  char hostname[256];
258  char kernel_version[128];
260 
261 // ═══════════════════════════════════════════════════════════════════════════════
262 // Recommendations
263 // ═══════════════════════════════════════════════════════════════════════════════
264 
265 typedef enum {
271 
272 typedef enum {
279 
280 #define MAX_RECOMMENDATIONS 32
281 
282 typedef struct {
285  char title[128];
286  char description[512];
287  char action[256];
289 
290 typedef struct {
294 
295 // ═══════════════════════════════════════════════════════════════════════════════
296 // API Functions
297 // ═══════════════════════════════════════════════════════════════════════════════
298 
299 // Main discovery function
301 
302 // Individual discovery functions
310 
311 // Generate recommendations
313  RecommendationList *recs);
314 
315 // Display functions
316 void topology_print_summary(const SystemTopology *topo);
317 void topology_print_cpu(const CPUInfo *cpu);
318 void topology_print_cache(const CacheTopology *cache, int logical_cores);
319 void topology_print_numa(const NUMATopology *numa, int sockets);
320 void topology_print_memory(const MemoryInfo *mem);
321 void topology_print_pcie(const PCIeTopology *pcie);
322 void topology_print_network(const NetworkTopology *net);
323 void topology_print_affinity(const AffinityInfo *aff);
325 
326 // Distributed training info
328 
329 // Utility
332  uint64_t model_size_mb);
333 
334 #ifdef __cplusplus
335 }
336 #endif
337 
338 #endif // SYSTEM_TOPOLOGY_H
int logical_cores
bool has_avx512bw
int pcie_generation
bool has_vnni
bool has_avx512_bf16
float max_freq_mhz
int cores_per_socket
bool has_amx_int8
int physical_cores
bool has_amx_bf16
int pcie_lanes_total
bool has_avx512f
int threads_per_core
bool has_avx
bool has_amx
bool has_avx512vl
float base_freq_mhz
bool has_sse4_2
bool has_amx_tile
bool has_avx2
int line_size_bytes
int ways_of_associativity
int shared_by_cores
float measured_bandwidth_gbs
int channels_populated
uint64_t total_mb
float theoretical_bandwidth_gbs
int estimated_channels
uint64_t available_mb
int bw_test_num_threads
uint64_t cached_mb
uint64_t size_mb
uint64_t memory_free_mb
uint64_t memory_total_mb
float bandwidth_max_gbs
float bandwidth_gbs
RecommendationPriority priority
RecommendationCategory category
CacheTopology cache
NUMATopology numa
PCIeTopology pcie
NetworkTopology network
MemoryInfo memory
AffinityInfo affinity
void topology_print_memory(const MemoryInfo *mem)
Definition: show_config.c:292
int topology_discover_memory(MemoryInfo *mem)
int topology_discover_pcie(PCIeTopology *pcie)
int topology_discover(SystemTopology *topo)
#define MAX_RECOMMENDATIONS
int topology_discover_cpu(CPUInfo *cpu)
void topology_print_network(const NetworkTopology *net)
Definition: show_config.c:458
void topology_print_pcie(const PCIeTopology *pcie)
Definition: show_config.c:395
void topology_print_distributed_potential(const SystemTopology *topo)
Definition: show_config.c:584
void topology_print_numa(const NUMATopology *numa, int sockets)
Definition: show_config.c:213
#define MAX_STR_LEN
#define MAX_CACHE_LEVELS
#define MAX_NUMA_NODES
void topology_print_cpu(const CPUInfo *cpu)
Definition: show_config.c:103
#define MAX_MEMORY_SLOTS
int topology_discover_cache(CacheTopology *cache)
#define MAX_CPUS
int topology_discover_network(NetworkTopology *net)
RecommendationPriority
@ REC_PRIORITY_MEDIUM
@ REC_PRIORITY_CRITICAL
@ REC_PRIORITY_HIGH
@ REC_PRIORITY_LOW
void topology_print_affinity(const AffinityInfo *aff)
Definition: show_config.c:528
float topology_estimate_network_training_time(const NetworkTopology *net, uint64_t model_size_mb)
#define MAX_NICS
RecommendationCategory
@ REC_CATEGORY_AFFINITY
@ REC_CATEGORY_CPU
@ REC_CATEGORY_MEMORY
@ REC_CATEGORY_NETWORK
@ REC_CATEGORY_PCIE
int topology_generate_recommendations(const SystemTopology *topo, RecommendationList *recs)
void topology_print_recommendations(const RecommendationList *recs)
Definition: show_config.c:545
void topology_print_summary(const SystemTopology *topo)
Definition: show_config.c:649
float topology_estimate_memory_bandwidth(const MemoryInfo *mem)
#define MAX_PCIE_DEVICES
int topology_discover_affinity(AffinityInfo *aff)
void topology_print_cache(const CacheTopology *cache, int logical_cores)
Definition: show_config.c:169
int topology_discover_numa(NUMATopology *numa)