pub use cl3::platform;
use super::Result;
use cl3::device;
#[allow(unused_imports)]
use cl3::dx9_media_sharing;
#[allow(unused_imports)]
use cl3::ext;
#[allow(unused_imports)]
use cl3::program;
#[allow(unused_imports)]
use cl3::types::{
cl_device_id, cl_device_type, cl_name_version, cl_platform_id, cl_platform_info, cl_uint,
cl_ulong, cl_version,
};
#[allow(unused_imports)]
use libc::{c_void, intptr_t};
#[derive(Copy, Clone, Debug)]
pub struct Platform {
id: intptr_t,
}
impl From<cl_platform_id> for Platform {
fn from(value: cl_platform_id) -> Self {
Platform {
id: value as intptr_t,
}
}
}
impl From<Platform> for cl_platform_id {
fn from(value: Platform) -> Self {
value.id as cl_platform_id
}
}
unsafe impl Send for Platform {}
unsafe impl Sync for Platform {}
impl Platform {
pub fn new(id: cl_platform_id) -> Platform {
Platform { id: id as intptr_t }
}
pub fn id(&self) -> cl_platform_id {
self.id as cl_platform_id
}
pub fn get_devices(&self, device_type: cl_device_type) -> Result<Vec<cl_device_id>> {
Ok(device::get_device_ids(self.id(), device_type)?)
}
#[cfg(feature = "cl_khr_dx9_media_sharing")]
pub fn get_device_ids_from_dx9_intel(
&self,
dx9_device_source: dx9_media_sharing::cl_dx9_device_source_intel,
dx9_object: *mut c_void,
dx9_device_set: dx9_media_sharing::cl_dx9_device_set_intel,
) -> Result<Vec<cl_device_id>> {
Ok(dx9_media_sharing::get_device_ids_from_dx9_intel(
self.id(),
dx9_device_source,
dx9_object,
dx9_device_set,
)?)
}
pub fn profile(&self) -> Result<String> {
Ok(platform::get_platform_info(self.id(), platform::CL_PLATFORM_PROFILE)?.into())
}
pub fn version(&self) -> Result<String> {
Ok(platform::get_platform_info(self.id(), platform::CL_PLATFORM_VERSION)?.into())
}
pub fn name(&self) -> Result<String> {
Ok(platform::get_platform_info(self.id(), platform::CL_PLATFORM_NAME)?.into())
}
pub fn vendor(&self) -> Result<String> {
Ok(platform::get_platform_info(self.id(), platform::CL_PLATFORM_VENDOR)?.into())
}
pub fn extensions(&self) -> Result<String> {
Ok(platform::get_platform_info(self.id(), platform::CL_PLATFORM_EXTENSIONS)?.into())
}
pub fn host_timer_resolution(&self) -> Result<cl_ulong> {
Ok(
platform::get_platform_info(self.id(), platform::CL_PLATFORM_HOST_TIMER_RESOLUTION)?
.into(),
)
}
pub fn numeric_version(&self) -> Result<cl_version> {
Ok(platform::get_platform_info(self.id(), platform::CL_PLATFORM_NUMERIC_VERSION)?.into())
}
pub fn extensions_with_version(&self) -> Result<Vec<cl_name_version>> {
Ok(
platform::get_platform_info(self.id(), platform::CL_PLATFORM_EXTENSIONS_WITH_VERSION)?
.into(),
)
}
pub fn platform_external_memory_import_handle_types_khr(&self) -> Result<Vec<cl_name_version>> {
Ok(platform::get_platform_info(
self.id(),
ext::CL_PLATFORM_EXTERNAL_MEMORY_IMPORT_HANDLE_TYPES_KHR,
)?
.into())
}
pub fn platform_semaphore_import_handle_types_khr(&self) -> Result<Vec<cl_name_version>> {
Ok(platform::get_platform_info(
self.id(),
ext::CL_PLATFORM_SEMAPHORE_IMPORT_HANDLE_TYPES_KHR,
)?
.into())
}
pub fn platform_semaphore_export_handle_types_khr(&self) -> Result<Vec<cl_name_version>> {
Ok(platform::get_platform_info(
self.id(),
ext::CL_PLATFORM_SEMAPHORE_EXPORT_HANDLE_TYPES_KHR,
)?
.into())
}
pub fn platform_semaphore_types_khr(&self) -> Result<Vec<cl_name_version>> {
Ok(platform::get_platform_info(self.id(), ext::CL_PLATFORM_SEMAPHORE_TYPES_KHR)?.into())
}
pub fn get_data(&self, param_name: cl_platform_info) -> Result<Vec<u8>> {
Ok(platform::get_platform_data(self.id(), param_name)?)
}
#[cfg(feature = "CL_VERSION_1_2")]
pub unsafe fn unload_compiler(&self) -> Result<()> {
Ok(program::unload_platform_compiler(self.id())?)
}
}
pub fn get_platforms() -> Result<Vec<Platform>> {
let platform_ids = platform::get_platform_ids()?;
Ok(platform_ids
.iter()
.map(|id| Platform::new(*id))
.collect::<Vec<Platform>>())
}
#[cfg(feature = "cl_khr_icd")]
pub fn icd_get_platform_ids_khr() -> Result<Vec<Platform>> {
let platform_ids = ext::icd_get_platform_ids_khr()?;
Ok(platform_ids
.iter()
.map(|id| Platform::new(*id))
.collect::<Vec<Platform>>())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_platforms() {
let platforms = get_platforms().unwrap();
println!("Number of platforms: {}", platforms.len());
assert!(0 < platforms.len());
for platform in platforms {
println!("Platform Debug Trait: {:?}", platform);
println!("CL_PLATFORM_NAME: {}", platform.name().unwrap());
println!("CL_PLATFORM_PROFILE: {}", platform.profile().unwrap());
let value = platform.version().unwrap();
println!("CL_PLATFORM_VERSION: {:?}", value);
println!("CL_PLATFORM_VENDOR: {}", platform.vendor().unwrap());
println!(
"CL_PLATFORM_EXTENSIONS: {:?}",
platform.extensions().unwrap()
);
match platform.host_timer_resolution() {
Ok(value) => {
println!("CL_PLATFORM_HOST_TIMER_RESOLUTION: {}", value)
}
Err(e) => println!(
"OpenCL error, CL_PLATFORM_HOST_TIMER_RESOLUTION: {:?}, {}",
e, e
),
};
println!();
}
}
}