Skip to main content

cbtop/grammar/
strategy.rs

1//! Execution strategy (Geometry equivalent in Grammar of Graphics).
2
3use super::resources::ResourceMapping;
4use super::workload::WorkloadSpec;
5
6/// SIMD width specification
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum SimdWidth {
9    /// Auto-detect best available
10    Auto,
11    /// SSE2 (128-bit)
12    Sse2,
13    /// AVX2 (256-bit)
14    Avx2,
15    /// AVX-512 (512-bit)
16    Avx512,
17    /// ARM NEON (128-bit)
18    Neon,
19    /// WASM SIMD128
20    Wasm,
21}
22
23/// GPU device specification
24#[derive(Debug, Clone, PartialEq)]
25pub enum GpuDevice {
26    /// Auto-select best available
27    Auto,
28    /// Specific device by ID
29    Id(u32),
30    /// CUDA device
31    Cuda(u32),
32    /// wgpu device
33    Wgpu(u32),
34}
35
36/// Kernel specification for GPU
37#[derive(Debug, Clone, PartialEq)]
38pub struct KernelSpec {
39    /// Kernel name
40    pub name: String,
41    /// Block size (threads per block)
42    pub block_size: (u32, u32, u32),
43    /// Grid size (number of blocks)
44    pub grid_size: Option<(u32, u32, u32)>,
45    /// Shared memory per block
46    pub shared_mem: usize,
47}
48
49/// Execution strategy (analogous to Geometry)
50#[derive(Debug, Clone, PartialEq)]
51pub enum ExecutionStrategy {
52    /// Sequential execution (baseline)
53    Sequential,
54    /// SIMD vectorization
55    Simd { width: SimdWidth },
56    /// Multi-threaded parallel
57    Parallel { threads: usize, chunk_size: usize },
58    /// GPU acceleration
59    Gpu {
60        device: GpuDevice,
61        kernel: Option<KernelSpec>,
62    },
63    /// Distributed across nodes
64    Distributed { nodes: Vec<String> },
65    /// Hybrid CPU+GPU
66    Hybrid { cpu_fraction: f64 },
67}
68
69impl ExecutionStrategy {
70    /// Create SIMD strategy with auto width
71    pub fn simd_auto() -> Self {
72        ExecutionStrategy::Simd {
73            width: SimdWidth::Auto,
74        }
75    }
76
77    /// Create SIMD strategy with specific width
78    pub fn simd(width: SimdWidth) -> Self {
79        ExecutionStrategy::Simd { width }
80    }
81
82    /// Create parallel strategy
83    pub fn parallel(threads: usize) -> Self {
84        ExecutionStrategy::Parallel {
85            threads,
86            chunk_size: 1024,
87        }
88    }
89
90    /// Create GPU strategy with auto device
91    pub fn gpu_auto() -> Self {
92        ExecutionStrategy::Gpu {
93            device: GpuDevice::Auto,
94            kernel: None,
95        }
96    }
97
98    /// Create GPU strategy with specific device
99    pub fn gpu(device: GpuDevice) -> Self {
100        ExecutionStrategy::Gpu {
101            device,
102            kernel: None,
103        }
104    }
105}
106
107/// Strategy layer (analogous to ggplot2 Layer)
108#[derive(Debug, Clone, PartialEq)]
109pub struct StrategyLayer {
110    /// Execution strategy
111    pub strategy: ExecutionStrategy,
112    /// Layer-specific workload override
113    pub workload: Option<WorkloadSpec>,
114    /// Layer-specific resource mapping
115    pub resources: ResourceMapping,
116    /// Layer priority (higher = try first)
117    pub priority: i32,
118}
119
120impl StrategyLayer {
121    /// Create new strategy layer
122    pub fn new(strategy: ExecutionStrategy) -> Self {
123        Self {
124            strategy,
125            workload: None,
126            resources: ResourceMapping::default(),
127            priority: 0,
128        }
129    }
130
131    /// Set layer priority
132    pub fn priority(mut self, priority: i32) -> Self {
133        self.priority = priority;
134        self
135    }
136}