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

Chao Liu's avatar
tidy yp  
Chao Liu committed
3
4
5
6
7
8
template <class Float,
          class SrcMatrix,
          class DstMatrix,
          index_t NRow,
          index_t NCol,
          index_t DataPerRead>
Chao Liu's avatar
Chao Liu committed
9
10
11
12
__device__ void threadwise_matrix_copy(SrcMatrix,
                                       const Float* __restrict__ p_src,
                                       DstMatrix,
                                       Float* __restrict__ p_dst,
Chao Liu's avatar
Chao Liu committed
13
14
                                       Sequence<NRow, NCol>,
                                       Number<DataPerRead>)
15
{
Chao Liu's avatar
Chao Liu committed
16
17
18
19
    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
20
21
    constexpr auto src_mtx = SrcMatrix{};
    constexpr auto dst_mtx = DstMatrix{};
22

Chao Liu's avatar
Chao Liu committed
23
    for(index_t i = 0; i < NRow; ++i)
24
    {
Chao Liu's avatar
Chao Liu committed
25
        for(index_t j = 0; j < NCol; j += DataPerRead)
26
        {
Chao Liu's avatar
Chao Liu committed
27
28
            const index_t src_index = src_mtx.Get1dIndex(i, j);
            const index_t dst_index = dst_mtx.Get1dIndex(i, j);
29

Chao Liu's avatar
tidy yp  
Chao Liu committed
30
            *reinterpret_cast<vector_t*>(&p_dst[dst_index]) =
Chao Liu's avatar
Chao Liu committed
31
                *reinterpret_cast<const vector_t*>(&p_src[src_index]);
32
33
34
35
36
37
38
39
40
41
42
43
        }
    }
}

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

Chao Liu's avatar
Chao Liu committed
61
62
63
        constexpr index_t M = c_mtx.NRow();
        constexpr index_t N = c_mtx.NCol();
        constexpr index_t K = a_mtx.NRow(); // A is transposed
64

Chao Liu's avatar
Chao Liu committed
65
        for(index_t k = 0; k < K; ++k)
66
        {
Jing Zhang's avatar
Jing Zhang committed
67
            for(index_t i = 0; i < M; i++)
68
            {
Jing Zhang's avatar
Jing Zhang committed
69
                for(index_t j = 0; j < N; j++)
70
                {
Chao Liu's avatar
Chao Liu committed
71
72
73
                    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);
74

Jing Zhang's avatar
Jing Zhang committed
75
                    p_c_thread[cindex] += p_a_thread[aindex] * p_b_thread[bindex];
76
77
78
79
80
81
82
83
84
85
                }
            }
        }
    }
    else
    {
        // not implemented
        assert(false);
    }
}