use crate::{
memory_management::{MemoryHandle, MemoryManagement},
storage::ComputeStorage,
ExecutionMode,
};
use alloc::vec::Vec;
use core::fmt::Debug;
use cubecl_common::{reader::Reader, sync_type::SyncType};
pub trait ComputeServer: Send + core::fmt::Debug
where
Self: Sized,
{
type Kernel: Send;
type DispatchOptions: Send;
type Storage: ComputeStorage;
type MemoryManagement: MemoryManagement<Self::Storage>;
type FeatureSet: Send + Sync;
fn read(&mut self, binding: Binding<Self>) -> Reader;
fn get_resource(
&mut self,
binding: Binding<Self>,
) -> <Self::Storage as ComputeStorage>::Resource;
fn create(&mut self, data: &[u8]) -> Handle<Self>;
fn empty(&mut self, size: usize) -> Handle<Self>;
unsafe fn execute(
&mut self,
kernel: Self::Kernel,
count: Self::DispatchOptions,
bindings: Vec<Binding<Self>>,
kind: ExecutionMode,
);
fn sync(&mut self, command: SyncType);
}
#[derive(new, Debug)]
pub struct Handle<Server: ComputeServer> {
pub memory: <Server::MemoryManagement as MemoryManagement<Server::Storage>>::Handle,
}
#[derive(new, Debug)]
pub struct Binding<Server: ComputeServer> {
pub memory: <Server::MemoryManagement as MemoryManagement<Server::Storage>>::Binding,
}
impl<Server: ComputeServer> Handle<Server> {
pub fn can_mut(&self) -> bool {
MemoryHandle::can_mut(&self.memory)
}
}
impl<Server: ComputeServer> Handle<Server> {
pub fn binding(self) -> Binding<Server> {
Binding {
memory: MemoryHandle::binding(self.memory),
}
}
}
impl<Server: ComputeServer> Clone for Handle<Server> {
fn clone(&self) -> Self {
Self {
memory: self.memory.clone(),
}
}
}
impl<Server: ComputeServer> Clone for Binding<Server> {
fn clone(&self) -> Self {
Self {
memory: self.memory.clone(),
}
}
}