opencl_core/
platform.rs

1// /// Platform has 3 basic functions (other than holding a cl object handle).
2// ///
3// /// Platform is the interface for listing platforms.
4// ///
5// /// Platform is the interface for getting metadata about platforms.
6// ///
7// /// Platform is the interface for listing Devices.
8// ///
9// /// NOTE: Platform is tested!
10
11use std::default::Default;
12use std::fmt;
13
14use crate::ll;
15use crate::ll::{ClPlatformID, Output, PlatformInfo, PlatformPtr};
16
17use ffi::cl_platform_id;
18
19pub struct Platform {
20    inner: ClPlatformID,
21    _unconstructable: (),
22}
23
24impl Platform {
25    pub fn new(p: ClPlatformID) -> Platform {
26        Platform {
27            inner: p,
28            _unconstructable: (),
29        }
30    }
31
32    pub fn low_level_platform(&self) -> &ClPlatformID {
33        &self.inner
34    }
35}
36
37unsafe impl Send for Platform {}
38unsafe impl Sync for Platform {}
39
40impl PlatformPtr for Platform {
41    fn platform_ptr(&self) -> cl_platform_id {
42        self.inner.platform_ptr()
43    }
44}
45
46impl PlatformPtr for &Platform {
47    fn platform_ptr(&self) -> cl_platform_id {
48        self.inner.platform_ptr()
49    }
50}
51
52impl Default for Platform {
53    fn default() -> Platform {
54        Platform::new(ClPlatformID::default())
55    }
56}
57
58impl Platform {
59    pub fn list_all() -> Output<Vec<Platform>> {
60        ll::list_platforms().map(|plats| plats.into_iter().map(|p| Platform::new(p)).collect())
61    }
62
63    fn info(&self, info_code: PlatformInfo) -> Output<String> {
64        ll::platform_info(self, info_code)
65    }
66
67    pub fn name(&self) -> Output<String> {
68        self.info(PlatformInfo::Name)
69    }
70
71    pub fn version(&self) -> Output<String> {
72        self.info(PlatformInfo::Version)
73    }
74
75    pub fn profile(&self) -> Output<String> {
76        self.info(PlatformInfo::Profile)
77    }
78
79    pub fn vendor(&self) -> Output<String> {
80        self.info(PlatformInfo::Vendor)
81    }
82
83    pub fn extensions(&self) -> Output<Vec<String>> {
84        self.info(PlatformInfo::Extensions)
85            .map(|exts| exts.split(' ').map(|ext| ext.to_string()).collect())
86    }
87
88    // v2.1
89    // pub fn host_timer_resolution(&self) -> Output<String> {
90    //     self.get_info(PlatformInfo::HostTimerResolution)
91    // }
92}
93
94impl fmt::Debug for Platform {
95    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
96        write!(f, "Platform{{{:?}}}", self.platform_ptr())
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    use crate::{testing, Platform};
103    // use crate::device::{Device, DeviceType, DevicePtr};
104
105    #[test]
106    fn platform_func_default_works() {
107        let _platform: Platform = Platform::default();
108    }
109
110    #[test]
111    fn platform_func_all_works() {
112        let platforms: Vec<Platform> = Platform::list_all().expect("Platform::list_all failed");
113        assert!(platforms.len() > 0);
114    }
115
116    #[test]
117    fn platform_has_methods_for_info() {
118        let platforms = testing::get_platforms();
119        assert_ne!(platforms.len(), 0);
120        for platform in platforms.into_iter() {
121            let empty_string = "".to_string();
122
123            let name = platform
124                .name()
125                .expect("failed to get platform info for name");
126
127            assert!(name != empty_string);
128
129            let version = platform
130                .version()
131                .expect("failed to get platform info for version");
132
133            assert!(version != empty_string);
134
135            let profile = platform
136                .profile()
137                .expect("failed to get platform info for profile");
138
139            assert!(profile != empty_string);
140
141            let vendor = platform
142                .vendor()
143                .expect("failed to get platform info for vendor");
144
145            assert!(vendor != empty_string);
146
147            let extensions = platform
148                .extensions()
149                .expect("failed to get platform info for extensions");
150
151            for ext in extensions.into_iter() {
152                assert!(ext != empty_string);
153            }
154            // v2.1
155            // let host_timer_resolution = platform.host_timer_resolution()
156            //     .expect("failed to get platform info for host_timer_resolution");
157
158            // assert_eq!(host_timer_resolution, "".to_string());
159        }
160    }
161}