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

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

6
7
8
9
#include "ck/utility/integral_constant.hpp"
#include "ck/utility/sequence.hpp"
#include "ck/utility/type.hpp"
#include "ck/utility/enable_if.hpp"
Chao Liu's avatar
Chao Liu committed
10
11
12

namespace ck {

Chao Liu's avatar
Chao Liu committed
13
namespace detail {
Chao Liu's avatar
Chao Liu committed
14

Chao Liu's avatar
Chao Liu committed
15
16
17
template <index_t>
struct TupleElementKey
{
Chao Liu's avatar
Chao Liu committed
18
    __host__ __device__ constexpr TupleElementKey() = default;
Chao Liu's avatar
Chao Liu committed
19
};
Chao Liu's avatar
Chao Liu committed
20

Chao Liu's avatar
Chao Liu committed
21
template <typename Key, typename Data>
22
struct TupleElementKeyData
Chao Liu's avatar
Chao Liu committed
23
{
24
25
26
27
28
#if 0 // workaround compiler complaint about implicitly-deleted default constructor
    __host__ __device__ constexpr TupleElementKeyData() = default;
#else
    __host__ __device__ constexpr TupleElementKeyData() : mData{} {}
#endif
Chao Liu's avatar
Chao Liu committed
29

30
31
32
33
    template <typename T,
              typename enable_if<!is_same<remove_cvref_t<T>, TupleElementKeyData>::value,
                                 bool>::type = false>
    __host__ __device__ constexpr TupleElementKeyData(T&& v) : mData(std::forward<T>(v))
Chao Liu's avatar
Chao Liu committed
34
35
36
    {
    }

Chao Liu's avatar
Chao Liu committed
37
    Data mData;
Chao Liu's avatar
Chao Liu committed
38
39
};

Chao Liu's avatar
Chao Liu committed
40
template <typename Key, typename Data>
41
42
__host__ __device__ constexpr const Data&
get_tuple_element_data(const TupleElementKeyData<Key, Data>& x)
Chao Liu's avatar
Chao Liu committed
43
{
Chao Liu's avatar
Chao Liu committed
44
    return static_cast<const Data&>(x.mData);
Chao Liu's avatar
Chao Liu committed
45
}
Chao Liu's avatar
Chao Liu committed
46

Chao Liu's avatar
Chao Liu committed
47
template <typename Key, typename Data>
48
__host__ __device__ constexpr Data& get_tuple_element_data(TupleElementKeyData<Key, Data>& x)
Chao Liu's avatar
Chao Liu committed
49
{
Chao Liu's avatar
Chao Liu committed
50
51
    return x.mData;
}
Chao Liu's avatar
Chao Liu committed
52

Chao Liu's avatar
Chao Liu committed
53
// TODO: not sure the use of reference is correct
Chao Liu's avatar
Chao Liu committed
54
template <typename Key, typename Data>
55
__host__ __device__ constexpr Data&& get_tuple_element_data(TupleElementKeyData<Key, Data>&& x)
Chao Liu's avatar
Chao Liu committed
56
{
Chao Liu's avatar
Chao Liu committed
57
58
    return static_cast<Data&&>(x.mData);
}
Chao Liu's avatar
Chao Liu committed
59

Chao Liu's avatar
Chao Liu committed
60
61
62
63
template <typename Indices, typename... Xs>
struct TupleImpl;

template <index_t... Is, typename... Xs>
64
struct TupleImpl<Sequence<Is...>, Xs...> : TupleElementKeyData<TupleElementKey<Is>, Xs>...
Chao Liu's avatar
Chao Liu committed
65
{
Chao Liu's avatar
Chao Liu committed
66
67
    __host__ __device__ constexpr TupleImpl() = default;

Chao Liu's avatar
Chao Liu committed
68
69
    template <typename Y,
              typename enable_if<sizeof...(Is) == 1 && sizeof...(Xs) == 1 &&
Chao Liu's avatar
Chao Liu committed
70
                                     !is_same<remove_cvref_t<Y>, TupleImpl>::value,
Chao Liu's avatar
Chao Liu committed
71
                                 bool>::type = false>
Chao Liu's avatar
Chao Liu committed
72
    __host__ __device__ constexpr TupleImpl(Y&& y)
73
        : TupleElementKeyData<TupleElementKey<Is>, Xs>(std::forward<Y>(y))...
Chao Liu's avatar
Chao Liu committed
74
75
76
    {
    }

Chao Liu's avatar
Chao Liu committed
77
    template <typename... Ys, typename enable_if<sizeof...(Ys) >= 2, bool>::type = false>
Chao Liu's avatar
Chao Liu committed
78
    __host__ __device__ constexpr TupleImpl(Ys&&... ys)
79
        : TupleElementKeyData<TupleElementKey<Is>, Xs>(std::forward<Ys>(ys))...
Chao Liu's avatar
Chao Liu committed
80
    {
Chao Liu's avatar
Chao Liu committed
81
82
        static_assert(sizeof...(Is) == sizeof...(Xs) && sizeof...(Is) == sizeof...(Ys),
                      "wrong! inconsistent size");
Chao Liu's avatar
Chao Liu committed
83
84
85
86
87
    }

    __host__ __device__ static constexpr index_t Size() { return sizeof...(Xs); }

    template <index_t I>
88
    __host__ __device__ constexpr const auto& GetElementDataByKey(TupleElementKey<I>) const
Chao Liu's avatar
Chao Liu committed
89
    {
90
        return get_tuple_element_data<TupleElementKey<I>>(*this);
Chao Liu's avatar
Chao Liu committed
91
92
93
    }

    template <index_t I>
94
    __host__ __device__ constexpr auto& GetElementDataByKey(TupleElementKey<I>)
Chao Liu's avatar
Chao Liu committed
95
    {
96
        return get_tuple_element_data<TupleElementKey<I>>(*this);
Chao Liu's avatar
Chao Liu committed
97
    }
Chao Liu's avatar
Chao Liu committed
98
99
};

Chao Liu's avatar
Chao Liu committed
100
101
102
103
} // namespace detail

template <typename... Xs>
struct Tuple : detail::TupleImpl<typename arithmetic_sequence_gen<0, sizeof...(Xs), 1>::type, Xs...>
Chao Liu's avatar
Chao Liu committed
104
{
Chao Liu's avatar
Chao Liu committed
105
106
107
    using base =
        detail::TupleImpl<typename arithmetic_sequence_gen<0, sizeof...(Xs), 1>::type, Xs...>;

Chao Liu's avatar
Chao Liu committed
108
109
110
    __host__ __device__ constexpr Tuple() = default;

    template <typename Y,
Chao Liu's avatar
Chao Liu committed
111
              typename enable_if<sizeof...(Xs) == 1 && !is_same<remove_cvref_t<Y>, Tuple>::value,
Chao Liu's avatar
Chao Liu committed
112
                                 bool>::type = false>
Chao Liu's avatar
Chao Liu committed
113
    __host__ __device__ constexpr Tuple(Y&& y) : base(std::forward<Y>(y))
Chao Liu's avatar
Chao Liu committed
114
115
116
    {
    }

Chao Liu's avatar
Chao Liu committed
117
    template <typename... Ys,
Chao Liu's avatar
Chao Liu committed
118
119
              typename enable_if<sizeof...(Ys) == sizeof...(Xs) && sizeof...(Ys) >= 2, bool>::type =
                  false>
Chao Liu's avatar
Chao Liu committed
120
121
122
123
124
125
    __host__ __device__ constexpr Tuple(Ys&&... ys) : base(std::forward<Ys>(ys)...)
    {
    }

    __host__ __device__ static constexpr index_t Size() { return sizeof...(Xs); }

126
    // read access
Chao Liu's avatar
Chao Liu committed
127
128
129
130
    template <index_t I>
    __host__ __device__ constexpr const auto& At(Number<I>) const
    {
        static_assert(I < base::Size(), "wrong! out of range");
131
        return base::GetElementDataByKey(detail::TupleElementKey<I>{});
Chao Liu's avatar
Chao Liu committed
132
133
    }

134
    // write access
Chao Liu's avatar
Chao Liu committed
135
136
137
138
    template <index_t I>
    __host__ __device__ constexpr auto& At(Number<I>)
    {
        static_assert(I < base::Size(), "wrong! out of range");
139
        return base::GetElementDataByKey(detail::TupleElementKey<I>{});
Chao Liu's avatar
Chao Liu committed
140
    }
Chao Liu's avatar
Chao Liu committed
141

142
    // read access
Chao Liu's avatar
Chao Liu committed
143
144
145
146
147
    template <index_t I>
    __host__ __device__ constexpr const auto& operator[](Number<I> i) const
    {
        return At(i);
    }
148

149
    // write access
Chao Liu's avatar
Chao Liu committed
150
151
152
153
154
    template <index_t I>
    __host__ __device__ constexpr auto& operator()(Number<I> i)
    {
        return At(i);
    }
Chao Liu's avatar
Chao Liu committed
155

Chao Liu's avatar
Chao Liu committed
156
157
158
159
    template <typename T>
    __host__ __device__ constexpr auto operator=(const T& a)
    {
        static_assert(T::Size() == Size(), "wrong! size not the same");
Chao Liu's avatar
Chao Liu committed
160

Chao Liu's avatar
Chao Liu committed
161
        static_for<0, Size(), 1>{}([&](auto i) { operator()(i) = a[i]; });
Chao Liu's avatar
Chao Liu committed
162

Chao Liu's avatar
Chao Liu committed
163
164
        return *this;
    }
zjing14's avatar
zjing14 committed
165
166

    __host__ __device__ static constexpr bool IsStaticBuffer() { return true; }
Chao Liu's avatar
Chao Liu committed
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
template <>
struct Tuple<>
{
    __host__ __device__ constexpr Tuple() = default;

    __host__ __device__ static constexpr index_t Size() { return 0; }

    template <typename T>
    __host__ __device__ constexpr auto operator=(const T&)
    {
        return *this;
    }

    __host__ __device__ static constexpr bool IsStaticBuffer() { return true; }
};

template <index_t I, typename TTuple>
struct tuple_element
{
    using type = decltype(TTuple{}.At(Number<I>{}));
};

template <index_t I, typename TTuple>
using tuple_element_t = typename tuple_element<I, TTuple>::type;

Chao Liu's avatar
Chao Liu committed
194
195
template <typename... Xs>
__host__ __device__ constexpr auto make_tuple(Xs&&... xs)
Chao Liu's avatar
Chao Liu committed
196
{
Chao Liu's avatar
Chao Liu committed
197
    return Tuple<remove_cvref_t<Xs>...>(std::forward<Xs>(xs)...);
Chao Liu's avatar
Chao Liu committed
198
199
}

200
201
202
203
204
205
206
// https://en.cppreference.com/w/cpp/utility/tuple/tie
template <typename... Args>
constexpr Tuple<Args&...> tie(Args&... args) noexcept
{
    return {args...};
}

Chao Liu's avatar
Chao Liu committed
207
} // namespace ck