cubecl_common/device/
base.rs1use core::{
2 any::{Any, TypeId},
3 cmp::Ordering,
4};
5use cubecl_environment::sync::Arc;
6
7#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy, new)]
9pub struct DeviceId {
10 pub type_id: u16,
12 pub index_id: u16,
14}
15
16pub trait Device: Default + Clone + core::fmt::Debug + Send + Sync + 'static {
18 fn from_id(device_id: DeviceId) -> Self;
20 fn to_id(&self) -> DeviceId;
22}
23
24impl core::fmt::Display for DeviceId {
25 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26 f.write_fmt(format_args!(
27 "DeviceId(type={}, index={})",
28 self.type_id, self.index_id
29 ))
30 }
31}
32
33impl Ord for DeviceId {
34 fn cmp(&self, other: &Self) -> Ordering {
35 match self.type_id.cmp(&other.type_id) {
36 Ordering::Equal => self.index_id.cmp(&other.index_id),
37 other => other,
38 }
39 }
40}
41
42impl PartialOrd for DeviceId {
43 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
44 Some(self.cmp(other))
45 }
46}
47
48pub type ServerUtilitiesHandle = Arc<dyn Any + Send + Sync>;
50
51pub trait DeviceService: Send + 'static {
53 fn init(device_id: DeviceId) -> Self
55 where
56 Self: Sized;
57 fn utilities(&self) -> ServerUtilitiesHandle;
59 fn stage() -> DeviceServiceStage
65 where
66 Self: Sized,
67 {
68 DeviceServiceStage::Downstream
69 }
70}
71
72#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
75pub enum DeviceServiceStage {
76 Upstream = 0,
78 Downstream = 1,
80}
81
82#[derive(Debug, Hash, PartialEq, Eq, Clone, Copy)]
86pub struct ServiceId {
87 pub device: DeviceId,
89 pub service: TypeId,
91}
92
93impl ServiceId {
94 pub fn of<S: 'static>(device: DeviceId) -> Self {
96 Self {
97 device,
98 service: TypeId::of::<S>(),
99 }
100 }
101}
102
103impl core::fmt::Display for ServiceId {
104 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
105 f.write_fmt(format_args!("{} ({:?})", self.device, self.service))
106 }
107}