tuple.hpp 6.52 KB
Newer Older
Umang Yadav's avatar
Umang Yadav committed
1
2
3

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
Chao Liu's avatar
Chao Liu committed
4
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
5
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
Chao Liu's avatar
Chao Liu committed
6

7
#pragma once
Chao Liu's avatar
Chao Liu committed
8

9
10
11
12
#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
13
14
15

namespace ck {

Chao Liu's avatar
Chao Liu committed
16
namespace detail {
Chao Liu's avatar
Chao Liu committed
17

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

Chao Liu's avatar
Chao Liu committed
24
template <typename Key, typename Data>
25
struct TupleElementKeyData
Chao Liu's avatar
Chao Liu committed
26
{
27
28
    using DataType = Data;

29
30
31
32
33
#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
34

35
36
37
38
    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
39
40
41
    {
    }

42
    DataType mData;
Chao Liu's avatar
Chao Liu committed
43
44
};

45
// for read access of tuple element
Chao Liu's avatar
Chao Liu committed
46
template <typename Key, typename Data>
47
__host__ __device__ constexpr const Data&
48
get_tuple_element_data_reference(const TupleElementKeyData<Key, Data>& x)
Chao Liu's avatar
Chao Liu committed
49
{
Chao Liu's avatar
Chao Liu committed
50
    return static_cast<const Data&>(x.mData);
Chao Liu's avatar
Chao Liu committed
51
}
Chao Liu's avatar
Chao Liu committed
52

53
// for write access of tuple element
Chao Liu's avatar
Chao Liu committed
54
template <typename Key, typename Data>
55
56
__host__ __device__ constexpr Data&
get_tuple_element_data_reference(TupleElementKeyData<Key, Data>& x)
Chao Liu's avatar
Chao Liu committed
57
{
Chao Liu's avatar
Chao Liu committed
58
59
    return x.mData;
}
Chao Liu's avatar
Chao Liu committed
60

Chao Liu's avatar
Chao Liu committed
61
// TODO: not sure the use of reference is correct
Chao Liu's avatar
Chao Liu committed
62
template <typename Key, typename Data>
63
64
__host__ __device__ constexpr Data&&
get_tuple_element_data_reference(TupleElementKeyData<Key, Data>&& x)
Chao Liu's avatar
Chao Liu committed
65
{
Chao Liu's avatar
Chao Liu committed
66
67
    return static_cast<Data&&>(x.mData);
}
Chao Liu's avatar
Chao Liu committed
68

69
70
71
72
73
74
75
// for infering type of tuple element
template <typename Key, typename Data>
__host__ __device__ constexpr Data get_tuple_element_data(const TupleElementKeyData<Key, Data>& x)
{
    return std::forward(x.mData);
}

Chao Liu's avatar
Chao Liu committed
76
77
78
79
template <typename Indices, typename... Xs>
struct TupleImpl;

template <index_t... Is, typename... Xs>
80
struct TupleImpl<Sequence<Is...>, Xs...> : TupleElementKeyData<TupleElementKey<Is>, Xs>...
Chao Liu's avatar
Chao Liu committed
81
{
Chao Liu's avatar
Chao Liu committed
82
83
    __host__ __device__ constexpr TupleImpl() = default;

Chao Liu's avatar
Chao Liu committed
84
85
    template <typename Y,
              typename enable_if<sizeof...(Is) == 1 && sizeof...(Xs) == 1 &&
Chao Liu's avatar
Chao Liu committed
86
                                     !is_same<remove_cvref_t<Y>, TupleImpl>::value,
Chao Liu's avatar
Chao Liu committed
87
                                 bool>::type = false>
Chao Liu's avatar
Chao Liu committed
88
    __host__ __device__ constexpr TupleImpl(Y&& y)
89
        : TupleElementKeyData<TupleElementKey<Is>, Xs>(std::forward<Y>(y))...
Chao Liu's avatar
Chao Liu committed
90
91
92
    {
    }

Chao Liu's avatar
Chao Liu committed
93
    template <typename... Ys, typename enable_if<sizeof...(Ys) >= 2, bool>::type = false>
Chao Liu's avatar
Chao Liu committed
94
    __host__ __device__ constexpr TupleImpl(Ys&&... ys)
95
        : TupleElementKeyData<TupleElementKey<Is>, Xs>(std::forward<Ys>(ys))...
Chao Liu's avatar
Chao Liu committed
96
    {
Chao Liu's avatar
Chao Liu committed
97
98
        static_assert(sizeof...(Is) == sizeof...(Xs) && sizeof...(Is) == sizeof...(Ys),
                      "wrong! inconsistent size");
Chao Liu's avatar
Chao Liu committed
99
100
101
102
103
    }

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

    template <index_t I>
104
    __host__ __device__ constexpr const auto& GetElementDataByKey(TupleElementKey<I>) const
Chao Liu's avatar
Chao Liu committed
105
    {
106
        return get_tuple_element_data_reference<TupleElementKey<I>>(*this);
Chao Liu's avatar
Chao Liu committed
107
108
109
    }

    template <index_t I>
110
    __host__ __device__ constexpr auto& GetElementDataByKey(TupleElementKey<I>)
Chao Liu's avatar
Chao Liu committed
111
    {
112
        return get_tuple_element_data_reference<TupleElementKey<I>>(*this);
Chao Liu's avatar
Chao Liu committed
113
    }
Chao Liu's avatar
Chao Liu committed
114
115
};

Chao Liu's avatar
Chao Liu committed
116
117
118
119
} // 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
120
{
Chao Liu's avatar
Chao Liu committed
121
122
123
    using base =
        detail::TupleImpl<typename arithmetic_sequence_gen<0, sizeof...(Xs), 1>::type, Xs...>;

Chao Liu's avatar
Chao Liu committed
124
125
126
    __host__ __device__ constexpr Tuple() = default;

    template <typename Y,
Chao Liu's avatar
Chao Liu committed
127
              typename enable_if<sizeof...(Xs) == 1 && !is_same<remove_cvref_t<Y>, Tuple>::value,
Chao Liu's avatar
Chao Liu committed
128
                                 bool>::type = false>
Chao Liu's avatar
Chao Liu committed
129
    __host__ __device__ constexpr Tuple(Y&& y) : base(std::forward<Y>(y))
Chao Liu's avatar
Chao Liu committed
130
131
132
    {
    }

Chao Liu's avatar
Chao Liu committed
133
    template <typename... Ys,
Chao Liu's avatar
Chao Liu committed
134
135
              typename enable_if<sizeof...(Ys) == sizeof...(Xs) && sizeof...(Ys) >= 2, bool>::type =
                  false>
Chao Liu's avatar
Chao Liu committed
136
137
138
139
140
141
    __host__ __device__ constexpr Tuple(Ys&&... ys) : base(std::forward<Ys>(ys)...)
    {
    }

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

142
    // read access
Chao Liu's avatar
Chao Liu committed
143
144
145
146
    template <index_t I>
    __host__ __device__ constexpr const auto& At(Number<I>) const
    {
        static_assert(I < base::Size(), "wrong! out of range");
147
        return base::GetElementDataByKey(detail::TupleElementKey<I>{});
Chao Liu's avatar
Chao Liu committed
148
149
    }

150
    // write access
Chao Liu's avatar
Chao Liu committed
151
152
153
154
    template <index_t I>
    __host__ __device__ constexpr auto& At(Number<I>)
    {
        static_assert(I < base::Size(), "wrong! out of range");
155
        return base::GetElementDataByKey(detail::TupleElementKey<I>{});
Chao Liu's avatar
Chao Liu committed
156
    }
Chao Liu's avatar
Chao Liu committed
157

158
    // read access
Chao Liu's avatar
Chao Liu committed
159
160
161
162
163
    template <index_t I>
    __host__ __device__ constexpr const auto& operator[](Number<I> i) const
    {
        return At(i);
    }
164

165
    // write access
Chao Liu's avatar
Chao Liu committed
166
167
168
169
170
    template <index_t I>
    __host__ __device__ constexpr auto& operator()(Number<I> i)
    {
        return At(i);
    }
Chao Liu's avatar
Chao Liu committed
171

Chao Liu's avatar
Chao Liu committed
172
173
174
175
    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
176

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

Chao Liu's avatar
Chao Liu committed
179
180
        return *this;
    }
zjing14's avatar
zjing14 committed
181
182

    __host__ __device__ static constexpr bool IsStaticBuffer() { return true; }
Chao Liu's avatar
Chao Liu committed
183
};
184

185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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
{
204
205
    // type should keep the cv/ref qualifier of original tuple element
    using type = decltype(detail::get_tuple_element_data<detail::TupleElementKey<I>>(TTuple{}));
206
207
208
209
210
};

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

Chao Liu's avatar
Chao Liu committed
211
212
template <typename... Xs>
__host__ __device__ constexpr auto make_tuple(Xs&&... xs)
Chao Liu's avatar
Chao Liu committed
213
{
Chao Liu's avatar
Chao Liu committed
214
    return Tuple<remove_cvref_t<Xs>...>(std::forward<Xs>(xs)...);
Chao Liu's avatar
Chao Liu committed
215
216
}

217
218
219
220
221
222
223
// 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
224
} // namespace ck
Umang Yadav's avatar
Umang Yadav committed
225
226

#pragma clang diagnostic pop