static_buffer.hpp 6.43 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
5
6
7
8
9
10
#ifndef CK_STATIC_BUFFER_HPP
#define CK_STATIC_BUFFER_HPP

#include "statically_indexed_array.hpp"

namespace ck {

11
// static buffer for scalar
12
template <AddressSpaceEnum AddressSpace,
13
14
          typename T,
          index_t N,
15
          bool InvalidElementUseNumericalZeroValue> // TODO remove this bool, no longer needed
16
17
18
19
20
21
22
struct StaticBuffer : public StaticallyIndexedArray<T, N>
{
    using type = T;
    using base = StaticallyIndexedArray<T, N>;

    __host__ __device__ constexpr StaticBuffer() : base{} {}

Anthony Chang's avatar
Anthony Chang committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
    __host__ __device__ constexpr StaticBuffer& operator=(StaticBuffer& y)
    {
        StaticBuffer& x = *this;
        static_for<0, base::Size(), 1>{}([&](auto i) { x(i) = y[i]; });
        return x;
    }

    template <typename... Ys>
    __host__ __device__ constexpr StaticBuffer& operator=(const Tuple<Ys...>& y)
    {
        static_assert(base::Size() == sizeof...(Ys), "wrong! size not the same");
        StaticBuffer& x = *this;
        static_for<0, base::Size(), 1>{}([&](auto i) { x(i) = y[i]; });
        return x;
    }

    __host__ __device__ constexpr StaticBuffer& operator=(const T& y)
    {
        StaticBuffer& x = *this;
        static_for<0, base::Size(), 1>{}([&](auto i) { x(i) = y; });
        return x;
    }

46
    __host__ __device__ static constexpr AddressSpaceEnum GetAddressSpace() { return AddressSpace; }
47

48
49
50
51
52
    __host__ __device__ static constexpr bool IsStaticBuffer() { return true; }

    __host__ __device__ static constexpr bool IsDynamicBuffer() { return false; }

    // read access
53
    template <index_t I>
54
    __host__ __device__ constexpr const T& operator[](Number<I> i) const
55
    {
56
        return base::operator[](i);
57
58
    }

59
    // write access
60
    template <index_t I>
61
    __host__ __device__ constexpr T& operator()(Number<I> i)
62
    {
63
        return base::operator()(i);
64
    }
Jianfeng Yan's avatar
Jianfeng Yan committed
65

Anthony Chang's avatar
Anthony Chang committed
66
    __host__ __device__ void Set(T x)
Jianfeng Yan's avatar
Jianfeng Yan committed
67
    {
Anthony Chang's avatar
Anthony Chang committed
68
        static_for<0, N, 1>{}([&](auto i) { operator()(i) = T{x}; });
Jianfeng Yan's avatar
Jianfeng Yan committed
69
    }
Anthony Chang's avatar
Anthony Chang committed
70
71

    __host__ __device__ void Clear() { Set(T{0}); }
72
73
};

74
// static buffer for vector
75
template <AddressSpaceEnum AddressSpace,
76
77
78
79
80
81
82
          typename S,
          index_t NumOfVector,
          index_t ScalarPerVector,
          bool InvalidElementUseNumericalZeroValue, // TODO remove this bool, no longer needed,
          typename enable_if<is_scalar_type<S>::value, bool>::type = false>
struct StaticBufferTupleOfVector
    : public StaticallyIndexedArray<vector_type<S, ScalarPerVector>, NumOfVector>
83
{
84
85
86
87
88
    using V    = typename vector_type<S, ScalarPerVector>::type;
    using base = StaticallyIndexedArray<vector_type<S, ScalarPerVector>, NumOfVector>;

    static constexpr auto s_per_v   = Number<ScalarPerVector>{};
    static constexpr auto num_of_v_ = Number<NumOfVector>{};
Anthony Chang's avatar
Anthony Chang committed
89
    static constexpr auto s_per_buf = s_per_v * num_of_v_;
90

91
    __host__ __device__ constexpr StaticBufferTupleOfVector() : base{} {}
92

93
    __host__ __device__ static constexpr AddressSpaceEnum GetAddressSpace() { return AddressSpace; }
94

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

97
    __host__ __device__ static constexpr bool IsDynamicBuffer() { return false; }
98

Anthony Chang's avatar
Anthony Chang committed
99
100
    __host__ __device__ static constexpr index_t Size() { return s_per_buf; };

101
102
103
104
    // Get S
    // i is offset of S
    template <index_t I>
    __host__ __device__ constexpr const S& operator[](Number<I> i) const
105
    {
106
107
        constexpr auto i_v = i / s_per_v;
        constexpr auto i_s = i % s_per_v;
108

109
        return base::operator[](i_v).template AsType<S>()[i_s];
110
111
    }

112
113
    // Set S
    // i is offset of S
114
    template <index_t I>
115
    __host__ __device__ constexpr S& operator()(Number<I> i)
116
    {
117
118
        constexpr auto i_v = i / s_per_v;
        constexpr auto i_s = i % s_per_v;
119

120
        return base::operator()(i_v).template AsType<S>()(i_s);
121
122
    }

123
124
125
126
127
128
    // Get X
    // i is offset of S, not X. i should be aligned to X
    template <typename X,
              index_t I,
              typename enable_if<has_same_scalar_type<S, X>::value, bool>::type = false>
    __host__ __device__ constexpr auto GetAsType(Number<I> i) const
129
    {
130
131
132
133
134
135
136
        constexpr auto s_per_x = Number<scalar_type<remove_cvref_t<X>>::vector_size>{};

        static_assert(s_per_v % s_per_x == 0, "wrong! V must  one or multiple X");
        static_assert(i % s_per_x == 0, "wrong!");

        constexpr auto i_v = i / s_per_v;
        constexpr auto i_x = (i % s_per_v) / s_per_x;
137

138
        return base::operator[](i_v).template AsType<X>()[i_x];
139
140
    }

141
142
143
144
145
146
    // Set X
    // i is offset of S, not X. i should be aligned to X
    template <typename X,
              index_t I,
              typename enable_if<has_same_scalar_type<S, X>::value, bool>::type = false>
    __host__ __device__ constexpr void SetAsType(Number<I> i, X x)
147
    {
148
149
150
151
152
153
154
155
156
        constexpr auto s_per_x = Number<scalar_type<remove_cvref_t<X>>::vector_size>{};

        static_assert(s_per_v % s_per_x == 0, "wrong! V must contain one or multiple X");
        static_assert(i % s_per_x == 0, "wrong!");

        constexpr auto i_v = i / s_per_v;
        constexpr auto i_x = (i % s_per_v) / s_per_x;

        base::operator()(i_v).template AsType<X>()(i_x) = x;
157
158
    }

159
160
    // Get read access to vector_type V
    // i is offset of S, not V. i should be aligned to V
161
    template <index_t I>
162
    __host__ __device__ constexpr const auto& GetVectorTypeReference(Number<I> i) const
163
    {
164
165
166
167
168
        static_assert(i % s_per_v == 0, "wrong!");

        constexpr auto i_v = i / s_per_v;

        return base::operator[](i_v);
169
170
    }

171
172
    // Get write access to vector_type V
    // i is offset of S, not V. i should be aligned to V
173
    template <index_t I>
174
    __host__ __device__ constexpr auto& GetVectorTypeReference(Number<I> i)
175
    {
176
        static_assert(i % s_per_v == 0, "wrong!");
177

178
        constexpr auto i_v = i / s_per_v;
179

180
181
        return base::operator()(i_v);
    }
zjing14's avatar
zjing14 committed
182
183
184

    __host__ __device__ void Clear()
    {
Jianfeng Yan's avatar
Jianfeng Yan committed
185
        constexpr index_t NumScalars = NumOfVector * ScalarPerVector;
zjing14's avatar
zjing14 committed
186

Jianfeng Yan's avatar
Jianfeng Yan committed
187
        static_for<0, NumScalars, 1>{}([&](auto i) { SetAsType(i, S{0}); });
zjing14's avatar
zjing14 committed
188
    }
189
190
};

191
template <AddressSpaceEnum AddressSpace, typename T, index_t N>
192
193
__host__ __device__ constexpr auto make_static_buffer(Number<N>)
{
194
    return StaticBuffer<AddressSpace, T, N, true>{};
195
196
}

197
198
199
200
201
202
template <AddressSpaceEnum AddressSpace, typename T, long_index_t N>
__host__ __device__ constexpr auto make_static_buffer(LongNumber<N>)
{
    return StaticBuffer<AddressSpace, T, N, true>{};
}

203
204
} // namespace ck
#endif