Array.hpp 10.2 KB
Newer Older
1
#pragma once
Chao Liu's avatar
Chao Liu committed
2
3
#include "Sequence.hpp"
#include "functional2.hpp"
4

Chao Liu's avatar
Chao Liu committed
5
template <class TData, index_t NSize>
6
7
8
9
struct Array
{
    using Type = Array<TData, NSize>;

Chao Liu's avatar
Chao Liu committed
10
    static constexpr index_t nSize = NSize;
11

Chao Liu's avatar
Chao Liu committed
12
    index_t mData[nSize];
13
14

    template <class... Xs>
Chao Liu's avatar
Chao Liu committed
15
    __host__ __device__ constexpr Array(Xs... xs) : mData{static_cast<TData>(xs)...}
16
17
18
    {
    }

19
20
    __host__ __device__ constexpr index_t GetSize() const { return NSize; }

Chao Liu's avatar
Chao Liu committed
21
22
23
24
25
26
    template <index_t I>
    __host__ __device__ constexpr TData operator[](Number<I>) const
    {
        return mData[I];
    }

Chao Liu's avatar
Chao Liu committed
27
    __host__ __device__ constexpr TData operator[](index_t i) const { return mData[i]; }
28

Chao Liu's avatar
Chao Liu committed
29
30
31
32
33
34
35
    template <index_t I>
    __host__ __device__ TData& operator()(Number<I>)
    {
        return mData[I];
    }

    __host__ __device__ TData& operator()(index_t i) { return mData[i]; }
36

Chao Liu's avatar
Chao Liu committed
37
    template <index_t I>
Chao Liu's avatar
Chao Liu committed
38
    __host__ __device__ constexpr void Set(Number<I>, TData x)
Chao Liu's avatar
Chao Liu committed
39
    {
Chao Liu's avatar
Chao Liu committed
40
41
        static_assert(I < NSize, "wrong!");

Chao Liu's avatar
Chao Liu committed
42
43
44
        mData[I] = x;
    }

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
    __host__ __device__ constexpr void Set(index_t I, TData x) { mData[I] = x; }

    struct lambda_PushBack // emulate constexpr lambda
    {
        const Array<TData, NSize>& old_array;
        Array<TData, NSize + 1>& new_array;

        __host__ __device__ constexpr lambda_PushBack(const Array<TData, NSize>& old_array_,
                                                      Array<TData, NSize + 1>& new_array_)
            : old_array(old_array_), new_array(new_array_)
        {
        }

        template <index_t I>
        __host__ __device__ constexpr void operator()(Number<I>) const
        {
            new_array.Set(Number<I>{}, old_array[I]);
        }
    };

Chao Liu's avatar
Chao Liu committed
65
    __host__ __device__ constexpr auto PushBack(TData x) const
66
67
68
    {
        Array<TData, NSize + 1> new_array;

Chao Liu's avatar
Chao Liu committed
69
        static_for<0, NSize, 1>{}(lambda_PushBack(*this, new_array));
70

Chao Liu's avatar
Chao Liu committed
71
        new_array.Set(Number<NSize>{}, x);
72
73
74

        return new_array;
    }
75
};
76

Chao Liu's avatar
Chao Liu committed
77
78
79
80
81
82
83
84
85
template <index_t... Is>
__host__ __device__ constexpr auto sequence2array(Sequence<Is...>)
{
    return Array<index_t, sizeof...(Is)>{Is...};
}

template <class TData, index_t NSize>
__host__ __device__ constexpr auto make_zero_array()
{
Chao Liu's avatar
Chao Liu committed
86
87
88
    constexpr auto zero_sequence = typename uniform_sequence_gen<NSize, 0>::SeqType{};
    constexpr auto zero_array    = sequence2array(zero_sequence);
    return zero_array;
Chao Liu's avatar
Chao Liu committed
89
90
}

91
template <class TData, index_t NSize, index_t... IRs>
Chao Liu's avatar
Chao Liu committed
92
__host__ __device__ constexpr auto reorder_array_given_new2old(const Array<TData, NSize>& old_array,
Chao Liu's avatar
Chao Liu committed
93
                                                               Sequence<IRs...> /*new2old*/)
94
95
96
{
    static_assert(NSize == sizeof...(IRs), "NSize not consistent");

Chao Liu's avatar
Chao Liu committed
97
    static_assert(is_valid_sequence_map<Sequence<IRs...>>::value, "wrong! invalid reorder map");
98

Chao Liu's avatar
Chao Liu committed
99
    return Array<TData, NSize>{old_array.mSize[IRs]...};
100
101
}

Chao Liu's avatar
Chao Liu committed
102
template <class TData, index_t NSize, class MapOld2New>
Chao Liu's avatar
Chao Liu committed
103
struct lambda_reorder_array_given_old2new
Chao Liu's avatar
Chao Liu committed
104
{
Chao Liu's avatar
Chao Liu committed
105
106
    const Array<TData, NSize>& old_array;
    Array<TData, NSize>& new_array;
Chao Liu's avatar
Chao Liu committed
107

Chao Liu's avatar
Chao Liu committed
108
109
110
    __host__ __device__ constexpr lambda_reorder_array_given_old2new(
        const Array<TData, NSize>& old_array_, Array<TData, NSize>& new_array_)
        : old_array(old_array_), new_array(new_array_)
Chao Liu's avatar
Chao Liu committed
111
112
113
114
115
116
    {
    }

    template <index_t IOldDim>
    __host__ __device__ constexpr void operator()(Number<IOldDim>) const
    {
Chao Liu's avatar
Chao Liu committed
117
        TData old_data = old_array[IOldDim];
Chao Liu's avatar
Chao Liu committed
118
119
120

        constexpr index_t INewDim = MapOld2New::Get(Number<IOldDim>{});

Chao Liu's avatar
Chao Liu committed
121
        new_array.Set(Number<INewDim>{}, old_data);
Chao Liu's avatar
Chao Liu committed
122
123
124
125
126
    }
};

template <class TData, index_t NSize, index_t... IRs>
__host__ __device__ constexpr auto reorder_array_given_old2new(const Array<TData, NSize>& old_array,
Chao Liu's avatar
Chao Liu committed
127
                                                               Sequence<IRs...> /*old2new*/)
Chao Liu's avatar
Chao Liu committed
128
129
130
131
132
{
    Array<TData, NSize> new_array;

    static_assert(NSize == sizeof...(IRs), "NSize not consistent");

Chao Liu's avatar
Chao Liu committed
133
134
    static_assert(is_valid_sequence_map<Sequence<IRs...>>::value, "wrong! invalid reorder map");

Chao Liu's avatar
Chao Liu committed
135
    static_for<0, NSize, 1>{}(
Chao Liu's avatar
Chao Liu committed
136
        lambda_reorder_array_given_old2new<TData, NSize, Sequence<IRs...>>(old_array, new_array));
Chao Liu's avatar
Chao Liu committed
137
138
139

    return new_array;
}
Chao Liu's avatar
Chao Liu committed
140

141
template <class TData, index_t NSize, class ExtractSeq>
Chao Liu's avatar
Chao Liu committed
142
__host__ __device__ constexpr auto extract_array(const Array<TData, NSize>& old_array, ExtractSeq)
143
144
145
146
147
148
149
{
    Array<TData, ExtractSeq::GetSize()> new_array;

    constexpr index_t new_size = ExtractSeq::GetSize();

    static_assert(new_size <= NSize, "wrong! too many extract");

Chao Liu's avatar
Chao Liu committed
150
    static_for<0, new_size, 1>{}([&](auto I) { new_array(I) = old_array[ExtractSeq::Get(I)]; });
151
152
153
154

    return new_array;
}

Chao Liu's avatar
Chao Liu committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
template <class F, class X, class Y, class Z> // emulate constepxr lambda for array math
struct lambda_array_math
{
    const F& f;
    const X& x;
    const Y& y;
    Z& z;

    __host__ __device__ constexpr lambda_array_math(const F& f_, const X& x_, const Y& y_, Z& z_)
        : f(f_), x(x_), y(y_), z(z_)
    {
    }

    template <index_t IDim_>
    __host__ __device__ constexpr void operator()(Number<IDim_>) const
    {
        constexpr auto IDim = Number<IDim_>{};

        z.Set(IDim, f(x[IDim], y[IDim]));
    }
};

177
// Array = Array + Array
Chao Liu's avatar
Chao Liu committed
178
template <class TData, index_t NSize>
Chao Liu's avatar
Chao Liu committed
179
__host__ __device__ constexpr auto operator+(Array<TData, NSize> a, Array<TData, NSize> b)
Chao Liu's avatar
Chao Liu committed
180
181
182
{
    Array<TData, NSize> result;

Chao Liu's avatar
Chao Liu committed
183
    auto f = mod_conv::plus<index_t>{};
Chao Liu's avatar
Chao Liu committed
184

Chao Liu's avatar
Chao Liu committed
185
186
187
    static_for<0, NSize, 1>{}(
        lambda_array_math<decltype(f), decltype(a), decltype(b), decltype(result)>(
            f, a, b, result));
Chao Liu's avatar
Chao Liu committed
188
189
190

    return result;
}
Chao Liu's avatar
Chao Liu committed
191

192
193
194
195
196
197
// Array = Array - Array
template <class TData, index_t NSize>
__host__ __device__ constexpr auto operator-(Array<TData, NSize> a, Array<TData, NSize> b)
{
    Array<TData, NSize> result;

Chao Liu's avatar
Chao Liu committed
198
    auto f = mod_conv::minus<index_t>{};
199

Chao Liu's avatar
Chao Liu committed
200
201
202
    static_for<0, NSize, 1>{}(
        lambda_array_math<decltype(f), decltype(a), decltype(b), decltype(result)>(
            f, a, b, result));
203
204
205
206
207
208
209
210
211
212
213
214

    return result;
}

// Array = Array + Sequence
template <class TData, index_t NSize, index_t... Is>
__host__ __device__ constexpr auto operator+(Array<TData, NSize> a, Sequence<Is...> b)
{
    static_assert(sizeof...(Is) == NSize, "wrong! size not the same");

    Array<TData, NSize> result;

Chao Liu's avatar
Chao Liu committed
215
    auto f = mod_conv::plus<index_t>{};
216

Chao Liu's avatar
Chao Liu committed
217
218
219
    static_for<0, NSize, 1>{}(
        lambda_array_math<decltype(f), decltype(a), decltype(b), decltype(result)>(
            f, a, b, result));
220
221
222
223
224
225
226
227
228
229
230
231

    return result;
}

// Array = Array - Sequence
template <class TData, index_t NSize, index_t... Is>
__host__ __device__ constexpr auto operator-(Array<TData, NSize> a, Sequence<Is...> b)
{
    static_assert(sizeof...(Is) == NSize, "wrong! size not the same");

    Array<TData, NSize> result;

Chao Liu's avatar
Chao Liu committed
232
    auto f = mod_conv::minus<index_t>{};
233

Chao Liu's avatar
Chao Liu committed
234
235
236
    static_for<0, NSize, 1>{}(
        lambda_array_math<decltype(f), decltype(a), decltype(b), decltype(result)>(
            f, a, b, result));
237
238
239
240

    return result;
}

Chao Liu's avatar
Chao Liu committed
241
242
243
244
245
246
247
248
// Array = Array * Sequence
template <class TData, index_t NSize, index_t... Is>
__host__ __device__ constexpr auto operator*(Array<TData, NSize> a, Sequence<Is...> b)
{
    static_assert(sizeof...(Is) == NSize, "wrong! size not the same");

    Array<TData, NSize> result;

Chao Liu's avatar
Chao Liu committed
249
    auto f = mod_conv::multiplies<index_t>{};
Chao Liu's avatar
Chao Liu committed
250

Chao Liu's avatar
Chao Liu committed
251
252
253
    static_for<0, NSize, 1>{}(
        lambda_array_math<decltype(f), decltype(a), decltype(b), decltype(result)>(
            f, a, b, result));
Chao Liu's avatar
Chao Liu committed
254
255
256

    return result;
}
257

258
259
260
// Array = Sequence - Array
template <class TData, index_t NSize, index_t... Is>
__host__ __device__ constexpr auto operator-(Sequence<Is...> a, Array<TData, NSize> b)
261
{
262
263
264
    static_assert(sizeof...(Is) == NSize, "wrong! size not the same");

    Array<TData, NSize> result;
265

Chao Liu's avatar
Chao Liu committed
266
    auto f = mod_conv::minus<index_t>{};
267

Chao Liu's avatar
Chao Liu committed
268
269
270
    static_for<0, NSize, 1>{}(
        lambda_array_math<decltype(f), decltype(a), decltype(b), decltype(result)>(
            f, a, b, result));
271
272
273
274
275
276
277
278
279
280
281
282

    return result;
}

template <class TData, index_t NSize, class Reduce>
__host__ __device__ constexpr TData
accumulate_on_array(const Array<TData, NSize>& a, Reduce f, TData init)
{
    TData result = init;

    static_assert(NSize > 0, "wrong");

Chao Liu's avatar
Chao Liu committed
283
    static_for<0, NSize, 1>{}([&](auto I) { result = f(result, a[I]); });
284
285
286

    return result;
}
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

template <class T, index_t NSize>
__host__ __device__ void print_Array(const char* s, Array<T, NSize> a)
{
    constexpr index_t nsize = a.GetSize();

    static_assert(nsize > 0 && nsize <= 10, "wrong!");

    static_if<nsize == 1>{}([&](auto) { printf("%s size %u, {%u}\n", s, nsize, a[0]); });

    static_if<nsize == 2>{}([&](auto) { printf("%s size %u, {%u %u}\n", s, nsize, a[0], a[1]); });

    static_if<nsize == 3>{}(
        [&](auto) { printf("%s size %u, {%u %u %u}\n", s, nsize, a[0], a[1], a[2]); });

    static_if<nsize == 4>{}(
        [&](auto) { printf("%s size %u, {%u %u %u %u}\n", s, nsize, a[0], a[1], a[2], a[3]); });

    static_if<nsize == 5>{}([&](auto) {
        printf("%s size %u, {%u %u %u %u %u}\n", s, nsize, a[0], a[1], a[2], a[3], a[4]);
    });

    static_if<nsize == 6>{}([&](auto) {
        printf("%s size %u, {%u %u %u %u %u %u}\n", s, nsize, a[0], a[1], a[2], a[3], a[4], a[5]);
    });

    static_if<nsize == 7>{}([&](auto) {
        printf("%s size %u, {%u %u %u %u %u %u %u}\n",
               s,
               nsize,
               a[0],
               a[1],
               a[2],
               a[3],
               a[4],
               a[5],
               a[6]);
    });

    static_if<nsize == 8>{}([&](auto) {
        printf("%s size %u, {%u %u %u %u %u %u %u %u}\n",
               s,
               nsize,
               a[0],
               a[1],
               a[2],
               a[3],
               a[4],
               a[5],
               a[6],
               a[7]);
    });

    static_if<nsize == 9>{}([&](auto) {
        printf("%s size %u, {%u %u %u %u %u %u %u %u %u}\n",
               s,
               nsize,
               a[0],
               a[1],
               a[2],
               a[3],
               a[4],
               a[5],
               a[6],
               a[7],
               a[8]);
    });

    static_if<nsize == 10>{}([&](auto) {
        printf("%s size %u, {%u %u %u %u %u %u %u %u %u %u}\n",
               s,
               nsize,
               a[0],
               a[1],
               a[2],
               a[3],
               a[4],
               a[5],
               a[6],
               a[7],
               a[8],
               a[9]);
    });
}