2022-11-13 00:54:01 +08:00
|
|
|
//! Parallel iterators for matrices compatible with rayon.
|
2022-10-18 15:03:52 +08:00
|
|
|
|
2022-11-13 21:54:26 +08:00
|
|
|
//only enables the `doc_cfg` feature when
|
|
|
|
// the `docsrs` configuration attribute is defined
|
|
|
|
#![cfg_attr(docsrs, feature(doc_cfg))]
|
|
|
|
|
2022-10-18 15:03:52 +08:00
|
|
|
use crate::{
|
2022-10-21 14:51:41 +08:00
|
|
|
iter::{ColumnIter, ColumnIterMut},
|
2022-11-13 00:54:01 +08:00
|
|
|
Dim, Matrix, MatrixSlice, MatrixSliceMut, RawStorage, RawStorageMut, Scalar, U1,
|
|
|
|
};
|
2022-10-21 14:51:41 +08:00
|
|
|
use rayon::{iter::plumbing::bridge, prelude::*};
|
2022-10-18 15:03:52 +08:00
|
|
|
|
2022-11-13 02:01:28 +08:00
|
|
|
/// A rayon parallel iterator over the colums of a matrix. It is created
|
|
|
|
/// using the [`par_column_iter`] method of [`Matrix`].
|
|
|
|
///
|
|
|
|
/// [`par_column_iter`]: crate::Matrix::par_column_iter
|
|
|
|
/// [`Matrix`]: crate::Matrix
|
2022-11-14 00:01:15 +08:00
|
|
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
|
2022-10-18 15:03:52 +08:00
|
|
|
pub struct ParColumnIter<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> {
|
|
|
|
mat: &'a Matrix<T, R, Cols, S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> ParColumnIter<'a, T, R, Cols, S> {
|
2022-10-21 14:44:35 +08:00
|
|
|
/// create a new parallel iterator for the given matrix
|
2022-10-18 15:03:52 +08:00
|
|
|
fn new(matrix: &'a Matrix<T, R, Cols, S>) -> Self {
|
|
|
|
Self { mat: matrix }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 00:01:15 +08:00
|
|
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
|
2022-10-18 15:03:52 +08:00
|
|
|
impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> ParallelIterator
|
|
|
|
for ParColumnIter<'a, T, R, Cols, S>
|
|
|
|
where
|
2022-11-13 00:37:18 +08:00
|
|
|
T: Sync + Send + Scalar,
|
2022-10-18 15:03:52 +08:00
|
|
|
S: Sync,
|
|
|
|
{
|
|
|
|
type Item = MatrixSlice<'a, T, R, U1, S::RStride, S::CStride>;
|
|
|
|
|
|
|
|
fn drive_unindexed<Consumer>(self, consumer: Consumer) -> Consumer::Result
|
|
|
|
where
|
|
|
|
Consumer: rayon::iter::plumbing::UnindexedConsumer<Self::Item>,
|
|
|
|
{
|
|
|
|
bridge(self, consumer)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn opt_len(&self) -> Option<usize> {
|
|
|
|
Some(self.mat.ncols())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 00:01:15 +08:00
|
|
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
|
2022-10-18 15:03:52 +08:00
|
|
|
impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> IndexedParallelIterator
|
|
|
|
for ParColumnIter<'a, T, R, Cols, S>
|
|
|
|
where
|
2022-11-13 00:37:18 +08:00
|
|
|
T: Send + Sync + Scalar,
|
2022-10-18 15:03:52 +08:00
|
|
|
S: Sync,
|
|
|
|
{
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.mat.ncols()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn drive<C: rayon::iter::plumbing::Consumer<Self::Item>>(self, consumer: C) -> C::Result {
|
|
|
|
bridge(self, consumer)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn with_producer<CB: rayon::iter::plumbing::ProducerCallback<Self::Item>>(
|
|
|
|
self,
|
|
|
|
callback: CB,
|
|
|
|
) -> CB::Output {
|
|
|
|
let producer = ColumnIter::new(self.mat);
|
|
|
|
callback.callback(producer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 00:01:15 +08:00
|
|
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
|
2022-10-21 15:02:28 +08:00
|
|
|
impl<T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> Matrix<T, R, Cols, S>
|
2022-10-18 15:03:52 +08:00
|
|
|
where
|
2022-11-13 00:37:18 +08:00
|
|
|
T: Send + Sync + Scalar,
|
2022-10-18 15:03:52 +08:00
|
|
|
S: Sync,
|
|
|
|
{
|
2022-10-21 14:44:35 +08:00
|
|
|
/// Iterate through the columns of the matrix in parallel using rayon.
|
2022-11-13 21:54:26 +08:00
|
|
|
/// This iterates over *immutable* references ot the columns of the matrix,
|
|
|
|
/// if *mutable* access to the columns is required, use [`par_column_iter_mut`]
|
|
|
|
/// instead.
|
|
|
|
///
|
2022-11-14 00:01:15 +08:00
|
|
|
/// # Example
|
2022-11-13 21:54:26 +08:00
|
|
|
/// Using parallel column iterators to calculate the sum of the maximum
|
|
|
|
/// elements in each column:
|
|
|
|
/// ```
|
|
|
|
/// use nalgebra::{dmatrix,DMatrix};
|
|
|
|
/// use rayon::prelude::*;
|
|
|
|
///
|
|
|
|
/// let matrix : DMatrix<f64> =
|
|
|
|
/// nalgebra::dmatrix![1.,0.,5.;
|
|
|
|
/// 2.,4.,1.;
|
|
|
|
/// 3.,2.,2.;];
|
|
|
|
/// let sum_of_max :f64 =
|
|
|
|
/// matrix
|
|
|
|
/// .par_column_iter()
|
|
|
|
/// .map(|col|col.max())
|
|
|
|
/// .sum();
|
|
|
|
///
|
|
|
|
/// assert_eq!(sum_of_max,3.+4.+5.);
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [`par_column_iter_mut`]: crate::Matrix::par_column_iter_mut
|
2022-10-19 15:51:46 +08:00
|
|
|
pub fn par_column_iter(&self) -> ParColumnIter<'_, T, R, Cols, S> {
|
2022-10-18 15:03:52 +08:00
|
|
|
ParColumnIter::new(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 00:01:15 +08:00
|
|
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
|
2022-10-21 14:44:35 +08:00
|
|
|
/// A rayon parallel iterator through the mutable columns of a matrix
|
2022-10-21 14:51:41 +08:00
|
|
|
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>,
|
2022-10-18 15:03:52 +08:00
|
|
|
}
|
|
|
|
|
2022-11-14 00:01:15 +08:00
|
|
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
|
2022-10-21 14:51:41 +08:00
|
|
|
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>,
|
|
|
|
{
|
2022-10-21 14:44:35 +08:00
|
|
|
/// create a new parallel iterator for the given matrix
|
2022-10-21 14:51:41 +08:00
|
|
|
fn new(mat: &'a mut Matrix<T, R, Cols, S>) -> Self {
|
|
|
|
Self { mat }
|
2022-10-18 15:03:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 00:01:15 +08:00
|
|
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
|
2022-10-21 14:51:41 +08:00
|
|
|
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>,
|
2022-11-13 00:37:18 +08:00
|
|
|
T: Send + Sync + Scalar,
|
2022-10-21 14:51:41 +08:00
|
|
|
S: Send + Sync,
|
|
|
|
{
|
2022-10-18 15:03:52 +08:00
|
|
|
type Item = MatrixSliceMut<'a, T, R, U1, S::RStride, S::CStride>;
|
|
|
|
fn drive_unindexed<C>(self, consumer: C) -> C::Result
|
2022-10-21 14:51:41 +08:00
|
|
|
where
|
|
|
|
C: rayon::iter::plumbing::UnindexedConsumer<Self::Item>,
|
|
|
|
{
|
|
|
|
bridge(self, consumer)
|
2022-10-18 15:03:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn opt_len(&self) -> Option<usize> {
|
|
|
|
Some(self.mat.ncols())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 00:01:15 +08:00
|
|
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
|
2022-10-21 14:51:41 +08:00
|
|
|
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>,
|
2022-11-13 00:37:18 +08:00
|
|
|
T: Send + Sync + Scalar,
|
2022-10-21 14:51:41 +08:00
|
|
|
S: Send + Sync,
|
|
|
|
{
|
2022-10-18 15:03:52 +08:00
|
|
|
fn drive<C: rayon::iter::plumbing::Consumer<Self::Item>>(self, consumer: C) -> C::Result {
|
2022-10-21 14:51:41 +08:00
|
|
|
bridge(self, consumer)
|
2022-10-18 15:03:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn len(&self) -> usize {
|
|
|
|
self.mat.ncols()
|
|
|
|
}
|
|
|
|
|
2022-10-21 14:51:41 +08:00
|
|
|
fn with_producer<CB: rayon::iter::plumbing::ProducerCallback<Self::Item>>(
|
|
|
|
self,
|
|
|
|
callback: CB,
|
|
|
|
) -> CB::Output {
|
2022-10-18 15:03:52 +08:00
|
|
|
let producer = ColumnIterMut::new(self.mat);
|
|
|
|
callback.callback(producer)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 00:01:15 +08:00
|
|
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "par-iter")))]
|
2022-10-21 15:02:28 +08:00
|
|
|
impl<T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols> + RawStorageMut<T, R, Cols>>
|
2022-10-21 14:51:41 +08:00
|
|
|
Matrix<T, R, Cols, S>
|
2022-10-18 15:03:52 +08:00
|
|
|
where
|
2022-11-13 00:37:18 +08:00
|
|
|
T: Send + Sync + Scalar,
|
2022-10-18 15:03:52 +08:00
|
|
|
S: Sync,
|
|
|
|
{
|
2022-11-14 00:01:15 +08:00
|
|
|
/// Mutably iterate through the columns of this matrix in parallel using rayon.
|
|
|
|
/// Allows mutable access to the columns in parallel using mutable references.
|
|
|
|
/// If mutable access to the columns is not required rather use [`par_column_iter`]
|
|
|
|
/// instead.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
/// Normalize each column of a matrix with respect to its own maximum value.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nalgebra::{dmatrix,DMatrix};
|
|
|
|
/// use rayon::prelude::*;
|
|
|
|
///
|
|
|
|
/// let mut matrix : DMatrix<f64> =
|
|
|
|
/// dmatrix![2.,4.,6.;
|
|
|
|
/// 1.,2.,3.];
|
|
|
|
/// matrix.par_column_iter_mut().for_each(|mut col| col /= col.max());
|
|
|
|
///
|
|
|
|
/// assert_eq!(matrix,
|
|
|
|
/// dmatrix![1. ,1. , 1.;
|
|
|
|
/// 0.5,0.5,0.5]);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [`par_column_iter`]: crate::Matrix::par_column_iter
|
2022-10-19 15:51:46 +08:00
|
|
|
pub fn par_column_iter_mut(&mut self) -> ParColumnIterMut<'_, T, R, Cols, S> {
|
2022-10-18 15:03:52 +08:00
|
|
|
ParColumnIterMut::new(self)
|
|
|
|
}
|
|
|
|
}
|