Calculate SM occupancy from threads, registers and shared memory

Occupancy is the fraction of an SM's warp slots that a kernel keeps busy. It is limited by whichever resource runs out first: thread slots, block slots, registers or shared memory. Tuning block size and register use against it is everyday kernel work. This is a simplified model simulated in Python with the limits passed in, not a real device query.

Write occupancy(threads_per_block, regs_per_thread, smem_per_block, max_threads=2048, max_blocks=32, regs_per_sm=65536, smem_per_sm=102400, reg_unit=256, smem_unit=256).
- warps_per_block = ceil(threads_per_block / 32). max_warps = max_threads // 32.
- Registers are allocated per warp, rounded UP to a multiple of reg_unit: regs_per_warp = ceil(regs_per_thread * 32 / reg_unit) * reg_unit.
- Shared memory per block is rounded UP to a multiple of smem_unit.
- Block limits: by threads = max_warps // warps_per_block; by blocks = max_blocks; by registers = regs_per_sm // (regs_per_warp * warps_per_block) (no limit if regs_per_thread is 0); by shared memory = smem_per_sm // rounded_smem (no limit if smem_per_block is 0).
- active_blocks = the smallest limit. occupancy = active_blocks * warps_per_block / max_warps.
- limiter = name of the smallest limit among "threads", "blocks", "registers", "shared_memory" (ties: first in that order).
Return (active_blocks, occupancy, limiter).

Signaturedef occupancy(threads_per_block, regs_per_thread, smem_per_block, max_threads=2048, max_blocks=32, regs_per_sm=65536, smem_per_sm=102400, reg_unit=256, smem_unit=256)

Example

256 threads, 32 registers, 0 shared memory: 8 warps per block, threads limit 8, registers 65536 // 8192 = 8, so (8, 1.0, "threads").

Understand the idea

Occupancy is the share of an SM's warp slots a kernel keeps filled. More resident warps help hide memory latency. Several resources cap how many blocks fit at once: thread slots, a block count limit, registers and shared memory, and the tightest one wins. Registers and shared memory are handed out in rounded-up units. Worked example: 128 threads, 64 registers per thread, 10000 bytes of shared memory, default limits. Warps per block = 4, max warps = 2048 / 32 = 64. Threads limit = 64 // 4 = 16. Blocks limit = 32. Registers per warp = ceil(64 * 32 / 256) * 256 = 2048, per block 8192, limit 65536 // 8192 = 8. Shared memory rounds up to 10240, limit 102400 // 10240 = 10. Active blocks = 8, occupancy = 8 * 4 / 64 = 0.5, limiter 'registers'. Rule: compute the four limits (no limit for zero registers or zero shared memory), take the minimum, occupancy = active_blocks * warps_per_block / max_warps, and name the first limiter in the order threads, blocks, registers, shared_memory on ties. Used when tuning block size and register use in CUDA kernels.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks the hidden tests with Pro.

  • Sample 1: worked exampleoccupancy(256, 32, 0)
  • Sample 2: register bound with roundingoccupancy(128, 72, 0)