diff --git a/oneapi-rs/examples/kernel_launch_derive.rs b/oneapi-rs/examples/kernel_launch_derive.rs index 8be1e8a..d70d7ec 100644 --- a/oneapi-rs/examples/kernel_launch_derive.rs +++ b/oneapi-rs/examples/kernel_launch_derive.rs @@ -6,9 +6,10 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 // +use oneapi_rs::prelude::*; + use oneapi_rs::{ buffer::Buffer, - kernel::{KernelArgument, KernelArgumentList}, queue::Queue, range::NdRange, usm::{SharedAllocator, UsmAllocator}, diff --git a/oneapi-rs/examples/sycl-ls.rs b/oneapi-rs/examples/sycl-ls.rs index 7473408..e6e1419 100644 --- a/oneapi-rs/examples/sycl-ls.rs +++ b/oneapi-rs/examples/sycl-ls.rs @@ -6,8 +6,8 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 // -use oneapi_rs::info; -use oneapi_rs::platform::Platform; +use oneapi_rs::prelude::*; +use oneapi_rs::{info, platform::Platform}; fn main() { for platform in Platform::get_platforms() { diff --git a/oneapi-rs/src/device.rs b/oneapi-rs/src/device.rs index 8560062..d2d4d5c 100644 --- a/oneapi-rs/src/device.rs +++ b/oneapi-rs/src/device.rs @@ -8,13 +8,16 @@ use oneapi_rs_sys::device::ffi; -use crate::{info::device::DeviceInfo, platform::Platform}; +use crate::{info::InfoTarget, platform::Platform, private::Sealed}; /// The `Device` struct encapsulates a single SYCL device on which kernels can be executed. /// /// The `Device` struct provides the common reference semantics. pub struct Device(pub(crate) cxx::UniquePtr); +impl Sealed for Device {} +impl InfoTarget for Device {} + impl From> for Device { fn from(value: cxx::UniquePtr) -> Self { Self(value) @@ -31,14 +34,6 @@ impl Device { .collect() } - /// Queries this `Device` for information requested by the generic parameter `Param`. - /// The associated type `Param::Item` must be defined in accordance with the info parameters - /// in [`oneapi_rs::info::device`](`crate::info::device`) to facilitate returning the type - /// associated with the `Param` parameter. - pub fn get_info(&self) -> T::Item { - T::get_item(self) - } - /// Returns the associated SYCL platform. pub fn get_platform(&self) -> Platform { let raw_platform = ffi::get_platform(&self.0); diff --git a/oneapi-rs/src/event.rs b/oneapi-rs/src/event.rs index 0b120c1..9e8e1c3 100644 --- a/oneapi-rs/src/event.rs +++ b/oneapi-rs/src/event.rs @@ -16,7 +16,7 @@ use oneapi_rs_sys::{event::ffi, types::SharedWaker}; use pin_project::pin_project; -use crate::{info::event::EventInfo, queue::Queue}; +use crate::{info::InfoTarget, private::Sealed, queue::Queue}; pub struct Event(pub(crate) cxx::UniquePtr); @@ -24,12 +24,11 @@ impl Event { pub fn wait(&mut self) { ffi::wait(&mut self.0); } - - pub fn get_info(&self) -> T::Item { - T::get_item(self) - } } +impl Sealed for Event {} +impl InfoTarget for Event {} + impl From> for Event { fn from(value: cxx::UniquePtr) -> Self { Self(value) diff --git a/oneapi-rs/src/info.rs b/oneapi-rs/src/info.rs index 9811994..821c399 100644 --- a/oneapi-rs/src/info.rs +++ b/oneapi-rs/src/info.rs @@ -20,3 +20,22 @@ pub use oneapi_rs_sys::device::ffi::DeviceType; /// Event status of the contained action associated with this event. pub use oneapi_rs_sys::event::ffi::EventCommandStatus; + +use crate::private::Sealed; + +/// Types which can return an Item of information for a given Target. +pub trait Info: Sealed { + type Item; + type Target; + + /// Returns information for a given Target. + fn get_item(target: &Self::Target) -> Self::Item; +} + +/// Types which can be queried for information. +pub trait InfoTarget: Sealed { + /// Queries this object for information requested by given generic parameter. + fn get_info>(&self) -> T::Item { + T::get_item(self) + } +} diff --git a/oneapi-rs/src/info/device-info.rs b/oneapi-rs/src/info/device-info.rs index f630b72..3696850 100644 --- a/oneapi-rs/src/info/device-info.rs +++ b/oneapi-rs/src/info/device-info.rs @@ -7,36 +7,39 @@ // use crate::device::Device; +use crate::info::Info; +use crate::private::Sealed; use oneapi_rs_sys::device::ffi; -pub trait DeviceInfo { - type Item; - fn get_item(device: &Device) -> Self::Item; -} - /// Returns the device type associated with the device. May not return `oneapi_rs::info::DeviceType::All` pub struct DeviceType; -impl DeviceInfo for DeviceType { +impl Sealed for DeviceType {} +impl Info for DeviceType { type Item = crate::info::DeviceType; - fn get_item(device: &Device) -> Self::Item { - ffi::get_device_type(&device.0) + type Target = Device; + fn get_item(target: &Self::Target) -> Self::Item { + ffi::get_device_type(&target.0) } } /// Returns a backend-defined device version. pub struct Version; -impl DeviceInfo for Version { +impl Sealed for Version {} +impl Info for Version { type Item = String; - fn get_item(device: &Device) -> Self::Item { - ffi::get_version(&device.0) + type Target = Device; + fn get_item(target: &Self::Target) -> Self::Item { + ffi::get_version(&target.0) } } /// Returns the device name of this SYCL device. pub struct Name; -impl DeviceInfo for Name { +impl Sealed for Name {} +impl Info for Name { type Item = String; - fn get_item(device: &Device) -> Self::Item { - ffi::get_name(&device.0) + type Target = Device; + fn get_item(target: &Self::Target) -> Self::Item { + ffi::get_name(&target.0) } } diff --git a/oneapi-rs/src/info/event-info.rs b/oneapi-rs/src/info/event-info.rs index 1a8e6bf..9517429 100644 --- a/oneapi-rs/src/info/event-info.rs +++ b/oneapi-rs/src/info/event-info.rs @@ -7,18 +7,17 @@ // use crate::event::Event; +use crate::info::Info; +use crate::private::Sealed; use oneapi_rs_sys::event::ffi; -pub trait EventInfo { - type Item; - fn get_item(event: &Event) -> Self::Item; -} - /// Returns the event status of the action associated with this event. pub struct CommandExecutionStatus; -impl EventInfo for CommandExecutionStatus { +impl Sealed for CommandExecutionStatus {} +impl Info for CommandExecutionStatus { type Item = crate::info::EventCommandStatus; - fn get_item(event: &Event) -> Self::Item { - ffi::get_command_execution_status(&event.0) + type Target = Event; + fn get_item(target: &Self::Target) -> Self::Item { + ffi::get_command_execution_status(&target.0) } } diff --git a/oneapi-rs/src/info/platform-info.rs b/oneapi-rs/src/info/platform-info.rs index 168381d..179e4a8 100644 --- a/oneapi-rs/src/info/platform-info.rs +++ b/oneapi-rs/src/info/platform-info.rs @@ -6,37 +6,40 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 // +use crate::info::Info; use crate::platform::Platform; +use crate::private::Sealed; use oneapi_rs_sys::platform::ffi; -pub trait PlatformInfo { - type Item; - fn get_item(platform: &Platform) -> Self::Item; -} - /// Returns a backend-defined platform version. pub struct Version; -impl PlatformInfo for Version { +impl Sealed for Version {} +impl Info for Version { type Item = String; - fn get_item(platform: &Platform) -> Self::Item { - ffi::get_version(&platform.0) + type Target = Platform; + fn get_item(target: &Self::Target) -> Self::Item { + ffi::get_version(&target.0) } } /// Returns the name of the platform. pub struct Name; -impl PlatformInfo for Name { +impl Sealed for Name {} +impl Info for Name { type Item = String; - fn get_item(platform: &Platform) -> Self::Item { - ffi::get_name(&platform.0) + type Target = Platform; + fn get_item(target: &Self::Target) -> Self::Item { + ffi::get_name(&target.0) } } /// Returns the name of the vendor providing the platform. pub struct Vendor; -impl PlatformInfo for Vendor { +impl Sealed for Vendor {} +impl Info for Vendor { type Item = String; - fn get_item(platform: &Platform) -> Self::Item { - ffi::get_vendor(&platform.0) + type Target = Platform; + fn get_item(target: &Self::Target) -> Self::Item { + ffi::get_vendor(&target.0) } } diff --git a/oneapi-rs/src/lib.rs b/oneapi-rs/src/lib.rs index f179f20..42156bf 100644 --- a/oneapi-rs/src/lib.rs +++ b/oneapi-rs/src/lib.rs @@ -13,6 +13,7 @@ pub mod event; pub mod info; pub mod kernel; pub mod platform; +pub mod prelude; pub mod queue; pub mod range; pub mod usm; diff --git a/oneapi-rs/src/platform.rs b/oneapi-rs/src/platform.rs index 7af6fe3..c398771 100644 --- a/oneapi-rs/src/platform.rs +++ b/oneapi-rs/src/platform.rs @@ -8,7 +8,7 @@ use oneapi_rs_sys::platform::ffi; -use crate::{device::Device, info::platform::PlatformInfo}; +use crate::{device::Device, info::InfoTarget, private::Sealed}; /// Abstraction for SYCL platform. /// @@ -18,6 +18,9 @@ use crate::{device::Device, info::platform::PlatformInfo}; /// A `Platform` is also associated with one or more SYCL devices associated with the same SYCL backend. pub struct Platform(pub(crate) cxx::UniquePtr); +impl Sealed for Platform {} +impl InfoTarget for Platform {} + impl Platform { /// Returns a [`Vec`] containing all SYCL platforms from all SYCL backends available in the system. pub fn get_platforms() -> Vec { @@ -34,11 +37,4 @@ impl Platform { .map(|device| Device(device.ptr)) .collect() } - - /// Queries this `Platform` for information requested by the generic parameter `Param`. - /// The associated type `Param::Item` must be defined in accordance with the info parameters - /// in [`oneapi_rs::info::platform`](`crate::info::platform`) to facilitate returning the type associated with the `Param` parameter. - pub fn get_info(&self) -> T::Item { - T::get_item(self) - } } diff --git a/oneapi-rs/src/prelude.rs b/oneapi-rs/src/prelude.rs new file mode 100644 index 0000000..5cb0e43 --- /dev/null +++ b/oneapi-rs/src/prelude.rs @@ -0,0 +1,4 @@ +pub use crate::{ + info::InfoTarget, + kernel::{KernelArgument, KernelArgumentList}, +};