Buffer

Struct Buffer 

Source
pub struct Buffer { /* private fields */ }
Expand description

Data storring buffer.

Deletes data on drop.

Implementations§

Source§

impl Buffer

Buffer creation section.

Source

pub fn try_clone(&self) -> Result<Self, Error>

Try clone buffer content.

Note: If memory type is not Cpu, should be called in sync with cuda context pinned (check module level documentation for more info).

Source

pub fn alloc<T: Sample>( count: usize, memory_type: MemoryType, ) -> Result<Self, Error>

Allocate new buffer of requested memory type.
count: size of buffer in T units (i.e. 128 chunks of f32 (that has byte size 512) should be allocated with count=128).
memory_type: Cpu/Pinned/Gpu.

Note: If memory type is not Cpu, should be called in sync with cuda context pinned (check module level documentation for more info).

Source

pub fn alloc_with_data_type( count: usize, memory_type: MemoryType, data_type: DataType, ) -> Result<Self, Error>

Allocate new buffer of requested memory type.
count: size of buffer in T units (i.e. 128 chunks of f32 (that has byte size 512) should be allocated with count=128).
memory_type: Cpu/Pinned/Gpu.
data_type: type of Samples.

Note: If memory type is not Cpu, should be called in sync with cuda context pinned (check module level documentation for more info).

Source

pub fn from<T: Sample, S: AsRef<[T]>>(slice: S) -> Self

Create CPU buffer of data type T::DARA_TYPE from slice of T.

Source§

impl Buffer

Buffer metadata section.

Source

pub fn memory_type(&self) -> MemoryType

Get memory type of storred data.

Source

pub fn data_type(&self) -> DataType

Get data type of storred data.

Source

pub fn size(&self) -> usize

Get byte size of data.

Source

pub fn is_empty(&self) -> bool

True if not containing any data.

Source§

impl Buffer

Buffer data permutation section.

Source

pub fn copy_from_slice<S: AsRef<[T]>, T: Sample>( &mut self, offset: usize, source: S, ) -> Result<(), Error>

Copy source content to self from the offset position.
Returns error if offset + size_of_val(source) > self.size().

offset: offset (in bytes) from the beginning of the Buffer to location to copy source to. source: slice of Samples.

Note: If self.memory_type is not Cpu, should be called in sync with cuda context pinned (check module level documentation for more info).

Source

pub fn copy_from_cuda_array( &mut self, offset: usize, source: CudaArray, ) -> Result<(), Error>

Copy source content to self from the offset position.
Returns error if offset + source.len > self.size().

offset: offset (in bytes) from the beginning of the Buffer to location to copy source to. source: cuda array.

Note: This method should be called in sync with cuda context pinned (check module level documentation for more info).

Source

pub fn into_cpu(self) -> Result<Self, Error>

Move this Buffer content to CPU memory.

Note: If self.memory_type() is not Cpu, method should be called in sync with cuda context pinned (check module level documentation for more info).

Source

pub fn into_pinned(self) -> Result<Self, Error>

Move this Buffer content to Pinned memory.

Note: This method should be called in sync with cuda context pinned (check module level documentation for more info).

Source

pub fn into_gpu(self) -> Result<Self, Error>

Move this Buffer content to Gpu memory.

Note: This method should be called in sync with cuda context pinned (check module level documentation for more info).

Source§

impl Buffer

Obtaining buffer content section.

Source

pub fn bytes(&self) -> &[u8]

Get buffer content as bytes.

Will return nothing if self.memory_type == Gpu. Use Buffer::get_owned_slice instead.

Source

pub fn bytes_mut(&mut self) -> &mut [u8]

Get buffer mutable content as bytes.

Will return nothing if self.memory_type == Gpu. Buffer::get_cuda_array can be used instead to implement this logic.

Source

pub fn get_owned_slice<Range: RangeBounds<usize> + Debug>( &self, range: Range, ) -> Result<Vec<u8>, Error>

Get content of the buffer as host located bytes.
range: part of the buffer to return.

Source

pub unsafe fn get_cuda_array(&self) -> CudaArray

Get content of the GPU based buffer.

§Panics

Panics if self.memory_type != Gpu.

§Safety

Returned struct points to the same location as buffer, so the rules are the same as sharing *mut on an object.
Be careful: Buffer will delete data on drop, so be afraid of double memory free. Also any shinenigans with the data during the inference are forbidden: Triton must have exclusive write ascess to data during the inference.

Trait Implementations§

Source§

impl<T: Sample> AsMut<[T]> for Buffer

Source§

fn as_mut(&mut self) -> &mut [T]

Converts this type into a mutable reference on the slice of T.

Will return nothing if self.memory_type == Gpu.

§Panics

Panics if T does not match Buffer data type.

Source§

impl<T: Sample> AsRef<[T]> for Buffer

Source§

fn as_ref(&self) -> &[T]

Converts this type into a shared reference on the slice of T.

Will return nothing if self.memory_type == Gpu.

§Panics

Panics if T does not match Buffer data type.

Source§

impl Debug for Buffer

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Drop for Buffer

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl From<Buffer> for CudaArray

Available on crate feature gpu only.

Create CudaArray from Buffer.

Buffer destructor will not be invoked so data will be safe.

Source§

fn from(value: Buffer) -> CudaArray

Converts to this type from the input type.
Source§

impl From<CudaArray> for Buffer

Available on crate feature gpu only.

Create GPU buffers of DataType::Uint8 from CudaArray. Result memory type will be MemoryType::Gpu.

Note that nothing is allocated on this call, meaning that result buffer will just point on data provided by argument.

Source§

fn from(value: CudaArray) -> Self

Converts to this type from the input type.
Source§

impl Send for Buffer

Source§

impl Sync for Buffer

Auto Trait Implementations§

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> 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, 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.