Skip to content
Open
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
2 changes: 1 addition & 1 deletion oneapi-rs-sys/src/event-sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub mod ffi {
#[namespace = "sycl_shims"]
type Queue = crate::types::ffi::Queue;

fn wait(event: &mut UniquePtr<Event>);
fn wait(event: &mut UniquePtr<Event>) -> Result<()>;
unsafe fn register_callback(
queue: &mut UniquePtr<Queue>,
event: &Event,
Expand Down
10 changes: 7 additions & 3 deletions oneapi-rs-sys/src/kernel-bundle-sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ pub mod ffi {
fn create_kernel_bundle_from_source(
ctxt: &Context,
source: &str,
) -> UniquePtr<SourceKernelBundle>;
fn build(source: &mut UniquePtr<SourceKernelBundle>) -> UniquePtr<ExecutableKernelBundle>;
) -> Result<UniquePtr<SourceKernelBundle>>;

fn build(
source: &mut UniquePtr<SourceKernelBundle>,
) -> Result<UniquePtr<ExecutableKernelBundle>>;

fn get_kernel(
bundle: &mut UniquePtr<ExecutableKernelBundle>,
name: &str,
) -> UniquePtr<Kernel>;
) -> Result<UniquePtr<Kernel>>;
}
}
14 changes: 9 additions & 5 deletions oneapi-rs-sys/src/queue-sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,36 @@ pub mod ffi {
dep_events: Vec<EventPtr>,
) -> UniquePtr<Event>;

fn barrier(queue: &mut UniquePtr<Queue>, dep_events: Vec<EventPtr>) -> UniquePtr<Event>;
fn wait(queue: &mut UniquePtr<Queue>);
fn barrier(
queue: &mut UniquePtr<Queue>,
dep_events: Vec<EventPtr>,
) -> Result<UniquePtr<Event>>;

fn wait(queue: &mut UniquePtr<Queue>) -> Result<()>;

unsafe fn launch_1d(
queue: &mut UniquePtr<Queue>,
global_size: Range1,
local_size: Range1,
kernel: &Kernel,
args: &[&[u8]],
) -> UniquePtr<Event>;
) -> Result<UniquePtr<Event>>;

unsafe fn launch_2d(
queue: &mut UniquePtr<Queue>,
global_size: Range2,
local_size: Range2,
kernel: &Kernel,
args: &[&[u8]],
) -> UniquePtr<Event>;
) -> Result<UniquePtr<Event>>;

unsafe fn launch_3d(
queue: &mut UniquePtr<Queue>,
global_size: Range3,
local_size: Range3,
kernel: &Kernel,
args: &[&[u8]],
) -> UniquePtr<Event>;
) -> Result<UniquePtr<Event>>;

unsafe fn memcpy(
queue: &mut UniquePtr<Queue>,
Expand Down
20 changes: 11 additions & 9 deletions oneapi-rs/examples/kernel_launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,33 @@ void iota(float start, float *ptr) {
"#;

#[tokio::main]
async fn main() {
async fn main() -> oneapi_rs::Result<()> {
let mut queue = Queue::new();
let mut device_buffer = queue.alloc_device::<f32>(1024).await;
let mut device_buffer = queue.alloc_device::<f32>(1024).await?;

let kernel = queue
.get_context()
.create_kernel_bundle_from_source(IOTA_SRC)
.build()
.get_kernel("iota");
.create_kernel_bundle_from_source(IOTA_SRC)?
.build()?
.get_kernel("iota")?;

unsafe {
queue.launch(
NdRange::new([1024], [16]),
&kernel,
(3.14_f32, &mut device_buffer),
)
}
.await;
}?
.await?;

let mut host_buffer = queue.alloc_host::<f32>(1024).await;
let mut host_buffer = queue.alloc_host::<f32>(1024).await?;

queue.copy(&device_buffer, &mut host_buffer).await;
queue.copy(&device_buffer, &mut host_buffer).await?;

for e in host_buffer.iter() {
print!("{e} ");
}
println!();

Ok(())
}
16 changes: 9 additions & 7 deletions oneapi-rs/examples/kernel_launch_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ struct IotaArgs<'a> {
ptr: &'a mut SharedBuffer<f32>,
}

fn main() {
fn main() -> oneapi_rs::Result<()> {
let mut queue = Queue::new();
let mut buffer = queue.alloc_shared::<f32>(1024).wait();
let mut buffer = queue.alloc_shared::<f32>(1024).wait()?;

let kernel = queue
.get_context()
.create_kernel_bundle_from_source(IOTA_SRC)
.build()
.get_kernel("iota");
.create_kernel_bundle_from_source(IOTA_SRC)?
.build()?
.get_kernel("iota")?;

unsafe {
queue.launch(
Expand All @@ -46,11 +46,13 @@ fn main() {
ptr: &mut buffer,
},
)
}
.wait();
}?
.wait()?;

for e in buffer.iter() {
print!("{e} ");
}
println!();

Ok(())
}
12 changes: 6 additions & 6 deletions oneapi-rs/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use bytemuck::Pod;
use pin_project::pin_project;

use crate::{
Result,
event::{Event, EventFuture},
kernel::KernelArgument,
usm::{
Expand Down Expand Up @@ -121,9 +122,8 @@ impl<T, A: UsmAlloc> EnqueuedBuffer<T, A> {

impl<T, A: UsmAlloc> EnqueuedBuffer<T, A> {
/// Waits for [`Buffer`] initialization to finish.
pub fn wait(mut self) -> Buffer<T, A> {
self.event.wait();
self.buffer
pub fn wait(mut self) -> Result<Buffer<T, A>> {
self.event.wait().map(|_| self.buffer)
}
}

Expand All @@ -140,17 +140,17 @@ pub struct BufferFuture<T, A: UsmAlloc> {
}

impl<T, A: UsmAlloc> Future for BufferFuture<T, A> {
type Output = Buffer<T, A>;
type Output = Result<Buffer<T, A>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
this.event_future
.poll(cx)
.map(|_| this.buffer.take().unwrap())
.map(|result| result.map(|_| this.buffer.take().unwrap()))
}
}

impl<T, A: UsmAlloc> IntoFuture for EnqueuedBuffer<T, A> {
type Output = Buffer<T, A>;
type Output = Result<Buffer<T, A>>;
type IntoFuture = BufferFuture<T, A>;

fn into_future(self) -> Self::IntoFuture {
Expand Down
6 changes: 3 additions & 3 deletions oneapi-rs/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use oneapi_rs_sys::{context::ffi, kernel_bundle, types::ffi::DevicePtr};

use crate::{device::Device, kernel::SourceKernelBundle};
use crate::{Result, device::Device, kernel::SourceKernelBundle};

/// A context represents the runtime data structures and state required by a SYCL backend API
/// to interact with a group of devices associated with a platform.
Expand All @@ -32,7 +32,7 @@ impl Context {
ffi::new_context(devices).into()
}

pub fn create_kernel_bundle_from_source(&self, source: &str) -> SourceKernelBundle {
kernel_bundle::ffi::create_kernel_bundle_from_source(&self.0, source).into()
pub fn create_kernel_bundle_from_source(&self, source: &str) -> Result<SourceKernelBundle> {
kernel_bundle::ffi::create_kernel_bundle_from_source(&self.0, source).map(Into::into)
}
}
16 changes: 9 additions & 7 deletions oneapi-rs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ use oneapi_rs_sys::{event::ffi, types::SharedWaker};

use pin_project::pin_project;

use crate::{info::InfoTarget, private::Sealed, queue::Queue};
use crate::{Result, info::InfoTarget, private::Sealed, queue::Queue};

pub struct Event(pub(crate) cxx::UniquePtr<ffi::Event>);

impl Event {
pub fn wait(&mut self) {
ffi::wait(&mut self.0);
pub fn wait(&mut self) -> Result<()> {
ffi::wait(&mut self.0)
}
}

Expand Down Expand Up @@ -50,7 +50,7 @@ pub struct EventFuture {
}

impl Future for EventFuture {
type Output = ();
type Output = Result<()>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
Expand All @@ -67,7 +67,8 @@ impl Future for EventFuture {
} else {
// Quick check before registering to avoid wasting time
if this.shared.done.load(Relaxed) {
return Poll::Ready(());
// The event finished - waiting for it returns immediately
return Poll::Ready(this.event.wait());
}

this.shared.waker.register(cx.waker());
Expand All @@ -76,15 +77,16 @@ impl Future for EventFuture {
// Check the event again to avoid a race condition
// https://docs.rs/futures/latest/futures/task/struct.AtomicWaker.html#examples
if this.shared.done.load(Relaxed) {
Poll::Ready(())
// The event finished - waiting for it returns immediately
Poll::Ready(this.event.wait())
} else {
Poll::Pending
}
}
}

impl IntoFuture for Event {
type Output = ();
type Output = Result<()>;
type IntoFuture = EventFuture;

fn into_future(self) -> Self::IntoFuture {
Expand Down
9 changes: 5 additions & 4 deletions oneapi-rs/src/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
//

use crate::Result;
use bytemuck::Pod;
use oneapi_rs_sys::{kernel_bundle::ffi, types};

Expand All @@ -19,8 +20,8 @@ impl From<cxx::UniquePtr<types::ffi::SourceKernelBundle>> for SourceKernelBundle
}

impl SourceKernelBundle {
pub fn build(&mut self) -> ExecutableKernelBundle {
ffi::build(&mut self.0).into()
pub fn build(&mut self) -> Result<ExecutableKernelBundle> {
ffi::build(&mut self.0).map(Into::into)
}
}

Expand All @@ -34,8 +35,8 @@ impl From<cxx::UniquePtr<types::ffi::ExecutableKernelBundle>> for ExecutableKern
}

impl ExecutableKernelBundle {
pub fn get_kernel(&mut self, name: &str) -> Kernel {
ffi::get_kernel(&mut self.0, name).into()
pub fn get_kernel(&mut self, name: &str) -> Result<Kernel> {
ffi::get_kernel(&mut self.0, name).map(Into::into)
}
}

Expand Down
3 changes: 3 additions & 0 deletions oneapi-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ pub mod queue;
pub mod range;
pub mod usm;

pub type SyclError = cxx::Exception;
pub type Result<T> = std::result::Result<T, SyclError>;

mod private {
pub trait Sealed {}
}
15 changes: 8 additions & 7 deletions oneapi-rs/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
//

use crate::Result;
use bytemuck::Pod;
use oneapi_rs_sys::{queue::ffi, types::ffi::EventPtr};

Expand Down Expand Up @@ -121,24 +122,24 @@ impl Queue {
}

/// Submits a barrier to the queue.
pub fn barrier(&mut self) -> Event {
self.barrier_with_deps(&[])
pub fn barrier(&mut self) -> Result<Event> {
self.barrier_with_deps(&[]).map(Into::into)
}

/// Submits a barrier to the queue after all specified events finish.
pub fn barrier_with_deps(&mut self, dep_events: &[&Event]) -> Event {
pub fn barrier_with_deps(&mut self, dep_events: &[&Event]) -> Result<Event> {
let dep_events = dep_events
.iter()
.map(|e| EventPtr {
ptr: (*e).clone().0,
})
.collect::<Vec<_>>();
ffi::barrier(&mut self.0, dep_events).into()
ffi::barrier(&mut self.0, dep_events).map(Into::into)
}

/// Performs a blocking wait for the completion of all enqueued tasks in the queue.
pub fn wait(&mut self) {
ffi::wait(&mut self.0);
pub fn wait(&mut self) -> Result<()> {
ffi::wait(&mut self.0)
}

/// Enqueues a kernel object to the queue as an ND-range kernel, using the number of work-items
Expand All @@ -148,7 +149,7 @@ impl Queue {
nd_range: NdRange<DIMENSIONS>,
kernel: &Kernel,
args: impl KernelArgumentList<ARGC>,
) -> Event
) -> Result<Event>
where
NdRange<DIMENSIONS>: ValidDimension,
{
Expand Down
Loading