numrs/backend/
mod.rs

1//! Backend selection and execution helpers
2//!
3//! ## Modern API
4//! - `dispatch::get_dispatch_table()` - Zero-cost function pointer dispatch
5//! - `dispatch::validate_backends()` - Runtime backend validation
6//! - `ops::fast::*` - Fast-path operations using dispatch system
7
8pub mod cpu;
9// WebGPU module (handles both native and WASM internally)
10#[cfg(all(feature = "blas-backend", not(feature = "disabled-blas")))]
11pub mod blas;
12pub mod capabilities;
13pub mod cuda;
14pub mod dispatch;
15pub mod heuristics;
16pub mod metal;
17pub mod microbench;
18pub mod webgpu; // Sistema de microbenchmarking para kernel selection
19
20/// Selected backend abstraction
21#[derive(Debug, Clone)]
22pub enum SelectedBackend {
23    CPU(cpu::CpuBackend),
24    WebGPU(webgpu::WebGpuBackend),
25    CUDA(cuda::CudaBackend),
26    Metal(metal::MetalBackend),
27    #[cfg(all(feature = "blas-backend", not(feature = "disabled-blas")))]
28    Blas(blas::BlasBackend),
29}
30
31impl SelectedBackend {
32    /// Detect a backend automatically. On this prototype we use an env var
33    /// `NUMRS_BACKEND` (webgpu, cuda, metal, cpu, blas) or choose best-effort.
34    pub fn auto_detect() -> Self {
35        use std::env;
36        match env::var("NUMRS_BACKEND").ok().as_deref() {
37            Some("webgpu") => SelectedBackend::WebGPU(webgpu::WebGpuBackend::new()),
38            Some("cuda") => SelectedBackend::CUDA(cuda::CudaBackend::new()),
39            Some("metal") => SelectedBackend::Metal(metal::MetalBackend::new()),
40            #[cfg(all(feature = "blas-backend", not(feature = "disabled-blas")))]
41            Some("blas") => SelectedBackend::Blas(blas::BlasBackend::new()),
42            Some("auto") | None => {
43                // Use dispatch system to determine best backend
44                let validation = dispatch::validate_backends();
45                #[cfg(all(feature = "blas-backend", not(feature = "disabled-blas")))]
46                if validation.blas_validated {
47                    return SelectedBackend::Blas(blas::BlasBackend::new());
48                }
49                if validation.webgpu_validated {
50                    return SelectedBackend::WebGPU(webgpu::WebGpuBackend::new());
51                }
52                SelectedBackend::CPU(cpu::CpuBackend::new())
53            }
54            _ => {
55                // default fallback to CPU
56                SelectedBackend::CPU(cpu::CpuBackend::new())
57            }
58        }
59    }
60
61    // execute_llo() removed - use ops::fast::* functions with dispatch system instead
62}
63
64// ============================================================================
65// Public API: Modern Dispatch System
66// ============================================================================
67
68pub use dispatch::{
69    get_dispatch_table, init_dispatch_table, validate_backends, BackendValidation, DispatchTable,
70    RuntimeCapabilities,
71};
72
73pub use capabilities::{
74    BackendCapabilities, BLAS_CAPABILITIES, CUDA_CAPABILITIES, METAL_CAPABILITIES,
75    SCALAR_CAPABILITIES, SIMD_CAPABILITIES, WEBGPU_CAPABILITIES,
76};