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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//! An `OpenCL` platform identifier.
//!
//! Documentation copied from [https://www.khronos.org/registry/cl/sdk/1.2/doc
//! s/man/xhtml/clGetPlatformInfo.html](https://www.khronos.org/registry/cl/sd
//! k/1.2/docs/man/xhtml/clGetPlatformInfo.html)

// use std::fmt::{std::fmt::Display, std::fmt::Formatter, Result as std::fmt::Result};
use std;
use std::ops::{Deref, DerefMut};
// use std::convert::Into;
use ffi::cl_platform_id;
use core::{self, PlatformId as PlatformIdCore, PlatformInfo, PlatformInfoResult, ClPlatformIdPtr};

/// A platform identifier.
///
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct Platform(PlatformIdCore);

impl Platform {
    /// Returns a list of all platforms avaliable on the host machine.
    pub fn list() -> Vec<Platform> {
        let list_core = core::get_platform_ids()
            .expect("Platform::list: Error retrieving platform list");

        list_core.into_iter().map(Platform::new).collect()
    }

    // DEPRICATED:
    // /// Returns the first available platform on the host machine.
    // pub fn first() -> Platform {
    //     let list_core = core::get_platform_ids()
    //         .expect("Platform::default: Error retrieving platform");

    //     // let first_idx = list_core.len() - 1;
    //     let first_idx = 0;

    //     Platform::new(list_core[first_idx].clone());
    //     panic!("Platform::default(): This method has been depricated. Please use 'Platform::default()'");
    // }

    /// Creates a new `Platform` from a `PlatformIdCore`.
    ///
    /// ## Safety
    ///
    /// Not meant to be called unless you know what you're doing.
    ///
    /// Use list to get a list of platforms.
    pub fn new(id_core: PlatformIdCore) -> Platform {
        Platform(id_core)
    }

    /// Returns a list of `Platform`s from a list of `PlatformIdCore`s
    pub fn list_from_core(platforms: Vec<PlatformIdCore>) -> Vec<Platform> {
        platforms.into_iter().map(Platform::new).collect()
    }

    // /// Returns a string containing a formatted list of every platform property.
    // pub fn to_string(&self) -> String {
    //     // self.clone().into()
    //     format!("{}", self)
    // }

    /// Returns info about the platform.
    pub fn info(&self, info_kind: PlatformInfo) -> PlatformInfoResult {
        // match core::get_platform_info(Some(self.0.clone()), info_kind) {
        //     Ok(pi) => pi,
        //     Err(err) => PlatformInfoResult::Error(Box::new(err)),
        // }
        core::get_platform_info(&self.0, info_kind)
    }

    /// Returns the platform profile as a string.
    ///
    /// Returns the profile name supported by the implementation. The profile
    /// name returned can be one of the following strings:
    ///
    /// * FULL_PROFILE - if the implementation supports the OpenCL
    ///   specification (functionality defined as part of the core
    ///   specification and does not require any extensions to be supported).
    ///
    /// * EMBEDDED_PROFILE - if the implementation supports the OpenCL
    ///   embedded profile. The embedded profile is defined to be a subset for
    ///   each version of OpenCL.
    ///
    pub fn profile(&self) -> String {
        // match core::get_platform_info(Some(self.0.clone()), PlatformInfo::Profile) {
        //     Ok(pi) => pi.into(),
        //     Err(err) => err.into(),
        // }
        core::get_platform_info(&self.0, PlatformInfo::Profile).into()
    }

    /// Returns the platform driver version as a string.
    ///
    /// Returns the OpenCL version supported by the implementation. This
    /// version string has the following format:
    ///
    /// * OpenCL<space><major_version.minor_version><space><platform-specific
    ///   information>
    ///
    /// * The major_version.minor_version value returned will be '1.2'.
    ///
    /// * TODO: Convert this to new version system returning an `OpenclVersion`.
    pub fn version(&self) -> String {
        // match core::get_platform_info(Some(self.0.clone()), PlatformInfo::Version) {
        //     Ok(pi) => pi.into(),
        //     Err(err) => err.into(),
        // }
        core::get_platform_info(&self.0, PlatformInfo::Version).into()
    }

    /// Returns the platform name as a string.
    pub fn name(&self) -> String {
        // match core::get_platform_info(Some(self.0.clone()), PlatformInfo::Name) {
        //     Ok(pi) => pi.into(),
        //     Err(err) => err.into(),
        // }
        core::get_platform_info(&self.0, PlatformInfo::Name).into()
    }

    /// Returns the platform vendor as a string.
    pub fn vendor(&self) -> String {
        // match core::get_platform_info(Some(self.0.clone()), PlatformInfo::Vendor) {
        //     Ok(pi) => pi.into(),
        //     Err(err) => err.into(),
        // }
        core::get_platform_info(&self.0, PlatformInfo::Vendor).into()
    }

    /// Returns the list of platform extensions as a string.
    ///
    /// Returns a space-separated list of extension names (the extension names
    /// themselves do not contain any spaces) supported by the platform.
    /// Extensions defined here must be supported by all devices associated
    /// with this platform.
    pub fn extensions(&self) -> String {
        // match core::get_platform_info(Some(self.0.clone()), PlatformInfo::Extensions) {
        //     Ok(pi) => pi.into(),
        //     Err(err) => err.into(),
        // }
        core::get_platform_info(&self.0, PlatformInfo::Extensions).into()
    }

    /// Returns a reference to the underlying `PlatformIdCore`.
    pub fn as_core(&self) -> &PlatformIdCore {
        &self.0
    }

    fn fmt_info(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.debug_struct("Platform")
            .field("Profile", &self.info(PlatformInfo::Profile))
            .field("Version", &self.info(PlatformInfo::Version))
            .field("Name", &self.info(PlatformInfo::Name))
            .field("Vendor", &self.info(PlatformInfo::Vendor))
            .field("Extensions", &self.info(PlatformInfo::Extensions))
            .finish()
    }
}

unsafe impl ClPlatformIdPtr for Platform {
    fn as_ptr(&self) -> cl_platform_id {
        self.0.as_ptr()
    }
}
// unsafe impl<'a> ClPlatformIdPtr for &'a Platform {}

impl Default for Platform {
    fn default() -> Platform {
        // let list_core = core::get_platform_ids()
        //     .expect("Platform::default: Error retrieving platform");

        // // let first_idx = list_core.len() - 1;
        // let first_idx = 0;

        let dflt_plat_core = core::default_platform().expect("Platform::default()");
        Platform::new(dflt_plat_core)
    }
}

impl From<PlatformIdCore> for Platform {
    fn from(core: PlatformIdCore) -> Platform {
        Platform(core)
    }
}

impl From<Platform> for String {
    fn from(p: Platform) -> String {
        format!("{}", p)
    }
}

impl From<Platform> for PlatformIdCore {
    fn from(p: Platform) -> PlatformIdCore {
        p.0
    }
}

impl<'a> From<&'a Platform> for PlatformIdCore {
    fn from(p: &Platform) -> PlatformIdCore {
        p.0.clone()
    }
}

impl std::fmt::Display for Platform {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        self.fmt_info(f)
    }
}


impl Deref for Platform {
    type Target = PlatformIdCore;

    fn deref(&self) -> &PlatformIdCore {
        &self.0
    }
}

impl DerefMut for Platform {
    fn deref_mut(&mut self) -> &mut PlatformIdCore {
        &mut self.0
    }
}