"docs/vscode:/vscode.git/clone" did not exist on "e3b49faf3cd586540228f4ae0c6c79cfd9450643"
container_element_picker.hpp 4.03 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef CK_CONTAINER_ELEMENT_PICKER_HPP
#define CK_CONTAINER_ELEMENT_PICKER_HPP

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

namespace ck {

// Arr: Array or StaticallyIndexedArray
// Picks: Sequence<...>
template <typename Arr, typename Picks>
struct ContainerElementPicker
{
    using type = ContainerElementPicker;
#if 0
    using data_type = typename Arr::data_type;
#endif

    __host__ __device__ constexpr ContainerElementPicker() = delete;

    __host__ __device__ constexpr ContainerElementPicker(Arr& array) : mArray{array}
    {
Chao Liu's avatar
Chao Liu committed
23
24
        constexpr index_t imax =
            reduce_on_sequence(Picks{}, math::maximize<index_t>{}, Number<0>{});
Chao Liu's avatar
Chao Liu committed
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

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

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

    template <index_t I>
    __host__ __device__ constexpr const auto& At(Number<I> i) const
    {
        static_assert(I < Size(), "wrong!");

        constexpr auto IP = Picks{}[i];
        return mArray[IP];
    }

    template <index_t I>
    __host__ __device__ constexpr auto& At(Number<I> i)
    {
        static_assert(I < Size(), "wrong!");

        constexpr auto IP = Picks{}[i];
        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;
};

// Arr: Array or StaticallyIndexedArray
// Picks: Sequence<...>
template <typename Arr, typename Picks>
struct ConstantContainerElementPicker
{
    using type = ConstantContainerElementPicker;
#if 0
    using data_type = typename Arr::data_type;
#endif

    __host__ __device__ constexpr ConstantContainerElementPicker() = delete;

    __host__ __device__ constexpr ConstantContainerElementPicker(const Arr& array) : mArray{array}
    {
Chao Liu's avatar
Chao Liu committed
89
90
        constexpr index_t imax =
            reduce_on_sequence(Picks{}, math::maximize<index_t>{}, Number<0>{});
Chao Liu's avatar
Chao Liu committed
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

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

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

    template <index_t I>
    __host__ __device__ constexpr const auto& At(Number<I> i) const
    {
        static_assert(I < Size(), "wrong!");

        constexpr auto IP = Picks{}[i];
        return mArray[IP];
    }

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

    private:
    const Arr& mArray;
};

template <typename Arr, typename Picks, typename X>
__host__ __device__ constexpr auto operator+=(ContainerElementPicker<Arr, Picks>& y, const X& x)
{
    using Y                 = ContainerElementPicker<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-=(ContainerElementPicker<Arr, Picks>& y, const X& x)
{
    using Y                 = ContainerElementPicker<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>
__host__ __device__ constexpr auto pick_container_element(Arr& a, Picks)
{
    return ContainerElementPicker<Arr, Picks>(a);
}

template <typename Arr, typename Picks>
__host__ __device__ constexpr auto pick_container_element(const Arr& a, Picks)
{
    return ConstantContainerElementPicker<Arr, Picks>(a);
}

} // namespace ck
#endif