use cubecl::prelude::*;
use cubecl::server::Handle;
use super::buffer;
#[derive(Debug, Clone, Default)]
pub struct Scratch {
handle: Option<Handle>,
capacity_bytes: usize,
}
impl Scratch {
pub fn new() -> Self {
Self::default()
}
pub fn get<E: CubeElement>(&mut self, client: &Client, len: usize) -> Handle {
let need = buffer::bytes::<E>(len);
match &self.handle {
Some(handle) if self.capacity_bytes >= need => handle.clone(),
_ => {
let handle = client.empty(need);
self.handle = Some(handle.clone());
self.capacity_bytes = need;
handle
}
}
}
}