Skip to content

Implement rayon's ParallelIterator on a wrapper around AxisIter, AxisIterMut #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,24 @@ optional = true
blas-sys = { version = "0.6.5", optional = true, default-features = false }
matrixmultiply = { version = "0.1.13" }

rayon = { version = "0.6.0", optional = true }

[dependencies.serde]
version = "0.8.20"
optional = true

[dev-dependencies]
num_cpus = "1.2"

[features]
blas = ["blas-sys"]

# These features are used for testing
blas-openblas-sys = ["blas"]
test = ["blas-openblas-sys"]
test = ["blas-openblas-sys", "rayon"]

# This feature is used for docs
docs = ["rustc-serialize", "serde"]
docs = ["rustc-serialize", "serde", "rayon"]

[profile.release]
[profile.bench]
Expand Down
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ your `Cargo.toml`.
Uses ``blas-sys`` for pluggable backend, which needs to be configured
separately.

- ``rayon``

- Optional, compatible with Rust stable
- Implement rayon 0.6 parallelization.

How to use with cargo::

[dependencies]
Expand Down
114 changes: 114 additions & 0 deletions benches/rayon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

#![feature(test)]

extern crate num_cpus;
extern crate test;
use test::Bencher;

#[macro_use(s)]
extern crate ndarray;
use ndarray::prelude::*;

extern crate rayon;
use rayon::prelude::*;

const EXP_N: usize = 128;

use std::cmp::max;

fn set_threads() {
let n = max(1, num_cpus::get() / 2);
let cfg = rayon::Configuration::new().set_num_threads(n);
let _ = rayon::initialize(cfg);
}

#[bench]
fn map_exp_regular(bench: &mut Bencher)
{
let mut a = Array2::<f64>::zeros((EXP_N, EXP_N));
a.swap_axes(0, 1);
bench.iter(|| {
a.mapv_inplace(|x| x.exp());
});
}

#[bench]
fn rayon_exp_regular(bench: &mut Bencher)
{
set_threads();
let mut a = Array2::<f64>::zeros((EXP_N, EXP_N));
a.swap_axes(0, 1);
bench.iter(|| {
a.view_mut().into_par_iter().for_each(|x| *x = x.exp());
});
}

const FASTEXP: usize = 800;

#[inline]
fn fastexp(x: f64) -> f64 {
let x = 1. + x/1024.;
x.powi(1024)
}

#[bench]
fn map_fastexp_regular(bench: &mut Bencher)
{
let mut a = Array2::<f64>::zeros((FASTEXP, FASTEXP));
bench.iter(|| {
a.mapv_inplace(|x| fastexp(x))
});
}

#[bench]
fn rayon_fastexp_regular(bench: &mut Bencher)
{
set_threads();
let mut a = Array2::<f64>::zeros((FASTEXP, FASTEXP));
bench.iter(|| {
a.view_mut().into_par_iter().for_each(|x| *x = fastexp(*x));
});
}

#[bench]
fn map_fastexp_cut(bench: &mut Bencher)
{
let mut a = Array2::<f64>::zeros((FASTEXP, FASTEXP));
let mut a = a.slice_mut(s![.., ..-1]);
bench.iter(|| {
a.mapv_inplace(|x| fastexp(x))
});
}

#[bench]
fn rayon_fastexp_cut(bench: &mut Bencher)
{
set_threads();
let mut a = Array2::<f64>::zeros((FASTEXP, FASTEXP));
let mut a = a.slice_mut(s![.., ..-1]);
bench.iter(|| {
a.view_mut().into_par_iter().for_each(|x| *x = fastexp(*x));
});
}

#[bench]
fn map_fastexp_by_axis(bench: &mut Bencher)
{
let mut a = Array2::<f64>::zeros((FASTEXP, FASTEXP));
bench.iter(|| {
for mut sheet in a.axis_iter_mut(Axis(0)) {
sheet.mapv_inplace(fastexp)
}
});
}

#[bench]
fn rayon_fastexp_by_axis(bench: &mut Bencher)
{
set_threads();
let mut a = Array2::<f64>::zeros((FASTEXP, FASTEXP));
bench.iter(|| {
a.axis_iter_mut(Axis(0)).into_par_iter()
.for_each(|mut sheet| sheet.mapv_inplace(fastexp));
});
}
4 changes: 0 additions & 4 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,10 +688,6 @@ impl<A, S, D> ArrayBase<S, D> where S: Data<Elem=A>, D: Dimension
is_standard_layout(&self.dim, &self.strides)
}

fn is_contiguous(&self) -> bool {
D::is_contiguous(&self.dim, &self.strides)
}

/// Return a pointer to the first element in the array.
///
/// Raw access to array elements needs to follow the strided indexing
Expand Down
58 changes: 56 additions & 2 deletions src/impl_views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,62 @@ use error::ShapeError;

use StrideShape;

pub trait ArrayViewPrivate {
type Item;
type Slice: IntoIterator<Item=Self::Item>;
unsafe fn into_slice_memory_order(self) -> Self::Slice;

fn into_fold<Acc, F>(self, acc: Acc, f: F) -> Acc
where F: FnMut(Acc, Self::Item) -> Acc;
}

impl<'a, A, D> ArrayViewPrivate for ArrayView<'a, A, D>
where D: Dimension,
{
type Item = &'a A;
type Slice = &'a [A];
unsafe fn into_slice_memory_order(self) -> Self::Slice {
debug_assert!(self.is_contiguous());
slice::from_raw_parts(self.ptr, self.len())
}

fn into_fold<Acc, F>(self, acc: Acc, f: F) -> Acc
where F: FnMut(Acc, Self::Item) -> Acc
{
if self.is_contiguous() {
unsafe {
self.into_slice_memory_order().into_iter().fold(acc, f)
}
} else {
self.into_iter().fold(acc, f)
}
}
}


impl<'a, A, D> ArrayViewPrivate for ArrayViewMut<'a, A, D>
where D: Dimension,
{
type Item = &'a mut A;
type Slice = &'a mut [A];
unsafe fn into_slice_memory_order(self) -> Self::Slice {
debug_assert!(self.is_contiguous());
slice::from_raw_parts_mut(self.ptr, self.len())
}

fn into_fold<Acc, F>(self, acc: Acc, f: F) -> Acc
where F: FnMut(Acc, Self::Item) -> Acc
{
if self.is_contiguous() {
unsafe {
self.into_slice_memory_order().into_iter().fold(acc, f)
}
} else {
self.into_iter().fold(acc, f)
}
}
}

/// # Methods Specific to Array Views
///
/// Methods for read-only array views `ArrayView<'a, A, D>`
Expand Down Expand Up @@ -126,7 +182,6 @@ impl<'a, A, D> ArrayBase<ViewRepr<&'a A>, D>
None
}
}

}

/// Methods for read-write array views `ArrayViewMut<'a, A, D>`
Expand Down Expand Up @@ -237,6 +292,5 @@ impl<'a, A, D> ArrayBase<ViewRepr<&'a mut A>, D>
None
}
}

}

3 changes: 3 additions & 0 deletions src/iterators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use super::{
Axis,
};

#[cfg(feature = "rayon")]
pub mod par;

/// Base for array iterators
///
/// Iterator element type is `&'a A`.
Expand Down
Loading