记录 Fortran CUDA 编程相关问题及实现
Question
- CUDA 和 GPU 软硬件映射关系
- 如何进行 CUDA 线程分配
- 如何调用 CUDA 接口进行并行计算
- 如何优化 Cache 管理
- 如何进行 异构 交互
Introduction
- Compute Unified Device Architecture
- GPU
- SM: Streaming Multiprocessor
- SM = Compute Units Registers + L1 + SHM
-注意 L1 和 SHM (shared memory) 共用 cache
- CUDA
- Kernel 1 (host) => Grid 1 (Device)
- Gird 1 => multi-block => multi-thread
- gridDim blockId threadId
- 4096 16 256
- GPU
- Device => SM => Core
- Memory => L1/SHM => Register
- 需要注意: SM 可映射多个 block,Core 也会映射不同的 thread,执行时自动调度
- SIMT: Single Instruction Multiple Threads
- Run time divergence
- Cache 管理
- memory coalescing: 数据内存连续
- banks confict: 不同 thread 访问相同 shared memory 会造成冲突,而 broadcast 操作可以执行
GPU – CPU
- CPU
- Core - L1 Cache/Control - L2 Cache - L3 Cache - DRAM
- GPU
- Multi-Core - L1 Cache/Control - L2 Cache - DRAM
- Use parallelization to hide latency
- Heterogeneous Interaction
- 阻塞
Debug
cuda-gdb: cuda block 1 thread 3
- Profile
Optimization
- CPU – GPU 管理:数据转换,同时计算
- GPU 计算资源分配:管理 block, cache 等大小
- 循环及循环展开 (unrolling)
- 进行 Cache 管理
- 避免原子操作 (atomic ops),但 shared memory 中很快
- 使用成熟库 Thrust, CUB, CuTensor, CuBLAS, CUTLASS
- PyTorch 很快
- Software pipelining