diff --git a/src/base/par_iter.rs b/src/base/par_iter.rs index 3854e678..3109e1ac 100644 --- a/src/base/par_iter.rs +++ b/src/base/par_iter.rs @@ -1,15 +1,12 @@ //! this module implements parallelators to make matrices work with //! the rayon crate seamlessly -use core::fmt::Debug; -use rayon::{ - iter::plumbing::{bridge}, - prelude::*, -}; use crate::{ - iter::{ColumnIter, ColumnIterMut}, Dim, Matrix, MatrixSlice, MatrixSliceMut, - RawStorage, RawStorageMut, U1, + iter::{ColumnIter, ColumnIterMut}, + Dim, Matrix, MatrixSlice, MatrixSliceMut, RawStorage, RawStorageMut, U1, }; +use core::fmt::Debug; +use rayon::{iter::plumbing::bridge, prelude::*}; /// A rayon parallel iterator over the colums of a matrix pub struct ParColumnIter<'a, T, R: Dim, Cols: Dim, S: RawStorage> { @@ -78,29 +75,42 @@ where } /// A rayon parallel iterator through the mutable columns of a matrix -pub struct ParColumnIterMut<'a,T,R:Dim ,Cols:Dim, S:RawStorage+RawStorageMut> { - mat : &'a mut Matrix, +pub struct ParColumnIterMut< + 'a, + T, + R: Dim, + Cols: Dim, + S: RawStorage + RawStorageMut, +> { + mat: &'a mut Matrix, } -impl<'a,T,R,Cols,S> ParColumnIterMut<'a,T,R,Cols,S> -where R: Dim, Cols : Dim, S:RawStorage + RawStorageMut { +impl<'a, T, R, Cols, S> ParColumnIterMut<'a, T, R, Cols, S> +where + R: Dim, + Cols: Dim, + S: RawStorage + RawStorageMut, +{ /// create a new parallel iterator for the given matrix - fn new(mat : &'a mut Matrix) -> Self { - Self { - mat, - } + fn new(mat: &'a mut Matrix) -> Self { + Self { mat } } } -impl<'a,T,R,Cols,S> ParallelIterator for ParColumnIterMut<'a,T,R,Cols,S> -where R: Dim, Cols : Dim, S:RawStorage + RawStorageMut, -T : Send + Sync + Debug + PartialEq + Clone + 'static, -S : Send + Sync { +impl<'a, T, R, Cols, S> ParallelIterator for ParColumnIterMut<'a, T, R, Cols, S> +where + R: Dim, + Cols: Dim, + S: RawStorage + RawStorageMut, + T: Send + Sync + Debug + PartialEq + Clone + 'static, + S: Send + Sync, +{ type Item = MatrixSliceMut<'a, T, R, U1, S::RStride, S::CStride>; fn drive_unindexed(self, consumer: C) -> C::Result - where - C: rayon::iter::plumbing::UnindexedConsumer { - bridge(self,consumer) + where + C: rayon::iter::plumbing::UnindexedConsumer, + { + bridge(self, consumer) } fn opt_len(&self) -> Option { @@ -108,26 +118,33 @@ S : Send + Sync { } } - -impl<'a,T,R,Cols,S> IndexedParallelIterator for ParColumnIterMut<'a,T,R,Cols,S> -where R: Dim, Cols : Dim, S:RawStorage + RawStorageMut, -T : Send + Sync + Debug + PartialEq + Clone + 'static, -S : Send + Sync { +impl<'a, T, R, Cols, S> IndexedParallelIterator for ParColumnIterMut<'a, T, R, Cols, S> +where + R: Dim, + Cols: Dim, + S: RawStorage + RawStorageMut, + T: Send + Sync + Debug + PartialEq + Clone + 'static, + S: Send + Sync, +{ fn drive>(self, consumer: C) -> C::Result { - bridge(self,consumer) + bridge(self, consumer) } fn len(&self) -> usize { self.mat.ncols() } - fn with_producer>(self, callback: CB) -> CB::Output { + fn with_producer>( + self, + callback: CB, + ) -> CB::Output { let producer = ColumnIterMut::new(self.mat); callback.callback(producer) } } -impl<'a, T, R: Dim, Cols: Dim, S: RawStorage + RawStorageMut> Matrix +impl<'a, T, R: Dim, Cols: Dim, S: RawStorage + RawStorageMut> + Matrix where T: Send + Sync + Clone + Debug + PartialEq + 'static, S: Sync, diff --git a/tests/core/matrix.rs b/tests/core/matrix.rs index a4ad5057..5021df8b 100644 --- a/tests/core/matrix.rs +++ b/tests/core/matrix.rs @@ -1141,111 +1141,116 @@ fn omatrix_to_string() { fn column_iteration() { // dynamic matrix let dmat = nalgebra::dmatrix![ - 13,14,15; - 23,24,25; - 33,34,35; - ]; + 13,14,15; + 23,24,25; + 33,34,35; + ]; let mut col_iter = dmat.column_iter(); - assert_eq!(col_iter.next(),Some(dmat.column(0))); - assert_eq!(col_iter.next(),Some(dmat.column(1))); - assert_eq!(col_iter.next(),Some(dmat.column(2))); - assert_eq!(col_iter.next(),None); + assert_eq!(col_iter.next(), Some(dmat.column(0))); + assert_eq!(col_iter.next(), Some(dmat.column(1))); + assert_eq!(col_iter.next(), Some(dmat.column(2))); + assert_eq!(col_iter.next(), None); // statically sized matrix let smat: nalgebra::SMatrix = nalgebra::matrix![1.0, 2.0; 3.0, 4.0]; let mut col_iter = smat.column_iter(); - assert_eq!(col_iter.next(),Some(smat.column(0))); - assert_eq!(col_iter.next(),Some(smat.column(1))); - assert_eq!(col_iter.next(),None); + assert_eq!(col_iter.next(), Some(smat.column(0))); + assert_eq!(col_iter.next(), Some(smat.column(1))); + assert_eq!(col_iter.next(), None); } #[test] fn column_iteration_mut() { let mut dmat = nalgebra::dmatrix![ - 13,14,15; - 23,24,25; - 33,34,35; - ]; + 13,14,15; + 23,24,25; + 33,34,35; + ]; let mut cloned = dmat.clone(); let mut col_iter = dmat.column_iter_mut(); - assert_eq!(col_iter.next(),Some(cloned.column_mut(0))); - assert_eq!(col_iter.next(),Some(cloned.column_mut(1))); - assert_eq!(col_iter.next(),Some(cloned.column_mut(2))); - assert_eq!(col_iter.next(),None); + assert_eq!(col_iter.next(), Some(cloned.column_mut(0))); + assert_eq!(col_iter.next(), Some(cloned.column_mut(1))); + assert_eq!(col_iter.next(), Some(cloned.column_mut(2))); + assert_eq!(col_iter.next(), None); // statically sized matrix let mut smat: nalgebra::SMatrix = nalgebra::matrix![1.0, 2.0; 3.0, 4.0]; let mut cloned = smat.clone(); let mut col_iter = smat.column_iter_mut(); - assert_eq!(col_iter.next(),Some(cloned.column_mut(0))); - assert_eq!(col_iter.next(),Some(cloned.column_mut(1))); - assert_eq!(col_iter.next(),None); + assert_eq!(col_iter.next(), Some(cloned.column_mut(0))); + assert_eq!(col_iter.next(), Some(cloned.column_mut(1))); + assert_eq!(col_iter.next(), None); } #[test] fn column_iteration_double_ended() { let dmat = nalgebra::dmatrix![ - 13,14,15,16,17; - 23,24,25,26,27; - 33,34,35,36,37; - ]; + 13,14,15,16,17; + 23,24,25,26,27; + 33,34,35,36,37; + ]; let mut col_iter = dmat.column_iter(); - assert_eq!(col_iter.next(),Some(dmat.column(0))); - assert_eq!(col_iter.next(),Some(dmat.column(1))); - assert_eq!(col_iter.next_back(),Some(dmat.column(4))); - assert_eq!(col_iter.next_back(),Some(dmat.column(3))); - assert_eq!(col_iter.next(),Some(dmat.column(2))); - assert_eq!(col_iter.next_back(),None); - assert_eq!(col_iter.next(),None); + assert_eq!(col_iter.next(), Some(dmat.column(0))); + assert_eq!(col_iter.next(), Some(dmat.column(1))); + assert_eq!(col_iter.next_back(), Some(dmat.column(4))); + assert_eq!(col_iter.next_back(), Some(dmat.column(3))); + assert_eq!(col_iter.next(), Some(dmat.column(2))); + assert_eq!(col_iter.next_back(), None); + assert_eq!(col_iter.next(), None); } #[test] fn parallel_column_iteration() { + use nalgebra::dmatrix; use rayon::prelude::*; - use nalgebra::{dmatrix,dvector}; - let dmat : DMatrix = dmatrix![ - 13.,14.; - 23.,24.; - 33.,34.; - ]; + let dmat: DMatrix = dmatrix![ + 13.,14.; + 23.,24.; + 33.,34.; + ]; let cloned = dmat.clone(); // test that correct columns are iterated over - dmat.par_column_iter().enumerate().for_each(|(idx,col)| { - assert_eq!(col,cloned.column(idx)); + dmat.par_column_iter().enumerate().for_each(|(idx, col)| { + assert_eq!(col, cloned.column(idx)); }); // test that a more complex expression produces the same // result as the serial equivalent - let par_result : f64 = dmat.par_column_iter().map(|col| col.norm()).sum(); - let ser_result : f64= dmat.column_iter().map(|col| col.norm()).sum(); - assert_eq!(par_result,ser_result); + let par_result: f64 = dmat.par_column_iter().map(|col| col.norm()).sum(); + let ser_result: f64 = dmat.column_iter().map(|col| col.norm()).sum(); + assert_eq!(par_result, ser_result); } - #[test] fn colum_iteration_mut_double_ended() { let dmat = nalgebra::dmatrix![ - 13,14,15,16,17; - 23,24,25,26,27; - 33,34,35,36,37; - ]; + 13,14,15,16,17; + 23,24,25,26,27; + 33,34,35,36,37; + ]; let cloned = dmat.clone(); let mut col_iter = dmat.column_iter(); - assert_eq!(col_iter.next(),Some(cloned.column(0))); - assert_eq!(col_iter.next(),Some(cloned.column(1))); - assert_eq!(col_iter.next_back(),Some(cloned.column(4))); - assert_eq!(col_iter.next_back(),Some(cloned.column(3))); - assert_eq!(col_iter.next(),Some(cloned.column(2))); - assert_eq!(col_iter.next_back(),None); - assert_eq!(col_iter.next(),None); + assert_eq!(col_iter.next(), Some(cloned.column(0))); + assert_eq!(col_iter.next(), Some(cloned.column(1))); + assert_eq!(col_iter.next_back(), Some(cloned.column(4))); + assert_eq!(col_iter.next_back(), Some(cloned.column(3))); + assert_eq!(col_iter.next(), Some(cloned.column(2))); + assert_eq!(col_iter.next_back(), None); + assert_eq!(col_iter.next(), None); } #[test] fn parallel_column_iteration_mut() { use rayon::prelude::*; - let mut first = DMatrix::::zeros(400,300); - let mut second = DMatrix::::zeros(400,300); - first.column_iter_mut().enumerate().for_each(|(idx,mut col)|col[idx]=1.); - second.par_column_iter_mut().enumerate().for_each(|(idx,mut col)| col[idx]=1.); - assert_eq!(first,second); - assert_eq!(second,DMatrix::identity(400,300)); + let mut first = DMatrix::::zeros(400, 300); + let mut second = DMatrix::::zeros(400, 300); + first + .column_iter_mut() + .enumerate() + .for_each(|(idx, mut col)| col[idx] = 1.); + second + .par_column_iter_mut() + .enumerate() + .for_each(|(idx, mut col)| col[idx] = 1.); + assert_eq!(first, second); + assert_eq!(second, DMatrix::identity(400, 300)); }