blockwise_tensor_op.cuh 16.7 KB
Newer Older
1
2
3
#pragma once
#include "constant_tensor_descriptor.cuh"

Chao Liu's avatar
Chao Liu committed
4
5
6
7
8
9
#define BLOCKWISE_TENSOR_OP_METHOD 12

#if BLOCKWISE_TENSOR_OP_METHOD == 11
template <class TFloat,
          class SrcDesc,
          class DstDesc,
Chao Liu's avatar
Chao Liu committed
10
11
12
13
          unsigned NBlockOpLen0,
          unsigned NBlockOpLen1,
          unsigned NBlockOpLen2,
          unsigned NBlockOpLen3,
Chao Liu's avatar
Chao Liu committed
14
15
          class F,
          unsigned BlockSize>
Chao Liu's avatar
Chao Liu committed
16
__device__ void blockwise_4d_tensor_op_binary(
Chao Liu's avatar
Chao Liu committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    SrcDesc, TFloat* const __restrict__ p_src, DstDesc, TFloat* __restrict__ p_dst, F f)
{
    constexpr auto I0 = Index<0>{};
    constexpr auto I1 = Index<1>{};
    constexpr auto I2 = Index<2>{};
    constexpr auto I3 = Index<3>{};

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

    static_assert(is_same<decltype(src_desc.GetLengths()), decltype(dst_desc.GetLengths())>::value);

    constexpr auto desc = make_ConstantTensorDescriptor(src_desc.GetLengths());

31
#if 0
Chao Liu's avatar
Chao Liu committed
32
33
    if(threadIdx.x == 0)
    {
Chao Liu's avatar
Chao Liu committed
34
35
        print_ConstantTensorDescriptor(src_desc, "blockwise_4d_tensor_op_binary: src_desc: ");
        print_ConstantTensorDescriptor(dst_desc, "blockwise_4d_tensor_op_binary: dst_desc: ");
Chao Liu's avatar
Chao Liu committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    }
#endif

    for(unsigned i = threadIdx.x; i < desc.GetElementSize(); i += BlockSize)
    {
        unsigned is = i;

        const unsigned did0 = is / desc.GetStride(I0);

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

        const unsigned did1 = is / desc.GetStride(I1);

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

        const unsigned did2 = is / desc.GetStride(I2);

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

        const unsigned did3 = is / desc.GetStride(I3);

        const unsigned sindex = src_desc.Get1dIndex(did0, did1, did2, did3);

        const unsigned dindex = dst_desc.Get1dIndex(did0, did1, did2, did3);

        f(p_src[sindex], p_dst[dindex]);
    }
}
#endif

#if BLOCKWISE_TENSOR_OP_METHOD == 12
67
68
69
template <class TFloat,
          class SrcDesc,
          class DstDesc,
Chao Liu's avatar
Chao Liu committed
70
71
72
73
          unsigned NBlockOpLen0,
          unsigned NBlockOpLen1,
          unsigned NBlockOpLen2,
          unsigned NBlockOpLen3,
74
75
          class F,
          unsigned BlockSize>
Chao Liu's avatar
Chao Liu committed
76
__device__ void blockwise_4d_tensor_op_binary(
77
78
79
80
81
82
83
84
85
86
87
88
    SrcDesc, TFloat* const __restrict__ p_src, DstDesc, TFloat* __restrict__ p_dst, F f)
{
    constexpr auto I0 = Index<0>{};
    constexpr auto I1 = Index<1>{};
    constexpr auto I2 = Index<2>{};
    constexpr auto I3 = Index<3>{};

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

    static_assert(is_same<decltype(src_desc.GetLengths()), decltype(dst_desc.GetLengths())>::value);

Chao Liu's avatar
Chao Liu committed
89
90
    constexpr auto desc = make_ConstantTensorDescriptor(src_desc.GetLengths());

91
92
93
#if 0
    if(threadIdx.x == 0)
    {
Chao Liu's avatar
Chao Liu committed
94
95
        print_ConstantTensorDescriptor(src_desc, "blockwise_4d_tensor_op_binary: src_desc: ");
        print_ConstantTensorDescriptor(dst_desc, "blockwise_4d_tensor_op_binary: dst_desc: ");
96
97
98
    }
#endif

Chao Liu's avatar
Chao Liu committed
99
100
    constexpr unsigned NLoop = desc.GetElementSize() / BlockSize;

Chao Liu's avatar
faster  
Chao Liu committed
101
    for(unsigned iloop = 0; iloop < NLoop; ++iloop)
Chao Liu's avatar
Chao Liu committed
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    {
        unsigned is = threadIdx.x + iloop * BlockSize;

        const unsigned did0 = is / desc.GetStride(I0);

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

        const unsigned did1 = is / desc.GetStride(I1);

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

        const unsigned did2 = is / desc.GetStride(I2);

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

        const unsigned did3 = is / desc.GetStride(I3);

        const unsigned sindex = src_desc.Get1dIndex(did0, did1, did2, did3);

        const unsigned dindex = dst_desc.Get1dIndex(did0, did1, did2, did3);

        f(p_src[sindex], p_dst[dindex]);
    }

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

    if(has_tail)
    {
        unsigned is = threadIdx.x + NLoop * BlockSize;

        if(is < desc.GetElementSize())
        {
            const unsigned did0 = is / desc.GetStride(I0);

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

            const unsigned did1 = is / desc.GetStride(I1);

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

            const unsigned did2 = is / desc.GetStride(I2);

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

            const unsigned did3 = is / desc.GetStride(I3);

            const unsigned sindex = src_desc.Get1dIndex(did0, did1, did2, did3);

            const unsigned dindex = dst_desc.Get1dIndex(did0, did1, did2, did3);

            f(p_src[sindex], p_dst[dindex]);
        }
    }
}
Chao Liu's avatar
Chao Liu committed
156
157
158

template <class TFloat,
          class DstDesc,
Chao Liu's avatar
Chao Liu committed
159
160
161
162
          unsigned NBlockOpLen0,
          unsigned NBlockOpLen1,
          unsigned NBlockOpLen2,
          unsigned NBlockOpLen3,
Chao Liu's avatar
Chao Liu committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
          class F,
          unsigned BlockSize>
__device__ void blockwise_4d_tensor_op_unary(DstDesc, TFloat* __restrict__ p_dst, F f)
{
    constexpr auto I0 = Index<0>{};
    constexpr auto I1 = Index<1>{};
    constexpr auto I2 = Index<2>{};
    constexpr auto I3 = Index<3>{};

    constexpr auto dst_desc = DstDesc{};

    constexpr auto desc = make_ConstantTensorDescriptor(dst_desc.GetLengths());

#if 0
    if(threadIdx.x == 0)
    {
        print_ConstantTensorDescriptor(dst_desc, "blockwise_4d_tensor_op_unary: dst_desc: ");
        print_ConstantTensorDescriptor(desc, "blockwise_4d_tensor_op_unary: desc: ");
    }
#endif

    constexpr unsigned NLoop = desc.GetElementSize() / BlockSize;

    for(unsigned iloop = 0; iloop < NLoop; ++iloop)
    {
        unsigned is = threadIdx.x + iloop * BlockSize;

        const unsigned did0 = is / desc.GetStride(I0);

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

        const unsigned did1 = is / desc.GetStride(I1);

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

        const unsigned did2 = is / desc.GetStride(I2);

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

        const unsigned did3 = is / desc.GetStride(I3);

        const unsigned dindex = dst_desc.Get1dIndex(did0, did1, did2, did3);

        f(p_dst[dindex]);
    }

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

    if(has_tail)
    {
        unsigned is = threadIdx.x + NLoop * BlockSize;

        if(is < desc.GetElementSize())
        {
            const unsigned did0 = is / desc.GetStride(I0);

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

            const unsigned did1 = is / desc.GetStride(I1);

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

            const unsigned did2 = is / desc.GetStride(I2);

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

            const unsigned did3 = is / desc.GetStride(I3);

            const unsigned dindex = dst_desc.Get1dIndex(did0, did1, did2, did3);

            f(p_dst[dindex]);
        }
    }
}
Chao Liu's avatar
Chao Liu committed
237
238
239
240
241
242
#endif

#if BLOCKWISE_TENSOR_OP_METHOD == 21
template <class TFloat,
          class SrcDesc,
          class DstDesc,
Chao Liu's avatar
Chao Liu committed
243
244
245
246
          unsigned NBlockOpLen0,
          unsigned NBlockOpLen1,
          unsigned NBlockOpLen2,
          unsigned NBlockOpLen3,
Chao Liu's avatar
Chao Liu committed
247
248
          class F,
          unsigned BlockSize>
Chao Liu's avatar
Chao Liu committed
249
__device__ void blockwise_4d_tensor_op_binary(
Chao Liu's avatar
Chao Liu committed
250
251
252
253
254
255
256
257
258
259
260
261
    SrcDesc, TFloat* const __restrict__ p_src, DstDesc, TFloat* __restrict__ p_dst, F f)
{
    constexpr auto I0 = Index<0>{};
    constexpr auto I1 = Index<1>{};
    constexpr auto I2 = Index<2>{};
    constexpr auto I3 = Index<3>{};

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

    static_assert(is_same<decltype(src_desc.GetLengths()), decltype(dst_desc.GetLengths())>::value);

Chao Liu's avatar
Chao Liu committed
262
263
264
265
    constexpr unsigned NBlockOpStride3 = 1;
    constexpr unsigned NBlockOpStride2 = NBlockOpLen3 * NBlockOpStride3;
    constexpr unsigned NBlockOpStride1 = NBlockOpLen2 * NBlockOpStride2;
    constexpr unsigned NBlockOpStride0 = NBlockOpLen1 * NBlockOpStride1;
266

Chao Liu's avatar
Chao Liu committed
267
    unsigned itmp = threadIdx.x;
268

Chao Liu's avatar
Chao Liu committed
269
    const unsigned did0_begin = itmp / NBlockOpStride0;
270

Chao Liu's avatar
Chao Liu committed
271
    itmp -= did0_begin * NBlockOpStride0;
272

Chao Liu's avatar
Chao Liu committed
273
    const unsigned did1_begin = itmp / NBlockOpStride1;
274

Chao Liu's avatar
Chao Liu committed
275
    itmp -= did1_begin * NBlockOpStride1;
276

Chao Liu's avatar
Chao Liu committed
277
    const unsigned did2_begin = itmp / NBlockOpStride2;
278

Chao Liu's avatar
Chao Liu committed
279
    itmp -= did2_begin * NBlockOpStride2;
280

Chao Liu's avatar
Chao Liu committed
281
    const unsigned did3_begin = itmp / NBlockOpStride3;
282

Chao Liu's avatar
Chao Liu committed
283
    for(unsigned did0 = did0_begin; did0 < src_desc.GetLength(I0); did0 += NBlockOpLen0)
284
    {
Chao Liu's avatar
Chao Liu committed
285
        for(unsigned did1 = did1_begin; did1 < src_desc.GetLength(I1); did1 += NBlockOpLen1)
286
        {
Chao Liu's avatar
Chao Liu committed
287
            for(unsigned did2 = did2_begin; did2 < src_desc.GetLength(I2); did2 += NBlockOpLen2)
288
            {
Chao Liu's avatar
Chao Liu committed
289
                for(unsigned did3 = did3_begin; did3 < src_desc.GetLength(I3); did3 += NBlockOpLen3)
290
                {
Chao Liu's avatar
Chao Liu committed
291
                    const unsigned sindex = src_desc.Get1dIndex(did0, did1, did2, did3);
292

Chao Liu's avatar
Chao Liu committed
293
                    const unsigned dindex = dst_desc.Get1dIndex(did0, did1, did2, did3);
294

Chao Liu's avatar
Chao Liu committed
295
                    f(p_src[sindex], p_dst[dindex]);
296
297
298
299
300
                }
            }
        }
    }
}
Chao Liu's avatar
Chao Liu committed
301
#endif
302

Chao Liu's avatar
Chao Liu committed
303
#if BLOCKWISE_TENSOR_OP_METHOD == 22
304
305
306
template <class TFloat,
          class SrcDesc,
          class DstDesc,
Chao Liu's avatar
Chao Liu committed
307
308
309
310
          unsigned NBlockOpLen0,
          unsigned NBlockOpLen1,
          unsigned NBlockOpLen2,
          unsigned NBlockOpLen3,
311
312
          class F,
          unsigned BlockSize>
Chao Liu's avatar
Chao Liu committed
313
__device__ void blockwise_4d_tensor_op_binary(
314
315
316
317
318
319
320
321
322
323
324
325
    SrcDesc, TFloat* const __restrict__ p_src, DstDesc, TFloat* __restrict__ p_dst, F f)
{
    constexpr auto I0 = Index<0>{};
    constexpr auto I1 = Index<1>{};
    constexpr auto I2 = Index<2>{};
    constexpr auto I3 = Index<3>{};

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

    static_assert(is_same<decltype(src_desc.GetLengths()), decltype(dst_desc.GetLengths())>::value);

Chao Liu's avatar
Chao Liu committed
326
327
328
329
    constexpr unsigned NBlockOpStride3 = 1;
    constexpr unsigned NBlockOpStride2 = NBlockOpLen3 * NBlockOpStride3;
    constexpr unsigned NBlockOpStride1 = NBlockOpLen2 * NBlockOpStride2;
    constexpr unsigned NBlockOpStride0 = NBlockOpLen1 * NBlockOpStride1;
Chao Liu's avatar
Chao Liu committed
330

Chao Liu's avatar
Chao Liu committed
331
332
    unsigned itmp = threadIdx.x;

Chao Liu's avatar
Chao Liu committed
333
    const unsigned did0_begin = itmp / NBlockOpStride0;
Chao Liu's avatar
Chao Liu committed
334

Chao Liu's avatar
Chao Liu committed
335
    itmp -= did0_begin * NBlockOpStride0;
Chao Liu's avatar
Chao Liu committed
336

Chao Liu's avatar
Chao Liu committed
337
    const unsigned did1_begin = itmp / NBlockOpStride1;
Chao Liu's avatar
Chao Liu committed
338

Chao Liu's avatar
Chao Liu committed
339
    itmp -= did1_begin * NBlockOpStride1;
Chao Liu's avatar
Chao Liu committed
340

Chao Liu's avatar
Chao Liu committed
341
    const unsigned did2_begin = itmp / NBlockOpStride2;
Chao Liu's avatar
Chao Liu committed
342

Chao Liu's avatar
Chao Liu committed
343
    itmp -= did2_begin * NBlockOpStride2;
Chao Liu's avatar
Chao Liu committed
344

Chao Liu's avatar
Chao Liu committed
345
    const unsigned did3_begin = itmp / NBlockOpStride3;
Chao Liu's avatar
Chao Liu committed
346
347
348
349

    unsigned sindex = src_desc.Get1dIndex(did0_begin, did1_begin, did2_begin, did3_begin);
    unsigned dindex = dst_desc.Get1dIndex(did0_begin, did1_begin, did2_begin, did3_begin);

Chao Liu's avatar
Chao Liu committed
350
    for(unsigned did0 = did0_begin; did0 < src_desc.GetLength(I0); did0 += NBlockOpLen0)
351
    {
Chao Liu's avatar
Chao Liu committed
352
353
354
        const unsigned sindex_save0 = sindex;
        const unsigned dindex_save0 = dindex;

Chao Liu's avatar
Chao Liu committed
355
        for(unsigned did1 = did1_begin; did1 < src_desc.GetLength(I1); did1 += NBlockOpLen1)
Chao Liu's avatar
Chao Liu committed
356
357
358
359
        {
            const unsigned sindex_save1 = sindex;
            const unsigned dindex_save1 = dindex;

Chao Liu's avatar
Chao Liu committed
360
            for(unsigned did2 = did2_begin; did2 < src_desc.GetLength(I2); did2 += NBlockOpLen2)
Chao Liu's avatar
Chao Liu committed
361
362
363
364
            {
                const unsigned sindex_save2 = sindex;
                const unsigned dindex_save2 = dindex;

Chao Liu's avatar
Chao Liu committed
365
                for(unsigned did3 = did3_begin; did3 < src_desc.GetLength(I3); did3 += NBlockOpLen3)
Chao Liu's avatar
Chao Liu committed
366
367
368
                {
                    f(p_src[sindex], p_dst[dindex]);

Chao Liu's avatar
Chao Liu committed
369
370
                    sindex += NBlockOpLen3 * src_desc.GetStride(I3);
                    dindex += NBlockOpLen3 * dst_desc.GetStride(I3);
Chao Liu's avatar
Chao Liu committed
371
372
                }

Chao Liu's avatar
Chao Liu committed
373
374
                sindex = sindex_save2 + NBlockOpLen2 * src_desc.GetStride(I2);
                dindex = dindex_save2 + NBlockOpLen2 * dst_desc.GetStride(I2);
Chao Liu's avatar
Chao Liu committed
375
376
            }

Chao Liu's avatar
Chao Liu committed
377
378
            sindex = sindex_save1 + NBlockOpLen1 * src_desc.GetStride(I1);
            dindex = dindex_save1 + NBlockOpLen1 * dst_desc.GetStride(I1);
Chao Liu's avatar
Chao Liu committed
379
380
        }

Chao Liu's avatar
Chao Liu committed
381
382
        sindex = sindex_save0 + NBlockOpLen0 * src_desc.GetStride(I0);
        dindex = dindex_save0 + NBlockOpLen0 * dst_desc.GetStride(I0);
383
    }
Chao Liu's avatar
Chao Liu committed
384
}
385
386
#endif

Chao Liu's avatar
Chao Liu committed
387
388
389
390
#if BLOCKWISE_TENSOR_OP_METHOD == 23
template <class TFloat,
          class SrcDesc,
          class DstDesc,
Chao Liu's avatar
Chao Liu committed
391
392
393
394
          unsigned NBlockOpLen0,
          unsigned NBlockOpLen1,
          unsigned NBlockOpLen2,
          unsigned NBlockOpLen3,
Chao Liu's avatar
Chao Liu committed
395
396
          class F,
          unsigned BlockSize>
Chao Liu's avatar
Chao Liu committed
397
__device__ void blockwise_4d_tensor_op_binary(
Chao Liu's avatar
Chao Liu committed
398
399
400
401
402
403
404
405
406
407
408
409
    SrcDesc, TFloat* const __restrict__ p_src, DstDesc, TFloat* __restrict__ p_dst, F f)
{
    constexpr auto I0 = Index<0>{};
    constexpr auto I1 = Index<1>{};
    constexpr auto I2 = Index<2>{};
    constexpr auto I3 = Index<3>{};

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

    static_assert(is_same<decltype(src_desc.GetLengths()), decltype(dst_desc.GetLengths())>::value);

Chao Liu's avatar
Chao Liu committed
410
411
412
413
    constexpr unsigned NBlockOpStride3 = 1;
    constexpr unsigned NBlockOpStride2 = NBlockOpLen3 * NBlockOpStride3;
    constexpr unsigned NBlockOpStride1 = NBlockOpLen2 * NBlockOpStride2;
    constexpr unsigned NBlockOpStride0 = NBlockOpLen1 * NBlockOpStride1;
Chao Liu's avatar
Chao Liu committed
414
415
416

    unsigned itmp = threadIdx.x;

Chao Liu's avatar
Chao Liu committed
417
    const unsigned did0_begin = itmp / NBlockOpStride0;
Chao Liu's avatar
Chao Liu committed
418

Chao Liu's avatar
Chao Liu committed
419
    itmp -= did0_begin * NBlockOpStride0;
Chao Liu's avatar
Chao Liu committed
420

Chao Liu's avatar
Chao Liu committed
421
    const unsigned did1_begin = itmp / NBlockOpStride1;
Chao Liu's avatar
Chao Liu committed
422

Chao Liu's avatar
Chao Liu committed
423
    itmp -= did1_begin * NBlockOpStride1;
Chao Liu's avatar
Chao Liu committed
424

Chao Liu's avatar
Chao Liu committed
425
    const unsigned did2_begin = itmp / NBlockOpStride2;
Chao Liu's avatar
Chao Liu committed
426

Chao Liu's avatar
Chao Liu committed
427
    itmp -= did2_begin * NBlockOpStride2;
428

Chao Liu's avatar
Chao Liu committed
429
    const unsigned did3_begin = itmp / NBlockOpStride3;
Chao Liu's avatar
Chao Liu committed
430
431
432
433

    unsigned sindex = src_desc.Get1dIndex(did0_begin, did1_begin, did2_begin, did3_begin);
    unsigned dindex = dst_desc.Get1dIndex(did0_begin, did1_begin, did2_begin, did3_begin);

Chao Liu's avatar
Chao Liu committed
434
    for(unsigned did0 = did0_begin; did0 < src_desc.GetLength(I0); did0 += NBlockOpLen0)
435
    {
Chao Liu's avatar
Chao Liu committed
436
        unsigned i1 = 0;
Chao Liu's avatar
Chao Liu committed
437
        for(unsigned did1 = did1_begin; did1 < src_desc.GetLength(I1); did1 += NBlockOpLen1)
Chao Liu's avatar
Chao Liu committed
438
439
        {
            unsigned i2 = 0;
Chao Liu's avatar
Chao Liu committed
440
            for(unsigned did2 = did2_begin; did2 < src_desc.GetLength(I2); did2 += NBlockOpLen2)
Chao Liu's avatar
Chao Liu committed
441
442
            {
                unsigned i3 = 0;
Chao Liu's avatar
Chao Liu committed
443
                for(unsigned did3 = did3_begin; did3 < src_desc.GetLength(I3); did3 += NBlockOpLen3)
Chao Liu's avatar
Chao Liu committed
444
445
                {
                    f(p_src[sindex], p_dst[dindex]);
446

Chao Liu's avatar
Chao Liu committed
447
448
                    sindex += NBlockOpLen3 * src_desc.GetStride(I3);
                    dindex += NBlockOpLen3 * dst_desc.GetStride(I3);
449

Chao Liu's avatar
Chao Liu committed
450
451
                    ++i3;
                }
452

Chao Liu's avatar
Chao Liu committed
453
454
455
456
                sindex += NBlockOpLen2 * src_desc.GetStride(I2) -
                          i3 * NBlockOpLen3 * src_desc.GetStride(I3);
                dindex += NBlockOpLen2 * dst_desc.GetStride(I2) -
                          i3 * NBlockOpLen3 * dst_desc.GetStride(I3);
457

Chao Liu's avatar
Chao Liu committed
458
459
                ++i2;
            }
460

Chao Liu's avatar
Chao Liu committed
461
462
463
464
            sindex +=
                NBlockOpLen1 * src_desc.GetStride(I1) - i2 * NBlockOpLen2 * src_desc.GetStride(I2);
            dindex +=
                NBlockOpLen1 * dst_desc.GetStride(I1) - i2 * NBlockOpLen2 * dst_desc.GetStride(I2);
465

Chao Liu's avatar
Chao Liu committed
466
467
            ++i1;
        }
468

Chao Liu's avatar
Chao Liu committed
469
470
471
472
        sindex +=
            NBlockOpLen0 * src_desc.GetStride(I0) - i1 * NBlockOpLen1 * src_desc.GetStride(I1);
        dindex +=
            NBlockOpLen0 * dst_desc.GetStride(I0) - i1 * NBlockOpLen1 * dst_desc.GetStride(I1);
Chao Liu's avatar
Chao Liu committed
473
474
475
    }
}
#endif
476

Chao Liu's avatar
Chao Liu committed
477
478
479
480
#if BLOCKWISE_TENSOR_OP_METHOD == 31
template <class TFloat,
          class SrcDesc,
          class DstDesc,
Chao Liu's avatar
Chao Liu committed
481
482
483
484
          unsigned NBlockOpLen0,
          unsigned NBlockOpLen1,
          unsigned NBlockOpLen2,
          unsigned NBlockOpLen3,
Chao Liu's avatar
Chao Liu committed
485
486
          class F,
          unsigned BlockSize>
Chao Liu's avatar
Chao Liu committed
487
__device__ void blockwise_4d_tensor_op_binary(
Chao Liu's avatar
Chao Liu committed
488
489
490
491
492
493
    SrcDesc, TFloat* const __restrict__ p_src, DstDesc, TFloat* __restrict__ p_dst, F f)
{
    constexpr auto I0 = Index<0>{};
    constexpr auto I1 = Index<1>{};
    constexpr auto I2 = Index<2>{};
    constexpr auto I3 = Index<3>{};
494

Chao Liu's avatar
Chao Liu committed
495
496
    constexpr auto src_desc = SrcDesc{};
    constexpr auto dst_desc = DstDesc{};
497

Chao Liu's avatar
Chao Liu committed
498
499
    static_assert(is_same<decltype(src_desc.GetLengths()), decltype(dst_desc.GetLengths())>::value);

Chao Liu's avatar
Chao Liu committed
500
501
502
503
    constexpr unsigned NBlockOpStride3 = 1;
    constexpr unsigned NBlockOpStride2 = NBlockOpLen3 * NBlockOpStride3;
    constexpr unsigned NBlockOpStride1 = NBlockOpLen2 * NBlockOpStride2;
    constexpr unsigned NBlockOpStride0 = NBlockOpLen1 * NBlockOpStride1;
Chao Liu's avatar
Chao Liu committed
504
505
506

    unsigned itmp = threadIdx.x;

Chao Liu's avatar
Chao Liu committed
507
    const unsigned did0_begin = itmp / NBlockOpStride0;
Chao Liu's avatar
Chao Liu committed
508

Chao Liu's avatar
Chao Liu committed
509
    itmp -= did0_begin * NBlockOpStride0;
Chao Liu's avatar
Chao Liu committed
510

Chao Liu's avatar
Chao Liu committed
511
    const unsigned did1_begin = itmp / NBlockOpStride1;
Chao Liu's avatar
Chao Liu committed
512

Chao Liu's avatar
Chao Liu committed
513
    itmp -= did1_begin * NBlockOpStride1;
Chao Liu's avatar
Chao Liu committed
514

Chao Liu's avatar
Chao Liu committed
515
    const unsigned did2_begin = itmp / NBlockOpStride2;
Chao Liu's avatar
Chao Liu committed
516

Chao Liu's avatar
Chao Liu committed
517
    itmp -= did2_begin * NBlockOpStride2;
Chao Liu's avatar
Chao Liu committed
518

Chao Liu's avatar
Chao Liu committed
519
    const unsigned did3_begin = itmp / NBlockOpStride3;
Chao Liu's avatar
Chao Liu committed
520

Chao Liu's avatar
Chao Liu committed
521
    for(unsigned did0 = did0_begin; did0 < src_desc.GetLength(I0); did0 += NBlockOpLen0)
Chao Liu's avatar
Chao Liu committed
522
    {
Chao Liu's avatar
Chao Liu committed
523
        for(unsigned did1 = did1_begin; did1 < src_desc.GetLength(I1); did1 += NBlockOpLen1)
Chao Liu's avatar
Chao Liu committed
524
        {
Chao Liu's avatar
Chao Liu committed
525
            for(unsigned did2 = did2_begin; did2 < src_desc.GetLength(I2); did2 += NBlockOpLen2)
Chao Liu's avatar
Chao Liu committed
526
            {
Chao Liu's avatar
Chao Liu committed
527
                for(unsigned did3 = did3_begin; did3 < src_desc.GetLength(I3); did3 += NBlockOpLen3)
Chao Liu's avatar
Chao Liu committed
528
529
530
531
532
533
534
535
536
                {
                    const unsigned sindex = src_desc.Get1dIndex(did0, did1, did2, did3);

                    const unsigned dindex = dst_desc.Get1dIndex(did0, did1, did2, did3);

                    f(p_src[sindex], p_dst[dindex]);
                }
            }
        }
537
538
539
    }
}
#endif