gemm.cuh 11 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
#pragma once

Chao Liu's avatar
Chao Liu committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template <class Float, class SrcMatrix, class DstMatrix, unsigned NRow, unsigned NCol>
__device__ void
threadwise_matrix_copy(SrcMatrix, Float* const p_src, DstMatrix, Float* p_dst, Sequence<NRow, NCol>)
{
    const auto src_mtx = SrcMatrix{}; // constexpr doesn't compile
    const auto dst_mtx = DstMatrix{}; // constexpr doesn't compile

    for(unsigned i = 0; i < NRow; ++i)
    {
        for(unsigned j = 0; j < NCol; ++j)
        {
            const unsigned src_index = src_mtx.Get1dIndex(i, j);
            const unsigned dst_index = dst_mtx.Get1dIndex(i, j);

            p_dst[dst_index] = p_src[src_index];
        }
    }
}

template <class MatrixA,
          class MatrixB,
          class MatrixC,
Chao Liu's avatar
Chao Liu committed
25
          bool TransA,
Chao Liu's avatar
Chao Liu committed
26
          bool TransB,
Chao Liu's avatar
Chao Liu committed
27
28
          bool TransC,
          class FloatA,
Chao Liu's avatar
Chao Liu committed
29
30
31
          class FloatB,
          class FloatC,
          class Accumulator>
Chao Liu's avatar
Chao Liu committed
32
__device__ void threadwise_gemm(MatrixA,
Chao Liu's avatar
Chao Liu committed
33
34
                                Constant<bool, TransA>,
                                FloatA* const p_a_thread,
Chao Liu's avatar
Chao Liu committed
35
                                MatrixB,
Chao Liu's avatar
Chao Liu committed
36
37
                                Constant<bool, TransB>,
                                FloatB* const p_b_thread,
Chao Liu's avatar
Chao Liu committed
38
                                MatrixC,
Chao Liu's avatar
Chao Liu committed
39
40
                                Constant<bool, TransC>,
                                FloatC* p_c_thread,
Chao Liu's avatar
Chao Liu committed
41
                                Accumulator f_accum)
Chao Liu's avatar
Chao Liu committed
42
{
Chao Liu's avatar
Chao Liu committed
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
    if(TransA && (!TransB) && (!TransC))
    {
        const auto a_mtx = MatrixA{}; // constexpr doesn't compile
        const auto b_mtx = MatrixB{}; // constexpr doesn't compile
        const auto c_mtx = MatrixC{}; // constexpr doesn't compile

        constexpr unsigned M = c_mtx.NRow();
        constexpr unsigned N = c_mtx.NCol();
        constexpr unsigned K = a_mtx.NRow(); // A is transposed

        for(unsigned i = 0; i < M; ++i)
        {
            for(unsigned j = 0; j < N; ++j)
            {
                for(unsigned k = 0; k < K; ++k)
                {
                    const unsigned aindex = a_mtx.Get1dIndex(k, i); // A is transposed
                    const unsigned bindex = b_mtx.Get1dIndex(k, j);
                    const unsigned cindex = c_mtx.Get1dIndex(i, j);

                    f_accum(p_c_thread[cindex], p_a_thread[aindex] * p_b_thread[bindex]);
                }
            }
        }
    }
    else
    {
        // not implemented
        assert(false);
    }
Chao Liu's avatar
Chao Liu committed
73
74
75
76
77
}

template <unsigned BlockSize,
          class BlockMatrixA,
          class BlockMatrixB,
Chao Liu's avatar
Chao Liu committed
78
          class ThreadMatrixC,
Chao Liu's avatar
Chao Liu committed
79
80
          bool TransA,
          bool TransB,
Chao Liu's avatar
Chao Liu committed
81
          bool TransC,
Chao Liu's avatar
Chao Liu committed
82
83
          unsigned BlockMatrixStrideA,
          unsigned BlockMatrixStrideB,
Chao Liu's avatar
Chao Liu committed
84
85
          unsigned ThreadMatrixStrideC,
          unsigned BatchSize,
Chao Liu's avatar
Chao Liu committed
86
          unsigned BatchPerThread,
Chao Liu's avatar
Chao Liu committed
87
88
          unsigned KPerThreadLoop,
          bool DistributeThreadAlongColumnFirst>
Chao Liu's avatar
Chao Liu committed
89
90
struct blockwise_1d_strided_batched_gemm_block_a_block_b_thread_c
{
Chao Liu's avatar
Chao Liu committed
91
92
93
    unsigned mMyThreadOffsetA = 0;
    unsigned mMyThreadOffsetB = 0;

Chao Liu's avatar
Chao Liu committed
94
95
96
    struct MatrixIndex
    {
        unsigned batch_begin;
Chao Liu's avatar
Chao Liu committed
97
98
        unsigned row_begin;
        unsigned col_begin;
Chao Liu's avatar
Chao Liu committed
99
100
101
102
    };

    __device__ blockwise_1d_strided_batched_gemm_block_a_block_b_thread_c()
    {
Chao Liu's avatar
Chao Liu committed
103
104
        const auto a_block_mtx = BlockMatrixA{}; // constexpr doesn't compile
        const auto b_block_mtx = BlockMatrixB{}; // constexpr doesn't compile
Chao Liu's avatar
Chao Liu committed
105

Chao Liu's avatar
Chao Liu committed
106
        const auto c_thread_mtx_index = CalculateThreadMatrixCIndex(get_thread_local_1d_id());
Chao Liu's avatar
Chao Liu committed
107

Chao Liu's avatar
Chao Liu committed
108
109
110
        mMyThreadOffsetA = c_thread_mtx_index.batch_begin * a_block_mtx.GetElementSpace() +
                           ((!TransA) ? a_block_mtx.Get1dIndex(c_thread_mtx_index.row_begin, 0)
                                      : a_block_mtx.Get1dIndex(0, c_thread_mtx_index.row_begin));
Chao Liu's avatar
Chao Liu committed
111

Chao Liu's avatar
Chao Liu committed
112
113
114
115
        mMyThreadOffsetB = c_thread_mtx_index.batch_begin * b_block_mtx.GetElementSpace() +
                           ((!TransB) ? b_block_mtx.Get1dIndex(0, c_thread_mtx_index.col_begin)
                                      : b_block_mtx.Get1dIndex(c_thread_mtx_index.col_begin, 0));
    }
Chao Liu's avatar
Chao Liu committed
116

Chao Liu's avatar
Chao Liu committed
117
118
    __device__ MatrixIndex CalculateThreadMatrixCIndex(unsigned thread_id) const
    {
Chao Liu's avatar
Chao Liu committed
119

Chao Liu's avatar
Chao Liu committed
120
121
122
123
        if(TransA && (!TransB) && (!TransC))
        {
            const auto a_block_mtx = BlockMatrixA{}; // constexpr doesn't compile
            const auto b_block_mtx = BlockMatrixB{}; // constexpr doesn't compile
Chao Liu's avatar
Chao Liu committed
124

Chao Liu's avatar
Chao Liu committed
125
126
            static_assert(a_block_mtx.NRow() == b_block_mtx.NRow(),
                          "wrong! k dimension not consistent!");
Chao Liu's avatar
Chao Liu committed
127

Chao Liu's avatar
Chao Liu committed
128
129
            constexpr unsigned MPerBlock = a_block_mtx.NCol();
            constexpr unsigned NPerBlock = b_block_mtx.NCol();
Chao Liu's avatar
Chao Liu committed
130

Chao Liu's avatar
Chao Liu committed
131
            const auto c_thread_mtx = ThreadMatrixC{}; // constexpr doesn't compile
Chao Liu's avatar
Chao Liu committed
132

Chao Liu's avatar
Chao Liu committed
133
134
135
            // divide thread work
            constexpr unsigned MPerThread = c_thread_mtx.NRow();
            constexpr unsigned NPerThread = c_thread_mtx.NCol();
Chao Liu's avatar
Chao Liu committed
136

Chao Liu's avatar
Chao Liu committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
            static_assert(BatchSize % BatchPerThread == 0, "BatchSize % BatchPerThread != 0");
            static_assert(MPerBlock % MPerThread == 0, "MPerBlock % MPerThread != 0");
            static_assert(NPerBlock % NPerThread == 0, "NPerBlock % NPerThread != 0");

            constexpr unsigned BThreadWork = (BatchSize + BatchPerThread - 1) / BatchPerThread;
            constexpr unsigned MThreadWork = (MPerBlock + MPerThread - 1) / MPerThread;
            constexpr unsigned NThreadWork = (NPerBlock + NPerThread - 1) / NPerThread;

            static_assert(BlockSize == BThreadWork * MThreadWork * NThreadWork,
                          "wrong! wrong BlockSize");

            // printf("%u %u, %u %u\n", get_block_1d_id(), get_thread_local_1d_id(), MThreadWork,
            // NThreadWork);

            if(DistributeThreadAlongColumnFirst)
            {
                // num of operations can be reduced
                const unsigned b_work_id = thread_id / (MThreadWork * NThreadWork);
                unsigned itmp            = thread_id - b_work_id * (MThreadWork * NThreadWork);
                const unsigned m_work_id = itmp / NThreadWork;
                const unsigned n_work_id = itmp - m_work_id * NThreadWork;

                return MatrixIndex{
                    b_work_id * BatchPerThread, m_work_id * MPerThread, n_work_id * NPerThread};
            }
            else
            {
                // not implemented
                assert(false);
            }
        }
        else
        {
            // not implemented
            assert(false);
        }
Chao Liu's avatar
Chao Liu committed
173
174
    }

Chao Liu's avatar
Chao Liu committed
175
176
177
178
179
    template <class FloatA, class FloatB, class FloatC, class Accumulator>
    __device__ void run(FloatA* const p_a_block,
                        FloatB* const p_b_block,
                        FloatC* p_c_thread,
                        Accumulator f_accum) const
Chao Liu's avatar
Chao Liu committed
180
    {
Chao Liu's avatar
Chao Liu committed
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
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
273
274
        if(TransA && (!TransB) && (!TransC))
        {
            constexpr auto True  = Constant<bool, true>{};
            constexpr auto False = Constant<bool, false>{};

            const auto a_block_mtx  = BlockMatrixA{};  // constexpr doesn't compile
            const auto b_block_mtx  = BlockMatrixB{};  // constexpr doesn't compile
            const auto c_thread_mtx = ThreadMatrixC{}; // constexpr doesn't compile

            constexpr unsigned KPerBlock = a_block_mtx.NRow(); // A is transposed

            constexpr unsigned MPerThread = c_thread_mtx.NRow();
            constexpr unsigned NPerThread = c_thread_mtx.NCol();

            // a is transposed, b is not
            const auto a_thread_mtx = make_ConstantMatrixDescriptor(
                Number<KPerThreadLoop>{}, Number<MPerThread>{}); // constexpr doesn't compile

            const auto b_thread_mtx = make_ConstantMatrixDescriptor(
                Number<KPerThreadLoop>{}, Number<NPerThread>{}); // constexpr doesn't compile

            FloatA p_a_thread[a_thread_mtx.GetElementSpace()];
            FloatB p_b_thread[b_thread_mtx.GetElementSpace()];

            // loop over k
            for(unsigned k_begin = 0; k_begin < KPerBlock; k_begin += KPerThreadLoop)
            {
                // read first batch of a, b
                threadwise_matrix_copy(a_block_mtx,
                                       p_a_block + mMyThreadOffsetA +
                                           k_begin * a_block_mtx.RowStride(),
                                       a_thread_mtx,
                                       p_a_thread,
                                       a_thread_mtx.GetLengths());

                threadwise_matrix_copy(b_block_mtx,
                                       p_b_block + mMyThreadOffsetB +
                                           k_begin * b_block_mtx.RowStride(),
                                       b_thread_mtx,
                                       p_b_thread,
                                       b_thread_mtx.GetLengths());

                // loop over batch
                for(unsigned ib = 0; ib + 1 < BatchPerThread; ++ib)
                {
                    // do current batch of gemm
                    threadwise_gemm(a_thread_mtx,
                                    True,
                                    p_a_thread,
                                    b_thread_mtx,
                                    False,
                                    p_b_thread,
                                    c_thread_mtx,
                                    False,
                                    p_c_thread + ib * ThreadMatrixStrideC,
                                    f_accum);

                    // read next batch of a, b
                    if(BlockMatrixStrideA != 0)
                    {
                        threadwise_matrix_copy(a_block_mtx,
                                               p_a_block + mMyThreadOffsetA +
                                                   (ib + 1) * BlockMatrixStrideA +
                                                   +k_begin * a_block_mtx.RowStride(),
                                               a_thread_mtx,
                                               p_a_thread,
                                               a_thread_mtx.GetLengths());
                    }

                    if(BlockMatrixStrideB != 0)
                    {
                        threadwise_matrix_copy(b_block_mtx,
                                               p_b_block + mMyThreadOffsetB +
                                                   (ib + 1) * BlockMatrixStrideB +
                                                   k_begin * b_block_mtx.RowStride(),
                                               b_thread_mtx,
                                               p_b_thread,
                                               b_thread_mtx.GetLengths());
                    }
                }

                // do last batch of gemm
                threadwise_gemm(a_thread_mtx,
                                True,
                                p_a_thread,
                                b_thread_mtx,
                                False,
                                p_b_thread,
                                c_thread_mtx,
                                False,
                                p_c_thread + (BatchPerThread - 1) * ThreadMatrixStrideC,
                                f_accum);
            }
        }
Chao Liu's avatar
Chao Liu committed
275
    }
Chao Liu's avatar
Chao Liu committed
276
};