官方文档CUDA C++ Best Practices Guide中提到一个例子:对于7.0的设备,每个multiprocessor有65536个寄存器,且支持最大的并发线程束为64。要想每个multiprocessor达到100%的占用率,意味着每个线程最多能使用32个寄存器。可以使用官方提供的NVIDIA GPU Computing Toolkit\CUDA\v11.1\tools目录下的CUDA_Occupancy_Calculator.xls来计算相关参数。
For purposes of calculating occupancy, the number of registers used by each thread is one of the key factors. For example, on devices of compute capability 7.0 each multiprocessor has 65,536 32-bit registers and can have a maximum of 2048 simultaneous threads resident (64 warps x 32 threads per warp). This means that in one of these devices, for a multiprocessor to have 100% occupancy, each thread can use at most 32 registers. However, this approach of determining how register count affects occupancy does not take into account the register allocation granularity. For example, on a device of compute capability 7.0, a kernel with 128- thread blocks using 37 registers per thread results in an occupancy of 75% with 12 active 128-thread blocks per multi-processor, whereas a kernel with 320-thread blocks using the same 37 registers per thread results in an occupancy of 63% because only four 320-thread blocks can reside on a multiprocessor. Furthermore, register allocations are rounded up to the nearest 256 registers per block on devices with compute capability 7.0.
- 如果使用128 size的线程块(每个线程使用37个寄存器),会有12个活跃线程块/SM,从而有12*4(128/32,线程束/SM) = 48活跃线程束/SM,那么占用率为48/64=75%。
- 如果使用320 size的线程块(每个线程使用37个寄存器),会有4个活跃线程块/SM, 从而有4*10(320/32,线程束/SM) = 40活跃线程束/SM,那么占用率为40/64=62.5%。
这里最重要的数据是每个SM上的活跃线程块,这个数值受两个方面的资源限制,一个是寄存器资源,一个是共享内存的资源。
每个线程块使用128个线程
每个线程块使用320个线程