| 1 |
//! Parallel iterators for matrices compatible with rayon. |
| 2 |
|
| 3 |
use crate::{ |
| 4 |
Dim, Matrix, MatrixView, MatrixViewMut, RawStorage, RawStorageMut, Scalar, U1, |
| 5 |
iter::{ColumnIter, ColumnIterMut}, |
| 6 |
}; |
| 7 |
use rayon::iter::plumbing::Producer; |
| 8 |
use rayon::{iter::plumbing::bridge, prelude::*}; |
| 9 |
|
| 10 |
/// A rayon parallel iterator over the columns of a matrix. It is created |
| 11 |
/// using the [`par_column_iter`] method of [`Matrix`]. |
| 12 |
/// |
| 13 |
/// *Only available if compiled with the feature `rayon`.* |
| 14 |
/// |
| 15 |
/// [`par_column_iter`]: crate::Matrix::par_column_iter |
| 16 |
/// [`Matrix`]: crate::Matrix |
| 17 |
#[cfg_attr(docsrs, doc(cfg(feature = "rayon")))] |
| 18 |
pub struct ParColumnIter<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> { |
| 19 |
mat: &'a Matrix<T, R, Cols, S>, |
| 20 |
} |
| 21 |
|
| 22 |
impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> ParColumnIter<'a, T, R, Cols, S> { |
| 23 |
/// Create a new parallel iterator for the given matrix. |
| 24 |
fn new(matrix: &'a Matrix<T, R, Cols, S>) -> Self { |
| 25 |
Self { mat: matrix } |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
#[cfg_attr(docsrs, doc(cfg(feature = "rayon")))] |
| 30 |
impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> ParallelIterator |
| 31 |
for ParColumnIter<'a, T, R, Cols, S> |
| 32 |
where |
| 33 |
T: Sync + Send + Scalar, |
| 34 |
S: Sync, |
| 35 |
{ |
| 36 |
type Item = MatrixView<'a, T, R, U1, S::RStride, S::CStride>; |
| 37 |
|
| 38 |
fn drive_unindexed<Consumer>(self, consumer: Consumer) -> Consumer::Result |
| 39 |
where |
| 40 |
Consumer: rayon::iter::plumbing::UnindexedConsumer<Self::Item>, |
| 41 |
{ |
| 42 |
bridge(self, consumer) |
| 43 |
} |
| 44 |
|
| 45 |
fn opt_len(&self) -> Option<usize> { |
| 46 |
Some(self.mat.ncols()) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
#[cfg_attr(docsrs, doc(cfg(feature = "rayon")))] |
| 51 |
/// *Only available if compiled with the feature `rayon`.* |
| 52 |
impl<T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> IndexedParallelIterator |
| 53 |
for ParColumnIter<'_, T, R, Cols, S> |
| 54 |
where |
| 55 |
T: Send + Sync + Scalar, |
| 56 |
S: Sync, |
| 57 |
{ |
| 58 |
fn len(&self) -> usize { |
| 59 |
self.mat.ncols() |
| 60 |
} |
| 61 |
|
| 62 |
fn drive<C: rayon::iter::plumbing::Consumer<Self::Item>>(self, consumer: C) -> C::Result { |
| 63 |
bridge(self, consumer) |
| 64 |
} |
| 65 |
|
| 66 |
fn with_producer<CB: rayon::iter::plumbing::ProducerCallback<Self::Item>>( |
| 67 |
self, |
| 68 |
callback: CB, |
| 69 |
) -> CB::Output { |
| 70 |
let producer = ColumnProducer(ColumnIter::new(self.mat)); |
| 71 |
callback.callback(producer) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
#[cfg_attr(docsrs, doc(cfg(feature = "rayon")))] |
| 76 |
/// A rayon parallel iterator through the mutable columns of a matrix. |
| 77 |
/// *Only available if compiled with the feature `rayon`.* |
| 78 |
pub struct ParColumnIterMut< |
| 79 |
'a, |
| 80 |
T, |
| 81 |
R: Dim, |
| 82 |
Cols: Dim, |
| 83 |
S: RawStorage<T, R, Cols> + RawStorageMut<T, R, Cols>, |
| 84 |
> { |
| 85 |
mat: &'a mut Matrix<T, R, Cols, S>, |
| 86 |
} |
| 87 |
|
| 88 |
#[cfg_attr(docsrs, doc(cfg(feature = "rayon")))] |
| 89 |
/// *only available if compiled with the feature `rayon`* |
| 90 |
impl<'a, T, R, Cols, S> ParColumnIterMut<'a, T, R, Cols, S> |
| 91 |
where |
| 92 |
R: Dim, |
| 93 |
Cols: Dim, |
| 94 |
S: RawStorage<T, R, Cols> + RawStorageMut<T, R, Cols>, |
| 95 |
{ |
| 96 |
/// create a new parallel iterator for the given matrix. |
| 97 |
fn new(mat: &'a mut Matrix<T, R, Cols, S>) -> Self { |
| 98 |
Self { mat } |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
#[cfg_attr(docsrs, doc(cfg(feature = "rayon")))] |
| 103 |
/// *Only available if compiled with the feature `rayon`* |
| 104 |
impl<'a, T, R, Cols, S> ParallelIterator for ParColumnIterMut<'a, T, R, Cols, S> |
| 105 |
where |
| 106 |
R: Dim, |
| 107 |
Cols: Dim, |
| 108 |
S: RawStorage<T, R, Cols> + RawStorageMut<T, R, Cols>, |
| 109 |
T: Send + Sync + Scalar, |
| 110 |
S: Send + Sync, |
| 111 |
{ |
| 112 |
type Item = MatrixViewMut<'a, T, R, U1, S::RStride, S::CStride>; |
| 113 |
fn drive_unindexed<C>(self, consumer: C) -> C::Result |
| 114 |
where |
| 115 |
C: rayon::iter::plumbing::UnindexedConsumer<Self::Item>, |
| 116 |
{ |
| 117 |
bridge(self, consumer) |
| 118 |
} |
| 119 |
|
| 120 |
fn opt_len(&self) -> Option<usize> { |
| 121 |
Some(self.mat.ncols()) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
#[cfg_attr(docsrs, doc(cfg(feature = "rayon")))] |
| 126 |
/// *Only available if compiled with the feature `rayon`* |
| 127 |
impl<T, R, Cols, S> IndexedParallelIterator for ParColumnIterMut<'_, T, R, Cols, S> |
| 128 |
where |
| 129 |
R: Dim, |
| 130 |
Cols: Dim, |
| 131 |
S: RawStorage<T, R, Cols> + RawStorageMut<T, R, Cols>, |
| 132 |
T: Send + Sync + Scalar, |
| 133 |
S: Send + Sync, |
| 134 |
{ |
| 135 |
fn drive<C: rayon::iter::plumbing::Consumer<Self::Item>>(self, consumer: C) -> C::Result { |
| 136 |
bridge(self, consumer) |
| 137 |
} |
| 138 |
|
| 139 |
fn len(&self) -> usize { |
| 140 |
self.mat.ncols() |
| 141 |
} |
| 142 |
|
| 143 |
fn with_producer<CB: rayon::iter::plumbing::ProducerCallback<Self::Item>>( |
| 144 |
self, |
| 145 |
callback: CB, |
| 146 |
) -> CB::Output { |
| 147 |
let producer = ColumnProducerMut(ColumnIterMut::new(self.mat)); |
| 148 |
callback.callback(producer) |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
#[cfg_attr(docsrs, doc(cfg(feature = "rayon")))] |
| 153 |
/// # Parallel iterators using `rayon` |
| 154 |
/// *Only available if compiled with the feature `rayon`* |
| 155 |
impl<T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> Matrix<T, R, Cols, S> |
| 156 |
where |
| 157 |
T: Send + Sync + Scalar, |
| 158 |
S: Sync, |
| 159 |
{ |
| 160 |
/// Iterate through the columns of the matrix in parallel using rayon. |
| 161 |
/// This iterates over *immutable* references to the columns of the matrix, |
| 162 |
/// if *mutable* access to the columns is required, use [`par_column_iter_mut`] |
| 163 |
/// instead. |
| 164 |
/// |
| 165 |
/// # Example |
| 166 |
/// Using parallel column iterators to calculate the sum of the maximum |
| 167 |
/// elements in each column: |
| 168 |
/// ``` |
| 169 |
/// use nalgebra::{dmatrix, DMatrix}; |
| 170 |
/// use rayon::prelude::*; |
| 171 |
/// |
| 172 |
/// let matrix : DMatrix<f64> = dmatrix![1.0, 0.0, 5.0; |
| 173 |
/// 2.0, 4.0, 1.0; |
| 174 |
/// 3.0, 2.0, 2.0; |
| 175 |
/// ]; |
| 176 |
/// let sum_of_max :f64 = matrix |
| 177 |
/// .par_column_iter() |
| 178 |
/// .map(|col| col.max()) |
| 179 |
/// .sum(); |
| 180 |
/// |
| 181 |
/// assert_eq!(sum_of_max,3.0 + 4.0 + 5.0); |
| 182 |
/// |
| 183 |
/// ``` |
| 184 |
/// |
| 185 |
/// [`par_column_iter_mut`]: crate::Matrix::par_column_iter_mut |
| 186 |
pub fn par_column_iter(&self) -> ParColumnIter<'_, T, R, Cols, S> { |
| 187 |
ParColumnIter::new(self) |
| 188 |
} |
| 189 |
|
| 190 |
/// Mutably iterate through the columns of this matrix in parallel using rayon. |
| 191 |
/// Allows mutable access to the columns in parallel using mutable references. |
| 192 |
/// If mutable access to the columns is not required rather use [`par_column_iter`] |
| 193 |
/// instead. |
| 194 |
/// |
| 195 |
/// # Example |
| 196 |
/// Normalize each column of a matrix with respect to its own maximum value. |
| 197 |
/// |
| 198 |
/// ``` |
| 199 |
/// use nalgebra::{dmatrix, DMatrix}; |
| 200 |
/// use rayon::prelude::*; |
| 201 |
/// |
| 202 |
/// let mut matrix : DMatrix<f64> = dmatrix![ |
| 203 |
/// 2.0, 4.0, 6.0; |
| 204 |
/// 1.0, 2.0, 3.0; |
| 205 |
/// ]; |
| 206 |
/// matrix.par_column_iter_mut().for_each(|mut col| col /= col.max()); |
| 207 |
/// |
| 208 |
/// assert_eq!(matrix, dmatrix![1.0, 1.0, 1.0; 0.5, 0.5, 0.5]); |
| 209 |
/// ``` |
| 210 |
/// |
| 211 |
/// [`par_column_iter`]: crate::Matrix::par_column_iter |
| 212 |
pub fn par_column_iter_mut(&mut self) -> ParColumnIterMut<'_, T, R, Cols, S> |
| 213 |
where |
| 214 |
S: RawStorageMut<T, R, Cols>, |
| 215 |
{ |
| 216 |
ParColumnIterMut::new(self) |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/// A private helper newtype that wraps the `ColumnIter` and implements |
| 221 |
/// the rayon `Producer` trait. It's just here so we don't have to make the |
| 222 |
/// rayon trait part of the public interface of the `ColumnIter`. |
| 223 |
struct ColumnProducer<'a, T, R: Dim, C: Dim, S: RawStorage<T, R, C>>(ColumnIter<'a, T, R, C, S>); |
| 224 |
|
| 225 |
#[cfg_attr(docsrs, doc(cfg(feature = "rayon")))] |
| 226 |
/// *only available if compiled with the feature `rayon`* |
| 227 |
impl<'a, T, R: Dim, Cols: Dim, S: RawStorage<T, R, Cols>> Producer |
| 228 |
for ColumnProducer<'a, T, R, Cols, S> |
| 229 |
where |
| 230 |
T: Send + Sync + Scalar, |
| 231 |
S: Sync, |
| 232 |
{ |
| 233 |
type Item = MatrixView<'a, T, R, U1, S::RStride, S::CStride>; |
| 234 |
type IntoIter = ColumnIter<'a, T, R, Cols, S>; |
| 235 |
|
| 236 |
#[inline] |
| 237 |
fn into_iter(self) -> Self::IntoIter { |
| 238 |
self.0 |
| 239 |
} |
| 240 |
|
| 241 |
#[inline] |
| 242 |
fn split_at(self, index: usize) -> (Self, Self) { |
| 243 |
// The index is relative to the size of this current iterator. |
| 244 |
// It will always start at zero so it serves as an offset. |
| 245 |
let (left_iter, right_iter) = self.0.split_at(index); |
| 246 |
(Self(left_iter), Self(right_iter)) |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
/// See `ColumnProducer`. A private wrapper newtype that keeps the Producer |
| 251 |
/// implementation private |
| 252 |
struct ColumnProducerMut<'a, T, R: Dim, C: Dim, S: RawStorageMut<T, R, C>>( |
| 253 |
ColumnIterMut<'a, T, R, C, S>, |
| 254 |
); |
| 255 |
|
| 256 |
impl<'a, T, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Producer |
| 257 |
for ColumnProducerMut<'a, T, R, C, S> |
| 258 |
where |
| 259 |
T: Send + Sync + Scalar, |
| 260 |
S: Send + Sync, |
| 261 |
{ |
| 262 |
type Item = MatrixViewMut<'a, T, R, U1, S::RStride, S::CStride>; |
| 263 |
type IntoIter = ColumnIterMut<'a, T, R, C, S>; |
| 264 |
|
| 265 |
fn into_iter(self) -> Self::IntoIter { |
| 266 |
self.0 |
| 267 |
} |
| 268 |
|
| 269 |
fn split_at(self, index: usize) -> (Self, Self) { |
| 270 |
// The index is relative to the size of this current iterator |
| 271 |
// it will always start at zero so it serves as an offset. |
| 272 |
let (left_iter, right_iter) = self.0.split_at(index); |
| 273 |
(Self(left_iter), Self(right_iter)) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
/// this implementation is safe because we are enforcing exclusive access |
| 278 |
/// to the columns through the active range of the iterator |
| 279 |
unsafe impl<'a, T: Scalar, R: Dim, C: Dim, S: 'a + RawStorageMut<T, R, C>> Send |
| 280 |
for ColumnIterMut<'a, T, R, C, S> |
| 281 |
{ |
| 282 |
} |