apply fmt

This commit is contained in:
geo-ant 2022-10-21 08:51:41 +02:00 committed by Sébastien Crozet
parent daade1cf5e
commit a4e28a136e
2 changed files with 114 additions and 92 deletions

View File

@ -1,15 +1,12 @@
//! this module implements parallelators to make matrices work with //! this module implements parallelators to make matrices work with
//! the rayon crate seamlessly //! the rayon crate seamlessly
use core::fmt::Debug;
use rayon::{
iter::plumbing::{bridge},
prelude::*,
};
use crate::{ use crate::{
iter::{ColumnIter, ColumnIterMut}, Dim, Matrix, MatrixSlice, MatrixSliceMut, iter::{ColumnIter, ColumnIterMut},
RawStorage, RawStorageMut, U1, 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 /// A rayon parallel iterator over the colums of a matrix
pub struct ParColumnIter<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> { pub struct ParColumnIter<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> {
@ -78,28 +75,41 @@ where
} }
/// A rayon parallel iterator through the mutable columns of a matrix /// A rayon parallel iterator through the mutable columns of a matrix
pub struct ParColumnIterMut<'a,T,R:Dim ,Cols:Dim, S:RawStorage<T,R,Cols>+RawStorageMut<T,R,Cols>> { pub struct ParColumnIterMut<
'a,
T,
R: Dim,
Cols: Dim,
S: RawStorage<T, R, Cols> + RawStorageMut<T, R, Cols>,
> {
mat: &'a mut Matrix<T, R, Cols, S>, mat: &'a mut Matrix<T, R, Cols, S>,
} }
impl<'a, T, R, Cols, S> ParColumnIterMut<'a, T, R, Cols, S> impl<'a, T, R, Cols, S> ParColumnIterMut<'a, T, R, Cols, S>
where R: Dim, Cols : Dim, S:RawStorage<T,R,Cols> + RawStorageMut<T,R,Cols> { where
R: Dim,
Cols: Dim,
S: RawStorage<T, R, Cols> + RawStorageMut<T, R, Cols>,
{
/// create a new parallel iterator for the given matrix /// create a new parallel iterator for the given matrix
fn new(mat: &'a mut Matrix<T, R, Cols, S>) -> Self { fn new(mat: &'a mut Matrix<T, R, Cols, S>) -> Self {
Self { Self { mat }
mat,
}
} }
} }
impl<'a, T, R, Cols, S> ParallelIterator for ParColumnIterMut<'a, T, R, Cols, S> impl<'a, T, R, Cols, S> ParallelIterator for ParColumnIterMut<'a, T, R, Cols, S>
where R: Dim, Cols : Dim, S:RawStorage<T,R,Cols> + RawStorageMut<T,R,Cols>, where
R: Dim,
Cols: Dim,
S: RawStorage<T, R, Cols> + RawStorageMut<T, R, Cols>,
T: Send + Sync + Debug + PartialEq + Clone + 'static, T: Send + Sync + Debug + PartialEq + Clone + 'static,
S : Send + Sync { S: Send + Sync,
{
type Item = MatrixSliceMut<'a, T, R, U1, S::RStride, S::CStride>; type Item = MatrixSliceMut<'a, T, R, U1, S::RStride, S::CStride>;
fn drive_unindexed<C>(self, consumer: C) -> C::Result fn drive_unindexed<C>(self, consumer: C) -> C::Result
where where
C: rayon::iter::plumbing::UnindexedConsumer<Self::Item> { C: rayon::iter::plumbing::UnindexedConsumer<Self::Item>,
{
bridge(self, consumer) bridge(self, consumer)
} }
@ -108,11 +118,14 @@ S : Send + Sync {
} }
} }
impl<'a, T, R, Cols, S> IndexedParallelIterator for ParColumnIterMut<'a, T, R, Cols, S> impl<'a, T, R, Cols, S> IndexedParallelIterator for ParColumnIterMut<'a, T, R, Cols, S>
where R: Dim, Cols : Dim, S:RawStorage<T,R,Cols> + RawStorageMut<T,R,Cols>, where
R: Dim,
Cols: Dim,
S: RawStorage<T, R, Cols> + RawStorageMut<T, R, Cols>,
T: Send + Sync + Debug + PartialEq + Clone + 'static, T: Send + Sync + Debug + PartialEq + Clone + 'static,
S : Send + Sync { S: Send + Sync,
{
fn drive<C: rayon::iter::plumbing::Consumer<Self::Item>>(self, consumer: C) -> C::Result { fn drive<C: rayon::iter::plumbing::Consumer<Self::Item>>(self, consumer: C) -> C::Result {
bridge(self, consumer) bridge(self, consumer)
} }
@ -121,13 +134,17 @@ S : Send + Sync {
self.mat.ncols() self.mat.ncols()
} }
fn with_producer<CB: rayon::iter::plumbing::ProducerCallback<Self::Item>>(self, callback: CB) -> CB::Output { fn with_producer<CB: rayon::iter::plumbing::ProducerCallback<Self::Item>>(
self,
callback: CB,
) -> CB::Output {
let producer = ColumnIterMut::new(self.mat); let producer = ColumnIterMut::new(self.mat);
callback.callback(producer) callback.callback(producer)
} }
} }
impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols> + RawStorageMut<T,R,Cols>> Matrix<T, R, Cols, S> impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols> + RawStorageMut<T, R, Cols>>
Matrix<T, R, Cols, S>
where where
T: Send + Sync + Clone + Debug + PartialEq + 'static, T: Send + Sync + Clone + Debug + PartialEq + 'static,
S: Sync, S: Sync,

View File

@ -1201,8 +1201,8 @@ fn column_iteration_double_ended() {
#[test] #[test]
fn parallel_column_iteration() { fn parallel_column_iteration() {
use nalgebra::dmatrix;
use rayon::prelude::*; use rayon::prelude::*;
use nalgebra::{dmatrix,dvector};
let dmat: DMatrix<f64> = dmatrix![ let dmat: DMatrix<f64> = dmatrix![
13.,14.; 13.,14.;
23.,24.; 23.,24.;
@ -1220,7 +1220,6 @@ fn parallel_column_iteration() {
assert_eq!(par_result, ser_result); assert_eq!(par_result, ser_result);
} }
#[test] #[test]
fn colum_iteration_mut_double_ended() { fn colum_iteration_mut_double_ended() {
let dmat = nalgebra::dmatrix![ let dmat = nalgebra::dmatrix![
@ -1244,8 +1243,14 @@ 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);
let mut second = DMatrix::<f32>::zeros(400, 300); let mut second = DMatrix::<f32>::zeros(400, 300);
first.column_iter_mut().enumerate().for_each(|(idx,mut col)|col[idx]=1.); first
second.par_column_iter_mut().enumerate().for_each(|(idx,mut col)| col[idx]=1.); .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!(first, second);
assert_eq!(second, DMatrix::identity(400, 300)); assert_eq!(second, DMatrix::identity(400, 300));
} }