"driver/driver.cpp" did not exist on "d51b81588ff6102dbde9c9d91810c1bb8f709cfc"
threadwise_gemm.hpp 3.75 KB
Newer Older
1
2
3
#ifndef CK_THREADWISE_GEMM_HPP
#define CK_THREADWISE_GEMM_HPP

Chao Liu's avatar
Chao Liu committed
4
5
#include "common.hpp"
#include "ConstantMatrixDescriptor.hpp"
Chao Liu's avatar
Chao Liu committed
6

7
8
namespace ck {

Chao Liu's avatar
Chao Liu committed
9
10
11
12
13
14
15
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)
        {
16
            const index_t id = Matrix::GetOffsetFromMultiIndex(i, j);
Chao Liu's avatar
Chao Liu committed
17
            p_thread[id]     = Float(0);
Chao Liu's avatar
Chao Liu committed
18
19
20
        }
    }
}
21

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

Chao Liu's avatar
Chao Liu committed
42
    for(index_t i = 0; i < NRow; ++i)
43
    {
Chao Liu's avatar
Chao Liu committed
44
        for(index_t j = 0; j < NCol; j += DataPerRead)
45
        {
46
47
            const index_t src_index = src_mtx.GetOffsetFromMultiIndex(i, j);
            const index_t dst_index = dst_mtx.GetOffsetFromMultiIndex(i, j);
48

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

template <class MatrixA,
          class MatrixB,
          class MatrixC,
          bool TransA,
          bool TransB,
          bool TransC,
          class FloatA,
          class FloatB,
63
          class FloatC>
64
__device__ void threadwise_gemm(MatrixA,
Chao Liu's avatar
Chao Liu committed
65
                                integral_constant<bool, TransA>,
Chao Liu's avatar
Chao Liu committed
66
                                const FloatA* __restrict__ p_a_thread,
67
                                MatrixB,
Chao Liu's avatar
Chao Liu committed
68
                                integral_constant<bool, TransB>,
Chao Liu's avatar
Chao Liu committed
69
                                const FloatB* __restrict__ p_b_thread,
70
                                MatrixC,
Chao Liu's avatar
Chao Liu committed
71
                                integral_constant<bool, TransC>,
72
                                FloatC* __restrict__ p_c_thread)
73
{
Chao Liu's avatar
Chao Liu committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#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

90
91
    if(TransA && (!TransB) && (!TransC))
    {
Chao Liu's avatar
Chao Liu committed
92
93
94
        constexpr auto a_mtx = MatrixA{};
        constexpr auto b_mtx = MatrixB{};
        constexpr auto c_mtx = MatrixC{};
95

Chao Liu's avatar
Chao Liu committed
96
97
98
        constexpr index_t M = c_mtx.NRow();
        constexpr index_t N = c_mtx.NCol();
        constexpr index_t K = a_mtx.NRow(); // A is transposed
99

Chao Liu's avatar
Chao Liu committed
100
        for(index_t k = 0; k < K; ++k)
101
        {
Chao Liu's avatar
Chao Liu committed
102
            for(index_t i = 0; i < M; ++i)
103
            {
Chao Liu's avatar
Chao Liu committed
104
                for(index_t j = 0; j < N; ++j)
105
                {
106
107
108
                    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);
109

Jing Zhang's avatar
Jing Zhang committed
110
                    p_c_thread[cindex] += p_a_thread[aindex] * p_b_thread[bindex];
111
112
113
114
115
116
117
118
119
120
                }
            }
        }
    }
    else
    {
        // not implemented
        assert(false);
    }
}
121
122
123

} // namespace ck
#endif