#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum DeviceType {
Cpu,
Cuda,
Rocm,
CoreMl,
Mlx,
WebGpu,
Qnn,
OpenVino,
Custom(u32),
}
impl DeviceType {
pub fn is_host_accessible(self) -> bool {
matches!(self, DeviceType::Cpu | DeviceType::Mlx)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct DeviceId {
pub device_type: DeviceType,
pub index: u32,
}
impl DeviceId {
pub fn new(device_type: DeviceType, index: u32) -> Self {
Self { device_type, index }
}
pub fn cpu() -> Self {
Self::new(DeviceType::Cpu, 0)
}
pub fn cuda(index: u32) -> Self {
Self::new(DeviceType::Cuda, index)
}
pub fn is_host_accessible(self) -> bool {
self.device_type.is_host_accessible()
}
}
impl Default for DeviceId {
fn default() -> Self {
Self::cpu()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_is_cpu0() {
assert_eq!(DeviceId::default(), DeviceId::cpu());
assert_eq!(DeviceId::default().index, 0);
}
#[test]
fn host_accessibility() {
assert!(DeviceId::cpu().is_host_accessible());
assert!(DeviceId::new(DeviceType::Mlx, 0).is_host_accessible());
assert!(!DeviceId::cuda(0).is_host_accessible());
}
}