dynamic_buffer.hpp 16.7 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
#pragma once
Chao Liu's avatar
Chao Liu committed
5
6

#include "ck/ck.hpp"
7
#include "ck/utility/data_type.hpp"
8
#include "enable_if.hpp"
9
10
#include "c_style_pointer_cast.hpp"
#include "amd_buffer_addressing.hpp"
rocking5566's avatar
rocking5566 committed
11
#include "generic_memory_space_atomic.hpp"
12
13
14

namespace ck {

15
16
17
18
// T may be scalar or vector
// X may be scalar or vector
// T and X have same scalar type
// X contains multiple T
19
template <AddressSpaceEnum BufferAddressSpace,
20
21
22
23
24
25
26
          typename T,
          typename ElementSpaceSize,
          bool InvalidElementUseNumericalZeroValue>
struct DynamicBuffer
{
    using type = T;

Chao Liu's avatar
Chao Liu committed
27
    T* p_data_ = nullptr;
28
    ElementSpaceSize element_space_size_;
Chao Liu's avatar
Chao Liu committed
29
    remove_cvref_t<T> invalid_element_value_ = T{0};
30

Chao Liu's avatar
Chao Liu committed
31
32
33
34
35
    __host__ __device__ constexpr DynamicBuffer()
        : p_data_{}, element_space_size_{}, invalid_element_value_{}
    {
    }

36
    __host__ __device__ constexpr DynamicBuffer(T* p_data, ElementSpaceSize element_space_size)
Chao Liu's avatar
Chao Liu committed
37
        : p_data_{p_data}, element_space_size_{element_space_size}, invalid_element_value_{0}
38
39
40
41
42
43
44
45
46
47
48
49
    {
    }

    __host__ __device__ constexpr DynamicBuffer(T* p_data,
                                                ElementSpaceSize element_space_size,
                                                T invalid_element_value)
        : p_data_{p_data},
          element_space_size_{element_space_size},
          invalid_element_value_{invalid_element_value}
    {
    }

Chao Liu's avatar
Chao Liu committed
50
    __device__ static constexpr AddressSpaceEnum GetAddressSpace() { return BufferAddressSpace; }
51

Chao Liu's avatar
Chao Liu committed
52
    __device__ constexpr const T& operator[](index_t i) const { return p_data_[i]; }
53

Chao Liu's avatar
Chao Liu committed
54
    __device__ constexpr T& operator()(index_t i) { return p_data_[i]; }
55

56
    template <typename X,
Chao Liu's avatar
Chao Liu committed
57
58
59
              typename enable_if<is_same<typename scalar_type<remove_cvref_t<X>>::type,
                                         typename scalar_type<remove_cvref_t<T>>::type>::value,
                                 bool>::type = false>
Chao Liu's avatar
Chao Liu committed
60
    __device__ constexpr auto Get(index_t i, bool is_valid_element) const
61
62
    {
        // X contains multiple T
Chao Liu's avatar
Chao Liu committed
63
        constexpr index_t scalar_per_t_vector = scalar_type<remove_cvref_t<T>>::vector_size;
64

Chao Liu's avatar
Chao Liu committed
65
        constexpr index_t scalar_per_x_vector = scalar_type<remove_cvref_t<X>>::vector_size;
66
67

        static_assert(scalar_per_x_vector % scalar_per_t_vector == 0,
68
                      "wrong! X should contain multiple T");
69

Jianfeng Yan's avatar
Jianfeng Yan committed
70
#if CK_USE_AMD_BUFFER_LOAD
71
72
73
74
75
        bool constexpr use_amd_buffer_addressing = true;
#else
        bool constexpr use_amd_buffer_addressing = false;
#endif

76
        if constexpr(GetAddressSpace() == AddressSpaceEnum::Global && use_amd_buffer_addressing)
77
78
79
80
81
        {
            constexpr index_t t_per_x = scalar_per_x_vector / scalar_per_t_vector;

            if constexpr(InvalidElementUseNumericalZeroValue)
            {
Jianfeng Yan's avatar
Jianfeng Yan committed
82
                return amd_buffer_load_invalid_element_return_zero<remove_cvref_t<T>, t_per_x>(
Chao Liu's avatar
Chao Liu committed
83
                    p_data_, i, is_valid_element, element_space_size_);
84
85
86
            }
            else
            {
Chao Liu's avatar
Chao Liu committed
87
88
                return amd_buffer_load_invalid_element_return_customized_value<remove_cvref_t<T>,
                                                                               t_per_x>(
89
90
91
92
93
                    p_data_, i, is_valid_element, element_space_size_, invalid_element_value_);
            }
        }
        else
        {
94
            if(is_valid_element)
95
            {
96
97
98
99
100
#if CK_EXPERIMENTAL_USE_MEMCPY_FOR_VECTOR_ACCESS
                X tmp;

                __builtin_memcpy(&tmp, &(p_data_[i]), sizeof(X));

101
                return tmp;
102
#else
103
                return *c_style_pointer_cast<const X*>(&p_data_[i]);
104
#endif
105
106
107
            }
            else
            {
108
109
110
111
112
113
114
115
                if constexpr(InvalidElementUseNumericalZeroValue)
                {
                    return X{0};
                }
                else
                {
                    return X{invalid_element_value_};
                }
116
117
118
119
            }
        }
    }

120
    template <InMemoryDataOperationEnum Op,
121
122
123
124
              typename X,
              typename enable_if<is_same<typename scalar_type<remove_cvref_t<X>>::type,
                                         typename scalar_type<remove_cvref_t<T>>::type>::value,
                                 bool>::type = false>
Chao Liu's avatar
Chao Liu committed
125
    __device__ void Update(index_t i, bool is_valid_element, const X& x)
126
    {
127
        if constexpr(Op == InMemoryDataOperationEnum::Set)
128
129
130
        {
            this->template Set<X>(i, is_valid_element, x);
        }
131
        else if constexpr(Op == InMemoryDataOperationEnum::AtomicAdd)
132
133
134
        {
            this->template AtomicAdd<X>(i, is_valid_element, x);
        }
rocking5566's avatar
rocking5566 committed
135
136
137
138
        else if constexpr(Op == InMemoryDataOperationEnum::AtomicMax)
        {
            this->template AtomicMax<X>(i, is_valid_element, x);
        }
139
        else if constexpr(Op == InMemoryDataOperationEnum::Add)
140
141
142
143
144
145
146
147
        {
            auto tmp = this->template Get<X>(i, is_valid_element);
            this->template Set<X>(i, is_valid_element, x + tmp);
            // tmp += x;
            // this->template Set<X>(i, is_valid_element, tmp);
        }
    }

148
    template <typename X,
Chao Liu's avatar
Chao Liu committed
149
150
151
              typename enable_if<is_same<typename scalar_type<remove_cvref_t<X>>::type,
                                         typename scalar_type<remove_cvref_t<T>>::type>::value,
                                 bool>::type = false>
Chao Liu's avatar
Chao Liu committed
152
    __device__ void Set(index_t i, bool is_valid_element, const X& x)
153
154
    {
        // X contains multiple T
Chao Liu's avatar
Chao Liu committed
155
        constexpr index_t scalar_per_t_vector = scalar_type<remove_cvref_t<T>>::vector_size;
156

Chao Liu's avatar
Chao Liu committed
157
        constexpr index_t scalar_per_x_vector = scalar_type<remove_cvref_t<X>>::vector_size;
158
159

        static_assert(scalar_per_x_vector % scalar_per_t_vector == 0,
160
                      "wrong! X should contain multiple T");
161

Jianfeng Yan's avatar
Jianfeng Yan committed
162
#if CK_USE_AMD_BUFFER_STORE
163
        bool constexpr use_amd_buffer_addressing = true;
164
#else
165
166
        bool constexpr use_amd_buffer_addressing      = false;
#endif
167

168
169
#if CK_WORKAROUND_SWDEV_XXXXXX_INT8_DS_WRITE_ISSUE
        bool constexpr workaround_int8_ds_write_issue = true;
170
#else
171
        bool constexpr workaround_int8_ds_write_issue = false;
172
#endif
173
174
175
176
177
178
179

        if constexpr(GetAddressSpace() == AddressSpaceEnum::Global && use_amd_buffer_addressing)
        {
            constexpr index_t t_per_x = scalar_per_x_vector / scalar_per_t_vector;

            amd_buffer_store<remove_cvref_t<T>, t_per_x>(
                x, p_data_, i, is_valid_element, element_space_size_);
180
        }
181
182
183
        else if constexpr(GetAddressSpace() == AddressSpaceEnum::Lds &&
                          is_same<typename scalar_type<remove_cvref_t<T>>::type, int8_t>::value &&
                          workaround_int8_ds_write_issue)
184
185
186
        {
            if(is_valid_element)
            {
187
                // HACK: compiler would lower IR "store<i8, 16> address_space(3)" into inefficient
188
189
190
                // ISA, so I try to let compiler emit IR "store<i32, 4>" which would be lower to
                // ds_write_b128
                // TODO: remove this after compiler fix
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
                static_assert((is_same<remove_cvref_t<T>, int8_t>::value &&
                               is_same<remove_cvref_t<X>, int8_t>::value) ||
                                  (is_same<remove_cvref_t<T>, int8_t>::value &&
                                   is_same<remove_cvref_t<X>, int8x2_t>::value) ||
                                  (is_same<remove_cvref_t<T>, int8_t>::value &&
                                   is_same<remove_cvref_t<X>, int8x4_t>::value) ||
                                  (is_same<remove_cvref_t<T>, int8_t>::value &&
                                   is_same<remove_cvref_t<X>, int8x8_t>::value) ||
                                  (is_same<remove_cvref_t<T>, int8_t>::value &&
                                   is_same<remove_cvref_t<X>, int8x16_t>::value) ||
                                  (is_same<remove_cvref_t<T>, int8x4_t>::value &&
                                   is_same<remove_cvref_t<X>, int8x4_t>::value) ||
                                  (is_same<remove_cvref_t<T>, int8x8_t>::value &&
                                   is_same<remove_cvref_t<X>, int8x8_t>::value) ||
                                  (is_same<remove_cvref_t<T>, int8x16_t>::value &&
                                   is_same<remove_cvref_t<X>, int8x16_t>::value),
                              "wrong! not implemented for this combination, please add "
                              "implementation");

                if constexpr(is_same<remove_cvref_t<T>, int8_t>::value &&
                             is_same<remove_cvref_t<X>, int8_t>::value)
212
                {
213
214
215
216
                    // HACK: cast pointer of x is bad
                    // TODO: remove this after compiler fix
                    *c_style_pointer_cast<int8_t*>(&p_data_[i]) =
                        *c_style_pointer_cast<const int8_t*>(&x);
217
                }
218
219
                else if constexpr(is_same<remove_cvref_t<T>, int8_t>::value &&
                                  is_same<remove_cvref_t<X>, int8x2_t>::value)
220
                {
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
                    // HACK: cast pointer of x is bad
                    // TODO: remove this after compiler fix
                    *c_style_pointer_cast<int16_t*>(&p_data_[i]) =
                        *c_style_pointer_cast<const int16_t*>(&x);
                }
                else if constexpr(is_same<remove_cvref_t<T>, int8_t>::value &&
                                  is_same<remove_cvref_t<X>, int8x4_t>::value)
                {
                    // HACK: cast pointer of x is bad
                    // TODO: remove this after compiler fix
                    *c_style_pointer_cast<int32_t*>(&p_data_[i]) =
                        *c_style_pointer_cast<const int32_t*>(&x);
                }
                else if constexpr(is_same<remove_cvref_t<T>, int8_t>::value &&
                                  is_same<remove_cvref_t<X>, int8x8_t>::value)
                {
                    // HACK: cast pointer of x is bad
                    // TODO: remove this after compiler fix
                    *c_style_pointer_cast<int32x2_t*>(&p_data_[i]) =
                        *c_style_pointer_cast<const int32x2_t*>(&x);
                }
                else if constexpr(is_same<remove_cvref_t<T>, int8_t>::value &&
                                  is_same<remove_cvref_t<X>, int8x16_t>::value)
                {
                    // HACK: cast pointer of x is bad
                    // TODO: remove this after compiler fix
                    *c_style_pointer_cast<int32x4_t*>(&p_data_[i]) =
                        *c_style_pointer_cast<const int32x4_t*>(&x);
                }
                else if constexpr(is_same<remove_cvref_t<T>, int8x4_t>::value &&
                                  is_same<remove_cvref_t<X>, int8x4_t>::value)
                {
                    // HACK: cast pointer of x is bad
                    // TODO: remove this after compiler fix
                    *c_style_pointer_cast<int32_t*>(&p_data_[i]) =
                        *c_style_pointer_cast<const int32_t*>(&x);
                }
                else if constexpr(is_same<remove_cvref_t<T>, int8x8_t>::value &&
                                  is_same<remove_cvref_t<X>, int8x8_t>::value)
                {
                    // HACK: cast pointer of x is bad
                    // TODO: remove this after compiler fix
                    *c_style_pointer_cast<int32x2_t*>(&p_data_[i]) =
                        *c_style_pointer_cast<const int32x2_t*>(&x);
                }
                else if constexpr(is_same<remove_cvref_t<T>, int8x16_t>::value &&
                                  is_same<remove_cvref_t<X>, int8x16_t>::value)
                {
                    // HACK: cast pointer of x is bad
                    // TODO: remove this after compiler fix
                    *c_style_pointer_cast<int32x4_t*>(&p_data_[i]) =
                        *c_style_pointer_cast<const int32x4_t*>(&x);
273
274
275
276
277
278
279
                }
            }
        }
        else
        {
            if(is_valid_element)
            {
280
281
282
283
284
#if CK_EXPERIMENTAL_USE_MEMCPY_FOR_VECTOR_ACCESS
                X tmp = x;

                __builtin_memcpy(&(p_data_[i]), &tmp, sizeof(X));
#else
285
                *c_style_pointer_cast<X*>(&p_data_[i]) = x;
286
#endif
287
288
289
290
            }
        }
    }

zjing14's avatar
zjing14 committed
291
    template <typename X,
Chao Liu's avatar
Chao Liu committed
292
293
294
              typename enable_if<is_same<typename scalar_type<remove_cvref_t<X>>::type,
                                         typename scalar_type<remove_cvref_t<T>>::type>::value,
                                 bool>::type = false>
Chao Liu's avatar
Chao Liu committed
295
    __device__ void AtomicAdd(index_t i, bool is_valid_element, const X& x)
zjing14's avatar
zjing14 committed
296
    {
297
298
        using scalar_t = typename scalar_type<remove_cvref_t<T>>::type;

zjing14's avatar
zjing14 committed
299
        // X contains multiple T
Chao Liu's avatar
Chao Liu committed
300
        constexpr index_t scalar_per_t_vector = scalar_type<remove_cvref_t<T>>::vector_size;
zjing14's avatar
zjing14 committed
301

Chao Liu's avatar
Chao Liu committed
302
        constexpr index_t scalar_per_x_vector = scalar_type<remove_cvref_t<X>>::vector_size;
zjing14's avatar
zjing14 committed
303
304

        static_assert(scalar_per_x_vector % scalar_per_t_vector == 0,
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
                      "wrong! X should contain multiple T");

        static_assert(GetAddressSpace() == AddressSpaceEnum::Global, "only support global mem");

#if CK_USE_AMD_BUFFER_ATOMIC_ADD_INTEGER && CK_USE_AMD_BUFFER_ATOMIC_ADD_FLOAT
        bool constexpr use_amd_buffer_addressing =
            is_same_v<remove_cvref_t<scalar_t>, int32_t> ||
            is_same_v<remove_cvref_t<scalar_t>, float> ||
            (is_same_v<remove_cvref_t<scalar_t>, half_t> && scalar_per_x_vector % 2 == 0);
#elif CK_USE_AMD_BUFFER_ATOMIC_ADD_INTEGER && (!CK_USE_AMD_BUFFER_ATOMIC_ADD_FLOAT)
        bool constexpr use_amd_buffer_addressing = is_same_v<remove_cvref_t<scalar_t>, int32_t>;
#elif(!CK_USE_AMD_BUFFER_ATOMIC_ADD_INTEGER) && CK_USE_AMD_BUFFER_ATOMIC_ADD_FLOAT
        bool constexpr use_amd_buffer_addressing =
            is_same_v<remove_cvref_t<scalar_t>, float> ||
            (is_same_v<remove_cvref_t<scalar_t>, half_t> && scalar_per_x_vector % 2 == 0);
#else
        bool constexpr use_amd_buffer_addressing = false;
#endif
zjing14's avatar
zjing14 committed
323

324
325
326
        if constexpr(use_amd_buffer_addressing)
        {
            constexpr index_t t_per_x = scalar_per_x_vector / scalar_per_t_vector;
zjing14's avatar
zjing14 committed
327

328
329
330
331
            amd_buffer_atomic_add<remove_cvref_t<T>, t_per_x>(
                x, p_data_, i, is_valid_element, element_space_size_);
        }
        else
zjing14's avatar
zjing14 committed
332
        {
333
334
            if(is_valid_element)
            {
Chao Liu's avatar
Chao Liu committed
335
                atomic_add<X>(c_style_pointer_cast<X*>(&p_data_[i]), x);
336
            }
zjing14's avatar
zjing14 committed
337
338
339
        }
    }

rocking5566's avatar
rocking5566 committed
340
341
342
343
    template <typename X,
              typename enable_if<is_same<typename scalar_type<remove_cvref_t<X>>::type,
                                         typename scalar_type<remove_cvref_t<T>>::type>::value,
                                 bool>::type = false>
Chao Liu's avatar
Chao Liu committed
344
    __device__ void AtomicMax(index_t i, bool is_valid_element, const X& x)
rocking5566's avatar
rocking5566 committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
    {
        // X contains multiple T
        constexpr index_t scalar_per_t_vector = scalar_type<remove_cvref_t<T>>::vector_size;

        constexpr index_t scalar_per_x_vector = scalar_type<remove_cvref_t<X>>::vector_size;

        static_assert(scalar_per_x_vector % scalar_per_t_vector == 0,
                      "wrong! X should contain multiple T");

        static_assert(GetAddressSpace() == AddressSpaceEnum::Global, "only support global mem");

#if CK_USE_AMD_BUFFER_ATOMIC_MAX_FLOAT64
        using scalar_t                           = typename scalar_type<remove_cvref_t<T>>::type;
        bool constexpr use_amd_buffer_addressing = is_same_v<remove_cvref_t<scalar_t>, double>;
#else
        bool constexpr use_amd_buffer_addressing = false;
#endif

        if constexpr(use_amd_buffer_addressing)
        {
            constexpr index_t t_per_x = scalar_per_x_vector / scalar_per_t_vector;

            amd_buffer_atomic_max<remove_cvref_t<T>, t_per_x>(
                x, p_data_, i, is_valid_element, element_space_size_);
        }
        else if(is_valid_element)
        {
            atomic_max<X>(c_style_pointer_cast<X*>(&p_data_[i]), x);
        }
    }

Chao Liu's avatar
Chao Liu committed
376
    __device__ static constexpr bool IsStaticBuffer() { return false; }
377

Chao Liu's avatar
Chao Liu committed
378
    __device__ static constexpr bool IsDynamicBuffer() { return true; }
379
380
};

381
template <AddressSpaceEnum BufferAddressSpace, typename T, typename ElementSpaceSize>
382
383
384
385
386
__host__ __device__ constexpr auto make_dynamic_buffer(T* p, ElementSpaceSize element_space_size)
{
    return DynamicBuffer<BufferAddressSpace, T, ElementSpaceSize, true>{p, element_space_size};
}

387
template <
388
    AddressSpaceEnum BufferAddressSpace,
389
390
391
392
    typename T,
    typename ElementSpaceSize,
    typename X,
    typename enable_if<is_same<remove_cvref_t<T>, remove_cvref_t<X>>::value, bool>::type = false>
393
__host__ __device__ constexpr auto
394
make_dynamic_buffer(T* p, ElementSpaceSize element_space_size, X invalid_element_value)
395
396
397
398
399
400
{
    return DynamicBuffer<BufferAddressSpace, T, ElementSpaceSize, false>{
        p, element_space_size, invalid_element_value};
}

} // namespace ck