-
Notifications
You must be signed in to change notification settings - Fork 2
Add USM allocations #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
b8892db
Add C++ sycl::queue bindings
szymon-zadworny a8ddc52
Add Rust Queue abstraction
szymon-zadworny bc7225e
Add C++ USM module
szymon-zadworny dff66f1
Add C++ sycl::free binding
szymon-zadworny 561b657
Add Allocator trait
szymon-zadworny de0bc58
Add safe USM allocator abstraction
szymon-zadworny c16a455
Add support for memory alignment
szymon-zadworny 1576121
Add C++ aligned_alloc_host binding
szymon-zadworny 68d217d
Add C++ aligned_alloc_shared binding
szymon-zadworny 0ee4ce7
Add generic UsmAllocator trait
szymon-zadworny 1042197
Hide DeviceAllocator
szymon-zadworny ec64da1
Add a host allocator
szymon-zadworny dd96083
Add a shared allocator
szymon-zadworny 5c9c7c1
Use allocator_api2
szymon-zadworny 2682dfa
Add a Buffer abstraction
szymon-zadworny 02c5d3a
Add alloc shared support
szymon-zadworny 6df58cc
Add alloc example
szymon-zadworny 3d2a2d8
Add missing header to source list
szymon-zadworny 53955dc
Add alloc host support
szymon-zadworny 10dc6a5
Determine buffer size at runtime
szymon-zadworny 8f106af
Mark uninitialized allocations as unsafe
szymon-zadworny c45267a
Add Buffer documentation
szymon-zadworny 2e0b61f
Add USM documentation
szymon-zadworny 409e687
Add Queue::alloc_uninit_* documentation
szymon-zadworny f8f78c1
Mark UsmAlloc as an unsafe trait
szymon-zadworny a1ae562
Fix doc formatting
szymon-zadworny 693bfaf
Style fixes
szymon-zadworny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // | ||
| // Copyright (C) 2026 Intel Corporation | ||
| // | ||
| // Under the MIT License or the Apache License v2.0. | ||
| // See LICENSE-MIT and LICENSE-APACHE for license information. | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "oneapi-rs-sys/include/types.hpp" | ||
| #include "rust/cxx.h" | ||
|
|
||
| #include <memory> | ||
|
|
||
| namespace sycl_shims::queue { | ||
| std::unique_ptr<Queue> new_queue(); | ||
| std::unique_ptr<Queue> new_queue_from_device(Device const &); | ||
| } // namespace sycl_shims::queue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // | ||
| // Copyright (C) 2026 Intel Corporation | ||
| // | ||
| // Under the MIT License or the Apache License v2.0. | ||
| // See LICENSE-MIT and LICENSE-APACHE for license information. | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
| // | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "oneapi-rs-sys/include/types.hpp" | ||
| #include "rust/cxx.h" | ||
|
|
||
| #include <memory> | ||
|
|
||
| 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 &); | ||
| } // namespace sycl_shims::usm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // | ||
| // Copyright (C) 2026 Intel Corporation | ||
| // | ||
| // Under the MIT License or the Apache License v2.0. | ||
| // See LICENSE-MIT and LICENSE-APACHE for license information. | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
| // | ||
|
|
||
| #[cxx::bridge(namespace = "sycl_shims::queue")] | ||
| pub mod ffi { | ||
| unsafe extern "C++" { | ||
| include!("oneapi-rs-sys/include/queue.hpp"); | ||
|
|
||
| #[namespace = "sycl_shims"] | ||
| type Queue = crate::types::ffi::Queue; | ||
| #[namespace = "sycl_shims"] | ||
| type Device = crate::types::ffi::Device; | ||
|
|
||
| fn new_queue() -> UniquePtr<Queue>; | ||
| fn new_queue_from_device(device: &Device) -> UniquePtr<Queue>; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // | ||
| // Copyright (C) 2026 Intel Corporation | ||
| // | ||
| // Under the MIT License or the Apache License v2.0. | ||
| // See LICENSE-MIT and LICENSE-APACHE for license information. | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
| // | ||
|
|
||
| #include "oneapi-rs-sys/include/queue.hpp" | ||
| #include "oneapi-rs-sys/src/queue-sys.rs.h" | ||
|
|
||
| namespace sycl_shims::queue { | ||
| std::unique_ptr<Queue> new_queue() { | ||
| return std::make_unique<Queue>(sycl::queue()); | ||
| } | ||
| std::unique_ptr<Queue> new_queue_from_device(Device const & device) { | ||
| return std::make_unique<Queue>(sycl::queue(device)); | ||
| } | ||
| } // namespace sycl_shims::queue |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // | ||
| // Copyright (C) 2026 Intel Corporation | ||
| // | ||
| // Under the MIT License or the Apache License v2.0. | ||
| // See LICENSE-MIT and LICENSE-APACHE for license information. | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
| // | ||
|
|
||
| #[cxx::bridge(namespace = "sycl_shims::usm")] | ||
| pub mod ffi { | ||
| unsafe extern "C++" { | ||
| #[namespace = "sycl_shims"] | ||
| type Queue = crate::types::ffi::Queue; | ||
| } | ||
|
|
||
| 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 free(ptr: *mut u8, queue: &Queue); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // | ||
| // Copyright (C) 2026 Intel Corporation | ||
| // | ||
| // Under the MIT License or the Apache License v2.0. | ||
| // See LICENSE-MIT and LICENSE-APACHE for license information. | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
| // | ||
|
|
||
| #include "oneapi-rs-sys/include/usm.hpp" | ||
| #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<std::uint8_t*>(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<std::uint8_t*>(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<std::uint8_t*>(sycl::aligned_alloc_shared(alignment, num_bytes, queue)); | ||
| } | ||
|
|
||
| void free(std::uint8_t* memory, Queue const & queue) { | ||
| sycl::free(static_cast<void*>(memory), queue); | ||
| } | ||
| } // namespace sycl_shims::usm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // | ||
| // Copyright (C) 2026 Intel Corporation | ||
| // | ||
| // Under the MIT License or the Apache License v2.0. | ||
| // See LICENSE-MIT and LICENSE-APACHE for license information. | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
| // | ||
|
|
||
| use oneapi_rs::queue::Queue; | ||
|
|
||
| fn main() { | ||
| let queue = Queue::new(); | ||
| let mut buffer = unsafe { queue.alloc_uninit_shared::<u32>(10) }; | ||
|
|
||
| for i in 0..buffer.len() { | ||
| buffer[i] = i as u32; | ||
| } | ||
|
|
||
| for e in buffer.iter() { | ||
| print!("{e} ") | ||
| } | ||
|
|
||
| println!(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // | ||
| // Copyright (C) 2026 Intel Corporation | ||
| // | ||
| // Under the MIT License or the Apache License v2.0. | ||
| // See LICENSE-MIT and LICENSE-APACHE for license information. | ||
| // SPDX-License-Identifier: MIT OR Apache-2.0 | ||
| // | ||
|
|
||
| use std::{alloc::{Layout, handle_alloc_error}, ops::{Deref, DerefMut}, ptr::NonNull, slice}; | ||
|
|
||
| use crate::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 | ||
| /// dimensions that the data is stored and accessed through. | ||
| /// | ||
| /// A Buffer does not map to only one underlying backend object, and all SYCL backend memory objects | ||
| /// may be temporary for use on a specific device. | ||
| /// | ||
| /// Buffers can be constructed by methods provided by the [`Queue`](`crate::queue::Queue`) class. | ||
| /// | ||
| /// The Buffer struct template takes a template parameter [`UsmAlloc`](`crate::usm::UsmAlloc`) for | ||
| /// specifying an allocator which is used by the SYCL runtime when allocating temporary memory on | ||
| /// the host. | ||
| pub struct Buffer<T, A: UsmAlloc> { | ||
| data: NonNull<T>, | ||
| len: usize, | ||
| layout: Layout, | ||
| allocator: A, | ||
| } | ||
|
|
||
| impl<T, A: UsmAlloc> Buffer<T, A> { | ||
| /// Creates a new buffer given an allocator. | ||
| /// Safety: returns uninitialized memory. | ||
| pub(crate) unsafe fn new(allocator: A, len: usize) -> Self { | ||
| let layout = Layout::array::<T>(len).unwrap(); | ||
| let ptr = match allocator.allocate(layout.clone()) { | ||
| Ok(ptr) => ptr, | ||
| _ => handle_alloc_error(layout) | ||
| }; | ||
|
|
||
| Self { | ||
| data: ptr.cast(), | ||
| len, | ||
| layout, | ||
| allocator, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T, A: UsmAlloc> Deref for Buffer<T, A> { | ||
| type Target = [T]; | ||
| fn deref(&self) -> &Self::Target { | ||
| unsafe { | ||
| slice::from_raw_parts(self.data.as_ptr(), self.len) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T, A: UsmAlloc> DerefMut for Buffer<T, A> { | ||
| fn deref_mut(&mut self) -> &mut Self::Target { | ||
| unsafe { | ||
| slice::from_raw_parts_mut(self.data.as_ptr(), self.len) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<T, A: UsmAlloc> Drop for Buffer<T, A> { | ||
| fn drop(&mut self) { | ||
| unsafe { self.allocator.deallocate(self.data.cast(), self.layout); } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,3 +9,6 @@ | |
| pub mod info; | ||
| pub mod platform; | ||
| pub mod device; | ||
| pub mod queue; | ||
| pub mod usm; | ||
| pub mod buffer; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.