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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
use crate::{
    iterator_traits::{IterGetSet, ParStridedHelper, ParStridedIteratorZip, ShapeManipulator},
    par_strided_fold::ParStridedFold,
    par_strided_map::ParStridedMap,
    shape_manipulate::{par_expand, par_reshape, par_transpose},
};
use hpt_common::{
    axis::axis::Axis,
    layout::layout::Layout,
    shape::shape::Shape,
    shape::shape_utils::{mt_intervals, try_pad_shape},
    strides::strides::Strides,
    strides::strides_utils::preprocess_strides,
    utils::pointer::Pointer,
};
use hpt_traits::tensor::{CommonBounds, TensorInfo};
use rayon::iter::{
    plumbing::{bridge_unindexed, Folder, UnindexedConsumer, UnindexedProducer},
    ParallelIterator,
};
use std::sync::Arc;

/// A module for parallel strided iterators.
pub mod par_strided_simd {
    use hpt_types::vectors::traits::VecTrait;
    use std::sync::Arc;

    use crate::{CommonBounds, TensorInfo};
    use hpt_common::{
        axis::axis::Axis,
        layout::layout::Layout,
        shape::shape::Shape,
        shape::shape_utils::{mt_intervals, try_pad_shape},
        strides::strides::Strides,
        strides::strides_utils::preprocess_strides,
        utils::pointer::Pointer,
        utils::simd_ref::MutVec,
    };
    use rayon::iter::{
        plumbing::{bridge_unindexed, Folder, UnindexedConsumer, UnindexedProducer},
        ParallelIterator,
    };

    use crate::{
        iterator_traits::{
            IterGetSetSimd, ParStridedHelper, ParStridedIteratorSimd, ParStridedIteratorSimdZip,
            ShapeManipulator,
        },
        par_strided_map::par_strided_map_simd::ParStridedMapSimd,
        shape_manipulate::{par_expand, par_reshape, par_transpose},
    };

    /// Parallel strided iterator for SIMD operations.
    ///
    /// This struct is used to iterate over a tensor in parallel, with SIMD support.
    #[derive(Clone)]
    pub struct ParStridedSimd<T: Send + Copy + Sync> {
        /// Pointer to the data.
        pub(crate) ptr: Pointer<T>,
        /// Layout of the tensor.
        pub(crate) layout: Layout,
        /// Progress of the loop.
        pub(crate) prg: Vec<i64>,
        /// Chunk intervals for the outer loop.
        pub(crate) intervals: Arc<Vec<(usize, usize)>>,
        /// Start index of the chunk intervals.
        pub(crate) start_index: usize,
        /// End index of the chunk intervals.
        pub(crate) end_index: usize,
        /// Stride of the last dimension.
        pub(crate) last_stride: i64,
    }
    impl<T: CommonBounds> ParStridedSimd<T> {
        /// Returns the shape of the iterator.
        pub fn shape(&self) -> &Shape {
            self.layout.shape()
        }

        /// Returns the strides of the iterator.
        pub fn strides(&self) -> &Strides {
            self.layout.strides()
        }

        /// Create a new parallel strided iterator for SIMD operations.
        pub fn new<U: TensorInfo<T>>(tensor: U) -> Self {
            let inner_loop_size = *tensor.shape().last().unwrap() as usize;
            let outer_loop_size = tensor.size() / 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 = mt_intervals(outer_loop_size, num_threads);
            let len = intervals.len();
            ParStridedSimd {
                ptr: tensor.ptr(),
                layout: tensor.layout().clone(),
                prg: vec![],
                intervals: Arc::new(intervals),
                start_index: 0,
                end_index: len,
                last_stride: *tensor.strides().last().unwrap(),
            }
        }

        /// Map the iterator with a function.
        pub fn strided_map_simd<'a, F, F2>(
            self,
            f: F,
            vec_op: F2,
        ) -> ParStridedMapSimd<'a, ParStridedSimd<T>, T, F, F2>
        where
            F: Fn((&mut T, <Self as IterGetSetSimd>::Item)) + Sync + Send + 'a,
            <Self as IterGetSetSimd>::Item: Send,
            F2: Send + Sync + Copy + Fn((MutVec<'_, T::Vec>, <Self as IterGetSetSimd>::SimdItem)),
        {
            {
                ParStridedMapSimd {
                    iter: self,
                    f,
                    f2: vec_op,
                    phantom: std::marker::PhantomData,
                }
            }
        }
    }

    impl<T: CommonBounds> ParStridedIteratorSimdZip for ParStridedSimd<T> {}
    impl<T: CommonBounds> ParStridedIteratorSimd for ParStridedSimd<T> {}

    impl<T: CommonBounds> IterGetSetSimd for ParStridedSimd<T>
    where
        T::Vec: Send,
    {
        type Item = T;

        type SimdItem = T::Vec;

        fn set_end_index(&mut self, end_index: usize) {
            self.end_index = end_index;
        }

        fn set_intervals(&mut self, intervals: Arc<Vec<(usize, usize)>>) {
            self.intervals = intervals;
        }

        fn set_strides(&mut self, strides: Strides) {
            self.layout.set_strides(strides);
        }

        fn set_shape(&mut self, shape: Shape) {
            self.layout.set_shape(shape);
        }

        fn set_prg(&mut self, prg: Vec<i64>) {
            self.prg = prg;
        }

        fn intervals(&self) -> &Arc<Vec<(usize, usize)>> {
            &self.intervals
        }

        fn strides(&self) -> &Strides {
            self.layout.strides()
        }

        fn shape(&self) -> &Shape {
            self.layout.shape()
        }

        fn layout(&self) -> &Layout {
            &self.layout
        }

        fn broadcast_set_strides(&mut self, shape: &Shape) {
            let self_shape = try_pad_shape(self.shape(), shape.len());
            self.set_strides(preprocess_strides(&self_shape, self.strides()).into());
            self.last_stride = self.strides()[self.strides().len() - 1];
        }

        fn outer_loop_size(&self) -> usize {
            self.intervals[self.start_index].1 - self.intervals[self.start_index].0
        }

        fn inner_loop_size(&self) -> usize {
            self.shape().last().unwrap().clone() as usize
        }

        fn next(&mut self) {
            for j in (0..(self.shape().len() as i64) - 1).rev() {
                let j = j as usize;
                if self.prg[j] < self.shape()[j] {
                    self.prg[j] += 1;
                    self.ptr.offset(self.strides()[j]);
                    break;
                } else {
                    self.prg[j] = 0;
                    self.ptr.offset(-self.strides()[j] * self.shape()[j]);
                }
            }
        }

        fn next_simd(&mut self) {}

        fn inner_loop_next(&mut self, index: usize) -> Self::Item {
            unsafe { *self.ptr.get_ptr().add(index * (self.last_stride as usize)) }
        }

        #[inline(always)]
        fn inner_loop_next_simd(&mut self, index: usize) -> Self::SimdItem {
            unsafe { T::Vec::from_ptr(self.ptr.get_ptr().add(index * T::Vec::SIZE)) }
        }

        fn all_last_stride_one(&self) -> bool {
            self.last_stride == 1
        }

        fn lanes(&self) -> Option<usize> {
            Some(T::Vec::SIZE)
        }
    }

    impl<T> ParallelIterator for ParStridedSimd<T>
    where
        T: CommonBounds,
        T::Vec: Send,
    {
        type Item = T;

        fn drive_unindexed<C>(self, consumer: C) -> C::Result
        where
            C: UnindexedConsumer<Self::Item>,
        {
            bridge_unindexed(self, consumer)
        }
    }

    impl<T> UnindexedProducer for ParStridedSimd<T>
    where
        T: CommonBounds,
        T::Vec: Send,
    {
        type Item = T;

        fn split(mut self) -> (Self, Option<Self>) {
            if self.end_index - self.start_index <= 1 {
                let mut curent_shape_prg: Vec<i64> = vec![0; self.shape().len()];
                let mut amount =
                    self.intervals[self.start_index].0 * (*self.shape().last().unwrap() as usize);
                for j in (0..self.shape().len()).rev() {
                    curent_shape_prg[j] = (amount as i64) % self.shape()[j];
                    amount /= self.shape()[j] as usize;
                    self.ptr += curent_shape_prg[j] * self.strides()[j];
                }
                self.prg = curent_shape_prg;
                let mut new_shape = self.shape().to_vec();
                new_shape.iter_mut().for_each(|x| {
                    *x -= 1;
                });
                self.last_stride = self.strides()[self.strides().len() - 1];
                self.set_shape(Shape::from(new_shape));
                return (self, None);
            }
            let _left_interval = &self.intervals[self.start_index..self.end_index];
            let left = _left_interval.len() / 2;
            let right = _left_interval.len() / 2 + (_left_interval.len() % 2);
            (
                ParStridedSimd {
                    ptr: self.ptr.clone(),
                    layout: self.layout.clone(),
                    prg: vec![],
                    intervals: self.intervals.clone(),
                    start_index: self.start_index,
                    end_index: self.start_index + left,
                    last_stride: self.last_stride,
                },
                Some(ParStridedSimd {
                    ptr: self.ptr.clone(),
                    layout: self.layout.clone(),
                    prg: vec![],
                    intervals: self.intervals.clone(),
                    start_index: self.start_index + left,
                    end_index: self.start_index + left + right,
                    last_stride: self.last_stride,
                }),
            )
        }

        fn fold_with<F>(self, folder: F) -> F
        where
            F: Folder<Self::Item>,
        {
            folder
        }
    }

    impl<T: CommonBounds> ParStridedHelper for ParStridedSimd<T> {
        fn _set_last_strides(&mut self, stride: i64) {
            self.last_stride = stride;
        }

        fn _set_strides(&mut self, strides: Strides) {
            self.layout.set_strides(strides);
        }

        fn _set_shape(&mut self, shape: Shape) {
            self.layout.set_shape(shape);
        }

        fn _layout(&self) -> &Layout {
            &self.layout
        }

        fn _set_intervals(&mut self, intervals: Arc<Vec<(usize, usize)>>) {
            self.intervals = intervals;
        }

        fn _set_end_index(&mut self, end_index: usize) {
            self.end_index = end_index;
        }
    }

    impl<T: CommonBounds> ShapeManipulator for ParStridedSimd<T>
    where
        T::Vec: Send,
    {
        fn reshape<S: Into<Shape>>(self, shape: S) -> Self {
            par_reshape(self, shape)
        }

        fn transpose<AXIS: Into<Axis>>(self, axes: AXIS) -> Self {
            par_transpose(self, axes)
        }

        fn expand<S: Into<Shape>>(self, shape: S) -> Self {
            par_expand(self, shape)
        }
    }
}

/// A parallel strided iterator over tensor elements.
///
/// This struct provides mutable access to tensor elements with strided access patterns optimized for parallel processing.
#[derive(Clone)]
pub struct ParStrided<T> {
    /// A pointer to the tensor's data.
    pub(crate) ptr: Pointer<T>,
    /// The layout of the tensor, including shape and strides.
    pub(crate) layout: Layout,
    /// Progress of the loop.
    pub(crate) prg: Vec<i64>,
    /// Chunk intervals for the outer loop.
    pub(crate) intervals: Arc<Vec<(usize, usize)>>,
    /// Start index of the chunk intervals.
    pub(crate) start_index: usize,
    /// End index of the chunk intervals.
    pub(crate) end_index: usize,
    /// Stride of the last dimension.
    pub(crate) last_stride: i64,
}

impl<T: CommonBounds> ParStrided<T> {
    /// Retrieves the shape of the tensor.
    ///
    /// # Returns
    ///
    /// A reference to the `Shape` struct representing the tensor's dimensions.
    pub fn shape(&self) -> &Shape {
        self.layout.shape()
    }
    /// Retrieves the strides of the tensor.
    ///
    /// # Returns
    ///
    /// A reference to the `Strides` struct representing the tensor's stride information.
    pub fn strides(&self) -> &Strides {
        self.layout.strides()
    }
    /// Creates a new `ParStrided` instance from a given tensor.
    ///
    /// This constructor initializes the `ParStrided` iterator by determining the appropriate number of threads
    /// based on the tensor's outer loop size and Rayon’s current number of threads. It then divides the
    /// iteration workload into intervals for parallel execution.
    ///
    /// # Arguments
    ///
    /// * `tensor` - The tensor implementing the `TensorInfo<T>` trait to iterate over mutably.
    ///
    /// # Returns
    ///
    /// A new instance of `ParStrided` initialized with the provided tensor.
    pub fn new<U: TensorInfo<T>>(tensor: U) -> Self {
        let inner_loop_size = tensor.shape()[tensor.shape().len() - 1] as usize;
        let outer_loop_size = tensor.size() / 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 = mt_intervals(outer_loop_size, num_threads);
        let len = intervals.len();
        ParStrided {
            ptr: tensor.ptr(),
            layout: tensor.layout().clone(),
            prg: vec![],
            intervals: Arc::new(intervals),
            start_index: 0,
            end_index: len,
            last_stride: tensor.strides()[tensor.strides().len() - 1],
        }
    }
    /// Performs a parallel fold (reduce) operation over the tensor elements.
    ///
    /// This method applies a folding function `fold_op` to accumulate tensor elements into an initial
    /// identity value `identity`. It leverages parallel iteration to perform the fold operation efficiently.
    ///
    /// # Type Parameters
    ///
    /// * `ID` - The type of the accumulator.
    /// * `F` - The folding function.
    ///
    /// # Arguments
    ///
    /// * `identity` - The initial value for the accumulator.
    /// * `fold_op` - A function that takes the current accumulator and an element, returning the updated accumulator.
    ///
    /// # Returns
    ///
    /// A `ParStridedFold` instance that represents the fold operation.
    pub fn par_strided_fold<ID, F>(self, identity: ID, fold_op: F) -> ParStridedFold<Self, ID, F>
    where
        F: Fn(ID, T) -> ID + Sync + Send + Copy,
        ID: Sync + Send + Copy,
    {
        ParStridedFold {
            iter: self,
            identity,
            fold_op,
        }
    }
    /// Transforms the zipped 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 `ParStridedMap` instance that applies the provided function during iteration.
    pub fn strided_map<'a, F, U>(self, f: F) -> ParStridedMap<'a, ParStrided<T>, T, F>
    where
        F: Fn((&mut U, T)) + Sync + Send + 'a,
        U: CommonBounds,
    {
        ParStridedMap {
            iter: self,
            f,
            phantom: std::marker::PhantomData,
        }
    }
}

impl<T: CommonBounds> ParStridedIteratorZip for ParStrided<T> {}

impl<T: CommonBounds> IterGetSet for ParStrided<T> {
    type Item = T;

    fn set_end_index(&mut self, end_index: usize) {
        self.end_index = end_index;
    }

    fn set_intervals(&mut self, intervals: Arc<Vec<(usize, usize)>>) {
        self.intervals = intervals;
    }

    fn set_strides(&mut self, strides: Strides) {
        self.layout.set_strides(strides);
    }

    fn set_shape(&mut self, shape: Shape) {
        self.layout.set_shape(shape);
    }

    fn set_prg(&mut self, prg: Vec<i64>) {
        self.prg = prg;
    }

    fn intervals(&self) -> &Arc<Vec<(usize, usize)>> {
        &self.intervals
    }

    fn strides(&self) -> &Strides {
        self.layout.strides()
    }

    fn shape(&self) -> &Shape {
        self.layout.shape()
    }

    fn layout(&self) -> &Layout {
        &self.layout
    }

    fn broadcast_set_strides(&mut self, shape: &Shape) {
        let self_shape = try_pad_shape(self.shape(), shape.len());
        self.set_strides(preprocess_strides(&self_shape, self.strides()).into());
        self.last_stride = self.strides()[self.strides().len() - 1];
    }

    fn outer_loop_size(&self) -> usize {
        self.intervals[self.start_index].1 - self.intervals[self.start_index].0
    }

    fn inner_loop_size(&self) -> usize {
        self.shape().last().unwrap().clone() as usize
    }

    fn next(&mut self) {
        for j in (0..(self.shape().len() as i64) - 1).rev() {
            let j = j as usize;
            if self.prg[j] < self.shape()[j] {
                self.prg[j] += 1;
                self.ptr.offset(self.strides()[j]);
                break;
            } else {
                self.prg[j] = 0;
                self.ptr.offset(-self.strides()[j] * self.shape()[j]);
            }
        }
    }

    fn inner_loop_next(&mut self, index: usize) -> Self::Item {
        unsafe { *self.ptr.get_ptr().add(index * (self.last_stride as usize)) }
    }
}

impl<T> ParallelIterator for ParStrided<T>
where
    T: CommonBounds,
    T::Vec: Send,
{
    type Item = T;

    fn drive_unindexed<C>(self, consumer: C) -> C::Result
    where
        C: UnindexedConsumer<Self::Item>,
    {
        bridge_unindexed(self, consumer)
    }
}

impl<T> UnindexedProducer for ParStrided<T>
where
    T: CommonBounds,
    T::Vec: Send,
{
    type Item = T;

    fn split(mut self) -> (Self, Option<Self>) {
        if self.end_index - self.start_index <= 1 {
            let mut curent_shape_prg: Vec<i64> = vec![0; self.shape().len()];
            let mut amount =
                self.intervals[self.start_index].0 * (*self.shape().last().unwrap() as usize);
            let mut index = 0;
            for j in (0..self.shape().len()).rev() {
                curent_shape_prg[j] = (amount as i64) % self.shape()[j];
                amount /= self.shape()[j] as usize;
                index += curent_shape_prg[j] * self.strides()[j];
            }
            self.ptr.offset(index);
            self.prg = curent_shape_prg;
            let mut new_shape = self.shape().to_vec();
            new_shape.iter_mut().for_each(|x| {
                *x -= 1;
            });
            self.last_stride = self.strides()[self.strides().len() - 1];
            self.set_shape(Shape::from(new_shape));
            return (self, None);
        }
        let _left_interval = &self.intervals[self.start_index..self.end_index];
        let left = _left_interval.len() / 2;
        let right = _left_interval.len() / 2 + (_left_interval.len() % 2);
        (
            ParStrided {
                ptr: self.ptr.clone(),
                layout: self.layout.clone(),
                prg: vec![],
                intervals: self.intervals.clone(),
                start_index: self.start_index,
                end_index: self.start_index + left,
                last_stride: self.last_stride,
            },
            Some(ParStrided {
                ptr: self.ptr.clone(),
                layout: self.layout.clone(),
                prg: vec![],
                intervals: self.intervals.clone(),
                start_index: self.start_index + left,
                end_index: self.start_index + left + right,
                last_stride: self.last_stride,
            }),
        )
    }

    fn fold_with<F>(self, folder: F) -> F
    where
        F: Folder<Self::Item>,
    {
        folder
    }
}

impl<T> ParStridedHelper for ParStrided<T> {
    fn _set_last_strides(&mut self, last_stride: i64) {
        self.last_stride = last_stride;
    }

    fn _set_strides(&mut self, strides: Strides) {
        self.layout.set_strides(strides);
    }

    fn _set_shape(&mut self, shape: Shape) {
        self.layout.set_shape(shape);
    }

    fn _layout(&self) -> &Layout {
        &self.layout
    }

    fn _set_intervals(&mut self, intervals: Arc<Vec<(usize, usize)>>) {
        self.intervals = intervals;
    }

    fn _set_end_index(&mut self, end_index: usize) {
        self.end_index = end_index;
    }
}

impl<T: CommonBounds> ShapeManipulator for ParStrided<T> {
    #[track_caller]
    fn reshape<S: Into<Shape>>(self, shape: S) -> Self {
        par_reshape(self, shape)
    }

    fn transpose<AXIS: Into<Axis>>(self, axes: AXIS) -> Self {
        par_transpose(self, axes)
    }

    fn expand<S: Into<Shape>>(self, shape: S) -> Self {
        par_expand(self, shape)
    }
}