threadwise_gemm.hip.hpp 3.18 KB
Newer Older
1
#pragma once
Chao Liu's avatar
Chao Liu committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "common.hip.hpp"
#include "ConstantMatrixDescriptor.hip.hpp"

template <class Float, class Matrix>
__device__ void threadwise_matrix_set_zero(Matrix, Float* __restrict__ p_thread)
{
    for(index_t i = 0; i < Matrix::NRow(); ++i)
    {
        for(index_t j = 0; j < Matrix::NCol(); ++j)
        {
            const index_t id = Matrix::Get1dIndex(i, j);
            p_thread[id]     = 0;
        }
    }
}
17

Chao Liu's avatar
tidy yp  
Chao Liu committed
18
19
20
21
22
23
template <class Float,
          class SrcMatrix,
          class DstMatrix,
          index_t NRow,
          index_t NCol,
          index_t DataPerRead>
Chao Liu's avatar
Chao Liu committed
24
25
26
27
__device__ void threadwise_matrix_copy(SrcMatrix,
                                       const Float* __restrict__ p_src,
                                       DstMatrix,
                                       Float* __restrict__ p_dst,
Chao Liu's avatar
Chao Liu committed
28
29
                                       Sequence<NRow, NCol>,
                                       Number<DataPerRead>)
30
{
Chao Liu's avatar
Chao Liu committed
31
32
33
34
    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
35
36
    constexpr auto src_mtx = SrcMatrix{};
    constexpr auto dst_mtx = DstMatrix{};
37

Chao Liu's avatar
Chao Liu committed
38
    for(index_t i = 0; i < NRow; ++i)
39
    {
Chao Liu's avatar
Chao Liu committed
40
        for(index_t j = 0; j < NCol; j += DataPerRead)
41
        {
Chao Liu's avatar
Chao Liu committed
42
43
            const index_t src_index = src_mtx.Get1dIndex(i, j);
            const index_t dst_index = dst_mtx.Get1dIndex(i, j);
44

Chao Liu's avatar
tidy yp  
Chao Liu committed
45
            *reinterpret_cast<vector_t*>(&p_dst[dst_index]) =
Chao Liu's avatar
Chao Liu committed
46
                *reinterpret_cast<const vector_t*>(&p_src[src_index]);
47
48
49
50
51
52
53
54
55
56
57
58
        }
    }
}

template <class MatrixA,
          class MatrixB,
          class MatrixC,
          bool TransA,
          bool TransB,
          bool TransC,
          class FloatA,
          class FloatB,
59
          class FloatC>
60
__device__ void threadwise_gemm(MatrixA,
Chao Liu's avatar
Chao Liu committed
61
                                integral_constant<bool, TransA>,
Chao Liu's avatar
Chao Liu committed
62
                                const FloatA* __restrict__ p_a_thread,
63
                                MatrixB,
Chao Liu's avatar
Chao Liu committed
64
                                integral_constant<bool, TransB>,
Chao Liu's avatar
Chao Liu committed
65
                                const FloatB* __restrict__ p_b_thread,
66
                                MatrixC,
Chao Liu's avatar
Chao Liu committed
67
                                integral_constant<bool, TransC>,
68
                                FloatC* __restrict__ p_c_thread)
69
70
71
{
    if(TransA && (!TransB) && (!TransC))
    {
Chao Liu's avatar
Chao Liu committed
72
73
74
        constexpr auto a_mtx = MatrixA{};
        constexpr auto b_mtx = MatrixB{};
        constexpr auto c_mtx = MatrixC{};
75

Chao Liu's avatar
Chao Liu committed
76
77
78
        constexpr index_t M = c_mtx.NRow();
        constexpr index_t N = c_mtx.NCol();
        constexpr index_t K = a_mtx.NRow(); // A is transposed
79

Chao Liu's avatar
Chao Liu committed
80
        for(index_t k = 0; k < K; ++k)
81
        {
Chao Liu's avatar
Chao Liu committed
82
            for(index_t i = 0; i < M; ++i)
83
            {
Chao Liu's avatar
Chao Liu committed
84
                for(index_t j = 0; j < N; ++j)
85
                {
Chao Liu's avatar
Chao Liu committed
86
87
88
                    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);
89

Jing Zhang's avatar
Jing Zhang committed
90
                    p_c_thread[cindex] += p_a_thread[aindex] * p_b_thread[bindex];
91
92
93
94
95
96
97
98
99
100
                }
            }
        }
    }
    else
    {
        // not implemented
        assert(false);
    }
}