Expand description
Unified (managed) memory buffer.
UnifiedBuffer<T> wraps cuMemAllocManaged, which allocates memory
that is automatically migrated between host and device by the CUDA
Unified Memory subsystem. The allocation is accessible from both CPU
code (via as_slice /
as_mut_slice) and GPU kernels (via
as_device_ptr).
§Coherence caveat
The host-side accessors are only safe to call when no GPU kernel is concurrently reading or writing the same memory. After launching a kernel that touches a unified buffer, synchronise the stream (or the entire context) before accessing the data from the host.
§Ownership
The allocation is freed with cuMemFree_v2 on drop. Errors during
drop are logged via tracing::warn.
§Example
let mut ubuf = UnifiedBuffer::<f32>::alloc(512)?;
// Write from the host side (no kernel running).
for (i, v) in ubuf.as_mut_slice().iter_mut().enumerate() {
*v = i as f32;
}
// Pass ubuf.as_device_ptr() to a kernel…Structs§
- Unified
Buffer - A contiguous buffer of
Telements in CUDA unified (managed) memory.