cubecl_runtime/channel/
cell.rs

1use super::ComputeChannel;
2use crate::server::{Binding, ComputeServer, CubeCount, Handle};
3use crate::storage::BindingResource;
4use crate::ExecutionMode;
5use alloc::sync::Arc;
6use alloc::vec::Vec;
7use cubecl_common::benchmark::TimestampsResult;
8
9/// A channel using a [ref cell](core::cell::RefCell) to access the server with mutability.
10///
11/// # Important
12///
13/// Only use this channel if you don't use any threading in your application, otherwise it will
14/// panic or cause undefined behaviors.
15///
16/// This is mosly useful for `no-std` environments where threads aren't supported, otherwise prefer
17/// the [mutex](super::MutexComputeChannel) or the [mpsc](super::MpscComputeChannel) channels.
18#[derive(Debug)]
19pub struct RefCellComputeChannel<Server> {
20    server: Arc<core::cell::RefCell<Server>>,
21}
22
23impl<S> Clone for RefCellComputeChannel<S> {
24    fn clone(&self) -> Self {
25        Self {
26            server: self.server.clone(),
27        }
28    }
29}
30
31impl<Server> RefCellComputeChannel<Server>
32where
33    Server: ComputeServer,
34{
35    /// Create a new cell compute channel.
36    pub fn new(server: Server) -> Self {
37        Self {
38            server: Arc::new(core::cell::RefCell::new(server)),
39        }
40    }
41}
42
43impl<Server> ComputeChannel<Server> for RefCellComputeChannel<Server>
44where
45    Server: ComputeServer + Send,
46{
47    async fn read(&self, bindings: Vec<Binding>) -> Vec<Vec<u8>> {
48        let future = {
49            let mut server = self.server.borrow_mut();
50            server.read(bindings)
51        };
52        future.await
53    }
54
55    fn get_resource(&self, binding: Binding) -> BindingResource<Server> {
56        self.server.borrow_mut().get_resource(binding)
57    }
58
59    fn create(&self, resource: &[u8]) -> Handle {
60        self.server.borrow_mut().create(resource)
61    }
62
63    fn empty(&self, size: usize) -> Handle {
64        self.server.borrow_mut().empty(size)
65    }
66
67    unsafe fn execute(
68        &self,
69        kernel_description: Server::Kernel,
70        count: CubeCount,
71        bindings: Vec<Binding>,
72        kind: ExecutionMode,
73    ) {
74        self.server
75            .borrow_mut()
76            .execute(kernel_description, count, bindings, kind)
77    }
78
79    fn flush(&self) {
80        self.server.borrow_mut().flush()
81    }
82
83    async fn sync(&self) {
84        let future = {
85            let mut server = self.server.borrow_mut();
86            server.sync()
87        };
88        future.await
89    }
90
91    async fn sync_elapsed(&self) -> TimestampsResult {
92        let future = {
93            let mut server = self.server.borrow_mut();
94            server.sync_elapsed()
95        };
96        future.await
97    }
98
99    fn memory_usage(&self) -> crate::memory_management::MemoryUsage {
100        self.server.borrow_mut().memory_usage()
101    }
102
103    fn enable_timestamps(&self) {
104        self.server.borrow_mut().enable_timestamps();
105    }
106
107    fn disable_timestamps(&self) {
108        self.server.borrow_mut().disable_timestamps();
109    }
110}
111
112/// This is unsafe, since no concurrency is supported by the `RefCell` channel.
113/// However using this channel should only be done in single threaded environments such as `no-std`.
114unsafe impl<Server: ComputeServer> Send for RefCellComputeChannel<Server> {}
115unsafe impl<Server: ComputeServer> Sync for RefCellComputeChannel<Server> {}