BLOGLLM Logo
Go Pro
Guest
Not signed in
All BlogsCoursesPracticeInterview prepSeriesTagsBookmarksPricing
Log in
All BlogsCoursesPracticeTagsBookmarksPricing
My PrepProblemsStudy PlansResearch PapersRoadmapInterviewContestsLeaderboardGames
Articles
Practice / GPU kernels (simulated)10 of 24 in topic

681. Calculate SM occupancy from threads, registers and shared memory

MediumGPU kernels (simulated) Pro

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).

Functiondef 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 1

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

Example 2

Input: threads_per_block = 128, regs_per_thread = 72, smem_per_block = 0

Output: (7, 0.4375, 'registers')

Constraints

  • Python 3 standard library. Code runs in your browser.
  • Run checks the sample tests. Submit checks the full hidden test set (Pro).
Hint 1

Active blocks are capped by four separate resources, and the smallest cap wins; occupancy follows from that number.

Hint 2

Compute warps per block, then the four block limits (threads, blocks, registers, shared memory) in that order, take the minimum, compute occupancy, and name the first limit that equals the minimum.

Hint 3

Round up with integer math, like -(-a // b); registers per warp are ceil(regs_per_thread * 32 / reg_unit) * reg_unit. Use a very large number as the limit when regs or shared memory is 0, and lims.index(min(lims)) gives the first in order on ties.

Common mistakes
  • The buggy version divides by raw usage, which overestimates how many blocks fit when the count is just over a unit boundary.
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'.

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.

Topics
GPU kernels (simulated)
Similar questions
  • Count global memory loads for naive vs tiled matmulMedium
  • Reduce a warp with butterfly XOR shufflesMedium
  • Find the argmax with a parallel tree reduction and lowest-index tiesMedium
  • Simulate a warp-per-row matrix-vector kernelMedium
All GPU kernels (simulated) problems
Report
681 / 903
Python 3solution.pyPython idle
1
2
3
4
def 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):
# your code here
pass
​

Run checks these cases in your browser, no sign-in needed. Submit checks the full hidden test set with Pro.

Ctrl/Cmd + Enter to run
© BlogLLM, Bengaluru, IndiaAboutTermsPrivacyRefundsDMCAContactRSS