threadwise_gemm.hip.hpp 3.11 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
#if 0
Chao Liu's avatar
Chao Liu committed
16
    for(index_t i = 0; i < NRow; ++i)
17
    {
Chao Liu's avatar
Chao Liu committed
18
        for(index_t j = 0; j < NCol; ++j)
19
        {
Chao Liu's avatar
Chao Liu committed
20
21
            const index_t src_index = src_mtx.Get1dIndex(i, j);
            const index_t dst_index = dst_mtx.Get1dIndex(i, j);
22
23
24
25

            p_dst[dst_index] = p_src[src_index];
        }
    }
Jing Zhang's avatar
Jing Zhang committed
26
#else
Chao Liu's avatar
Chao Liu committed
27
28
29
30
31
32
33
    static_assert(NCol == 4, "only for NCol == 4");

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

Jing Zhang's avatar
Jing Zhang committed
34
35
36
37
        Float4 *reg_p = (Float4 *)&p_dst[dst_index];
        Float4 *loc_p = (Float4 *)&p_src[src_index];

        ds_read_b128(reg_p[0], (void *)&loc_p[0]);
Chao Liu's avatar
Chao Liu committed
38
39
    }
#endif
40
41
42
43
44
45
46
47
48
49
50
51
52
}

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
53
                                integral_constant<bool, TransA>,
Chao Liu's avatar
Chao Liu committed
54
                                const FloatA* __restrict__ p_a_thread,
55
                                MatrixB,
Chao Liu's avatar
Chao Liu committed
56
                                integral_constant<bool, TransB>,
Chao Liu's avatar
Chao Liu committed
57
                                const FloatB* __restrict__ p_b_thread,
58
                                MatrixC,
Chao Liu's avatar
Chao Liu committed
59
                                integral_constant<bool, TransC>,
Chao Liu's avatar
Chao Liu committed
60
                                FloatC* __restrict__ p_c_thread,
61
62
63
64
                                Accumulator f_accum)
{
    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+=4)
76
            {
Jing Zhang's avatar
Jing Zhang committed
77
78
79
80
                const index_t aindex = a_mtx.Get1dIndex(k, i); // A is transposed
                const Float4 *a_vec = (const Float4 *)&p_a_thread[aindex];

                for(index_t j = 0; j < N; j+=4)
81
                {
Chao Liu's avatar
Chao Liu committed
82
83
                    const index_t bindex = b_mtx.Get1dIndex(k, j);
                    const index_t cindex = c_mtx.Get1dIndex(i, j);
84

Jing Zhang's avatar
Jing Zhang committed
85
86
87
88
                    const Float4 *b_vec = (const Float4 *)&p_b_thread[bindex];
                    Float4 *c_vec = (Float4 *)&p_c_thread[cindex];

                    outerProduct4x4(a_vec[0], b_vec[0], c_vec[0], c_vec[2], c_vec[4], c_vec[6]);
89
90
91
92
93
94
95
96
97
98
                }
            }
        }
    }
    else
    {
        // not implemented
        assert(false);
    }
}