Expand description
Type-safe buffer reinterpretation for device memory.
This module provides BufferView and BufferViewMut, which allow
reinterpreting a DeviceBuffer<T> as a different element type U
without copying data. This is useful for viewing a buffer of f32
values as u32 (e.g., for bitwise operations in a kernel), or for
interpreting raw byte buffers as structured types.
§Size and alignment constraints
The total byte size of the original buffer must be evenly divisible
by std::mem::size_of::<U>(), and the buffer’s device pointer must be
aligned to std::mem::align_of::<U>(). If either constraint is
violated, the view creation returns CudaError::InvalidValue.
Allocations made via cuMemAlloc-backed buffers (e.g.
DeviceBuffer::alloc) are
always sufficiently aligned for any U no larger than the CUDA driver’s
allocation alignment guarantee, so this only rejects genuinely
misaligned reinterpretations (e.g. viewing a byte-offset sub-buffer as a
wider type).
§Example
let buf = DeviceBuffer::<f32>::alloc(256)?;
// Reinterpret as u32 (same size, different type)
let view: BufferView<'_, u32> = buf.view_as::<u32>()?;
assert_eq!(view.len(), 256);Structs§
- Buffer
View - An immutable, type-reinterpreted view into a
DeviceBuffer. - Buffer
View Mut - A mutable, type-reinterpreted view into a
DeviceBuffer.