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

Chao Liu's avatar
Chao Liu committed
3
template <class Float, class SrcMatrix, class DstMatrix, index_t NRow, index_t NCol, index_t DataPerRead>
Chao Liu's avatar
Chao Liu committed
4
5
6
7
__device__ void threadwise_matrix_copy(SrcMatrix,
                                       const Float* __restrict__ p_src,
                                       DstMatrix,
                                       Float* __restrict__ p_dst,
Chao Liu's avatar
Chao Liu committed
8
9
                                       Sequence<NRow, NCol>,
                                       Number<DataPerRead>)
10
{
Chao Liu's avatar
Chao Liu committed
11
12
13
14
    static_assert(NCol % DataPerRead == 0, "wrong! should be NCol % == DataPerRead == 0");

    using vector_t = typename vector_type<Float, DataPerRead>::MemoryType;

Chao Liu's avatar
Chao Liu committed
15
16
    constexpr auto src_mtx = SrcMatrix{};
    constexpr auto dst_mtx = DstMatrix{};
17

Chao Liu's avatar
Chao Liu committed
18
    for(index_t i = 0; i < NRow; ++i)
19
    {
Chao Liu's avatar
Chao Liu committed
20
        for(index_t j = 0; j < NCol; j += DataPerRead)
21
        {
Chao Liu's avatar
Chao Liu committed
22
23
            const index_t src_index = src_mtx.Get1dIndex(i, j);
            const index_t dst_index = dst_mtx.Get1dIndex(i, j);
24

Chao Liu's avatar
Chao Liu committed
25
26
            *reinterpret_cast<vector_t*>(&p_dst[dst_index]) = 
                *reinterpret_cast<const vector_t*>(&p_src[src_index]);
27
28
29
30
31
32
33
34
35
36
37
38
        }
    }
}

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

Chao Liu's avatar
Chao Liu committed
56
57
58
        constexpr index_t M = c_mtx.NRow();
        constexpr index_t N = c_mtx.NCol();
        constexpr index_t K = a_mtx.NRow(); // A is transposed
59

Chao Liu's avatar
Chao Liu committed
60
        for(index_t k = 0; k < K; ++k)
61
        {
Jing Zhang's avatar
Jing Zhang committed
62
            for(index_t i = 0; i < M; i++)
63
            {
Jing Zhang's avatar
Jing Zhang committed
64
                for(index_t j = 0; j < N; j++)
65
                {
Chao Liu's avatar
Chao Liu committed
66
67
68
                    const index_t aindex = a_mtx.Get1dIndex(k, i); // A is transposed
                    const index_t bindex = b_mtx.Get1dIndex(k, j);
                    const index_t cindex = c_mtx.Get1dIndex(i, j);
69

Jing Zhang's avatar
Jing Zhang committed
70
                    p_c_thread[cindex] += p_a_thread[aindex] * p_b_thread[bindex];
71
72
73
74
75
76
77
78
79
80
                }
            }
        }
    }
    else
    {
        // not implemented
        assert(false);
    }
}