Skip to main content

xtap_core_lib/
hardware.rs

1/*
2 * Copyright (c) 2026 星TAP实验室
3 * Skill: 硬件资源管理大师 (Hardware Resource Master)
4 */
5
6use crate::error::Result;
7use rayon::prelude::*;
8
9/// 动态硬件识别与优化建议
10pub struct HardwareMaster;
11
12impl HardwareMaster {
13    /// 获取 CPU 物理核心数
14    pub fn logical_cpus() -> usize {
15        num_cpus::get()
16    }
17
18    /// 获取物理核心数 (非超线程)
19    pub fn physical_cpus() -> usize {
20        num_cpus::get_physical()
21    }
22
23    /// 自动配置全局并行线程池 (基于 Rayon)
24    /// 这能确保在高并发处理时不会打死系统
25    pub fn init_global_pool(threads: Option<usize>) -> Result<()> {
26        let t = threads.unwrap_or_else(Self::logical_cpus);
27        rayon::ThreadPoolBuilder::new()
28            .num_threads(t)
29            .build_global()?;
30        Ok(())
31    }
32
33    /// [Master Skill] 并行加速处理模版
34    /// 示例:快速处理海量数据,自动分配 CPU 线程
35    pub fn parallel_process<T, R, F>(items: Vec<T>, f: F) -> Vec<R>
36    where
37        T: Send,
38        R: Send,
39        F: Fn(T) -> R + Sync + Send,
40    {
41        items.into_par_iter().map(f).collect()
42    }
43}
44
45/// GPU 加速感知 (如果开启了 gpu feature)
46#[cfg(feature = "gpu")]
47pub mod gpu {
48    use wgpu;
49
50    pub async fn check_gpu_status() {
51        let instance = wgpu::Instance::default();
52        let adapters = instance.enumerate_adapters(wgpu::Backends::all());
53        for adapter in adapters {
54            println!("检测到 GPU: {:?}", adapter.get_info().name);
55        }
56    }
57}