-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsgemm.cu
More file actions
880 lines (774 loc) · 36.4 KB
/
sgemm.cu
File metadata and controls
880 lines (774 loc) · 36.4 KB
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
#include "common.cuh"
template <const int BM, const int BN, const int BK, const int TM, const int TN, const int TK>
__global__ void sgemm_v1(const float *A, const float *B, float *C, const int M, const int N, const int K) {
const int x = blockIdx.x * BN + threadIdx.x;
const int y = blockIdx.y * BM + threadIdx.y;
if (x < N && y < M) {
float reg = 0;
for (int i = 0; i < K; ++i)
reg += A[y * K + i] * B[i * N + x];
C[y * N + x] = reg;
}
}
/*
* 使用 shared memory 提高速度
*/
// BM = BN = BK = 16
template <const int BM, const int BN, const int BK, const int TM, const int TN, const int TK>
__global__ void sgemm_v2(const float *A, const float *B, float *C, const int M, const int N, const int K) {
__shared__ float s_a[BM][BK], s_b[BK][BN];
float reg = 0.0f;
const int tx = threadIdx.x, ty = threadIdx.y;
const int bx = blockIdx.x * BN, by = blockIdx.y * BM;
const int x = bx + tx, y = by + ty;
if (y < M && x < N) {
#pragma unroll
for (int offs = 0; offs < K; offs += BK) {
if (offs + tx < K)
s_a[ty][tx] = A[y * K + (offs + tx)];
if (offs + ty < K)
s_b[ty][tx] = B[(offs + ty) * N + x];
__syncthreads();
#pragma unroll
for (int k = 0; k < BK && (offs + k) < K; ++k)
reg += s_a[ty][k] * s_b[k][tx];
__syncthreads();
}
C[y * N + x] = reg;
}
}
/*
* 思路: 1. 之前一次只 load 一个 float 到 smem, 太少了, 能不能用 float4 来向量化加载, 一次 load 4 个 float?
* 实现: 1. block 还是 (16, 16), 但是由于每个 thread 一次 load 4 个 float, 所以 BK 变成 16x4 = 64
*/
// BM = BN = 16, BK = 64
template <const int BM, const int BN, const int BK, const int TM, const int TN, const int TK>
__global__ void sgemm_v3(const float *A, const float *B, float *C, const int M, const int N, const int K) {
__shared__ float s_a[BM][BK], s_b[BK][BN];
float reg = 0.0f;
const int tx = threadIdx.x, ty = threadIdx.y;
const int bx = blockIdx.x * BN, by = blockIdx.y * BM;
const int x = bx + tx, y = by + ty;
const int tid = ty * blockDim.x + tx;
const int row_smem_a = tid / (BK / 4);
const int col_smem_a = (tid % (BK / 4)) * 4;
const int row_smem_b = tid / (BN / 4);
const int col_smem_b = (tid % (BN / 4)) * 4;
const int row_A = by + row_smem_a;
const int col_B = bx + col_smem_b;
if (y < M && x < N) {
#pragma unroll
for (int offs = 0; offs < K; offs += BK) {
int col_A = offs + col_smem_a;
int row_B = offs + row_smem_b;
FLOAT4(s_a[row_smem_a][col_smem_a]) = CFLOAT4(A[row_A * K + col_A]);
FLOAT4(s_b[row_smem_b][col_smem_b]) = CFLOAT4(B[row_B * N + col_B]);
__syncthreads();
#pragma unroll
for (int k = 0; k < BK; ++k)
reg += s_a[ty][k] * s_b[k][tx];
__syncthreads();
}
C[y * N + x] = reg;
}
}
/*
* 思路: 1. 每个 thread 实际上只算了 C 的一个元素, 能不能多计算一些?
* 2. 进一步考虑: 之前是从 global memory 取子块到 shared memory, 能不能从 shared memory 取子块到 register?
* 3. 对于每一个 block, 计算量是 2 * K * BM * BN, 访存量是 4 * (BM * K + K * BN) Bytes, 计算访存比为 1/(2(1/BM + 1/BN)),
* 可以看到, 如果增大 BM 和 BN, 可以增大计算访存比, 提高效率
* 实现: 1. 这里的 block 布局还是和之前一样 (16, 16), 但是每个 thread 变成计算 4x4 的子块
* 2. TK = 1, 相比之前就是: 点 -> 线
* 3. 这里一个 thread 不止 load 一个 float4, 而是 ((BM * BK) / 4) / (( BM / TM) * (BN / TN)) = (BK * TM * TN) / (4 * BN) 个 float4
*/
// BM = BN = 64, BK = 64, TM = TN = 4, TK = 1
template <const int BM, const int BN, const int BK, const int TM, const int TN, const int TK>
__global__ void sgemm_v4(float *A, float *B, float *C, int M, int N, int K) {
SHARED_ALIEN16 float s_a[BM][BK];
SHARED_ALIEN16 float s_b[BK][BN];
float r_a[TM], r_b[TN];
float r_c[TM][TN];
memset(r_a, 0, sizeof(r_a));
memset(r_b, 0, sizeof(r_b));
memset(r_c, 0, sizeof(r_c));
const int tx = threadIdx.x, ty = threadIdx.y;
const int tid = ty * blockDim.x + tx;
const int total_threads = blockDim.x * blockDim.y;
const int total_loads_a = (BM * BK) / 4;
const int total_loads_b = (BK * BN) / 4;
const int bx = blockIdx.x * BN;
const int by = blockIdx.y * BM;
for (int offs = 0; offs < K; offs += BK) {
for (int load_offs = tid; load_offs < total_loads_a; load_offs += total_threads) {
int row_smem_a = load_offs / (BK / 4);
int col_smem_a = (load_offs % (BK / 4)) * 4;
int row_A = by + row_smem_a;
int col_A = offs + col_smem_a;
if (row_A < M && col_A + 3 < K)
FLOAT4(s_a[row_smem_a][col_smem_a]) = FLOAT4(A[row_A * K + col_A]);
else {
s_a[row_smem_a][col_smem_a + 0] = (row_A < M && col_A < K) ? A[row_A * K + col_A] : 0.0f;
s_a[row_smem_a][col_smem_a + 1] = (row_A < M && col_A + 1 < K) ? A[row_A * K + col_A + 1] : 0.0f;
s_a[row_smem_a][col_smem_a + 2] = (row_A < M && col_A + 2 < K) ? A[row_A * K + col_A + 2] : 0.0f;
s_a[row_smem_a][col_smem_a + 3] = (row_A < M && col_A + 3 < K) ? A[row_A * K + col_A + 3] : 0.0f;
}
}
for (int load_offs = tid; load_offs < total_loads_b; load_offs += total_threads) {
int row_smem_b = load_offs / (BN / 4);
int col_smem_b = (load_offs % (BN / 4)) * 4;
int row_B = offs + row_smem_b;
int col_B = bx + col_smem_b;
if (row_B < K && col_B + 3 < N)
FLOAT4(s_b[row_smem_b][col_smem_b]) = FLOAT4(B[row_B * N + col_B]);
else {
s_b[row_smem_b][col_smem_b + 0] = (row_B < K && col_B < N) ? B[row_B * N + col_B] : 0.0f;
s_b[row_smem_b][col_smem_b + 1] = (row_B < K && col_B + 1 < N) ? B[row_B * N + col_B + 1] : 0.0f;
s_b[row_smem_b][col_smem_b + 2] = (row_B < K && col_B + 2 < N) ? B[row_B * N + col_B + 2] : 0.0f;
s_b[row_smem_b][col_smem_b + 3] = (row_B < K && col_B + 3 < N) ? B[row_B * N + col_B + 3] : 0.0f;
}
}
__syncthreads();
#pragma unroll
for (int k = 0; k < BK; ++k) {
int sa_row_start = ty * TM;
int sb_col_start = tx * TN;
#pragma unroll
for (int i = 0; i < TM; ++i)
r_a[i] = s_a[sa_row_start + i][k];
#pragma unroll
for (int j = 0; j < TN; j += 4)
FLOAT4(r_b[j]) = FLOAT4(s_b[k][sb_col_start + j]);
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j)
r_c[i][j] = __fmaf_rn(r_a[i], r_b[j], r_c[i][j]);
}
__syncthreads();
}
#pragma unroll
for (int i = 0; i < TM; ++i) {
#pragma unroll
for (int j = 0; j < TN; j += 4) {
int x = bx + tx * TN + j;
int y = by + ty * TM + i;
if (x < N && y < M)
FLOAT4(C[y * N + x]) = FLOAT4(r_c[i][j]);
}
}
}
/*
* 思路: 1. 使用 double buffer 隐藏从 gmem 到 smem 的访存延迟
* 实现: 1. 使用两个 smem, 一个用于 load, 一个用于计算
* 2. 由于 smem 空间有限, 此时 BK 变为原来的一半
*/
// BM = BN = 64, BK = 32, TM = TN = 4, TK = 1
template <const int BM, const int BN, const int BK, const int TM, const int TN, const int TK>
__global__ void sgemm_v5(float *A, float *B, float *C, int M, int N, int K) {
SHARED_ALIEN16 float s_a[2][BM][BK];
SHARED_ALIEN16 float s_b[2][BK][BN];
float r_a[TM], r_b[TN];
float r_c[TM][TN];
memset(r_a, 0, sizeof(r_a));
memset(r_b, 0, sizeof(r_b));
memset(r_c, 0, sizeof(r_c));
const int tx = threadIdx.x, ty = threadIdx.y;
const int tid = ty * blockDim.x + tx;
const int total_threads = blockDim.x * blockDim.y;
const int total_loads_a = (BM * BK) / 4;
const int total_loads_b = (BK * BN) / 4;
const int bx = blockIdx.x * BN;
const int by = blockIdx.y * BM;
int cur_use = 0, next_load = 1;
for (int load_offs = tid; load_offs < total_loads_a; load_offs += total_threads) {
int row_smem_a = load_offs / (BK / 4);
int col_smem_a = (load_offs % (BK / 4)) * 4;
int row_A = by + row_smem_a;
int col_A = col_smem_a;
if (row_A < M && col_A + 3 < K)
FLOAT4(s_a[0][row_smem_a][col_smem_a]) = FLOAT4(A[row_A * K + col_A]);
else {
s_a[0][row_smem_a][col_smem_a + 0] = (row_A < M && col_A < K) ? A[row_A * K + col_A] : 0.0f;
s_a[0][row_smem_a][col_smem_a + 1] = (row_A < M && col_A + 1 < K) ? A[row_A * K + col_A + 1] : 0.0f;
s_a[0][row_smem_a][col_smem_a + 2] = (row_A < M && col_A + 2 < K) ? A[row_A * K + col_A + 2] : 0.0f;
s_a[0][row_smem_a][col_smem_a + 3] = (row_A < M && col_A + 3 < K) ? A[row_A * K + col_A + 3] : 0.0f;
}
}
for (int load_offs = tid; load_offs < total_loads_b; load_offs += total_threads) {
int row_smem_b = load_offs / (BN / 4);
int col_smem_b = (load_offs % (BN / 4)) * 4;
int row_B = row_smem_b;
int col_B = bx + col_smem_b;
if (row_B < K && col_B + 3 < N)
FLOAT4(s_b[0][row_smem_b][col_smem_b]) = FLOAT4(B[row_B * N + col_B]);
else {
s_b[0][row_smem_b][col_smem_b + 0] = (row_B < K && col_B < N) ? B[row_B * N + col_B] : 0.0f;
s_b[0][row_smem_b][col_smem_b + 1] = (row_B < K && col_B + 1 < N) ? B[row_B * N + col_B + 1] : 0.0f;
s_b[0][row_smem_b][col_smem_b + 2] = (row_B < K && col_B + 2 < N) ? B[row_B * N + col_B + 2] : 0.0f;
s_b[0][row_smem_b][col_smem_b + 3] = (row_B < K && col_B + 3 < N) ? B[row_B * N + col_B + 3] : 0.0f;
}
}
__syncthreads();
for (int offs = BK; offs < K; offs += BK) {
for (int load_offs = tid; load_offs < total_loads_a; load_offs += total_threads) {
int row_smem_a = load_offs / (BK / 4);
int col_smem_a = (load_offs % (BK / 4)) * 4;
int row_A = by + row_smem_a;
int col_A = offs + col_smem_a;
if (row_A < M && col_A + 3 < K)
FLOAT4(s_a[next_load][row_smem_a][col_smem_a]) = FLOAT4(A[row_A * K + col_A]);
else {
s_a[next_load][row_smem_a][col_smem_a + 0] = (row_A < M && col_A < K) ? A[row_A * K + col_A] : 0.0f;
s_a[next_load][row_smem_a][col_smem_a + 1] = (row_A < M && col_A + 1 < K) ? A[row_A * K + col_A + 1] : 0.0f;
s_a[next_load][row_smem_a][col_smem_a + 2] = (row_A < M && col_A + 2 < K) ? A[row_A * K + col_A + 2] : 0.0f;
s_a[next_load][row_smem_a][col_smem_a + 3] = (row_A < M && col_A + 3 < K) ? A[row_A * K + col_A + 3] : 0.0f;
}
}
for (int load_offs = tid; load_offs < total_loads_b; load_offs += total_threads) {
int row_smem_b = load_offs / (BN / 4);
int col_smem_b = (load_offs % (BN / 4)) * 4;
int row_B = offs + row_smem_b;
int col_B = bx + col_smem_b;
if (row_B < K && col_B + 3 < N)
FLOAT4(s_b[next_load][row_smem_b][col_smem_b]) = FLOAT4(B[row_B * N + col_B]);
else {
s_b[next_load][row_smem_b][col_smem_b + 0] = (row_B < K && col_B < N) ? B[row_B * N + col_B] : 0.0f;
s_b[next_load][row_smem_b][col_smem_b + 1] = (row_B < K && col_B + 1 < N) ? B[row_B * N + col_B + 1] : 0.0f;
s_b[next_load][row_smem_b][col_smem_b + 2] = (row_B < K && col_B + 2 < N) ? B[row_B * N + col_B + 2] : 0.0f;
s_b[next_load][row_smem_b][col_smem_b + 3] = (row_B < K && col_B + 3 < N) ? B[row_B * N + col_B + 3] : 0.0f;
}
}
#pragma unroll
for (int k = 0; k < BK; ++k) {
int sa_row_start = ty * TM;
int sb_col_start = tx * TN;
#pragma unroll
for (int i = 0; i < TM; ++i)
r_a[i] = s_a[cur_use][sa_row_start + i][k];
#pragma unroll
for (int j = 0; j < TN; j += 4)
FLOAT4(r_b[j]) = FLOAT4(s_b[cur_use][k][sb_col_start + j]);
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j)
r_c[i][j] = __fmaf_rn(r_a[i], r_b[j], r_c[i][j]);
}
__syncthreads();
cur_use ^= 1;
next_load ^= 1;
}
#pragma unroll
for (int k = 0; k < BK; ++k) {
int sa_row_start = ty * TM;
int sb_col_start = tx * TN;
#pragma unroll
for (int i = 0; i < TM; ++i)
r_a[i] = s_a[cur_use][sa_row_start + i][k];
#pragma unroll
for (int j = 0; j < TN; j += 4)
FLOAT4(r_b[j]) = FLOAT4(s_b[cur_use][k][sb_col_start + j]);
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j)
r_c[i][j] = __fmaf_rn(r_a[i], r_b[j], r_c[i][j]);
}
__syncthreads();
#pragma unroll
for (int i = 0; i < TM; ++i) {
#pragma unroll
for (int j = 0; j < TN; j += 4) {
int x = bx + tx * TN + j;
int y = by + ty * TM + i;
if (x < N && y < M)
FLOAT4(C[y * N + x]) = FLOAT4(r_c[i][j]);
}
}
}
/*
* 观察到从 s_a 读数据到 r_a 存在 bank conflict, 考虑使用 padding 缓解
* 这里为了使用 float4, 所以 padding 应该是 4 的倍数, 而不能是 1
*/
// BM = BN = 64, BK = 32, TM = TN = 4, TK = 1
template <const int BM, const int BN, const int BK, const int TM, const int TN, const int TK>
__global__ void sgemm_v6(float *A, float *B, float *C, int M, int N, int K) {
SHARED_ALIEN16 float s_a[2][BM][BK + 4];
SHARED_ALIEN16 float s_b[2][BK][BN + 4];
float r_a[TM], r_b[TN];
float r_c[TM][TN];
memset(r_a, 0, sizeof(r_a));
memset(r_b, 0, sizeof(r_b));
memset(r_c, 0, sizeof(r_c));
const int tx = threadIdx.x, ty = threadIdx.y;
const int tid = ty * blockDim.x + tx;
const int total_threads = blockDim.x * blockDim.y;
const int total_loads_a = (BM * BK) / 4;
const int total_loads_b = (BK * BN) / 4;
const int bx = blockIdx.x * BN;
const int by = blockIdx.y * BM;
int cur_use = 0, next_load = 1;
for (int load_offs = tid; load_offs < total_loads_a; load_offs += total_threads) {
int row_smem_a = load_offs / (BK / 4);
int col_smem_a = (load_offs % (BK / 4)) * 4;
int row_A = by + row_smem_a;
int col_A = col_smem_a;
if (row_A < M && col_A + 3 < K)
FLOAT4(s_a[0][row_smem_a][col_smem_a]) = FLOAT4(A[row_A * K + col_A]);
else {
s_a[0][row_smem_a][col_smem_a + 0] = (row_A < M && col_A < K) ? A[row_A * K + col_A] : 0.0f;
s_a[0][row_smem_a][col_smem_a + 1] = (row_A < M && col_A + 1 < K) ? A[row_A * K + col_A + 1] : 0.0f;
s_a[0][row_smem_a][col_smem_a + 2] = (row_A < M && col_A + 2 < K) ? A[row_A * K + col_A + 2] : 0.0f;
s_a[0][row_smem_a][col_smem_a + 3] = (row_A < M && col_A + 3 < K) ? A[row_A * K + col_A + 3] : 0.0f;
}
}
for (int load_offs = tid; load_offs < total_loads_b; load_offs += total_threads) {
int row_smem_b = load_offs / (BN / 4);
int col_smem_b = (load_offs % (BN / 4)) * 4;
int row_B = row_smem_b;
int col_B = bx + col_smem_b;
if (row_B < K && col_B + 3 < N)
FLOAT4(s_b[0][row_smem_b][col_smem_b]) = FLOAT4(B[row_B * N + col_B]);
else {
s_b[0][row_smem_b][col_smem_b + 0] = (row_B < K && col_B < N) ? B[row_B * N + col_B] : 0.0f;
s_b[0][row_smem_b][col_smem_b + 1] = (row_B < K && col_B + 1 < N) ? B[row_B * N + col_B + 1] : 0.0f;
s_b[0][row_smem_b][col_smem_b + 2] = (row_B < K && col_B + 2 < N) ? B[row_B * N + col_B + 2] : 0.0f;
s_b[0][row_smem_b][col_smem_b + 3] = (row_B < K && col_B + 3 < N) ? B[row_B * N + col_B + 3] : 0.0f;
}
}
__syncthreads();
for (int offs = BK; offs < K; offs += BK) {
for (int load_offs = tid; load_offs < total_loads_a; load_offs += total_threads) {
int row_smem_a = load_offs / (BK / 4);
int col_smem_a = (load_offs % (BK / 4)) * 4;
int row_A = by + row_smem_a;
int col_A = offs + col_smem_a;
if (row_A < M && col_A + 3 < K)
FLOAT4(s_a[next_load][row_smem_a][col_smem_a]) = FLOAT4(A[row_A * K + col_A]);
else {
s_a[next_load][row_smem_a][col_smem_a + 0] = (row_A < M && col_A < K) ? A[row_A * K + col_A] : 0.0f;
s_a[next_load][row_smem_a][col_smem_a + 1] = (row_A < M && col_A + 1 < K) ? A[row_A * K + col_A + 1] : 0.0f;
s_a[next_load][row_smem_a][col_smem_a + 2] = (row_A < M && col_A + 2 < K) ? A[row_A * K + col_A + 2] : 0.0f;
s_a[next_load][row_smem_a][col_smem_a + 3] = (row_A < M && col_A + 3 < K) ? A[row_A * K + col_A + 3] : 0.0f;
}
}
for (int load_offs = tid; load_offs < total_loads_b; load_offs += total_threads) {
int row_smem_b = load_offs / (BN / 4);
int col_smem_b = (load_offs % (BN / 4)) * 4;
int row_B = offs + row_smem_b;
int col_B = bx + col_smem_b;
if (row_B < K && col_B + 3 < N)
FLOAT4(s_b[next_load][row_smem_b][col_smem_b]) = FLOAT4(B[row_B * N + col_B]);
else {
s_b[next_load][row_smem_b][col_smem_b + 0] = (row_B < K && col_B < N) ? B[row_B * N + col_B] : 0.0f;
s_b[next_load][row_smem_b][col_smem_b + 1] = (row_B < K && col_B + 1 < N) ? B[row_B * N + col_B + 1] : 0.0f;
s_b[next_load][row_smem_b][col_smem_b + 2] = (row_B < K && col_B + 2 < N) ? B[row_B * N + col_B + 2] : 0.0f;
s_b[next_load][row_smem_b][col_smem_b + 3] = (row_B < K && col_B + 3 < N) ? B[row_B * N + col_B + 3] : 0.0f;
}
}
#pragma unroll
for (int k = 0; k < BK; ++k) {
int sa_row_start = ty * TM;
int sb_col_start = tx * TN;
#pragma unroll
for (int i = 0; i < TM; ++i)
r_a[i] = s_a[cur_use][sa_row_start + i][k];
#pragma unroll
for (int j = 0; j < TN; j += 4)
FLOAT4(r_b[j]) = FLOAT4(s_b[cur_use][k][sb_col_start + j]);
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j)
r_c[i][j] = __fmaf_rn(r_a[i], r_b[j], r_c[i][j]);
}
__syncthreads();
cur_use ^= 1;
next_load ^= 1;
}
#pragma unroll
for (int k = 0; k < BK; ++k) {
int sa_row_start = ty * TM;
int sb_col_start = tx * TN;
#pragma unroll
for (int i = 0; i < TM; ++i)
r_a[i] = s_a[cur_use][sa_row_start + i][k];
#pragma unroll
for (int j = 0; j < TN; j += 4)
FLOAT4(r_b[j]) = FLOAT4(s_b[cur_use][k][sb_col_start + j]);
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j)
r_c[i][j] = __fmaf_rn(r_a[i], r_b[j], r_c[i][j]);
}
__syncthreads();
#pragma unroll
for (int i = 0; i < TM; ++i) {
#pragma unroll
for (int j = 0; j < TN; j += 4) {
int x = bx + tx * TN + j;
int y = by + ty * TM + i;
if (x < N && y < M)
FLOAT4(C[y * N + x]) = FLOAT4(r_c[i][j]);
}
}
}
/*
* 使用 padding 会带来额外的 smem 开销, 考虑使用 swizzle 来缓解 bank conflict
* 通过改变共享内存的数据布局,将原本可能冲突的访问分散到不同的bank
* 与常规 swizzle 不同, 这里为了方便 swizzle 后也能直接用 float4 加载, 所以 swizzle 的是行而不是列
* 缺点: 1. 增大计算量
* 2. swizzle 函数不好找
*/
// BM = BN = 64, BK = 32, TM = TN = 4, TK = 1
template <const int BM, const int BN, const int BK, const int TM, const int TN, const int TK>
__global__ void sgemm_v7(float *A, float *B, float *C, int M, int N, int K) {
SHARED_ALIEN16 float s_a[2][BM][BK];
SHARED_ALIEN16 float s_b[2][BK][BN];
float r_a[TM], r_b[TN];
float r_c[TM][TN];
memset(r_a, 0, sizeof(r_a));
memset(r_b, 0, sizeof(r_b));
memset(r_c, 0, sizeof(r_c));
const int tx = threadIdx.x, ty = threadIdx.y;
const int tid = ty * blockDim.x + tx;
const int total_threads = blockDim.x * blockDim.y;
const int total_loads_a = (BM * BK) / 4;
const int total_loads_b = (BK * BN) / 4;
const int bx = blockIdx.x * BN;
const int by = blockIdx.y * BM;
int cur_use = 0, next_load = 1;
for (int load_offs = tid; load_offs < total_loads_a; load_offs += total_threads) {
int row_smem_a = load_offs / (BK / 4);
int col_smem_a = (load_offs % (BK / 4)) * 4;
int row_A = by + row_smem_a;
int col_A = col_smem_a;
int swizzled_row = SWIZZLE_FLOAT4(row_smem_a, col_smem_a);
if (row_A < M && col_A + 3 < K)
FLOAT4(s_a[0][swizzled_row][col_smem_a]) = FLOAT4(A[row_A * K + col_A]);
else {
s_a[0][swizzled_row][col_smem_a + 0] = (row_A < M && col_A < K) ? A[row_A * K + col_A] : 0.0f;
s_a[0][swizzled_row][col_smem_a + 1] = (row_A < M && col_A + 1 < K) ? A[row_A * K + col_A + 1] : 0.0f;
s_a[0][swizzled_row][col_smem_a + 2] = (row_A < M && col_A + 2 < K) ? A[row_A * K + col_A + 2] : 0.0f;
s_a[0][swizzled_row][col_smem_a + 3] = (row_A < M && col_A + 3 < K) ? A[row_A * K + col_A + 3] : 0.0f;
}
}
for (int load_offs = tid; load_offs < total_loads_b; load_offs += total_threads) {
int row_smem_b = load_offs / (BN / 4);
int col_smem_b = (load_offs % (BN / 4)) * 4;
int row_B = row_smem_b;
int col_B = bx + col_smem_b;
int swizzled_row = SWIZZLE_FLOAT4(row_smem_b, col_smem_b);
if (row_B < K && col_B + 3 < N)
FLOAT4(s_b[0][swizzled_row][col_smem_b]) = FLOAT4(B[row_B * N + col_B]);
else {
s_b[0][swizzled_row][col_smem_b + 0] = (row_B < K && col_B < N) ? B[row_B * N + col_B] : 0.0f;
s_b[0][swizzled_row][col_smem_b + 1] = (row_B < K && col_B + 1 < N) ? B[row_B * N + col_B + 1] : 0.0f;
s_b[0][swizzled_row][col_smem_b + 2] = (row_B < K && col_B + 2 < N) ? B[row_B * N + col_B + 2] : 0.0f;
s_b[0][swizzled_row][col_smem_b + 3] = (row_B < K && col_B + 3 < N) ? B[row_B * N + col_B + 3] : 0.0f;
}
}
__syncthreads();
for (int offs = BK; offs < K; offs += BK) {
for (int load_offs = tid; load_offs < total_loads_a; load_offs += total_threads) {
int row_smem_a = load_offs / (BK / 4);
int col_smem_a = (load_offs % (BK / 4)) * 4;
int row_A = by + row_smem_a;
int col_A = offs + col_smem_a;
int swizzled_row = SWIZZLE_FLOAT4(row_smem_a, col_smem_a);
if (row_A < M && col_A + 3 < K)
FLOAT4(s_a[next_load][swizzled_row][col_smem_a]) = FLOAT4(A[row_A * K + col_A]);
else {
s_a[next_load][swizzled_row][col_smem_a + 0] = (row_A < M && col_A < K) ? A[row_A * K + col_A] : 0.0f;
s_a[next_load][swizzled_row][col_smem_a + 1] = (row_A < M && col_A + 1 < K) ? A[row_A * K + col_A + 1] : 0.0f;
s_a[next_load][swizzled_row][col_smem_a + 2] = (row_A < M && col_A + 2 < K) ? A[row_A * K + col_A + 2] : 0.0f;
s_a[next_load][swizzled_row][col_smem_a + 3] = (row_A < M && col_A + 3 < K) ? A[row_A * K + col_A + 3] : 0.0f;
}
}
for (int load_offs = tid; load_offs < total_loads_b; load_offs += total_threads) {
int row_smem_b = load_offs / (BN / 4);
int col_smem_b = (load_offs % (BN / 4)) * 4;
int row_B = offs + row_smem_b;
int col_B = bx + col_smem_b;
int swizzled_row = SWIZZLE_FLOAT4(row_smem_b, col_smem_b);
if (row_B < K && col_B + 3 < N)
FLOAT4(s_b[next_load][swizzled_row][col_smem_b]) = FLOAT4(B[row_B * N + col_B]);
else {
s_b[next_load][swizzled_row][col_smem_b + 0] = (row_B < K && col_B < N) ? B[row_B * N + col_B] : 0.0f;
s_b[next_load][swizzled_row][col_smem_b + 1] = (row_B < K && col_B + 1 < N) ? B[row_B * N + col_B + 1] : 0.0f;
s_b[next_load][swizzled_row][col_smem_b + 2] = (row_B < K && col_B + 2 < N) ? B[row_B * N + col_B + 2] : 0.0f;
s_b[next_load][swizzled_row][col_smem_b + 3] = (row_B < K && col_B + 3 < N) ? B[row_B * N + col_B + 3] : 0.0f;
}
}
#pragma unroll
for (int k = 0; k < BK; ++k) {
int sa_row_start = ty * TM;
int sb_col_start = tx * TN;
#pragma unroll
for (int i = 0; i < TM; ++i)
r_a[i] = s_a[cur_use][SWIZZLE_FLOAT4(sa_row_start + i, k)][k];
#pragma unroll
for (int j = 0; j < TN; j += 4)
FLOAT4(r_b[j]) = FLOAT4(s_b[cur_use][SWIZZLE_FLOAT4(k, sb_col_start)][sb_col_start + j]);
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j)
r_c[i][j] = __fmaf_rn(r_a[i], r_b[j], r_c[i][j]);
}
__syncthreads();
cur_use ^= 1;
next_load ^= 1;
}
#pragma unroll
for (int k = 0; k < BK; ++k) {
int sa_row_start = ty * TM;
int sb_col_start = tx * TN;
#pragma unroll
for (int i = 0; i < TM; ++i)
r_a[i] = s_a[cur_use][SWIZZLE_FLOAT4(sa_row_start + i, k)][k];
#pragma unroll
for (int j = 0; j < TN; j += 4)
FLOAT4(r_b[j]) = FLOAT4(s_b[cur_use][SWIZZLE_FLOAT4(k, sb_col_start)][sb_col_start + j]);
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j)
r_c[i][j] = __fmaf_rn(r_a[i], r_b[j], r_c[i][j]);
}
__syncthreads();
#pragma unroll
for (int i = 0; i < TM; ++i) {
#pragma unroll
for (int j = 0; j < TN; j += 4) {
int x = bx + tx * TN + j;
int y = by + ty * TM + i;
if (x < N && y < M) {
FLOAT4(C[y * N + x]) = FLOAT4(r_c[i][j]);
}
}
}
}
/*
* 除了上面两种解决 s_a 的 bank conflict 方法, 其实还可以考虑改变 s_a 的排布方式
* 因为 s_b 读到 r_b 是没有 bank conflict 的, 所以如果把 s_a 的排布方式改成和 s_b 一样, 也可以缓解 bank conflict
* s_a: [BM][BK] -> [BK][BM], 使用 r_a 的前四个位置辅助存储
* 但是要注意的是, 由于 store s_a 的方式变了, 此时在 shared store 的时候会发生 bank conflict
*/
// BM = BN = 64, BK = 32, TM = TN = 4, TK = 1
template <const int BM, const int BN, const int BK, const int TM, const int TN, const int TK>
__global__ void sgemm_v8(float *A, float *B, float *C, int M, int N, int K) {
SHARED_ALIEN16 float s_a[2][BK][BM];
SHARED_ALIEN16 float s_b[2][BK][BN];
float r_a[TM], r_b[TN];
float r_c[TM][TN];
memset(r_a, 0, sizeof(r_a));
memset(r_b, 0, sizeof(r_b));
memset(r_c, 0, sizeof(r_c));
const int tx = threadIdx.x, ty = threadIdx.y;
const int tid = ty * blockDim.x + tx;
const int total_threads = blockDim.x * blockDim.y;
const int total_loads_a = (BM * BK) / 4;
const int total_loads_b = (BK * BN) / 4;
const int bx = blockIdx.x * BN;
const int by = blockIdx.y * BM;
int cur_use = 0, next_load = 1;
for (int load_offs = tid; load_offs < total_loads_a; load_offs += total_threads) {
int row_smem_a = load_offs / (BK / 4);
int col_smem_a = (load_offs % (BK / 4)) * 4;
int row_A = by + row_smem_a;
int col_A = col_smem_a;
if (row_A < M && col_A + 3 < K)
FLOAT4(r_a[0]) = FLOAT4(A[row_A * K + col_A]);
else {
r_a[0] = (row_A < M && col_A < K) ? A[row_A * K + col_A] : 0.0f;
r_a[1] = (row_A < M && col_A + 1 < K) ? A[row_A * K + col_A + 1] : 0.0f;
r_a[2] = (row_A < M && col_A + 2 < K) ? A[row_A * K + col_A + 2] : 0.0f;
r_a[3] = (row_A < M && col_A + 3 < K) ? A[row_A * K + col_A + 3] : 0.0f;
}
s_a[0][col_smem_a + 0][row_smem_a] = r_a[0];
s_a[0][col_smem_a + 1][row_smem_a] = r_a[1];
s_a[0][col_smem_a + 2][row_smem_a] = r_a[2];
s_a[0][col_smem_a + 3][row_smem_a] = r_a[3];
}
for (int load_offs = tid; load_offs < total_loads_b; load_offs += total_threads) {
int row_smem_b = load_offs / (BN / 4);
int col_smem_b = (load_offs % (BN / 4)) * 4;
int row_B = row_smem_b;
int col_B = bx + col_smem_b;
if (row_B < K && col_B + 3 < N)
FLOAT4(s_b[0][row_smem_b][col_smem_b]) = FLOAT4(B[row_B * N + col_B]);
else {
s_b[0][row_smem_b][col_smem_b + 0] = (row_B < K && col_B < N) ? B[row_B * N + col_B] : 0.0f;
s_b[0][row_smem_b][col_smem_b + 1] = (row_B < K && col_B + 1 < N) ? B[row_B * N + col_B + 1] : 0.0f;
s_b[0][row_smem_b][col_smem_b + 2] = (row_B < K && col_B + 2 < N) ? B[row_B * N + col_B + 2] : 0.0f;
s_b[0][row_smem_b][col_smem_b + 3] = (row_B < K && col_B + 3 < N) ? B[row_B * N + col_B + 3] : 0.0f;
}
}
__syncthreads();
for (int offs = BK; offs < K; offs += BK) {
for (int load_offs = tid; load_offs < total_loads_a; load_offs += total_threads) {
int row_smem_a = load_offs / (BK / 4);
int col_smem_a = (load_offs % (BK / 4)) * 4;
int row_A = by + row_smem_a;
int col_A = offs + col_smem_a;
if (row_A < M && col_A + 3 < K)
FLOAT4(r_a[0]) = FLOAT4(A[row_A * K + col_A]);
else {
r_a[0] = (row_A < M && col_A < K) ? A[row_A * K + col_A] : 0.0f;
r_a[1] = (row_A < M && col_A + 1 < K) ? A[row_A * K + col_A + 1] : 0.0f;
r_a[2] = (row_A < M && col_A + 2 < K) ? A[row_A * K + col_A + 2] : 0.0f;
r_a[3] = (row_A < M && col_A + 3 < K) ? A[row_A * K + col_A + 3] : 0.0f;
}
s_a[next_load][col_smem_a + 0][row_smem_a] = r_a[0];
s_a[next_load][col_smem_a + 1][row_smem_a] = r_a[1];
s_a[next_load][col_smem_a + 2][row_smem_a] = r_a[2];
s_a[next_load][col_smem_a + 3][row_smem_a] = r_a[3];
}
for (int load_offs = tid; load_offs < total_loads_b; load_offs += total_threads) {
int row_smem_b = load_offs / (BN / 4);
int col_smem_b = (load_offs % (BN / 4)) * 4;
int row_B = offs + row_smem_b;
int col_B = bx + col_smem_b;
if (row_B < K && col_B + 3 < N)
FLOAT4(s_b[next_load][row_smem_b][col_smem_b]) = FLOAT4(B[row_B * N + col_B]);
else {
s_b[next_load][row_smem_b][col_smem_b + 0] = (row_B < K && col_B < N) ? B[row_B * N + col_B] : 0.0f;
s_b[next_load][row_smem_b][col_smem_b + 1] = (row_B < K && col_B + 1 < N) ? B[row_B * N + col_B + 1] : 0.0f;
s_b[next_load][row_smem_b][col_smem_b + 2] = (row_B < K && col_B + 2 < N) ? B[row_B * N + col_B + 2] : 0.0f;
s_b[next_load][row_smem_b][col_smem_b + 3] = (row_B < K && col_B + 3 < N) ? B[row_B * N + col_B + 3] : 0.0f;
}
}
#pragma unroll
for (int k = 0; k < BK; ++k) {
int sa_row_start = ty * TM;
int sb_col_start = tx * TN;
#pragma unroll
for (int i = 0; i < TM; i += 4)
FLOAT4(r_a[i]) = FLOAT4(s_a[cur_use][k][sa_row_start + i]);
#pragma unroll
for (int j = 0; j < TN; j += 4)
FLOAT4(r_b[j]) = FLOAT4(s_b[cur_use][k][sb_col_start + j]);
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j)
r_c[i][j] = __fmaf_rn(r_a[i], r_b[j], r_c[i][j]);
}
__syncthreads();
cur_use ^= 1;
next_load ^= 1;
}
#pragma unroll
for (int k = 0; k < BK; ++k) {
int sa_row_start = ty * TM;
int sb_col_start = tx * TN;
#pragma unroll
for (int i = 0; i < TM; i += 4)
FLOAT4(r_a[i]) = FLOAT4(s_a[cur_use][k][sa_row_start + i]);
#pragma unroll
for (int j = 0; j < TN; j += 4)
FLOAT4(r_b[j]) = FLOAT4(s_b[cur_use][k][sb_col_start + j]);
#pragma unroll
for (int i = 0; i < TM; ++i)
#pragma unroll
for (int j = 0; j < TN; ++j)
r_c[i][j] = __fmaf_rn(r_a[i], r_b[j], r_c[i][j]);
}
__syncthreads();
#pragma unroll
for (int i = 0; i < TM; ++i) {
#pragma unroll
for (int j = 0; j < TN; j += 4) {
int x = bx + tx * TN + j;
int y = by + ty * TM + i;
if (x < N && y < M)
FLOAT4(C[y * N + x]) = FLOAT4(r_c[i][j]);
}
}
}
#define UPDATE_GRID_BLOCK(BM, BN, TM, TN) \
{ \
block.x = DIV_UP(BN, TN); \
block.y = DIV_UP(BM, TM); \
grid.x = DIV_UP(N, BN); \
grid.y = DIV_UP(M, BM); \
}
#define WRAPPER(version, BM, BN, BK, TM, TN, TK) \
{ \
UPDATE_GRID_BLOCK(BM, BN, TM, TN); \
CHECK_CUDA_ERROR(cudaMemset(d_c, 0, size_C)); \
timer.start_timer(); \
sgemm_##version<BM, BN, BK, TM, TN, TK><<<grid, block>>>(d_a, d_b, d_c, M, N, K); \
CHECK_CUDA_ERROR(cudaGetLastError()); \
timer.stop_timer(); \
CHECK_CUDA_ERROR(cudaMemcpy(h_c, d_c, size_C, cudaMemcpyDeviceToHost)); \
printf(#version " | time: %10.6f ms | max diff: %10.6f\n", timer.get_time(), max_diff(h_cublas, h_c, M *N)); \
}
void one_turn(const int M, const int N, const int K) {
printf(" ===== M = %d, N = %d, K = %d =====\n", M, N, K);
const int size_A = M * K * sizeof(float);
const int size_B = K * N * sizeof(float);
const int size_C = M * N * sizeof(float);
const float alpha = 1.0f;
const float beta = 0.0f;
Timer timer;
dim3 block, grid;
cublasHandle_t handle;
CHECK_CUBLAS_ERROR(cublasCreate(&handle));
float *h_a = (float *)malloc(size_A);
float *h_b = (float *)malloc(size_B);
float *h_c = (float *)malloc(size_C);
float *h_cublas = (float *)malloc(size_C);
float *d_a, *d_b, *d_c;
CHECK_CUDA_ERROR(cudaMalloc(&d_a, size_A));
CHECK_CUDA_ERROR(cudaMalloc(&d_b, size_B));
CHECK_CUDA_ERROR(cudaMalloc(&d_c, size_C));
random_init(h_a, M * K);
random_init(h_b, K * N);
memset(h_c, 0, size_C);
memset(h_cublas, 0, size_C);
CHECK_CUDA_ERROR(cudaMemcpy(d_a, h_a, size_A, cudaMemcpyHostToDevice));
CHECK_CUDA_ERROR(cudaMemcpy(d_b, h_b, size_B, cudaMemcpyHostToDevice));
CHECK_CUDA_ERROR(cudaMemset(d_c, 0, size_C));
timer.start_timer();
CHECK_CUBLAS_ERROR(cublasSgemm(handle, CUBLAS_OP_N, CUBLAS_OP_N,
N, M, K,
&alpha,
d_b, N,
d_a, K,
&beta,
d_c, N));
timer.stop_timer();
CHECK_CUDA_ERROR(cudaMemcpy(h_cublas, d_c, size_C, cudaMemcpyDeviceToHost));
printf("cublas | time: %10.6f ms | max diff: N/A\n", timer.get_time());
WRAPPER(v1, 16, 16, 1, 1, 1, 1);
WRAPPER(v2, 16, 16, 16, 1, 1, 1);
WRAPPER(v3, 16, 16, 64, 1, 1, 1);
WRAPPER(v4, 64, 64, 64, 4, 4, 1);
WRAPPER(v5, 64, 64, 32, 4, 4, 1);
WRAPPER(v6, 64, 64, 32, 4, 4, 1);
WRAPPER(v7, 64, 64, 32, 4, 4, 1);
WRAPPER(v8, 64, 64, 32, 4, 4, 1);
cublasDestroy(handle);
free(h_a);
free(h_b);
free(h_c);
free(h_cublas);
CHECK_CUDA_ERROR(cudaFree(d_a));
CHECK_CUDA_ERROR(cudaFree(d_b));
CHECK_CUDA_ERROR(cudaFree(d_c));
}
int main(int argc, char const *argv[]) {
const int Ms[] = {256, 512, 1024, 2048, 4096, 8192};
const int Ns[] = {256, 512, 1024, 2048, 4096, 8192};
const int Ks[] = {1024, 1024, 1024, 1024, 1024, 1024};
const int n_groups = sizeof(Ms) / sizeof(Ms[0]);
warmup();
for (int i = 0; i < n_groups; ++i) {
const int M = Ms[i], N = Ns[i], K = Ks[i];
one_turn(M, N, K);
}
return 0;
}