advanced24 min studyLesson 478 in Engineering

Transpose kernels: coalesce the boundary and inspect bank mapping

Separate a transpose view from a physical layout conversion, then derive a scoped shared-memory padding example and its costs.

On this page

What you’ll understand

  • Map an out-of-place transpose's input and output coordinates.
  • Derive same-bank versus spread-bank accesses under a stated scalar bank model.
  • Account for padding and conversion cost without claiming universal speedup.

A transpose view can be a cheap metadata operation, while a physical transpose reads and writes every element. A conversion kernel may improve the next operation's layout, but it must justify its own traffic. Shared-memory tiling can reorder accesses so global reads and writes are more favorable, provided the local bank mapping and synchronization are also correct.

A view and a conversion solve different problems

For A with shape M by N, an out-of-place transpose stores B with shape N by M such that B[j,i]=A[i,j]. A metadata view can express that relationship by exchanging strides, but a dense row-major B requires actual data movement. A direct kernel can make one side contiguous across neighboring lanes and the other side strided. A tiled approach can read contiguous source regions, synchronize in shared storage, and have lanes write transposed regions with another favorable access pattern. The exact grouping determines coalescing on the target architecture.

Out-of-place conversion assumes input and output storage do not overlap in a way that changes unread values. An in-place transpose needs a different cycle or tile-exchange protocol; launching the same independent output kernel on aliased storage can create races. Partial tiles need masks on both load and store, while required cooperative participants still follow the synchronization plan. Include the conversion's allocation and lifetime in a pipeline analysis. Paying to convert once can be worthwhile across repeated consumers, but converting back after every operation can erase the expected gain.

Bji=Aij,Qideal=2MNsB_{ji}=A_{ij},\qquad Q_{\rm ideal}=2MNs
M,N are positive dimensions and s is bytes per element for an out-of-place same-dtype conversion. Q_ideal counts one input read and one output write at the selected external boundary, excluding extra traffic, allocation, padding storage, and overlapping/in-place algorithms.↔ Scroll wide equations horizontally.

Bank arithmetic needs an architecture-scoped access model

Consider the 32-bank, four-byte-word shared-memory mapping described in the archived CUDA 12.8 guide, with one NVIDIA warp issuing distinct scalar word accesses. Successive words map to successive banks. For a row-major tile with pitch P words, a column access by lane r addresses word rP+c relative to an appropriately specified base. Pitch 32 sends all thirty-two distinct column words to one bank; pitch 33 spreads them across banks. This reasoning concerns that scalar access pattern, not every vector instruction, element width, or accelerator.

Multiple lanes reading the same shared word can use a broadcast path rather than behaving like thirty-two distinct conflicting addresses. Distinguish that case from a column with equal bank indices but different words. Adding a padding column changes pitch and storage use while leaving the logical tile dimensions unchanged. Swizzling is another layout technique with its own mapping. An access model can predict a conflict mechanism, but measured speed also depends on global traffic, barriers, occupancy, instruction behavior, and compiler choices. Avoid promising a thirty-twofold kernel speedup from a bank-count calculation.

bank⁡(r,c)=(bword+rP+c) mod 32\operatorname{bank}(r,c)=(b_{\rm word}+rP+c)\bmod32
The formula assumes the stated 32-bank scalar 32-bit-word mapping, base word offset b_word, and pitch P in words. It is used for distinct word addresses in one scoped warp access. Broadcasts, wider/vector instructions, and other hardware mappings require separate treatment.↔ Scroll wide equations horizontally.

Measure whether the conversion earns its place

Compare a strided consuming kernel with a conversion-plus-contiguous-consumer path using identical values, precision, and output semantics. Report standalone conversion bandwidth and end-to-end latency separately. A copy-like logical byte count can produce an effective bandwidth even when measured device-memory traffic differs through caching or extra transactions. Use profiling evidence to test the proposed bank or coalescing bottleneck. Recheck tails and aliasing after changing tile layout, because padding can fix one access pattern while breaking a hard-coded stride elsewhere.

The modular lab shows only the arithmetic pattern in a smaller domain. Reset, set m=16 and additive step a=0, predict period one with every residue zero, then Play or Step. Separately Reset, set m=16 and a=1, predict period sixteen, then Play or Step. These are reachable controls; the instrument cannot set thirty-two banks or pitch thirty-three. It illustrates all-equal versus spread residues, not a bank-conflict simulator or timing model. Derive the actual thirty-two-bank example separately and keep its architecture assumptions visible.

Pad a thirty-two-by-thirty-two FP32 tile

Assume the scoped thirty-two-bank scalar-word model, base bank offset zero, and column c=0. Compare FP32 shared tiles with physical pitches 32 and 33 while their logical shape stays 32 by 32.

  1. With pitch 32, lane r accesses word 32r, whose bank is zero for every r from zero through thirty-one. These are distinct words, so the broadcast exception does not apply.

  2. With pitch 33, the bank is 33r mod32=r. The thirty-two lanes reach distinct banks under the stated model.

  3. Unpadded storage is 32×32×4=4,096 bytes; padded storage is 32×33×4=4,224 bytes. The added 128 bytes are 3.125% of the original storage, with no standalone promise about total kernel speed.

The result

A single extra physical column changes the scoped bank mapping at a small explicit storage cost. Logical transpose semantics and synchronization still need their own proof.

Explore the model

Interactive lab

Interactive lab · modular

Inspect equal versus spread residues

Reset, set m=16 and a=0, predict period 1 and residue 0 throughout, then Play or Step. Separately Reset, set m=16 and a=1, predict period 16, then Play or Step. The additive residue lab is not a 32-bank GPU model and has no pitch-33 or memory-instruction controls.

Inspect equal versus spread residuesAddition modulo m starts at zero: rₙ = na mod m. Playback advances two additions per second. The orbit has period m/gcd(m,a), including period one for a = 0. This is not a cryptographic security test or a primality algorithm. Current readings: Residue r0: 0; Orbit period · m/gcd(m,a): 12; Distinct residues visited: 1 of 12. All parameters are adjustable with the labeled controls below.Repeated addition modulo 12: the current residue is 0, and the orbit period is 12.01234567891011+ 5mod 12An additive orbitn = 0rₙ = 0Period = 12Cycle starts at 0:0 → 5 → 10 → 3 →8 → 1 → 6 → 11 →4 → 9 → 2 → 7 → 0Exact residues · two additions per second
Additive orbitCurrent residue / next edge
Residue r0
0
Orbit period · m/gcd(m,a)
12
Distinct residues visited
1 of 12
t = 0 s

Addition modulo m starts at zero: rₙ = na mod m. Playback advances two additions per second. The orbit has period m/gcd(m,a), including period one for a = 0. This is not a cryptographic security test or a primality algorithm. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forModulo arithmetic can explain a layout mechanism only after the real bank and access assumptions are specified.

Check your understanding

Why do thirty-two lanes reading the same shared word differ from reading thirty-two distinct words mapping to one bank?

Try it yourself

A same-dtype out-of-place transpose handles 1,024×2,048 four-byte elements. Find its ideal logical read-plus-write bytes and explain why this is not a measured DRAM-byte count.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Count both the input read and output write once.

Compare with the solution

There are 2,097,152 elements. One payload is 8,388,608 bytes, so ideal read-plus-write traffic is 16,777,216 bytes, exactly 16 MiB. Caches, uncoalesced transactions, extra loads, and implementation details can change actual device-memory traffic; the logical count defines a useful effective-bandwidth boundary rather than measuring it.

Take it with you

  • A physical layout conversion has traffic and lifetime costs beyond a view.
  • Bank conflicts depend on address mapping, access width, and word identity.
  • Padding can fix a scoped mechanism while whole-kernel performance still requires measurement.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.