cuda_rust_wasm/backend/
backend_trait.rs

1//! Common backend interface
2
3use crate::Result;
4use async_trait::async_trait;
5
6/// Backend capabilities
7#[derive(Debug, Clone)]
8pub struct BackendCapabilities {
9    pub name: String,
10    pub supports_cuda: bool,
11    pub supports_opencl: bool,
12    pub supports_vulkan: bool,
13    pub supports_webgpu: bool,
14    pub max_threads: u32,
15    pub max_threads_per_block: u32,
16    pub max_blocks_per_grid: u32,
17    pub max_shared_memory: usize,
18    pub supports_dynamic_parallelism: bool,
19    pub supports_unified_memory: bool,
20    pub max_grid_dim: [u32; 3],
21    pub max_block_dim: [u32; 3],
22    pub warp_size: u32,
23}
24
25/// Common interface for all backends
26#[async_trait]
27pub trait BackendTrait: Send + Sync {
28    /// Get backend name
29    fn name(&self) -> &str;
30    
31    /// Get backend capabilities
32    fn capabilities(&self) -> &BackendCapabilities;
33    
34    /// Initialize the backend
35    async fn initialize(&mut self) -> Result<()>;
36    
37    /// Compile a kernel
38    async fn compile_kernel(&self, source: &str) -> Result<Vec<u8>>;
39    
40    /// Launch a kernel
41    async fn launch_kernel(
42        &self,
43        kernel: &[u8],
44        grid: (u32, u32, u32),
45        block: (u32, u32, u32),
46        args: &[*const u8],
47    ) -> Result<()>;
48    
49    /// Allocate device memory
50    fn allocate_memory(&self, size: usize) -> Result<*mut u8>;
51    
52    /// Free device memory
53    fn free_memory(&self, ptr: *mut u8) -> Result<()>;
54    
55    /// Copy memory
56    fn copy_memory(
57        &self,
58        dst: *mut u8,
59        src: *const u8,
60        size: usize,
61        kind: MemcpyKind,
62    ) -> Result<()>;
63    
64    /// Synchronize device
65    fn synchronize(&self) -> Result<()>;
66}
67
68/// Memory copy direction
69#[derive(Debug, Clone, Copy)]
70pub enum MemcpyKind {
71    HostToDevice,
72    DeviceToHost,
73    DeviceToDevice,
74    HostToHost,
75}