Skip to content

Commit

Permalink
added feature-specific writing interface to views
Browse files Browse the repository at this point in the history
  • Loading branch information
imrn99 committed Nov 10, 2023
1 parent b1b2179 commit 77d558f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ rayon = ["dep:rayon"]
cxx = "*"
cfg-if = "*"
rayon = {version = "*", optional=true}
atomic = "0.6.0"

[dev-dependencies]
criterion = { version = "*", features = ["html_reports"] }
Expand Down
37 changes: 29 additions & 8 deletions src/view/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct ViewBase<'a, const N: usize, T> {
pub stride: [usize; N],
}

#[cfg(not(any(feature = "rayon", feature = "threads")))]
// ~~~~~~~~ Constructors
impl<'a, const N: usize, T> ViewBase<'a, N, T>
where
T: Default + Clone, // fair assumption imo
Expand Down Expand Up @@ -122,6 +124,27 @@ where
stride: self.stride,
}
}
}

impl<'a, const N: usize, T> ViewBase<'a, N, T> {
// ~~~~~~~~ Uniform writing interface across all features

#[inline(always)]
#[cfg(not(any(feature = "rayon", feature = "threads")))]
/// Serial writing interface. Uses mutable indexing implementation.
pub fn set(&mut self, index: [usize; N], val: T) {
self[index] = val;
}

#[inline(always)]
#[cfg(any(feature = "rayon", feature = "threads"))]
/// Thread-safe writing interface. Uses non-mutable indexing and
/// immutability of atomic type methods.
pub fn set(&self, index: [usize; N], val: T) {
self[index].store(val, atomic::Ordering::Relaxed);
}

// ~~~~~~~~ Convenience

pub fn raw_val<'b>(self) -> Result<Vec<T>, ViewError<'b>> {
if let DataType::Owned(v) = self.data {
Expand All @@ -143,10 +166,8 @@ where
}
}

impl<'a, const N: usize, T> Index<[usize; N]> for ViewBase<'a, N, T>
where
T: Default + Clone,
{
/// Read-only access is always implemented.
impl<'a, const N: usize, T> Index<[usize; N]> for ViewBase<'a, N, T> {
type Output = T;

fn index(&self, index: [usize; N]) -> &Self::Output {
Expand All @@ -168,10 +189,10 @@ where
}
}

impl<'a, const N: usize, T> IndexMut<[usize; N]> for ViewBase<'a, N, T>
where
T: Default + Clone,
{
#[cfg(not(any(feature = "rayon", feature = "threads")))]
/// Read-write access is implemented using [IndexMut] trait when no parallel
/// features are enabled.
impl<'a, const N: usize, T> IndexMut<[usize; N]> for ViewBase<'a, N, T> {
fn index_mut(&mut self, index: [usize; N]) -> &mut Self::Output {
let flat_idx: usize = self.flat_idx(index);
match &mut self.data {
Expand Down
15 changes: 12 additions & 3 deletions src/view/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@
//! - Memory traits?
//!
#[cfg(any(feature = "rayon", feature = "threads"))]
use atomic::Atomic;

/// Maximum possible depth (i.e. number of dimensions) for a view.
pub const MAX_VIEW_DEPTH: usize = 8;

#[cfg(not(any(feature = "rayon", feature = "threads")))]
pub type InnerDataType<T> = T;

#[cfg(any(feature = "rayon", feature = "threads"))]
pub type InnerDataType<T> = Atomic<T>;

#[derive(Debug)]
/// Enum used to identify the type of data the view is holding. See variants for more
/// information. The policy used to implement the [PartialEq] trait is based on Kokkos'
/// [`equal` algorithm](https://kokkos.github.io/kokkos-core-wiki/API/algorithms/std-algorithms/all/StdEqual.html).
pub enum DataType<'a, T> {
/// The view owns the data.
Owned(Vec<T>),
Owned(Vec<InnerDataType<T>>),
/// The view borrows the data and can only read it.
Borrowed(&'a [T]),
Borrowed(&'a [InnerDataType<T>]),
/// The view borrows the data and can both read and modify it.
MutBorrowed(&'a mut [T]),
MutBorrowed(&'a mut [InnerDataType<T>]),
}

/// Kokkos implements equality check by comparing the pointers, i.e.
Expand Down

0 comments on commit 77d558f

Please sign in to comment.