Skip to content

Commit

Permalink
Work on read/write/sync portion
Browse files Browse the repository at this point in the history
  • Loading branch information
jonysy committed Mar 6, 2017
1 parent 3c920bc commit 5466f37
Show file tree
Hide file tree
Showing 18 changed files with 559 additions and 470 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ enum_primitive = "0.1.1"
lazy_static = "0.2.4"
libloading = "0.3.2"
log = "0.3.6"
ndarray = "0.8.0"
ndarray = "0.8.0"

[dev-dependencies]
compiletest_rs = "0.2.5"
26 changes: 13 additions & 13 deletions examples/sigmoid.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
extern crate parenchyma;
// extern crate parenchyma;

use parenchyma::{Backend, SharedTensor};
// use parenchyma::{Backend, SharedTensor};

fn main() {
let backend = Backend::new()?;
let x = SharedTensor::with(&backend, [1], &mut [10.0])?;
let mut result = SharedTensor::with(&backend, [1], &mut [0.0])?;
// let backend = Backend::new()?;
// let x = SharedTensor::with(&backend, [1], &mut [10.0])?;
// let mut result = SharedTensor::with(&backend, [1], &mut [0.0])?;

backend.sigmoid(&x, &result)?;
// backend.sigmoid(&x, &result)?;

let tensor = result.view(&backend)?;
let output = tensor.buffer()[0];
// let tensor = result.view(&backend)?;
// let output = tensor.buffer()[0];

// TODO
// http://floating-point-gui.de/errors/comparison/
// https://randomascii.wordpress.com/2014/01/27/theres-only-four-billion-floatsso-test-them-all/
// // TODO
// // http://floating-point-gui.de/errors/comparison/
// // https://randomascii.wordpress.com/2014/01/27/theres-only-four-billion-floatsso-test-them-all/

assert!((output - 0.99995460213).abs() < 0.000000000001);
// assert!((output - 0.99995460213).abs() < 0.000000000001);

println!("{}", output);
// println!("{}", output);
}
19 changes: 12 additions & 7 deletions src/backend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Context, Device, Framework};
use super::{Context, ComputeDevice, Framework};
use super::error::Result;

/// The heart of Parenchyma - provides an interface for running parallel computations on one or
Expand Down Expand Up @@ -47,7 +47,7 @@ use super::error::Result;
#[derive(Debug)]
pub struct Backend {
/// The initialized framework.
pub framework: Box<Framework>,
pub framework: Box<Framework>, /* &'static str,*/
/// The context associated with the `framework`.
///
/// Contexts are the heart of both OpenCL and CUDA applications. See the [`Context`] trait for
Expand All @@ -56,7 +56,9 @@ pub struct Backend {
/// [`Context`]: (./trait.Context.html)
pub context: Box<Context>,
/// The chosen device
device: usize,
///
/// The default active device is the first device found (index = `0`).
active: usize,
}

impl Backend {
Expand All @@ -67,18 +69,21 @@ impl Backend {
unimplemented!()
}

/// Attempts to construct a backend from the specified `framework` and the index of
/// the `selected` device.
pub fn with<F>(framework: F, selected: usize) -> Result<Backend> where F: Framework {
/// Attempts to construct a backend from the specified `framework`.
pub fn with<F>(framework: F) -> Result<Backend> where F: Framework {

unimplemented!()
}

// /// Try all provided `frameworks` in the specified order, choosing the first framework that
// // initializes without failure.
// pub fn try(frameworks: Vec<Box<Framework>>) -> Result<Backend>;
}

impl Backend {

/// Returns the current device.
pub fn device<T>(&self) -> &Device<T> {
pub fn compute_device<T>(&self) -> &ComputeDevice<T> {

unimplemented!()
}
Expand Down
67 changes: 0 additions & 67 deletions src/buffer.rs

This file was deleted.

31 changes: 0 additions & 31 deletions src/context.rs

This file was deleted.

21 changes: 0 additions & 21 deletions src/device.rs

This file was deleted.

16 changes: 0 additions & 16 deletions src/framework.rs

This file was deleted.

7 changes: 7 additions & 0 deletions src/frameworks/native/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
//! Native backend support.
use ndarray::{Array, IxDyn};

/// Provides the native framework.
#[derive(Debug)]
pub struct Native;

/// Represents a native array.
///
/// note: named `Memory` for consistency across frameworks.
pub type Memory<T> = Array<T, IxDyn>;

/// The native context.
#[derive(Clone, Debug)]
pub struct NativeContext;
Expand Down
7 changes: 5 additions & 2 deletions src/frameworks/opencl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ pub struct OpenCL {
// pub available_devices: Vec<OpenClDevice>,
}

/// A type wrapping around an OpenCL buffer id that manages its deallocation.
/// A `Memory` wraps around an OpenCL buffer id that manages its deallocation, named
/// as such for consistency's sake.
///
/// OpenCL buffers are only context specific, not device specific.
#[derive(Clone, Debug)]
pub struct OpenClBuffer {
pub struct Memory {
hl: hl::Buffer,
capacity: usize,
}
Expand Down
111 changes: 111 additions & 0 deletions src/interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
use std::fmt::Debug;

use super::{Memory, Shape};
use super::error::Result;

/// Represents the location of a buffer or memory, which the associated device can
/// use to access it.
#[derive(Debug, Eq, PartialEq)]
pub struct Address {
/// A string literal containing the name of the framework.
pub framework: &'static str,
/// The context identifier
pub context: isize,
/// The device identifier.
pub device: isize,
}

/// A device capable of processing data.
///
/// The `T` type associated with the [`SharedTensor`](./struct.SharedTensor.html).
pub trait ComputeDevice<T> {

/// Allocates memory on the device.
fn allocate(&self, shape: &Shape) -> Result<Memory<T>>;

/// Allocates memory on the device.
fn allocate_with(&self, shape: &Shape, slice: &mut [T]) -> Result<Memory<T>>;

// /// Synchronizes `memory` from `source`.
// fn sync_in(&self, memory: &mut Memory<T>, source: &Memory<T>) -> Result;

/// Synchronizes `memory` to `destination`.
fn sync_out(&self, memory: &Memory<T>, destination: &mut Memory<T>) -> Result;

/// Returns the location of the device.
///
/// The `addr` method is used by `SharedTensor`s for memory storage purposes. The _address_
/// is simply the name of the framework associated with the device, the device's unique
/// identifier, and an integer associated with the context the device is contained in.
fn addr(&self) -> Address;
}

/// Contexts are the heart of both OpenCL and CUDA applications. Contexts provide a container for
/// objects such as memory, command-queues, programs/modules and kernels.
pub trait Context: Debug {

}

/// A trait implemented for all frameworks. `Framework`s contain a list of all available devices as
/// well as other objects specific to the implementor.
///
/// The default framework is simply the host CPU for common computation. To make use of other
/// devices such as GPUs, you may choose a GPGPU framework (such as OpenCL or CUDA) to access the
/// processing capabilities of the device(s).
pub trait Framework: Debug {
/// The name of the framework.
///
/// This associated constant is mainly used for the purposes of debugging and reporting errors.
///
/// note: *uses the "SCREAMING_SNAKE_CASE" naming convention (e.g., `"OPEN_CL"`).
const FRAMEWORK_NAME: &'static str;

// type Context: Context;

// fn try_init(&self) -> Result;

// fn try_context(&self, selection: Vec<Hardware>) -> Result<Box<Context>>;

// TODO:
// https://github.com/rust-lang/rust/issues/29924
#[doc(hidden)]
fn name(&self) -> &'static str {
Self::FRAMEWORK_NAME
}
}

// /// The object-safe version of `Framework`.
// trait FrameworkObject: Debug { }

/// The generic hardware representation for a `ComputeDevice`.
///
/// A compute device is a processor, such as a CPU or a GPU.
pub struct Hardware {
/// The unique ID of the hardware.
id: usize,
/// The type of compute device, such as a CPU or a GPU.
kind: HardwareKind,
/// The name.
name: String,
/// The number of compute units.
///
/// A compute device usually has multiple compute units.
compute_units: usize,
// /// Framework marker
// framework: PhantomData<F>,
}

/// General categories for devices, used to identify the type of a device.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub enum HardwareKind {
/// Used for accelerators. Accelerators can communicate with host processor using a peripheral
/// interconnect such as PCIe.
Accelerator,
/// Used for devices that are host processors. The host processor runs the implementations
/// and is a single or multi-core CPU.
Central,
/// Used for GPU devices.
Graphics,
/// Used for anything else.
Other,
}
Loading

0 comments on commit 5466f37

Please sign in to comment.