-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactor Shape struct * Implement realloc method * Add write to memory and write to memory offset methods to SharedTensor * Refactor native memory representation
- Loading branch information
Showing
13 changed files
with
331 additions
and
203 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,69 @@ | ||
use ndarray::{Array, IxDyn}; | ||
use std::fmt; | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
/// Represents a native array. | ||
/// A newtype (with an internal type of an n-dimensional array) representing a native memory buffer. | ||
/// | ||
/// note: named `Memory` for consistency across frameworks. | ||
pub type NativeMemory<T> = Array<T, IxDyn>; | ||
pub struct NativeMemory<T>(Array<T, IxDyn>); | ||
|
||
impl<T> NativeMemory<T> { | ||
|
||
/// Constructs a `NativeMemory` from the provided `array`. | ||
pub fn new(array: Array<T, IxDyn>) -> NativeMemory<T> { | ||
NativeMemory(array) | ||
} | ||
|
||
/// Returns the number of elements in the array. | ||
pub fn len(&self) -> usize { | ||
self.0.len() | ||
} | ||
|
||
/// Returns a flattened, linear representation of the tensor. | ||
/// | ||
/// **caution**: this method uses `unwrap`. | ||
pub fn as_flat(&self) -> &[T] { | ||
self.0.as_slice_memory_order().expect("the array's data is not contiguous") | ||
} | ||
|
||
/// Returns a mutable flattened, linear representation of the tensor. | ||
/// | ||
/// **caution**: this method uses `unwrap`. | ||
pub fn as_mut_flat(&mut self) -> &mut [T] { | ||
self.0.as_slice_memory_order_mut().expect("the array's data is not contiguous") | ||
} | ||
} | ||
|
||
impl<T> Clone for NativeMemory<T> where Array<T, IxDyn>: Clone { | ||
|
||
fn clone(&self) -> NativeMemory<T> { | ||
NativeMemory(self.0.clone()) | ||
} | ||
|
||
fn clone_from(&mut self, other: &Self) { | ||
self.0.clone_from(&other.0) | ||
} | ||
} | ||
|
||
impl<T> Deref for NativeMemory<T> { | ||
|
||
type Target = Array<T, IxDyn>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<T> DerefMut for NativeMemory<T> { | ||
|
||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl<T> fmt::Debug for NativeMemory<T> where T: fmt::Debug { | ||
|
||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "{:?}", self.0) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::cell::Cell; | ||
|
||
/// A "newtype" with an internal type of `Cell<u64>`. `u64Map` uses [bit manipulation][1] to manage | ||
/// memory versions. | ||
/// | ||
/// [1]: http://stackoverflow.com/a/141873/2561805 | ||
#[allow(non_camel_case_types)] | ||
#[derive(Debug)] | ||
pub struct u64Map(Cell<u64>); | ||
|
||
impl u64Map { | ||
/// The maximum number of bits in the bit map can contain. | ||
pub const CAPACITY: usize = 64; | ||
|
||
/// Constructs a new `u64Map`. | ||
pub fn new() -> u64Map { | ||
u64Map::with(0) | ||
} | ||
|
||
/// Constructs a new `u64Map` with the supplied `n`. | ||
pub fn with(n: u64) -> u64Map { | ||
u64Map(Cell::new(n)) | ||
} | ||
|
||
pub fn get(&self) -> u64 { | ||
self.0.get() | ||
} | ||
|
||
pub fn set(&self, v: u64) { | ||
self.0.set(v) | ||
} | ||
|
||
pub fn empty(&self) -> bool { | ||
self.0.get() == 0 | ||
} | ||
|
||
pub fn insert(&self, k: usize) { | ||
self.0.set(self.0.get() | (1 << k)) | ||
} | ||
|
||
pub fn contains(&self, k: usize) -> bool { | ||
k < Self::CAPACITY && (self.0.get() & (1 << k) != 0) | ||
} | ||
|
||
pub fn latest(&self) -> u32 { | ||
self.0.get().trailing_zeros() | ||
} | ||
} |
Oops, something went wrong.