TensorTonic
Problems
Matrix Multiplication as a CUDA Kernel
Linear AlgebraGPU Programming
Medium

Implement a CUDA kernel that computes the matrix product C=A⋅BC = A \cdot BC=A⋅B, where AAA has shape (M,K)(M, K)(M,K) and BBB has shape (K,N)(K, N)(K,N), producing CCC of shape (M,N)(M, N)(M,N). For each output element:

C[i,j]=∑k=0K−1A[i,k]⋅B[k,j]C[i, j] = \sum_{k=0}^{K-1} A[i, k] \cdot B[k, j]C[i,j]=k=0∑K−1​A[i,k]⋅B[k,j]

All buffers are stored in row-major layout: A\texttt{A}A has M⋅KM \cdot KM⋅K floats, B\texttt{B}B has K⋅NK \cdot NK⋅N floats, and C\texttt{C}C has M⋅NM \cdot NM⋅N floats. The kernel writes one output element per thread into the preallocated C\texttt{C}C buffer.

Loading visualization...

Examples

Input: M = 2, K = 3, N = 2, A = [[1, 2, 3], [4, 5, 6]], B = [[7, 8], [9, 10], [11, 12]]

Output: C = [[58, 64], [139, 154]]

Each output cell is a dot product of an A row and a B column.

Input: M = 3, K = 2, N = 3, A = [[1, 0], [0, 1], [1, 1]], B = [[2, 3, 4], [5, 6, 7]]

Output: C = [[2, 3, 4], [5, 6, 7], [7, 9, 11]]

Rectangular product where M, K, N all differ.

Hint 1

The inner loop runs over the shared dimension KKK, which appears in both A\texttt{A}A and B\texttt{B}B but not in C\texttt{C}C.

Hint 2

Each output cell C[i,j]C[i, j]C[i,j] is independent of every other output cell, so one thread can own one (i,j)(i, j)(i,j) pair.

Requirements

  • Use one thread per output element C[i,j]C[i, j]C[i,j]
  • Accumulate the inner product over the shared dimension KKK
  • Respect row-major layout: A[i, k] = A[i*K + k]\texttt{A[i, k] = A[i*K + k]}A[i, k] = A[i*K + k] and B[k, j] = B[k*N + j]\texttt{B[k, j] = B[k*N + j]}B[k, j] = B[k*N + j]
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M,N,K≤10241 \le M, N, K \le 10241≤M,N,K≤1024
  • Input and output buffers are device pointers
  • Use a 16 by 16 thread block
  • Values fit in 32-bit float
Try Similar Problems
RMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumMatrix-Vector Multiplication as a CUDA KernelMediumOuter Product as a CUDA KernelEasyMatrix Addition as a CUDA KernelEasyHadamard Product as a CUDA KernelEasyMatrix Transpose as a CUDA KernelEasyL1 Normalization as a CUDA KernelMediumCosine Similarity as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMediumL2 Normalization as a CUDA KernelMediumTiled Matrix Multiplication as a CUDA KernelHard

Accepts: array

Accepts: array

Accepts: number

Accepts: number

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Matrix Multiplication as a CUDA Kernel

Matrix Multiplication as a CUDA Kernel

Linear AlgebraGPU Programming
Medium

Implement a CUDA kernel that computes the matrix product C=A⋅BC = A \cdot BC=A⋅B, where AAA has shape (M,K)(M, K)(M,K) and BBB has shape (K,N)(K, N)(K,N), producing CCC of shape (M,N)(M, N)(M,N). For each output element:

C[i,j]=∑k=0K−1A[i,k]⋅B[k,j]C[i, j] = \sum_{k=0}^{K-1} A[i, k] \cdot B[k, j]C[i,j]=k=0∑K−1​A[i,k]⋅B[k,j]

All buffers are stored in row-major layout: A\texttt{A}A has M⋅KM \cdot KM⋅K floats, B\texttt{B}B has K⋅NK \cdot NK⋅N floats, and C\texttt{C}C has M⋅NM \cdot NM⋅N floats. The kernel writes one output element per thread into the preallocated C\texttt{C}C buffer.

Loading visualization...

Examples

Input: M = 2, K = 3, N = 2, A = [[1, 2, 3], [4, 5, 6]], B = [[7, 8], [9, 10], [11, 12]]

Output: C = [[58, 64], [139, 154]]

Each output cell is a dot product of an A row and a B column.

Input: M = 3, K = 2, N = 3, A = [[1, 0], [0, 1], [1, 1]], B = [[2, 3, 4], [5, 6, 7]]

Output: C = [[2, 3, 4], [5, 6, 7], [7, 9, 11]]

Rectangular product where M, K, N all differ.

Hint 1

The inner loop runs over the shared dimension KKK, which appears in both A\texttt{A}A and B\texttt{B}B but not in C\texttt{C}C.

Hint 2

Each output cell C[i,j]C[i, j]C[i,j] is independent of every other output cell, so one thread can own one (i,j)(i, j)(i,j) pair.

Requirements

  • Use one thread per output element C[i,j]C[i, j]C[i,j]
  • Accumulate the inner product over the shared dimension KKK
  • Respect row-major layout: A[i, k] = A[i*K + k]\texttt{A[i, k] = A[i*K + k]}A[i, k] = A[i*K + k] and B[k, j] = B[k*N + j]\texttt{B[k, j] = B[k*N + j]}B[k, j] = B[k*N + j]
  • The solve\texttt{solve}solve signature must not change

Constraints

  • 1≤M,N,K≤10241 \le M, N, K \le 10241≤M,N,K≤1024
  • Input and output buffers are device pointers
  • Use a 16 by 16 thread block
  • Values fit in 32-bit float
Try Similar Problems
RMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumMatrix-Vector Multiplication as a CUDA KernelMediumOuter Product as a CUDA KernelEasyMatrix Addition as a CUDA KernelEasyHadamard Product as a CUDA KernelEasyMatrix Transpose as a CUDA KernelEasyL1 Normalization as a CUDA KernelMediumCosine Similarity as a CUDA KernelMediumCUDA Dot Product: Multi-Block ReductionMediumL2 Normalization as a CUDA KernelMediumTiled Matrix Multiplication as a CUDA KernelHard

Accepts: array

Accepts: array

Accepts: number

Accepts: number

Accepts: number

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1