Skip to main content

tpt_gpu_primitives/
kernel.rs

1//! Kernel trait and dispatch interface
2use crate::error::TptpResult;
3use crate::memory::{DType, GpuBuffer};
4use crate::vendor::VendorBackend;
5use std::fmt;
6
7/// Kernel configuration for launch parameters
8#[derive(Clone)]
9pub struct KernelConfig {
10    pub grid_size: [u32; 3],
11    pub block_size: [u32; 3],
12    pub shared_mem_bytes: u32,
13    pub synchronous: bool,
14}
15
16impl KernelConfig {
17    pub fn new(grid_size: [u32; 3], block_size: [u32; 3]) -> Self {
18        KernelConfig {
19            grid_size,
20            block_size,
21            shared_mem_bytes: 0,
22            synchronous: false,
23        }
24    }
25    pub fn with_shared_mem(mut self, bytes: u32) -> Self {
26        self.shared_mem_bytes = bytes;
27        self
28    }
29    pub fn with_synchronous(mut self, sync: bool) -> Self {
30        self.synchronous = sync;
31        self
32    }
33    pub fn num_blocks(&self) -> u64 {
34        self.grid_size[0] as u64 * self.grid_size[1] as u64 * self.grid_size[2] as u64
35    }
36    pub fn threads_per_block(&self) -> u64 {
37        self.block_size[0] as u64 * self.block_size[1] as u64 * self.block_size[2] as u64
38    }
39    pub fn num_threads(&self) -> u64 {
40        self.num_blocks() * self.threads_per_block()
41    }
42}
43
44impl Default for KernelConfig {
45    fn default() -> Self {
46        KernelConfig {
47            grid_size: [1, 1, 1],
48            block_size: [256, 1, 1],
49            shared_mem_bytes: 0,
50            synchronous: false,
51        }
52    }
53}
54
55impl fmt::Debug for KernelConfig {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        f.debug_struct("KernelConfig")
58            .field("grid_size", &self.grid_size)
59            .field("block_size", &self.block_size)
60            .field("shared_mem_bytes", &self.shared_mem_bytes)
61            .field("synchronous", &self.synchronous)
62            .finish()
63    }
64}
65
66/// Kernel dispatch strategy
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
68pub enum KernelDispatch {
69    VendorLibrary,
70    TptirKernel,
71    Auto,
72}
73
74/// Result of a kernel execution
75pub struct KernelResult {
76    pub outputs: Vec<GpuBuffer<f32>>,
77    pub execution_time_ms: Option<f64>,
78    pub backend_used: String,
79}
80
81impl Clone for KernelResult {
82    fn clone(&self) -> Self {
83        KernelResult {
84            outputs: Vec::new(), // GpuBuffer can't be cloned meaningfully
85            execution_time_ms: self.execution_time_ms,
86            backend_used: self.backend_used.clone(),
87        }
88    }
89}
90
91/// Common trait for all primitive kernels
92pub trait PrimitiveKernel: Send + Sync {
93    fn name(&self) -> &str;
94    fn supported_dtypes(&self) -> &[DType];
95    fn can_execute(&self, inputs: &[&GpuBuffer<f32>]) -> bool;
96    fn default_config(&self) -> KernelConfig;
97    fn execute(
98        &self,
99        inputs: &[&GpuBuffer<f32>],
100        output: &mut GpuBuffer<f32>,
101        config: &KernelConfig,
102    ) -> TptpResult<KernelResult>;
103    fn execute_with_vendor(
104        &self,
105        inputs: &[&GpuBuffer<f32>],
106        output: &mut GpuBuffer<f32>,
107        vendor: &VendorBackend,
108        config: &KernelConfig,
109    ) -> TptpResult<KernelResult>;
110}
111
112/// Kernel builder for constructing kernel launches
113pub struct KernelBuilder {
114    config: KernelConfig,
115    dispatch: KernelDispatch,
116}
117
118impl KernelBuilder {
119    pub fn new() -> Self {
120        KernelBuilder {
121            config: KernelConfig::default(),
122            dispatch: KernelDispatch::Auto,
123        }
124    }
125    pub fn grid_size(mut self, x: u32, y: u32, z: u32) -> Self {
126        self.config.grid_size = [x, y, z];
127        self
128    }
129    pub fn block_size(mut self, x: u32, y: u32, z: u32) -> Self {
130        self.config.block_size = [x, y, z];
131        self
132    }
133    pub fn shared_mem(mut self, bytes: u32) -> Self {
134        self.config.shared_mem_bytes = bytes;
135        self
136    }
137    pub fn dispatch(mut self, dispatch: KernelDispatch) -> Self {
138        self.dispatch = dispatch;
139        self
140    }
141    pub fn synchronous(mut self, sync: bool) -> Self {
142        self.config.synchronous = sync;
143        self
144    }
145    pub fn config(&self) -> &KernelConfig {
146        &self.config
147    }
148    pub fn dispatch_strategy(&self) -> KernelDispatch {
149        self.dispatch
150    }
151}
152
153impl Default for KernelBuilder {
154    fn default() -> Self {
155        Self::new()
156    }
157}
158
159#[cfg(test)]
160mod tests {
161    use super::*;
162    #[test]
163    fn test_kernel_config() {
164        let config = KernelConfig::new([16, 1, 1], [256, 1, 1]);
165        assert_eq!(config.num_blocks(), 16);
166        assert_eq!(config.threads_per_block(), 256);
167        assert_eq!(config.num_threads(), 4096);
168    }
169    #[test]
170    fn test_kernel_builder() {
171        let builder = KernelBuilder::new()
172            .grid_size(32, 1, 1)
173            .block_size(128, 1, 1)
174            .shared_mem(4096);
175        assert_eq!(builder.config().grid_size, [32, 1, 1]);
176        assert_eq!(builder.config().block_size, [128, 1, 1]);
177        assert_eq!(builder.config().shared_mem_bytes, 4096);
178    }
179}