blockwise_4d_tensor_op.hip.hpp 41.9 KB
Newer Older
1
#pragma once
2
#include "ConstantTensorDescriptor.hip.hpp"
3
#include "threadwise_nd_tensor_op.hip.hpp"
4

Chao Liu's avatar
Chao Liu committed
5
template <index_t BlockSize, class Float, class DstDesc, class F>
6
__device__ void
Chao Liu's avatar
Chao Liu committed
7
blockwise_4d_tensor_pointwise_operation_unary(DstDesc, Float* __restrict__ p_dst, F f)
Chao Liu's avatar
Chao Liu committed
8
{
Chao Liu's avatar
Chao Liu committed
9
10
11
12
    constexpr auto I0 = Number<0>{};
    constexpr auto I1 = Number<1>{};
    constexpr auto I2 = Number<2>{};
    constexpr auto I3 = Number<3>{};
Chao Liu's avatar
Chao Liu committed
13

14
15
    constexpr auto dst_desc = DstDesc{};

Chao Liu's avatar
Chao Liu committed
16
    constexpr auto desc = make_ConstantTensorDescriptor(dst_desc.GetLengths());
Chao Liu's avatar
Chao Liu committed
17

18
#if 0
19
    if(get_thread_local_1d_id() == 0)
20
    {
Chao Liu's avatar
Chao Liu committed
21
22
        print_ConstantTensorDescriptor(dst_desc, "blockwise_4d_tensor_op_unary: dst_desc: ");
        print_ConstantTensorDescriptor(desc, "blockwise_4d_tensor_op_unary: desc: ");
23
24
25
    }
#endif

Chao Liu's avatar
Chao Liu committed
26
    constexpr index_t NLoop = desc.GetElementSize() / BlockSize;
Chao Liu's avatar
Chao Liu committed
27

Chao Liu's avatar
Chao Liu committed
28
    for(index_t iloop = 0; iloop < NLoop; ++iloop)
Chao Liu's avatar
Chao Liu committed
29
    {
30
        index_t is = get_thread_local_1d_id() + iloop * BlockSize;
Chao Liu's avatar
Chao Liu committed
31

Chao Liu's avatar
Chao Liu committed
32
        const index_t did0 = is / desc.GetStride(I0);
Chao Liu's avatar
Chao Liu committed
33
34
35

        is -= did0 * desc.GetStride(I0);

Chao Liu's avatar
Chao Liu committed
36
        const index_t did1 = is / desc.GetStride(I1);
Chao Liu's avatar
Chao Liu committed
37
38
39

        is -= did1 * desc.GetStride(I1);

Chao Liu's avatar
Chao Liu committed
40
        const index_t did2 = is / desc.GetStride(I2);
Chao Liu's avatar
Chao Liu committed
41
42
43

        is -= did2 * desc.GetStride(I2);

Chao Liu's avatar
Chao Liu committed
44
        const index_t did3 = is / desc.GetStride(I3);
Chao Liu's avatar
Chao Liu committed
45

Chao Liu's avatar
Chao Liu committed
46
        const index_t dindex = dst_desc.Get1dIndex(did0, did1, did2, did3);
Chao Liu's avatar
Chao Liu committed
47

Chao Liu's avatar
Chao Liu committed
48
        f(p_dst[dindex]);
Chao Liu's avatar
Chao Liu committed
49
50
51
52
53
54
    }

    constexpr bool has_tail = (desc.GetElementSize() > NLoop * BlockSize);

    if(has_tail)
    {
55
        index_t is = get_thread_local_1d_id() + NLoop * BlockSize;
Chao Liu's avatar
Chao Liu committed
56
57
58

        if(is < desc.GetElementSize())
        {
Chao Liu's avatar
Chao Liu committed
59
            const index_t did0 = is / desc.GetStride(I0);
Chao Liu's avatar
Chao Liu committed
60
61
62

            is -= did0 * desc.GetStride(I0);

Chao Liu's avatar
Chao Liu committed
63
            const index_t did1 = is / desc.GetStride(I1);
Chao Liu's avatar
Chao Liu committed
64
65
66

            is -= did1 * desc.GetStride(I1);

Chao Liu's avatar
Chao Liu committed
67
            const index_t did2 = is / desc.GetStride(I2);
Chao Liu's avatar
Chao Liu committed
68
69
70

            is -= did2 * desc.GetStride(I2);

Chao Liu's avatar
Chao Liu committed
71
            const index_t did3 = is / desc.GetStride(I3);
Chao Liu's avatar
Chao Liu committed
72

Chao Liu's avatar
Chao Liu committed
73
            const index_t dindex = dst_desc.Get1dIndex(did0, did1, did2, did3);
Chao Liu's avatar
Chao Liu committed
74

Chao Liu's avatar
Chao Liu committed
75
            f(p_dst[dindex]);
Chao Liu's avatar
Chao Liu committed
76
77
78
        }
    }
}
Chao Liu's avatar
Chao Liu committed
79

Chao Liu's avatar
Chao Liu committed
80
// Function: p_dst[reorder[i0], reorder[i1], reorder[i2], reorder[i3]] = p_src[i0,i1,i2,i3]
81
82
// TODO: in order to optimize mem access for different mem type,
// need to write specialized version
Chao Liu's avatar
Chao Liu committed
83
template <index_t BlockSize,
Chao Liu's avatar
Chao Liu committed
84
          class Float,
85
86
          class SrcDesc,
          class DstDesc,
Chao Liu's avatar
Chao Liu committed
87
          class SrcOpLengths,
88
          class MapDst2Src,
Chao Liu's avatar
Chao Liu committed
89
          class F>
Chao Liu's avatar
Chao Liu committed
90
91
__device__ void blockwise_4d_tensor_pointwise_operation_binary_reorder_by_get_dst_from_src(
    SrcDesc,
Chao Liu's avatar
Chao Liu committed
92
    const Float* __restrict__ p_src,
Chao Liu's avatar
Chao Liu committed
93
94
95
    DstDesc,
    Float* __restrict__ p_dst,
    SrcOpLengths,
96
    MapDst2Src,
Chao Liu's avatar
Chao Liu committed
97
    F f)
Chao Liu's avatar
Chao Liu committed
98
{
Chao Liu's avatar
Chao Liu committed
99
100
101
102
    constexpr auto I0 = Number<0>{};
    constexpr auto I1 = Number<1>{};
    constexpr auto I2 = Number<2>{};
    constexpr auto I3 = Number<3>{};
Chao Liu's avatar
Chao Liu committed
103

104
105
106
107
    constexpr index_t IR0 = MapDst2Src{}.Get(I0);
    constexpr index_t IR1 = MapDst2Src{}.Get(I1);
    constexpr index_t IR2 = MapDst2Src{}.Get(I2);
    constexpr index_t IR3 = MapDst2Src{}.Get(I3);
Chao Liu's avatar
Chao Liu committed
108

109
110
    constexpr auto src_desc = SrcDesc{};
    constexpr auto dst_desc = DstDesc{};
Chao Liu's avatar
Chao Liu committed
111
    constexpr auto ref_desc = make_ConstantTensorDescriptor(SrcOpLengths{});
Chao Liu's avatar
Chao Liu committed
112

Chao Liu's avatar
Chao Liu committed
113
    constexpr index_t NLoop = ref_desc.GetElementSize() / BlockSize;
Chao Liu's avatar
Chao Liu committed
114

Chao Liu's avatar
Chao Liu committed
115
    for(index_t iloop = 0; iloop < NLoop; ++iloop)
Chao Liu's avatar
Chao Liu committed
116
    {
117
        index_t is = get_thread_local_1d_id() + iloop * BlockSize;
Chao Liu's avatar
Chao Liu committed
118

Chao Liu's avatar
Chao Liu committed
119
        index_t did[4];
Chao Liu's avatar
Chao Liu committed
120

121
        did[0] = is / ref_desc.GetStride(I0);
Chao Liu's avatar
Chao Liu committed
122

123
        is -= did[0] * ref_desc.GetStride(I0);
Chao Liu's avatar
Chao Liu committed
124

125
        did[1] = is / ref_desc.GetStride(I1);
Chao Liu's avatar
Chao Liu committed
126

127
        is -= did[1] * ref_desc.GetStride(I1);
Chao Liu's avatar
Chao Liu committed
128

129
        did[2] = is / ref_desc.GetStride(I2);
Chao Liu's avatar
Chao Liu committed
130

131
        is -= did[2] * ref_desc.GetStride(I2);
Chao Liu's avatar
Chao Liu committed
132

133
        did[3] = is / ref_desc.GetStride(I3);
Chao Liu's avatar
Chao Liu committed
134

Chao Liu's avatar
Chao Liu committed
135
        const index_t src_index = src_desc.Get1dIndex(did[0], did[1], did[2], did[3]);
Chao Liu's avatar
Chao Liu committed
136

Chao Liu's avatar
Chao Liu committed
137
        const index_t dst_index = dst_desc.Get1dIndex(did[IR0], did[IR1], did[IR2], did[IR3]);
138

Chao Liu's avatar
Chao Liu committed
139
        f(p_src[src_index], p_dst[dst_index]);
Chao Liu's avatar
Chao Liu committed
140
141
    }

142
    constexpr bool has_tail = (ref_desc.GetElementSize() > NLoop * BlockSize);
Chao Liu's avatar
Chao Liu committed
143
144
145

    if(has_tail)
    {
146
        index_t is = get_thread_local_1d_id() + NLoop * BlockSize;
Chao Liu's avatar
Chao Liu committed
147

148
        if(is < ref_desc.GetElementSize())
Chao Liu's avatar
Chao Liu committed
149
        {
Chao Liu's avatar
Chao Liu committed
150
            index_t did[4];
151
152

            did[0] = is / ref_desc.GetStride(I0);
Chao Liu's avatar
Chao Liu committed
153

154
            is -= did[0] * ref_desc.GetStride(I0);
Chao Liu's avatar
Chao Liu committed
155

156
            did[1] = is / ref_desc.GetStride(I1);
Chao Liu's avatar
Chao Liu committed
157

158
            is -= did[1] * ref_desc.GetStride(I1);
Chao Liu's avatar
Chao Liu committed
159

160
            did[2] = is / ref_desc.GetStride(I2);
Chao Liu's avatar
Chao Liu committed
161

162
            is -= did[2] * ref_desc.GetStride(I2);
Chao Liu's avatar
Chao Liu committed
163

164
            did[3] = is / ref_desc.GetStride(I3);
Chao Liu's avatar
Chao Liu committed
165

Chao Liu's avatar
Chao Liu committed
166
            const index_t src_index = src_desc.Get1dIndex(did[0], did[1], did[2], did[3]);
167

Chao Liu's avatar
Chao Liu committed
168
            const index_t dst_index = dst_desc.Get1dIndex(did[IR0], did[IR1], did[IR2], did[IR3]);
169

Chao Liu's avatar
Chao Liu committed
170
            f(p_src[src_index], p_dst[dst_index]);
171
172
173
174
        }
    }
}

Chao Liu's avatar
Chao Liu committed
175
template <index_t BlockSize, class Float, class DstDesc>
Chao Liu's avatar
Chao Liu committed
176
__device__ void blockwise_4d_tensor_set_zero(DstDesc, Float* __restrict__ p_dst)
177
{
Chao Liu's avatar
Chao Liu committed
178
    auto f_set_zero = [](Float& v) { v = Float(0); };
Chao Liu's avatar
Chao Liu committed
179

Chao Liu's avatar
Chao Liu committed
180
    blockwise_4d_tensor_pointwise_operation_unary<BlockSize>(DstDesc{}, p_dst, f_set_zero);
Chao Liu's avatar
Chao Liu committed
181
}
182

Chao Liu's avatar
Chao Liu committed
183
template <index_t BlockSize,
Chao Liu's avatar
Chao Liu committed
184
          class Float,
185
186
          class SrcDesc,
          class DstDesc,
Chao Liu's avatar
Chao Liu committed
187
          class SrcOpLengths,
188
          class MapDst2Src>
Chao Liu's avatar
Chao Liu committed
189
190
__device__ void
blockwise_4d_tensor_copy_reorder_by_get_dst_from_src(SrcDesc,
Chao Liu's avatar
Chao Liu committed
191
                                                     const Float* __restrict__ p_src,
Chao Liu's avatar
Chao Liu committed
192
193
194
                                                     DstDesc,
                                                     Float* __restrict__ p_dst,
                                                     SrcOpLengths,
195
                                                     MapDst2Src)
196
{
Chao Liu's avatar
Chao Liu committed
197
    auto f_copy = [](const Float& src, Float& dst) { dst = src; };
198

Chao Liu's avatar
Chao Liu committed
199
    blockwise_4d_tensor_pointwise_operation_binary_reorder_by_get_dst_from_src<BlockSize>(
200
        SrcDesc{}, p_src, DstDesc{}, p_dst, SrcOpLengths{}, MapDst2Src{}, f_copy);
201
202
}

Chao Liu's avatar
Chao Liu committed
203
template <index_t BlockSize,
Chao Liu's avatar
Chao Liu committed
204
205
206
207
          class Float,
          class SrcDesc,
          class DstDesc,
          class CopyLengths,
Chao Liu's avatar
Chao Liu committed
208
          index_t DataPerRead>
209
struct Blockwise4dTensorCopy1
Chao Liu's avatar
Chao Liu committed
210
{
211
    using vector_t = typename vector_type<Float, DataPerRead>::MemoryType;
Chao Liu's avatar
Chao Liu committed
212

213
    __device__ constexpr Blockwise4dTensorCopy1()
Chao Liu's avatar
Chao Liu committed
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};

        static_assert(DataPerRead == 1 ||
                          (SrcDesc{}.GetStride(I3) == 1 && DstDesc{}.GetStride(I3) == 1),
                      "wrong! only support stride3 == 1 if DataPerRead > 1!\n");

        static_assert(DataPerRead == 1 || DataPerRead == 2 || DataPerRead == 4,
                      "wrong! only support DataPerRead == 1, 2 or 4!\n");

        static_assert(SrcDesc{}.GetStride(I2) % DataPerRead == 0 &&
                          DstDesc{}.GetStride(I2) % DataPerRead == 0,
                      "src and dst stride2 should be multiple of DataPerRead to keep alignment");

        // we allow out-of-bound read from src in D3 dimension,
        //   but we need to make sure dst stride2 is big enough,
        //   so that the out-of-bound write won't contaminate next line in dst
Chao Liu's avatar
Chao Liu committed
234
        constexpr index_t L3          = CopyLengths{}.Get(I3);
235
        constexpr index_t read_per_d3 = mod_conv::integer_divide_ceil(L3, DataPerRead);
Chao Liu's avatar
Chao Liu committed
236
237
238
239
240

        static_assert(read_per_d3 * DataPerRead <= DstDesc{}.GetStride(I2),
                      "wrong! out-of-bound write will contaminate next line!\n");
    }

Chao Liu's avatar
Chao Liu committed
241
    __device__ void Run(const Float* __restrict__ p_src, Float* __restrict__ p_dst) const
Chao Liu's avatar
Chao Liu committed
242
    {
Chao Liu's avatar
Chao Liu committed
243
244
245
246
247
248
249
250
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};

        constexpr auto src_desc = SrcDesc{};
        constexpr auto dst_desc = DstDesc{};

Chao Liu's avatar
Chao Liu committed
251
252
253
254
        constexpr index_t L0 = CopyLengths{}.Get(I0);
        constexpr index_t L1 = CopyLengths{}.Get(I1);
        constexpr index_t L2 = CopyLengths{}.Get(I2);
        constexpr index_t L3 = CopyLengths{}.Get(I3);
Chao Liu's avatar
Chao Liu committed
255

256
        constexpr index_t read_per_d3 = mod_conv::integer_divide_ceil(L3, DataPerRead);
Chao Liu's avatar
Chao Liu committed
257
258
259

        constexpr auto ref_desc =
            make_ConstantTensorDescriptor(Sequence<L0, L1, L2, read_per_d3>{});
260

Chao Liu's avatar
Chao Liu committed
261
        constexpr index_t NLoop = ref_desc.GetElementSize() / BlockSize;
Chao Liu's avatar
Chao Liu committed
262

Chao Liu's avatar
Chao Liu committed
263
264
        auto f_copy = [&](index_t is) {
            index_t did[4];
Chao Liu's avatar
Chao Liu committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

            did[0] = is / ref_desc.GetStride(I0);

            is -= did[0] * ref_desc.GetStride(I0);

            did[1] = is / ref_desc.GetStride(I1);

            is -= did[1] * ref_desc.GetStride(I1);

            did[2] = is / ref_desc.GetStride(I2);

            is -= did[2] * ref_desc.GetStride(I2);

            did[3] = is / ref_desc.GetStride(I3);

Chao Liu's avatar
Chao Liu committed
280
            const index_t src_index =
Chao Liu's avatar
Chao Liu committed
281
                src_desc.Get1dIndex(did[0], did[1], did[2], did[3] * DataPerRead);
Chao Liu's avatar
Chao Liu committed
282
            const index_t dst_index =
Chao Liu's avatar
Chao Liu committed
283
284
285
286
287
288
                dst_desc.Get1dIndex(did[0], did[1], did[2], did[3] * DataPerRead);

            *(reinterpret_cast<vector_t*>(p_dst + dst_index)) =
                *(reinterpret_cast<const vector_t*>(p_src + src_index));
        };

Chao Liu's avatar
Chao Liu committed
289
        for(index_t iloop = 0; iloop < NLoop; ++iloop)
Chao Liu's avatar
Chao Liu committed
290
        {
291
            index_t is = get_thread_local_1d_id() + iloop * BlockSize;
Chao Liu's avatar
Chao Liu committed
292
293
294
295
296
297
298
299

            f_copy(is);
        }

        constexpr bool has_tail = (ref_desc.GetElementSize() > NLoop * BlockSize);

        if(has_tail)
        {
300
            index_t is = get_thread_local_1d_id() + NLoop * BlockSize;
Chao Liu's avatar
Chao Liu committed
301
302
303
304
305
306

            if(is < ref_desc.GetElementSize())
            {
                f_copy(is);
            }
        }
Chao Liu's avatar
Chao Liu committed
307
308
309
    }
};

Chao Liu's avatar
Chao Liu committed
310
template <index_t BlockSize,
311
312
313
314
315
          class Float,
          class SrcDesc,
          class DstDesc,
          class DstOpLengths,
          class GlobalLowerPads>
316
struct BlockwiseChwnTensorCopyPadded
317
{
Chao Liu's avatar
Chao Liu committed
318
    __device__ void Run(const Float* __restrict__ p_src,
Chao Liu's avatar
Chao Liu committed
319
320
321
322
                        index_t c_block_data_begin,
                        index_t ho_block_data_begin,
                        index_t wo_block_data_begin,
                        index_t n_block_data_begin,
323
                        Float* __restrict__ p_dst,
Chao Liu's avatar
Chao Liu committed
324
325
326
327
                        index_t h_block_pad_low,
                        index_t w_block_pad_low,
                        index_t h_block_pad_up,
                        index_t w_block_pad_up) const
328
329
330
331
332
333
334
335
336
337
338
339
340
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};

        constexpr auto src_desc = SrcDesc{};
        constexpr auto dst_desc = DstDesc{};
        constexpr auto ref_desc = make_ConstantTensorDescriptor(DstOpLengths{});

        constexpr auto h_global_pad_low = GlobalLowerPads{}.Get(I0);
        constexpr auto w_global_pad_low = GlobalLowerPads{}.Get(I1);

Chao Liu's avatar
Chao Liu committed
341
        constexpr index_t NLoop = ref_desc.GetElementSize() / BlockSize;
342

Chao Liu's avatar
Chao Liu committed
343
        const Float* p_src_tmp =
344
345
346
347
            p_src + src_desc.Get1dIndex(c_block_data_begin,
                                        (ho_block_data_begin + h_block_pad_low) - h_global_pad_low,
                                        (wo_block_data_begin + w_block_pad_low) - w_global_pad_low,
                                        n_block_data_begin);
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370

#if 0
        if(get_thread_local_1d_id() == 0)
        {
            print_ConstantTensorDescriptor(src_desc, "src_desc: ");
            print_ConstantTensorDescriptor(dst_desc, "dst_desc: ");
            print_ConstantTensorDescriptor(ref_desc, "ref_desc: ");

            printf("%u %u, \t"
                   "h_global_pad_low %u w_global_pad_low %u \t"
                   "h_block_pad_low %u w_block_pad_low %u h_block_pad_up %u  w_block_pad_up %u \t"
                   "\n",
                   get_block_1d_id(),
                   get_thread_local_1d_id(),
                   h_global_pad_low,
                   w_global_pad_low,
                   h_block_pad_low,
                   w_block_pad_low,
                   h_block_pad_up,
                   w_block_pad_up);
        }
#endif

Chao Liu's avatar
Chao Liu committed
371
        for(index_t iloop = 0; iloop < NLoop; ++iloop)
372
        {
373
            index_t is = get_thread_local_1d_id() + iloop * BlockSize;
374

Chao Liu's avatar
Chao Liu committed
375
            index_t did[4];
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390

            did[0] = is / ref_desc.GetStride(I0);

            is -= did[0] * ref_desc.GetStride(I0);

            did[1] = is / ref_desc.GetStride(I1);

            is -= did[1] * ref_desc.GetStride(I1);

            did[2] = is / ref_desc.GetStride(I2);

            is -= did[2] * ref_desc.GetStride(I2);

            did[3] = is / ref_desc.GetStride(I3);

Chao Liu's avatar
Chao Liu committed
391
            const index_t bindex = dst_desc.Get1dIndex(did[0], did[1], did[2], did[3]);
392
393
394
395
396
397
398
399
400
401
402
403

            p_dst[bindex] =
                (did[1] < h_block_pad_low || did[1] + h_block_pad_up >= ref_desc.GetLength(I1) ||
                 did[2] < w_block_pad_low || did[2] + w_block_pad_up >= ref_desc.GetLength(I2))
                    ? Float(0)
                    : p_src_tmp[src_desc.Get1dIndex(did[0], did[1], did[2], did[3])];
        }

        constexpr bool has_tail = (ref_desc.GetElementSize() > NLoop * BlockSize);

        if(has_tail)
        {
404
            index_t is = get_thread_local_1d_id() + NLoop * BlockSize;
405
406
407

            if(is < ref_desc.GetElementSize())
            {
Chao Liu's avatar
Chao Liu committed
408
                index_t did[4];
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423

                did[0] = is / ref_desc.GetStride(I0);

                is -= did[0] * ref_desc.GetStride(I0);

                did[1] = is / ref_desc.GetStride(I1);

                is -= did[1] * ref_desc.GetStride(I1);

                did[2] = is / ref_desc.GetStride(I2);

                is -= did[2] * ref_desc.GetStride(I2);

                did[3] = is / ref_desc.GetStride(I3);

Chao Liu's avatar
Chao Liu committed
424
                const index_t bindex = dst_desc.Get1dIndex(did[0], did[1], did[2], did[3]);
425
426
427
428
429
430
431
432
433
434

                p_dst[bindex] =
                    (did[1] < h_block_pad_low ||
                     did[1] + h_block_pad_up >= ref_desc.GetLength(I1) ||
                     did[2] < w_block_pad_low || did[2] + w_block_pad_up >= ref_desc.GetLength(I2))
                        ? Float(0)
                        : p_src_tmp[src_desc.Get1dIndex(did[0], did[1], did[2], did[3])];
            }
        }
    }
Chao Liu's avatar
Chao Liu committed
435
};
Chao Liu's avatar
Chao Liu committed
436
437
438

// starting point need to be aligned to float4 or float2 or float
// stride3 need to be 1 for both source and destination
Chao Liu's avatar
Chao Liu committed
439
template <index_t BlockSize,
Chao Liu's avatar
Chao Liu committed
440
441
442
443
444
          class Float,
          class SrcDesc,
          class DstDesc,
          class CopyLengths,
          class ThreadPerDims,
Chao Liu's avatar
Chao Liu committed
445
          index_t DataPerRead>
Chao Liu's avatar
Chao Liu committed
446
447
struct Blockwise4dTensorCopy3
{
448
    using vector_t = typename vector_type<Float, DataPerRead>::MemoryType;
Chao Liu's avatar
Chao Liu committed
449

Chao Liu's avatar
Chao Liu committed
450
451
    index_t mSrcMyThreadOffset;
    index_t mDstMyThreadOffset;
Chao Liu's avatar
Chao Liu committed
452
453
454
455
456
457
458
459

    __device__ Blockwise4dTensorCopy3()
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};

Chao Liu's avatar
Chao Liu committed
460
461
462
        static_assert(DataPerRead == 1 ||
                          (SrcDesc{}.GetStride(I3) == 1 && DstDesc{}.GetStride(I3) == 1),
                      "wrong! only support stride3 == 1 if DataPerRead > 1!\n");
Chao Liu's avatar
Chao Liu committed
463
464
465
466
467
468
469

        static_assert(DataPerRead == 1 || DataPerRead == 2 || DataPerRead == 4,
                      "wrong! only support DataPerRead == 1, 2 or 4!\n");

        static_assert(
            SrcDesc{}.GetStride(I2) % DataPerRead == 0 &&
                DstDesc{}.GetStride(I2) % DataPerRead == 0,
Chao Liu's avatar
Chao Liu committed
470
            "wrong! src and dst stride2 should be multiple of DataPerRead to keep alignment");
Chao Liu's avatar
Chao Liu committed
471

Chao Liu's avatar
Chao Liu committed
472
473
474
475
        constexpr index_t L0 = CopyLengths{}.Get(I0);
        constexpr index_t L1 = CopyLengths{}.Get(I1);
        constexpr index_t L2 = CopyLengths{}.Get(I2);
        constexpr index_t L3 = CopyLengths{}.Get(I3);
Chao Liu's avatar
Chao Liu committed
476

Chao Liu's avatar
Chao Liu committed
477
478
479
480
        constexpr index_t thread_per_d0 = ThreadPerDims{}.Get(I0);
        constexpr index_t thread_per_d1 = ThreadPerDims{}.Get(I1);
        constexpr index_t thread_per_d2 = ThreadPerDims{}.Get(I2);
        constexpr index_t thread_per_d3 = ThreadPerDims{}.Get(I3);
Chao Liu's avatar
Chao Liu committed
481
482
483
484

        // we allow out-of-bound read from src in D3 dimension,
        //   but we need to make sure dst stride is big enough,
        //   so that the out-of-bound write won't contaminate next line in dst
485
        constexpr index_t nloop_d3 = mod_conv::integer_divide_ceil(L3, thread_per_d3 * DataPerRead);
Chao Liu's avatar
Chao Liu committed
486
487
488
489
490
491
492
493
494
495

        static_assert(nloop_d3 * thread_per_d3 * DataPerRead <= DstDesc{}.GetStride(I2),
                      "wrong! out-of-bound write will contaminate next line!\n");

        static_assert(L0 % thread_per_d0 == 0 && L1 % thread_per_d1 == 0 && L2 % thread_per_d2 == 0,
                      "wrong! L0, L1, L2 should be divided evenly!\n");

        static_assert(BlockSize >= thread_per_d0 * thread_per_d1 * thread_per_d2 * thread_per_d3,
                      "wrrong! BlockSize is not big enough for ThreadPerDims!");

Chao Liu's avatar
Chao Liu committed
496
        constexpr index_t num_active_thread =
497
            accumulate_on_sequence(ThreadPerDims{}, mod_conv::multiplies<index_t>{}, Number<1>{});
Chao Liu's avatar
Chao Liu committed
498
499
500
501
502
503
504
505
506

        if(BlockSize > num_active_thread)
        {
            if(get_thread_local_1d_id() >= num_active_thread)
            {
                return;
            }
        }

507
508
        constexpr auto thread_cluster_desc = make_ConstantTensorDescriptor(ThreadPerDims{});
        const auto thread_multi_id = thread_cluster_desc.GetMultiIndex(get_thread_local_1d_id());
Chao Liu's avatar
Chao Liu committed
509

510
511
512
513
514
515
516
517
518
        mSrcMyThreadOffset = SrcDesc{}.Get1dIndex(thread_multi_id[0],
                                                  thread_multi_id[1],
                                                  thread_multi_id[2],
                                                  thread_multi_id[3] * DataPerRead);

        mDstMyThreadOffset = DstDesc{}.Get1dIndex(thread_multi_id[0],
                                                  thread_multi_id[1],
                                                  thread_multi_id[2],
                                                  thread_multi_id[3] * DataPerRead);
Chao Liu's avatar
Chao Liu committed
519
520
521
522
523
524
525
526
527
    }

    __device__ void Run(const Float* __restrict__ p_src, Float* __restrict__ p_dst) const
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};

Chao Liu's avatar
Chao Liu committed
528
529
530
531
        constexpr index_t L0 = CopyLengths{}.Get(I0);
        constexpr index_t L1 = CopyLengths{}.Get(I1);
        constexpr index_t L2 = CopyLengths{}.Get(I2);
        constexpr index_t L3 = CopyLengths{}.Get(I3);
Chao Liu's avatar
Chao Liu committed
532

Chao Liu's avatar
Chao Liu committed
533
534
535
536
        constexpr index_t thread_per_d0 = ThreadPerDims{}.Get(I0);
        constexpr index_t thread_per_d1 = ThreadPerDims{}.Get(I1);
        constexpr index_t thread_per_d2 = ThreadPerDims{}.Get(I2);
        constexpr index_t thread_per_d3 = ThreadPerDims{}.Get(I3);
Chao Liu's avatar
Chao Liu committed
537

Chao Liu's avatar
Chao Liu committed
538
        constexpr index_t num_active_thread =
Chao Liu's avatar
Chao Liu committed
539
540
541
542
543
544
545
546
547
548
            thread_per_d0 * thread_per_d1 * thread_per_d2 * thread_per_d3;

        if(BlockSize > num_active_thread)
        {
            if(get_thread_local_1d_id() >= num_active_thread)
            {
                return;
            }
        }

Chao Liu's avatar
Chao Liu committed
549
550
551
        constexpr index_t nloop_d0 = L0 / thread_per_d0;
        constexpr index_t nloop_d1 = L1 / thread_per_d1;
        constexpr index_t nloop_d2 = L2 / thread_per_d2;
552
        constexpr index_t nloop_d3 = mod_conv::integer_divide_ceil(L3, thread_per_d3 * DataPerRead);
Chao Liu's avatar
Chao Liu committed
553
554

#pragma unroll
Chao Liu's avatar
Chao Liu committed
555
        for(index_t iloop_d0 = 0; iloop_d0 < nloop_d0; ++iloop_d0)
Chao Liu's avatar
Chao Liu committed
556
557
        {
#pragma unroll
Chao Liu's avatar
Chao Liu committed
558
            for(index_t iloop_d1 = 0; iloop_d1 < nloop_d1; ++iloop_d1)
Chao Liu's avatar
Chao Liu committed
559
560
            {
#pragma unroll
Chao Liu's avatar
Chao Liu committed
561
                for(index_t iloop_d2 = 0; iloop_d2 < nloop_d2; ++iloop_d2)
Chao Liu's avatar
Chao Liu committed
562
563
                {
#pragma unroll
Chao Liu's avatar
Chao Liu committed
564
                    for(index_t iloop_d3 = 0; iloop_d3 < nloop_d3; ++iloop_d3)
Chao Liu's avatar
Chao Liu committed
565
                    {
Chao Liu's avatar
Chao Liu committed
566
                        const index_t src_offset =
Chao Liu's avatar
Chao Liu committed
567
568
569
570
571
                            SrcDesc{}.Get1dIndex(iloop_d0 * thread_per_d0,
                                                 iloop_d1 * thread_per_d1,
                                                 iloop_d2 * thread_per_d2,
                                                 iloop_d3 * thread_per_d3 * DataPerRead);

Chao Liu's avatar
Chao Liu committed
572
                        const index_t dst_offset =
Chao Liu's avatar
Chao Liu committed
573
574
575
576
577
                            DstDesc{}.Get1dIndex(iloop_d0 * thread_per_d0,
                                                 iloop_d1 * thread_per_d1,
                                                 iloop_d2 * thread_per_d2,
                                                 iloop_d3 * thread_per_d3 * DataPerRead);

578
                        *(reinterpret_cast<vector_t*>(&p_dst[dst_offset + mDstMyThreadOffset])) =
Chao Liu's avatar
tidy yp  
Chao Liu committed
579
580
                            *(reinterpret_cast<const vector_t*>(
                                &p_src[src_offset + mSrcMyThreadOffset]));
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
                    }
                }
            }
        }
    }

    __device__ constexpr index_t GetRegisterClipboardSize() const
    {
        static_assert(is_same<Float, float>::value, "wrong! only support float!\n");

        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};

        constexpr index_t L0 = CopyLengths{}.Get(I0);
        constexpr index_t L1 = CopyLengths{}.Get(I1);
        constexpr index_t L2 = CopyLengths{}.Get(I2);
        constexpr index_t L3 = CopyLengths{}.Get(I3);

        constexpr index_t thread_per_d0 = ThreadPerDims{}.Get(I0);
        constexpr index_t thread_per_d1 = ThreadPerDims{}.Get(I1);
        constexpr index_t thread_per_d2 = ThreadPerDims{}.Get(I2);
        constexpr index_t thread_per_d3 = ThreadPerDims{}.Get(I3);

        constexpr index_t nloop_d0 = L0 / thread_per_d0;
        constexpr index_t nloop_d1 = L1 / thread_per_d1;
        constexpr index_t nloop_d2 = L2 / thread_per_d2;
609
        constexpr index_t nloop_d3 = mod_conv::integer_divide_ceil(L3, thread_per_d3 * DataPerRead);
610
611
612
613

        return DataPerRead * nloop_d0 * nloop_d1 * nloop_d2 * nloop_d3;
    }

Chao Liu's avatar
tidy yp  
Chao Liu committed
614
615
    __device__ void RunLoadRegisterClipboard(const Float* __restrict__ p_src,
                                             Float* __restrict__ p_clipboard) const
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};

        constexpr index_t L0 = CopyLengths{}.Get(I0);
        constexpr index_t L1 = CopyLengths{}.Get(I1);
        constexpr index_t L2 = CopyLengths{}.Get(I2);
        constexpr index_t L3 = CopyLengths{}.Get(I3);

        constexpr index_t thread_per_d0 = ThreadPerDims{}.Get(I0);
        constexpr index_t thread_per_d1 = ThreadPerDims{}.Get(I1);
        constexpr index_t thread_per_d2 = ThreadPerDims{}.Get(I2);
        constexpr index_t thread_per_d3 = ThreadPerDims{}.Get(I3);

        constexpr index_t num_active_thread =
            thread_per_d0 * thread_per_d1 * thread_per_d2 * thread_per_d3;

        if(BlockSize > num_active_thread)
        {
            if(get_thread_local_1d_id() >= num_active_thread)
            {
                return;
            }
        }

        constexpr index_t nloop_d0 = L0 / thread_per_d0;
        constexpr index_t nloop_d1 = L1 / thread_per_d1;
        constexpr index_t nloop_d2 = L2 / thread_per_d2;
646
        constexpr index_t nloop_d3 = mod_conv::integer_divide_ceil(L3, thread_per_d3 * DataPerRead);
647

648
649
650
        constexpr auto clipboard_desc = make_ConstantTensorDescriptor(
            Sequence<nloop_d0, nloop_d1, nloop_d2, nloop_d3 * DataPerRead>{});

651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#pragma unroll
        for(index_t iloop_d0 = 0; iloop_d0 < nloop_d0; ++iloop_d0)
        {
#pragma unroll
            for(index_t iloop_d1 = 0; iloop_d1 < nloop_d1; ++iloop_d1)
            {
#pragma unroll
                for(index_t iloop_d2 = 0; iloop_d2 < nloop_d2; ++iloop_d2)
                {
#pragma unroll
                    for(index_t iloop_d3 = 0; iloop_d3 < nloop_d3; ++iloop_d3)
                    {
                        const index_t src_offset =
                            SrcDesc{}.Get1dIndex(iloop_d0 * thread_per_d0,
                                                 iloop_d1 * thread_per_d1,
                                                 iloop_d2 * thread_per_d2,
                                                 iloop_d3 * thread_per_d3 * DataPerRead);

669
670
                        const index_t clipboard_offset = clipboard_desc.Get1dIndex(
                            iloop_d0, iloop_d1, iloop_d2, iloop_d3 * DataPerRead);
671

672
                        *(reinterpret_cast<vector_t*>(&p_clipboard[clipboard_offset])) =
Chao Liu's avatar
tidy yp  
Chao Liu committed
673
674
                            *(reinterpret_cast<const vector_t*>(
                                &p_src[src_offset + mSrcMyThreadOffset]));
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
                    }
                }
            }
        }
    }

    __device__ void RunStoreRegisterClipboard(const Float* __restrict__ p_clipboard,
                                              Float* __restrict__ p_dst) const
    {
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};

        constexpr index_t L0 = CopyLengths{}.Get(I0);
        constexpr index_t L1 = CopyLengths{}.Get(I1);
        constexpr index_t L2 = CopyLengths{}.Get(I2);
        constexpr index_t L3 = CopyLengths{}.Get(I3);

        constexpr index_t thread_per_d0 = ThreadPerDims{}.Get(I0);
        constexpr index_t thread_per_d1 = ThreadPerDims{}.Get(I1);
        constexpr index_t thread_per_d2 = ThreadPerDims{}.Get(I2);
        constexpr index_t thread_per_d3 = ThreadPerDims{}.Get(I3);

        constexpr index_t num_active_thread =
            thread_per_d0 * thread_per_d1 * thread_per_d2 * thread_per_d3;

        if(BlockSize > num_active_thread)
        {
            if(get_thread_local_1d_id() >= num_active_thread)
            {
                return;
            }
        }

        constexpr index_t nloop_d0 = L0 / thread_per_d0;
        constexpr index_t nloop_d1 = L1 / thread_per_d1;
        constexpr index_t nloop_d2 = L2 / thread_per_d2;
713
        constexpr index_t nloop_d3 = mod_conv::integer_divide_ceil(L3, thread_per_d3 * DataPerRead);
714

715
716
717
        constexpr auto clipboard_desc = make_ConstantTensorDescriptor(
            Sequence<nloop_d0, nloop_d1, nloop_d2, nloop_d3 * DataPerRead>{});

718
719
720
721
722
723
724
725
726
727
728
729
#pragma unroll
        for(index_t iloop_d0 = 0; iloop_d0 < nloop_d0; ++iloop_d0)
        {
#pragma unroll
            for(index_t iloop_d1 = 0; iloop_d1 < nloop_d1; ++iloop_d1)
            {
#pragma unroll
                for(index_t iloop_d2 = 0; iloop_d2 < nloop_d2; ++iloop_d2)
                {
#pragma unroll
                    for(index_t iloop_d3 = 0; iloop_d3 < nloop_d3; ++iloop_d3)
                    {
730
731
                        const index_t clipboard_offset = clipboard_desc.Get1dIndex(
                            iloop_d0, iloop_d1, iloop_d2, iloop_d3 * DataPerRead);
732
733
734
735
736
737
738
739

                        const index_t dst_offset =
                            DstDesc{}.Get1dIndex(iloop_d0 * thread_per_d0,
                                                 iloop_d1 * thread_per_d1,
                                                 iloop_d2 * thread_per_d2,
                                                 iloop_d3 * thread_per_d3 * DataPerRead);

                        *(reinterpret_cast<vector_t*>(&p_dst[dst_offset + mDstMyThreadOffset])) =
740
                            *(reinterpret_cast<const vector_t*>(&p_clipboard[clipboard_offset]));
Chao Liu's avatar
Chao Liu committed
741
742
743
744
745
746
                    }
                }
            }
        }
    }
};
747
748
749
750
751
752

template <index_t BlockSize,
          class Float,
          class SrcDesc,
          class DstDesc,
          class SrcOpLengths,
753
          class MapDst2Src>
754
755
756
757
758
759
760
struct Blockwise4dTensorCopyReorder1
{
    __device__ void Run(const Float* __restrict__ p_src, Float* __restrict__ p_dst) const
    {
        auto f_copy = [](const Float& src, Float& dst) { dst = src; };

        blockwise_4d_tensor_pointwise_operation_binary_reorder_by_get_dst_from_src<BlockSize>(
761
            SrcDesc{}, p_src, DstDesc{}, p_dst, SrcOpLengths{}, MapDst2Src{}, f_copy);
762
763
764
765
766
767
768
769
770
    }
};

template <index_t BlockSize,
          class Float,
          class SrcDesc,
          class DstDesc,
          class SrcLengths,
          class SrcSubLengths,
771
772
773
774
775
          class SrcClusterLengths,
          class MapDst2Src,
          class MapThreadCluster2SrcCluster,
          index_t SrcDataPerRead,
          index_t DstDataPerWrite>
776
777
struct Blockwise4dTensorCopyReorder3
{
778
779
    static constexpr index_t nDim = SrcLengths::GetSize();

780
781
782
783
784
    index_t mSrcMyThreadOffset;
    index_t mDstMyThreadOffset;

    __device__ Blockwise4dTensorCopyReorder3()
    {
785
786
        constexpr auto src_desc = SrcDesc{};
        constexpr auto dst_desc = DstDesc{};
787

788
        constexpr auto src_lengths = SrcLengths{};
789

790
        constexpr auto map_dst2src = MapDst2Src{};
791

792
793
        constexpr auto src_sub_lengths = SrcSubLengths{};
        constexpr auto dst_sub_lengths = src_sub_lengths.ReorderGivenNew2Old(map_dst2src);
794

795
        constexpr auto map_thread_cluster_2_src_cluster = MapThreadCluster2SrcCluster{};
796

797
798
799
        constexpr auto src_cluster_lengths = SrcClusterLengths{};
        constexpr auto thread_cluster_lengths =
            src_cluster_lengths.ReorderGivenNew2Old(map_thread_cluster_2_src_cluster);
800

801
802
803
804
        constexpr auto thread_cluster_desc = make_ConstantTensorDescriptor(thread_cluster_lengths);

        // sanity check: data type
        static_assert(is_same<Float, float>::value, "wrong! only support float for now!\n");
805

806
807
808
809
810
811
812
813
814
        // sanity check: nDim
        static_assert(SrcDesc::GetDimension() == nDim && DstDesc::GetDimension() == nDim &&
                          SrcLengths::GetSize() == nDim && SrcSubLengths::GetSize() == nDim &&
                          SrcClusterLengths::GetSize() == nDim && MapDst2Src::GetSize() == nDim &&
                          MapThreadCluster2SrcCluster::GetSize() == nDim,
                      "wrong! nDim is not consistent\n");

        // sanity check: BlockSize
        constexpr index_t num_active_thread = thread_cluster_desc.GetElementSize();
815
816
817
818

        static_assert(BlockSize >= num_active_thread,
                      "wrong! BlockSize is not big enough for ThreadPerDims!");

819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
        // sanity check: work division
        static_for<0, nDim, 1>{}([](auto IDim) {
            constexpr auto I                  = decltype(IDim){};
            constexpr index_t src_len         = src_lengths.Get(I);
            constexpr index_t src_sub_len     = src_sub_lengths.Get(I);
            constexpr index_t src_cluster_len = src_cluster_lengths.Get(I);
            static_assert(src_len % (src_sub_len * src_cluster_len) == 0,
                          "wrong! cannot evenly divide Src tensor lengths");
        });

        // sanity check: src read
        static_assert(SrcDataPerRead == 1 || SrcDataPerRead == 2 || SrcDataPerRead == 4,
                      "wrong! only support SrcDataPerRead == 1, 2 or 4!\n");

        static_assert(SrcDataPerRead == 1 || src_desc.GetStride(Number<nDim - 1>{}) == 1,
                      "wrong! only support src.stride(nDim-1) == 1 if SrcDataPerRead > 1!\n");

        static_assert(src_sub_lengths.Get(Number<nDim - 1>{}) % SrcDataPerRead == 0,
                      "wrong! src_sub_lengths[nDim-1] % SrcDataPerRead != 0\n");

        static_assert(src_desc.GetStride(Number<nDim - 2>{}) % SrcDataPerRead == 0,
                      "wrong! should satisfy src_desc.stride(nDim-2) % SrcDataPerRead == 0, to "
                      "keep alignment");

        // sanity check: dst write
        static_assert(DstDataPerWrite == 1 || DstDataPerWrite == 2 || DstDataPerWrite == 4,
                      "wrong! only support DstDataPerWrite == 1, 2 or 4!\n");

        static_assert(DstDataPerWrite == 1 || dst_desc.GetStride(Number<nDim - 1>{}) == 1,
                      "wrong! only support dst.stride(nDim-1) == 1 if DstDataPerWrite > 1!\n");

        static_assert(dst_sub_lengths.Get(Number<nDim - 1>{}) % DstDataPerWrite == 0,
                      "wrong! dst_sub_lengths[nDim-1] % DstDataPerWrite != 0\n");

        static_assert(dst_desc.GetStride(Number<nDim - 2>{}) % DstDataPerWrite == 0,
                      "wrong! should satisfy dst_desc.stride(nDim-2) % DstDataPerWrite == 0, to "
                      "keep alignment");

        // start dividing work
858
859
860
861
862
863
864
865
        if(BlockSize > num_active_thread)
        {
            if(get_thread_local_1d_id() >= num_active_thread)
            {
                return;
            }
        }

866
        const auto thread_multi_id = thread_cluster_desc.GetMultiIndex(get_thread_local_1d_id());
867

868
869
870
871
872
873
874
875
876
877
878
879
        // compiler: thread_multi_id, src_data_multi_id, dst_data_multi_id, will use separate
        // regsiters, or only one copy???
        auto src_data_multi_id =
            reorder_array_given_old2new(thread_multi_id, map_thread_cluster_2_src_cluster);

        static_for<0, nDim, 1>{}([&](auto IDim) {
            constexpr auto I    = decltype(IDim){};
            constexpr index_t i = I.Get();
            // compiler: will it really compute index here, or be associated with Get1dIndex and
            // optimized away???
            src_data_multi_id[i] *= src_sub_lengths.Get(I);
        });
880

881
882
883
        // compiler: will it really compute index here, or be associated with Get1dIndex and
        // optimized away???
        const auto dst_data_multi_id = reorder_array_given_new2old(src_data_multi_id, map_dst2src);
884

885
886
        mSrcMyThreadOffset = src_desc.Get1dIndex(src_data_multi_id);
        mDstMyThreadOffset = dst_desc.Get1dIndex(dst_data_multi_id);
887

888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
#if 0
        if(get_block_1d_id() == 0)
        {
            printf("tid %5u, "
                   "thread_multi_id %5u %5u %5u %5u, "
                   "src_data_multi_id %5u %5u %5u %5u, "
                   "dst_data_multi_id %5u %5u %5u %5u, "
                   "mSrcMyThreadOffset %u, mDstMyThreadOffset %u\n",
                   get_thread_local_1d_id(),
                   thread_multi_id[0],
                   thread_multi_id[1],
                   thread_multi_id[2],
                   thread_multi_id[3],
                   src_data_multi_id[0],
                   src_data_multi_id[1],
                   src_data_multi_id[2],
                   src_data_multi_id[3],
                   dst_data_multi_id[0],
                   dst_data_multi_id[1],
                   dst_data_multi_id[2],
                   dst_data_multi_id[3],
                   mSrcMyThreadOffset,
                   mDstMyThreadOffset);
        }
#endif
913
914
915
916
    }

    __device__ static constexpr index_t GetRegisterClipboardSize()
    {
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
        constexpr auto thread_sub_tensor_lengths = SrcSubLengths{};

        constexpr auto src_data_per_cluster_per_dims = transform_sequences(
            mod_conv::multiplies<index_t>{}, thread_sub_tensor_lengths, SrcClusterLengths{});

        constexpr auto cluster_per_dims =
            transform_sequences(mod_conv::integer_divide_ceiler<index_t>{},
                                SrcLengths{},
                                src_data_per_cluster_per_dims);

        constexpr auto thread_tensor_lengths = transform_sequences(
            mod_conv::multiplies<index_t>{}, thread_sub_tensor_lengths, cluster_per_dims);

        constexpr auto thread_tensor_desc = make_ConstantTensorDescriptor(thread_tensor_lengths);

        return thread_tensor_desc.GetElementSpace();
933
934
935
936
937
    }

    __device__ void RunLoadRegisterClipboard(const Float* __restrict__ p_src,
                                             Float* __restrict__ p_clipboard) const
    {
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};

        constexpr auto thread_sub_tensor_lengths = SrcSubLengths{};

        constexpr auto src_data_per_cluster_per_dims = transform_sequences(
            mod_conv::multiplies<index_t>{}, thread_sub_tensor_lengths, SrcClusterLengths{});

        constexpr auto cluster_per_dims =
            transform_sequences(mod_conv::integer_divide_ceiler<index_t>{},
                                SrcLengths{},
                                src_data_per_cluster_per_dims);

        constexpr auto thread_tensor_lengths = transform_sequences(
            mod_conv::multiplies<index_t>{}, thread_sub_tensor_lengths, cluster_per_dims);

        constexpr auto thread_tensor_desc = make_ConstantTensorDescriptor(thread_tensor_lengths);

        constexpr auto thread_sub_tensor_desc =
            make_ConstantTensorDescriptor(SrcClusterLengths{}, thread_tensor_desc.GetStrides());

961
#if 1
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
        for(index_t icluster_d0 = 0; icluster_d0 < cluster_per_dims.Get(I0); ++icluster_d0)
        {
            for(index_t icluster_d1 = 0; icluster_d1 < cluster_per_dims.Get(I1); ++icluster_d1)
            {
                for(index_t icluster_d2 = 0; icluster_d2 < cluster_per_dims.Get(I2); ++icluster_d2)
                {
                    for(index_t icluster_d3 = 0; icluster_d3 < cluster_per_dims.Get(I3);
                        ++icluster_d3)
                    {
                        const index_t src_offset = SrcDesc{}.Get1dIndex(
                            icluster_d0 * src_data_per_cluster_per_dims.Get(I0),
                            icluster_d1 * src_data_per_cluster_per_dims.Get(I1),
                            icluster_d2 * src_data_per_cluster_per_dims.Get(I2),
                            icluster_d3 * src_data_per_cluster_per_dims.Get(I3));

                        const index_t clipboard_offset = thread_tensor_desc.Get1dIndex(
                            icluster_d0 * thread_sub_tensor_lengths.Get(I0),
                            icluster_d1 * thread_sub_tensor_lengths.Get(I1),
                            icluster_d2 * thread_sub_tensor_lengths.Get(I2),
                            icluster_d3 * thread_sub_tensor_lengths.Get(I3));

983
984
985
986
987
988
                        threadwise_nd_tensor_copy(SrcDesc{},
                                                  p_src + src_offset + mSrcMyThreadOffset,
                                                  thread_tensor_desc,
                                                  p_clipboard + clipboard_offset,
                                                  thread_sub_tensor_lengths,
                                                  Number<SrcDataPerRead>{});
989
990
991
992
                    }
                }
            }
        }
993
994
995
996
997
#else
        static_ford<decltype(cluster_per_dims)>{}([=](auto cluster_ids) {

        });
#endif
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014

#if 0
        if(get_block_1d_id() == 0)
        {
            printf("tid %5u, "
                   "data: %f %f %f %f %f %f %f %f\n",
                   get_thread_local_1d_id(),
                   p_clipboard[0],
                   p_clipboard[1],
                   p_clipboard[2],
                   p_clipboard[3],
                   p_clipboard[4],
                   p_clipboard[5],
                   p_clipboard[6],
                   p_clipboard[7]);
        }
#endif
1015
1016
1017
1018
1019
    }

    __device__ void RunStoreRegisterClipboard(const Float* __restrict__ p_clipboard,
                                              Float* __restrict__ p_dst) const
    {
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
        constexpr auto I0 = Number<0>{};
        constexpr auto I1 = Number<1>{};
        constexpr auto I2 = Number<2>{};
        constexpr auto I3 = Number<3>{};

        constexpr auto thread_sub_tensor_lengths = SrcSubLengths{};

        constexpr auto src_data_per_cluster_per_dims = transform_sequences(
            mod_conv::multiplies<index_t>{}, thread_sub_tensor_lengths, SrcClusterLengths{});

        constexpr auto cluster_per_dims =
            transform_sequences(mod_conv::integer_divide_ceiler<index_t>{},
                                SrcLengths{},
                                src_data_per_cluster_per_dims);

        constexpr auto thread_tensor_lengths = transform_sequences(
            mod_conv::multiplies<index_t>{}, thread_sub_tensor_lengths, cluster_per_dims);

        constexpr auto thread_tensor_desc = make_ConstantTensorDescriptor(thread_tensor_lengths);

        constexpr auto thread_sub_tensor_desc =
            make_ConstantTensorDescriptor(SrcClusterLengths{}, thread_tensor_desc.GetStrides());

        for(index_t icluster_d0 = 0; icluster_d0 < cluster_per_dims.Get(I0); ++icluster_d0)
        {
            for(index_t icluster_d1 = 0; icluster_d1 < cluster_per_dims.Get(I1); ++icluster_d1)
            {
                for(index_t icluster_d2 = 0; icluster_d2 < cluster_per_dims.Get(I2); ++icluster_d2)
                {
                    for(index_t icluster_d3 = 0; icluster_d3 < cluster_per_dims.Get(I3);
                        ++icluster_d3)
                    {
                        const index_t clipboard_offset = thread_tensor_desc.Get1dIndex(
                            icluster_d0 * thread_sub_tensor_lengths.Get(I0),
                            icluster_d1 * thread_sub_tensor_lengths.Get(I1),
                            icluster_d2 * thread_sub_tensor_lengths.Get(I2),
                            icluster_d3 * thread_sub_tensor_lengths.Get(I3));

                        const auto dst_multi_id = reorder_array_given_new2old(
                            Array<index_t, nDim>{
                                icluster_d0 * src_data_per_cluster_per_dims.Get(I0),
                                icluster_d1 * src_data_per_cluster_per_dims.Get(I1),
                                icluster_d2 * src_data_per_cluster_per_dims.Get(I2),
                                icluster_d3 * src_data_per_cluster_per_dims.Get(I3)},
                            MapDst2Src{});

                        const index_t dst_offset = DstDesc{}.Get1dIndex(dst_multi_id);

#if 0
                        if(get_block_1d_id() == 0)
                        {
                            printf("tid %5u, "
                                    "clipboard_offsetm %5u, dst_offset %5u\n",
                            get_thread_local_1d_id(),
                            clipboard_offset,
                            dst_offset);
                        }
#endif

1079
                        threadwise_4d_tensor_copy_reorder_given_dst2src_v2(
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
                            thread_tensor_desc,
                            p_clipboard + clipboard_offset,
                            DstDesc{},
                            p_dst + dst_offset + mDstMyThreadOffset,
                            thread_sub_tensor_lengths,
                            MapDst2Src{});
                    }
                }
            }
        }
    }

    __device__ void Run(const Float* __restrict__ p_src, Float* __restrict__ p_dst) const
    {
        Float p_clipboard[GetRegisterClipboardSize()];

        RunLoadRegisterClipboard(p_src, p_clipboard);
        RunStoreRegisterClipboard(p_clipboard, p_dst);
1098
1099
    }
};