1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use rayon::par_iter::ParallelIterator;
use rayon::par_iter::IndexedParallelIterator;
use rayon::par_iter::ExactParallelIterator;
use rayon::par_iter::BoundedParallelIterator;
use rayon::par_iter::internal::{Consumer, UnindexedConsumer};
use rayon::par_iter::internal::bridge;
use rayon::par_iter::internal::ProducerCallback;
use rayon::par_iter::internal::Producer;
use rayon::par_iter::internal::UnindexedProducer;
use rayon::par_iter::internal::bridge_unindexed;
use ndarray::AxisIter;
use ndarray::AxisIterMut;
use ndarray::{Dimension};
use ndarray::{ArrayView, ArrayViewMut};
use super::NdarrayIntoParallelIterator;
#[derive(Copy, Clone, Debug)]
pub struct Parallel<I> {
iter: I,
}
#[derive(Copy, Clone, Debug)]
struct ParallelProducer<I>(I);
macro_rules! par_iter_wrapper {
($iter_name:ident, [$($thread_bounds:tt)*]) => {
impl<'a, A, D> NdarrayIntoParallelIterator for $iter_name<'a, A, D>
where D: Dimension,
A: $($thread_bounds)*,
{
type Item = <Self as Iterator>::Item;
type Iter = Parallel<Self>;
fn into_par_iter(self) -> Self::Iter {
Parallel {
iter: self,
}
}
}
impl<'a, A, D> ParallelIterator for Parallel<$iter_name<'a, A, D>>
where D: Dimension,
A: $($thread_bounds)*,
{
type Item = <$iter_name<'a, A, D> as Iterator>::Item;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where C: UnindexedConsumer<Self::Item>
{
bridge(self, consumer)
}
fn opt_len(&mut self) -> Option<usize> {
Some(self.iter.len())
}
}
impl<'a, A, D> IndexedParallelIterator for Parallel<$iter_name<'a, A, D>>
where D: Dimension,
A: $($thread_bounds)*,
{
fn with_producer<Cb>(self, callback: Cb) -> Cb::Output
where Cb: ProducerCallback<Self::Item>
{
callback.callback(ParallelProducer(self.iter))
}
}
impl<'a, A, D> ExactParallelIterator for Parallel<$iter_name<'a, A, D>>
where D: Dimension,
A: $($thread_bounds)*,
{
fn len(&mut self) -> usize {
ExactSizeIterator::len(&self.iter)
}
}
impl<'a, A, D> BoundedParallelIterator for Parallel<$iter_name<'a, A, D>>
where D: Dimension,
A: $($thread_bounds)*,
{
fn upper_bound(&mut self) -> usize {
ExactSizeIterator::len(&self.iter)
}
fn drive<C>(self, consumer: C) -> C::Result
where C: Consumer<Self::Item>
{
bridge(self, consumer)
}
}
impl<'a, A, D> IntoIterator for ParallelProducer<$iter_name<'a, A, D>>
where D: Dimension,
{
type IntoIter = $iter_name<'a, A, D>;
type Item = <Self::IntoIter as Iterator>::Item;
fn into_iter(self) -> Self::IntoIter {
self.0
}
}
impl<'a, A, D> Producer for ParallelProducer<$iter_name<'a, A, D>>
where D: Dimension,
A: $($thread_bounds)*,
{
fn cost(&mut self, len: usize) -> f64 {
len as f64
}
fn split_at(self, i: usize) -> (Self, Self) {
let (a, b) = self.0.split_at(i);
(ParallelProducer(a), ParallelProducer(b))
}
}
}
}
par_iter_wrapper!(AxisIter, [Sync]);
par_iter_wrapper!(AxisIterMut, [Send + Sync]);
macro_rules! par_iter_view_wrapper {
($view_name:ident, [$($thread_bounds:tt)*]) => {
impl<'a, A, D> NdarrayIntoParallelIterator for $view_name<'a, A, D>
where D: Dimension,
A: $($thread_bounds)*,
{
type Item = <Self as IntoIterator>::Item;
type Iter = Parallel<Self>;
fn into_par_iter(self) -> Self::Iter {
Parallel {
iter: self,
}
}
}
impl<'a, A, D> ParallelIterator for Parallel<$view_name<'a, A, D>>
where D: Dimension,
A: $($thread_bounds)*,
{
type Item = <$view_name<'a, A, D> as IntoIterator>::Item;
fn drive_unindexed<C>(self, consumer: C) -> C::Result
where C: UnindexedConsumer<Self::Item>
{
bridge_unindexed(ParallelProducer(self.iter), consumer)
}
fn opt_len(&mut self) -> Option<usize> {
Some(self.iter.len())
}
}
impl<'a, A, D> UnindexedProducer for ParallelProducer<$view_name<'a, A, D>>
where D: Dimension,
A: $($thread_bounds)*,
{
fn can_split(&self) -> bool {
self.0.len() > 1
}
fn split(self) -> (Self, Self) {
let array = self.0;
let max_axis = array.max_stride_axis();
let mid = array.len_of(max_axis) / 2;
let (a, b) = array.split_at(max_axis, mid);
(ParallelProducer(a), ParallelProducer(b))
}
#[cfg(rayon_fold_with)]
fn fold_with<F>(self, folder: F) -> F
where F: Folder<Self::Item>,
{
self.into_iter().fold(folder, move |f, elt| f.consume(elt))
}
}
impl<'a, A, D> IntoIterator for ParallelProducer<$view_name<'a, A, D>>
where D: Dimension,
A: $($thread_bounds)*,
{
type Item = <$view_name<'a, A, D> as IntoIterator>::Item;
type IntoIter = <$view_name<'a, A, D> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
}
}
par_iter_view_wrapper!(ArrayView, [Sync]);
par_iter_view_wrapper!(ArrayViewMut, [Sync + Send]);