18. Sparse Matrix-Vector Multiplication medium
Implement a GPU program that performs sparse matrix-vector multiplication.
Given a sparse matrix of dimensions and a dense vector of length ,
compute the product vector , which will have length . A is stored in row-major order.
nnz is the number of non-zero elements in A.
Mathematically, the operation is defined as:
The matrix is approximately 60 - 70% sparse.
Implementation Requirements
- Use only GPU native features (external libraries are not permitted)
- The
solvefunction signature must remain unchanged - The final result must be stored in vector
y
Example:
Input:
Matrix ():
Vector :
Output:
Vector :
Constraints
- 1 ≤
M,N≤ 10,000 - The matrix is approximately 60-70% sparse (i.e., 60-70% of elements are zero)
- Performance is measured with
M= 1,000,N= 10,000
Starter code (CUDA)
#include <cuda_runtime.h>
// A, x, y are device pointers
extern "C" void solve(const float* A, const float* x, float* y, int M, int N, int nnz) {}
From LeetGPU, CC BY-NC-ND 4.0.