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
impl PinnedHost
Sourcepub fn with_capacity<T>(&self, capacity: usize) -> Result<PinnedVec<T>>
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.
Sourcepub fn from_slice<T: Copy>(&self, values: &[T]) -> Result<PinnedVec<T>>
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.
Sourcepub fn zeroed<T: ZeroableDeviceRepr>(
&self,
length: usize,
) -> Result<PinnedVec<T>>
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.
Sourcepub fn release_cached(&self) -> Result<()>
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.
Sourcepub fn stats(&self) -> PinnedStats
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.
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>
fn allocate(&self, layout: Layout) -> StdResult<NonNull<[u8]>, AllocError>
Source§unsafe fn deallocate(&self, block: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, block: NonNull<u8>, layout: Layout)
ptr. Read moreSource§unsafe fn grow(
&self,
block: NonNull<u8>,
old: Layout,
new: Layout,
) -> StdResult<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, block: NonNull<u8>, old: Layout, new: Layout, ) -> StdResult<NonNull<[u8]>, AllocError>
Source§unsafe fn grow_zeroed(
&self,
block: NonNull<u8>,
old: Layout,
new: Layout,
) -> StdResult<NonNull<[u8]>, AllocError>
unsafe fn grow_zeroed( &self, block: NonNull<u8>, old: Layout, new: Layout, ) -> StdResult<NonNull<[u8]>, AllocError>
grow, but also ensures that the new contents are set to zero before being
returned. Read moreSource§unsafe fn shrink(
&self,
block: NonNull<u8>,
old: Layout,
new: Layout,
) -> StdResult<NonNull<[u8]>, AllocError>
unsafe fn shrink( &self, block: NonNull<u8>, old: Layout, new: Layout, ) -> StdResult<NonNull<[u8]>, AllocError>
1.100.0 (const: unstable) · Source§fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocate, but also ensures that the returned memory is zero-initialized. Read more