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
// /// Platform has 3 basic functions (other than holding a cl object handle).
// ///
// /// Platform is the interface for listing platforms.
// ///
// /// Platform is the interface for getting metadata about platforms.
// ///
// /// Platform is the interface for listing Devices.
// ///
// /// NOTE: Platform is tested!

use std::default::Default;
use std::fmt;

use crate::ll;
use crate::ll::{ClPlatformID, Output, PlatformInfo, PlatformPtr};

use ffi::cl_platform_id;

pub struct Platform {
    inner: ClPlatformID,
    _unconstructable: (),
}

impl Platform {
    pub fn new(p: ClPlatformID) -> Platform {
        Platform {
            inner: p,
            _unconstructable: (),
        }
    }

    pub fn low_level_platform(&self) -> &ClPlatformID {
        &self.inner
    }
}

unsafe impl Send for Platform {}
unsafe impl Sync for Platform {}

impl PlatformPtr for Platform {
    fn platform_ptr(&self) -> cl_platform_id {
        self.inner.platform_ptr()
    }
}

impl PlatformPtr for &Platform {
    fn platform_ptr(&self) -> cl_platform_id {
        self.inner.platform_ptr()
    }
}

impl Default for Platform {
    fn default() -> Platform {
        Platform::new(ClPlatformID::default())
    }
}

impl Platform {
    pub fn list_all() -> Output<Vec<Platform>> {
        ll::list_platforms().map(|plats| plats.into_iter().map(|p| Platform::new(p)).collect())
    }

    fn info(&self, info_code: PlatformInfo) -> Output<String> {
        ll::platform_info(self, info_code)
    }

    pub fn name(&self) -> Output<String> {
        self.info(PlatformInfo::Name)
    }

    pub fn version(&self) -> Output<String> {
        self.info(PlatformInfo::Version)
    }

    pub fn profile(&self) -> Output<String> {
        self.info(PlatformInfo::Profile)
    }

    pub fn vendor(&self) -> Output<String> {
        self.info(PlatformInfo::Vendor)
    }

    pub fn extensions(&self) -> Output<Vec<String>> {
        self.info(PlatformInfo::Extensions)
            .map(|exts| exts.split(' ').map(|ext| ext.to_string()).collect())
    }

    // v2.1
    // pub fn host_timer_resolution(&self) -> Output<String> {
    //     self.get_info(PlatformInfo::HostTimerResolution)
    // }
}

impl fmt::Debug for Platform {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Platform{{{:?}}}", self.platform_ptr())
    }
}

#[cfg(test)]
mod tests {
    use crate::{testing, Platform};
    // use crate::device::{Device, DeviceType, DevicePtr};

    #[test]
    fn platform_func_default_works() {
        let _platform: Platform = Platform::default();
    }

    #[test]
    fn platform_func_all_works() {
        let platforms: Vec<Platform> = Platform::list_all().expect("Platform::list_all failed");
        assert!(platforms.len() > 0);
    }

    #[test]
    fn platform_has_methods_for_info() {
        let platforms = testing::get_platforms();
        assert_ne!(platforms.len(), 0);
        for platform in platforms.into_iter() {
            let empty_string = "".to_string();

            let name = platform
                .name()
                .expect("failed to get platform info for name");

            assert!(name != empty_string);

            let version = platform
                .version()
                .expect("failed to get platform info for version");

            assert!(version != empty_string);

            let profile = platform
                .profile()
                .expect("failed to get platform info for profile");

            assert!(profile != empty_string);

            let vendor = platform
                .vendor()
                .expect("failed to get platform info for vendor");

            assert!(vendor != empty_string);

            let extensions = platform
                .extensions()
                .expect("failed to get platform info for extensions");

            for ext in extensions.into_iter() {
                assert!(ext != empty_string);
            }
            // v2.1
            // let host_timer_resolution = platform.host_timer_resolution()
            //     .expect("failed to get platform info for host_timer_resolution");

            // assert_eq!(host_timer_resolution, "".to_string());
        }
    }
}