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
use hpt_common::shape::shape::Shape;
use hpt_traits::{
ops::creation::TensorCreator,
tensor::{CommonBounds, TensorInfo},
};
use rayon::iter::{plumbing::UnindexedProducer, ParallelIterator};
use crate::{
iterator_traits::{IterGetSet, ShapeManipulator},
par_strided_map_mut::ParStridedMapMut,
par_strided_mut::par_strided_map_mut_simd::ParStridedMutSimd,
};
/// A module for parallel strided map iterator.
pub mod par_strided_map_simd {
use crate::{CommonBounds, TensorInfo};
use hpt_common::utils::simd_ref::MutVec;
use hpt_traits::ops::creation::TensorCreator;
use hpt_types::dtype::TypeCommon;
use rayon::iter::{plumbing::UnindexedProducer, ParallelIterator};
use crate::{
iterator_traits::{IterGetSetSimd, ParStridedIteratorSimdZip, ShapeManipulator},
par_strided_mut::par_strided_map_mut_simd::ParStridedMutSimd,
with_simd::WithSimd,
};
/// A parallel SIMD-optimized map iterator over tensor elements.
///
/// This struct allows for applying two separate functions (`f` and `f2`) to elements of a tensor
/// in a SIMD-optimized and parallel manner.
#[derive(Clone)]
pub struct ParStridedMapSimd<'a, I, T: 'a, F, F2>
where
I: UnindexedProducer<Item = T>
+ 'a
+ IterGetSetSimd<Item = T>
+ ParallelIterator
+ ShapeManipulator,
{
/// The underlying parallel SIMD-optimized strided iterator.
pub(crate) iter: I,
/// The first function to apply to each item.
pub(crate) f: F,
/// The second function to apply to SIMD items.
pub(crate) f2: F2,
/// Phantom data to associate the lifetime `'a` with the struct.
pub(crate) phantom: std::marker::PhantomData<&'a ()>,
}
impl<
'a,
I: UnindexedProducer<Item = T>
+ 'a
+ IterGetSetSimd<Item = T>
+ ParallelIterator
+ ShapeManipulator,
T: 'a,
F,
F2,
> ParStridedMapSimd<'a, I, T, F, F2>
{
/// Collects the results of the map operation into a new tensor.
///
/// This method applies the provided functions `f` and `f2` to each element and SIMD item
/// respectively, accumulating the results into a new tensor of type `U`.
///
/// # Type Parameters
///
/// * `U` - The type of the tensor to collect the results into. Must implement `TensorAlloc`, `Clone`,
/// and `TensorInfo`.
///
/// # Arguments
///
/// * `self` - The `ParStridedMapSimd` iterator instance.
///
/// # Returns
///
/// A new tensor of type `U` containing the results of the map operation.
pub fn collect<U>(self) -> U
where
F: Fn((&mut <U as TensorCreator>::Meta, <I as IterGetSetSimd>::Item))
+ Sync
+ Send
+ 'a,
U: Clone + TensorInfo<U::Meta> + TensorCreator<Output = U>,
<I as IterGetSetSimd>::Item: Send,
<U as TensorCreator>::Meta: CommonBounds,
F2: Send
+ Sync
+ Copy
+ Fn(
(
MutVec<'_, <<U as TensorCreator>::Meta as TypeCommon>::Vec>,
<I as IterGetSetSimd>::SimdItem,
),
),
{
let res = U::empty(self.iter.shape().clone()).unwrap();
let par_strided = ParStridedMutSimd::new(res.clone());
let zip = par_strided.zip(self.iter);
let with_simd = WithSimd {
base: zip,
vec_op: self.f2,
};
with_simd.for_each(|x| {
(self.f)(x);
});
res
}
}
}
/// A parallel map iterator over tensor elements.
///
/// This struct allows for applying a single function (`f`) to elements of a tensor
#[derive(Clone)]
pub struct ParStridedMap<'a, I, T: 'a, F>
where
I: UnindexedProducer<Item = T> + 'a + IterGetSet<Item = T> + ParallelIterator,
{
/// The underlying parallel strided iterator.
pub(crate) iter: I,
/// The function to apply to each item.
pub(crate) f: F,
/// Phantom data to associate the lifetime `'a` with the struct.
pub(crate) phantom: std::marker::PhantomData<&'a ()>,
}
impl<
'a,
I: UnindexedProducer<Item = T> + 'a + IterGetSet<Item = T> + ParallelIterator,
T: 'a,
F,
> ParStridedMap<'a, I, T, F>
{
/// Collects the results of the map operation into a new tensor.
///
/// This method applies the provided function `f` to each element of the tensor,
/// accumulating the results into a new tensor of type `U`.
///
/// # Type Parameters
///
/// * `U` - The type of the tensor to collect the results into. Must implement `TensorAlloc`, `Clone`,
/// and `TensorInfo`.
///
/// # Arguments
///
/// * `self` - The `ParStridedMap` iterator instance.
///
/// # Returns
///
/// A new tensor of type `U` containing the results of the map operation.
pub fn collect<U>(self) -> U
where
F: Fn((&mut U::Meta, T)) + Sync + Send,
U: Clone + TensorInfo<U::Meta> + TensorCreator<Output = U>,
<I as IterGetSet>::Item: Send,
<U as TensorCreator>::Meta: CommonBounds,
{
let res = U::empty(self.iter.shape().clone()).unwrap();
let strided_mut = ParStridedMapMut::new(res.clone());
let zip = strided_mut.zip(self.iter);
zip.for_each(|(x, y)| {
(self.f)((x, y));
});
res
}
}
impl<'a, T: CommonBounds> ShapeManipulator for ParStridedMutSimd<'a, T> {
fn reshape<S: Into<hpt_common::shape::shape::Shape>>(self, shape: S) -> Self {
let shape: Shape = shape.into();
let new_base = self.base.reshape(shape);
ParStridedMutSimd {
base: new_base,
phantom: self.phantom,
}
}
fn transpose<AXIS: Into<hpt_common::axis::axis::Axis>>(self, axes: AXIS) -> Self {
let axes = axes.into();
let new_base = self.base.transpose(axes);
ParStridedMutSimd {
base: new_base,
phantom: self.phantom,
}
}
fn expand<S: Into<hpt_common::shape::shape::Shape>>(self, shape: S) -> Self {
let shape: Shape = shape.into();
let new_base = self.base.expand(shape);
ParStridedMutSimd {
base: new_base,
phantom: self.phantom,
}
}
}