Skip to main content

oneapi_rs/
queue.rs

1//
2// Copyright (C) 2026 Intel Corporation
3//
4// Under the MIT License or the Apache License v2.0.
5// See LICENSE-MIT and LICENSE-APACHE for license information.
6// SPDX-License-Identifier: MIT OR Apache-2.0
7//
8
9use oneapi_rs_sys::queue::ffi;
10
11use crate::{buffer::Buffer, device::Device, usm::{HostAllocator, SharedAllocator, UsmAllocator}};
12
13/// The `Queue` connects a host program to a single device. Programs submit tasks to a device via the
14/// `Queue` and may monitor the `Queue` for completion. A program initiates the task by submitting
15/// a kernel.
16pub struct Queue(pub(crate) cxx::UniquePtr<ffi::Queue>);
17
18impl Queue {
19    /// Construct a `Queue` based on the device returned from the default selector.
20    pub fn new() -> Self {
21        Self(ffi::new_queue())
22    }
23
24    /// Allocates memory and creates a host-side [`Buffer`] that can store an array of T.
25    /// Safety: the buffer contents are uninitialized.
26    pub unsafe fn alloc_uninit_host<T>(&self, len: usize) -> Buffer<T, UsmAllocator<'_, HostAllocator>> {
27        let allocator = UsmAllocator::from(self);
28        unsafe { Buffer::new(allocator, len) }
29    }
30
31    /// Allocates memory and creates a shared [`Buffer`] that can store an array of T.
32    /// Safety: the buffer contents are uninitialized.
33    pub unsafe fn alloc_uninit_shared<T>(&self, len: usize) -> Buffer<T, UsmAllocator<'_, SharedAllocator>> {
34        let allocator = UsmAllocator::from(self);
35        unsafe { Buffer::new(allocator, len) }
36    }
37}
38
39impl From<&Device> for Queue {
40    fn from(value: &Device) -> Self {
41        Self(ffi::new_queue_from_device(&value.0))
42    }
43}