Count shared-memory bank conflict passes per warp
Shared memory is split into banks. If two threads in a warp hit different words in the same bank, the access is replayed (serialized). Reading the same word from many threads is a broadcast and costs nothing extra. Padding a tile by one column is the classic fix for column reads. This is a simplified model simulated in Python, not a measurement of real hardware.
Write smem_passes(byte_addrs, access_bytes=4, num_banks=32, warp_size=32).
- byte_addrs: one byte address per thread, in thread order, or -1 for an inactive thread. Warps are consecutive groups of warp_size threads (the last may be partial).
- Each active thread touches access_bytes / 4 consecutive 4-byte words starting at word addr // 4 (addresses are multiples of access_bytes).
- bank(word) = word % num_banks.
- For one warp: passes = max over banks of the number of DISTINCT words of that bank touched by the warp, or 0 if no thread is active.
Return a list with the number of passes for each warp.
def smem_passes(byte_addrs, access_bytes=4, num_banks=32, warp_size=32)Example
32 threads reading a column of a 32 x 32 float tile, addresses 4 * (32 * i), all hit bank 0 with different words: [32]. With one padding column, 4 * (33 * i), every thread hits its own bank: [1].
Understand the idea
Shared memory is split into banks, each serving one 4-byte word per cycle. If threads in a warp ask one bank for different words, those requests are served one after another, called a bank conflict. If they ask for the same word, it is broadcast for free. So a warp's cost is set by its busiest bank, counting distinct words only. Worked example with 32 banks: thread i reads byte 8 * i, so word 2i, bank 2i % 32. Banks 0, 2, ..., 30 are each hit by two distinct words (from threads i and i + 16), so 2 passes. If every thread reads the same address, there is one word, so 1 pass. A column read of a 32 x 32 float tile hits bank 0 with 32 different words: 32 passes; padding each row to 33 words spreads them over all banks: 1 pass. Rule: for each active thread, its words run from addr // 4 for access_bytes / 4 words; bank = word % num_banks; passes = max over banks of the number of distinct words; a warp with no active threads (all -1) gives 0. Padding is the standard fix in transpose kernels.