-
Notifications
You must be signed in to change notification settings - Fork 0
/
src.jl
545 lines (519 loc) · 24.5 KB
/
src.jl
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
using Unicode
using SIMD
# macro to generate simple three-nested-loop implementations
macro def_gemm_xyz!(xyz)
x, y, z = Symbol.(graphemes(String(xyz)))
indexrange(q) =
q == :i ? :(1:size(C, 1)) :
q == :j ? :(1:size(C, 2)) :
q == :p ? :(1:size(A, 2)) :
throw(ArgumentError("$q"))
return quote
function $(esc(Symbol(:gemm_, xyz, :!)))(C, A, B)
for $(x) in $(indexrange(x)),
$(y) in $(indexrange(y)),
$(z) in $(indexrange(z))
@inbounds C[i, j] += A[i, p] * B[p, j]
end
return C
end
end
end
@def_gemm_xyz! jip # defines gemm_jip!(C, A, B)
# non-packing gemm implementation
function gemm_nonpacking!(C, A, B, (cacheM, cacheK, cacheN), ::Val{microM}, ::Val{microN}) where {microM,microN}
m = size(C, 1)
k = size(A, 2)
n = size(C, 2)
# cache-blocking loops
for cachejstart in 1:cacheN:n; cachejend = min(cachejstart + cacheN - 1, n)
for cachepstart in 1:cacheK:k; cachepend = min(cachepstart + cacheK - 1, k)
for cacheistart in 1:cacheM:m; cacheiend = min(cacheistart + cacheM - 1, m)
# macrokernel loops
for macrojstart in cachejstart:microN:cachejend
for macroistart in cacheistart:microM:cacheiend
gemm_nonpacking_microkernel!(C, A, B,
macroistart, macrojstart,
cachepstart, cachepend,
Val(microM), Val(microN))
end
end
end
end
end
return C
end
@inline function gemm_nonpacking_microkernel!(C, A, B, i, j, pstart, pend, ::Val{12}, ::Val{4})
T = eltype(C)
m = size(C, 1)
# load 12x4 block Cij into vector registers
ptrCij = pointer(C, (j - 1)*m + i) # pointer to C[i, j]
# load Cij[1:12, 1]
Cij_r1c1vec = vload(Vec{4,T}, ptrCij ) # C[i , j]
Cij_r2c1vec = vload(Vec{4,T}, ptrCij + 4*sizeof(T)) # C[i+4, j]
Cij_r3c1vec = vload(Vec{4,T}, ptrCij + 8*sizeof(T)) # C[i+8, j]
# load Cij[1:12, 2]
Cij_r1c2vec = vload(Vec{4,T}, ptrCij + m*sizeof(T)) # C[i , j+1]
Cij_r2c2vec = vload(Vec{4,T}, ptrCij + (4 + m)*sizeof(T)) # C[i+4, j+1]
Cij_r3c2vec = vload(Vec{4,T}, ptrCij + (8 + m)*sizeof(T)) # C[i+8, j+1]
# load Cij[1:12, 3]
Cij_r1c3vec = vload(Vec{4,T}, ptrCij + 2m*sizeof(T)) # C[i , j+2]
Cij_r2c3vec = vload(Vec{4,T}, ptrCij + (4 + 2m)*sizeof(T)) # C[i+4, j+2]
Cij_r3c3vec = vload(Vec{4,T}, ptrCij + (8 + 2m)*sizeof(T)) # C[i+8, j+2]
# load Cij[1:12, 4]
Cij_r1c4vec = vload(Vec{4,T}, ptrCij + 3m*sizeof(T)) # C[i , j+3]
Cij_r2c4vec = vload(Vec{4,T}, ptrCij + (4 + 3m)*sizeof(T)) # C[i+4, j+3]
Cij_r3c4vec = vload(Vec{4,T}, ptrCij + (8 + 3m)*sizeof(T)) # C[i+8, j+3]
# update Cij with series of outer products
# from the associated Aip and Bpj micropanels
for p in pstart:pend
# update with A[i:(i+11), p] * B[p, j:(j+3)] outer product
ptrAip = pointer(A, (p - 1)*m + i) # pointer to A[i, p]
Aip_vec1 = vload(Vec{4,T}, ptrAip)
Aip_vec2 = vload(Vec{4,T}, ptrAip + 4*sizeof(T))
Aip_vec3 = vload(Vec{4,T}, ptrAip + 8*sizeof(T))
# Cij[1:12, 1] += A[i:(i+11), p] * B[p, j]
Bpj_c1vec = Vec{4,T}(@inbounds B[p, j])
Cij_r1c1vec = fma(Aip_vec1, Bpj_c1vec, Cij_r1c1vec)
Cij_r2c1vec = fma(Aip_vec2, Bpj_c1vec, Cij_r2c1vec)
Cij_r3c1vec = fma(Aip_vec3, Bpj_c1vec, Cij_r3c1vec)
# Cij[1:12, 2] += A[i:(i+11), p] * B[p, j+1]
Bpj_c2vec = Vec{4,T}(@inbounds B[p, j+1])
Cij_r1c2vec = fma(Aip_vec1, Bpj_c2vec, Cij_r1c2vec)
Cij_r2c2vec = fma(Aip_vec2, Bpj_c2vec, Cij_r2c2vec)
Cij_r3c2vec = fma(Aip_vec3, Bpj_c2vec, Cij_r3c2vec)
# Cij[1:12, 3] += A[i:(i+11), p] * B[p, j+2]
Bpj_c3vec = Vec{4,T}(@inbounds B[p, j+2])
Cij_r1c3vec = fma(Aip_vec1, Bpj_c3vec, Cij_r1c3vec)
Cij_r2c3vec = fma(Aip_vec2, Bpj_c3vec, Cij_r2c3vec)
Cij_r3c3vec = fma(Aip_vec3, Bpj_c3vec, Cij_r3c3vec)
# Cij[1:12, 4] += A[i:(i+11), p] * B[p, j+3]
Bpj_c4vec = Vec{4,T}(@inbounds B[p, j+3])
Cij_r1c4vec = fma(Aip_vec1, Bpj_c4vec, Cij_r1c4vec)
Cij_r2c4vec = fma(Aip_vec2, Bpj_c4vec, Cij_r2c4vec)
Cij_r3c4vec = fma(Aip_vec3, Bpj_c4vec, Cij_r3c4vec)
end
# store Cij[1:12, 1]
vstore(Cij_r1c1vec, ptrCij ) # C[i , j]
vstore(Cij_r2c1vec, ptrCij + 4*sizeof(T)) # C[i+4, j]
vstore(Cij_r3c1vec, ptrCij + 8*sizeof(T)) # C[i+8, j]
# store Cij[1:12, 2]
vstore(Cij_r1c2vec, ptrCij + m*sizeof(T)) # C[i , j+1]
vstore(Cij_r2c2vec, ptrCij + (4 + m)*sizeof(T)) # C[i+4, j+1]
vstore(Cij_r3c2vec, ptrCij + (8 + m)*sizeof(T)) # C[i+8, j+1]
# store Cij[1:12, 3]
vstore(Cij_r1c3vec, ptrCij + 2m*sizeof(T)) # C[i , j+2]
vstore(Cij_r2c3vec, ptrCij + (4 + 2m)*sizeof(T)) # C[i+4, j+2]
vstore(Cij_r3c3vec, ptrCij + (8 + 2m)*sizeof(T)) # C[i+8, j+2]
# store Cij[1:12, 4]
vstore(Cij_r1c4vec, ptrCij + 3m*sizeof(T)) # C[i , j+3]
vstore(Cij_r2c4vec, ptrCij + (4 + 3m)*sizeof(T)) # C[i+4, j+3]
vstore(Cij_r3c4vec, ptrCij + (8 + 3m)*sizeof(T)) # C[i+8, j+3]
return nothing
end
# packing gemm implementation
function gemm_packing!(C, A, B, Abuff, Bbuff, (cacheM, cacheK, cacheN), ::Val{microM}, ::Val{microN}) where {microM,microN}
m = size(C, 1)
k = size(A, 2)
n = size(C, 2)
# cache-blocking loops
for cachejstart in 1:cacheN:n; cachejend = min(cachejstart + cacheN - 1, n)
for cachepstart in 1:cacheK:k; cachepend = min(cachepstart + cacheK - 1, k)
packBbuffer!(Bbuff, B, cachepstart, cachepend, cachejstart, cachejend, microN)
for cacheistart in 1:cacheM:m; cacheiend = min(cacheistart + cacheM - 1, m)
packAbuffer!(Abuff, A, cacheistart, cacheiend, cachepstart, cachepend, microM)
# macrokernel loops
for macrojstart in cachejstart:microN:cachejend
for macroistart in cacheistart:microM:cacheiend
gemm_packing_microkernel!(C, Abuff, Bbuff,
macroistart, cacheistart,
macrojstart, cachejstart,
cachepstart, cachepend,
Val(microM), Val(microN))
end
end
end
end
end
return C
end
@inline function gemm_packing_microkernel!(C, Abuff, Bbuff,
istart, blockistart,
jstart, blockjstart,
blockpstart, blockpend,
::Val{12}, ::Val{4})
microM, microN = 12, 4
T = eltype(C)
m = size(C, 1)
# load 12x4 block Cij into vector registers
Cstart = (jstart - 1)*m + istart
ptrCij = pointer(C, Cstart) # pointer to C[i, j]
# load Cij[1:12, 1]
Cij_r1c1vec = vload(Vec{4,T}, ptrCij ) # C[i , j]
Cij_r2c1vec = vload(Vec{4,T}, ptrCij + 4*sizeof(T)) # C[i+4, j]
Cij_r3c1vec = vload(Vec{4,T}, ptrCij + 8*sizeof(T)) # C[i+8, j]
# load Cij[1:12, 2]
Cij_r1c2vec = vload(Vec{4,T}, ptrCij + m*sizeof(T)) # C[i , j+1]
Cij_r2c2vec = vload(Vec{4,T}, ptrCij + (4 + m)*sizeof(T)) # C[i+4, j+1]
Cij_r3c2vec = vload(Vec{4,T}, ptrCij + (8 + m)*sizeof(T)) # C[i+8, j+1]
# load Cij[:, 3]
Cij_r1c3vec = vload(Vec{4,T}, ptrCij + 2m*sizeof(T)) # C[i , j+2]
Cij_r2c3vec = vload(Vec{4,T}, ptrCij + (4 + 2m)*sizeof(T)) # C[i+4, j+2]
Cij_r3c3vec = vload(Vec{4,T}, ptrCij + (8 + 2m)*sizeof(T)) # C[i+8, j+2]
# load Cij[:, 4]
Cij_r1c4vec = vload(Vec{4,T}, ptrCij + 3m*sizeof(T)) # C[i , j+3]
Cij_r2c4vec = vload(Vec{4,T}, ptrCij + (4 + 3m)*sizeof(T)) # C[i+4, j+3]
Cij_r3c4vec = vload(Vec{4,T}, ptrCij + (8 + 3m)*sizeof(T)) # C[i+8, j+3]
blockplength = blockpend - blockpstart + 1
Abuffstart = (istart - blockistart)*blockplength + 1
Bbuffstart = (jstart - blockjstart)*blockplength + 1
ptrAbuffpos = pointer(Abuff, Abuffstart)
indBbuffpos = Bbuffstart
# update Cij with series of outer products
# from the associated Aip and Bpj micropanels
for p in blockpstart:blockpend
# update with A[i:(i+11), p] * B[p, j:(j+3)] outer product
# ptrAip = pointer(A, (p - 1)*m + i) # pointer to A[i, p]
Aip_vec1 = vload(Vec{4,T}, ptrAbuffpos)
Aip_vec2 = vload(Vec{4,T}, ptrAbuffpos + 4*sizeof(T))
Aip_vec3 = vload(Vec{4,T}, ptrAbuffpos + 8*sizeof(T))
# Cij[1:12, 1] += A[i:(i+11), p] * B[p, j]
Bpj_c1vec = Vec{4,T}(@inbounds Bbuff[indBbuffpos])
Cij_r1c1vec = fma(Aip_vec1, Bpj_c1vec, Cij_r1c1vec)
Cij_r2c1vec = fma(Aip_vec2, Bpj_c1vec, Cij_r2c1vec)
Cij_r3c1vec = fma(Aip_vec3, Bpj_c1vec, Cij_r3c1vec)
# Cij[1:12, 2] += A[i:(i+11), p] * B[p, j+1]
Bpj_c2vec = Vec{4,T}(@inbounds Bbuff[indBbuffpos + 1])
Cij_r1c2vec = fma(Aip_vec1, Bpj_c2vec, Cij_r1c2vec)
Cij_r2c2vec = fma(Aip_vec2, Bpj_c2vec, Cij_r2c2vec)
Cij_r3c2vec = fma(Aip_vec3, Bpj_c2vec, Cij_r3c2vec)
# Cij[1:12, 3] += A[i:(i+11), p] * B[p, j+2]
Bpj_c3vec = Vec{4,T}(@inbounds Bbuff[indBbuffpos + 2])
Cij_r1c3vec = fma(Aip_vec1, Bpj_c3vec, Cij_r1c3vec)
Cij_r2c3vec = fma(Aip_vec2, Bpj_c3vec, Cij_r2c3vec)
Cij_r3c3vec = fma(Aip_vec3, Bpj_c3vec, Cij_r3c3vec)
# Cij[1:12, 4] += A[i:(i+11), p] * B[p, j+3]
Bpj_c4vec = Vec{4,T}(@inbounds Bbuff[indBbuffpos + 3])
Cij_r1c4vec = fma(Aip_vec1, Bpj_c4vec, Cij_r1c4vec)
Cij_r2c4vec = fma(Aip_vec2, Bpj_c4vec, Cij_r2c4vec)
Cij_r3c4vec = fma(Aip_vec3, Bpj_c4vec, Cij_r3c4vec)
ptrAbuffpos += microM*sizeof(T)
indBbuffpos += microN
end
# store Cij[:, 1]
vstore(Cij_r1c1vec, ptrCij ) # C[i , j]
vstore(Cij_r2c1vec, ptrCij + 4*sizeof(T)) # C[i+4, j]
vstore(Cij_r3c1vec, ptrCij + 8*sizeof(T)) # C[i+8, j]
# store Cij[:, 2]
vstore(Cij_r1c2vec, ptrCij + m*sizeof(T)) # C[i , j+1]
vstore(Cij_r2c2vec, ptrCij + (4 + m)*sizeof(T)) # C[i+4, j+1]
vstore(Cij_r3c2vec, ptrCij + (8 + m)*sizeof(T)) # C[i+8, j+1]
# store Cij[:, 3]
vstore(Cij_r1c3vec, ptrCij + 2m*sizeof(T)) # C[i , j+2]
vstore(Cij_r2c3vec, ptrCij + (4 + 2m)*sizeof(T)) # C[i+4, j+2]
vstore(Cij_r3c3vec, ptrCij + (8 + 2m)*sizeof(T)) # C[i+8, j+2]
# store Cij[:, 4]
vstore(Cij_r1c4vec, ptrCij + 3m*sizeof(T)) # C[i , j+3]
vstore(Cij_r2c4vec, ptrCij + (4 + 3m)*sizeof(T)) # C[i+4, j+3]
vstore(Cij_r3c4vec, ptrCij + (8 + 3m)*sizeof(T)) # C[i+8, j+3]
return nothing
end
@inline function gemm_packing_microkernel!(C, Abuff, Bbuff,
istart, blockistart,
jstart, blockjstart,
blockpstart, blockpend,
::Val{8}, ::Val{6})
microM, microN = 8, 6
T = eltype(C)
m = size(C, 1)
# load 8x6 block Cij into vector registers
Cstart = (jstart - 1)*m + istart
ptrCij = pointer(C, Cstart) # pointer to C[i, j]
# load Cij[:, 1]
Cij_r1c1vec = vload(Vec{4,T}, ptrCij ) # C[i , j]
Cij_r2c1vec = vload(Vec{4,T}, ptrCij + 4*sizeof(T)) # C[i+4, j]
# load Cij[:, 2]
Cij_r1c2vec = vload(Vec{4,T}, ptrCij + m*sizeof(T)) # C[i , j+1]
Cij_r2c2vec = vload(Vec{4,T}, ptrCij + (4 + m)*sizeof(T)) # C[i+4, j+1]
# load Cij[:, 3]
Cij_r1c3vec = vload(Vec{4,T}, ptrCij + 2m*sizeof(T)) # C[i , j+2]
Cij_r2c3vec = vload(Vec{4,T}, ptrCij + (4 + 2m)*sizeof(T)) # C[i+4, j+2]
# load Cij[:, 4]
Cij_r1c4vec = vload(Vec{4,T}, ptrCij + 3m*sizeof(T)) # C[i , j+3]
Cij_r2c4vec = vload(Vec{4,T}, ptrCij + (4 + 3m)*sizeof(T)) # C[i+4, j+3]
# load Cij[:, 5]
Cij_r1c5vec = vload(Vec{4,T}, ptrCij + 4m*sizeof(T)) # C[i , j+4]
Cij_r2c5vec = vload(Vec{4,T}, ptrCij + (4 + 4m)*sizeof(T)) # C[i+4, j+4]
# load Cij[:, 6]
Cij_r1c6vec = vload(Vec{4,T}, ptrCij + 5m*sizeof(T)) # C[i , j+5]
Cij_r2c6vec = vload(Vec{4,T}, ptrCij + (4 + 5m)*sizeof(T)) # C[i+4, j+6]
blockplength = blockpend - blockpstart + 1
Abuffstart = (istart - blockistart)*blockplength + 1
Bbuffstart = (jstart - blockjstart)*blockplength + 1
ptrAbuffpos = pointer(Abuff, Abuffstart)
indBbuffpos = Bbuffstart
# update Cij with series of outer products
# from the associated Aip and Bpj micropanels
for p in blockpstart:blockpend
# update with A[i:(i+7), p] * B[p, j:(j+5)] outer product
# ptrAip = pointer(A, (p - 1)*m + i) # pointer to A[i, p]
Aip_vec1 = vload(Vec{4,T}, ptrAbuffpos)
Aip_vec2 = vload(Vec{4,T}, ptrAbuffpos + 4*sizeof(T))
# Cij[1:8, 1] += A[i:(i+7), p] * B[p, j]
Bpj_c1vec = Vec{4,T}(@inbounds Bbuff[indBbuffpos])
Cij_r1c1vec = fma(Aip_vec1, Bpj_c1vec, Cij_r1c1vec)
Cij_r2c1vec = fma(Aip_vec2, Bpj_c1vec, Cij_r2c1vec)
# Cij[1:8, 2] += A[i:(i+7), p] * B[p, j+1]
Bpj_c2vec = Vec{4,T}(@inbounds Bbuff[indBbuffpos + 1])
Cij_r1c2vec = fma(Aip_vec1, Bpj_c2vec, Cij_r1c2vec)
Cij_r2c2vec = fma(Aip_vec2, Bpj_c2vec, Cij_r2c2vec)
# Cij[1:8, 3] += A[i:(i+7), p] * B[p, j+2]
Bpj_c3vec = Vec{4,T}(@inbounds Bbuff[indBbuffpos + 2])
Cij_r1c3vec = fma(Aip_vec1, Bpj_c3vec, Cij_r1c3vec)
Cij_r2c3vec = fma(Aip_vec2, Bpj_c3vec, Cij_r2c3vec)
# Cij[1:8, 4] += A[i:(i+7), p] * B[p, j+3]
Bpj_c4vec = Vec{4,T}(@inbounds Bbuff[indBbuffpos + 3])
Cij_r1c4vec = fma(Aip_vec1, Bpj_c4vec, Cij_r1c4vec)
Cij_r2c4vec = fma(Aip_vec2, Bpj_c4vec, Cij_r2c4vec)
# Cij[1:8, 5] += A[i:(i+7), p] * B[p, j+4]
Bpj_c5vec = Vec{4,T}(@inbounds Bbuff[indBbuffpos + 4])
Cij_r1c5vec = fma(Aip_vec1, Bpj_c5vec, Cij_r1c5vec)
Cij_r2c5vec = fma(Aip_vec2, Bpj_c5vec, Cij_r2c5vec)
# Cij[1:8, 6] += A[i:(i+7), p] * B[p, j+5]
Bpj_c6vec = Vec{4,T}(@inbounds Bbuff[indBbuffpos + 5])
Cij_r1c6vec = fma(Aip_vec1, Bpj_c6vec, Cij_r1c6vec)
Cij_r2c6vec = fma(Aip_vec2, Bpj_c6vec, Cij_r2c6vec)
ptrAbuffpos += microM*sizeof(T)
indBbuffpos += microN
end
# store Cij[:, 1]
vstore(Cij_r1c1vec, ptrCij ) # C[i , j]
vstore(Cij_r2c1vec, ptrCij + 4*sizeof(T)) # C[i+4, j]
# store Cij[:, 2]
vstore(Cij_r1c2vec, ptrCij + m*sizeof(T)) # C[i , j+1]
vstore(Cij_r2c2vec, ptrCij + (4 + m)*sizeof(T)) # C[i+4, j+1]
# store Cij[:, 3]
vstore(Cij_r1c3vec, ptrCij + 2m*sizeof(T)) # C[i , j+2]
vstore(Cij_r2c3vec, ptrCij + (4 + 2m)*sizeof(T)) # C[i+4, j+2]
# store Cij[:, 4]
vstore(Cij_r1c4vec, ptrCij + 3m*sizeof(T)) # C[i , j+3]
vstore(Cij_r2c4vec, ptrCij + (4 + 3m)*sizeof(T)) # C[i+4, j+3]
# store Cij[:, 5]
vstore(Cij_r1c5vec, ptrCij + 4m*sizeof(T)) # C[i , j+4]
vstore(Cij_r2c5vec, ptrCij + (4 + 4m)*sizeof(T)) # C[i+4, j+4]
# store Cij[:, 6]
vstore(Cij_r1c6vec, ptrCij + 5m*sizeof(T)) # C[i , j+5]
vstore(Cij_r2c6vec, ptrCij + (4 + 5m)*sizeof(T)) # C[i+4, j+5]
return nothing
end
function packAbuffer!(Abuff, A, blockistart, blockiend, blockpstart, blockpend, microM)
l = 0 # write position in packing buffer
for panelistart in blockistart:microM:blockiend # iterate over row panels
paneliend = panelistart + microM - 1
if paneliend <= blockiend # row panel is full height
for p in blockpstart:blockpend # iterate through panel cols
for i in panelistart:paneliend # iterate though panel rows
@inbounds Abuff[l += 1] = A[i, p]
end
end
else # row panel is not full height
for p in blockpstart:blockpend # iterate through panel cols
for i in panelistart:blockiend # iterate through live panel rows
@inbounds Abuff[l += 1] = A[i, p]
end
for i in (blockiend + 1):paneliend
@inbounds Abuff[l += 1] = zero(eltype(Abuff))
end
end
end
end
return nothing
end
function packBbuffer!(Bbuff, B, blockpstart, blockpend, blockjstart, blockjend, microN)
l = 0 # write position in packing buffer
for paneljstart in blockjstart:microN:blockjend # iterate over column panels
paneljend = paneljstart + microN - 1
if paneljend <= blockjend # column panel is full width
for p in blockpstart:blockpend # iterate through panel rows
for j in paneljstart:paneljend # iterate through panel cols
@inbounds Bbuff[l += 1] = B[p, j]
end
end
else # column panel is not full width
for p in blockpstart:blockpend # iterate through panel rows
for j in paneljstart:blockjend # iterate through live panel cols
@inbounds Bbuff[l += 1] = B[p, j]
end
for j in (blockjend + 1):paneljend # zero pad for absent cols
@inbounds Bbuff[l += 1] = zero(eltype(Bbuff))
end
end
end
end
return nothing
end
# select implementation (packing, non-packing) dependent upon whether the problem roughly fits in L3 caches
gemm_switchpacking!(C, A, B, Abuff, Bbuff, cachePsnp, cachePsp, ::Val{microM}, ::Val{microN}) where {microM,microN} =
(sizeof(C) + sizeof(A) + sizeof(B)) < 4*2^20 ?
gemm_nonpacking!(C, A, B, cachePsnp, Val(microM), Val(microN)) :
gemm_packing!(C, A, B, Abuff, Bbuff, cachePsp, Val(microM), Val(microN))
# check correctness
using Test
let
m, n, k = 48 .* (3, 2, 1)
C = rand(m, n)
A = rand(m, k)
B = rand(k, n)
Cref = A * B
# simple three-nested-loop impelmentations
@test gemm_jip!(fill!(C, 0), A, B) ≈ Cref
# non-packing implementation
microM, microN = 12, 4
cacheM, cacheN, cacheK = 72, 72, 4080
cachePsnp = (cacheM, cacheN, cacheK)
@test gemm_nonpacking!(fill!(C, 0), A, B, cachePsnp, Val(microM), Val(microN)) ≈ Cref
# packing implementation, 12x4
microM, microN = 12, 4
cacheM, cacheN, cacheK = 72, 192, 4080
cachePsp = (cacheM, cacheN, cacheK)
Abuff = zeros(cacheM*cacheK)
Bbuff = zeros(cacheK*cacheN)
@test gemm_packing!(fill!(C, 0), A, B, Abuff, Bbuff, cachePsp, Val(microM), Val(microN)) ≈ Cref
# hybrid
@test gemm_switchpacking!(fill!(C, 0), A, B, Abuff, Bbuff, cachePsnp, cachePsp, Val(microM), Val(microN)) ≈ Cref
# packing implementation, 8x6
microM, microN = 8, 6
cacheM, cacheN, cacheK = 72, 192, 4080
cachePsp = (cacheM, cacheN, cacheK)
Abuff = zeros(cacheM*cacheK)
Bbuff = zeros(cacheK*cacheN)
@test gemm_packing!(fill!(C, 0), A, B, Abuff, Bbuff, cachePsp, Val(microM), Val(microN)) ≈ Cref
end
# generate benchmark data
using LinearAlgebra
using BenchmarkTools
LinearAlgebra.BLAS.set_num_threads(1) # single-thread OpenBLAS
BenchmarkTools.DEFAULT_PARAMETERS.seconds = 0.5 # constrain bench time
mnks = 48*(1:2:30)
foo = zeros(length(mnks));
# reference implementation (openblas)
timings_ref = copy(foo);
# three-nested-loop implementation
timings_jip = copy(foo);
# non-packing implementation
timings_nonpacking_72x72x4080_12x4 = copy(foo);
# packing implementation
timings_packing_72x192x4080_12x4 = copy(foo);
# timings_packing_72x192x4080_8x6 = copy(foo)
# switch packing implementations
timings_switchpacking_12x4 = copy(foo);
# timings_switchpacking_8x6 = copy(foo)
for (mnkind, mnk) in enumerate(mnks)
A = rand(mnk, mnk)
B = rand(mnk, mnk)
C = rand(mnk, mnk)
print("Benchmarking with $mnk-by-$mnk matrices..."); @time begin
# reference implementation (openblas)
timings_ref[mnkind] = @belapsed mul!($C, $A, $B)
# three-nested-loop implementation
timings_jip[mnkind] = @belapsed gemm_jip!($C, $A, $B)
# non-packing implementation
microM, microN = 12, 4
cacheM, cacheN, cacheK = 72, 72, 4080
cachePsnp = (cacheM, cacheN, cacheK)
timings_nonpacking_72x72x4080_12x4[mnkind] =
@belapsed gemm_nonpacking!($C, $A, $B, $cachePsnp, $(Val(microM)), $(Val(microN)))
# packed implementation, 12x4
microM, microN = 12, 4
cacheM, cacheN, cacheK = 72, 192, 4080
cachePsp = (cacheM, cacheN, cacheK)
Abuff = zeros(cacheM*cacheK)
Bbuff = zeros(cacheK*cacheN)
timings_packing_72x192x4080_12x4[mnkind] =
@belapsed gemm_packing!($C, $A, $B, $Abuff, $Bbuff, $cachePsp, $(Val(microM)), $(Val(microN)))
# # packed implementation, 8x6
# microM, microN = 8, 6
# cacheM, cacheN, cacheK = 72, 192, 4080
# cachePsp = (cacheM, cacheN, cacheK)
# Abuff = zeros(cacheM*cacheK)
# Bbuff = zeros(cacheK*cacheN)
# timings_packing_72x192x4080_8x6[mnkind] =
# @belapsed gemm_packing!($C, $A, $B, $Abuff, $Bbuff, $cachePsp, $(Val(microM)), $(Val(microN)))
# packing switch implementation
microM, microN = 12, 4
Abuff = zeros(cacheM*cacheK)
Bbuff = zeros(cacheK*cacheN)
timings_switchpacking_12x4[mnkind] =
@belapsed gemm_switchpacking!($C, $A, $B, $Abuff, $Bbuff, $cachePsnp, $cachePsp, $(Val(microM)), $(Val(microN)))
end
end;
# visualize benchmark data
using Gaston
Gaston.set(linewidth = 4)
Gaston.set(terminal = "qt")
Gaston.gnuplot_send("set term qt font \"sans,14\"")
tableaurgb = ((255, 158, 74), (237, 102, 93),
(173, 139, 201), (114, 158, 206), (103, 191, 92), (237, 151, 202),
(205, 204, 93), (168, 120, 110), (162, 162, 162), (109, 204, 218))
tableauhex = (rgb -> string("0x", string.(rgb; base = 16, pad = 2)...)).(tableaurgb)
# visualize timings
ylims = extrema(vcat(timings_ref, timings_jip,
timings_nonpacking_72x72x4080_12x4,
timings_packing_72x192x4080_12x4,
# timings_packing_72x192x4080_8x6,
timings_switchpacking_12x4,
))
plot(mnks, timings_ref, legend = "ref", color = "black",
xlabel = "matrix dimensions (m = n = k)",
ylabel = "minimum sample time (seconds)",
xrange = "[$(first(mnks)):$(last(mnks))]",
yrange = "[$(first(ylims)):$(last(ylims))]",)
plot!(mnks, timings_jip, legend = "jip", color = "black", linestyle="-")
# non-packing implementations
plot!(mnks, timings_nonpacking_72x72x4080_12x4, legend = "nonpacking\\_72x72x4080\\_12x4", color = tableauhex[1], linestyle=".")
# packing implementations
plot!(mnks, timings_packing_72x192x4080_12x4, legend = "packing\\_72x192x4080\\_12x4", color = tableauhex[3], linestyle=".")
# plot!(mnks, timings_packing_72x192x4080_8x6, legend = "packing\\_72x192x4080\\_8x6", color = tableauhex[3], linestyle=".")
# switch packing implementations
plot!(mnks, timings_switchpacking_12x4, legend = "switchpacking\\_12x4", color = tableauhex[4])
# visualize gflops
hidereference = false
timetogflops(mnk, time) = 2 * mnk^3 / time / 10^9
# reference implementation (openblas)
gflops_ref = timetogflops.(mnks, timings_ref)
# three-nested-loop implementation
gflops_jip = timetogflops.(mnks, timings_jip)
# non-packing implementations
gflops_nonpacking_72x72x4080_12x4 = timetogflops.(mnks, timings_nonpacking_72x72x4080_12x4)
# packing implementations
gflops_packing_72x192x4080_12x4 = timetogflops.(mnks, timings_packing_72x192x4080_12x4)
# gflops_packing_72x192x4080_8x6 = timetogflops.(mnks, timings_packing_72x192x4080_8x6)
# switch packing implementations
gflops_switchpacking_12x4 = timetogflops.(mnks, timings_switchpacking_12x4)
ylims = [0, maximum(vcat(gflops_jip, (hidereference ? [] : gflops_ref),
gflops_nonpacking_72x72x4080_12x4,
gflops_packing_72x192x4080_12x4,
# gflops_packing_72x192x4080_8x6,
gflops_switchpacking_12x4,
))]
plot(mnks, gflops_jip, legend = "jip", color = "black", linestyle=".", linewidth=1,
xlabel = "matrix dimensions (m = n = k)",
ylabel = "GFLOPS from minimum sample time",
xrange = "[$(first(mnks)):$(last(mnks))]",
yrange = "[$(first(ylims)):$(last(ylims))]",
box = "bottom right",)
# non-packing implementations
plot!(mnks, gflops_nonpacking_72x72x4080_12x4,
legend = "nonpacking\\_72x72x4080\\_12x4",
color = tableauhex[1], linestyle="..", linewidth = 3)
# packing implementations
plot!(mnks, gflops_packing_72x192x4080_12x4,
legend = "packing\\_72x192x4080\\_12x4",
color = tableauhex[3], linestyle="..", linewidth = 3)
# plot!(mnks, gflops_packing_72x192x4080_8x6,
# legend = "packing\\_72x192x4080\\_8x6",
# color = tableauhex[3], linestyle="..", linewidth = 3)
# switch packing implementations
plot!(mnks, gflops_switchpacking_12x4, legend = "switchpacking\\_12x4", color = tableauhex[4])
# reference
hidereference || plot!(mnks, gflops_ref, legend = "ref", color = "black")