Skip to main content

PinnedHost

Struct PinnedHost 

Source
pub struct PinnedHost(/* private fields */);
Expand description

An allocator for page-locked host memory, drawn from a pool its CUDA context owns.

Get one from Context::pinned_host. A Vec or Box that uses it as its allocator lives in pinned memory, which CUDA copies read and write directly instead of staging through pageable memory. PinnedVec is such a vector, and it is what this crate’s pinned copies accept. Cloning is cheap, and all clones from one context share its pool, so a buffer may be freed through any of them.

§GPU work

The pool does not know which buffers queued GPU work still uses. Keep a buffer alive until that work has finished; scoped copies and this crate’s submissions do so.

§Allocation failure

A Vec that cannot grow aborts the process, as it does with any allocator. When the size is known up front, allocate with PinnedHost::with_capacity, PinnedHost::from_slice, or PinnedHost::zeroed, which return an error instead.

§How it works

The pool takes chunks of at least 2 MiB from cudaHostAlloc and splits them into blocks. Blocks up to 2 MiB come in power-of-two sizes from 256 bytes, and a larger block gets a chunk of its own, rounded up to a multiple of 2 MiB. Dropping a buffer returns its block to the pool without a CUDA call, so it is cheap and allowed inside a stream callback. PinnedHost::release_cached returns unused chunks to CUDA. Chunks are portable and mapped, so any context can copy from them.

Implementations§

Source§

impl PinnedHost

Source

pub fn context(&self) -> &Arc<Context> ⓘ

Returns the context whose pool this allocator uses.

Source

pub fn with_capacity<T>(&self, capacity: usize) -> Result<PinnedVec<T>>

Allocates an empty vector with room for exactly capacity values.

§Errors

Returns an error if the byte size overflows, T needs an alignment above 4096 bytes, the context cannot be bound, or CUDA cannot allocate page-locked memory.

Source

pub fn from_slice<T: Copy>(&self, values: &[T]) -> Result<PinnedVec<T>>

Allocates a vector holding a copy of values.

§Errors

Returns an error if allocation fails, for the reasons PinnedHost::with_capacity gives.

Source

pub fn zeroed<T: ZeroableDeviceRepr>( &self, length: usize, ) -> Result<PinnedVec<T>>

Allocates a vector of length zero values.

§Errors

Returns an error if allocation fails, for the reasons PinnedHost::with_capacity gives.

Source

pub fn release_cached(&self) -> Result<()>

Frees the pool’s chunks whose blocks are all free.

Chunks with a live block stay. The pool also frees idle single-block chunks on its own, when an allocation finds no free block of its size.

§Errors

Returns an error if the context cannot be bound or CUDA fails to free a chunk; every idle chunk leaves the pool either way.

Source

pub fn stats(&self) -> PinnedStats

Returns how many bytes and chunks the pool holds, and how many bytes are in use.

Trait Implementations§

Source§

impl Allocator for PinnedHost

Allocates blocks from the context’s pool. A zero-sized layout takes no memory. Growing or shrinking keeps a block in place while the new layout falls in the block’s size class.

Source§

fn allocate(&self, layout: Layout) -> StdResult<NonNull<[u8]>, AllocError>

Attempts to allocate a block of memory. Read more
Source§

unsafe fn deallocate(&self, block: NonNull<u8>, layout: Layout)

Deallocates the memory referenced by ptr. Read more
Source§

unsafe fn grow( &self, block: NonNull<u8>, old: Layout, new: Layout, ) -> StdResult<NonNull<[u8]>, AllocError>

Attempts to extend the memory block. Read more
Source§

unsafe fn grow_zeroed( &self, block: NonNull<u8>, old: Layout, new: Layout, ) -> StdResult<NonNull<[u8]>, AllocError>

Behaves like grow, but also ensures that the new contents are set to zero before being returned. Read more
Source§

unsafe fn shrink( &self, block: NonNull<u8>, old: Layout, new: Layout, ) -> StdResult<NonNull<[u8]>, AllocError>

Attempts to shrink the memory block. Read more
1.100.0 (const: unstable) · Source§

fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>

Behaves like allocate, but also ensures that the returned memory is zero-initialized. Read more
Source§

impl Clone for PinnedHost

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for PinnedHost

Source§

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

Formats the value using the given formatter. Read more

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.