static_buffer.hpp 5.41 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
// static buffer for scalar
9
template <AddressSpaceEnum 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 GetAddressSpace() { return AddressSpace; }
21

22
23
24
25
26
    __host__ __device__ static constexpr bool IsStaticBuffer() { return true; }

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

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

33
    // write access
34
    template <index_t I>
35
    __host__ __device__ constexpr T& operator()(Number<I> i)
36
    {
37
        return base::operator()(i);
38
    }
Jianfeng Yan's avatar
Jianfeng Yan committed
39
40
41
42
43

    __host__ __device__ void Clear()
    {
        static_for<0, N, 1>{}([&](auto i) { operator()(i) = T{0}; });
    }
44
45
};

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

62
    __host__ __device__ constexpr StaticBufferTupleOfVector() : base{} {}
63

64
    __host__ __device__ static constexpr AddressSpaceEnum GetAddressSpace() { return AddressSpace; }
65

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

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

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

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

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

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

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

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

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

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

        constexpr auto i_v = i / s_per_v;

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

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

147
        constexpr auto i_v = i / s_per_v;
148

149
150
        return base::operator()(i_v);
    }
zjing14's avatar
zjing14 committed
151
152
153

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

Jianfeng Yan's avatar
Jianfeng Yan committed
156
        static_for<0, NumScalars, 1>{}([&](auto i) { SetAsType(i, S{0}); });
zjing14's avatar
zjing14 committed
157
    }
158
159
};

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

166
167
168
169
170
171
template <AddressSpaceEnum AddressSpace, typename T, long_index_t N>
__host__ __device__ constexpr auto make_static_buffer(LongNumber<N>)
{
    return StaticBuffer<AddressSpace, T, N, true>{};
}

172
173
} // namespace ck
#endif