Skip to main content

TensorStorage

Enum TensorStorage 

Source
pub enum TensorStorage<T: TensorElement> {
    InMemory(Arc<RwLock<Vec<T>>>),
    MemoryMapped(Arc<RwLock<MemoryMappedStorage<T>>>),
}
Expand description

Storage abstraction for tensor data

Variants§

§

InMemory(Arc<RwLock<Vec<T>>>)

In-memory storage for smaller tensors

§

MemoryMapped(Arc<RwLock<MemoryMappedStorage<T>>>)

Memory-mapped storage for large tensors

Implementations§

Source§

impl<T: TensorElement + Copy> TensorStorage<T>

Source

pub fn in_memory(data: Vec<T>) -> Self

Create in-memory storage

Source

pub fn memory_mapped(data: Vec<T>, file_path: Option<PathBuf>) -> Result<Self>

Create memory-mapped storage

Source

pub fn fast_result(data: Vec<T>) -> Self

🚀 Phase 7: Create fast result storage (skips alignment copy)

For SIMD operation results where we already have the data in a Vec, uses InMemory storage to avoid the ~10µs alignment copy overhead.

§Performance
  • Skips AlignedVec copy (saves ~10µs for 50K elements)
  • Uses InMemory storage (has RwLock but we just created it)
  • Optimal for result tensors that won’t be immediately used in SIMD ops
Source

pub fn create_optimal(data: Vec<T>) -> Result<Self>

Create storage automatically based on size and performance characteristics

Storage Selection Strategy:

  • Very large (>1GB): Memory-mapped for virtual memory efficiency
  • Large (>10KB, SIMD enabled): SimdOptimized (lock-free reads)
  • Medium (>1KB, SIMD enabled): Aligned storage for SIMD alignment
  • Small (<1KB): In-memory with RwLock
Source

pub fn len(&self) -> usize

Get the number of elements

Source

pub fn is_empty(&self) -> bool

Check if storage is empty

Source

pub fn get(&self, index: usize) -> Result<T>
where T: Copy,

Get element at index

Source

pub fn set(&self, index: usize, value: T) -> Result<()>
where T: Copy,

Set element at index

Source

pub fn get_slice(&self, start: usize, len: usize) -> Result<Vec<T>>
where T: Copy,

Get multiple elements

Source

pub fn set_slice(&self, start: usize, values: &[T]) -> Result<()>
where T: Copy,

Set multiple elements

Source

pub fn to_vec(&self) -> Result<Vec<T>>
where T: Copy,

Convert to vector (useful for small tensors or debugging)

Source

pub fn storage_type(&self) -> &'static str

Get storage type information

Source

pub fn memory_usage(&self) -> usize

Get estimated memory usage in bytes

Source

pub fn with_slice<R, F>(&self, f: F) -> Result<R>
where F: FnOnce(&[T]) -> Result<R>, T: Copy,

Execute a function with immutable access to data slice (zero-copy within scope)

This enables zero-copy SIMD operations by providing direct &[T] access within the closure scope while the lock is held.

§Arguments
  • f - Closure that receives &[T] and returns Result<R>
§Returns

Result from the closure

§Performance
  • Zero allocations for in-memory and aligned storage
  • Converts memory-mapped storage to Vec (one allocation)
§Examples
storage.with_slice(|data| {
    // Direct SIMD access to data
    f32::simd_add(&data, &other_data)
})?;
Source

pub fn with_slice_mut<R, F>(&self, f: F) -> Result<R>
where F: FnOnce(&mut [T]) -> Result<R>, T: Copy,

Execute a function with mutable access to data slice (zero-copy within scope)

This enables zero-copy in-place operations by providing direct &mut [T] access within the closure scope while the lock is held.

§Arguments
  • f - Closure that receives &mut [T] and returns Result<R>
§Returns

Result from the closure

§Performance
  • Zero allocations for in-memory storage
  • Aligned storage not yet supported (returns error)
  • Memory-mapped storage not supported for mutable access (returns error)
§Examples
storage.with_slice_mut(|data| {
    // In-place SIMD operation
    f32::simd_add_inplace(data, &other_data)
})?;

Trait Implementations§

Source§

impl<T: TensorElement> Clone for TensorStorage<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: TensorElement> Debug for TensorStorage<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for TensorStorage<T>

§

impl<T> RefUnwindSafe for TensorStorage<T>

§

impl<T> Send for TensorStorage<T>

§

impl<T> Sync for TensorStorage<T>

§

impl<T> Unpin for TensorStorage<T>

§

impl<T> UnsafeUnpin for TensorStorage<T>

§

impl<T> UnwindSafe for TensorStorage<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V