quantrs2_core/platform/
detector.rs1use super::capabilities::*;
4use std::env;
5
6pub fn detect_platform_capabilities() -> PlatformCapabilities {
8 PlatformCapabilities {
13 cpu: detect_cpu_capabilities(),
14 gpu: detect_gpu_capabilities(),
15 memory: detect_memory_capabilities(),
16 platform_type: detect_platform_type(),
17 os: detect_operating_system(),
18 architecture: detect_architecture(),
19 }
20}
21
22fn detect_cpu_capabilities() -> CpuCapabilities {
24 let logical_cores = num_cpus::get();
25 let physical_cores = num_cpus::get_physical();
26
27 CpuCapabilities {
28 physical_cores,
29 logical_cores,
30 simd: detect_simd_capabilities(),
31 cache: detect_cache_info(),
32 base_clock_mhz: None, vendor: detect_cpu_vendor(),
34 model_name: detect_cpu_model(),
35 }
36}
37
38fn detect_simd_capabilities() -> SimdCapabilities {
40 #[cfg(target_arch = "x86_64")]
44 {
45 SimdCapabilities {
46 sse: is_x86_feature_detected!("sse"),
47 sse2: is_x86_feature_detected!("sse2"),
48 sse3: is_x86_feature_detected!("sse3"),
49 ssse3: is_x86_feature_detected!("ssse3"),
50 sse4_1: is_x86_feature_detected!("sse4.1"),
51 sse4_2: is_x86_feature_detected!("sse4.2"),
52 avx: is_x86_feature_detected!("avx"),
53 avx2: is_x86_feature_detected!("avx2"),
54 avx512: cfg!(target_feature = "avx512f"),
55 fma: is_x86_feature_detected!("fma"),
56 neon: false,
57 sve: false,
58 }
59 }
60
61 #[cfg(target_arch = "aarch64")]
62 {
63 SimdCapabilities {
64 sse: false,
65 sse2: false,
66 sse3: false,
67 ssse3: false,
68 sse4_1: false,
69 sse4_2: false,
70 avx: false,
71 avx2: false,
72 avx512: false,
73 fma: false,
74 neon: cfg!(target_feature = "neon"),
75 sve: cfg!(target_feature = "sve"),
76 }
77 }
78
79 #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
80 {
81 SimdCapabilities {
82 sse: false,
83 sse2: false,
84 sse3: false,
85 ssse3: false,
86 sse4_1: false,
87 sse4_2: false,
88 avx: false,
89 avx2: false,
90 avx512: false,
91 fma: false,
92 neon: false,
93 sve: false,
94 }
95 }
96}
97
98fn detect_cache_info() -> CacheInfo {
100 CacheInfo {
102 l1_data: Some(32 * 1024), l1_instruction: Some(32 * 1024), l2: Some(256 * 1024), l3: Some(8 * 1024 * 1024), line_size: Some(64), }
108}
109
110fn detect_cpu_vendor() -> String {
112 #[cfg(target_arch = "x86_64")]
113 {
114 "Unknown".to_string()
116 }
117 #[cfg(target_arch = "aarch64")]
118 {
119 "ARM".to_string()
120 }
121 #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
122 {
123 "Unknown".to_string()
124 }
125}
126
127fn detect_cpu_model() -> String {
129 "Unknown".to_string()
131}
132
133fn detect_gpu_capabilities() -> GpuCapabilities {
135 let devices = Vec::new();
137
138 GpuCapabilities {
142 available: false,
143 devices,
144 primary_device: None,
145 }
146}
147
148fn detect_memory_capabilities() -> MemoryCapabilities {
150 use sysinfo::System;
151
152 let mut sys = System::new_all();
153 sys.refresh_memory();
154
155 MemoryCapabilities {
156 total_memory: sys.total_memory() as usize,
157 available_memory: sys.available_memory() as usize,
158 bandwidth_gbps: None, numa_nodes: 1, hugepage_support: detect_hugepage_support(),
161 }
162}
163
164fn detect_hugepage_support() -> bool {
166 #[cfg(target_os = "linux")]
167 {
168 std::path::Path::new("/sys/kernel/mm/hugepages").exists()
169 }
170 #[cfg(not(target_os = "linux"))]
171 {
172 false
173 }
174}
175
176fn detect_platform_type() -> PlatformType {
178 if env::var("KUBERNETES_SERVICE_HOST").is_ok() || env::var("ECS_CONTAINER_METADATA_URI").is_ok()
180 {
181 PlatformType::Cloud
182 } else if cfg!(target_os = "android") {
183 PlatformType::Mobile
184 } else {
185 PlatformType::Desktop
187 }
188}
189
190fn detect_operating_system() -> OperatingSystem {
192 #[cfg(target_os = "linux")]
193 {
194 OperatingSystem::Linux
195 }
196 #[cfg(target_os = "windows")]
197 {
198 OperatingSystem::Windows
199 }
200 #[cfg(target_os = "macos")]
201 {
202 OperatingSystem::MacOS
203 }
204 #[cfg(target_os = "freebsd")]
205 {
206 OperatingSystem::FreeBSD
207 }
208 #[cfg(target_os = "android")]
209 {
210 OperatingSystem::Android
211 }
212 #[cfg(not(any(
213 target_os = "linux",
214 target_os = "windows",
215 target_os = "macos",
216 target_os = "freebsd",
217 target_os = "android"
218 )))]
219 {
220 OperatingSystem::Unknown
221 }
222}
223
224fn detect_architecture() -> Architecture {
226 #[cfg(target_arch = "x86_64")]
227 {
228 Architecture::X86_64
229 }
230 #[cfg(target_arch = "aarch64")]
231 {
232 Architecture::Aarch64
233 }
234 #[cfg(target_arch = "riscv64")]
235 {
236 Architecture::Riscv64
237 }
238 #[cfg(target_arch = "wasm32")]
239 {
240 Architecture::Wasm32
241 }
242 #[cfg(not(any(
243 target_arch = "x86_64",
244 target_arch = "aarch64",
245 target_arch = "riscv64",
246 target_arch = "wasm32"
247 )))]
248 {
249 Architecture::Unknown
250 }
251}