Skip to content

Commit

Permalink
refactor/lib: prepare for library support
Browse files Browse the repository at this point in the history
Pull request: #2
Approved by: MichaelHirn
  • Loading branch information
MichaelHirn authored and homu committed Nov 27, 2015
1 parent 9f6fc44 commit 1da70be
Show file tree
Hide file tree
Showing 18 changed files with 337 additions and 105 deletions.
34 changes: 22 additions & 12 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
//! }
//! ```
use framework::{IFramework, FrameworkError};
use error::Error;
use framework::IFramework;
use frameworks::{Native, OpenCL};
use device::IDevice;
use device::{IDevice, DeviceType};
use libraries::blas::IBlas;

#[derive(Debug, Clone)]
Expand All @@ -59,20 +60,20 @@ pub struct Backend<F: IFramework> {
framework: Box<F>,
/// Provides a device, created from one or many hardwares, which are ready to execute kernel
/// methods and synchronize memory.
device: F::D,
device: DeviceType,
}

/// Defines the functionality of the Backend.
impl<F: IFramework + Clone> Backend<F> {
/// Initialize a new Backend from a BackendConfig.
pub fn new(config: BackendConfig<F>) -> Result<Backend<F>, FrameworkError> {
match config.framework.new_device(config.hardwares) {
Ok(device) => Ok(Backend {
/// Initialize a new native Backend from a BackendConfig.
pub fn new(config: BackendConfig<F>) -> Result<Backend<F>, Error> {
let device = try!(config.framework.new_device(config.hardwares));
Ok(
Backend {
framework: Box::new(config.framework),
device: device,
}),
Err(err) => Err(err),
}
}
)
}

/// Returns the available hardware.
Expand All @@ -86,8 +87,8 @@ impl<F: IFramework + Clone> Backend<F> {
}

/// Returns the backend device.
pub fn device(&self) -> F::D {
self.device.clone()
pub fn device(&self) -> &DeviceType {
&self.device
}

/// Returns the blas binary.
Expand All @@ -102,6 +103,10 @@ impl IBlas for Backend<OpenCL> {
fn binary(&self) -> Self::B {
self.binary()
}

fn device(&self) -> &DeviceType {
self.device()
}
}

impl IBlas for Backend<Native> {
Expand All @@ -110,6 +115,10 @@ impl IBlas for Backend<Native> {
fn binary(&self) -> Self::B {
self.binary()
}

fn device(&self) -> &DeviceType {
self.device()
}
}

#[derive(Debug, Clone)]
Expand All @@ -130,3 +139,4 @@ impl<F: IFramework + Clone> BackendConfig<F> {
}
}
}

42 changes: 42 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//! Defines the general set of error types in Collenchyma.
use std::{error, fmt};

#[derive(Debug)]
/// Defines the set of available Collenchyma error types.
pub enum Error {
/// Failure related to the Framework implementation.
Framework(::framework::Error),
/// Failure related to the SharedMemory.
SharedMemory(::shared_memory::Error),
/// Failure realted to an Library(Operation).
Operation(::libraries::Error),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Framework(ref err) => write!(f, "Framwork error: {}", err),
Error::SharedMemory(ref err) => write!(f, "SharedMemory error: {}", err),
Error::Operation(ref err) => write!(f, "Library/Operation error: {}", err),
}
}
}

impl error::Error for Error {
fn description(&self) -> &str {
match *self {
Error::Framework(ref err) => err.description(),
Error::SharedMemory(ref err) => err.description(),
Error::Operation(ref err) => err.description(),
}
}

fn cause(&self) -> Option<&error::Error> {
match *self {
Error::Framework(ref err) => Some(err),
Error::SharedMemory(ref err) => Some(err),
Error::Operation(ref err) => Some(err),
}
}
}
30 changes: 18 additions & 12 deletions src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! ```
use hardware::IHardware;
use device::IDevice;
use device::{IDevice, DeviceType};
use binary::IBinary;
use frameworks::opencl::Error as OpenCLError;
use std::error;
Expand All @@ -46,7 +46,7 @@ pub trait IFramework {
fn new() -> Self where Self: Sized;

/// Initializes all the available hardwares.
fn load_hardwares() -> Result<Vec<Self::H>, FrameworkError>;
fn load_hardwares() -> Result<Vec<Self::H>, Error>;

/// Returns the cached and available hardwares.
fn hardwares(&self) -> Vec<Self::H>;
Expand All @@ -55,40 +55,46 @@ pub trait IFramework {
fn binary(&self) -> Self::B;

/// Initializes a new Device from the provided hardwares.
fn new_device(&self, Vec<Self::H>) -> Result<Self::D, FrameworkError>;
fn new_device(&self, Vec<Self::H>) -> Result<DeviceType, Error>;
}

#[derive(Debug)]
/// Defines a generic set of Framework Errors.
pub enum FrameworkError {
pub enum Error {
/// Failures related to the OpenCL framework implementation.
OpenCL(OpenCLError),
}

impl fmt::Display for FrameworkError {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
FrameworkError::OpenCL(ref err) => write!(f, "OpenCL error: {}", err),
Error::OpenCL(ref err) => write!(f, "OpenCL error: {}", err),
}
}
}

impl error::Error for FrameworkError {
impl error::Error for Error {
fn description(&self) -> &str {
match *self {
FrameworkError::OpenCL(ref err) => err.description(),
Error::OpenCL(ref err) => err.description(),
}
}

fn cause(&self) -> Option<&error::Error> {
match *self {
FrameworkError::OpenCL(ref err) => Some(err),
Error::OpenCL(ref err) => Some(err),
}
}
}

impl From<OpenCLError> for FrameworkError {
fn from(err: OpenCLError) -> FrameworkError {
FrameworkError::OpenCL(err)
impl From<OpenCLError> for Error {
fn from(err: OpenCLError) -> Error {
Error::OpenCL(err)
}
}

impl From<Error> for ::error::Error {
fn from(err: Error) -> ::error::Error {
::error::Error::Framework(err)
}
}
8 changes: 4 additions & 4 deletions src/frameworks/native/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ pub struct Binary {
}

impl Binary {
/// Initializes a native CPU hardware.
pub fn new(id: isize) -> Binary {
/// Initializes the native CPU binary.
pub fn new() -> Binary {
Binary {
id: id,
blas_dot: Function::new(1)
id: 0,
blas_dot: Function::new()
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions src/frameworks/native/flatbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ impl FlatBox {
/// The preffered way to access native memory.
pub fn as_slice<T>(&self) -> &[T] {
unsafe {
slice::from_raw_parts_mut(self.raw_box as *mut T,
self.len / mem::size_of::<T>())
slice::from_raw_parts_mut(
self.raw_box as *mut T,
self.len / mem::size_of::<T>()
)
}
}

Expand All @@ -35,8 +37,10 @@ impl FlatBox {
/// The preffered way to access native memory.
pub fn as_mut_slice<T>(&mut self) -> &mut [T] {
unsafe {
slice::from_raw_parts_mut(self.raw_box as *mut T,
self.len / mem::size_of::<T>())
slice::from_raw_parts_mut(
self.raw_box as *mut T,
self.len / mem::size_of::<T>()
)
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/frameworks/native/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ use operation::IOperation;

#[derive(Debug, Copy, Clone)]
/// Defines a host CPU operation.
pub struct Function {
id: isize,
}
pub struct Function;

impl Function {
/// Initializes a native CPU hardware.
pub fn new(id: isize) -> Function {
Function { id: id }
pub fn new() -> Function {
Function
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/frameworks/native/libraries/blas.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Provides BLAS for a Native backend.
use frameworks::native::{Function, Binary};
use memory::MemoryType;
use libraries::blas::*;

impl IBlasBinary for Binary {
Expand All @@ -12,7 +13,13 @@ impl IBlasBinary for Binary {
}

impl IOperationDot for Function {
fn compute(&self, a: i32) {
println!("{}", format!("NATIVE"))
fn compute<T>(&self, x: &MemoryType, y: &MemoryType, result: &MemoryType) -> Result<(), Error> {
match x {
&MemoryType::Native(ref x) => {
let x_slice = x.as_slice::<T>();
},
_ => ()
}
Ok(println!("{}", format!("NATIVE")))
}
}
12 changes: 6 additions & 6 deletions src/frameworks/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
//!
//!
use framework::{IFramework, FrameworkError};
use framework::IFramework;
use hardware::{HardwareType, IHardware};
use device::IDevice;
use device::DeviceType;
use self::hardware::Hardware;
pub use self::device::Cpu;
pub use self::function::Function;
Expand Down Expand Up @@ -46,13 +46,13 @@ impl IFramework for Native {
match Native::load_hardwares() {
Ok(hardwares) => Native {
hardwares: hardwares,
binary: Binary::new(1)
binary: Binary::new()
},
Err(err) => panic!(err)
}
}

fn load_hardwares() -> Result<Vec<Hardware>, FrameworkError> {
fn load_hardwares() -> Result<Vec<Hardware>, ::framework::Error> {
let cpu = Hardware::new(1)
.set_name(Some(String::from("Host CPU")))
.set_hardware_type(Some(HardwareType::CPU))
Expand All @@ -69,7 +69,7 @@ impl IFramework for Native {
self.binary.clone()
}

fn new_device(&self, devices: Vec<Hardware>) -> Result<Cpu, FrameworkError> {
Ok(Cpu::new(devices.to_vec()))
fn new_device(&self, devices: Vec<Hardware>) -> Result<DeviceType, ::framework::Error> {
Ok(DeviceType::Native(Cpu::new(devices.to_vec())))
}
}
5 changes: 3 additions & 2 deletions src/frameworks/opencl/libraries/blas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use frameworks::opencl::Kernel;
use frameworks::opencl::Program;
use memory::MemoryType;
use libraries::blas::*;

impl IBlasBinary for Program {
Expand All @@ -13,7 +14,7 @@ impl IBlasBinary for Program {
}

impl IOperationDot for Kernel {
fn compute(&self, a: i32) {
println!("{}", format!("OPENCL"))
fn compute<T>(&self, x: &MemoryType, y: &MemoryType, result: &MemoryType) -> Result<(), Error> {
unimplemented!()
}
}
14 changes: 6 additions & 8 deletions src/frameworks/opencl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#[cfg(target_os = "linux")]
extern { }

use framework::{IFramework, FrameworkError};
use framework::IFramework;
use device::DeviceType;
pub use self::platform::Platform;
pub use self::context::Context;
pub use self::memory::Memory;
Expand Down Expand Up @@ -63,11 +64,8 @@ impl IFramework for OpenCL {
}
}

fn load_hardwares() -> Result<Vec<Device>, FrameworkError> {
let platforms = match API::load_platforms() {
Ok(p) => p,
Err(err) => return Err(FrameworkError::OpenCL(err))
};
fn load_hardwares() -> Result<Vec<Device>, ::framework::Error> {
let platforms = try!(API::load_platforms());

let mut hardware_container: Vec<Device> = vec!();
for platform in &platforms {
Expand All @@ -91,7 +89,7 @@ impl IFramework for OpenCL {
/// Contexts are used by the OpenCL runtime for managing objects such as command-queues,
/// memory, program and kernel objects and for executing kernels on one or more hardwares
/// specified in the context.
fn new_device(&self, hardwares: Vec<Device>) -> Result<Context, FrameworkError> {
Ok(try!(Context::new(hardwares)))
fn new_device(&self, hardwares: Vec<Device>) -> Result<DeviceType, ::framework::Error> {
Ok(DeviceType::OpenCL(try!(Context::new(hardwares))))
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,4 @@ pub mod libraries;
pub mod shared_memory;
pub mod operation;
pub mod binary;
pub mod error;
Loading

0 comments on commit 1da70be

Please sign in to comment.