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:
- 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:

View File

@ -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"

View File

@ -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<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.
#[derive(Debug)]
pub struct ColumnIterMut<'a, T, R: Dim, C: Dim, S: RawStorageMut<T, R, C>> {
@ -469,6 +439,40 @@ impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> DoubleEndedI
}
}
/// implementations for parallel iteration with rayon
#[cfg(feature = "rayon")]
mod parallel {
use super::*;
use rayon::iter::plumbing::Producer;
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
}
}
impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Producer
for ColumnIterMut<'a, T, R, C, S>
where
@ -501,7 +505,10 @@ where
}
}
/// 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>
{
}
}

View File

@ -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")]

View File

@ -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::<f32>::zeros(400, 300);

View File

@ -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;