Skip to main content

qualia_core_db/query/graph_accel/
path.rs

1//! Which accelerator actually ran. NPU is reserved: today's `npu_ffi` sieve is wgpu,
2//! not a distinct integer backend. The live chain is GPU → CPU.
3
4use std::fmt;
5
6/// Best-path that produced a result. Callers never fail because GPU is missing.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum AccelPath {
9    Gpu,
10    /// Reserved. `npu_available()` is false until a real NPU integer path exists.
11    Npu,
12    Cpu,
13}
14
15impl AccelPath {
16    /// Stable string form.
17    pub fn as_str(self) -> &'static str {
18        match self {
19            Self::Gpu => "gpu",
20            Self::Npu => "npu",
21            Self::Cpu => "cpu",
22        }
23    }
24}
25
26impl fmt::Display for AccelPath {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        f.write_str(self.as_str())
29    }
30}
31
32/// How the dispatcher should choose. `QUALIA_GRAPH_ACCEL=cpu|gpu|auto` overrides.
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum AccelPolicy {
35    Auto,
36    GpuPreferred,
37    CpuOnly,
38}
39
40impl AccelPolicy {
41    /// Builds `AccelPolicy` from `env`.
42    pub fn from_env() -> Self {
43        match std::env::var("QUALIA_GRAPH_ACCEL")
44            .ok()
45            .as_deref()
46            .map(str::trim)
47            .map(str::to_ascii_lowercase)
48            .as_deref()
49        {
50            Some("cpu") => Self::CpuOnly,
51            Some("gpu") => Self::GpuPreferred,
52            _ => Self::Auto,
53        }
54    }
55}
56
57/// Distinct NPU integer backend (DirectML/Hexagon/CoreML as *compute*, not wgpu).
58/// Today's `platform::npu_ffi` dispatches `sieve.wgsl` on wgpu — that is [`AccelPath::Gpu`].
59#[inline]
60pub fn npu_available() -> bool {
61    false
62}
63
64/// Prefer GPU when a shared device exists and policy allows it.
65///
66/// Without `gpu-runtime` this is the CPU floor: [`crate::gpu_context::try_shared_gpu`]
67/// returns `None`. On wasm32 the shared WebGPU device is installed by
68/// `crate::gpu_context::ensure_shared_gpu()`; until that completes this is also
69/// the CPU floor. Never panics.
70pub fn gpu_available() -> bool {
71    crate::gpu_context::try_shared_gpu().is_some()
72}
73
74/// Below this many records, PCIe + dispatch lose to the CPU radix floor.
75pub const GPU_SORT_MIN: usize = 65_536;
76pub const GPU_SIEVE_MIN: usize = 4_096;
77pub const GPU_JOIN_MIN: usize = 16_384;
78
79#[cfg(test)]
80mod tests {
81    #[test]
82    fn gpu_available_never_panics() {
83        let _ = super::gpu_available();
84    }
85
86    #[cfg(not(all(not(target_arch = "wasm32"), feature = "gpu-runtime")))]
87    #[test]
88    fn gpu_available_is_cpu_floor_without_gpu_runtime() {
89        assert!(!super::gpu_available());
90    }
91}