Skip to content
Merged
Show file tree
Hide file tree
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 Jul 6, 2026
a8ddc52
Add Rust Queue abstraction
szymon-zadworny Jul 6, 2026
bc7225e
Add C++ USM module
szymon-zadworny Jul 6, 2026
dff66f1
Add C++ sycl::free binding
szymon-zadworny Jul 6, 2026
561b657
Add Allocator trait
szymon-zadworny Jul 6, 2026
de0bc58
Add safe USM allocator abstraction
szymon-zadworny Jul 6, 2026
c16a455
Add support for memory alignment
szymon-zadworny Jul 7, 2026
1576121
Add C++ aligned_alloc_host binding
szymon-zadworny Jul 7, 2026
68d217d
Add C++ aligned_alloc_shared binding
szymon-zadworny Jul 7, 2026
0ee4ce7
Add generic UsmAllocator trait
szymon-zadworny Jul 7, 2026
1042197
Hide DeviceAllocator
szymon-zadworny Jul 7, 2026
ec64da1
Add a host allocator
szymon-zadworny Jul 7, 2026
dd96083
Add a shared allocator
szymon-zadworny Jul 7, 2026
5c9c7c1
Use allocator_api2
szymon-zadworny Jul 7, 2026
2682dfa
Add a Buffer abstraction
szymon-zadworny Jul 7, 2026
02c5d3a
Add alloc shared support
szymon-zadworny Jul 7, 2026
6df58cc
Add alloc example
szymon-zadworny Jul 7, 2026
3d2a2d8
Add missing header to source list
szymon-zadworny Jul 7, 2026
53955dc
Add alloc host support
szymon-zadworny Jul 7, 2026
10dc6a5
Determine buffer size at runtime
szymon-zadworny Jul 10, 2026
8f106af
Mark uninitialized allocations as unsafe
szymon-zadworny Jul 13, 2026
c45267a
Add Buffer documentation
szymon-zadworny Jul 13, 2026
2e0b61f
Add USM documentation
szymon-zadworny Jul 13, 2026
409e687
Add Queue::alloc_uninit_* documentation
szymon-zadworny Jul 13, 2026
f8f78c1
Mark UsmAlloc as an unsafe trait
szymon-zadworny Jul 13, 2026
a1ae562
Fix doc formatting
szymon-zadworny Jul 13, 2026
693bfaf
Style fixes
szymon-zadworny Jul 14, 2026
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
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions oneapi-rs-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,23 @@ fn main() {
"src/types-sys.rs",
"src/platform-sys.rs",
"src/device-sys.rs",
"src/queue-sys.rs",
Comment thread
bratpiorka marked this conversation as resolved.
"src/usm-sys.rs",
];

let cpp_sources = [
"src/platform.cpp",
"src/device.cpp",
"src/queue.cpp",
"src/usm.cpp",
];

let cpp_headers = [
"include/types.hpp",
"include/platform.hpp",
"include/device.hpp",
"include/queue.hpp",
"include/usm.hpp",
];

cxx_build::bridges(&rust_sources)
Expand Down
19 changes: 19 additions & 0 deletions oneapi-rs-sys/include/queue.hpp
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
1 change: 1 addition & 0 deletions oneapi-rs-sys/include/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
namespace sycl_shims {
using Device = sycl::device;
using Platform = sycl::platform;
using Queue = sycl::queue;
}
21 changes: 21 additions & 0 deletions oneapi-rs-sys/include/usm.hpp
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
6 changes: 6 additions & 0 deletions oneapi-rs-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ pub mod platform;

#[path = "device-sys.rs"]
pub mod device;

#[path = "queue-sys.rs"]
pub mod queue;

#[path = "usm-sys.rs"]
pub mod usm;
22 changes: 22 additions & 0 deletions oneapi-rs-sys/src/queue-sys.rs
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>;
}
}
19 changes: 19 additions & 0 deletions oneapi-rs-sys/src/queue.cpp
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
2 changes: 2 additions & 0 deletions oneapi-rs-sys/src/types-sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub mod ffi {
include!("oneapi-rs-sys/include/types.hpp");
type Device;
type Platform;
type Queue;
}

// This is a workaround - cxx currently doesn't support passing
Expand Down Expand Up @@ -39,6 +40,7 @@ pub mod ffi {

impl UniquePtr<Device> {}
impl UniquePtr<Platform> {}
impl UniquePtr<Queue> {}

impl Vec<DevicePtr> {}
impl Vec<PlatformPtr> {}
Expand Down
23 changes: 23 additions & 0 deletions oneapi-rs-sys/src/usm-sys.rs
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);
}
}
28 changes: 28 additions & 0 deletions oneapi-rs-sys/src/usm.cpp
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
2 changes: 2 additions & 0 deletions oneapi-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ edition = "2024"
license = "MIT OR Apache-2.0"

[dependencies]
allocator-api2 = "0.4.0"
cxx = "1.0.194"
oneapi-rs-sys = { path = "../oneapi-rs-sys" }
thiserror = "2.0.18"
24 changes: 24 additions & 0 deletions oneapi-rs/examples/alloc.rs
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!();
}
72 changes: 72 additions & 0 deletions oneapi-rs/src/buffer.rs
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); }
}
}
3 changes: 3 additions & 0 deletions oneapi-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
pub mod info;
pub mod platform;
pub mod device;
pub mod queue;
pub mod usm;
pub mod buffer;
Loading