SWE @ U of C
__global__ void matmul_tiled_kernel(float* C, const float* A, const float* B, int M, int N, int K) { __shared__ float a_tile[TILE_SIZE][TILE_SIZE], b_tile[TILE_SIZE][TILE_SIZE]; int tx = threadIdx.x, ty = threadIdx.y, row = blockIdx.y * blockDim.y + ty, col = blockIdx.x * blockDim.x + tx; float sum = 0.0f; for (int phase = 0; phase < (K + TILE_SIZE - 1) / TILE_SIZE; phase++) { a_tile[ty][tx] = (row < M && phase * TILE_SIZE + tx < K) ? A[row * K + phase * TILE_SIZE + tx] : 0.0f; b_tile[ty][tx] = (col < N && phase * TILE_SIZE + ty < K) ? B[(phase * TILE_SIZE + ty) * N + col] : 0.0f; __syncthreads(); for (int i = 0; i < TILE_SIZE; i++) { sum += a_tile[ty][i] * b_tile[i][tx]; } __syncthreads(); } if (row < M && col < N) { C[row * N + col] = sum; } }