Compute the matrix-vector product on the GPU. Given a matrix A of shape (M,N) and a vector x of length N, produce an output vector y of length M where each entry is the dot product of one row of A with x:
y[i]=j=0∑N−1A[i,j]⋅x[j]Shapes follow the pattern (M,N)⋅(N,)→(M,). The matrix A is stored in row-major order, so element A[i,j] lives at flat index A[i*N + j]. All buffers are device pointers and all values are float32.
Input: A = [[1, 2, 3], [4, 5, 6]], x = [1, 1, 1], M = 2, N = 3
Output: [6, 15]
Row 0 sums to 1+2+3=6; row 1 sums to 4+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 x, giving 2, 3, and 2+3=5.
Map blockIdx.x * blockDim.x + threadIdx.x to the output row index i, then guard with if (i < M).
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.
Read row-major elements as A[i * N + j] and multiply by x[j] inside the inner loop.
Accepts: array
Accepts: array
Submit a correct solution to see your kernel performance.
Compute the matrix-vector product on the GPU. Given a matrix A of shape (M,N) and a vector x of length N, produce an output vector y of length M where each entry is the dot product of one row of A with x:
y[i]=j=0∑N−1A[i,j]⋅x[j]Shapes follow the pattern (M,N)⋅(N,)→(M,). The matrix A is stored in row-major order, so element A[i,j] lives at flat index A[i*N + j]. All buffers are device pointers and all values are float32.
Input: A = [[1, 2, 3], [4, 5, 6]], x = [1, 1, 1], M = 2, N = 3
Output: [6, 15]
Row 0 sums to 1+2+3=6; row 1 sums to 4+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 x, giving 2, 3, and 2+3=5.
Map blockIdx.x * blockDim.x + threadIdx.x to the output row index i, then guard with if (i < M).
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.
Read row-major elements as A[i * N + j] and multiply by x[j] inside the inner loop.
Accepts: array
Accepts: array
Submit a correct solution to see your kernel performance.