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

Compute the matrix-vector product on the GPU. Given a matrix AAA of shape (M,N)(M, N)(M,N) and a vector xxx of length NNN, produce an output vector yyy of length MMM where each entry is the dot product of one row of AAA with xxx:

y[i]=∑j=0N−1A[i,j]⋅x[j]y[i] = \sum_{j=0}^{N-1} A[i, j] \cdot x[j]y[i]=j=0∑N−1​A[i,j]⋅x[j]

Shapes follow the pattern (M,N)⋅(N,)→(M,)(M, N) \cdot (N,) \to (M,)(M,N)⋅(N,)→(M,). The matrix AAA is stored in row-major order, so element A[i,j]A[i, j]A[i,j] lives at flat index A[i*N + j]\texttt{A[i*N + j]}A[i*N + j]. All buffers are device pointers and all values are float32.

Loading visualization...

Examples

Input: A = [[1, 2, 3], [4, 5, 6]], x = [1, 1, 1], M = 2, N = 3

Output: [6, 15]

Row 000 sums to 1+2+3=61+2+3=61+2+3=6; row 111 sums to 4+5+6=154+5+6=154+5+6=15.

Input: A = [[1, 0], [0, 1], [1, 1]], x = [2, 3], M = 3, N = 2

Output: [2, 3, 5]

Each row picks or sums the entries of xxx, giving 222, 333, and 2+3=52+3=52+3=5.

Hint 1

Map blockIdx.x * blockDim.x + threadIdx.x to the output row index i, then guard with if (i < M).

Hint 2

Each output y[i] is an independent dot product, so a single thread can accumulate it in a register with a serial loop over j.

Hint 3

Read row-major elements as A[i * N + j] and multiply by x[j] inside the inner loop.

Requirements

  • Launch one thread per output row so each thread computes a single entry y[i]y[i]y[i].
  • Inside the kernel, run an inner loop over jjj from 000 to N−1N - 1N−1 to accumulate the dot product.
  • Index AAA with the row-major formula A[i*N + j]\texttt{A[i*N + j]}A[i*N + j]; the harness allocates AAA as a contiguous M⋅NM \cdot NM⋅N buffer.
  • Do not change the solve\texttt{solve}solve signature; the runner binds it to a fixed ctypes prototype.

Constraints

  • 1≤M,N≤40961 \le M, N \le 40961≤M,N≤4096.
  • A\texttt{A}A, x\texttt{x}x, y\texttt{y}y are device pointers managed by the harness; do not allocate or copy them inside solve\texttt{solve}solve.
  • Launch with 256256256 threads per block and a 1D grid sized to cover MMM output rows.
  • All buffers are float32.
Try Similar Problems
RMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumMatrix 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

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Matrix-Vector Multiplication as a CUDA Kernel

Matrix-Vector Multiplication as a CUDA Kernel

Linear AlgebraGPU Programming
Medium

Compute the matrix-vector product on the GPU. Given a matrix AAA of shape (M,N)(M, N)(M,N) and a vector xxx of length NNN, produce an output vector yyy of length MMM where each entry is the dot product of one row of AAA with xxx:

y[i]=∑j=0N−1A[i,j]⋅x[j]y[i] = \sum_{j=0}^{N-1} A[i, j] \cdot x[j]y[i]=j=0∑N−1​A[i,j]⋅x[j]

Shapes follow the pattern (M,N)⋅(N,)→(M,)(M, N) \cdot (N,) \to (M,)(M,N)⋅(N,)→(M,). The matrix AAA is stored in row-major order, so element A[i,j]A[i, j]A[i,j] lives at flat index A[i*N + j]\texttt{A[i*N + j]}A[i*N + j]. All buffers are device pointers and all values are float32.

Loading visualization...

Examples

Input: A = [[1, 2, 3], [4, 5, 6]], x = [1, 1, 1], M = 2, N = 3

Output: [6, 15]

Row 000 sums to 1+2+3=61+2+3=61+2+3=6; row 111 sums to 4+5+6=154+5+6=154+5+6=15.

Input: A = [[1, 0], [0, 1], [1, 1]], x = [2, 3], M = 3, N = 2

Output: [2, 3, 5]

Each row picks or sums the entries of xxx, giving 222, 333, and 2+3=52+3=52+3=5.

Hint 1

Map blockIdx.x * blockDim.x + threadIdx.x to the output row index i, then guard with if (i < M).

Hint 2

Each output y[i] is an independent dot product, so a single thread can accumulate it in a register with a serial loop over j.

Hint 3

Read row-major elements as A[i * N + j] and multiply by x[j] inside the inner loop.

Requirements

  • Launch one thread per output row so each thread computes a single entry y[i]y[i]y[i].
  • Inside the kernel, run an inner loop over jjj from 000 to N−1N - 1N−1 to accumulate the dot product.
  • Index AAA with the row-major formula A[i*N + j]\texttt{A[i*N + j]}A[i*N + j]; the harness allocates AAA as a contiguous M⋅NM \cdot NM⋅N buffer.
  • Do not change the solve\texttt{solve}solve signature; the runner binds it to a fixed ctypes prototype.

Constraints

  • 1≤M,N≤40961 \le M, N \le 40961≤M,N≤4096.
  • A\texttt{A}A, x\texttt{x}x, y\texttt{y}y are device pointers managed by the harness; do not allocate or copy them inside solve\texttt{solve}solve.
  • Launch with 256256256 threads per block and a 1D grid sized to cover MMM output rows.
  • All buffers are float32.
Try Similar Problems
RMS Normalization as a CUDA KernelMediumLayer Normalization as a CUDA KernelMediumMatrix 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

You must run your code first.

Kernel performance

Submit a correct solution to see your kernel performance.

Problems
Loading...
1 / 1