Struct custos_math::custos::prelude::Cache
source · pub struct Cache<D>where
D: RawConv,{
pub nodes: HashMap<Ident, Rc<<D as CacheReturn>::CT>, RandomState>,
/* private fields */
}Fields§
§nodes: HashMap<Ident, Rc<<D as CacheReturn>::CT>, RandomState>Implementations§
source§impl<D> Cache<D>where
D: RawConv,
impl<D> Cache<D>where D: RawConv,
sourcepub fn add_node<'a, T, S>(
&mut self,
device: &'a D,
node: Ident,
_add_node: impl AddGraph
) -> Buffer<'a, T, D, S>where
S: Shape,
D: Alloc<'a, T, S> + RawConv,
pub fn add_node<'a, T, S>( &mut self, device: &'a D, node: Ident, _add_node: impl AddGraph ) -> Buffer<'a, T, D, S>where S: Shape, D: Alloc<'a, T, S> + RawConv,
Adds a new cache entry to the cache. The next get call will return this entry if the Ident is correct.
Example
use custos::prelude::*;
use custos::Ident;
let device = CPU::new();
let cache: Buffer = device
.cache()
.add_node(&device, Ident { idx: 0, len: 7 }, ());
let ptr = device
.cache()
.nodes
.get(&Ident { idx: 0, len: 7 })
.unwrap()
.clone();
assert_eq!(cache.host_ptr(), ptr.ptr as *mut f32);sourcepub fn get<'a, T, S>(
device: &'a D,
len: usize,
add_node: impl AddGraph
) -> Buffer<'a, T, D, S>where
S: Shape,
D: Alloc<'a, T, S> + RawConv,
pub fn get<'a, T, S>( device: &'a D, len: usize, add_node: impl AddGraph ) -> Buffer<'a, T, D, S>where S: Shape, D: Alloc<'a, T, S> + RawConv,
Retrieves cached pointers and constructs a Buffer with the pointers and the given length.
If a cached pointer doesn’t exist, a new Buffer will be added to the cache and returned.
Example
use custos::prelude::*;
let device = CPU::new();
let cache_entry: Buffer = Cache::get(&device, 10, ());
let new_cache_entry: Buffer = Cache::get(&device, 10, ());
assert_ne!(cache_entry.ptrs(), new_cache_entry.ptrs());
set_count(0);
let first_entry: Buffer = Cache::get(&device, 10, ());
assert_eq!(cache_entry.ptrs(), first_entry.ptrs());