Skip to content

Commit

Permalink
doc/example: add shared_memory example
Browse files Browse the repository at this point in the history
Pull request: #2
Approved by: MichaelHirn
  • Loading branch information
hobofan authored and homu committed Nov 27, 2015
1 parent f5c8fda commit 2328dd3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ pub enum MemoryType {
}

impl MemoryType {
/// Returns the native memory type, if available.
/// Extract the FlatBox if MemoryType is Native.
pub fn as_native(&self) -> Option<&FlatBox> {
match *self {
MemoryType::Native(ref ret) => Some(ret),
_ => None,
}
}

/// Returns the mut native memory type, if available.
/// Extract the FlatBox mutably if MemoryType is Native.
pub fn as_mut_native(&mut self) -> Option<&mut FlatBox> {
match *self {
MemoryType::Native(ref mut ret) => Some(ret),
_ => None,
}
}

/// Returns the opencl memory type, if available.
/// Extract the OpenCL Memory if MemoryType is OpenCL.
pub fn as_opencl(&self) -> Option<&Memory> {
match *self {
MemoryType::OpenCL(ref ret) => Some(ret),
_ => None,
}
}

/// Returns the mut opencl memory type, if available.
/// Extract the OpenCL Memory mutably if MemoryType is OpenCL.
pub fn as_mut_opencl(&mut self) -> Option<&mut Memory> {
match *self {
MemoryType::OpenCL(ref mut ret) => Some(ret),
Expand Down
22 changes: 22 additions & 0 deletions src/shared_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@
//!
//! [frameworks]: ../frameworks/index.html
//! [mem]: ../memory/index.html
//!
//! ## Examples
//!
//! Create SharedMemory and fill it with some numbers:
//!
//! ```
//! #![feature(clone_from_slice)]
//! # extern crate collenchyma;
//! use collenchyma::framework::IFramework;
//! use collenchyma::frameworks::Native;
//! use collenchyma::shared_memory::SharedMemory;
//! # fn main() {
//! // allocate memory
//! let native = Native::new();
//! let device = native.new_device(native.hardwares()).unwrap();
//! let shared_data = &mut SharedMemory::<i32>::new(&device, 5);
//! // fill memory with some numbers
//! let local_data = [0, 1, 2, 3, 4];
//! let data = shared_data.get_mut(&device).unwrap().as_mut_native().unwrap();
//! data.as_mut_slice().clone_from_slice(&local_data);
//! # }
//! ```
use std::collections::HashMap;
use device::{IDevice, DeviceType};
Expand Down
20 changes: 10 additions & 10 deletions tests/framework_opencl_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ mod framework_opencl_spec {
println!("{:?}", frm.new_device(hardwares));
}

#[test]
#[allow(unused_must_use)]
fn it_allocates_memory() {
let vec_a = vec![0isize, 1, 2, -3, 4, 5, 6, 7];
let frm = OpenCL::new();
if let DeviceType::OpenCL(ctx) = frm.new_device(frm.hardwares()[0..1].to_vec()).unwrap() {
// OpenCL memory
Memory::new(ctx.id_c(), vec_a.len());
}
}
// #[test]
// #[allow(unused_must_use)]
// fn it_allocates_memory() {
// let vec_a = vec![0isize, 1, 2, -3, 4, 5, 6, 7];
// let frm = OpenCL::new();
// if let DeviceType::OpenCL(ctx) = frm.new_device(frm.hardwares()[0..1].to_vec()).unwrap() {
// // OpenCL memory
// Memory::new(ctx.id_c(), vec_a.len());
// }
// }
}
6 changes: 3 additions & 3 deletions tests/shared_memory_specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ mod shared_memory_spec {
#[test]
fn it_has_correct_latest_device() {
let ntv = Native::new();
let cpu = ntv.new_device(ntv.hardwares()).unwrap();
let shared_data = &mut SharedMemory::<f32>::new(&cpu, 10);
assert_eq!(cpu, *shared_data.latest_device());
let cpu_dev = ntv.new_device(ntv.hardwares()).unwrap();
let shared_data = &mut SharedMemory::<f32>::new(&cpu_dev, 10);
assert_eq!(&cpu_dev, shared_data.latest_device());
}
}

0 comments on commit 2328dd3

Please sign in to comment.