01-allocate.rs 588 Bytes
Newer Older
yongshk's avatar
yongshk committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
use cudarc::driver::{CudaDevice, CudaSlice, DriverError};

fn main() -> Result<(), DriverError> {
    let dev = CudaDevice::new(0)?;

    // unsafe initialization of unset memory
    let _: CudaSlice<f32> = unsafe { dev.alloc::<f32>(10) }?;

    // this will have memory initialized as 0
    let _: CudaSlice<f64> = dev.alloc_zeros::<f64>(10)?;

    // initialize with a rust vec
    let _: CudaSlice<usize> = dev.htod_copy(vec![0; 10])?;

    // or finially, initialize with a slice. this is synchronous though.
    let _: CudaSlice<u32> = dev.htod_sync_copy(&[1, 2, 3])?;

    Ok(())
}