Module krnl::buffer

source ·
Expand description

Buffers.

Buffers store data, and can be arguments to kernels. Buffers on the host store data inline, essentially as a Vec or slice.

BufferBase is a generic buffer. ScalarBufferBase is a dynamically typed buffer.

§Example

use krnl::{anyhow::Result, device::Device, buffer::{Buffer, Slice, SliceMut}};

fn saxpy(x: Slice<f32>, alpha: f32, mut y: SliceMut<f32>) -> Result<()> {
    if let Some((x, y)) = x.as_host_slice().zip(y.as_host_slice_mut()) {
        for (x, y) in x.iter().copied().zip(y) {
            *y += alpha * x;
        }
        return Ok(());
    }
    todo!()
}

fn main() -> Result<()> {
    let x = vec![1f32];
    let alpha = 2f32;
    let y = vec![0f32];
    let device = Device::builder().build().ok().unwrap_or(Device::host());
    let x = Buffer::from(x).into_device(device.clone())?;
    let mut y = Buffer::from(y).into_device(device.clone())?;
    saxpy(x.as_slice(), alpha, y.as_slice_mut())?;
    let y = y.into_vec()?;
    println!("{y:?}");
    Ok(())
}

Modules§

Structs§

Enums§

Traits§

Type Aliases§