From 85e7551c817f2c923bf0c902117283689ad66b05 Mon Sep 17 00:00:00 2001 From: geo-ant <54497890+geo-ant@users.noreply.github.com> Date: Mon, 24 Oct 2022 08:39:03 +0200 Subject: [PATCH] feature gate functionality --- .github/workflows/nalgebra-ci-build.yml | 2 +- Cargo.toml | 3 +- src/base/iter.rs | 125 +++++++++++++----------- src/base/mod.rs | 1 + tests/core/matrix.rs | 3 + tests/lib.rs | 4 + 6 files changed, 77 insertions(+), 61 deletions(-) diff --git a/.github/workflows/nalgebra-ci-build.yml b/.github/workflows/nalgebra-ci-build.yml index 9e500084..d7027127 100644 --- a/.github/workflows/nalgebra-ci-build.yml +++ b/.github/workflows/nalgebra-ci-build.yml @@ -61,7 +61,7 @@ jobs: steps: - uses: actions/checkout@v2 - 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: runs-on: ubuntu-latest steps: diff --git a/Cargo.toml b/Cargo.toml index 48262dce..b8f25b5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,6 +33,7 @@ libm = [ "simba/libm" ] libm-force = [ "simba/libm_force" ] macros = [ "nalgebra-macros" ] cuda = [ "cust_core", "simba/cuda" ] +rayon = [ "dep:rayon" ] # Conversion convert-mint = [ "mint" ] @@ -101,7 +102,7 @@ glam020 = { package = "glam", version = "0.20", optional = true } glam021 = { package = "glam", version = "0.21", optional = true } glam022 = { package = "glam", version = "0.22", 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] serde_json = "1.0" diff --git a/src/base/iter.rs b/src/base/iter.rs index 925fefbb..0ecd057c 100644 --- a/src/base/iter.rs +++ b/src/base/iter.rs @@ -6,8 +6,6 @@ use std::iter::FusedIterator; use std::marker::PhantomData; use std::mem; -use rayon::iter::plumbing::Producer; - use crate::base::dimension::{Dim, U1}; use crate::base::storage::{RawStorage, RawStorageMut}; use crate::base::{Matrix, MatrixView, MatrixViewMut, Scalar}; @@ -365,34 +363,6 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorage> ExactSizeIterat } } -impl<'a, T, R: Dim, Cols: Dim, S: RawStorage> 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. #[derive(Debug)] pub struct ColumnIterMut<'a, T, R: Dim, C: Dim, S: RawStorageMut> { @@ -469,39 +439,76 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut> DoubleEndedI } } -impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut> Producer - for ColumnIterMut<'a, T, R, C, S> -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>; +/// implementations for parallel iteration with rayon +#[cfg(feature = "rayon")] +mod parallel { + use super::*; + use rayon::iter::plumbing::Producer; - fn into_iter(self) -> Self::IntoIter { - self + impl<'a, T, R: Dim, Cols: Dim, S: RawStorage> 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 + } } - 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 + impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut> Producer + for ColumnIterMut<'a, T, R, C, S> + 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 { - mat: self.mat, - range: self.range.start..(self.range.start + index), - phantom: Default::default(), - }; + fn into_iter(self) -> Self::IntoIter { + self + } - let right = Self { - mat: self.mat, - range: (self.range.start + index)..self.range.end, - phantom: Default::default(), - }; - (left, right) + 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), + 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> Send + for ColumnIterMut<'a, T, R, C, S> + { } } - -unsafe impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut> Send - for ColumnIterMut<'a, T, R, C, S> -{ -} diff --git a/src/base/mod.rs b/src/base/mod.rs index b828d0a1..0f09cc33 100644 --- a/src/base/mod.rs +++ b/src/base/mod.rs @@ -42,6 +42,7 @@ mod min_max; /// Mechanisms for working with values that may not be initialized. pub mod uninit; +#[cfg(feature = "rayon")] pub mod par_iter; #[cfg(feature = "rkyv-serialize-no-std")] diff --git a/tests/core/matrix.rs b/tests/core/matrix.rs index 5021df8b..150eb678 100644 --- a/tests/core/matrix.rs +++ b/tests/core/matrix.rs @@ -1200,6 +1200,7 @@ fn column_iteration_double_ended() { } #[test] +#[cfg(feature = "rayon")] fn parallel_column_iteration() { use nalgebra::dmatrix; use rayon::prelude::*; @@ -1221,6 +1222,7 @@ fn parallel_column_iteration() { } #[test] +#[cfg(feature = "rayon")] fn colum_iteration_mut_double_ended() { let dmat = nalgebra::dmatrix![ 13,14,15,16,17; @@ -1239,6 +1241,7 @@ fn colum_iteration_mut_double_ended() { } #[test] +#[cfg(feature = "rayon")] fn parallel_column_iteration_mut() { use rayon::prelude::*; let mut first = DMatrix::::zeros(400, 300); diff --git a/tests/lib.rs b/tests/lib.rs index 546aa8a7..fb13b3ae 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -9,6 +9,10 @@ compile_error!( 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"))] #[macro_use] extern crate approx;