75. Sparse Matrix-Dense Matrix Multiplication medium
Implement a GPU program that multiplies a sparse matrix A of dimensions M × N
by a dense matrix B of dimensions N × K, producing a dense output matrix
C of dimensions M × K.
All matrices are stored in row-major order using 32-bit floats.
The matrix A is approximately 60–70% sparse (i.e., 60–70% of elements are zero),
and nnz gives the number of non-zero elements in A.
Mathematically, the operation is defined as:
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 matrix
C
Example
Input:
Matrix ():
Matrix ():
Output:
Matrix ():
Constraints
- 1 ≤
M,N,K≤ 8,192 - All values in
AandBare 32-bit floats in the range [−10, 10] - The matrix
Ais approximately 60–70% sparse - Performance is measured with
M= 4,096,N= 2,048,K= 512
Starter code (CUDA)
#include <cuda_runtime.h>
// A, B, C are device pointers
extern "C" void solve(const float* A, const float* B, float* C, int M, int N, int K, int nnz) {}
From LeetGPU, CC BY-NC-ND 4.0.