-
Notifications
You must be signed in to change notification settings - Fork 11
/
sgemm_sse.h
451 lines (401 loc) · 9.88 KB
/
sgemm_sse.h
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/* public domain Simple, Minimalistic, Fast GEMM library
* ©2018 Yuichiro Nakada
*
* Basic usage:
* sgemm_sse('R', 'N', 'N', m_col_a, n_row_b, k_row_a_col_b, alpha, a, col_a, b, col_b, beta, c, col_c);
* */
#ifdef _MSC_VER
#include <intrin.h>
#else
#include <x86intrin.h>
#endif
#define MC 384
#define KC 384
#define NC 4096
#define MR 8
#define NR 8
// Local buffers for storing panels from A, B and C
static float _a[MC*KC] __attribute__ ((aligned (16)));
static float _b[KC*NC] __attribute__ ((aligned (16)));
static float _c[MR*NR] __attribute__ ((aligned (16)));
// Packing complete panels from A (i.e. without padding)
static inline void _pack_MRxk(int k, const float *A, int incRowA, int incColA, float *buffer)
{
int i, j;
for (j=0; j<k; ++j) {
for (i=0; i<MR; ++i) {
buffer[i] = A[i*incRowA];
}
buffer += MR;
A += incColA;
}
}
// Packing panels from A with padding if required
static inline void _pack_A(int mc, int kc, const float *A, int incRowA, int incColA, float *buffer)
{
int mp = mc / MR;
int _mr = mc % MR;
int i, j;
for (i=0; i<mp; ++i) {
_pack_MRxk(kc, A, incRowA, incColA, buffer);
buffer += kc*MR;
A += MR*incRowA;
}
if (_mr>0) {
for (j=0; j<kc; ++j) {
for (i=0; i<_mr; ++i) {
buffer[i] = A[i*incRowA];
}
for (i=_mr; i<MR; ++i) {
buffer[i] = 0.0;
}
buffer += MR;
A += incColA;
}
}
}
// Packing complete panels from B (i.e. without padding)
static inline void _pack_kxNR(int k, const float *B, int incRowB, int incColB, float *buffer)
{
int i, j;
for (i=0; i<k; ++i) {
for (j=0; j<NR; ++j) {
buffer[j] = B[j*incColB];
}
buffer += NR;
B += incRowB;
}
}
// Packing panels from B with padding if required
static inline void _pack_B(int kc, int nc, const float *B, int incRowB, int incColB, float *buffer)
{
int np = nc / NR;
int _nr = nc % NR;
int i, j;
for (j=0; j<np; ++j) {
_pack_kxNR(kc, B, incRowB, incColB, buffer);
buffer += kc*NR;
B += NR*incColB;
}
if (_nr>0) {
for (i=0; i<kc; ++i) {
for (j=0; j<_nr; ++j) {
buffer[j] = B[j*incColB];
}
for (j=_nr; j<NR; ++j) {
buffer[j] = 0.0;
}
buffer += NR;
B += incRowB;
}
}
}
static inline void dot8x8_avx(const float *a, const float *b, float *c, const int kc)
{
/*register*/ __m256 a0, b0, b1, b2, b3, b4, b5, b6, b7;
register __m256 c0, c1, c2, c3, c4, c5, c6, c7;
__asm__ volatile( "prefetcht2 0(%0) \n\t" : :"r"(b) );
__asm__ volatile( "prefetcht0 0(%0) \n\t" : :"r"(a) );
c0 = _mm256_setzero_ps();
c1 = _mm256_setzero_ps();
c2 = _mm256_setzero_ps();
c3 = _mm256_setzero_ps();
c4 = _mm256_setzero_ps();
c5 = _mm256_setzero_ps();
c6 = _mm256_setzero_ps();
c7 = _mm256_setzero_ps();
// #pragma unroll(KC)
for (int i=0; i<kc; i++) {
b0 = _mm256_broadcast_ss(b);
b1 = _mm256_broadcast_ss(b+1);
b2 = _mm256_broadcast_ss(b+2);
b3 = _mm256_broadcast_ss(b+3);
b4 = _mm256_broadcast_ss(b+4);
b5 = _mm256_broadcast_ss(b+5);
b6 = _mm256_broadcast_ss(b+6);
b7 = _mm256_broadcast_ss(b+7);
//#ifdef CATSEYE_ALIGNED
a0 = _mm256_load_ps(a);
//#else
// a0 = _mm256_loadu_ps(a);
//#endif
a += 8;
b += 8;
__asm__ volatile( "prefetcht2 0(%0) \n\t" : :"r"(b) );
__asm__ volatile( "prefetcht0 0(%0) \n\t" : :"r"(a) );
c0 = _mm256_add_ps(c0, _mm256_mul_ps(a0, b0));
c1 = _mm256_add_ps(c1, _mm256_mul_ps(a0, b1));
c2 = _mm256_add_ps(c2, _mm256_mul_ps(a0, b2));
c3 = _mm256_add_ps(c3, _mm256_mul_ps(a0, b3));
c4 = _mm256_add_ps(c4, _mm256_mul_ps(a0, b4));
c5 = _mm256_add_ps(c5, _mm256_mul_ps(a0, b5));
c6 = _mm256_add_ps(c6, _mm256_mul_ps(a0, b6));
c7 = _mm256_add_ps(c7, _mm256_mul_ps(a0, b7));
}
//#ifdef CATSEYE_ALIGNED
_mm256_store_ps(c, c0);
_mm256_store_ps(c+8, c1);
_mm256_store_ps(c+16, c2);
_mm256_store_ps(c+24, c3);
_mm256_store_ps(c+32, c4);
_mm256_store_ps(c+40, c5);
_mm256_store_ps(c+48, c6);
_mm256_store_ps(c+56, c7);
/*#else
_mm256_storeu_ps(c, c0);
_mm256_storeu_ps(c+8, c1);
_mm256_storeu_ps(c+16, c2);
_mm256_storeu_ps(c+24, c3);
_mm256_storeu_ps(c+32, c4);
_mm256_storeu_ps(c+40, c5);
_mm256_storeu_ps(c+48, c6);
_mm256_storeu_ps(c+56, c7);
#endif*/
}
// Micro kernel for multiplying panels from A and B.
static inline void _sgemm_micro_kernel(
long kc,
float alpha, const float *A, const float *B,
float beta,
float *C, long incRowC, long incColC)
{
static float AB[MR*NR] __attribute__ ((aligned (16)));
int i, j;
// Compute AB = A*B
dot8x8_avx(A, B, AB, kc);
#if 0
memset(AB, 0, MR*NR*sizeof(float));
for (int l=0; l<kc; ++l) {
dot8x8_avx(A, B, AB);
/*for (j=0; j<NR; ++j) {
for (i=0; i<MR; ++i) {
AB[i+j*MR] += A[i]*B[j];
}
}*/
A += MR;
B += NR;
}
#endif
// Update C <- beta*C
if (beta==0.0) {
for (j=0; j<NR; ++j) {
for (i=0; i<MR; ++i) {
C[i*incRowC+j*incColC] = 0.0;
}
}
} else if (beta!=1.0) {
for (j=0; j<NR; ++j) {
for (i=0; i<MR; ++i) {
C[i*incRowC+j*incColC] *= beta;
}
}
}
// Update C <- C + alpha*AB (note: the case alpha==0.0 was already treated in
// the above layer sgemm_nn)
if (alpha==1.0) {
for (j=0; j<NR; ++j) {
for (i=0; i<MR; ++i) {
C[i*incRowC+j*incColC] += AB[i+j*MR];
}
}
} else {
for (j=0; j<NR; ++j) {
for (i=0; i<MR; ++i) {
C[i*incRowC+j*incColC] += alpha*AB[i+j*MR];
}
}
}
}
// Compute Y += alpha*X
static inline void sgeaxpy(
int m,
int n,
float alpha,
const float *X,
int incRowX,
int incColX,
float *Y,
int incRowY,
int incColY)
{
int i, j;
if (alpha!=1.0) {
for (j=0; j<n; ++j) {
for (i=0; i<m; ++i) {
Y[i*incRowY+j*incColY] += alpha*X[i*incRowX+j*incColX];
}
}
} else {
for (j=0; j<n; ++j) {
for (i=0; i<m; ++i) {
Y[i*incRowY+j*incColY] += X[i*incRowX+j*incColX];
}
}
}
}
// Compute X *= alpha
static inline void sgescal(int m, int n, float alpha, float *X, int incRowX, int incColX)
{
int i, j;
if (alpha!=0.0) {
for (j=0; j<n; ++j) {
for (i=0; i<m; ++i) {
X[i*incRowX+j*incColX] *= alpha;
}
}
} else {
for (j=0; j<n; ++j) {
for (i=0; i<m; ++i) {
X[i*incRowX+j*incColX] = 0.0;
}
}
}
}
// Macro Kernel for the multiplication of blocks of A and B. We assume that
// these blocks were previously packed to buffers _A and _B.
static inline void _sgemm_macro_kernel(
int mc,
int nc,
int kc,
float alpha,
float beta,
float *C,
int incRowC,
int incColC)
{
int mp = (mc+MR-1) / MR;
int np = (nc+NR-1) / NR;
int _mr = mc % MR;
int _nr = nc % NR;
int mr, nr;
int i, j;
for (j=0; j<np; ++j) {
nr = (j!=np-1 || _nr==0) ? NR : _nr;
for (i=0; i<mp; ++i) {
mr = (i!=mp-1 || _mr==0) ? MR : _mr;
if (mr==MR && nr==NR) {
_sgemm_micro_kernel(kc, alpha, &_a[i*kc*MR], &_b[j*kc*NR],
beta, &C[i*MR*incRowC+j*NR*incColC], incRowC, incColC);
} else {
_sgemm_micro_kernel(kc, alpha, &_a[i*kc*MR], &_b[j*kc*NR], 0.0, _c, 1, MR);
sgescal(mr, nr, beta, &C[i*MR*incRowC+j*NR*incColC], incRowC, incColC);
sgeaxpy(mr, nr, 1.0, _c, 1, MR, &C[i*MR*incRowC+j*NR*incColC], incRowC, incColC);
}
}
}
}
// Compute C <- beta*C + alpha*A*B
static inline void sgemm_nn(
int m, int n, int k, float alpha,
const float *A, int incRowA, int incColA, const float *B, int incRowB, int incColB,
float beta, float *C, int incRowC, int incColC)
{
int mb = (m+MC-1) / MC;
int nb = (n+NC-1) / NC;
int kb = (k+KC-1) / KC;
int _mc = m % MC;
int _nc = n % NC;
int _kc = k % KC;
int mc, nc, kc;
int i, j, l;
float _beta;
if (alpha==0.0 || k==0) {
sgescal(m, n, beta, C, incRowC, incColC);
return;
}
for (j=0; j<nb; ++j) {
nc = (j!=nb-1 || _nc==0) ? NC : _nc;
for (l=0; l<kb; ++l) {
kc = (l!=kb-1 || _kc==0) ? KC : _kc;
_beta = (l==0) ? beta : 1.0;
_pack_B(kc, nc, &B[l*KC*incRowB+j*NC*incColB], incRowB, incColB, _b);
for (i=0; i<mb; ++i) {
mc = (i!=mb-1 || _mc==0) ? MC : _mc;
_pack_A(mc, kc, &A[i*MC*incRowA+l*KC*incColA], incRowA, incColA, _a);
_sgemm_macro_kernel(mc, nc, kc, alpha, _beta, &C[i*MC*incRowC+j*NC*incColC], incRowC, incColC);
}
}
}
}
static void sgemm_sse(
char major,
char transA,
char transB,
const int m,
const int n,
const int k,
const float alpha,
const float *A,
const int ldA,
const float *B,
const int ldB,
const float beta,
float *C,
const int ldC)
{
int i, j;
// Quick return if possible
if (m==0 || n==0 || ((alpha==0.0 || k==0) && (beta==1.0))) {
return;
}
// And if alpha is exactly zero
if (alpha==0.0) {
if (beta==0.0) {
for (j=0; j<n; j++) {
for (i=0; i<m; i++) {
C[i+j*ldC] = 0.0;
}
}
} else {
for (j=0; j<n; j++) {
for (i=0; i<m; i++) {
C[i+j*ldC] *= beta;
}
}
}
return;
}
// Start the operations
if (major == 'C') {
if (transB=='N') {
if (transA=='N') {
// Form C := alpha*A*B + beta*C
sgemm_nn(m, n, k, alpha, A, 1, ldA, B, 1, ldB, beta, C, 1, ldC);
} else {
// Form C := alpha*A**T*B + beta*C
sgemm_nn(m, n, k, alpha, A, ldA, 1, B, 1, ldB, beta, C, 1, ldC);
}
} else {
if (transA=='N') {
// Form C := alpha*A*B**T + beta*C
sgemm_nn(m, n, k, alpha, A, 1, ldA, B, ldB, 1, beta, C, 1, ldC);
} else {
// Form C := alpha*A**T*B**T + beta*C
sgemm_nn(m, n, k, alpha, A, ldA, 1, B, ldB, 1, beta, C, 1, ldC);
}
}
} else {
if (transB=='N') {
if (transA=='N') {
// Form C := alpha*A*B + beta*C
sgemm_nn(m, n, k, alpha, A, ldA, 1, B, ldB, 1, beta, C, ldC, 1);
} else {
// Form C := alpha*A**T*B + beta*C
sgemm_nn(m, n, k, alpha, A, 1, ldA, B, ldB, 1, beta, C, ldC, 1);
}
} else {
if (transA=='N') {
// Form C := alpha*A*B**T + beta*C
sgemm_nn(m, n, k, alpha, A, ldA, 1, B, 1, ldB, beta, C, ldC, 1);
} else {
// Form C := alpha*A**T*B**T + beta*C
sgemm_nn(m, n, k, alpha, A, 1, ldA, B, 1, ldB, beta, C, ldC, 1);
}
}
}
}
#undef MC
#undef KC
#undef NC
#undef MR
#undef NR