diff --git a/oneapi-rs-sys/build.rs b/oneapi-rs-sys/build.rs index c57aa82..853dffc 100644 --- a/oneapi-rs-sys/build.rs +++ b/oneapi-rs-sys/build.rs @@ -68,7 +68,7 @@ fn get_compiler_path() -> Result { if let Ok(path) = std::env::var("CMPLR_ROOT") { let path = PathBuf::from(path).join("bin/icpx"); if path.exists() { - return Ok(path) + return Ok(path); } } if let Ok(path) = which("icpx") { @@ -81,5 +81,8 @@ fn get_compiler_path() -> Result { return Ok(path); } - Err(Error::new(std::io::ErrorKind::NotFound, "No DPC++ compiler found")) + Err(Error::new( + std::io::ErrorKind::NotFound, + "No DPC++ compiler found", + )) } diff --git a/oneapi-rs-sys/include/device.hpp b/oneapi-rs-sys/include/device.hpp index 9dfaa4c..0cbbf1a 100644 --- a/oneapi-rs-sys/include/device.hpp +++ b/oneapi-rs-sys/include/device.hpp @@ -8,12 +8,11 @@ #pragma once +#include + #include "oneapi-rs-sys/include/types.hpp" #include "rust/cxx.h" -#include -#include - namespace sycl_shims { struct DevicePtr; struct PlatformPtr; diff --git a/oneapi-rs-sys/include/event.hpp b/oneapi-rs-sys/include/event.hpp index f90710b..0250353 100644 --- a/oneapi-rs-sys/include/event.hpp +++ b/oneapi-rs-sys/include/event.hpp @@ -8,19 +8,20 @@ #pragma once +#include + #include "oneapi-rs-sys/include/types.hpp" #include "oneapi-rs-sys/src/event-sys.rs.h" #include "rust/cxx.h" -#include - namespace sycl_shims { enum class EventCommandStatus : std::uint8_t; } // namespace sycl_shims namespace sycl_shims::event { void wait(std::unique_ptr &); -void register_callback(std::unique_ptr &, Event const &, SharedWaker const *); +void register_callback(std::unique_ptr &, Event const &, + SharedWaker const *); EventCommandStatus get_command_execution_status(Event const &); std::unique_ptr clone(Event const &); } // namespace sycl_shims::event diff --git a/oneapi-rs-sys/include/platform.hpp b/oneapi-rs-sys/include/platform.hpp index 9dbb3f7..f727662 100644 --- a/oneapi-rs-sys/include/platform.hpp +++ b/oneapi-rs-sys/include/platform.hpp @@ -8,13 +8,12 @@ #pragma once -#include "rust/cxx.h" -#include "oneapi-rs-sys/include/types.hpp" +#include #include -#include -#include +#include "oneapi-rs-sys/include/types.hpp" +#include "rust/cxx.h" namespace sycl_shims { struct DevicePtr; @@ -23,8 +22,8 @@ struct PlatformPtr; namespace sycl_shims::platform { rust::Vec get_platforms(); -rust::Vec get_devices(Platform const&); -rust::String get_version(Platform const&); -rust::String get_name(Platform const&); -rust::String get_vendor(Platform const&); +rust::Vec get_devices(Platform const &); +rust::String get_version(Platform const &); +rust::String get_name(Platform const &); +rust::String get_vendor(Platform const &); } // namespace sycl_shims::platform diff --git a/oneapi-rs-sys/include/queue.hpp b/oneapi-rs-sys/include/queue.hpp index 344102e..c8fa997 100644 --- a/oneapi-rs-sys/include/queue.hpp +++ b/oneapi-rs-sys/include/queue.hpp @@ -8,11 +8,11 @@ #pragma once +#include + #include "oneapi-rs-sys/include/types.hpp" #include "rust/cxx.h" -#include - namespace sycl_shims { struct EventPtr; } // namespace sycl_shims @@ -22,13 +22,9 @@ std::unique_ptr new_queue(); std::unique_ptr new_queue_immediate(); std::unique_ptr new_queue_from_device(Device const &); std::unique_ptr clone(Queue const &); -std::unique_ptr memset( - std::unique_ptr &, - std::uint8_t * ptr, - int value, - std::size_t num_bytes, - rust::Vec -); +std::unique_ptr memset(std::unique_ptr &, std::uint8_t *ptr, + int value, std::size_t num_bytes, + rust::Vec); std::unique_ptr barrier(std::unique_ptr &, rust::Vec); void wait(std::unique_ptr &); } // namespace sycl_shims::queue diff --git a/oneapi-rs-sys/include/types.hpp b/oneapi-rs-sys/include/types.hpp index 82e9bbb..db147b1 100644 --- a/oneapi-rs-sys/include/types.hpp +++ b/oneapi-rs-sys/include/types.hpp @@ -15,4 +15,4 @@ using Device = sycl::device; using Platform = sycl::platform; using Queue = sycl::queue; using Event = sycl::event; -} +} // namespace sycl_shims diff --git a/oneapi-rs-sys/include/usm.hpp b/oneapi-rs-sys/include/usm.hpp index d946121..f3b7b01 100644 --- a/oneapi-rs-sys/include/usm.hpp +++ b/oneapi-rs-sys/include/usm.hpp @@ -8,14 +8,17 @@ #pragma once +#include + #include "oneapi-rs-sys/include/types.hpp" #include "rust/cxx.h" -#include - namespace sycl_shims::usm { -std::uint8_t* aligned_alloc_device(std::size_t alignment, std::size_t num_bytes, Queue const &); -std::uint8_t* aligned_alloc_host(std::size_t alignment, std::size_t num_bytes, Queue const &); -std::uint8_t* aligned_alloc_shared(std::size_t alignment, std::size_t num_bytes, Queue const &); -void free(std::uint8_t*, Queue const &); +std::uint8_t *aligned_alloc_device(std::size_t alignment, std::size_t num_bytes, + Queue const &); +std::uint8_t *aligned_alloc_host(std::size_t alignment, std::size_t num_bytes, + Queue const &); +std::uint8_t *aligned_alloc_shared(std::size_t alignment, std::size_t num_bytes, + Queue const &); +void free(std::uint8_t *, Queue const &); } // namespace sycl_shims::usm diff --git a/oneapi-rs-sys/src/event-sys.rs b/oneapi-rs-sys/src/event-sys.rs index 533fd12..6158ac8 100644 --- a/oneapi-rs-sys/src/event-sys.rs +++ b/oneapi-rs-sys/src/event-sys.rs @@ -26,7 +26,11 @@ pub mod ffi { type Queue = crate::types::ffi::Queue; fn wait(event: &mut UniquePtr); - unsafe fn register_callback(queue: &mut UniquePtr, event: &Event, waker: *const SharedWaker); + unsafe fn register_callback( + queue: &mut UniquePtr, + event: &Event, + waker: *const SharedWaker, + ); fn get_command_execution_status(event: &Event) -> EventCommandStatus; fn clone(event: &Event) -> UniquePtr; } diff --git a/oneapi-rs-sys/src/event.cpp b/oneapi-rs-sys/src/event.cpp index 024de84..fb73831 100644 --- a/oneapi-rs-sys/src/event.cpp +++ b/oneapi-rs-sys/src/event.cpp @@ -13,27 +13,28 @@ using sycl::info::event_command_status; namespace syclintel = sycl::ext::intel; namespace sycl_shims::event { -void wait(std::unique_ptr & event) { - event->wait(); -} -std::unique_ptr clone(Event const & event) { +void wait(std::unique_ptr &event) { event->wait(); } +std::unique_ptr clone(Event const &event) { return std::make_unique(sycl::event(event)); } -EventCommandStatus get_command_execution_status(Event const & event) { + +EventCommandStatus get_command_execution_status(Event const &event) { auto status = event.get_info(); switch (status) { - case event_command_status::submitted: - return EventCommandStatus::Submitted; - case event_command_status::running: - return EventCommandStatus::Running; - case event_command_status::complete: - return EventCommandStatus::Complete; - default: - return EventCommandStatus::Unknown; + case event_command_status::submitted: + return EventCommandStatus::Submitted; + case event_command_status::running: + return EventCommandStatus::Running; + case event_command_status::complete: + return EventCommandStatus::Complete; + default: + return EventCommandStatus::Unknown; } } -void register_callback(std::unique_ptr & queue, Event const & event, SharedWaker const * waker) { - queue->submit([=](sycl::handler& cgh) { + +void register_callback(std::unique_ptr &queue, Event const &event, + SharedWaker const *waker) { + queue->submit([=](sycl::handler &cgh) { cgh.depends_on(event); cgh.host_task([=]() { waker->wake(); }); }); diff --git a/oneapi-rs-sys/src/queue-sys.rs b/oneapi-rs-sys/src/queue-sys.rs index d9f7a1f..f79c6f5 100644 --- a/oneapi-rs-sys/src/queue-sys.rs +++ b/oneapi-rs-sys/src/queue-sys.rs @@ -33,7 +33,7 @@ pub mod ffi { ptr: *mut u8, value: i32, num_bytes: usize, - dep_events: Vec + dep_events: Vec, ) -> UniquePtr; fn barrier(queue: &mut UniquePtr, dep_events: Vec) -> UniquePtr; fn wait(queue: &mut UniquePtr); diff --git a/oneapi-rs-sys/src/queue.cpp b/oneapi-rs-sys/src/queue.cpp index 60692c3..3209cd1 100644 --- a/oneapi-rs-sys/src/queue.cpp +++ b/oneapi-rs-sys/src/queue.cpp @@ -9,46 +9,43 @@ #include "oneapi-rs-sys/include/queue.hpp" #include "oneapi-rs-sys/src/queue-sys.rs.h" -using sycl::property::queue::in_order; using sycl::ext::intel::property::queue::immediate_command_list; +using sycl::property::queue::in_order; namespace sycl_shims::queue { std::unique_ptr new_queue() { - return std::make_unique(sycl::queue({ - in_order() - })); + return std::make_unique(sycl::queue({in_order()})); } + std::unique_ptr new_queue_immediate() { - return std::make_unique(sycl::queue({ - in_order(), - immediate_command_list() - })); + return std::make_unique( + sycl::queue({in_order(), immediate_command_list()})); } -std::unique_ptr new_queue_from_device(Device const & device) { + +std::unique_ptr new_queue_from_device(Device const &device) { return std::make_unique(sycl::queue(device, {in_order()})); } -std::unique_ptr clone(Queue const & queue) { + +std::unique_ptr clone(Queue const &queue) { return std::make_unique(sycl::queue(queue)); } -std::unique_ptr memset( - std::unique_ptr & queue, - std::uint8_t * ptr, - int value, - std::size_t num_bytes, - rust::Vec dep_events -) { + +std::unique_ptr memset(std::unique_ptr &queue, std::uint8_t *ptr, + int value, std::size_t num_bytes, + rust::Vec dep_events) { std::vector deps; - for (auto&& e: dep_events) + for (auto &&e : dep_events) deps.push_back(std::move(*e.ptr)); return std::make_unique(queue->memset(ptr, value, num_bytes, deps)); } -std::unique_ptr barrier(std::unique_ptr & queue, rust::Vec dep_events) { + +std::unique_ptr barrier(std::unique_ptr &queue, + rust::Vec dep_events) { std::vector deps; - for (auto&& e: dep_events) + for (auto &&e : dep_events) deps.push_back(std::move(*e.ptr)); return std::make_unique(queue->ext_oneapi_submit_barrier(deps)); } -void wait(std::unique_ptr & queue) { - queue->wait(); -} + +void wait(std::unique_ptr &queue) { queue->wait(); } } // namespace sycl_shims::queue diff --git a/oneapi-rs-sys/src/types-sys.rs b/oneapi-rs-sys/src/types-sys.rs index 27e2442..2c9a49e 100644 --- a/oneapi-rs-sys/src/types-sys.rs +++ b/oneapi-rs-sys/src/types-sys.rs @@ -6,20 +6,20 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 // -use std::sync::atomic::{Ordering::Relaxed, AtomicBool}; +use std::sync::atomic::{AtomicBool, Ordering::Relaxed}; use futures::task::AtomicWaker; pub struct SharedWaker { pub waker: AtomicWaker, - pub done: AtomicBool + pub done: AtomicBool, } impl SharedWaker { pub fn new() -> Self { Self { waker: AtomicWaker::new(), - done: AtomicBool::new(false) + done: AtomicBool::new(false), } } @@ -44,15 +44,15 @@ pub mod ffi { // https://github.com/dtolnay/cxx/issues/774#issuecomment-808674945 // We must use pointer wrapper structs instead. struct DevicePtr { - ptr: UniquePtr + ptr: UniquePtr, } struct PlatformPtr { - ptr: UniquePtr + ptr: UniquePtr, } - + struct EventPtr { - ptr: UniquePtr + ptr: UniquePtr, } #[derive(Debug)] @@ -63,7 +63,7 @@ pub mod ffi { Custom, Automatic, All, - Unimplemented + Unimplemented, } #[derive(Debug)] @@ -71,7 +71,7 @@ pub mod ffi { Submitted, Running, Complete, - Unknown + Unknown, } impl UniquePtr {} diff --git a/oneapi-rs-sys/src/usm-sys.rs b/oneapi-rs-sys/src/usm-sys.rs index 2f6cb51..2d9c6c6 100644 --- a/oneapi-rs-sys/src/usm-sys.rs +++ b/oneapi-rs-sys/src/usm-sys.rs @@ -15,9 +15,21 @@ pub mod ffi { extern "C++" { include!("oneapi-rs-sys/include/usm.hpp"); - unsafe fn aligned_alloc_device(alignment: usize, num_bytes: usize, queue: &Queue) -> Result<*mut u8>; - unsafe fn aligned_alloc_host(alignment: usize, num_bytes: usize, queue: &Queue) -> Result<*mut u8>; - unsafe fn aligned_alloc_shared(alignment: usize, num_bytes: usize, queue: &Queue) -> Result<*mut u8>; + unsafe fn aligned_alloc_device( + alignment: usize, + num_bytes: usize, + queue: &Queue, + ) -> Result<*mut u8>; + unsafe fn aligned_alloc_host( + alignment: usize, + num_bytes: usize, + queue: &Queue, + ) -> Result<*mut u8>; + unsafe fn aligned_alloc_shared( + alignment: usize, + num_bytes: usize, + queue: &Queue, + ) -> Result<*mut u8>; unsafe fn free(ptr: *mut u8, queue: &Queue); } } diff --git a/oneapi-rs-sys/src/usm.cpp b/oneapi-rs-sys/src/usm.cpp index 9004fa7..6a9cbb0 100644 --- a/oneapi-rs-sys/src/usm.cpp +++ b/oneapi-rs-sys/src/usm.cpp @@ -10,19 +10,25 @@ #include "oneapi-rs-sys/src/usm-sys.rs.h" namespace sycl_shims::usm { -std::uint8_t* aligned_alloc_device(std::size_t alignment, std::size_t num_bytes, Queue const & queue) { - return static_cast(sycl::aligned_alloc_device(alignment, num_bytes, queue)); +std::uint8_t *aligned_alloc_device(std::size_t alignment, std::size_t num_bytes, + Queue const &queue) { + return static_cast( + sycl::aligned_alloc_device(alignment, num_bytes, queue)); } -std::uint8_t* aligned_alloc_host(std::size_t alignment, std::size_t num_bytes, Queue const & queue) { - return static_cast(sycl::aligned_alloc_host(alignment, num_bytes, queue)); +std::uint8_t *aligned_alloc_host(std::size_t alignment, std::size_t num_bytes, + Queue const &queue) { + return static_cast( + sycl::aligned_alloc_host(alignment, num_bytes, queue)); } -std::uint8_t* aligned_alloc_shared(std::size_t alignment, std::size_t num_bytes, Queue const & queue) { - return static_cast(sycl::aligned_alloc_shared(alignment, num_bytes, queue)); +std::uint8_t *aligned_alloc_shared(std::size_t alignment, std::size_t num_bytes, + Queue const &queue) { + return static_cast( + sycl::aligned_alloc_shared(alignment, num_bytes, queue)); } -void free(std::uint8_t* memory, Queue const & queue) { - sycl::free(static_cast(memory), queue); +void free(std::uint8_t *memory, Queue const &queue) { + sycl::free(static_cast(memory), queue); } } // namespace sycl_shims::usm diff --git a/oneapi-rs/examples/alloc.rs b/oneapi-rs/examples/alloc.rs index 2e8476b..52924c0 100644 --- a/oneapi-rs/examples/alloc.rs +++ b/oneapi-rs/examples/alloc.rs @@ -12,7 +12,7 @@ fn main() { let mut queue = Queue::new(); let mut buffer = queue.alloc_shared::(10).wait(); - for e in buffer.iter() { + for e in buffer.iter() { print!("{e} ") } @@ -22,7 +22,7 @@ fn main() { buffer[i] = i as u32; } - for e in buffer.iter() { + for e in buffer.iter() { print!("{e} ") } diff --git a/oneapi-rs/examples/async.rs b/oneapi-rs/examples/async.rs index ad54f51..f3ff4b4 100644 --- a/oneapi-rs/examples/async.rs +++ b/oneapi-rs/examples/async.rs @@ -13,7 +13,7 @@ async fn main() { let mut queue = Queue::new(); let mut buffer = queue.alloc_shared::(10).await; - for e in buffer.iter() { + for e in buffer.iter() { print!("{e} ") } @@ -23,7 +23,7 @@ async fn main() { buffer[i] = i as u32; } - for e in buffer.iter() { + for e in buffer.iter() { print!("{e} ") } diff --git a/oneapi-rs/examples/sycl-ls.rs b/oneapi-rs/examples/sycl-ls.rs index 9870312..7473408 100644 --- a/oneapi-rs/examples/sycl-ls.rs +++ b/oneapi-rs/examples/sycl-ls.rs @@ -6,8 +6,8 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 // -use oneapi_rs::platform::Platform; use oneapi_rs::info; +use oneapi_rs::platform::Platform; fn main() { for platform in Platform::get_platforms() { @@ -18,7 +18,9 @@ fn main() { let device_version = device.get_info::(); let platform_version = platform.get_info::(); - println!("[{device_type:?}] {platform_name}, {device_name} {device_version} [{platform_version}]"); + println!( + "[{device_type:?}] {platform_name}, {device_name} {device_version} [{platform_version}]" + ); } } } diff --git a/oneapi-rs/src/buffer.rs b/oneapi-rs/src/buffer.rs index e1fa270..32d2a44 100644 --- a/oneapi-rs/src/buffer.rs +++ b/oneapi-rs/src/buffer.rs @@ -6,11 +6,21 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 // -use std::{alloc::{Layout, handle_alloc_error}, ops::{Deref, DerefMut}, pin::Pin, ptr::NonNull, slice, task::{Context, Poll}}; +use std::{ + alloc::{Layout, handle_alloc_error}, + ops::{Deref, DerefMut}, + pin::Pin, + ptr::NonNull, + slice, + task::{Context, Poll}, +}; use pin_project::pin_project; -use crate::{event::{Event, EventFuture}, usm::UsmAlloc}; +use crate::{ + event::{Event, EventFuture}, + usm::UsmAlloc, +}; /// The Buffer struct defines a shared array of one, two or three dimensions that can be used /// by the SYCL kernel. Buffers are templated on the type of their data, and the number of @@ -38,7 +48,7 @@ impl Buffer { let layout = Layout::array::(len).unwrap(); let ptr = match allocator.allocate(layout.clone()) { Ok(ptr) => ptr, - _ => handle_alloc_error(layout) + _ => handle_alloc_error(layout), }; Self { @@ -61,38 +71,33 @@ impl Buffer { impl Deref for Buffer { type Target = [T]; fn deref(&self) -> &Self::Target { - unsafe { - slice::from_raw_parts(self.data.as_ptr(), self.len) - } + unsafe { slice::from_raw_parts(self.data.as_ptr(), self.len) } } } impl DerefMut for Buffer { fn deref_mut(&mut self) -> &mut Self::Target { - unsafe { - slice::from_raw_parts_mut(self.data.as_ptr(), self.len) - } + unsafe { slice::from_raw_parts_mut(self.data.as_ptr(), self.len) } } } impl Drop for Buffer { fn drop(&mut self) { - unsafe { self.allocator.deallocate(self.data.cast(), self.layout); } + unsafe { + self.allocator.deallocate(self.data.cast(), self.layout); + } } } /// A [`Buffer`] whose initialization has been enqueued. You need to wait/await it. pub struct EnqueuedBuffer { buffer: Buffer, - event: Event + event: Event, } impl EnqueuedBuffer { pub(crate) fn new(buffer: Buffer, event: Event) -> Self { - Self { - buffer, - event - } + Self { buffer, event } } } @@ -109,14 +114,16 @@ impl EnqueuedBuffer { pub struct BufferFuture { buffer: Option>, #[pin] - event_future: EventFuture + event_future: EventFuture, } impl Future for BufferFuture { type Output = Buffer; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { let this = self.project(); - this.event_future.poll(cx).map(|_| this.buffer.take().unwrap()) + this.event_future + .poll(cx) + .map(|_| this.buffer.take().unwrap()) } } @@ -127,7 +134,7 @@ impl IntoFuture for EnqueuedBuffer { fn into_future(self) -> Self::IntoFuture { Self::IntoFuture { buffer: Some(self.buffer), - event_future: self.event.into_future() + event_future: self.event.into_future(), } } } diff --git a/oneapi-rs/src/event.rs b/oneapi-rs/src/event.rs index d74001d..0b120c1 100644 --- a/oneapi-rs/src/event.rs +++ b/oneapi-rs/src/event.rs @@ -6,7 +6,11 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 // -use std::{pin::Pin, task::{Context, Poll}, sync::atomic::Ordering::Relaxed}; +use std::{ + pin::Pin, + sync::atomic::Ordering::Relaxed, + task::{Context, Poll}, +}; use oneapi_rs_sys::{event::ffi, types::SharedWaker}; @@ -61,8 +65,7 @@ impl Future for EventFuture { // Safety: the Future which holds the SharedWaker is pinned - the pointer will remain valid. unsafe { ffi::register_callback(&mut queue.0, &this.event.0, this.shared) }; this.queue.replace(queue); - } - else { + } else { // Quick check before registering to avoid wasting time if this.shared.done.load(Relaxed) { return Poll::Ready(()); @@ -75,8 +78,7 @@ impl Future for EventFuture { // https://docs.rs/futures/latest/futures/task/struct.AtomicWaker.html#examples if this.shared.done.load(Relaxed) { Poll::Ready(()) - } - else { + } else { Poll::Pending } } diff --git a/oneapi-rs/src/lib.rs b/oneapi-rs/src/lib.rs index 1f7a5b7..922b460 100644 --- a/oneapi-rs/src/lib.rs +++ b/oneapi-rs/src/lib.rs @@ -6,10 +6,10 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 // +pub mod buffer; +pub mod device; +pub mod event; pub mod info; pub mod platform; -pub mod device; pub mod queue; pub mod usm; -pub mod buffer; -pub mod event; diff --git a/oneapi-rs/src/queue.rs b/oneapi-rs/src/queue.rs index a7bd77a..70ba4fc 100644 --- a/oneapi-rs/src/queue.rs +++ b/oneapi-rs/src/queue.rs @@ -9,7 +9,12 @@ use bytemuck::Pod; use oneapi_rs_sys::{queue::ffi, types::ffi::EventPtr}; -use crate::{buffer::{Buffer, EnqueuedBuffer}, device::Device, event::Event, usm::{HostAllocator, SharedAllocator, UsmAlloc, UsmAllocator}}; +use crate::{ + buffer::{Buffer, EnqueuedBuffer}, + device::Device, + event::Event, + usm::{HostAllocator, SharedAllocator, UsmAlloc, UsmAllocator}, +}; /// The `Queue` connects a host program to a single device. Programs submit tasks to a device via the /// `Queue` and may monitor the `Queue` for completion. A program initiates the task by submitting @@ -28,7 +33,10 @@ impl Queue { } /// Allocates zeroed memory and creates a host-side [`Buffer`] that can store an array of T. - pub fn alloc_host(&mut self, len: usize) -> EnqueuedBuffer> { + pub fn alloc_host( + &mut self, + len: usize, + ) -> EnqueuedBuffer> { unsafe { let mut buffer = self.alloc_uninit_host(len); let event = self.memset(&mut buffer, 0); @@ -37,7 +45,10 @@ impl Queue { } /// Allocates zeroed memory and creates a shared [`Buffer`] that can store an array of T. - pub fn alloc_shared(&mut self, len: usize) -> EnqueuedBuffer> { + pub fn alloc_shared( + &mut self, + len: usize, + ) -> EnqueuedBuffer> { unsafe { let mut buffer = self.alloc_uninit_shared(len); let event = self.memset(&mut buffer, 0); @@ -47,21 +58,31 @@ 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(&self, len: usize) -> Buffer> { + pub unsafe fn alloc_uninit_host( + &self, + len: usize, + ) -> Buffer> { 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(&self, len: usize) -> Buffer> { + pub unsafe fn alloc_uninit_shared( + &self, + len: usize, + ) -> Buffer> { let allocator = UsmAllocator::from(self); unsafe { Buffer::new(allocator, len) } } /// Sets memory allocated with USM allocations. /// Safety: the caller must make sure the underlying memory isn't being aliased somewhere else. - pub unsafe fn memset(&mut self, buffer: &mut Buffer, value: i32) -> Event { + pub unsafe fn memset( + &mut self, + buffer: &mut Buffer, + value: i32, + ) -> Event { unsafe { self.memset_with_deps(buffer, value, &[]) } } @@ -71,13 +92,15 @@ impl Queue { &mut self, buffer: &mut Buffer, value: i32, - dep_events: &[&Event] + dep_events: &[&Event], ) -> Event { let ptr = buffer.get_byte_ptr(); let num_bytes = buffer.get_byte_size(); let dep_events = dep_events .iter() - .map(|e| EventPtr { ptr: (*e).clone().0 }) + .map(|e| EventPtr { + ptr: (*e).clone().0, + }) .collect::>(); unsafe { ffi::memset(&mut self.0, ptr, value, num_bytes, dep_events) }.into() } @@ -91,7 +114,9 @@ impl Queue { pub fn barrier_with_deps(&mut self, dep_events: &[&Event]) -> Event { let dep_events = dep_events .iter() - .map(|e| EventPtr { ptr: (*e).clone().0 }) + .map(|e| EventPtr { + ptr: (*e).clone().0, + }) .collect::>(); ffi::barrier(&mut self.0, dep_events).into() } diff --git a/oneapi-rs/src/usm.rs b/oneapi-rs/src/usm.rs index 39b41fd..4e27c5e 100644 --- a/oneapi-rs/src/usm.rs +++ b/oneapi-rs/src/usm.rs @@ -8,8 +8,8 @@ use crate::queue::Queue; +use allocator_api2::alloc::{AllocError, Allocator}; use oneapi_rs_sys::usm::ffi; -use allocator_api2::alloc::{Allocator, AllocError}; use std::{alloc::Layout, marker::PhantomData, ptr::NonNull}; @@ -18,11 +18,11 @@ type CxxResult = cxx::core::result::Result; /// An instance of a USM allocator. pub struct UsmAllocator { queue: Queue, - _kind: PhantomData + _kind: PhantomData, } /// A marker trait for USM allocators. -pub unsafe trait UsmAlloc : Allocator {} +pub unsafe trait UsmAlloc: Allocator {} unsafe impl UsmAlloc for UsmAllocator {} @@ -34,7 +34,7 @@ impl From<&Queue> for UsmAllocator { fn from(queue: &Queue) -> Self { Self { queue: queue.clone(), - _kind: PhantomData + _kind: PhantomData, } } } @@ -51,7 +51,9 @@ unsafe impl Allocator for UsmAllocator { } unsafe fn deallocate(&self, ptr: NonNull, _layout: Layout) { - unsafe { ffi::free(ptr.as_ptr(), &self.queue.0); } + unsafe { + ffi::free(ptr.as_ptr(), &self.queue.0); + } } }