Created
May 28, 2013 18:59
-
-
Save auroranockert/5665203 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[link(name = "cl", vers = "0.1", uuid = "1205b5b0-fbd0-4eeb-bfac-e85947419b4e")]; | |
| #[license = "MIT"]; | |
| #[crate_type = "lib"]; | |
| #[author = "Jens Nockert"]; | |
| #[comment = "OpenCL library for Rust"]; | |
| #[desc = "Trying to make OpenCL more usable from Rust, uses the openlc.rs bindings."]; | |
| extern mod string (uuid = "86237a28-e038-4119-a0be-df3fda9521de"); | |
| extern mod opencl (uuid = "f83bfc2b-e3ee-4e4c-b324-70e379fbcff2"); | |
| use string::Split; | |
| use opencl::*; | |
| use core::result; | |
| macro_rules! cl_call(($name:ident : $($arg:expr),+) => ({ | |
| let error = unsafe { opencl::$name($($arg),+) }; | |
| if (error != opencl::CL_SUCCESS) { | |
| return result::Err(error); | |
| } | |
| })) | |
| macro_rules! cl_call_unknown_length( | |
| ($name:ident, $n:ty, $in:ty, $out:ty) => ({ | |
| let mut n:$n = 0; | |
| cl_call!($name: 0, ptr::mut_null(), ptr::to_mut_unsafe_ptr(&mut n)); | |
| let mut result:~[$out] = vec::with_capacity(n as uint); | |
| cl_call!($name: n, vec::raw::to_mut_ptr(result) as *mut $in, ptr::mut_null()); | |
| unsafe { vec::raw::set_len(&mut result, n as uint) }; | |
| result | |
| }); | |
| ($name:ident, $n:ty, $in:ty, $out:ty : $($arg:expr),+) => ({ | |
| let mut n:$n = 0; | |
| cl_call!($name: $($arg),+, 0, ptr::mut_null(), ptr::to_mut_unsafe_ptr(&mut n)); | |
| let mut result:~[$out] = vec::with_capacity(n as uint); | |
| cl_call!($name: $($arg),+, n, vec::raw::to_mut_ptr(result) as *mut $in, ptr::mut_null()); | |
| unsafe { vec::raw::set_len(&mut result, n as uint) }; | |
| result | |
| })) | |
| macro_rules! cl_get_info(($name:expr) => (match self.get_info($name) { | |
| Ok(x) => x, | |
| Err(n) => fail!(fmt!("Error %? should not happen for %?", n, $name)) | |
| })) | |
| struct Platform { | |
| id: cl_platform_id | |
| } | |
| struct Device { | |
| id: cl_device_id | |
| } | |
| impl Clone for Platform { | |
| fn clone(&self) -> Platform { | |
| Platform { id: self.id } | |
| } | |
| } | |
| impl Clone for Device { | |
| fn clone(&self) -> Device { | |
| Device { id: self.id } | |
| } | |
| } | |
| impl Platform { | |
| pub fn all() -> result::Result<~[Platform], cl_int> { | |
| return result::Ok(cl_call_unknown_length!(clGetPlatformIDs, cl_uint, cl_platform_id, Platform)); | |
| } | |
| pub fn get_info(&self, name: cl_platform_info) -> result::Result<~str, cl_int> { | |
| return result::Ok(str::from_bytes(cl_call_unknown_length!(clGetPlatformInfo, libc::size_t, libc::c_void, u8: self.id, name))); | |
| } | |
| pub fn profile(&self) -> ~str { cl_get_info!(CL_PLATFORM_PROFILE) } | |
| pub fn version(&self) -> ~str { cl_get_info!(CL_PLATFORM_VERSION) } | |
| pub fn name(&self) -> ~str { cl_get_info!(CL_PLATFORM_NAME) } | |
| pub fn vendor(&self) -> ~str { cl_get_info!(CL_PLATFORM_VENDOR) } | |
| pub fn extensions(&self) -> ~[~str] { let result = cl_get_info!(CL_PLATFORM_EXTENSIONS); result.split(' ') } | |
| pub fn devices(&self, device_type: cl_device_type) -> result::Result<~[Device], cl_int> { | |
| return result::Ok(cl_call_unknown_length!(clGetDeviceIDs, cl_uint, cl_device_id, Device: self.id, device_type)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[link(name = "cl", | |
| vers = "0.1", | |
| uuid = "1205b5b0-fbd0-4eeb-bfac-e85947419b4e")]; | |
| #[license = "MIT"]; | |
| #[crate_type = "lib"]; | |
| #[author = "Jens Nockert"]; | |
| #[comment = "OpenCL library for Rust"]; | |
| #[desc = | |
| "Trying to make OpenCL more usable from Rust, uses the openlc.rs bindings."]; | |
| extern mod string(uuid = "86237a28-e038-4119-a0be-df3fda9521de"); | |
| extern mod opencl(uuid = "f83bfc2b-e3ee-4e4c-b324-70e379fbcff2"); | |
| use string::Split; | |
| use opencl::*; | |
| use core::result; | |
| struct Platform { | |
| id: cl_platform_id, | |
| } | |
| struct Device { | |
| id: cl_device_id, | |
| } | |
| impl Clone for Platform { | |
| fn clone(&self) -> Platform { Platform{id: self.id,} } | |
| } | |
| impl Clone for Device { | |
| fn clone(&self) -> Device { Device{id: self.id,} } | |
| } | |
| impl Platform { | |
| pub fn all() -> result::Result<~[Platform], cl_int> { | |
| return result::Ok({ | |
| let mut n: cl_uint = 0; | |
| { | |
| let error = | |
| unsafe { | |
| opencl::clGetPlatformIDs(0, | |
| ptr::mut_null(), | |
| ptr::to_mut_unsafe_ptr(&mut n)) | |
| }; | |
| if (error != opencl::CL_SUCCESS) { | |
| return result::Err(error); | |
| } | |
| }; | |
| let mut result: ~[Platform] = | |
| vec::with_capacity(n as uint); | |
| { | |
| let error = | |
| unsafe { | |
| opencl::clGetPlatformIDs(n, | |
| vec::raw::to_mut_ptr(result) | |
| as | |
| *mut cl_platform_id, | |
| ptr::mut_null()) | |
| }; | |
| if (error != opencl::CL_SUCCESS) { | |
| return result::Err(error); | |
| } | |
| }; | |
| unsafe { | |
| vec::raw::set_len(&mut result, n as uint) | |
| }; | |
| result | |
| }); | |
| } | |
| pub fn get_info(&self, name: cl_platform_info) -> | |
| result::Result<~str, cl_int> { | |
| return result::Ok(str::from_bytes({ | |
| let mut n: libc::size_t = 0; | |
| { | |
| let error = | |
| unsafe { | |
| opencl::clGetPlatformInfo(self.id, | |
| name, | |
| 0, | |
| ptr::mut_null(), | |
| ptr::to_mut_unsafe_ptr(&mut n)) | |
| }; | |
| if (error != | |
| opencl::CL_SUCCESS) | |
| { | |
| return result::Err(error); | |
| } | |
| }; | |
| let mut result: ~[u8] = | |
| vec::with_capacity(n as | |
| uint); | |
| { | |
| let error = | |
| unsafe { | |
| opencl::clGetPlatformInfo(self.id, | |
| name, | |
| n, | |
| vec::raw::to_mut_ptr(result) | |
| as | |
| *mut libc::c_void, | |
| ptr::mut_null()) | |
| }; | |
| if (error != | |
| opencl::CL_SUCCESS) | |
| { | |
| return result::Err(error); | |
| } | |
| }; | |
| unsafe { | |
| vec::raw::set_len(&mut result, | |
| n as uint) | |
| }; | |
| result | |
| })); | |
| } | |
| pub fn profile(&self) -> ~str { | |
| match self.get_info(CL_PLATFORM_PROFILE) { | |
| Ok(x) => x, | |
| Err(n) => | |
| ::core::sys::FailWithCause::fail_with({ | |
| let mut __fmtbuf = | |
| ~"Error "; | |
| ::unstable::extfmt::rt::conv_poly(::unstable::extfmt::rt::Conv{flags: | |
| ::unstable::extfmt::rt::flag_none, | |
| width: | |
| ::unstable::extfmt::rt::CountImplied, | |
| precision: | |
| ::unstable::extfmt::rt::CountImplied, | |
| ty: | |
| ::unstable::extfmt::rt::TyDefault,}, | |
| &n, | |
| &mut __fmtbuf); | |
| ::str::push_str(&mut __fmtbuf, | |
| " should not happen for "); | |
| ::unstable::extfmt::rt::conv_poly(::unstable::extfmt::rt::Conv{flags: | |
| ::unstable::extfmt::rt::flag_none, | |
| width: | |
| ::unstable::extfmt::rt::CountImplied, | |
| precision: | |
| ::unstable::extfmt::rt::CountImplied, | |
| ty: | |
| ::unstable::extfmt::rt::TyDefault,}, | |
| &CL_PLATFORM_PROFILE, | |
| &mut __fmtbuf); | |
| __fmtbuf | |
| }, "cl.rs/cl.rs", 89u) | |
| } | |
| } | |
| pub fn version(&self) -> ~str { | |
| match self.get_info(CL_PLATFORM_VERSION) { | |
| Ok(x) => x, | |
| Err(n) => | |
| ::core::sys::FailWithCause::fail_with({ | |
| let mut __fmtbuf = | |
| ~"Error "; | |
| ::unstable::extfmt::rt::conv_poly(::unstable::extfmt::rt::Conv{flags: | |
| ::unstable::extfmt::rt::flag_none, | |
| width: | |
| ::unstable::extfmt::rt::CountImplied, | |
| precision: | |
| ::unstable::extfmt::rt::CountImplied, | |
| ty: | |
| ::unstable::extfmt::rt::TyDefault,}, | |
| &n, | |
| &mut __fmtbuf); | |
| ::str::push_str(&mut __fmtbuf, | |
| " should not happen for "); | |
| ::unstable::extfmt::rt::conv_poly(::unstable::extfmt::rt::Conv{flags: | |
| ::unstable::extfmt::rt::flag_none, | |
| width: | |
| ::unstable::extfmt::rt::CountImplied, | |
| precision: | |
| ::unstable::extfmt::rt::CountImplied, | |
| ty: | |
| ::unstable::extfmt::rt::TyDefault,}, | |
| &CL_PLATFORM_VERSION, | |
| &mut __fmtbuf); | |
| __fmtbuf | |
| }, "cl.rs/cl.rs", 90u) | |
| } | |
| } | |
| pub fn name(&self) -> ~str { | |
| match self.get_info(CL_PLATFORM_NAME) { | |
| Ok(x) => x, | |
| Err(n) => | |
| ::core::sys::FailWithCause::fail_with({ | |
| let mut __fmtbuf = | |
| ~"Error "; | |
| ::unstable::extfmt::rt::conv_poly(::unstable::extfmt::rt::Conv{flags: | |
| ::unstable::extfmt::rt::flag_none, | |
| width: | |
| ::unstable::extfmt::rt::CountImplied, | |
| precision: | |
| ::unstable::extfmt::rt::CountImplied, | |
| ty: | |
| ::unstable::extfmt::rt::TyDefault,}, | |
| &n, | |
| &mut __fmtbuf); | |
| ::str::push_str(&mut __fmtbuf, | |
| " should not happen for "); | |
| ::unstable::extfmt::rt::conv_poly(::unstable::extfmt::rt::Conv{flags: | |
| ::unstable::extfmt::rt::flag_none, | |
| width: | |
| ::unstable::extfmt::rt::CountImplied, | |
| precision: | |
| ::unstable::extfmt::rt::CountImplied, | |
| ty: | |
| ::unstable::extfmt::rt::TyDefault,}, | |
| &CL_PLATFORM_NAME, | |
| &mut __fmtbuf); | |
| __fmtbuf | |
| }, "cl.rs/cl.rs", 91u) | |
| } | |
| } | |
| pub fn vendor(&self) -> ~str { | |
| match self.get_info(CL_PLATFORM_VENDOR) { | |
| Ok(x) => x, | |
| Err(n) => | |
| ::core::sys::FailWithCause::fail_with({ | |
| let mut __fmtbuf = | |
| ~"Error "; | |
| ::unstable::extfmt::rt::conv_poly(::unstable::extfmt::rt::Conv{flags: | |
| ::unstable::extfmt::rt::flag_none, | |
| width: | |
| ::unstable::extfmt::rt::CountImplied, | |
| precision: | |
| ::unstable::extfmt::rt::CountImplied, | |
| ty: | |
| ::unstable::extfmt::rt::TyDefault,}, | |
| &n, | |
| &mut __fmtbuf); | |
| ::str::push_str(&mut __fmtbuf, | |
| " should not happen for "); | |
| ::unstable::extfmt::rt::conv_poly(::unstable::extfmt::rt::Conv{flags: | |
| ::unstable::extfmt::rt::flag_none, | |
| width: | |
| ::unstable::extfmt::rt::CountImplied, | |
| precision: | |
| ::unstable::extfmt::rt::CountImplied, | |
| ty: | |
| ::unstable::extfmt::rt::TyDefault,}, | |
| &CL_PLATFORM_VENDOR, | |
| &mut __fmtbuf); | |
| __fmtbuf | |
| }, "cl.rs/cl.rs", 92u) | |
| } | |
| } | |
| pub fn extensions(&self) -> ~[~str] { | |
| let result = | |
| match self.get_info(CL_PLATFORM_EXTENSIONS) { | |
| Ok(x) => x, | |
| Err(n) => | |
| ::core::sys::FailWithCause::fail_with({ | |
| let mut __fmtbuf = | |
| ~"Error "; | |
| ::unstable::extfmt::rt::conv_poly(::unstable::extfmt::rt::Conv{flags: | |
| ::unstable::extfmt::rt::flag_none, | |
| width: | |
| ::unstable::extfmt::rt::CountImplied, | |
| precision: | |
| ::unstable::extfmt::rt::CountImplied, | |
| ty: | |
| ::unstable::extfmt::rt::TyDefault,}, | |
| &n, | |
| &mut __fmtbuf); | |
| ::str::push_str(&mut __fmtbuf, | |
| " should not happen for "); | |
| ::unstable::extfmt::rt::conv_poly(::unstable::extfmt::rt::Conv{flags: | |
| ::unstable::extfmt::rt::flag_none, | |
| width: | |
| ::unstable::extfmt::rt::CountImplied, | |
| precision: | |
| ::unstable::extfmt::rt::CountImplied, | |
| ty: | |
| ::unstable::extfmt::rt::TyDefault,}, | |
| &CL_PLATFORM_EXTENSIONS, | |
| &mut __fmtbuf); | |
| __fmtbuf | |
| }, "cl.rs/cl.rs", 93u) | |
| }; | |
| result.split(' ') | |
| } | |
| pub fn devices(&self, device_type: cl_device_type) -> | |
| result::Result<~[Device], cl_int> { | |
| return result::Ok({ | |
| let mut n: cl_uint = 0; | |
| { | |
| let error = | |
| unsafe { | |
| opencl::clGetDeviceIDs(self.id, | |
| device_type, | |
| 0, | |
| ptr::mut_null(), | |
| ptr::to_mut_unsafe_ptr(&mut n)) | |
| }; | |
| if (error != opencl::CL_SUCCESS) { | |
| return result::Err(error); | |
| } | |
| }; | |
| let mut result: ~[Device] = | |
| vec::with_capacity(n as uint); | |
| { | |
| let error = | |
| unsafe { | |
| opencl::clGetDeviceIDs(self.id, | |
| device_type, | |
| n, | |
| vec::raw::to_mut_ptr(result) | |
| as | |
| *mut cl_device_id, | |
| ptr::mut_null()) | |
| }; | |
| if (error != opencl::CL_SUCCESS) { | |
| return result::Err(error); | |
| } | |
| }; | |
| unsafe { | |
| vec::raw::set_len(&mut result, n as uint) | |
| }; | |
| result | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment