-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdgemm-blocked.c
443 lines (394 loc) · 19.9 KB
/
dgemm-blocked.c
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
/*
* Copyright (C) 2022 Xiao Song.
* All Rights Reserved.
* Content of this file is not for commertial use.
*/
#include <immintrin.h>
#include <stdlib.h>
const char* dgemm_desc = "Blocked dgemm.";
#define k_b 1620
#define m_b 31
#define m_r 31
#define n_r 8
// https://stackoverflow.com/questions/63404539/portable-loop-unrolling-with-template-parameter-in-c-with-gcc-icc
// Helper macros for stringification
#define TO_STRING_HELPER(X) #X
#define TO_STRING(X) TO_STRING_HELPER(X)
// Define loop unrolling depending on the compiler
#if defined(__ICC) || defined(__ICL)
#define UNROLL_LOOP(n) _Pragma(TO_STRING(unroll (n)))
#elif defined(__clang__)
#define UNROLL_LOOP(n) _Pragma(TO_STRING(unroll (n)))
#elif defined(__GNUC__) && !defined(__clang__)
#define UNROLL_LOOP(n) _Pragma(TO_STRING(GCC unroll (16)))
#elif defined(_MSC_BUILD)
#pragma message ("Microsoft Visual C++ (MSVC) detected: Loop unrolling not supported!")
#define UNROLL_LOOP(n)
#else
#warning "Unknown compiler: Loop unrolling not supported!"
#define UNROLL_LOOP(n)
#endif
void inner_kernel( double* __restrict__ hat_a, \
double* __restrict__ hat_b, \
double* __restrict__ hat_c, \
int ldc );
void pack_b( double* __restrict__ src_b, double* __restrict__ hat_b, int ldb, int n );
void pack_a( double* __restrict__ src_a, double* __restrict__ hat_a, int lda );
/**
* @brief Inner kernel for GEMM (row major order)
*
* \hat C : m_r * n_r
* \hat A : m_r * k_b
* \hat B : k_b * n_r
*
* \hat C += \hat A * \hat B
*
* m_r : 31
* n_r : 8
* k_b : not required to be hardcode by this function
*/
inline void inner_kernel( double* __restrict__ hat_a, \
double* __restrict__ hat_b, \
double* __restrict__ hat_c, \
int ldc )
{
__m512d R00, R01, R02, R03, R04, R05, R06, R07, R08, R09, \
R10, R11, R12, R13, R14, R15, R16, R17, R18, R19, \
R20, R21, R22, R23, R24, R25, R26, R27, R28, R29, \
R30, R31;
R00 = _mm512_setzero_pd();
R01 = _mm512_setzero_pd();
R02 = _mm512_setzero_pd();
R03 = _mm512_setzero_pd();
R04 = _mm512_setzero_pd();
R05 = _mm512_setzero_pd();
R06 = _mm512_setzero_pd();
R07 = _mm512_setzero_pd();
R08 = _mm512_setzero_pd();
R09 = _mm512_setzero_pd();
R10 = _mm512_setzero_pd();
R11 = _mm512_setzero_pd();
R12 = _mm512_setzero_pd();
R13 = _mm512_setzero_pd();
R14 = _mm512_setzero_pd();
R15 = _mm512_setzero_pd();
R16 = _mm512_setzero_pd();
R17 = _mm512_setzero_pd();
R18 = _mm512_setzero_pd();
R19 = _mm512_setzero_pd();
R20 = _mm512_setzero_pd();
R21 = _mm512_setzero_pd();
R22 = _mm512_setzero_pd();
R23 = _mm512_setzero_pd();
R24 = _mm512_setzero_pd();
R25 = _mm512_setzero_pd();
R26 = _mm512_setzero_pd();
R27 = _mm512_setzero_pd();
R28 = _mm512_setzero_pd();
R29 = _mm512_setzero_pd();
R30 = _mm512_setzero_pd();
// R00 = _mm512_loadu_pd( (void*)(hat_c + 0 * ldc) );
// R01 = _mm512_loadu_pd( (void*)(hat_c + 1 * ldc) );
// R02 = _mm512_loadu_pd( (void*)(hat_c + 2 * ldc) );
// R03 = _mm512_loadu_pd( (void*)(hat_c + 3 * ldc) );
// R04 = _mm512_loadu_pd( (void*)(hat_c + 4 * ldc) );
// R05 = _mm512_loadu_pd( (void*)(hat_c + 5 * ldc) );
// R06 = _mm512_loadu_pd( (void*)(hat_c + 6 * ldc) );
// R07 = _mm512_loadu_pd( (void*)(hat_c + 7 * ldc) );
// R08 = _mm512_loadu_pd( (void*)(hat_c + 8 * ldc) );
// R09 = _mm512_loadu_pd( (void*)(hat_c + 9 * ldc) );
// R10 = _mm512_loadu_pd( (void*)(hat_c + 10 * ldc) );
// R11 = _mm512_loadu_pd( (void*)(hat_c + 11 * ldc) );
// R12 = _mm512_loadu_pd( (void*)(hat_c + 12 * ldc) );
// R13 = _mm512_loadu_pd( (void*)(hat_c + 13 * ldc) );
// R14 = _mm512_loadu_pd( (void*)(hat_c + 14 * ldc) );
// R15 = _mm512_loadu_pd( (void*)(hat_c + 15 * ldc) );
// R16 = _mm512_loadu_pd( (void*)(hat_c + 16 * ldc) );
// R17 = _mm512_loadu_pd( (void*)(hat_c + 17 * ldc) );
// R18 = _mm512_loadu_pd( (void*)(hat_c + 18 * ldc) );
// R19 = _mm512_loadu_pd( (void*)(hat_c + 19 * ldc) );
// R20 = _mm512_loadu_pd( (void*)(hat_c + 20 * ldc) );
// R21 = _mm512_loadu_pd( (void*)(hat_c + 21 * ldc) );
// R22 = _mm512_loadu_pd( (void*)(hat_c + 22 * ldc) );
// R23 = _mm512_loadu_pd( (void*)(hat_c + 23 * ldc) );
// R24 = _mm512_loadu_pd( (void*)(hat_c + 24 * ldc) );
// R25 = _mm512_loadu_pd( (void*)(hat_c + 25 * ldc) );
// R26 = _mm512_loadu_pd( (void*)(hat_c + 26 * ldc) );
// R27 = _mm512_loadu_pd( (void*)(hat_c + 27 * ldc) );
// R28 = _mm512_loadu_pd( (void*)(hat_c + 28 * ldc) );
// R29 = _mm512_loadu_pd( (void*)(hat_c + 29 * ldc) );
// R30 = _mm512_loadu_pd( (void*)(hat_c + 30 * ldc) );
UNROLL_LOOP( 3 )
for ( int i = 0; i < k_b; ++i )
{
// Software prefetch from L2 to L1 for \hat A \hat B
// Each _mm_prefetch load one cache line of data
// \hat A need to load m_r * 8 (size of double) / 64 (size of cache line) = 3.8 cache line
// \hat B need to load n_r * 8 / 64 = 1 cache line
// NOTE: if use _MM_HINT_T1 (prefetch to L2 & L3), there won't be any performence gain compared with no prefetch
// NOTE: if only one cache line of hat_a is prefetch, the performence will be lower, indicating prefetch multiple lines is nessary
// here the prefetch is prefetch next \hat A \hat B
_mm_prefetch( hat_a + 12 * m_r + 64 * 0, _MM_HINT_T0 );
_mm_prefetch( hat_a + 12 * m_r + 64 * 1, _MM_HINT_T0 );
_mm_prefetch( hat_a + 12 * m_r + 64 * 2, _MM_HINT_T0 );
_mm_prefetch( hat_a + 12 * m_r + 64 * 3, _MM_HINT_T0 );
_mm_prefetch( hat_b + 32 * n_r + 64 * 0, _MM_HINT_T0 );
R31 = _mm512_load_pd( hat_b );
// R00 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 0) ), R31, R00 );
// R01 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 1) ), R31, R01 );
// R02 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 2) ), R31, R02 );
// R03 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 3) ), R31, R03 );
// R04 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 4) ), R31, R04 );
// R05 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 5) ), R31, R05 );
// R06 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 6) ), R31, R06 );
// R07 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 7) ), R31, R07 );
// R08 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 8) ), R31, R08 );
// R09 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 9) ), R31, R09 );
// R10 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 10) ), R31, R10 );
// R11 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 11) ), R31, R11 );
// R12 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 12) ), R31, R12 );
// R13 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 13) ), R31, R13 );
// R14 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 14) ), R31, R14 );
// R15 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 15) ), R31, R15 );
// R16 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 16) ), R31, R16 );
// R17 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 17) ), R31, R17 );
// R18 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 18) ), R31, R18 );
// R19 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 19) ), R31, R19 );
// R20 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 20) ), R31, R20 );
// R21 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 21) ), R31, R21 );
// R22 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 22) ), R31, R22 );
// R23 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 23) ), R31, R23 );
// R24 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 24) ), R31, R24 );
// R25 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 25) ), R31, R25 );
// R26 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 26) ), R31, R26 );
// R27 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 27) ), R31, R27 );
// R28 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 28) ), R31, R28 );
// R29 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 29) ), R31, R29 );
// R30 = _mm512_fmadd_pd( _mm512_set1_pd( *(hat_a + 30) ), R31, R30 );
// NOTE: Although the mapping is the same, using asm assembly is around 1% faster than intrinsic
asm volatile(
"vfmadd231pd 0(%[hat_a])%{1to8}, %[R31], %[R00]\n\t"
"vfmadd231pd 8(%[hat_a])%{1to8}, %[R31], %[R01]\n\t"
"vfmadd231pd 16(%[hat_a])%{1to8}, %[R31], %[R02]\n\t"
"vfmadd231pd 24(%[hat_a])%{1to8}, %[R31], %[R03]\n\t"
"vfmadd231pd 32(%[hat_a])%{1to8}, %[R31], %[R04]\n\t"
"vfmadd231pd 40(%[hat_a])%{1to8}, %[R31], %[R05]\n\t"
"vfmadd231pd 48(%[hat_a])%{1to8}, %[R31], %[R06]\n\t"
"vfmadd231pd 56(%[hat_a])%{1to8}, %[R31], %[R07]\n\t"
"vfmadd231pd 64(%[hat_a])%{1to8}, %[R31], %[R08]\n\t"
"vfmadd231pd 72(%[hat_a])%{1to8}, %[R31], %[R09]\n\t"
: [R00] "+v" (R00),
[R01] "+v" (R01),
[R02] "+v" (R02),
[R03] "+v" (R03),
[R04] "+v" (R04),
[R05] "+v" (R05),
[R06] "+v" (R06),
[R07] "+v" (R07),
[R08] "+v" (R08),
[R09] "+v" (R09)
: [R31] "v" (R31),
[hat_a] "r" (hat_a)
: "memory"
);
asm volatile(
"vfmadd231pd 80(%[hat_a])%{1to8}, %[R31], %[R10]\n\t"
"vfmadd231pd 88(%[hat_a])%{1to8}, %[R31], %[R11]\n\t"
"vfmadd231pd 96(%[hat_a])%{1to8}, %[R31], %[R12]\n\t"
"vfmadd231pd 104(%[hat_a])%{1to8}, %[R31], %[R13]\n\t"
"vfmadd231pd 112(%[hat_a])%{1to8}, %[R31], %[R14]\n\t"
"vfmadd231pd 120(%[hat_a])%{1to8}, %[R31], %[R15]\n\t"
"vfmadd231pd 128(%[hat_a])%{1to8}, %[R31], %[R16]\n\t"
"vfmadd231pd 136(%[hat_a])%{1to8}, %[R31], %[R17]\n\t"
"vfmadd231pd 144(%[hat_a])%{1to8}, %[R31], %[R18]\n\t"
"vfmadd231pd 152(%[hat_a])%{1to8}, %[R31], %[R19]\n\t"
: [R10] "+v" (R10),
[R11] "+v" (R11),
[R12] "+v" (R12),
[R13] "+v" (R13),
[R14] "+v" (R14),
[R15] "+v" (R15),
[R16] "+v" (R16),
[R17] "+v" (R17),
[R18] "+v" (R18),
[R19] "+v" (R19)
: [R31] "v" (R31),
[hat_a] "r" (hat_a)
: "memory"
);
asm volatile(
"vfmadd231pd 160(%[hat_a])%{1to8}, %[R31], %[R20]\n\t"
"vfmadd231pd 168(%[hat_a])%{1to8}, %[R31], %[R21]\n\t"
"vfmadd231pd 176(%[hat_a])%{1to8}, %[R31], %[R22]\n\t"
"vfmadd231pd 184(%[hat_a])%{1to8}, %[R31], %[R23]\n\t"
"vfmadd231pd 192(%[hat_a])%{1to8}, %[R31], %[R24]\n\t"
"vfmadd231pd 200(%[hat_a])%{1to8}, %[R31], %[R25]\n\t"
"vfmadd231pd 208(%[hat_a])%{1to8}, %[R31], %[R26]\n\t"
"vfmadd231pd 216(%[hat_a])%{1to8}, %[R31], %[R27]\n\t"
"vfmadd231pd 224(%[hat_a])%{1to8}, %[R31], %[R28]\n\t"
"vfmadd231pd 232(%[hat_a])%{1to8}, %[R31], %[R29]\n\t"
"vfmadd231pd 240(%[hat_a])%{1to8}, %[R31], %[R30]\n\t"
: [R20] "+v" (R20),
[R21] "+v" (R21),
[R22] "+v" (R22),
[R23] "+v" (R23),
[R24] "+v" (R24),
[R25] "+v" (R25),
[R26] "+v" (R26),
[R27] "+v" (R27),
[R28] "+v" (R28),
[R29] "+v" (R29),
[R30] "+v" (R30)
: [R31] "v" (R31),
[hat_a] "r" (hat_a)
: "memory"
);
hat_a += m_r;
hat_b += n_r;
}
_mm512_storeu_pd( (void*)(hat_c + 0 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 0 * ldc) ), R00 ) );
_mm512_storeu_pd( (void*)(hat_c + 1 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 1 * ldc) ), R01 ) );
_mm512_storeu_pd( (void*)(hat_c + 2 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 2 * ldc) ), R02 ) );
_mm512_storeu_pd( (void*)(hat_c + 3 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 3 * ldc) ), R03 ) );
_mm512_storeu_pd( (void*)(hat_c + 4 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 4 * ldc) ), R04 ) );
_mm512_storeu_pd( (void*)(hat_c + 5 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 5 * ldc) ), R05 ) );
_mm512_storeu_pd( (void*)(hat_c + 6 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 6 * ldc) ), R06 ) );
_mm512_storeu_pd( (void*)(hat_c + 7 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 7 * ldc) ), R07 ) );
_mm512_storeu_pd( (void*)(hat_c + 8 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 8 * ldc) ), R08 ) );
_mm512_storeu_pd( (void*)(hat_c + 9 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 9 * ldc) ), R09 ) );
_mm512_storeu_pd( (void*)(hat_c + 10 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 10 * ldc) ), R10 ) );
_mm512_storeu_pd( (void*)(hat_c + 11 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 11 * ldc) ), R11 ) );
_mm512_storeu_pd( (void*)(hat_c + 12 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 12 * ldc) ), R12 ) );
_mm512_storeu_pd( (void*)(hat_c + 13 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 13 * ldc) ), R13 ) );
_mm512_storeu_pd( (void*)(hat_c + 14 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 14 * ldc) ), R14 ) );
_mm512_storeu_pd( (void*)(hat_c + 15 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 15 * ldc) ), R15 ) );
_mm512_storeu_pd( (void*)(hat_c + 16 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 16 * ldc) ), R16 ) );
_mm512_storeu_pd( (void*)(hat_c + 17 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 17 * ldc) ), R17 ) );
_mm512_storeu_pd( (void*)(hat_c + 18 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 18 * ldc) ), R18 ) );
_mm512_storeu_pd( (void*)(hat_c + 19 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 19 * ldc) ), R19 ) );
_mm512_storeu_pd( (void*)(hat_c + 20 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 20 * ldc) ), R20 ) );
_mm512_storeu_pd( (void*)(hat_c + 21 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 21 * ldc) ), R21 ) );
_mm512_storeu_pd( (void*)(hat_c + 22 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 22 * ldc) ), R22 ) );
_mm512_storeu_pd( (void*)(hat_c + 23 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 23 * ldc) ), R23 ) );
_mm512_storeu_pd( (void*)(hat_c + 24 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 24 * ldc) ), R24 ) );
_mm512_storeu_pd( (void*)(hat_c + 25 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 25 * ldc) ), R25 ) );
_mm512_storeu_pd( (void*)(hat_c + 26 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 26 * ldc) ), R26 ) );
_mm512_storeu_pd( (void*)(hat_c + 27 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 27 * ldc) ), R27 ) );
_mm512_storeu_pd( (void*)(hat_c + 28 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 28 * ldc) ), R28 ) );
_mm512_storeu_pd( (void*)(hat_c + 29 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 29 * ldc) ), R29 ) );
_mm512_storeu_pd( (void*)(hat_c + 30 * ldc), _mm512_add_pd( _mm512_loadu_pd( (void*)(hat_c + 30 * ldc) ), R30 ) );
// _mm512_storeu_pd( (void*)(hat_c + 0 * ldc), R00 );
// _mm512_storeu_pd( (void*)(hat_c + 1 * ldc), R01 );
// _mm512_storeu_pd( (void*)(hat_c + 2 * ldc), R02 );
// _mm512_storeu_pd( (void*)(hat_c + 3 * ldc), R03 );
// _mm512_storeu_pd( (void*)(hat_c + 4 * ldc), R04 );
// _mm512_storeu_pd( (void*)(hat_c + 5 * ldc), R05 );
// _mm512_storeu_pd( (void*)(hat_c + 6 * ldc), R06 );
// _mm512_storeu_pd( (void*)(hat_c + 7 * ldc), R07 );
// _mm512_storeu_pd( (void*)(hat_c + 8 * ldc), R08 );
// _mm512_storeu_pd( (void*)(hat_c + 9 * ldc), R09 );
// _mm512_storeu_pd( (void*)(hat_c + 10 * ldc), R10 );
// _mm512_storeu_pd( (void*)(hat_c + 11 * ldc), R11 );
// _mm512_storeu_pd( (void*)(hat_c + 12 * ldc), R12 );
// _mm512_storeu_pd( (void*)(hat_c + 13 * ldc), R13 );
// _mm512_storeu_pd( (void*)(hat_c + 14 * ldc), R14 );
// _mm512_storeu_pd( (void*)(hat_c + 15 * ldc), R15 );
// _mm512_storeu_pd( (void*)(hat_c + 16 * ldc), R16 );
// _mm512_storeu_pd( (void*)(hat_c + 17 * ldc), R17 );
// _mm512_storeu_pd( (void*)(hat_c + 18 * ldc), R18 );
// _mm512_storeu_pd( (void*)(hat_c + 19 * ldc), R19 );
// _mm512_storeu_pd( (void*)(hat_c + 20 * ldc), R20 );
// _mm512_storeu_pd( (void*)(hat_c + 21 * ldc), R21 );
// _mm512_storeu_pd( (void*)(hat_c + 22 * ldc), R22 );
// _mm512_storeu_pd( (void*)(hat_c + 23 * ldc), R23 );
// _mm512_storeu_pd( (void*)(hat_c + 24 * ldc), R24 );
// _mm512_storeu_pd( (void*)(hat_c + 25 * ldc), R25 );
// _mm512_storeu_pd( (void*)(hat_c + 26 * ldc), R26 );
// _mm512_storeu_pd( (void*)(hat_c + 27 * ldc), R27 );
// _mm512_storeu_pd( (void*)(hat_c + 28 * ldc), R28 );
// _mm512_storeu_pd( (void*)(hat_c + 29 * ldc), R29 );
// _mm512_storeu_pd( (void*)(hat_c + 30 * ldc), R30 );
}
/**
* @brief Pack k_b * n_r of submatrix B (row major order)
*
*/
void pack_b( double* __restrict__ src_b, double* __restrict__ pak_b, int ldb, int n )
{
for ( int row_i = 0; row_i < k_b; ++row_i )
{
double* src_b_row_i = src_b + row_i * ldb;
double* pak_b_row_i = pak_b + row_i * n_r;
UNROLL_LOOP( 4 )
for ( int n_r_i = 0; n_r_i < (n / n_r); ++n_r_i )
{
double* src_b_row_n_r_i = src_b_row_i + n_r_i * n_r;
double* pak_b_row_n_r_i = pak_b_row_i + n_r_i * k_b * n_r;
UNROLL_LOOP( n_r )
for ( int col_i = 0; col_i < n_r; ++col_i )
{
*(pak_b_row_n_r_i + col_i) = *(src_b_row_n_r_i + col_i);
}
}
}
}
/**
* @brief Pack m_b * k_b of submatrix A (row major order)
*
*/
void pack_a( double* __restrict__ src_a, double* __restrict__ pak_a, int lda )
{
for ( int m_r_i = 0; m_r_i < (m_b / m_r); ++m_r_i )
{
double* src_a_row_m_r_i = src_a + m_r_i * m_r * lda;
double* pak_a_row_m_r_i = pak_a + m_r_i * m_r * k_b;
UNROLL_LOOP( 4 )
for ( int row_i = 0; row_i < m_r; ++row_i )
{
double* src_a_row_i = src_a_row_m_r_i + row_i * lda;
double* pak_a_row_i = pak_a_row_m_r_i + row_i;
UNROLL_LOOP( 8 * 4 )
for ( int col_i = 0; col_i < k_b; ++col_i )
{
*(pak_a_row_i + col_i * m_r) = *(src_a_row_i + col_i);
}
}
}
}
/**
* @brief DGEMM on KNL Node (row major order)
* A : m * k
* B : k * n
* C : m * n
*/
void dgemm_knl( int m, int k, int n, \
double* src_a, double* src_b, double* src_c, \
int lda, int ldb, int ldc )
{
// Memory for \tilde a and \tilde b
double* pak_a = (double*)_mm_malloc( m_b * k_b * sizeof( double ), 64 );
double* pak_b = (double*)_mm_malloc( k_b * n * sizeof( double ), 64 );
for ( int k_b_i = 0; k_b_i < k / k_b; k_b_i++)
{
// Pack \tilde b
pack_b( src_b + k_b_i * k_b * ldb, pak_b, ldb, n );
for ( int m_b_i = 0; m_b_i < m / m_b; m_b_i++ )
{
// Pack \tilde a
pack_a( src_a + m_b_i * m_b * lda + k_b_i * k_b, pak_a, lda );
for ( int n_r_i = 0; n_r_i < n / n_r; n_r_i++ )
{
for ( int m_r_i = 0; m_r_i < m_b / m_r; m_r_i++ )
{
// Inner Kernel (register blocking)
inner_kernel( pak_a + m_r_i * m_r * k_b, \
pak_b + n_r_i * n_r * k_b, \
src_c + m_b_i * m_b * ldc + m_r_i * m_r * ldc + n_r_i * n_r, \
ldc );
}
}
}
}
_mm_free( pak_a );
_mm_free( pak_b );
}