Skip to main content

Module zero_copy

Module zero_copy 

Source
Expand description

Zero-copy (host-mapped) memory.

Allows GPU kernels to directly access host memory without explicit transfers. Useful for small, frequently-updated data or when PCIe bandwidth is acceptable.

§How it works

Zero-copy memory is allocated on the host using cuMemAllocHost_v2, which allocates page-locked (pinned) memory that the CUDA driver maps into the device’s address space. A corresponding device pointer is obtained via cuMemHostGetDevicePointer_v2. GPU reads and writes traverse the PCIe bus on each access, so this is best suited for data that is accessed infrequently or streamed sequentially.

§Example

use oxicuda_memory::zero_copy::MappedBuffer;

oxicuda_driver::init()?;
let _ = oxicuda_driver::primary_context::PrimaryContext::retain(
    &oxicuda_driver::device::Device::get(0)?
)?;

let mut buf = MappedBuffer::<f32>::alloc(256)?;
// Write from the host.
for (i, val) in buf.as_host_slice_mut().iter_mut().enumerate() {
    *val = i as f32;
}
// `buf.as_device_ptr()` can now be passed to a kernel.

Structs§

MappedBuffer
A host-allocated, device-mapped (zero-copy) memory buffer.