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
| __global__ void sgemm_V1( float * __restrict__ a, float * __restrict__ b, float * __restrict__ c, const int M, const int N, const int K) {
const int BM = 128; const int BN = 128; const int BK = 8; const int TM = 8; const int TN = 8;
const int bx = blockIdx.x; const int by = blockIdx.y; const int tx = threadIdx.x; const int ty = threadIdx.y; const int tid = ty * blockDim.x + tx;
__shared__ float s_a[BM][BK]; __shared__ float s_b[BK][BN];
float r_c[TM][TN] = {0.0};
int load_a_smem_m = tid >> 1; int load_a_smem_k = (tid & 1) << 2; int load_b_smem_k = tid >> 5; int load_b_smem_n = (tid & 31) << 2;
int load_a_gmem_m = by * BM + load_a_smem_m; int load_b_gmem_n = bx * BN + load_b_smem_n;
for (int bk = 0; bk < (K + BK - 1) / BK; bk++) {
int load_a_gmem_k = bk * BK + load_a_smem_k; int load_a_gmem_addr = OFFSET(load_a_gmem_m, load_a_gmem_k, K); FLOAT4(s_a[load_a_smem_m][load_a_smem_k]) = FLOAT4(a[load_a_gmem_addr]); int load_b_gmem_k = bk * BK + load_b_smem_k; int load_b_gmem_addr = OFFSET(load_b_gmem_k, load_b_gmem_n, N); FLOAT4(s_b[load_b_smem_k][load_b_smem_n]) = FLOAT4(b[load_b_gmem_addr]); __syncthreads();
#pragma unroll
for (int k = 0; k < BK; k++) { #pragma unroll for (int m = 0; m < TM; m++) { #pragma unroll for (int n = 0; n < TN; n++) { int comp_a_smem_m = ty * TM + m; int comp_b_smem_n = tx * TN + n; r_c[m][n] += s_a[comp_a_smem_m][k] * s_b[k][comp_b_smem_n]; } } } __syncthreads(); }
#pragma unroll
for (int i = 0; i < TM; i++) { int store_c_gmem_m = by * BM + ty * TM + i; #pragma unroll for (int j = 0; j < TN; j += 4) { int store_c_gmem_n = bx * BN + tx * TN + j; int store_c_gmem_addr = OFFSET(store_c_gmem_m, store_c_gmem_n, N); FLOAT4(c[store_c_gmem_addr]) = FLOAT4(r_c[i][j]); } } }
|