MCPcopy Create free account
hub / github.com/Rust-GPU/rust-cuda / cuda_malloc_async

Function cuda_malloc_async

crates/cust/src/memory/malloc.rs:62–80  ·  view source on GitHub ↗

Unsafe wrapper around `cuMemAllocAsync` which queues a memory allocation operation on a stream. Retains all of the unsafe semantics of [`cuda_malloc`] with the extra requirement that the memory must not be used until it is allocated on the stream. Therefore, proper stream ordering semantics must be respected. # Safety The memory behind the returned pointer must not be used in any way until the a

(
    stream: &Stream,
    count: usize,
)

Source from the content-addressed store, hash-verified

60/// The memory behind the returned pointer must not be used in any way until the
61/// allocation actually takes place in the stream.
62pub unsafe fn cuda_malloc_async<T: DeviceCopy>(
63 stream: &Stream,
64 count: usize,
65) -> CudaResult<DevicePointer<T>> {
66 let size = count.checked_mul(mem::size_of::<T>()).unwrap_or(0);
67 if size == 0 {
68 return Err(CudaError::InvalidMemoryAllocation);
69 }
70
71 let mut ptr: *mut c_void = ptr::null_mut();
72 cuda::cuMemAllocAsync(
73 &mut ptr as *mut *mut c_void as *mut u64,
74 size,
75 stream.as_inner(),
76 )
77 .to_result()?;
78 let ptr = ptr as *mut T;
79 Ok(DevicePointer::from_raw(ptr as cuda::CUdeviceptr))
80}
81
82/// Unsafe wrapper around `cuMemFreeAsync` which queues a memory allocation free operation on a stream.
83/// Retains all of the unsafe semantics of [`cuda_free`] with the extra requirement that the memory

Callers 2

uninitialized_asyncMethod · 0.85
uninitialized_asyncMethod · 0.85

Calls 2

to_resultMethod · 0.45
as_innerMethod · 0.45

Tested by

no test coverage detected