array_element_picker.hpp 2.71 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef CK_ARRAY_ELEMENT_PICKER_HPP
#define CK_ARRAY_ELEMENT_PICKER_HPP

#include "functional2.hpp"
#include "sequence.hpp"

namespace ck {

// Arr: Array or StaticallyIndexedArray
// Picks: Sequence<...>
template <typename Arr, typename Picks>
struct ArrayElementPicker
{
Chao Liu's avatar
Chao Liu committed
14
15
    using type = ArrayElementPicker;
#if 0
Chao Liu's avatar
Chao Liu committed
16
    using data_type = typename Arr::data_type;
Chao Liu's avatar
Chao Liu committed
17
#endif
Chao Liu's avatar
Chao Liu committed
18
19
20
21
22
23
24
25
26
27
28
29
30

    __host__ __device__ constexpr ArrayElementPicker() = delete;

    __host__ __device__ explicit constexpr ArrayElementPicker(Arr& array) : mArray{array}
    {
        constexpr index_t imax = reduce_on_sequence(Picks{}, math::maxer<index_t>{}, Number<0>{});

        static_assert(imax < Arr::Size(), "wrong! exceeding # array element");
    }

    __host__ __device__ static constexpr auto Size() { return Picks::Size(); }

    template <index_t I>
Chao Liu's avatar
Chao Liu committed
31
    __host__ __device__ constexpr const auto& At(Number<I> i) const
Chao Liu's avatar
Chao Liu committed
32
33
34
    {
        static_assert(I < Size(), "wrong!");

Chao Liu's avatar
Chao Liu committed
35
        constexpr auto IP = Picks{}[i];
Chao Liu's avatar
Chao Liu committed
36
37
38
39
        return mArray[IP];
    }

    template <index_t I>
Chao Liu's avatar
Chao Liu committed
40
    __host__ __device__ constexpr auto& At(Number<I> i)
Chao Liu's avatar
Chao Liu committed
41
42
43
    {
        static_assert(I < Size(), "wrong!");

Chao Liu's avatar
Chao Liu committed
44
        constexpr auto IP = Picks{}[i];
Chao Liu's avatar
Chao Liu committed
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
        return mArray(IP);
    }

    template <index_t I>
    __host__ __device__ constexpr const auto& operator[](Number<I> i) const
    {
        return At(i);
    }

    template <index_t I>
    __host__ __device__ constexpr auto& operator()(Number<I> i)
    {
        return At(i);
    }

    template <typename T>
    __host__ __device__ constexpr auto operator=(const T& a)
    {
        static_assert(T::Size() == Size(), "wrong! size not the same");

        static_for<0, Size(), 1>{}([&](auto i) { operator()(i) = a[i]; });

        return *this;
    }

    private:
    Arr& mArray;
};

template <typename Arr, typename Picks, typename X>
__host__ __device__ constexpr auto operator+=(ArrayElementPicker<Arr, Picks>& y, const X& x)
{
    using Y                 = ArrayElementPicker<Arr, Picks>;
    constexpr index_t nsize = Y::Size();

    static_assert(nsize == X::Size(), "wrong! size not the same");

    static_for<0, nsize, 1>{}([&](auto i) { y(i) += x[i]; });

    return y;
}

template <typename Arr, typename Picks, typename X>
__host__ __device__ constexpr auto operator-=(ArrayElementPicker<Arr, Picks>& y, const X& x)
{
    using Y                 = ArrayElementPicker<Arr, Picks>;
    constexpr index_t nsize = Y::Size();

    static_assert(nsize == X::Size(), "wrong! size not the same");

    static_for<0, nsize, 1>{}([&](auto i) { y(i) -= x[i]; });

    return y;
}

Chao Liu's avatar
Chao Liu committed
100
101
102
103
104
105
template <typename Arr, typename Picks>
__host__ __device__ constexpr auto pick_array_element(Arr& a, Picks)
{
    return ArrayElementPicker<Arr, Picks>(a);
}

Chao Liu's avatar
Chao Liu committed
106
107
} // namespace ck
#endif