1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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!("");
}
}
}