"\" did not exist on "be33bea4755e47a7dc460a860f86a02c501f83d7"
threadwise_gemm.hip.hpp 3.91 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>
Chao Liu's avatar
Chao Liu committed
4
5
6
7
8
__device__ void threadwise_matrix_copy(SrcMatrix,
                                       const Float* __restrict__ p_src,
                                       DstMatrix,
                                       Float* __restrict__ p_dst,
                                       Sequence<NRow, NCol>)
9
{
Chao Liu's avatar
Chao Liu committed
10
11
    constexpr auto src_mtx = SrcMatrix{};
    constexpr auto dst_mtx = DstMatrix{};
12

Jing Zhang's avatar
Jing Zhang committed
13
14
#if 1
    //NRow = 1
Chao Liu's avatar
Chao Liu committed
15
    for(index_t i = 0; i < NRow; ++i)
16
    {
Jing Zhang's avatar
Jing Zhang committed
17
        //NCol = 4
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];
        }
    }
Chao Liu's avatar
Chao Liu committed
26
#elif 0
Chao Liu's avatar
Chao Liu committed
27
28
29
30
31
32
33
34
35
    static_assert(NCol == 4, "only for NCol == 4");

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

    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
36
37
38
39
40
41
42
43
44
#if 0
        *(reinterpret_cast<vector_t*>(&p_dst[dst_index]) =
            *(reinterpret_cast<const vector_t*>(&p_src[src_index]));
#elif 0
        asm volatile("\n \
                ds_read2_b64 %0, %1 offset1:1 \n \
                s_waitcnt lgkmcnt(0)"
                     : "=v"(*(reinterpret_cast<vector_t*>(&p_dst[dst_index])))
                     : "v"(__to_local((void*)(&p_src[src_index]))));
Chao Liu's avatar
Chao Liu committed
45
46
#elif 1
        asm volatile("\n \
Chao Liu's avatar
Chao Liu committed
47
48
49
50
                ds_read_b128 %0, %1 \n \
                s_waitcnt lgkmcnt(0)"
                     : "=v"(*(reinterpret_cast<vector_t*>(&p_dst[dst_index])))
                     : "v"(__to_local((void*)(&p_src[src_index]))));
Chao Liu's avatar
Chao Liu committed
51
52
53
#endif
    }
#endif
54
55
56
57
58
59
60
61
62
63
64
65
66
}

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
67
                                integral_constant<bool, TransA>,
Chao Liu's avatar
Chao Liu committed
68
                                const FloatA* __restrict__ p_a_thread,
69
                                MatrixB,
Chao Liu's avatar
Chao Liu committed
70
                                integral_constant<bool, TransB>,
Chao Liu's avatar
Chao Liu committed
71
                                const FloatB* __restrict__ p_b_thread,
72
                                MatrixC,
Chao Liu's avatar
Chao Liu committed
73
                                integral_constant<bool, TransC>,
Chao Liu's avatar
Chao Liu committed
74
                                FloatC* __restrict__ p_c_thread,
75
76
77
78
                                Accumulator f_accum)
{
    if(TransA && (!TransB) && (!TransC))
    {
Chao Liu's avatar
Chao Liu committed
79
80
81
        constexpr auto a_mtx = MatrixA{};
        constexpr auto b_mtx = MatrixB{};
        constexpr auto c_mtx = MatrixC{};
82

Chao Liu's avatar
Chao Liu committed
83
84
85
        constexpr index_t M = c_mtx.NRow();
        constexpr index_t N = c_mtx.NCol();
        constexpr index_t K = a_mtx.NRow(); // A is transposed
86

Jing Zhang's avatar
Jing Zhang committed
87
        // K = 1
Chao Liu's avatar
Chao Liu committed
88
        for(index_t k = 0; k < K; ++k)
89
        {
Jing Zhang's avatar
Jing Zhang committed
90
            // M = 8
Chao Liu's avatar
Chao Liu committed
91
            for(index_t i = 0; i < M; ++i)
92
            {
Jing Zhang's avatar
Jing Zhang committed
93
                // N = 8
Chao Liu's avatar
Chao Liu committed
94
                for(index_t j = 0; j < N; ++j)
95
                {
Chao Liu's avatar
Chao Liu committed
96
97
98
                    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);
99

Chao Liu's avatar
Chao Liu committed
100
#if 0
101
                    f_accum(p_c_thread[cindex], p_a_thread[aindex] * p_b_thread[bindex]);
Chao Liu's avatar
Chao Liu committed
102
103
104
105
106
107
108
109
110
#elif 1
                    asm volatile("\n \
                                v_mac_f32 %0, %1, %2 \n \
                                "
                                 : "=v"(p_c_thread[cindex])
                                 : "v"(p_a_thread[aindex]),
                                   "v"(p_b_thread[bindex]),
                                   "0"(p_c_thread[cindex]));
#endif
111
112
113
114
115
116
117
118
119
120
                }
            }
        }
    }
    else
    {
        // not implemented
        assert(false);
    }
}