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
use crate::{Array, ArrayBase, DataMut, Dimension, IntoNdProducer, NdProducer, Zip};
use crate::AssignElem;
use crate::parallel::prelude::*;
impl<A, S, D> ArrayBase<S, D>
where
S: DataMut<Elem = A>,
D: Dimension,
A: Send + Sync,
{
pub fn par_map_inplace<F>(&mut self, f: F)
where
F: Fn(&mut A) + Sync + Send,
{
self.view_mut().into_par_iter().for_each(f)
}
pub fn par_mapv_inplace<F>(&mut self, f: F)
where
F: Fn(A) -> A + Sync + Send,
A: Clone,
{
self.view_mut()
.into_par_iter()
.for_each(move |x| *x = f(x.clone()))
}
}
macro_rules! zip_impl {
($([$notlast:ident $($p:ident)*],)+) => {
$(
#[allow(non_snake_case)]
impl<D, $($p),*> Zip<($($p,)*), D>
where $($p::Item : Send , )*
$($p : Send , )*
D: Dimension,
$($p: NdProducer<Dim=D> ,)*
{
pub fn par_apply<F>(self, function: F)
where F: Fn($($p::Item),*) + Sync + Send
{
self.into_par_iter().for_each(move |($($p,)*)| function($($p),*))
}
expand_if!(@bool [$notlast]
pub fn par_apply_collect<R>(self, f: impl Fn($($p::Item,)* ) -> R + Sync + Send) -> Array<R, D>
where R: Copy + Send
{
let mut output = self.uninitalized_for_current_layout::<R>();
self.par_apply_assign_into(&mut output, f);
unsafe {
output.assume_init()
}
}
pub fn par_apply_assign_into<R, Q>(self, into: Q, f: impl Fn($($p::Item,)* ) -> R + Sync + Send)
where Q: IntoNdProducer<Dim=D>,
Q::Item: AssignElem<R> + Send,
Q::Output: Send,
{
self.and(into)
.par_apply(move |$($p, )* output_| {
output_.assign_elem(f($($p ),*));
});
}
);
}
)+
}
}
zip_impl! {
[true P1],
[true P1 P2],
[true P1 P2 P3],
[true P1 P2 P3 P4],
[true P1 P2 P3 P4 P5],
[false P1 P2 P3 P4 P5 P6],
}