Skip to main content

ocelotl_kernels/
lib.rs

1//! Portable kernel dispatch boundary.
2
3use ocelotl_core::{Device, OcelotlError, Result};
4
5#[derive(Debug, Clone)]
6pub struct KernelContext {
7    pub device: Device,
8}
9
10pub trait KernelBackend: Send + Sync {
11    fn name(&self) -> &'static str;
12    fn context(&self) -> &KernelContext;
13}
14
15#[derive(Debug, Clone)]
16pub struct CpuKernelBackend {
17    context: KernelContext,
18}
19
20impl Default for CpuKernelBackend {
21    fn default() -> Self {
22        Self {
23            context: KernelContext {
24                device: Device::Cpu,
25            },
26        }
27    }
28}
29
30impl KernelBackend for CpuKernelBackend {
31    fn name(&self) -> &'static str {
32        "cpu"
33    }
34
35    fn context(&self) -> &KernelContext {
36        &self.context
37    }
38}
39
40pub fn require_gpu(backend: &dyn KernelBackend) -> Result<()> {
41    match backend.context().device {
42        Device::Gpu { .. } => Ok(()),
43        Device::Cpu => Err(OcelotlError::Unsupported(
44            "GPU kernels are not configured in this build".to_string(),
45        )),
46    }
47}