feature gate functionality

This commit is contained in:
geo-ant 2022-10-24 08:39:03 +02:00 committed by Sébastien Crozet
parent 8638b796ac
commit 85e7551c81
6 changed files with 77 additions and 61 deletions

View File

@ -61,7 +61,7 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: test - name: test
run: cargo test --features arbitrary,rand,serde-serialize,sparse,debug,io,compare,libm,proptest-support,slow-tests,rkyv-safe-deser; run: cargo test --features arbitrary,rand,serde-serialize,sparse,debug,io,compare,libm,proptest-support,slow-tests,rkyv-safe-deser,rayon;
test-nalgebra-glm: test-nalgebra-glm:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:

View File

@ -33,6 +33,7 @@ libm = [ "simba/libm" ]
libm-force = [ "simba/libm_force" ] libm-force = [ "simba/libm_force" ]
macros = [ "nalgebra-macros" ] macros = [ "nalgebra-macros" ]
cuda = [ "cust_core", "simba/cuda" ] cuda = [ "cust_core", "simba/cuda" ]
rayon = [ "dep:rayon" ]
# Conversion # Conversion
convert-mint = [ "mint" ] convert-mint = [ "mint" ]
@ -101,7 +102,7 @@ glam020 = { package = "glam", version = "0.20", optional = true }
glam021 = { package = "glam", version = "0.21", optional = true } glam021 = { package = "glam", version = "0.21", optional = true }
glam022 = { package = "glam", version = "0.22", optional = true } glam022 = { package = "glam", version = "0.22", optional = true }
cust_core = { version = "0.1", optional = true } cust_core = { version = "0.1", optional = true }
rayon = "1.5" # TODO make this feature gated rayon = { version = "1.5", optional = true }
[dev-dependencies] [dev-dependencies]
serde_json = "1.0" serde_json = "1.0"

View File

@ -6,8 +6,6 @@ use std::iter::FusedIterator;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::mem; use std::mem;
use rayon::iter::plumbing::Producer;
use crate::base::dimension::{Dim, U1}; use crate::base::dimension::{Dim, U1};
use crate::base::storage::{RawStorage, RawStorageMut}; use crate::base::storage::{RawStorage, RawStorageMut};
use crate::base::{Matrix, MatrixView, MatrixViewMut, Scalar}; use crate::base::{Matrix, MatrixView, MatrixViewMut, Scalar};
@ -365,34 +363,6 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorage<T, R, C>> ExactSizeIterat
} }
} }
impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> Producer for ColumnIter<'a, T, R, Cols, S>
where
T: Send + Sync + Debug + PartialEq + Clone + 'static,
S: Sync,
{
type Item = MatrixSlice<'a, T, R, U1, S::RStride, S::CStride>;
type IntoIter = ColumnIter<'a, T, R, Cols, S>;
fn split_at(self, index: usize) -> (Self, Self) {
// the index is relative to the size of this current iterator
// it will always start at zero so it serves as an offset
let left = Self {
mat: self.mat,
range: self.range.start..(self.range.start + index),
};
let right = Self {
mat: self.mat,
range: (self.range.start + index)..self.range.end,
};
(left, right)
}
fn into_iter(self) -> Self::IntoIter {
self
}
}
/// An iterator through the mutable columns of a matrix. /// An iterator through the mutable columns of a matrix.
#[derive(Debug)] #[derive(Debug)]
pub struct ColumnIterMut<'a, T, R: Dim, C: Dim, S: RawStorageMut<T, R, C>> { pub struct ColumnIterMut<'a, T, R: Dim, C: Dim, S: RawStorageMut<T, R, C>> {
@ -469,39 +439,76 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> DoubleEndedI
} }
} }
impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Producer /// implementations for parallel iteration with rayon
for ColumnIterMut<'a, T, R, C, S> #[cfg(feature = "rayon")]
where mod parallel {
T: Send + Sync + Debug + PartialEq + Clone, use super::*;
S: Send + Sync, use rayon::iter::plumbing::Producer;
{
type Item = MatrixSliceMut<'a, T, R, U1, S::RStride, S::CStride>;
type IntoIter = ColumnIterMut<'a, T, R, C, S>;
fn into_iter(self) -> Self::IntoIter { impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> Producer for ColumnIter<'a, T, R, Cols, S>
self where
T: Send + Sync + Debug + PartialEq + Clone + 'static,
S: Sync,
{
type Item = MatrixSlice<'a, T, R, U1, S::RStride, S::CStride>;
type IntoIter = ColumnIter<'a, T, R, Cols, S>;
fn split_at(self, index: usize) -> (Self, Self) {
// the index is relative to the size of this current iterator
// it will always start at zero so it serves as an offset
let left = Self {
mat: self.mat,
range: self.range.start..(self.range.start + index),
};
let right = Self {
mat: self.mat,
range: (self.range.start + index)..self.range.end,
};
(left, right)
}
fn into_iter(self) -> Self::IntoIter {
self
}
} }
fn split_at(self, index: usize) -> (Self, Self) { impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Producer
// the index is relative to the size of this current iterator for ColumnIterMut<'a, T, R, C, S>
// it will always start at zero so it serves as an offset where
T: Send + Sync + Debug + PartialEq + Clone,
S: Send + Sync,
{
type Item = MatrixSliceMut<'a, T, R, U1, S::RStride, S::CStride>;
type IntoIter = ColumnIterMut<'a, T, R, C, S>;
let left = Self { fn into_iter(self) -> Self::IntoIter {
mat: self.mat, self
range: self.range.start..(self.range.start + index), }
phantom: Default::default(),
};
let right = Self { fn split_at(self, index: usize) -> (Self, Self) {
mat: self.mat, // the index is relative to the size of this current iterator
range: (self.range.start + index)..self.range.end, // it will always start at zero so it serves as an offset
phantom: Default::default(),
}; let left = Self {
(left, right) mat: self.mat,
range: self.range.start..(self.range.start + index),
phantom: Default::default(),
};
let right = Self {
mat: self.mat,
range: (self.range.start + index)..self.range.end,
phantom: Default::default(),
};
(left, right)
}
}
/// this implementation is safe because we are enforcing exclusive access
/// to the columns through the active range of the iterator
unsafe impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Send
for ColumnIterMut<'a, T, R, C, S>
{
} }
} }
unsafe impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Send
for ColumnIterMut<'a, T, R, C, S>
{
}

View File

@ -42,6 +42,7 @@ mod min_max;
/// Mechanisms for working with values that may not be initialized. /// Mechanisms for working with values that may not be initialized.
pub mod uninit; pub mod uninit;
#[cfg(feature = "rayon")]
pub mod par_iter; pub mod par_iter;
#[cfg(feature = "rkyv-serialize-no-std")] #[cfg(feature = "rkyv-serialize-no-std")]

View File

@ -1200,6 +1200,7 @@ fn column_iteration_double_ended() {
} }
#[test] #[test]
#[cfg(feature = "rayon")]
fn parallel_column_iteration() { fn parallel_column_iteration() {
use nalgebra::dmatrix; use nalgebra::dmatrix;
use rayon::prelude::*; use rayon::prelude::*;
@ -1221,6 +1222,7 @@ fn parallel_column_iteration() {
} }
#[test] #[test]
#[cfg(feature = "rayon")]
fn colum_iteration_mut_double_ended() { fn colum_iteration_mut_double_ended() {
let dmat = nalgebra::dmatrix![ let dmat = nalgebra::dmatrix![
13,14,15,16,17; 13,14,15,16,17;
@ -1239,6 +1241,7 @@ fn colum_iteration_mut_double_ended() {
} }
#[test] #[test]
#[cfg(feature = "rayon")]
fn parallel_column_iteration_mut() { fn parallel_column_iteration_mut() {
use rayon::prelude::*; use rayon::prelude::*;
let mut first = DMatrix::<f32>::zeros(400, 300); let mut first = DMatrix::<f32>::zeros(400, 300);

View File

@ -9,6 +9,10 @@ compile_error!(
Example: `cargo test --features debug,compare,rand,macros`" Example: `cargo test --features debug,compare,rand,macros`"
); );
// make sure to test the parallel iterators for all builds that do not require no_std
#[cfg(all(feature = "std", not(feature = "rayon")))]
compile_error!("Please additionally enable the `rayon` feature to compile and run the tests");
#[cfg(all(feature = "debug", feature = "compare", feature = "rand"))] #[cfg(all(feature = "debug", feature = "compare", feature = "rand"))]
#[macro_use] #[macro_use]
extern crate approx; extern crate approx;