Skip to content

Repository files navigation

LeetGPU

CUDA, PyTorch, and Triton implementations for selected LeetGPU exercises.

Each exercise directory owns its implementations and metadata:

  • src_cuda.cu: CUDA implementations.
  • cuda_registry.cuh: implementation names, test cases, benchmark axes.
  • test_cuda.cu: GoogleTest correctness tests.
  • benchmark_cuda.cu: NVBench benchmarks.
  • src_python.py / src_triton.py: Python framework implementations when available.

Requirements

  • CUDA Toolkit 12.8+ or 13.x.
  • CMake 3.30+.
  • A C++17 host compiler supported by CUDA.
  • Python 3.12+ and uv.

This repo is tested on WSL2 Ubuntu 24.04 with a GeForce RTX 3070 Laptop GPU and CUDA 13.0.

Linux / WSL

uv sync
make build
make test
make py-test

Release benchmarks:

make build-release
make bench

Run a single benchmark implementation with NVBench axes:

./build/001e_vector_addition_benchmark --axis "implementation=basic" --axis "N=1048576"
./build/001m_reduction_benchmark --axis "implementation=cub" --axis "N=16777216"
./build/002e_matrix_multiplication_benchmark --axis "implementation=smem_2d" --axis "case=square_512"

CMake presets are also available:

cmake --preset linux-debug
cmake --build --preset linux-debug
ctest --preset linux-debug

Windows Visual Studio

Install CUDA Toolkit, Visual Studio 2022 with C++ build tools, CMake, and Python 3.12. From a Developer PowerShell:

cmake --preset windows-vs-debug
cmake --build --preset windows-vs-debug
ctest --preset windows-vs-debug

Open build/windows-vs/LeetGPU.sln to debug individual *_test or *_benchmark targets.

You can also open this repository directly from Visual Studio:

  1. Use File -> Open -> Folder... and select D:\Projects\LeetGPU.
  2. Select the windows-vs-debug CMake preset.
  3. Wait for CMake configure to finish.
  4. Open View -> Other Windows -> CMake Targets View.
  5. Right-click a target such as 002e_matrix_multiplication_test and choose Debug.

Benchmark targets accept NVBench arguments in the Visual Studio project debug settings. Examples:

--profile --axis implementation=basic --axis N=1048576
--profile --axis implementation=cub --axis N=16777216
--profile --axis implementation=smem_2d --axis case=square_512

Common Visual Studio issues:

  • If CUDA is not detected, confirm the Windows CUDA Toolkit is installed and integrated with Visual Studio, then retry from a Developer PowerShell.

  • If the windows-vs-debug preset is missing, open the folder that contains CMakePresets.json, not the build/ directory.

  • If architecture detection fails, configure manually with an explicit value, for example -DCMAKE_CUDA_ARCHITECTURES=86 for an RTX 3070 or 90 for H100.

  • Triton is only a Linux dependency in this project. Windows Visual Studio is intended for the C++/CUDA tests and benchmarks.

  • NVBench and GTest are fetched by CMake on first configure, so the first Visual Studio configure can take a while and requires network access.

  • If CTest reports a target such as 002e_matrix_multiplication_test_NOT_BUILT, that test executable was not built or its GoogleTest discovery step did not run. Build the target explicitly, then rerun CTest:

    cmake --build --preset windows-vs-debug --target 002e_matrix_multiplication_test
    ctest --preset windows-vs-debug

    If the explicit build fails, fix the first compile or link error in that build output. If the explicit build succeeds but CTest still reports NOT_BUILT, remove the stale Visual Studio build directory and configure again:

    Remove-Item -Recurse -Force build\windows-vs
    cmake --preset windows-vs-debug
    cmake --build --preset windows-vs-debug
    ctest --preset windows-vs-debug

Python

uv sync
uv run pytest -rs
uv run ruff check .

The default Python tests cover PyTorch and Triton. The PyTorch C++/CUDA extension path is optional because it invokes just-in-time compilation:

LEETGPU_TEST_TORCH_EXTENSION=1 uv run pytest 001e-vector-addition -rs

Validation

Last validated in WSL2 with CUDA 13.0:

make clean && make build
ctest --test-dir build --output-on-failure
make py-test
uv run ruff check .
uv run ruff format --check .

The CUDA suite currently discovers 136 tests across vector addition, reduction, and matrix multiplication.

Nsight Compute

Generated .ncu-rep files are ignored by git. Example:

mkdir -p out/profiles
ncu --set=full \
  -f \
  --kernel-name-base demangled \
  --kernel-name 'regex:vector_add' \
  -o out/profiles/vector_add \
  ./build/001e_vector_addition_benchmark \
  --profile \
  --axis "implementation=basic" \
  --axis "N=67108864"

Open the report in Nsight Compute GUI on Windows, or inspect it with:

ncu -i out/profiles/vector_add.ncu-rep

About

No description or website provided.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages