PIXELBANKv9.1.0
Menu

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:

Input:
M = 64, N = 32
Output:
True
Reasoning:
  • The input values M = 64 and N = 32 are used to allocate a matrix a of size Mร—NM \times N.
  • The transpose_kernel function is launched, which loads a TPBร—TPBTPB \times TPB tile into shared memory with coalesced reads from the input matrix a.
  • The kernel then transposes the tile in shared memory and writes it out to the corresponding position in the output matrix out with coalesced writes, effectively computing out = a.T.
  • The resulting matrix out is compared to the reference solution a.T using np.allclose, and since the transpose operation is correctly implemented, the comparison returns True.

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
๐Ÿ”’

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
CUDA Shared-Memory Tiled Transpose - Hard | PixelBank