Skip to main content

BackendStorage

Trait BackendStorage 

Source
pub trait BackendStorage<T>:
    Debug
    + Send
    + Sync
    + 'static {
    // Required methods
    fn backend_family(&self) -> &'static str;
    fn len(&self) -> usize;
    fn as_any(&self) -> &dyn Any;

    // Provided methods
    fn is_empty(&self) -> bool { ... }
    fn allocation_domain(&self) -> Option<AllocationDomainId> { ... }
    fn allocation_id(&self) -> Option<AllocationId> { ... }
    fn map_read(&self) -> Result<HostReadGuard<'_, T>, HostAccessError> { ... }
    fn map_write(&mut self) -> Result<HostWriteGuard<'_, T>, HostAccessError> { ... }
    fn shared_host_slice(&self) -> Result<&[T], HostAccessError> { ... }
}
Expand description

Opaque backend-owned tensor buffer.

Tensor core never inspects backend-native allocations directly. Backend crates store their own concrete handle types behind this trait and downcast inside the owning backend only.

§Examples

use std::sync::Arc;
use tenferro_tensor::{BackendStorage, BackendStorageHandle};

let buffer: Arc<dyn BackendStorage<f64>> = Arc::new(BackendStorageHandle::<f64>::new_with_len(7, 2));
assert_eq!(buffer.backend_family(), "opaque");
assert_eq!(buffer.len(), 2);

Required Methods§

Source

fn backend_family(&self) -> &'static str

Stable backend family identifier.

Source

fn len(&self) -> usize

Number of logical elements in the backend allocation.

Source

fn as_any(&self) -> &dyn Any

Type-erased access for the backend crate that owns the concrete handle.

Provided Methods§

Source

fn is_empty(&self) -> bool

Returns true when the backend allocation is empty.

Source

fn allocation_domain(&self) -> Option<AllocationDomainId>

Return the shared-allocation domain, when this buffer belongs to one.

Source

fn allocation_id(&self) -> Option<AllocationId>

Return the stable physical allocation identity, when available.

Source

fn map_read(&self) -> Result<HostReadGuard<'_, T>, HostAccessError>

Map the allocation for closure-scoped host reads.

§Errors

The default returns HostAccessError::Unsupported. Host-visible backends return typed overlap, pending-GPU, or backend mapping failures.

Source

fn map_write(&mut self) -> Result<HostWriteGuard<'_, T>, HostAccessError>

Map the allocation for closure-scoped host writes.

The mutable receiver keeps write authority with the owning tensor or provider object; borrowed views do not clone or share that authority.

§Errors

The default returns HostAccessError::Unsupported. Host-visible backends return typed overlap, pending-GPU, or backend mapping failures.

Source

fn shared_host_slice(&self) -> Result<&[T], HostAccessError>

Borrow the allocation as shared memory the CPU and the device both address.

A provider implements this only for an allocation whose elements are readable at the same address from the host and from the device (a shared allocation domain). The call is the wait: an implementation waits for its pending device writes before it hands out the borrow, so there is no separate fence call and no copy, and the borrow itself is the only host access that exists for such an allocation.

This is deliberately a borrow rather than a closure-scoped guard like Self::map_read: a shared allocation is never mapped or unmapped, and a CPU kernel needs a StridedView<'a, T> that outlives such a guard. Exclusive CPU writes still go through Self::map_write and the ownership kernel’s exclusive capability.

§Examples
use tenferro_tensor::{BackendStorage, BackendStorageHandle};

// An opaque handle exposes no shared memory, so it keeps the refusal.
let buffer = BackendStorageHandle::<f32>::new_with_len(1, 4);
assert!(buffer.shared_host_slice().is_err());
§Errors

The default returns HostAccessError::Unsupported, so every device-local backend refuses shared host access exactly as before. A provider that cannot serve the borrow returns its own typed failure.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl<T: Send + Sync + 'static> BackendStorage<T> for BackendStorageHandle<T>