跳转到内容

01-SGEMM 教程

本模块是 CUDA Kernel Academy 的入门教程,聚焦于 SGEMM 的渐进式优化过程。

内容概览

  • Naive SGEMM
  • Shared-memory tiled SGEMM
  • Bank-conflict-free SGEMM
  • Double-buffered SGEMM
  • Tensor Core / WMMA SGEMM
  • 与 cuBLAS 对比的 benchmark
  • 基于 GoogleTest 的正确性测试

目录结构

text
01-sgemm-tutorial/
├── src/
│   ├── kernels/
│   ├── utils/
│   └── main.cu
├── tests/
│   └── test_sgemm.cu
├── Makefile
└── README.md

构建方式

该模块是独立教程模块,不参与根目录 CMake 构建。

构建 benchmark

bash
cd 01-sgemm-tutorial
make GPU_ARCH=sm_86
./build/sgemm_benchmark

运行测试

bash
cd 01-sgemm-tutorial
make test

说明:

  • make test 依赖系统可用的 GoogleTest(例如通过包管理器安装 gtest / libgtest-dev)。
  • GPU_ARCH 可按本机 GPU 调整,例如 sm_80sm_86sm_89sm_90

学习路径

1Baseline

Naive Global Memory

  • 每个线程读取 A/B 全局内存一次
  • 无共享内存复用
  • 内存带宽成为瓶颈
2↑ 4×

Shared Memory Tiling

  • A/B 分块载入 Shared Memory
  • 线程协作加载,减少全局访问
  • 计算与访存比例提升
3↑ 6×

Coalesced Access

  • 调整线程到数据的映射
  • 全局内存合并访问 (128B)
  • Shared Memory Bank Conflict 消除
4↑ 9×

Double Buffering

  • Software Pipeline 隐藏延迟
  • 预取下一块数据到寄存器
  • 计算与访存重叠
5↑ 14×

Vectorized Load

  • float4 / half2 向量加载
  • 减少指令数,提升吞吐
  • 寄存器压力可控
6↑ 18×

Tensor Core (WMMA)

  • Warp-level矩阵乘法指令
  • 混合精度 FP16/BF16
  • 峰值算力逼近理论值
7↑ 42×

cuBLAS (Reference)

  • NVIDIA 官方优化库
  • 自动调优与内核选择
  • 多版本架构适配

适合什么人

  • 想理解 CUDA GEMM 优化基本路径的学习者
  • 想从最小示例入手理解 shared memory、bank conflict、double buffering、WMMA 的读者

相关模块

References

Released under the MIT License.