use cl3::device;
use cl3::platform;
use cl3::program;
use cl3::types::{
cl_device_id, cl_device_type, cl_int, cl_name_version, cl_platform_id, cl_ulong, cl_version,
};
use std::ffi::CString;
pub struct Platform {
id: cl_platform_id,
}
impl Platform {
pub fn new(id: cl_platform_id) -> Platform {
Platform { id }
}
pub fn id(&self) -> cl_platform_id {
self.id
}
pub fn get_devices(&self, device_type: cl_device_type) -> Result<Vec<cl_device_id>, cl_int> {
device::get_device_ids(self.id, device_type)
}
pub fn profile(&self) -> Result<CString, cl_int> {
Ok(
platform::get_platform_info(self.id, platform::PlatformInfo::CL_PLATFORM_PROFILE)?
.to_str()
.unwrap(),
)
}
pub fn version(&self) -> Result<CString, cl_int> {
Ok(
platform::get_platform_info(self.id, platform::PlatformInfo::CL_PLATFORM_VERSION)?
.to_str()
.unwrap(),
)
}
pub fn name(&self) -> Result<CString, cl_int> {
Ok(
platform::get_platform_info(self.id, platform::PlatformInfo::CL_PLATFORM_NAME)?
.to_str()
.unwrap(),
)
}
pub fn vendor(&self) -> Result<CString, cl_int> {
Ok(
platform::get_platform_info(self.id, platform::PlatformInfo::CL_PLATFORM_VENDOR)?
.to_str()
.unwrap(),
)
}
pub fn extensions(&self) -> Result<CString, cl_int> {
Ok(
platform::get_platform_info(self.id, platform::PlatformInfo::CL_PLATFORM_EXTENSIONS)?
.to_str()
.unwrap(),
)
}
pub fn host_timer_resolution(&self) -> Result<cl_ulong, cl_int> {
Ok(platform::get_platform_info(
self.id,
platform::PlatformInfo::CL_PLATFORM_HOST_TIMER_RESOLUTION,
)?
.to_ulong())
}
pub fn numeric_version(&self) -> Result<cl_version, cl_int> {
Ok(platform::get_platform_info(
self.id,
platform::PlatformInfo::CL_PLATFORM_NUMERIC_VERSION,
)?
.to_uint())
}
pub fn extensions_with_version(&self) -> Result<Vec<cl_name_version>, cl_int> {
Ok(platform::get_platform_info(
self.id,
platform::PlatformInfo::CL_PLATFORM_EXTENSIONS_WITH_VERSION,
)?
.to_vec_name_version())
}
pub fn unload_compiler(&self) -> Result<(), cl_int> {
program::unload_platform_compiler(self.id)
}
}
pub fn get_platforms() -> Result<Vec<Platform>, cl_int> {
let platform_ids = platform::get_platform_ids()?;
let mut platforms: Vec<Platform> = Vec::with_capacity(platform_ids.len());
for id in platform_ids.iter() {
platforms.push(Platform::new(*id));
}
Ok(platforms)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::error_codes::error_text;
#[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!("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: {}",
error_text(e)
),
};
println!("");
}
}
}