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 sycl/sycl-rs-sys/src/context-sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ pub mod ffi {
#[namespace = "sycl_shims"]
type Context = crate::types::ffi::Context;

fn new_context(devices: Vec<DevicePtr>) -> UniquePtr<Context>;
fn new_context(devices: Vec<DevicePtr>) -> Result<UniquePtr<Context>>;
}
}
8 changes: 4 additions & 4 deletions sycl/sycl-rs-sys/src/device-sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub mod ffi {
#[namespace = "sycl_shims"]
type Platform = crate::types::ffi::Platform;

fn get_devices() -> Vec<DevicePtr>;
fn get_platform(device: &Device) -> UniquePtr<Platform>;
fn get_devices() -> Result<Vec<DevicePtr>>;
fn get_platform(device: &Device) -> Result<UniquePtr<Platform>>;
fn get_device_type(device: &Device) -> DeviceType;
fn get_version(device: &Device) -> String;
fn get_name(device: &Device) -> String;
Expand All @@ -39,9 +39,9 @@ pub mod ffi {
device: &mut UniquePtr<Device>,
peer: &Device,
value: PeerAccess,
) -> bool;
) -> Result<bool>;

fn clone(device: &Device) -> UniquePtr<Device>;
fn clone(device: &Device) -> Result<UniquePtr<Device>>;
fn has(device: &Device, aspect: Aspect) -> bool;
}
}
4 changes: 2 additions & 2 deletions sycl/sycl-rs-sys/src/event-sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub mod ffi {
waker: *const SharedWaker,
) -> Result<()>;

fn get_command_execution_status(event: &Event) -> EventCommandStatus;
fn clone(event: &Event) -> UniquePtr<Event>;
fn get_command_execution_status(event: &Event) -> Result<EventCommandStatus>;
fn clone(event: &Event) -> Result<UniquePtr<Event>>;
}

extern "Rust" {
Expand Down
4 changes: 2 additions & 2 deletions sycl/sycl-rs-sys/src/platform-sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ pub mod ffi {
#[namespace = "sycl_shims"]
type Platform = crate::types::ffi::Platform;

fn get_platforms() -> Vec<PlatformPtr>;
fn get_platforms() -> Result<Vec<PlatformPtr>>;

fn get_devices(platform: &Platform) -> Vec<DevicePtr>;
fn get_devices(platform: &Platform) -> Result<Vec<DevicePtr>>;

fn get_version(platform: &Platform) -> String;
fn get_name(platform: &Platform) -> String;
Expand Down
14 changes: 7 additions & 7 deletions sycl/sycl-rs-sys/src/queue-sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ pub mod ffi {
#[namespace = "sycl_shims"]
type Kernel = crate::types::ffi::Kernel;

fn new_queue() -> UniquePtr<Queue>;
fn new_queue_immediate() -> UniquePtr<Queue>;
fn new_queue_from_device(device: &Device) -> UniquePtr<Queue>;
fn new_queue() -> Result<UniquePtr<Queue>>;
fn new_queue_immediate() -> Result<UniquePtr<Queue>>;
fn new_queue_from_device(device: &Device) -> Result<UniquePtr<Queue>>;
fn new_queue_from_context_and_device(
context: &Context,
device: &Device,
) -> UniquePtr<Queue>;
) -> Result<UniquePtr<Queue>>;

fn get_context(queue: &Queue) -> UniquePtr<Context>;
fn get_device(queue: &Queue) -> UniquePtr<Device>;
fn get_context(queue: &Queue) -> Result<UniquePtr<Context>>;
fn get_device(queue: &Queue) -> Result<UniquePtr<Device>>;

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

/// # Safety
///
Expand Down
2 changes: 1 addition & 1 deletion sycl/sycl-rs-sys/src/usm-sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ pub mod ffi {
///
/// `ptr` must be a live USM allocation associated with `queue` and must not be used after
/// this call.
unsafe fn free(ptr: *mut u8, queue: &Queue);
unsafe fn free(ptr: *mut u8, queue: &Queue) -> Result<()>;
}
}
6 changes: 3 additions & 3 deletions sycl/sycl-rs/benches/batch_memcpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ fn memcpy_benchmark(c: &mut Criterion, test_count: u32) -> sycl_rs::Result<()> {
let buf_size = used_memory / buf_count;
let copy_count = buf_count / 2;

let mut queue = Queue::new();
let mut queue = Queue::new()?;
let mut host_buffers = Vec::<HostUsmBox<u32>>::new();
let mut device_buffers = Vec::<DeviceUsmBox<u32>>::new();

for _ in 1..copy_count {
let host_buffer = unsafe { queue.alloc_uninit_host::<u32>(buf_size) };
let device_buffer = unsafe { queue.alloc_uninit_device::<u32>(buf_size) };
let host_buffer = unsafe { queue.alloc_uninit_host::<u32>(buf_size)? };
let device_buffer = unsafe { queue.alloc_uninit_device::<u32>(buf_size)? };

host_buffers.push(host_buffer);
device_buffers.push(device_buffer);
Expand Down
4 changes: 2 additions & 2 deletions sycl/sycl-rs/examples/kernel_launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ void iota(sycl::half start, sycl::half *ptr) {

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

let kernel = queue
.get_context()
.get_context()?
.create_kernel_bundle_from_source(IOTA_SRC)?
.build()?
.get_kernel("iota")?;
Expand Down
4 changes: 2 additions & 2 deletions sycl/sycl-rs/examples/kernel_launch_derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ struct IotaArgs<'a> {
}

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

let kernel = queue
.get_context()
.get_context()?
.create_kernel_bundle_from_source(IOTA_SRC)?
.build()?
.get_kernel("iota")?;
Expand Down
7 changes: 4 additions & 3 deletions sycl/sycl-rs/examples/sycl-ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

use sycl_rs::prelude::*;

fn main() {
for platform in Platform::get_platforms() {
for device in platform.get_devices() {
fn main() -> sycl_rs::Result<()> {
for platform in Platform::get_platforms()? {
for device in platform.get_devices()? {
let device_type = device.get_info::<info::device::DeviceType>();
let platform_name = platform.get_info::<info::platform::Name>();
let device_name = device.get_info::<info::device::Name>();
Expand All @@ -35,4 +35,5 @@ fn main() {
);
}
}
Ok(())
}
15 changes: 9 additions & 6 deletions sycl/sycl-rs/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ impl From<cxx::UniquePtr<ffi::Context>> for Context {
}

impl Context {
pub fn new(devices: &[&Device]) -> Self {
/// Creates a context, returning errors from device copies or context creation.
pub fn new(devices: &[&Device]) -> Result<Self> {
let devices = devices
.iter()
.map(|d| DevicePtr {
ptr: (*d).clone().0,
.map(|d| {
Ok(DevicePtr {
ptr: sycl_rs_sys::device::ffi::clone(&d.0)?,
})
})
.collect::<Vec<_>>();
.collect::<Result<Vec<_>>>()?;

ffi::new_context(devices).into()
Ok(ffi::new_context(devices)?.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)
Ok(kernel_bundle::ffi::create_kernel_bundle_from_source(&self.0, source)?.into())
}
}
23 changes: 12 additions & 11 deletions sycl/sycl-rs/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@ impl From<cxx::UniquePtr<ffi::Device>> for Device {
impl Device {
/// Returns a [`Vec`] containing all the root devices from all SYCL backends
/// available in the system which have the device type encapsulated by [`DeviceType`](crate::info::DeviceType).
pub fn get_devices() -> Vec<Self> {
ffi::get_devices()
pub fn get_devices() -> Result<Vec<Self>> {
Ok(ffi::get_devices()?
.into_iter()
.map(|device| Self(device.ptr))
.collect()
.collect())
}

/// Returns the associated SYCL platform.
pub fn get_platform(&self) -> Platform {
let raw_platform = ffi::get_platform(&self.0);
Platform(raw_platform)
pub fn get_platform(&self) -> Result<Platform> {
Ok(Platform(ffi::get_platform(&self.0)?))
}

/// Returns whether this device has the requested aspect.
Expand All @@ -56,8 +55,8 @@ impl Device {
/// [`PeerAccess::AtomicsSupported`]: When this query returns true, it indicates that this
/// device may concurrently access and atomically modify USM device memory allocations located
/// on the peer device when peer access is enabled to that device.
pub fn can_access_peer(&mut self, peer: &Device, value: PeerAccess) -> bool {
ffi::can_access_peer(&mut self.0, &peer.0, value)
pub fn can_access_peer(&mut self, peer: &Device, value: PeerAccess) -> Result<bool> {
ffi::can_access_peer(&mut self.0, &peer.0, value).map_err(Into::into)
}

/// Enables this device to access USM device allocations located on the peer device.
Expand All @@ -67,17 +66,19 @@ impl Device {
/// access USM device allocations on the peer device subject to the normal rules about context
/// as described in the core SYCL specification.
pub fn enable_peer_access(&mut self, peer: &Device) -> Result<()> {
ffi::enable_peer_access(&mut self.0, &peer.0)
ffi::enable_peer_access(&mut self.0, &peer.0).map_err(Into::into)
}

/// Disables access to the peer device’s memory from this device.
pub fn disable_peer_access(&mut self, peer: &Device) -> Result<()> {
ffi::disable_peer_access(&mut self.0, &peer.0)
ffi::disable_peer_access(&mut self.0, &peer.0).map_err(Into::into)
}
}

impl Clone for Device {
fn clone(&self) -> Self {
ffi::clone(&self.0).into()
ffi::clone(&self.0)
.expect("failed to clone SYCL device")
.into()
}
}
10 changes: 6 additions & 4 deletions sycl/sycl-rs/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Event {
///
/// Dropping the event does not wait for its completion.
pub fn wait(&mut self) -> Result<()> {
ffi::wait(&mut self.0)
ffi::wait(&mut self.0).map_err(Into::into)
}
}

Expand All @@ -41,7 +41,9 @@ impl From<cxx::UniquePtr<ffi::Event>> for Event {

impl Clone for Event {
fn clone(&self) -> Self {
ffi::clone(&self.0).into()
ffi::clone(&self.0)
.expect("failed to clone SYCL event")
.into()
}
}

Expand All @@ -62,7 +64,7 @@ impl Future for EventFuture {
// Set the callback on first Future poll (Futures can't be active until polled)
if !*this.set_callback {
*this.set_callback = true;
let mut queue = Queue::new_immediate();
let mut queue = Queue::new_immediate()?;
this.shared.waker.register(cx.waker());

// Safety: registered callback will decrement the Arc strong reference count, which is
Expand All @@ -74,7 +76,7 @@ impl Future for EventFuture {
this.queue.replace(queue);
}
Err(_) => {
return Poll::Ready(result);
return Poll::Ready(result.map_err(Into::into));
}
}
} else {
Expand Down
5 changes: 3 additions & 2 deletions sycl/sycl-rs/src/info/event-info.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 crate::event::Event;
use crate::info::Info;
use crate::private::Sealed;
Expand All @@ -15,9 +16,9 @@ use sycl_rs_sys::event::ffi;
pub struct CommandExecutionStatus;
impl Sealed for CommandExecutionStatus {}
impl Info for CommandExecutionStatus {
type Item = crate::info::EventCommandStatus;
type Item = Result<crate::info::EventCommandStatus>;
type Target = Event;
fn get_item(target: &Self::Target) -> Self::Item {
ffi::get_command_execution_status(&target.0)
ffi::get_command_execution_status(&target.0).map_err(Into::into)
}
}
4 changes: 2 additions & 2 deletions sycl/sycl-rs/src/kernel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl From<cxx::UniquePtr<types::ffi::SourceKernelBundle>> for SourceKernelBundle

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

Expand All @@ -36,7 +36,7 @@ impl From<cxx::UniquePtr<types::ffi::ExecutableKernelBundle>> for ExecutableKern

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

Expand Down
14 changes: 11 additions & 3 deletions sycl/sycl-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@
//! #
//! fn main() -> sycl_rs::Result<()> {
//! // 1. Create a Queue. It's the main entry point to the SYCL API.
//! let mut queue = Queue::new();
//! let mut queue = Queue::new()?;
//! let mut device_array = queue.alloc_device::<f32>(1024)?.wait()?;
//!
//! // 3. Build a SYCL kernel.
//! let kernel = queue
//! .get_context()
//! .get_context()?
//! .create_kernel_bundle_from_source(IOTA_SRC)?
//! .build()?
//! .get_kernel("iota")?;
Expand Down Expand Up @@ -131,7 +131,15 @@ pub mod range;
pub mod usm;
pub mod usmbox;

pub type SyclError = cxx::Exception;
/// Errors reported by SYCL operations and USM allocation.
#[derive(Debug, thiserror::Error)]
pub enum SyclError {
#[error(transparent)]
Runtime(#[from] cxx::Exception),
#[error("invalid USM allocation layout")]
InvalidLayout(#[from] std::alloc::LayoutError),
}

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

mod private {
Expand Down
14 changes: 7 additions & 7 deletions sycl/sycl-rs/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use sycl_rs_sys::platform::ffi;

use crate::{device::Device, info::InfoTarget, private::Sealed};
use crate::{Result, device::Device, info::InfoTarget, private::Sealed};

/// Abstraction for SYCL platform.
///
Expand All @@ -23,18 +23,18 @@ impl InfoTarget for Platform {}

impl Platform {
/// Returns a [`Vec`] containing all SYCL platforms from all SYCL backends available in the system.
pub fn get_platforms() -> Vec<Self> {
ffi::get_platforms()
pub fn get_platforms() -> Result<Vec<Self>> {
Ok(ffi::get_platforms()?
.into_iter()
.map(|platform| Self(platform.ptr))
.collect()
.collect())
}

/// Returns a [`Vec`] containing all the root devices associated with this `Platform`.
pub fn get_devices(&self) -> Vec<Device> {
ffi::get_devices(&self.0)
pub fn get_devices(&self) -> Result<Vec<Device>> {
Ok(ffi::get_devices(&self.0)?
.into_iter()
.map(|device| Device(device.ptr))
.collect()
.collect())
}
}
Loading
Loading