static_buffer.hpp 6.28 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

Anthony Chang's avatar
Anthony Chang committed
7
#pragma once
8
9
10
11
12

#include "statically_indexed_array.hpp"

namespace ck {

13
// static buffer for scalar
14
template <AddressSpaceEnum AddressSpace,
15
16
          typename T,
          index_t N,
17
          bool InvalidElementUseNumericalZeroValue> // TODO remove this bool, no longer needed
18
19
20
21
22
23
24
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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    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;
    }

41
    __host__ __device__ static constexpr AddressSpaceEnum GetAddressSpace() { return AddressSpace; }
42

43
44
45
46
47
    __host__ __device__ static constexpr bool IsStaticBuffer() { return true; }

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

    // read access
48
    template <index_t I>
49
    __host__ __device__ constexpr const T& operator[](Number<I> i) const
50
    {
51
        return base::operator[](i);
52
53
    }

54
    // write access
55
    template <index_t I>
56
    __host__ __device__ constexpr T& operator()(Number<I> i)
57
    {
58
        return base::operator()(i);
59
    }
Jianfeng Yan's avatar
Jianfeng Yan committed
60

Anthony Chang's avatar
Anthony Chang committed
61
    __host__ __device__ void Set(T x)
Jianfeng Yan's avatar
Jianfeng Yan committed
62
    {
Anthony Chang's avatar
Anthony Chang committed
63
        static_for<0, N, 1>{}([&](auto i) { operator()(i) = T{x}; });
Jianfeng Yan's avatar
Jianfeng Yan committed
64
    }
Anthony Chang's avatar
Anthony Chang committed
65
66

    __host__ __device__ void Clear() { Set(T{0}); }
67
68
};

69
// static buffer for vector
70
template <AddressSpaceEnum AddressSpace,
71
72
73
74
75
76
77
          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>
78
{
79
80
81
82
83
    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
84
    static constexpr auto s_per_buf = s_per_v * num_of_v_;
85

86
    __host__ __device__ constexpr StaticBufferTupleOfVector() : base{} {}
87

88
    __host__ __device__ static constexpr AddressSpaceEnum GetAddressSpace() { return AddressSpace; }
89

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

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

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

96
97
98
99
    // Get S
    // i is offset of S
    template <index_t I>
    __host__ __device__ constexpr const S& operator[](Number<I> i) const
100
    {
101
102
        constexpr auto i_v = i / s_per_v;
        constexpr auto i_s = i % s_per_v;
103

104
        return base::operator[](i_v).template AsType<S>()[i_s];
105
106
    }

107
108
    // Set S
    // i is offset of S
109
    template <index_t I>
110
    __host__ __device__ constexpr S& operator()(Number<I> i)
111
    {
112
113
        constexpr auto i_v = i / s_per_v;
        constexpr auto i_s = i % s_per_v;
114

115
        return base::operator()(i_v).template AsType<S>()(i_s);
116
117
    }

118
119
120
121
122
123
    // 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
124
    {
125
126
127
128
129
130
131
        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;
132

133
        return base::operator[](i_v).template AsType<X>()[i_x];
134
135
    }

136
137
138
139
140
141
    // 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)
142
    {
143
144
145
146
147
148
149
150
151
        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;
152
153
    }

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

        constexpr auto i_v = i / s_per_v;

        return base::operator[](i_v);
164
165
    }

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

173
        constexpr auto i_v = i / s_per_v;
174

175
176
        return base::operator()(i_v);
    }
zjing14's avatar
zjing14 committed
177
178
179

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

Jianfeng Yan's avatar
Jianfeng Yan committed
182
        static_for<0, NumScalars, 1>{}([&](auto i) { SetAsType(i, S{0}); });
zjing14's avatar
zjing14 committed
183
    }
184
185
};

186
template <AddressSpaceEnum AddressSpace, typename T, index_t N>
187
188
__host__ __device__ constexpr auto make_static_buffer(Number<N>)
{
189
    return StaticBuffer<AddressSpace, T, N, true>{};
190
191
}

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

198
} // namespace ck
Umang Yadav's avatar
Umang Yadav committed
199
200

#pragma clang diagnostic pop