sycl_rs/device.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 sycl_rs_sys::device::ffi;
10
11pub use sycl_rs_sys::types::ffi::{Aspect, PeerAccess};
12
13use crate::{Result, info::InfoTarget, platform::Platform, private::Sealed};
14
15/// The `Device` struct encapsulates a single SYCL device on which kernels can be executed.
16///
17/// The `Device` struct provides the common reference semantics.
18pub struct Device(pub(crate) cxx::UniquePtr<ffi::Device>);
19
20impl Sealed for Device {}
21impl InfoTarget for Device {}
22
23impl From<cxx::UniquePtr<ffi::Device>> for Device {
24 fn from(value: cxx::UniquePtr<ffi::Device>) -> Self {
25 Self(value)
26 }
27}
28
29impl Device {
30 /// Returns a [`Vec`] containing all the root devices from all SYCL backends
31 /// available in the system which have the device type encapsulated by [`DeviceType`](crate::info::DeviceType).
32 pub fn get_devices() -> Vec<Self> {
33 ffi::get_devices()
34 .into_iter()
35 .map(|device| Self(device.ptr))
36 .collect()
37 }
38
39 /// Returns the associated SYCL platform.
40 pub fn get_platform(&self) -> Platform {
41 let raw_platform = ffi::get_platform(&self.0);
42 Platform(raw_platform)
43 }
44
45 /// Returns whether this device has the requested aspect.
46 pub fn has(&self, aspect: Aspect) -> bool {
47 ffi::has(&self.0, aspect)
48 }
49
50 /// Queries the peer access status between this device and `peer` according to the query
51 /// `value`.
52 ///
53 /// [`PeerAccess::AccessSupported`]: Returns true only if it is possible for this device to
54 /// enable peer access to USM device memory allocations located on the peer device.
55 ///
56 /// [`PeerAccess::AtomicsSupported`]: When this query returns true, it indicates that this
57 /// device may concurrently access and atomically modify USM device memory allocations located
58 /// on the peer device when peer access is enabled to that device.
59 pub fn can_access_peer(&mut self, peer: &Device, value: PeerAccess) -> bool {
60 ffi::can_access_peer(&mut self.0, &peer.0, value)
61 }
62
63 /// Enables this device to access USM device allocations located on the peer device.
64 /// This does not permit the peer device to access this device’s memory.
65 ///
66 /// Once this access is enabled, SYCL kernel functions and the explicit memory functions may
67 /// access USM device allocations on the peer device subject to the normal rules about context
68 /// as described in the core SYCL specification.
69 pub fn enable_peer_access(&mut self, peer: &Device) -> Result<()> {
70 ffi::enable_peer_access(&mut self.0, &peer.0)
71 }
72
73 /// Disables access to the peer device’s memory from this device.
74 pub fn disable_peer_access(&mut self, peer: &Device) -> Result<()> {
75 ffi::disable_peer_access(&mut self.0, &peer.0)
76 }
77}
78
79impl Clone for Device {
80 fn clone(&self) -> Self {
81 ffi::clone(&self.0).into()
82 }
83}