static_buffer.hpp 5.15 KB
Newer Older
1
2
3
4
5
6
7
#ifndef CK_STATIC_BUFFER_HPP
#define CK_STATIC_BUFFER_HPP

#include "statically_indexed_array.hpp"

namespace ck {

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

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

20
    __host__ __device__ static constexpr AddressSpaceEnum_t GetAddressSpace()
21
    {
22
        return AddressSpace;
23
24
    }

25
26
27
28
29
    __host__ __device__ static constexpr bool IsStaticBuffer() { return true; }

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

    // read access
30
    template <index_t I>
31
    __host__ __device__ constexpr const T& operator[](Number<I> i) const
32
    {
33
        return base::operator[](i);
34
35
    }

36
    // write access
37
    template <index_t I>
38
    __host__ __device__ constexpr T& operator()(Number<I> i)
39
    {
40
        return base::operator()(i);
41
    }
42
43
};

44
45
46
47
48
49
50
51
52
// static buffer for vector
template <AddressSpaceEnum_t AddressSpace,
          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>
53
{
54
55
56
57
58
    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>{};
59

60
    __host__ __device__ constexpr StaticBufferTupleOfVector() : base{} {}
61

62
    __host__ __device__ static constexpr AddressSpaceEnum_t GetAddressSpace()
63
    {
64
        return AddressSpace;
65
66
    }

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

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

71
72
73
74
    // Get S
    // i is offset of S
    template <index_t I>
    __host__ __device__ constexpr const S& operator[](Number<I> i) const
75
    {
76
77
        constexpr auto i_v = i / s_per_v;
        constexpr auto i_s = i % s_per_v;
78

79
        return base::operator[](i_v).template AsType<S>()[i_s];
80
81
    }

82
83
    // Set S
    // i is offset of S
84
    template <index_t I>
85
    __host__ __device__ constexpr S& operator()(Number<I> i)
86
    {
87
88
        constexpr auto i_v = i / s_per_v;
        constexpr auto i_s = i % s_per_v;
89

90
        return base::operator()(i_v).template AsType<S>()(i_s);
91
92
    }

93
94
95
96
97
98
    // 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
99
    {
100
101
102
103
104
105
106
        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;
107

108
        return base::operator[](i_v).template AsType<X>()[i_x];
109
110
    }

111
112
113
114
115
116
    // 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)
117
    {
118
119
120
121
122
123
124
125
126
        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;
127
128
    }

129
130
    // Get read access to vector_type V
    // i is offset of S, not V. i should be aligned to V
131
    template <index_t I>
132
    __host__ __device__ constexpr const auto& GetVectorTypeReference(Number<I> i) const
133
    {
134
135
136
137
138
        static_assert(i % s_per_v == 0, "wrong!");

        constexpr auto i_v = i / s_per_v;

        return base::operator[](i_v);
139
140
    }

141
142
    // Get write access to vector_type V
    // i is offset of S, not V. i should be aligned to V
143
    template <index_t I>
144
    __host__ __device__ constexpr auto& GetVectorTypeReference(Number<I> i)
145
    {
146
        static_assert(i % s_per_v == 0, "wrong!");
147

148
        constexpr auto i_v = i / s_per_v;
149

150
151
        return base::operator()(i_v);
    }
zjing14's avatar
zjing14 committed
152
153
154
155
156
157
158

    __host__ __device__ void Clear()
    {
        const index_t numScalars = NumOfVector * ScalarPerVector;

        static_for<0, Number<numScalars>{}, 1>{}([&](auto i) { SetAsType(i, S{0}); });
    }
159
160
};

161
template <AddressSpaceEnum_t AddressSpace, typename T, index_t N>
162
163
__host__ __device__ constexpr auto make_static_buffer(Number<N>)
{
164
    return StaticBuffer<AddressSpace, T, N, true>{};
165
166
167
168
}

} // namespace ck
#endif