Skip to main content

spatialrust_core/
device.rs

1/// Device kind supported by SpatialRust execution.
2#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
3#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
4pub enum DeviceKind {
5    /// Host CPU execution.
6    #[default]
7    Cpu,
8    /// Portable GPU execution via wgpu/WebGPU.
9    Wgpu,
10    /// NVIDIA CUDA execution.
11    Cuda,
12}
13
14impl DeviceKind {
15    /// Returns whether this device kind represents host CPU execution.
16    #[must_use]
17    pub const fn is_cpu(self) -> bool {
18        matches!(self, Self::Cpu)
19    }
20
21    /// Returns whether this device kind represents an accelerator device.
22    #[must_use]
23    pub const fn is_gpu(self) -> bool {
24        matches!(self, Self::Wgpu | Self::Cuda)
25    }
26}
27
28/// Minimal device abstraction defined in core and extended by `spatialrust-gpu`.
29pub trait Device: core::fmt::Debug + Send + Sync + 'static {
30    /// Returns the kind of this device.
31    fn kind(&self) -> DeviceKind;
32}
33
34/// Default CPU device.
35#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
36#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
37pub struct CpuDevice;
38
39impl Device for CpuDevice {
40    fn kind(&self) -> DeviceKind {
41        DeviceKind::Cpu
42    }
43}