🇨🇳 简体中文

Quick Start

Get GPU SpMV installed and running in 5 minutes.

Table of Contents

  1. Requirements
    1. Check CUDA Installation
  2. Installation
    1. 1. Clone Repository
    2. 2. Build Project
    3. 3. Run Tests
  3. First Program
    1. Compile and Run
  4. Next Steps
  5. FAQ
    1. Q: Build fails, cannot find CUDA?
    2. Q: Tests fail?

Requirements

Component Minimum Recommended
CUDA Toolkit 11.0 12.0+
CMake 3.18 3.25+
C++ Compiler GCC 7+ / MSVC 2019+ GCC 11+ / MSVC 2022+
NVIDIA GPU CC 7.0 (Volta) CC 8.6+ (Ampere)
GPU Memory 4 GB 8 GB+

Check CUDA Installation

1
2
nvcc --version
nvidia-smi

Installation

1. Clone Repository

1
2
git clone https://github.com/LessUp/gpu-spmv.git
cd gpu-spmv

2. Build Project

Using CMake Presets (recommended):

1
2
3
# Release build
cmake --preset release
cmake --build --preset release

Or traditional way:

1
2
3
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)

3. Run Tests

1
2
3
4
5
# Run all tests
ctest --preset default

# Or run directly
./build-release/spmv_tests

First Program

Create first_spmv.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <spmv/spmv.h>
#include <cstdio>

int main() {
    // Create 3x3 sparse matrix: [1 0 2; 0 3 4; 0 0 5]
    float dense[] = {1, 0, 2, 0, 3, 4, 0, 0, 5};
    
    CSRMatrix* csr = csr_create(3, 3, 5);
    csr_from_dense(csr, dense, 3, 3);
    csr_to_gpu(csr);
    
    // Prepare input vector x = [1, 1, 1]
    float h_x[] = {1, 1, 1};
    CudaBuffer<float> d_x(3), d_y(3);
    cudaMemcpy(d_x.data(), h_x, 3 * sizeof(float), cudaMemcpyHostToDevice);
    
    // Execute SpMV: y = A * x
    SpMVConfig config = spmv_auto_config(csr);
    SpMVResult result = spmv_csr(csr, d_x.data(), d_y.data(), &config, 3);
    
    if (result.error == SpMVError::SUCCESS) {
        printf("Success! Time: %.3f ms\n", result.time_ms);
        
        // Read result
        float h_y[3];
        cudaMemcpy(h_y, d_y.data(), 3 * sizeof(float), cudaMemcpyDeviceToHost);
        printf("Result: [%.0f, %.0f, %.0f]\n", h_y[0], h_y[1], h_y[2]);
        // Output: [3, 7, 5]
    }
    
    csr_destroy(csr);
    return 0;
}

Compile and Run

1
2
3
4
5
6
7
8
# Compile
nvcc -o first_spmv first_spmv.cpp \
    -I./include \
    -L./build-release -lgpu_spmv \
    -lcudart

# Run
./first_spmv

Next Steps


FAQ

Q: Build fails, cannot find CUDA?

Ensure CUDA is properly installed and environment variables are set:

1
2
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

Q: Tests fail?

Check if GPU is available:

1
nvidia-smi

If no GPU, use CPU-only test mode:

1
cmake --preset minimal

Problems? Submit Issue