Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions oneapi-rs/examples/kernel_launch_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@

use oneapi_rs::prelude::*;

use oneapi_rs::{
buffer::Buffer,
queue::Queue,
range::NdRange,
usm::{SharedAllocator, UsmAllocator},
};
use oneapi_rs::{buffer::SharedBuffer, queue::Queue, range::NdRange};

static IOTA_SRC: &str = r#"
#include <sycl/sycl.hpp>
Expand All @@ -31,7 +26,7 @@ void iota(float start, float *ptr) {
#[derive(KernelArgumentList)]
struct IotaArgs<'a> {
start: f32,
ptr: &'a mut Buffer<f32, UsmAllocator<SharedAllocator>>,
ptr: &'a mut SharedBuffer<f32>,
}

fn main() {
Expand Down
16 changes: 15 additions & 1 deletion oneapi-rs/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ use pin_project::pin_project;
use crate::{
event::{Event, EventFuture},
kernel::KernelArgument,
usm::{HostAccessible, UsmAlloc},
usm::{
DeviceAllocator, HostAccessible, HostAllocator, SharedAllocator, UsmAlloc, UsmAllocator,
},
};

/// The Buffer struct defines a shared array of one, two or three dimensions that can be used
Expand Down Expand Up @@ -101,6 +103,10 @@ impl<T, A: UsmAlloc> Drop for Buffer<T, A> {
}
}

pub type HostBuffer<T> = Buffer<T, UsmAllocator<HostAllocator>>;
pub type SharedBuffer<T> = Buffer<T, UsmAllocator<SharedAllocator>>;
pub type DeviceBuffer<T> = Buffer<T, UsmAllocator<DeviceAllocator>>;

/// A [`Buffer`] whose initialization has been enqueued. You need to wait/await it.
pub struct EnqueuedBuffer<T, A: UsmAlloc> {
buffer: Buffer<T, A>,
Expand All @@ -121,6 +127,10 @@ impl<T, A: UsmAlloc> EnqueuedBuffer<T, A> {
}
}

pub type EnqueuedHostBuffer<T> = EnqueuedBuffer<T, UsmAllocator<HostAllocator>>;
pub type EnqueuedSharedBuffer<T> = EnqueuedBuffer<T, UsmAllocator<SharedAllocator>>;
pub type EnqueuedDeviceBuffer<T> = EnqueuedBuffer<T, UsmAllocator<DeviceAllocator>>;

#[pin_project]
/// A [`Future`] which represents a pending [`Buffer`] allocation.
pub struct BufferFuture<T, A: UsmAlloc> {
Expand Down Expand Up @@ -151,6 +161,10 @@ impl<T, A: UsmAlloc> IntoFuture for EnqueuedBuffer<T, A> {
}
}

pub type HostBufferFuture<T> = BufferFuture<T, UsmAllocator<HostAllocator>>;
pub type SharedBufferFuture<T> = BufferFuture<T, UsmAllocator<SharedAllocator>>;
pub type DeviceBufferFuture<T> = BufferFuture<T, UsmAllocator<DeviceAllocator>>;

unsafe impl<T: Pod, A: UsmAlloc> KernelArgument for Buffer<T, A> {
unsafe fn as_raw_arg(&self) -> &[u8] {
unsafe { self.as_raw_arg_impl() }
Expand Down
37 changes: 11 additions & 26 deletions oneapi-rs/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ use bytemuck::Pod;
use oneapi_rs_sys::{queue::ffi, types::ffi::EventPtr};

use crate::{
buffer::{Buffer, EnqueuedBuffer},
buffer::{
Buffer, DeviceBuffer, EnqueuedBuffer, EnqueuedDeviceBuffer, EnqueuedHostBuffer,
EnqueuedSharedBuffer, HostBuffer, SharedBuffer,
},
context::Context,
device::Device,
event::Event,
kernel::{Kernel, KernelArgumentList},
range::{NdRange, ValidDimension},
usm::{DeviceAllocator, HostAllocator, SharedAllocator, UsmAlloc, UsmAllocator},
usm::{UsmAlloc, UsmAllocator},
};

/// The `Queue` connects a host program to a single device. Programs submit tasks to a device via the
Expand All @@ -43,10 +46,7 @@ impl Queue {
}

/// Allocates zeroed memory and creates a host-side [`Buffer`] that can store an array of T.
pub fn alloc_host<T: Pod>(
&mut self,
len: usize,
) -> EnqueuedBuffer<T, UsmAllocator<HostAllocator>> {
pub fn alloc_host<T: Pod>(&mut self, len: usize) -> EnqueuedHostBuffer<T> {
unsafe {
let mut buffer = self.alloc_uninit_host(len);
let event = self.memset(&mut buffer, 0);
Expand All @@ -55,10 +55,7 @@ impl Queue {
}

/// Allocates zeroed memory and creates a shared [`Buffer`] that can store an array of T.
pub fn alloc_shared<T: Pod>(
&mut self,
len: usize,
) -> EnqueuedBuffer<T, UsmAllocator<SharedAllocator>> {
pub fn alloc_shared<T: Pod>(&mut self, len: usize) -> EnqueuedSharedBuffer<T> {
unsafe {
let mut buffer = self.alloc_uninit_shared(len);
let event = self.memset(&mut buffer, 0);
Expand All @@ -67,10 +64,7 @@ impl Queue {
}

/// Allocates zeroed memory and creates a device [`Buffer`] that can store an array of T.
pub fn alloc_device<T: Pod>(
&mut self,
len: usize,
) -> EnqueuedBuffer<T, UsmAllocator<DeviceAllocator>> {
pub fn alloc_device<T: Pod>(&mut self, len: usize) -> EnqueuedDeviceBuffer<T> {
unsafe {
let mut buffer = self.alloc_uninit_device(len);
let event = self.memset(&mut buffer, 0);
Expand All @@ -80,30 +74,21 @@ impl Queue {

/// Allocates memory and creates a host-side [`Buffer`] that can store an array of T.
/// Safety: the buffer contents are uninitialized.
pub unsafe fn alloc_uninit_host<T>(
&self,
len: usize,
) -> Buffer<T, UsmAllocator<HostAllocator>> {
pub unsafe fn alloc_uninit_host<T>(&self, len: usize) -> HostBuffer<T> {
let allocator = UsmAllocator::from(self);
unsafe { Buffer::new(allocator, len) }
}

/// Allocates memory and creates a shared [`Buffer`] that can store an array of T.
/// Safety: the buffer contents are uninitialized.
pub unsafe fn alloc_uninit_shared<T>(
&self,
len: usize,
) -> Buffer<T, UsmAllocator<SharedAllocator>> {
pub unsafe fn alloc_uninit_shared<T>(&self, len: usize) -> SharedBuffer<T> {
let allocator = UsmAllocator::from(self);
unsafe { Buffer::new(allocator, len) }
}

/// Allocates memory and creates a device-side [`Buffer`] that can store an array of T.
/// Safety: the buffer contents are uninitialized.
pub unsafe fn alloc_uninit_device<T>(
&self,
len: usize,
) -> Buffer<T, UsmAllocator<DeviceAllocator>> {
pub unsafe fn alloc_uninit_device<T>(&self, len: usize) -> DeviceBuffer<T> {
let allocator = UsmAllocator::from(self);
unsafe { Buffer::new(allocator, len) }
}
Expand Down