threadwise_gemm.hip.hpp 3.12 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

Jing Zhang's avatar
Jing Zhang committed
15
#if 1
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);

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

Chao Liu's avatar
Chao Liu committed
37
        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
#if 1
Jing Zhang's avatar
Jing Zhang committed
76
            for(index_t i = 0; i < M; i++)
77
            {
Jing Zhang's avatar
Jing Zhang committed
78
79
                const index_t aindex = a_mtx.Get1dIndex(k, i); // A is transposed

Jing Zhang's avatar
Jing Zhang committed
80
                for(index_t j = 0; j < N; j++)
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
                    p_c_thread[cindex] += p_a_thread[aindex] * p_b_thread[bindex];
86
87
                }
            }
Jing Zhang's avatar
Jing Zhang committed
88
#else
Chao Liu's avatar
Chao Liu committed
89
90
91
            const Float4* a_vec = (const Float4*)p_a_thread;
            const Float4* b_vec = (const Float4*)p_b_thread;
            Float4* c_vec       = (Float4*)p_c_thread;
Jing Zhang's avatar
Jing Zhang committed
92
93
94

            outerProduct8x8(a_vec, b_vec, c_vec);
#endif
95
96
97
98
99
100
101
102
        }
    }
    else
    {
        // not implemented
        assert(false);
    }
}