Skip to content

Commit 1581663

Browse files
Merge pull request #37 from szymon-zadworny/usmbox
Rename Buffer to UsmBox
2 parents 49ea901 + d52eaeb commit 1581663

9 files changed

Lines changed: 120 additions & 119 deletions

File tree

sycl/sycl-rs/examples/kernel_launch.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ void iota(float start, float *ptr) {
2424
#[tokio::main]
2525
async fn main() -> sycl_rs::Result<()> {
2626
let mut queue = Queue::new();
27-
let mut device_buffer = queue.alloc_device::<f32>(1024)?.await?;
27+
let mut device_array = queue.alloc_device::<f32>(1024)?.await?;
2828

2929
let kernel = queue
3030
.get_context()
@@ -36,16 +36,16 @@ async fn main() -> sycl_rs::Result<()> {
3636
queue.launch(
3737
NdRange::new([1024], [16]),
3838
&kernel,
39-
(3.14_f32, &mut device_buffer),
39+
(3.14_f32, &mut device_array),
4040
)
4141
}?
4242
.await?;
4343

44-
let mut host_buffer = queue.alloc_host::<f32>(1024)?.await?;
44+
let mut host_array = queue.alloc_host::<f32>(1024)?.await?;
4545

46-
queue.copy(&device_buffer, &mut host_buffer)?.await?;
46+
queue.copy(&device_array, &mut host_array)?.await?;
4747

48-
for e in host_buffer.iter() {
48+
for e in host_array.iter() {
4949
print!("{e} ");
5050
}
5151
println!();

sycl/sycl-rs/examples/kernel_launch_derive.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ void iota(float start, float *ptr) {
2424
#[derive(KernelArgumentList)]
2525
struct IotaArgs<'a> {
2626
start: f32,
27-
ptr: &'a mut SharedBuffer<f32>,
27+
ptr: &'a mut SharedUsmBox<f32>,
2828
}
2929

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

3434
let kernel = queue
3535
.get_context()
@@ -43,13 +43,13 @@ fn main() -> sycl_rs::Result<()> {
4343
&kernel,
4444
IotaArgs {
4545
start: 3.14_f32,
46-
ptr: &mut buffer,
46+
ptr: &mut array,
4747
},
4848
)
4949
}?
5050
.wait()?;
5151

52-
for e in buffer.iter() {
52+
for e in array.iter() {
5353
print!("{e} ");
5454
}
5555
println!();

sycl/sycl-rs/src/lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
//! fn main() -> sycl_rs::Result<()> {
5656
//! // 1. Create a Queue. It's the main entry point to the SYCL API.
5757
//! let mut queue = Queue::new();
58-
//! let mut device_buffer = queue.alloc_device::<f32>(1024)?.wait()?;
58+
//! let mut device_array = queue.alloc_device::<f32>(1024)?.wait()?;
5959
//!
6060
//! // 3. Build a SYCL kernel.
6161
//! let kernel = queue
@@ -69,18 +69,18 @@
6969
//! queue.launch(
7070
//! NdRange::new([1024], [16]),
7171
//! &kernel,
72-
//! (3.14_f32, &mut device_buffer),
72+
//! (3.14_f32, &mut device_array),
7373
//! )
7474
//! }?
7575
//! .wait()?;
7676
//!
77-
//! let mut host_buffer = queue.alloc_host::<f32>(1024)?.wait()?;
77+
//! let mut host_array = queue.alloc_host::<f32>(1024)?.wait()?;
7878
//!
7979
//! // 5. Copy your data to the host.
80-
//! queue.copy(&device_buffer, &mut host_buffer)?.wait()?;
80+
//! queue.copy(&device_array, &mut host_array)?.wait()?;
8181
//!
8282
//! // You can access your host data just like a normal Rust slice.
83-
//! for e in host_buffer.iter() {
83+
//! for e in host_array.iter() {
8484
//! print!("{e} ");
8585
//! }
8686
//! println!();
@@ -90,10 +90,11 @@
9090
//! ```
9191
//!
9292
//! # Safety model
93-
//! - USM allocations are represented by a zero-cost `Buffer` type managed through RAII.
94-
//! - Note: Unlike SYCL buffers, SYCL-rs buffers do not rely on accessors.
95-
//! - Buffers are zero-initialized by default.
96-
//! - Buffers can only store types that implement [`bytemuck::Pod`].
93+
//! - USM allocations are represented by a zero-cost [`UsmBox`](crate::usmbox::UsmBox) type managed
94+
//! through RAII.
95+
//! - Note: `UsmBox` arrays do not rely on accessors, unlike SYCL buffers.
96+
//! - `UsmBox`es are zero-initialized by default.
97+
//! - `UsmBox`es can only store types that implement [`bytemuck::Pod`].
9798
//! - Kernel launch is inherently unsafe. In particular, the caller must ensure that every argument
9899
//! has the correct representation, layout, and alignment.
99100
//!
@@ -106,8 +107,8 @@
106107
//! event returned by [`Queue::barrier()`](crate::queue::Queue::barrier).
107108
//!
108109
//! All basic SYCL wrapper types (`Queue`, `Event`, `Context`, `Platform`, `Device`) are thread safe as
109-
//! indicated by the provided [`Send`] and [`Sync`] trait implementations. However - Buffers are
110-
//! not thread-safe. If you need a thread-safe Buffer you need to wrap it in an `Arc<Mutex<T>>`.
110+
//! indicated by the provided [`Send`] and [`Sync`] trait implementations. However - `UsmBox`es are
111+
//! not thread-safe. If you need a thread-safe `UsmBox` you need to wrap it in an `Arc<Mutex<T>>`.
111112
//!
112113
//! # Required extensions
113114
//! This project requires the following SYCL extensions to work:
@@ -118,7 +119,6 @@
118119
//! - [sycl_ext_intel_queue_immediate_command_list](https://github.com/intel/llvm/blob/sycl/sycl/doc/extensions/supported/sycl_ext_intel_queue_immediate_command_list.asciidoc)
119120
//! - [sycl_ext_oneapi_enqueue_barrier](https://github.com/intel/llvm/blob/sycl/sycl/doc/extensions/supported/sycl_ext_oneapi_enqueue_barrier.asciidoc)
120121
121-
pub mod buffer;
122122
pub mod context;
123123
pub mod device;
124124
pub mod event;
@@ -129,6 +129,7 @@ pub mod prelude;
129129
pub mod queue;
130130
pub mod range;
131131
pub mod usm;
132+
pub mod usmbox;
132133

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

sycl/sycl-rs/src/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
pub use crate::{
2-
buffer::{Buffer, DeviceBuffer, HostBuffer, SharedBuffer},
32
context::Context,
43
device::Device,
54
info::{self, InfoTarget},
65
kernel::{Kernel, KernelArgument, KernelArgumentList},
76
platform::Platform,
87
queue::Queue,
98
range::{NdRange, Range},
9+
usmbox::{DeviceUsmBox, HostUsmBox, SharedUsmBox, UsmBox},
1010
};

sycl/sycl-rs/src/queue.rs

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ use bytemuck::Pod;
1111
use sycl_rs_sys::{queue::ffi, types::ffi::EventPtr};
1212

1313
use crate::{
14-
buffer::{
15-
Buffer, DeviceBuffer, EnqueuedBuffer, EnqueuedDeviceBuffer, EnqueuedHostBuffer,
16-
EnqueuedSharedBuffer, HostBuffer, SharedBuffer,
17-
},
1814
context::Context,
1915
device::Device,
2016
event::Event,
2117
kernel::{Kernel, KernelArgumentList},
2218
range::{NdRange, ValidDimension},
2319
usm::{UsmAlloc, UsmAllocator},
20+
usmbox::{
21+
DeviceUsmBox, EnqueuedDeviceUsmBox, EnqueuedHostUsmBox, EnqueuedSharedUsmBox,
22+
EnqueuedUsmBox, HostUsmBox, SharedUsmBox, UsmBox,
23+
},
2424
};
2525

2626
/// The `Queue` connects a host program to a single device. Programs submit tasks to a device via the
@@ -44,74 +44,74 @@ impl Queue {
4444
ffi::get_context(&self.0).into()
4545
}
4646

47-
/// Allocates zeroed memory and creates a host-side [`Buffer`] that can store an array of T.
48-
pub fn alloc_host<T: Pod>(&mut self, len: usize) -> Result<EnqueuedHostBuffer<T>> {
47+
/// Allocates zeroed memory and creates a host-side [`UsmBox`] that can store an array of T.
48+
pub fn alloc_host<T: Pod>(&mut self, len: usize) -> Result<EnqueuedHostUsmBox<T>> {
4949
unsafe {
50-
let mut buffer = self.alloc_uninit_host(len);
51-
self.memset(&mut buffer, 0)
52-
.map(|event| EnqueuedBuffer::new(buffer, event))
50+
let mut array = self.alloc_uninit_host(len);
51+
self.memset(&mut array, 0)
52+
.map(|event| EnqueuedUsmBox::new(array, event))
5353
}
5454
}
5555

56-
/// Allocates zeroed memory and creates a shared [`Buffer`] that can store an array of T.
57-
pub fn alloc_shared<T: Pod>(&mut self, len: usize) -> Result<EnqueuedSharedBuffer<T>> {
56+
/// Allocates zeroed memory and creates a shared [`UsmBox`] that can store an array of T.
57+
pub fn alloc_shared<T: Pod>(&mut self, len: usize) -> Result<EnqueuedSharedUsmBox<T>> {
5858
unsafe {
59-
let mut buffer = self.alloc_uninit_shared(len);
60-
self.memset(&mut buffer, 0)
61-
.map(|event| EnqueuedBuffer::new(buffer, event))
59+
let mut array = self.alloc_uninit_shared(len);
60+
self.memset(&mut array, 0)
61+
.map(|event| EnqueuedUsmBox::new(array, event))
6262
}
6363
}
6464

65-
/// Allocates zeroed memory and creates a device [`Buffer`] that can store an array of T.
66-
pub fn alloc_device<T: Pod>(&mut self, len: usize) -> Result<EnqueuedDeviceBuffer<T>> {
65+
/// Allocates zeroed memory and creates a device [`UsmBox`] that can store an array of T.
66+
pub fn alloc_device<T: Pod>(&mut self, len: usize) -> Result<EnqueuedDeviceUsmBox<T>> {
6767
unsafe {
68-
let mut buffer = self.alloc_uninit_device(len);
69-
self.memset(&mut buffer, 0)
70-
.map(|event| EnqueuedBuffer::new(buffer, event))
68+
let mut array = self.alloc_uninit_device(len);
69+
self.memset(&mut array, 0)
70+
.map(|event| EnqueuedUsmBox::new(array, event))
7171
}
7272
}
7373

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

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

88-
/// Allocates memory and creates a device-side [`Buffer`] that can store an array of T.
89-
/// Safety: the buffer contents are uninitialized.
90-
pub unsafe fn alloc_uninit_device<T>(&self, len: usize) -> DeviceBuffer<T> {
88+
/// Allocates memory and creates a device-side [`UsmBox`] that can store an array of T.
89+
/// Safety: the array contents are uninitialized.
90+
pub unsafe fn alloc_uninit_device<T>(&self, len: usize) -> DeviceUsmBox<T> {
9191
let allocator = UsmAllocator::from(self);
92-
unsafe { Buffer::new(allocator, len) }
92+
unsafe { UsmBox::new(allocator, len) }
9393
}
9494

9595
/// Sets memory allocated with USM allocations.
9696
/// Safety: the caller must make sure the underlying memory isn't being aliased somewhere else.
9797
pub unsafe fn memset<T, A: UsmAlloc>(
9898
&mut self,
99-
buffer: &mut Buffer<T, A>,
99+
array: &mut UsmBox<T, A>,
100100
value: i32,
101101
) -> Result<Event> {
102-
unsafe { self.memset_with_deps(buffer, value, &[]) }
102+
unsafe { self.memset_with_deps(array, value, &[]) }
103103
}
104104

105105
/// Sets memory allocated with USM allocations after all specified events finish.
106106
/// Safety: the caller must make sure the underlying memory isn't being aliased somewhere else.
107107
pub unsafe fn memset_with_deps<T, A: UsmAlloc>(
108108
&mut self,
109-
buffer: &mut Buffer<T, A>,
109+
array: &mut UsmBox<T, A>,
110110
value: i32,
111111
dep_events: &[&Event],
112112
) -> Result<Event> {
113-
let ptr = buffer.get_byte_ptr();
114-
let num_bytes = buffer.get_byte_size();
113+
let ptr = array.get_byte_ptr();
114+
let num_bytes = array.get_byte_size();
115115
let dep_events = dep_events
116116
.iter()
117117
.map(|e| EventPtr {
@@ -162,10 +162,10 @@ impl Queue {
162162
unsafe { nd_range.launch(self, kernel, args) }
163163
}
164164

165-
/// Copies the contents of the source buffer to the destination buffer.
165+
/// Copies the contents of the source array to the destination array.
166166
///
167-
/// Panics if the source and destination buffer lengths differ.
168-
pub fn copy<T, A1, A2>(&mut self, src: &Buffer<T, A1>, dst: &mut Buffer<T, A2>) -> Result<Event>
167+
/// Panics if the source and destination array lengths differ.
168+
pub fn copy<T, A1, A2>(&mut self, src: &UsmBox<T, A1>, dst: &mut UsmBox<T, A2>) -> Result<Event>
169169
where
170170
T: Pod,
171171
A1: UsmAlloc,
@@ -174,14 +174,14 @@ impl Queue {
174174
self.copy_with_deps(src, dst, &[])
175175
}
176176

177-
/// Copies the contents of the source buffer to the destination buffer after all specified
177+
/// Copies the contents of the source array to the destination array after all specified
178178
/// events finish.
179179
///
180-
/// Panics if the source and destination buffer lengths differ.
180+
/// Panics if the source and destination array lengths differ.
181181
pub fn copy_with_deps<T, A1, A2>(
182182
&mut self,
183-
src: &Buffer<T, A1>,
184-
dst: &mut Buffer<T, A2>,
183+
src: &UsmBox<T, A1>,
184+
dst: &mut UsmBox<T, A2>,
185185
dep_events: &[&Event],
186186
) -> Result<Event>
187187
where
@@ -192,7 +192,7 @@ impl Queue {
192192
assert_eq!(
193193
src.get_len(),
194194
dst.get_len(),
195-
"source and destination buffer lengths differ"
195+
"source and destination array lengths differ"
196196
);
197197

198198
// TODO: Resolve the C++ lifetime elision issue

sycl/sycl-rs/src/usm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ unsafe impl<T: UsmAllocatorKind> Allocator for UsmAllocator<T> {
6868
}
6969
}
7070

71-
/// An allocator for Device-side buffers
71+
/// An allocator for Device-side arrays
7272
///
7373
/// Safety: memory allocated by this allocator cannot be accessed on the host side
7474
#[allow(dead_code)]
@@ -80,7 +80,7 @@ impl UsmAllocatorKind for DeviceAllocator {
8080
}
8181
}
8282

83-
/// An allocator for Host-side buffers
83+
/// An allocator for Host-side arrays
8484
pub struct HostAllocator;
8585

8686
impl UsmAllocatorKind for HostAllocator {
@@ -91,7 +91,7 @@ impl UsmAllocatorKind for HostAllocator {
9191

9292
unsafe impl HostAccessible for UsmAllocator<HostAllocator> {}
9393

94-
/// An allocator for shared memory buffers
94+
/// An allocator for shared memory arrays
9595
pub struct SharedAllocator;
9696

9797
impl UsmAllocatorKind for SharedAllocator {

0 commit comments

Comments
 (0)