tuple_helper.hpp 9.17 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
2
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
Chao Liu's avatar
Chao Liu committed
3

4
#pragma once
Chao Liu's avatar
Chao Liu committed
5
6
7

#include "functional4.hpp"
#include "tuple.hpp"
8
#include "is_detected.hpp"
Chao Liu's avatar
Chao Liu committed
9
10
11
12
13
14

namespace ck {

template <typename F, index_t N>
__host__ __device__ constexpr auto generate_tuple(F&& f, Number<N>)
{
Chao Liu's avatar
Chao Liu committed
15
    return unpack([&f](auto&&... is) { return make_tuple(f(is)...); },
Chao Liu's avatar
Chao Liu committed
16
17
18
                  typename arithmetic_sequence_gen<0, N, 1>::type{});
}

19
20
21
template <typename F, index_t N>
__host__ __device__ constexpr auto generate_tie(F&& f, Number<N>)
{
Chao Liu's avatar
Chao Liu committed
22
    return unpack([&f](auto&&... is) { return tie(f(is)...); },
23
24
25
                  typename arithmetic_sequence_gen<0, N, 1>::type{});
}

26
27
28
29
30
31
32
33
34
35
36
// tx and ty are tuple of references, return type of will tuple of referennce (not rvalue)
template <typename... X, typename... Y>
__host__ __device__ constexpr auto concat_tuple_of_reference(const Tuple<X&...>& tx,
                                                             const Tuple<Y&...>& ty)
{
    return unpack2(
        [&](auto&&... zs) { return Tuple<decltype(zs)...>{std::forward<decltype(zs)>(zs)...}; },
        tx,
        ty);
}

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
template <typename... X, typename... Y>
__host__ __device__ constexpr auto concat_tuple(const Tuple<X...>& tx, const Tuple<Y...>& ty)
{
    return unpack2(
        [&](auto... zs) { return Tuple<decltype(zs)...>{std::forward<decltype(zs)>(zs)...}; },
        tx,
        ty);
}

// Support any number of tuples to concat (also 1)
template <typename... X>
__host__ __device__ constexpr auto concat_tuple(const Tuple<X...>& tx)
{
    return tx;
}

template <typename... X, typename... Tuples>
__host__ __device__ constexpr auto concat_tuple(const Tuple<X...>& tx, const Tuples&... tuples)
{
    return concat_tuple(tx, concat_tuple(tuples...));
}

Chao Liu's avatar
Chao Liu committed
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
namespace detail {

template <typename F, typename X, index_t... Is>
__host__ __device__ constexpr auto transform_tuples_impl(F f, const X& x, Sequence<Is...>)
{
    return make_tuple(f(x.At(Number<Is>{}))...);
}

template <typename F, typename X, typename Y, index_t... Is>
__host__ __device__ constexpr auto
transform_tuples_impl(F f, const X& x, const Y& y, Sequence<Is...>)
{
    return make_tuple(f(x.At(Number<Is>{}), y.At(Number<Is>{}))...);
}

template <typename F, typename X, typename Y, typename Z, index_t... Is>
__host__ __device__ constexpr auto
transform_tuples_impl(F f, const X& x, const Y& y, const Z& z, Sequence<Is...>)
{
    return make_tuple(f(x.At(Number<Is>{}), y.At(Number<Is>{}), z.At(Number<Is>{}))...);
}

} // namespace detail

template <typename F, typename X>
__host__ __device__ constexpr auto transform_tuples(F f, const X& x)
{
    return detail::transform_tuples_impl(
        f, x, typename arithmetic_sequence_gen<0, X::Size(), 1>::type{});
}

template <typename F, typename X, typename Y>
__host__ __device__ constexpr auto transform_tuples(F f, const X& x, const Y& y)
{
    return detail::transform_tuples_impl(
        f, x, y, typename arithmetic_sequence_gen<0, X::Size(), 1>::type{});
}

template <typename F, typename X, typename Y, typename Z>
__host__ __device__ constexpr auto transform_tuples(F f, const X& x, const Y& y, const Z& z)
{
    return detail::transform_tuples_impl(
        f, x, y, z, typename arithmetic_sequence_gen<0, X::Size(), 1>::type{});
}

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
// By default unroll to the flatten
template <index_t Depth = 0, index_t MaxDepth = -1>
__host__ __device__ constexpr auto UnrollNestedTuple(const Tuple<>& element)
{
    return element;
}

template <index_t Depth = 0, index_t MaxDepth = -1, typename T>
__host__ __device__ constexpr auto UnrollNestedTuple(const T& element)
{
    return make_tuple(element);
}

template <index_t Depth = 0, index_t MaxDepth = -1, typename... Ts>
__host__ __device__ constexpr auto UnrollNestedTuple(const Tuple<Ts...>& tuple)
{
    if constexpr(Depth == MaxDepth)
    {
        return tuple;
    }
    else
    {
        return unpack(
            [&](auto&&... ts) {
                return concat_tuple(UnrollNestedTuple<Depth + 1, MaxDepth>(ts)...);
            },
            tuple);
    }
}

template <typename... Ts>
__host__ __device__ constexpr auto TupleReverse(const Tuple<Ts...>& tuple)
{
    return generate_tuple(
        [&](auto i) {
            using Idx = Number<Tuple<Ts...>::Size() - i - 1>;
            return tuple.At(Idx{});
        },
        Number<Tuple<Ts...>::Size()>{});
}

// Reduce tuple values in specific range using Function
template <index_t Idx, index_t End, typename F, typename... Ts>
__host__ __device__ constexpr auto TupleReduce(F&& f, const Tuple<Ts...>& tuple)
{
    static_assert(Idx < End, "Wrong parameters for TupleReduce");
    if constexpr(Idx + 1 == End)
    {
        return tuple.At(Number<Idx>{});
    }
    else
    {
        return f(tuple.At(Number<Idx>{}), TupleReduce<Idx + 1, End>(f, tuple));
    }
}

template <typename T>
using is_tuple = decltype(std::declval<T&>().IsTuple());

template <typename... Ts>
__host__ __device__ constexpr auto IsNestedTuple(const Tuple<Ts...>&)
{
    return (is_detected<is_tuple, Ts>::value || ...);
}

Chao Liu's avatar
Chao Liu committed
169
} // namespace ck
Chao Liu's avatar
Chao Liu committed
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

// Macro function
// convert constexpr Array to Tuple of Number
#define TO_TUPLE_OF_NUMBER(arr, n)                                                              \
    [&arr, &n] {                                                                                \
        static_assert(arr.Size() >= n, "wrong! out of bound");                                  \
                                                                                                \
        static_assert(n < 7, "not implemented");                                                \
                                                                                                \
        if constexpr(n == 0)                                                                    \
        {                                                                                       \
            return ck::Tuple<>{};                                                               \
        }                                                                                       \
        else if constexpr(n == 1)                                                               \
        {                                                                                       \
            return ck::Tuple<Number<arr[0]>>{};                                                 \
        }                                                                                       \
        else if constexpr(n == 2)                                                               \
        {                                                                                       \
            return ck::Tuple<Number<arr[0]>, Number<arr[1]>>{};                                 \
        }                                                                                       \
        else if constexpr(n == 3)                                                               \
        {                                                                                       \
            return ck::Tuple<Number<arr[0]>, Number<arr[1]>, Number<arr[2]>>{};                 \
        }                                                                                       \
        else if constexpr(n == 4)                                                               \
        {                                                                                       \
            return ck::Tuple<Number<arr[0]>, Number<arr[1]>, Number<arr[2]>, Number<arr[3]>>{}; \
        }                                                                                       \
        else if constexpr(n == 5)                                                               \
        {                                                                                       \
            return ck::Tuple<Number<arr[0]>,                                                    \
                             Number<arr[1]>,                                                    \
                             Number<arr[2]>,                                                    \
                             Number<arr[3]>,                                                    \
                             Number<arr[4]>>{};                                                 \
        }                                                                                       \
        else if constexpr(n == 6)                                                               \
        {                                                                                       \
            return ck::Tuple<Number<arr[0]>,                                                    \
                             Number<arr[1]>,                                                    \
                             Number<arr[2]>,                                                    \
                             Number<arr[3]>,                                                    \
                             Number<arr[4]>,                                                    \
                             Number<arr[5]>>{};                                                 \
        }                                                                                       \
    }()