velesdb_core/gpu.rs
1//! GPU-accelerated vector operations using wgpu (WebGPU).
2//!
3//! This module provides optional GPU acceleration for batch distance calculations.
4//! Enable with feature flag `gpu`.
5//!
6//! # When to use GPU
7//!
8//! - **Batch operations** (100+ queries at once)
9//! - **Large datasets** (500K+ vectors)
10//! - **Index construction** (HNSW graph building)
11//!
12//! For single queries on datasets ≤100K, CPU SIMD remains faster.
13//!
14//! # Platform Support
15//!
16//! | Platform | Backend |
17//! |----------|---------|
18//! | Windows | DirectX 12 / Vulkan |
19//! | macOS | Metal |
20//! | Linux | Vulkan |
21//! | Browser | WebGPU |
22
23#[cfg(feature = "gpu")]
24#[path = "gpu/gpu_backend.rs"]
25mod gpu_backend;
26
27#[cfg(all(test, feature = "gpu"))]
28#[path = "gpu/gpu_backend_tests.rs"]
29mod gpu_backend_tests;
30
31#[cfg(feature = "gpu")]
32pub use gpu_backend::GpuAccelerator;
33
34/// Compute backend selection.
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
36pub enum ComputeBackend {
37 /// CPU SIMD (default, always available)
38 #[default]
39 Simd,
40 /// GPU via wgpu (requires `gpu` feature)
41 #[cfg(feature = "gpu")]
42 Gpu,
43}
44
45impl ComputeBackend {
46 /// Returns the best available backend.
47 ///
48 /// Prefers GPU if available, falls back to SIMD.
49 #[must_use]
50 pub fn best_available() -> Self {
51 #[cfg(feature = "gpu")]
52 {
53 if gpu_backend::GpuAccelerator::is_available() {
54 return Self::Gpu;
55 }
56 }
57 Self::Simd
58 }
59
60 /// Returns true if GPU backend is available.
61 #[must_use]
62 pub fn gpu_available() -> bool {
63 #[cfg(feature = "gpu")]
64 {
65 gpu_backend::GpuAccelerator::is_available()
66 }
67 #[cfg(not(feature = "gpu"))]
68 {
69 false
70 }
71 }
72}