-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgemm_cpu.h
370 lines (326 loc) · 7.54 KB
/
gemm_cpu.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
#define MC 384
#define KC 384
#define NC 4096
#define MR 4
#define NR 4
// Local buffers for storing panels from A, B and C
static real GEMM(_A)[MC*KC] /*__attribute__ ((aligned(32)))*/;
static real GEMM(_B)[KC*NC] /*__attribute__ ((aligned(32)))*/;
static real GEMM(_C)[MR*NR] /*__attribute__ ((aligned(32)))*/;
// Packing complete panels from A (i.e. without padding)
static void GEMM(_pack_MRxk)(int k, const real *A, int incRowA, int incColA, real *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 void GEMM(_pack_A)(int mc, int kc, const real *A, int incRowA, int incColA, real *buffer)
{
int mp = mc / MR;
int _mr = mc % MR;
int i, j;
for (i=0; i<mp; ++i) {
GEMM(_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 void GEMM(_pack_kxNR)(int k, const real *B, int incRowB, int incColB, real *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 void GEMM(_pack_B)(int kc, int nc, const real *B, int incRowB, int incColB, real *buffer)
{
int np = nc / NR;
int _nr = nc % NR;
int i, j;
for (j=0; j<np; ++j) {
GEMM(_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;
}
}
}
// Micro kernel for multiplying panels from A and B.
static void GEMM(_micro_kernel)(
int kc, real alpha, const real *A, const real *B,
real beta, real *C, int incRowC, int incColC)
{
real AB[MR*NR];
int i, j, l;
// Compute AB = A*B
for (l=0; l<MR*NR; ++l) {
AB[l] = 0;
}
for (l=0; l<kc; ++l) {
for (j=0; j<NR; ++j) {
for (i=0; i<MR; ++i) {
AB[i+j*MR] += A[i]*B[j];
}
}
A += MR;
B += NR;
}
// 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 dgemm_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 void GEMM(_geaxpy)(
int m,
int n,
real alpha,
const real *X,
int incRowX,
int incColX,
real *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 void GEMM(_gescal)(int m, int n, real alpha, real *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 void GEMM(_macro_kernel)(
int mc,
int nc,
int kc,
real alpha,
real beta,
real *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) {
GEMM(_micro_kernel)(kc, alpha, &GEMM(_A)[i*kc*MR], &GEMM(_B)[j*kc*NR], beta, &C[i*MR*incRowC+j*NR*incColC], incRowC, incColC);
} else {
GEMM(_micro_kernel)(kc, alpha, &GEMM(_A)[i*kc*MR], &GEMM(_B)[j*kc*NR], 0.0, GEMM(_C), 1, MR);
GEMM(_gescal)(mr, nr, beta, &C[i*MR*incRowC+j*NR*incColC], incRowC, incColC);
GEMM(_geaxpy)(mr, nr, 1.0, GEMM(_C), 1, MR, &C[i*MR*incRowC+j*NR*incColC], incRowC, incColC);
}
}
}
}
// Compute C <- beta*C + alpha*A*B
void GEMM(_nn_cpu)(
int m, int n, int k,
real alpha,
const real *A, int incRowA, int incColA,
const real *B, int incRowB, int incColB,
real beta,
real *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;
real _beta;
if (alpha==0.0 || k==0) {
GEMM(_gescal)(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;
GEMM(_pack_B)(kc, nc, &B[l*KC*incRowB+j*NC*incColB], incRowB, incColB, GEMM(_B));
for (i=0; i<mb; ++i) {
mc = (i!=mb-1 || _mc==0) ? MC : _mc;
GEMM(_pack_A)(mc, kc, &A[i*MC*incRowA+l*KC*incColA], incRowA, incColA, GEMM(_A));
GEMM(_macro_kernel)(mc, nc, kc, alpha, _beta, &C[i*MC*incRowC+j*NC*incColC], incRowC, incColC);
}
}
}
}
void GEMM(_c)(
char major,
char transA,
char transB,
const int m,
const int n,
const int k,
const real alpha,
const real *A,
const int ldA,
const real *B,
const int ldB,
const real beta,
real *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
GEMM(_nn_cpu)(m, n, k, alpha, A, 1, ldA, B, 1, ldB, beta, C, 1, ldC);
} else {
// Form C := alpha*A**T*B + beta*C
GEMM(_nn_cpu)(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
GEMM(_nn_cpu)(m, n, k, alpha, A, 1, ldA, B, ldB, 1, beta, C, 1, ldC);
} else {
// Form C := alpha*A**T*B**T + beta*C
GEMM(_nn_cpu)(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
GEMM(_nn_cpu)(m, n, k, alpha, A, ldA, 1, B, ldB, 1, beta, C, ldC, 1);
} else {
// Form C := alpha*A**T*B + beta*C
GEMM(_nn_cpu)(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
GEMM(_nn_cpu)(m, n, k, alpha, A, ldA, 1, B, 1, ldB, beta, C, ldC, 1);
} else {
// Form C := alpha*A**T*B**T + beta*C
GEMM(_nn_cpu)(m, n, k, alpha, A, 1, ldA, B, 1, ldB, beta, C, ldC, 1);
}
}
}
}
#undef MC
#undef KC
#undef NC
#undef MR
#undef NR