hpt-iterator 0.1.2

An internal library implements iterator for hpt
Documentation
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
use std::sync::Arc;

use crate::CommonBounds;
use hpt_common::{
    axis::axis::Axis,
    layout::layout::Layout,
    shape::shape::Shape,
    shape::shape_utils::{mt_intervals, predict_broadcast_shape},
    strides::strides::Strides,
};
use rayon::iter::{plumbing::UnindexedProducer, ParallelIterator};

use crate::{
    par_strided_zip::{par_strided_zip_simd::ParStridedZipSimd, ParStridedZip},
    strided_map::StridedMap,
    strided_zip::{strided_zip_simd::StridedZipSimd, StridedZip},
    with_simd::WithSimd,
};

/// A trait for getting and setting values from an iterator.
pub trait IterGetSet {
    /// The type of the iterator's elements.
    type Item;
    /// set the end index of the iterator, this is used when rayon perform data splitting
    fn set_end_index(&mut self, end_index: usize);
    /// set the chunk intervals of the iterator, we chunk the outer loop
    fn set_intervals(&mut self, intervals: Arc<Vec<(usize, usize)>>);
    /// set the strides for the iterator, we call this method normally when we do broadcasting
    fn set_strides(&mut self, strides: Strides);
    /// set the shape for the iterator, we call this method normally when we do broadcasting
    fn set_shape(&mut self, shape: Shape);
    /// set the loop progress for the iterator
    fn set_prg(&mut self, prg: Vec<i64>);
    /// get the intervals of the iterator
    fn intervals(&self) -> &Arc<Vec<(usize, usize)>>;
    /// get the strides of the iterator
    fn strides(&self) -> &Strides;
    /// get the shape of the iterator
    fn shape(&self) -> &Shape;
    /// get the layout of the iterator
    fn layout(&self) -> &Layout;
    /// set the strides for all the iterators
    fn broadcast_set_strides(&mut self, shape: &Shape);
    /// get the outer loop size
    fn outer_loop_size(&self) -> usize;
    /// get the inner loop size
    fn inner_loop_size(&self) -> usize;
    /// update the loop progress
    fn next(&mut self);
    /// get the next element of the inner loop
    fn inner_loop_next(&mut self, index: usize) -> Self::Item;
}

/// A trait for getting and setting values from an simd iterator
pub trait IterGetSetSimd {
    /// The type of the iterator's elements.
    type Item;
    /// The type of the iterator's simd elements.
    type SimdItem;
    /// set the end index of the iterator, this is used when rayon perform data splitting
    fn set_end_index(&mut self, end_index: usize);
    /// set the chunk intervals of the iterator, we chunk the outer loop
    fn set_intervals(&mut self, intervals: Arc<Vec<(usize, usize)>>);
    /// set the strides for the iterator, we call this method normally when we do broadcasting
    fn set_strides(&mut self, last_stride: Strides);
    /// set the shape for the iterator, we call this method normally when we do broadcasting
    fn set_shape(&mut self, shape: Shape);
    /// set the loop progress for the iterator
    fn set_prg(&mut self, prg: Vec<i64>);
    /// get the intervals of the iterator
    fn intervals(&self) -> &Arc<Vec<(usize, usize)>>;
    /// get the strides of the iterator
    fn strides(&self) -> &Strides;
    /// get the shape of the iterator
    fn shape(&self) -> &Shape;
    /// get the layout of the iterator
    fn layout(&self) -> &Layout;
    /// set the strides for all the iterators
    fn broadcast_set_strides(&mut self, shape: &Shape);
    /// get the outer loop size
    fn outer_loop_size(&self) -> usize;
    /// get the inner loop size
    fn inner_loop_size(&self) -> usize;
    /// update the loop progress, this is called when we don't do simd iteration
    fn next(&mut self);
    /// update the loop progress, this is called when we do simd iteration
    fn next_simd(&mut self);
    /// get the next element of the inner loop
    fn inner_loop_next(&mut self, index: usize) -> Self::Item;
    /// get the next vector of the inner loop, this is called when we do simd iteration
    fn inner_loop_next_simd(&mut self, index: usize) -> Self::SimdItem;
    /// check if all iterators' last stride is one, only when all iterators' last stride is one, we can do simd iteration
    fn all_last_stride_one(&self) -> bool;
    /// get the simd vector size, if any of the iterator returned different vector size, it will return None
    fn lanes(&self) -> Option<usize>;
}

/// A trait for performing shape manipulation on an iterator.
pub trait ShapeManipulator {
    /// reshape the iterator, we can change the iteration behavior by changing the shape
    fn reshape<S: Into<Shape>>(self, shape: S) -> Self;
    /// transpose the iterator, we can change the iteration behavior by changing the axes
    fn transpose<AXIS: Into<Axis>>(self, axes: AXIS) -> Self;
    /// expand the iterator, we can change the iteration behavior by changing the shape
    fn expand<S: Into<Shape>>(self, shape: S) -> Self;
}

/// A trait for performing single thread iteration over an iterator.
pub trait StridedIterator: IterGetSet
where
    Self: Sized,
{
    /// perform scalar iteration, this method is for single thread iterator
    fn for_each<F>(mut self, func: F)
    where
        F: Fn(Self::Item),
    {
        let outer_loop_size = self.outer_loop_size();
        let inner_loop_size = self.inner_loop_size(); // we don't need to add 1 as we didn't subtract shape by 1
        self.set_prg(vec![0; self.shape().len()]);
        for _ in 0..outer_loop_size {
            for idx in 0..inner_loop_size {
                func(self.inner_loop_next(idx));
            }
            self.next();
        }
    }
    /// perform scalar iteration with init, this method is for single thread iterator
    fn for_each_init<F, INIT, T>(mut self, init: INIT, func: F)
    where
        F: Fn(&mut T, Self::Item),
        INIT: Fn() -> T,
    {
        let outer_loop_size = self.outer_loop_size();
        let inner_loop_size = self.inner_loop_size();
        self.set_prg(vec![0; self.shape().len()]);
        let mut init = init();
        for _ in 0..outer_loop_size {
            for idx in 0..inner_loop_size {
                func(&mut init, self.inner_loop_next(idx));
            }
            self.next();
        }
    }
}

/// A trait to zip two iterators together.
pub trait StridedIteratorZip: Sized {
    /// Combines this iterator with another iterator, enabling simultaneous iteration.
    ///
    /// This method zips together `self` and `other` into a `StridedZip` iterator, allowing for synchronized
    ///
    /// iteration over both iterators. This is particularly useful for operations that require processing
    ///
    /// elements from two tensors in parallel, such as element-wise arithmetic operations.
    ///
    /// # Arguments
    ///
    /// * `other` - The other iterator to zip with. It must implement the `IterGetSet` trait, and
    ///             its associated `Item` type must be `Send`.
    ///
    /// # Returns
    ///
    /// A `StridedZip` instance that encapsulates both `self` and `other`, allowing for synchronized
    ///
    /// iteration over their elements.
    ///
    /// # Panics
    ///
    /// This method will panic if the shapes of `self` and `other` cannot be broadcasted together.
    #[track_caller]
    fn zip<'a, C>(self, other: C) -> StridedZip<'a, Self, C>
    where
        C: IterGetSet + ShapeManipulator,
        Self: IterGetSet + ShapeManipulator,
        <C as IterGetSet>::Item: Send,
        <Self as IterGetSet>::Item: Send,
    {
        let new_shape = predict_broadcast_shape(&self.shape(), &other.shape())
            .expect("Cannot broadcast shapes");

        let mut a = self.reshape(new_shape.clone());
        let mut b = other.reshape(new_shape.clone());

        a.set_shape(new_shape.clone());
        b.set_shape(new_shape.clone());
        StridedZip::new(a, b)
    }
}

/// A trait to zip two parallel iterators together.
pub trait ParStridedIteratorZip: Sized + IterGetSet {
    /// Combines this iterator with another iterator, enabling simultaneous parallel iteration.
    ///
    /// This method performs shape broadcasting between `self` and `other` to ensure that both iterators
    /// iterate over tensors with compatible shapes. It adjusts the strides and shapes of both iterators
    /// to match the broadcasted shape and then returns a `ParStridedZip` that allows for synchronized
    /// parallel iteration over both iterators.
    ///
    /// # Arguments
    ///
    /// * `other` - The other iterator to zip with. It must implement the `IterGetSet`, `UnindexedProducer`,
    ///             and `ParallelIterator` traits, and its associated `Item` type must be `Send`.
    ///
    /// # Returns
    ///
    /// A `ParStridedZip` instance that zips together `self` and `other`, enabling synchronized
    /// parallel iteration over their elements.
    ///
    /// # Panics
    ///
    /// This method will panic if the shapes of `self` and `other` cannot be broadcasted together.
    /// Ensure that the shapes are compatible before calling this method.
    #[track_caller]
    fn zip<'a, C>(mut self, mut other: C) -> ParStridedZip<'a, Self, C>
    where
        C: UnindexedProducer + 'a + IterGetSet + ParallelIterator + ShapeManipulator,
        <C as IterGetSet>::Item: Send,
        Self: UnindexedProducer + ParallelIterator + ShapeManipulator,
        <Self as IterGetSet>::Item: Send,
    {
        let new_shape = predict_broadcast_shape(&self.shape(), &other.shape())
            .expect("Cannot broadcast shapes");

        let inner_loop_size = new_shape[new_shape.len() - 1] as usize;
        let outer_loop_size = (new_shape.size() as usize) / inner_loop_size;

        let num_threads;
        if outer_loop_size < rayon::current_num_threads() {
            num_threads = outer_loop_size;
        } else {
            num_threads = rayon::current_num_threads();
        }
        let intervals = Arc::new(mt_intervals(outer_loop_size, num_threads));
        let len = intervals.len();
        self.set_intervals(intervals.clone());
        self.set_end_index(len);
        other.set_intervals(intervals.clone());
        other.set_end_index(len);

        let mut a = self.reshape(new_shape.clone());
        let mut b = other.reshape(new_shape.clone());

        a.set_shape(new_shape.clone());
        b.set_shape(new_shape.clone());

        ParStridedZip::new(a, b)
    }
}

/// A trait to zip two parallel iterators together.
pub trait ParStridedIteratorSimdZip: Sized + IterGetSetSimd {
    /// Combines this `ParStridedZipSimd` iterator with another SIMD-optimized iterator, enabling simultaneous parallel iteration.
    ///
    /// This method performs shape broadcasting between `self` and `other` to ensure that both iterators
    /// iterate over tensors with compatible shapes. It calculates the appropriate iteration intervals based
    /// on the new broadcasted shape and configures both iterators accordingly. Finally, it returns a new
    /// `ParStridedZipSimd` instance that allows for synchronized parallel iteration over the combined iterators.
    ///
    /// # Arguments
    ///
    /// * `other` - The third iterator to zip with. It must implement the `IterGetSetSimd`, `UnindexedProducer`,
    ///             `ShapeManipulator`, and `ParallelIterator` traits,
    ///             and its associated `Item` type must be `Send`.
    ///
    /// # Returns
    ///
    /// A new `ParStridedZipSimd` instance that combines `self` and `other` for synchronized parallel iteration over all three iterators.
    ///
    /// # Panics
    ///
    /// This method will panic if the shapes of `self` and `other` cannot be broadcasted together.
    /// Ensure that the shapes are compatible before calling this method.
    #[track_caller]
    fn zip<'a, C>(mut self, mut other: C) -> ParStridedZipSimd<'a, Self, C>
    where
        C: UnindexedProducer + 'a + IterGetSetSimd + ParallelIterator + ShapeManipulator,
        <C as IterGetSetSimd>::Item: Send,
        Self: UnindexedProducer + ParallelIterator + ShapeManipulator,
        <Self as IterGetSetSimd>::Item: Send,
    {
        let new_shape = predict_broadcast_shape(&self.shape(), &other.shape())
            .expect("Cannot broadcast shapes");

        let inner_loop_size = new_shape[new_shape.len() - 1] as usize;
        let outer_loop_size = (new_shape.size() as usize) / inner_loop_size;

        let num_threads;
        if outer_loop_size < rayon::current_num_threads() {
            num_threads = outer_loop_size;
        } else {
            num_threads = rayon::current_num_threads();
        }
        let intervals = Arc::new(mt_intervals(outer_loop_size, num_threads));
        let len = intervals.len();
        self.set_intervals(intervals.clone());
        self.set_end_index(len);
        other.set_intervals(intervals.clone());
        other.set_end_index(len);

        let mut a = self.reshape(new_shape.clone());
        let mut b = other.reshape(new_shape.clone());

        a.set_shape(new_shape.clone());
        b.set_shape(new_shape.clone());

        ParStridedZipSimd::new(a, b)
    }
}

/// A trait to zip two simd iterators together.
pub trait StridedSimdIteratorZip: Sized {
    /// Combines this iterator with another SIMD-optimized iterator, enabling simultaneous iteration.
    ///
    /// This method performs shape broadcasting between `self` and `other` to ensure that both iterators
    /// iterate over tensors with compatible shapes. It adjusts the strides and shapes of both iterators
    /// to match the broadcasted shape and then returns a `StridedZipSimd` that allows for synchronized
    /// iteration over both iterators.
    ///
    /// # Arguments
    ///
    /// * `other` - The other iterator to zip with. It must implement the `IterGetSetSimd`, `UnindexedProducer`,
    ///             and `ParallelIterator` traits, and its associated `Item` type must be `Send`.
    ///
    /// # Returns
    ///
    /// A `StridedZipSimd` instance that zips together `self` and `other`, enabling synchronized
    /// iteration over their elements.
    #[track_caller]
    fn zip<'a, C>(mut self, mut other: C) -> StridedZipSimd<'a, Self, C>
    where
        C: 'a + IterGetSetSimd,
        <C as IterGetSetSimd>::Item: Send,
        Self: IterGetSetSimd,
        <Self as IterGetSetSimd>::Item: Send,
    {
        let new_shape =
            predict_broadcast_shape(self.shape(), other.shape()).expect("Cannot broadcast shapes");

        other.broadcast_set_strides(&new_shape);
        self.broadcast_set_strides(&new_shape);

        other.set_shape(new_shape.clone());
        self.set_shape(new_shape.clone());

        StridedZipSimd::new(self, other)
    }
}

/// A trait for performing single thread simd iteration over an iterator.
pub trait StridedIteratorSimd
where
    Self: Sized + IterGetSetSimd,
{
    /// perform simd iteration, this method is for single thread simd iterator
    fn for_each<F, F2>(mut self, op: F, vec_op: F2)
    where
        F: Fn(Self::Item),
        F2: Fn(Self::SimdItem),
    {
        let outer_loop_size = self.outer_loop_size();
        let inner_loop_size = self.inner_loop_size(); // we don't need to add 1 as we didn't subtract shape by 1
        self.set_prg(vec![0; self.shape().len()]);
        match (self.all_last_stride_one(), self.lanes()) {
            (true, Some(vec_size)) => {
                let remain = inner_loop_size % vec_size;
                let inner = inner_loop_size - remain;
                let n = inner / vec_size;
                let unroll = n % 4;
                if remain > 0 {
                    if unroll == 0 {
                        for _ in 0..outer_loop_size {
                            for idx in 0..n / 4 {
                                vec_op(self.inner_loop_next_simd(idx * 4));
                                vec_op(self.inner_loop_next_simd(idx * 4 + 1));
                                vec_op(self.inner_loop_next_simd(idx * 4 + 2));
                                vec_op(self.inner_loop_next_simd(idx * 4 + 3));
                            }
                            for idx in inner..inner_loop_size {
                                op(self.inner_loop_next(idx));
                            }
                            self.next();
                        }
                    } else {
                        for _ in 0..outer_loop_size {
                            for idx in 0..n {
                                vec_op(self.inner_loop_next_simd(idx));
                            }
                            for idx in inner..inner_loop_size {
                                op(self.inner_loop_next(idx));
                            }
                            self.next();
                        }
                    }
                } else {
                    if unroll == 0 {
                        for _ in 0..outer_loop_size {
                            for idx in 0..n / 4 {
                                vec_op(self.inner_loop_next_simd(idx * 4));
                                vec_op(self.inner_loop_next_simd(idx * 4 + 1));
                                vec_op(self.inner_loop_next_simd(idx * 4 + 2));
                                vec_op(self.inner_loop_next_simd(idx * 4 + 3));
                            }
                            self.next();
                        }
                    } else {
                        for _ in 0..outer_loop_size {
                            for idx in 0..n {
                                vec_op(self.inner_loop_next_simd(idx));
                            }
                            self.next();
                        }
                    }
                }
            }
            _ => {
                for _ in 0..outer_loop_size {
                    for idx in 0..inner_loop_size {
                        op(self.inner_loop_next(idx));
                    }
                    self.next();
                }
            }
        }
    }
    /// perform simd iteration with init, this method is for single thread simd iterator
    fn for_each_init<F, INIT, T>(mut self, init: INIT, func: F)
    where
        F: Fn(&mut T, Self::Item),
        INIT: Fn() -> T,
    {
        let outer_loop_size = self.outer_loop_size();
        let inner_loop_size = self.inner_loop_size();
        let mut init = init();
        for _ in 0..outer_loop_size {
            for idx in 0..inner_loop_size {
                func(&mut init, self.inner_loop_next(idx));
            }
            self.next();
        }
    }
}

/// A trait for performing single thread simd iteration over an iterator.
pub trait ParStridedIteratorSimd
where
    Self: Sized + UnindexedProducer + IterGetSetSimd + ParallelIterator,
{
    /// perform simd iteration, this method is for single thread simd iterator
    fn for_each<F, F2>(self, op: F, vec_op: F2)
    where
        F: Fn(<Self as IterGetSetSimd>::Item) + Sync,
        F2: Fn(<Self as IterGetSetSimd>::SimdItem) + Sync + Send + Copy,
        <Self as IterGetSetSimd>::SimdItem: Send,
        <Self as IterGetSetSimd>::Item: Send,
    {
        let with_simd = WithSimd { base: self, vec_op };
        with_simd.for_each(|x| {
            op(x);
        });
    }
}

/// A trait to map a function on the elements of an iterator.
pub trait StridedIteratorMap: Sized {
    /// Transforms the strided iterators by applying a provided function to their items.
    ///
    /// This method allows for element-wise operations on the zipped iterators by applying `func` to each item.
    ///
    /// # Type Parameters
    ///
    /// * `'a` - The lifetime associated with the iterators.
    /// * `F` - The function to apply to each item.
    /// * `U` - The output type after applying the function.
    ///
    /// # Arguments
    ///
    /// * `f` - A function that takes an item from the zipped iterator and returns a transformed value.
    ///
    /// # Returns
    ///
    /// A `StridedMap` instance that applies the provided function during iteration.
    fn map<'a, T, F, U>(self, f: F) -> StridedMap<'a, Self, T, F>
    where
        F: Fn(T) -> U + Sync + Send + 'a,
        U: CommonBounds,
        Self: IterGetSet<Item = T>,
    {
        StridedMap {
            iter: self,
            f,
            phantom: std::marker::PhantomData,
        }
    }
}

pub(crate) trait StridedHelper {
    fn _set_last_strides(&mut self, stride: i64);
    fn _set_strides(&mut self, strides: Strides);
    fn _set_shape(&mut self, shape: Shape);
    fn _layout(&self) -> &Layout;
}

pub(crate) trait ParStridedHelper {
    fn _set_last_strides(&mut self, stride: i64);
    fn _set_strides(&mut self, strides: Strides);
    fn _set_shape(&mut self, shape: Shape);
    fn _layout(&self) -> &Layout;
    fn _set_intervals(&mut self, intervals: Arc<Vec<(usize, usize)>>);
    fn _set_end_index(&mut self, end_index: usize);
}