CUDA Shared-Memory Tiled Transpose
Problem Statement
Transpose a matrix using a shared-memory tile so both the reads and the writes to global memory are coalesced: out = a.T. Assume M and N are multiples of the tile size TPB.
Background
The naive transpose has uncoalesced writes. Fix it by loading a TPB x TPB tile into shared memory with coalesced reads, cuda.syncthreads(), then writing the tile out to the transposed block position — reading tile[ty, tx] so the global write is again coalesced.
Your Task
Implement transpose_kernel and run(M=64, N=32) comparing to a.T.
How it is tested
Your solution must define a top-level function run(...) that allocates the inputs, copies them to the GPU, launches your @cuda.jit kernel, and returns a Python bool from np.allclose(gpu_result, reference). The grader prints run(...); the expected output is True.
Example
M = 64, N = 32
True
- The input values
M = 64andN = 32are used to allocate a matrixaof size . - The
transpose_kernelfunction is launched, which loads a tile into shared memory with coalesced reads from the input matrixa. - The kernel then transposes the tile in shared memory and writes it out to the corresponding position in the output matrix
outwith coalesced writes, effectively computingout = a.T. - The resulting matrix
outis compared to the reference solutiona.Tusingnp.allclose, and since the transpose operation is correctly implemented, the comparison returnsTrue.
Constraints
- Load tile[tx, ty] = a[x, y] (coalesced read), then cuda.syncthreads()
- Write to the swapped block: out[blockIdx.yTPB + tx, blockIdx.xTPB + ty] = tile[ty, tx]
- M and N are multiples of TPB