threadwise_gemm.hip.hpp 2.49 KB
Newer Older
1
2
3
#pragma once

template <class Float, class SrcMatrix, class DstMatrix, unsigned NRow, unsigned NCol>
Chao Liu's avatar
Chao Liu committed
4
5
6
7
8
__device__ void threadwise_matrix_copy(SrcMatrix,
                                       const Float* __restrict__ p_src,
                                       DstMatrix,
                                       Float* __restrict__ p_dst,
                                       Sequence<NRow, NCol>)
9
{
Chao Liu's avatar
Chao Liu committed
10
11
    constexpr auto src_mtx = SrcMatrix{};
    constexpr auto dst_mtx = DstMatrix{};
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

    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,
          bool TransA,
          bool TransB,
          bool TransC,
          class FloatA,
          class FloatB,
          class FloatC,
          class Accumulator>
__device__ void threadwise_gemm(MatrixA,
Chao Liu's avatar
Chao Liu committed
36
                                integral_constant<bool, TransA>,
Chao Liu's avatar
Chao Liu committed
37
                                const FloatA* __restrict__ p_a_thread,
38
                                MatrixB,
Chao Liu's avatar
Chao Liu committed
39
                                integral_constant<bool, TransB>,
Chao Liu's avatar
Chao Liu committed
40
                                const FloatB* __restrict__ p_b_thread,
41
                                MatrixC,
Chao Liu's avatar
Chao Liu committed
42
                                integral_constant<bool, TransC>,
Chao Liu's avatar
Chao Liu committed
43
                                FloatC* __restrict__ p_c_thread,
44
45
46
47
                                Accumulator f_accum)
{
    if(TransA && (!TransB) && (!TransC))
    {
Chao Liu's avatar
Chao Liu committed
48
49
50
        constexpr auto a_mtx = MatrixA{};
        constexpr auto b_mtx = MatrixB{};
        constexpr auto c_mtx = MatrixC{};
51
52
53
54
55

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

Chao Liu's avatar
Chao Liu committed
56
        for(unsigned k = 0; k < K; ++k)
57
        {
Chao Liu's avatar
Chao Liu committed
58
            for(unsigned i = 0; i < M; ++i)
59
            {
Chao Liu's avatar
Chao Liu committed
60
                for(unsigned j = 0; j < N; ++j)
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
                {
                    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);
    }
}