threadwise_gemm.hpp 3.66 KB
Newer Older
1
#pragma once
Chao Liu's avatar
Chao Liu committed
2
3
#include "common.hpp"
#include "ConstantMatrixDescriptor.hpp"
Chao Liu's avatar
Chao Liu committed
4
5
6
7
8
9
10
11

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)
        {
12
            const index_t id = Matrix::GetOffsetFromMultiIndex(i, j);
Chao Liu's avatar
Chao Liu committed
13
            p_thread[id]     = Float(0);
Chao Liu's avatar
Chao Liu committed
14
15
16
        }
    }
}
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
        {
42
43
            const index_t src_index = src_mtx.GetOffsetFromMultiIndex(i, j);
            const index_t dst_index = dst_mtx.GetOffsetFromMultiIndex(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
{
Chao Liu's avatar
Chao Liu committed
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#if 0
    if(get_thread_local_1d_id() == 0 && get_block_1d_id() == 0)
    {
        printf("p_a_thread: %f %f %f %f\n",
               p_a_thread[0],
               p_a_thread[1],
               p_a_thread[2],
               p_a_thread[3]);
        printf("p_b_thread: %f %f %f %f\n",
               p_b_thread[0],
               p_b_thread[1],
               p_b_thread[2],
               p_b_thread[3]);
    }
#endif

86
87
    if(TransA && (!TransB) && (!TransC))
    {
Chao Liu's avatar
Chao Liu committed
88
89
90
        constexpr auto a_mtx = MatrixA{};
        constexpr auto b_mtx = MatrixB{};
        constexpr auto c_mtx = MatrixC{};
91

Chao Liu's avatar
Chao Liu committed
92
93
94
        constexpr index_t M = c_mtx.NRow();
        constexpr index_t N = c_mtx.NCol();
        constexpr index_t K = a_mtx.NRow(); // A is transposed
95

Chao Liu's avatar
Chao Liu committed
96
        for(index_t k = 0; k < K; ++k)
97
        {
Chao Liu's avatar
Chao Liu committed
98
            for(index_t i = 0; i < M; ++i)
99
            {
Chao Liu's avatar
Chao Liu committed
100
                for(index_t j = 0; j < N; ++j)
101
                {
102
103
104
                    const index_t aindex = a_mtx.GetOffsetFromMultiIndex(k, i); // A is transposed
                    const index_t bindex = b_mtx.GetOffsetFromMultiIndex(k, j);
                    const index_t cindex = c_mtx.GetOffsetFromMultiIndex(i, j);
105

Jing Zhang's avatar
Jing Zhang committed
106
                    p_c_thread[cindex] += p_a_thread[aindex] * p_b_thread[bindex];
107
108
109
110
111
112
113
114
115
116
                }
            }
        }
    }
    else
    {
        // not implemented
        assert(false);
    }
}