CUDA Naive Matrix Transpose
Problem Statement
Transpose a 2D matrix with a 2D grid: out[j, i] = a[i, j], where a is (M, N) and out is (N, M).
Background
Each thread reads one element of a at (i, j) and writes it to the swapped position (j, i) in out. This "naive" transpose has uncoalesced writes — a later problem fixes that with shared memory — but it's the clearest place to start.
Your Task
Implement transpose_kernel and run(M=64, N=48) returning whether the result equals 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 = 48
True
- The input values
M = 64andN = 48define the dimensions of the 2D matrixaas . - The
transpose_kernelfunction is launched with a 2D grid, where each thread reads one element ofaat position(i, j)and writes it to the swapped position(j, i)in the output matrixout. - The resulting
outmatrix has dimensions , which is the transpose of the original matrixa, i.e., . - The
runfunction compares the resultingoutmatrix with the reference transpose usingnp.allcloseand returnsTrueif they are equal, which is the case for the given sample input.
Constraints
- i, j = cuda.grid(2) index into a (shape M x N)
- out has shape N x M; write out[j, i] = a[i, j]
- Guard with if i < M and j < N