-
Notifications
You must be signed in to change notification settings - Fork 0
/
kernel_u8u8u32.hpp
241 lines (214 loc) · 8.09 KB
/
kernel_u8u8u32.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <stdio.h>
#include <stdlib.h>
#include <utils.h>
#include <algorithm>
#include <cstdint>
#include <memory>
#include <vec.h>
#include <test_reference.h>
constexpr int fullVectored(int N, int size) {
return (N % (VLEN_BYTES / size)) == 0;
}
constexpr int kernelType(int ROWS, int COLS, int elementSize) {
return fullVectored(ROWS, elementSize) ? 1 : 2;
}
template <int ROWS, int COLS>
inline typename std::enable_if<kernelType(ROWS, COLS, sizeof(uint32_t)) == 1,
void>::type
gbp(dim_t k, const uint8_t *__restrict__ MP_A, const uint8_t *__restrict__ MP_B,
uint32_t *__restrict__ C, dim_t ldC) {
using vType = typename vec_type_t<uint32_t>::Type;
constexpr int VLEN = vec_type_t<uint32_t>::size();
const uint32_t *MT_A = reinterpret_cast<const uint32_t *>(MP_A);
const uint32_t *MT_B = reinterpret_cast<const uint32_t *>(MP_B);
vec_type_t<uint32_t> Caux[ROWS / VLEN][COLS] = {};
dim_t real_k = k / 4;
const vuint16 vz16 = {0};
const vuint8 mask
= {0, 1, 16, 17, 4, 5, 20, 21, 8, 9, 24, 25, 12, 13, 28, 29};
if (real_k & 1) {
for (int i = 0; i < ROWS / VLEN; i++) {
auto Ak = cast<uint8_t>(
vec_type_t<uint32_t>::load_hinted(&MT_A[i * VLEN]));
for (int j = 0; j < COLS; j++) {
auto BkI = cast<uint8_t>(vec_type_t<uint32_t> {MT_B[j]});
#if !defined(LOW_0_127)
Caux[i][j] = multiplySum4(Ak, BkI, Caux[i][j]);
#else
Caux[i][j] = multiplySum4Low(Ak, BkI, Caux[i][j]);
#endif
}
}
MT_A += ROWS;
MT_B += COLS;
}
asm("");
dim_t real_k_2 = real_k & (-2);
for (dim_t p = 0; p < real_k_2; p += 2) {
for (int i = 0; i < ROWS / VLEN; i++) {
auto Ak0 = cast<uint8_t>(
vec_type_t<uint32_t>::load_hinted(&MT_A[i * VLEN]));
auto Ak1 = cast<uint8_t>(
vec_type_t<uint32_t>::load_hinted(&MT_A[i * VLEN + ROWS]));
for (int j = 0; j < COLS; j++) {
auto BkI0 = cast<uint8_t>(vec_type_t<uint32_t> {MT_B[j]});
auto BkI1
= cast<uint8_t>(vec_type_t<uint32_t> {MT_B[j + COLS]});
#if !defined(LOW_0_127)
const vuint8 a0 = Ak0;
const vuint8 b0 = BkI0;
const vuint8 a1 = Ak1;
const vuint8 b1 = BkI1;
auto reso0 = vec_mulo(a0, b0);
auto rese0 = vec_mule(a0, b0);
auto reso1 = vec_mulo(a1, b1);
auto rese1 = vec_mule(a1, b1);
auto resh = vec_perm(reso0, rese0, mask);
Caux[i][j] = Caux[i][j].vec() + vec_sum4(reso1, reso0)
+ vec_sum4(rese1, rese0) + vec_sum4(resh, vz16);
#else
Caux[i][j] = multiplySum4Low(Ak0, BkI0, Caux[i][j]);
Caux[i][j] = multiplySum4Low(Ak1, BkI1, Caux[i][j]);
#endif
}
}
MT_A += 2 * ROWS;
MT_B += 2 * COLS;
}
asm("");
for (int j = 0; j < COLS; j++) {
for (int i = 0; i < ROWS / VLEN; i++) {
vType *C_ij = (vType *)&gPtr(i * VLEN, j);
*C_ij += (vType)Caux[i][j];
}
}
}
template <int ROWS, int COLS>
inline typename std::enable_if<kernelType(ROWS, COLS, sizeof(uint32_t)) == 2,
void>::type
gbp(dim_t k, const uint8_t *__restrict__ MP_A, const uint8_t *__restrict__ MP_B,
uint32_t *__restrict__ C, dim_t ldC) {
using vType = typename vec_type_t<uint32_t>::Type;
constexpr int VLEN = vec_type_t<uint32_t>::size();
const uint32_t *MT_A = reinterpret_cast<const uint32_t *>(MP_A);
const uint32_t *MT_B = reinterpret_cast<const uint32_t *>(MP_B);
vec_type_t<uint32_t> Caux[COLS] = {};
dim_t real_k = k / 4;
dim_t real_k_2 = real_k & (-2);
constexpr int BYTE_INDEX = ROWS * 2 * sizeof(uint8_t) - 1;
const vuint16 vz16 = {0};
const vuint8 mask
= {0, 1, 16, 17, 4, 5, 20, 21, 8, 9, 24, 25, 12, 13, 28, 29};
if (real_k & 1) {
auto Ak = cast<uint8_t>(
vec_type_t<uint32_t>::loadLen(MT_A, BYTE_INDEX));
for (int j = 0; j < COLS; j++) {
auto BkI = cast<uint8_t>(vec_type_t<uint32_t> {MT_B[j]});
#if !defined(LOW_0_127)
Caux[j] = multiplySum4(Ak, BkI, Caux[j]);
#else
Caux[j] = multiplySum4Low(Ak, BkI, Caux[j]);
#endif
}
MT_A += ROWS;
MT_B += COLS;
}
asm("");
for (dim_t p = 0; p < real_k_2; p += 2) {
auto Ak0 = cast<uint8_t>(
vec_type_t<uint32_t>::loadLen(&MT_A[0], BYTE_INDEX));
auto Ak1 = cast<uint8_t>(
vec_type_t<uint32_t>::loadLen(&MT_A[ROWS], BYTE_INDEX));
for (int j = 0; j < COLS; j++) {
auto BkI0 = cast<uint8_t>(vec_type_t<uint32_t> {MT_B[j]});
auto BkI1 = cast<uint8_t>(vec_type_t<uint32_t> {MT_B[j + COLS]});
#if !defined(LOW_0_127)
const vuint8 a0 = Ak0;
const vuint8 b0 = BkI0;
const vuint8 a1 = Ak1;
const vuint8 b1 = BkI1;
auto reso0 = vec_mulo(a0, b0);
auto rese0 = vec_mule(a0, b0);
auto reso1 = vec_mulo(a1, b1);
auto rese1 = vec_mule(a1, b1);
auto resh = vec_perm(reso0, rese0, mask);
Caux[j] = Caux[j].vec() + vec_sum4(reso1, reso0)
+ vec_sum4(rese1, rese0) + vec_sum4(resh, vz16);
#else
Caux[j] = multiplySum4Low(Ak0, BkI0, Caux[j]);
Caux[j] = multiplySum4Low(Ak1, BkI1, Caux[j]);
#endif
}
MT_A += 2 * ROWS;
MT_B += 2 * COLS;
}
asm("");
for (int j = 0; j < COLS; j++) {
auto C_ij = vec_type_t<uint32_t>::loadLen(&gPtr(0, j), BYTE_INDEX);
C_ij += Caux[j];
C_ij.storeLen(&gPtr(0, j), BYTE_INDEX);
}
}
template <int M, int ROWS, int COLS>
typename std::enable_if<(M >= ROWS), void>::type LoopOne_TAIL(dim_t m, dim_t k,
const uint8_t *Apacked, const uint8_t *Bpacked, uint32_t *C,
dim_t ldC) {
// end of the roll
}
template <int M, int ROWS, int COLS>
typename std::enable_if<(M < ROWS), void>::type LoopOne_TAIL(dim_t m, dim_t k,
const uint8_t *Apacked, const uint8_t *Bpacked, uint32_t *C,
dim_t ldC) {
if (m & M) {
gbp<M, COLS>(k, Apacked, Bpacked, C, ldC);
Apacked = &Apacked[M * k];
C = &gPtr(M, 0);
}
LoopOne_TAIL<2 * M, ROWS, COLS>(m, k, Apacked, Bpacked, C, ldC);
}
template <int ROWS, int COLS>
inline void LoopOne(dim_t m, dim_t k, const uint8_t *Apacked,
const uint8_t *Bpacked, uint32_t *C, dim_t ldC) {
for (dim_t i = 0; i < m / ROWS; i++) {
gbp<ROWS, COLS>(
k, &Apacked[i * ROWS * k], Bpacked, &gPtr(i * ROWS, 0), ldC);
}
dim_t II = m - m % ROWS;
if (m > II)
LoopOne_TAIL<1, ROWS, COLS>(
m - II, k, &Apacked[II * k], Bpacked, &gPtr(II, 0), ldC);
}
template <int N, int COLS>
typename std::enable_if<(N >= COLS), void>::type LoopTwo_TAIL(dim_t m, dim_t n,
dim_t k, const uint8_t *Apacked, const uint8_t *Bpacked, uint32_t *C,
dim_t ldC) {
// end of the roll
}
template <int N, int COLS>
typename std::enable_if<(N < COLS), void>::type LoopTwo_TAIL(dim_t m, dim_t n,
dim_t k, const uint8_t *Apacked, const uint8_t *Bpacked, uint32_t *C,
dim_t ldC) {
if (n & N) {
LoopOne<MR, N>(m, k, Apacked, Bpacked, C, ldC);
Bpacked = &Bpacked[N * k];
C = &gPtr(0, N);
}
LoopTwo_TAIL<2 * N, COLS>(m, n, k, Apacked, Bpacked, C, ldC);
}
template <int COLS>
__attribute__((noinline)) void LoopTwo(dim_t m, dim_t n, dim_t k,
const uint8_t *Apacked, const uint8_t *Bpacked, uint32_t *C,
dim_t ldC) {
for (dim_t j = 0; j < n / COLS; j++) {
LoopOne<MR, COLS>(
m, k, Apacked, &Bpacked[j * COLS * k], &gPtr(0, j * COLS), ldC);
}
// tails , should be unrolled
// for example n&1, n&2 and et cetera
// actually its possible to combine tail <VLEN as
// as we are using vec_load_len
dim_t JJ = n - n % COLS;
if (n > JJ)
LoopTwo_TAIL<1, COLS>(
m, n - JJ, k, Apacked, &Bpacked[JJ * k], &gPtr(0, JJ), ldC);
}