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

Jing Zhang's avatar
Jing Zhang committed
3
4
#include "inline_asm.hpp"

Chao Liu's avatar
Chao Liu committed
5
template <class Float, class SrcMatrix, class DstMatrix, index_t NRow, index_t NCol>
Chao Liu's avatar
Chao Liu committed
6
7
8
9
10
__device__ void threadwise_matrix_copy(SrcMatrix,
                                       const Float* __restrict__ p_src,
                                       DstMatrix,
                                       Float* __restrict__ p_dst,
                                       Sequence<NRow, NCol>)
11
{
Chao Liu's avatar
Chao Liu committed
12
13
    constexpr auto src_mtx = SrcMatrix{};
    constexpr auto dst_mtx = DstMatrix{};
14

Chao Liu's avatar
Chao Liu committed
15
    for(index_t i = 0; i < NRow; ++i)
16
    {
17
18
        // optimize for vector-4 load
        if(NCol % 4 == 0)
19
        {
20
            using vector_t = typename vector_type<Float, 4>::MemoryType;
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
            for(index_t j = 0; j < NCol / 4; ++j)
            {
                const index_t src_index = src_mtx.Get1dIndex(i, 4 * j);
                const index_t dst_index = dst_mtx.Get1dIndex(i, 4 * j);

                *reinterpret_cast<vector_t*>(&p_dst[dst_index]) =
                    *reinterpret_cast<const vector_t*>(&p_src[src_index]);
            }
        }
        else
        {
            for(index_t j = 0; j < NCol; ++j)
            {
                const index_t src_index = src_mtx.Get1dIndex(i, j);
                const index_t dst_index = dst_mtx.Get1dIndex(i, j);

                p_dst[dst_index] = p_src[src_index];
            }
40
41
42
43
44
45
46
47
48
49
50
51
        }
    }
}

template <class MatrixA,
          class MatrixB,
          class MatrixC,
          bool TransA,
          bool TransB,
          bool TransC,
          class FloatA,
          class FloatB,
52
          class FloatC>
53
__device__ void threadwise_gemm(MatrixA,
Chao Liu's avatar
Chao Liu committed
54
                                integral_constant<bool, TransA>,
Chao Liu's avatar
Chao Liu committed
55
                                const FloatA* __restrict__ p_a_thread,
56
                                MatrixB,
Chao Liu's avatar
Chao Liu committed
57
                                integral_constant<bool, TransB>,
Chao Liu's avatar
Chao Liu committed
58
                                const FloatB* __restrict__ p_b_thread,
59
                                MatrixC,
Chao Liu's avatar
Chao Liu committed
60
                                integral_constant<bool, TransC>,
61
                                FloatC* __restrict__ p_c_thread)
62
63
64
{
    if(TransA && (!TransB) && (!TransC))
    {
Chao Liu's avatar
Chao Liu committed
65
66
67
        constexpr auto a_mtx = MatrixA{};
        constexpr auto b_mtx = MatrixB{};
        constexpr auto c_mtx = MatrixC{};
68

Chao Liu's avatar
Chao Liu committed
69
70
71
        constexpr index_t M = c_mtx.NRow();
        constexpr index_t N = c_mtx.NCol();
        constexpr index_t K = a_mtx.NRow(); // A is transposed
72

Chao Liu's avatar
Chao Liu committed
73
        for(index_t k = 0; k < K; ++k)
74
        {
Jing Zhang's avatar
Jing Zhang committed
75
            for(index_t i = 0; i < M; i++)
76
            {
Jing Zhang's avatar
Jing Zhang committed
77
                for(index_t j = 0; j < N; j++)
78
                {
Chao Liu's avatar
Chao Liu committed
79
80
81
                    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);
82

Jing Zhang's avatar
Jing Zhang committed
83
                    p_c_thread[cindex] += p_a_thread[aindex] * p_b_thread[bindex];
84
85
86
87
88
89
90
91
92
93
                }
            }
        }
    }
    else
    {
        // not implemented
        assert(false);
    }
}