onnx_runtime_ir/
device.rs1#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
8pub enum DeviceType {
9 Cpu,
10 Cuda,
11 Rocm,
12 CoreMl,
13 Mlx,
14 WebGpu,
15 Qnn,
16 OpenVino,
17 Custom(u32),
19}
20
21impl DeviceType {
22 pub fn is_host_accessible(self) -> bool {
25 matches!(self, DeviceType::Cpu | DeviceType::Mlx)
27 }
28}
29
30#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
32pub struct DeviceId {
33 pub device_type: DeviceType,
34 pub index: u32,
35}
36
37impl DeviceId {
38 pub fn new(device_type: DeviceType, index: u32) -> Self {
40 Self { device_type, index }
41 }
42
43 pub fn cpu() -> Self {
45 Self::new(DeviceType::Cpu, 0)
46 }
47
48 pub fn cuda(index: u32) -> Self {
50 Self::new(DeviceType::Cuda, index)
51 }
52
53 pub fn is_host_accessible(self) -> bool {
55 self.device_type.is_host_accessible()
56 }
57}
58
59impl Default for DeviceId {
60 fn default() -> Self {
61 Self::cpu()
62 }
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn default_is_cpu0() {
71 assert_eq!(DeviceId::default(), DeviceId::cpu());
72 assert_eq!(DeviceId::default().index, 0);
73 }
74
75 #[test]
76 fn host_accessibility() {
77 assert!(DeviceId::cpu().is_host_accessible());
78 assert!(DeviceId::new(DeviceType::Mlx, 0).is_host_accessible());
79 assert!(!DeviceId::cuda(0).is_host_accessible());
80 }
81}