Worked examples — Coalesced memory access
6.2.8 · D3· Hardware › GPU Architecture › Coalesced memory access
Yeh deep-dive parent topic ke andar hai. Wahan humne seekha ki coalescing kya hoti hai. Yahan hum har tarah ke access patterns jo ek warp produce kar sakta hai — perfect, offset, strided, mixed, degenerate, aur ek real word problem — ke through grind karte hain, taaki wild mein koi bhi access pattern mile toh tumne already use paper par solve kar rakha ho.
Is poore page ke ground rules
Taaki har number checkable ho, hum ek fixed model architecture use karte hain saare examples ke liye (bilkul parent ka Volta-style model):
Sab kuch reduce hota hai "32 addresses kitne distinct sectors mein land karte hain?" par. Us number ko kahte hain. Wahi ek count transactions, bytes fetched, aur efficiency decide karta hai.
Address-ramp metaphor (hamara "regime map")
Matrix se pehle, ek picture har example ko jodti hai. Thread index ko horizontal axis par plot karo aur uska byte address vertical axis par. Har access pattern bas dots ki ek line (ya scatter) hai — ek address ramp. Us ramp ka slope stride hai, aur slope akela decide karta hai tum kitne coalesced ho. Yeh hamara stand-in hai "kaun sa quadrant / kaun sa sign" ke liye jo tum angles ke saath karte ho: quadrants ki jagah hamare paas slope regimes hain, figure mein labelled bands ki tarah.

Chaar slope regimes exactly hamare case split hain (example IDs neeche matrix aur worked sections mein fully introduce kiye gaye hain):
- Slope (flat): broadcast — Example 7.
- Slope (gentle, unbroken): coalesced — Examples 1, 6; Example 2 mein ek shift se toota hua.
- Slope strictly aur ke beech (medium): partial waste — Examples 3, 9.
- Slope (steep): ek transaction per thread, floor — Examples 4, 5, aur Example 8 ka column-access half (Example 8a, neeche padho is bullet par rely karne se pehle).
- Koi single slope nahi (scatter): mixed/irregular — Example 8b.
Numbers kahan se aate hain (T = 32 B arbitrary nahi hai)
Scenario matrix
Har access pattern jo ek warp throw kar sakta hai inhi cells mein se ek mein aata hai, cell order A → I mein listed. Jo examples follow karte hain woh har cell cover karte hain. Aakhri do cells woh hain jo students underestimate karte hain: Cell I "exam twist" hai — ek struct { float x, y, z; } ka ek field padhna (ek array-of-structs, AoS), jahan ek hidden 12-byte stride coalescing kharab kar deta hai — jise hum Example 9 mein fully build karte hain. Cell J ek mixed warp hai bina kisi single stride ke, Example 8b mein build kiya gaya.
| Cell | Pattern class | Isme kya khaas hai | Covered by |
|---|---|---|---|
| A | Perfectly sequential | stride = 1 element, aligned start | Example 1 |
| B | Sequential lekin misaligned start | consecutive, non-multiple of se offset | Example 2 |
| C | Small stride (2 elements) | partial waste, kuch sharing | Example 3 |
| D | Stride = exactly ek sector | boundary case, zero sharing | Example 4 |
| E | Huge stride (worst case) | ek transaction per thread | Example 5 |
| F | Reversed / permuted order | same addresses, shuffled threads | Example 6 |
| G | Degenerate: sab threads same address | broadcast, minimum possible | Example 7 |
| H | Real-world word problem (matrix rows vs cols) | layout choice kyun matter karta hai | Example 8a |
| I | Exam twist: struct of 3 floats (AoS) |
element size ≠ 4, hidden stride | Example 9 |
| J | Mixed / irregular warp (koi single stride nahi) | aadha sequential, aadha scattered | Example 8b |
Har row upar ke ramp picture ke ek slope regime se map hoti hai: increasing vs decreasing (Cell F), aligned vs shifted (Cell B), zero-slope (Cell G), steep limiting case (Cell E), aur no-single-slope scatter (Cell J).
Example 1 — Cell A: Perfect sequential (baseline)
Pehle forecast karo: Aage padhne se pehle sector count guess karo. (Hint: 32 floats = kitne bytes? 32 se divide karo.)
- Address function likho. bytes (har float 4 bytes ka hai, stride 1). Yeh step kyun? Baaki sab hai; pehle explicitly chahiye.
- Har thread ka sector nikalo. . Yeh step kyun? Yeh 32 addresses ko 32 sector numbers mein convert karta hai — wahi ek cheez jo matter karti hai.
- Distinct sectors count karo. Jab run karta hai, values leta hai. Toh . Yeh step kyun? hamara master number hai.
- Efficiency. . Yeh step kyun? Zero wasted bytes confirm karta hai — coalesced ki definition.
Figure walkthrough: Upar ke map mein, 32 coral bars (thread reads) exactly 4 mint sectors ke upar baithe hain, koi gaps nahi aur koi grey (unused) sectors nahi. "Coral perfectly tiles mint" — yahi 100% efficiency dikhti hai.
Verify: 4 sectors × 32 bytes = 128 bytes fetched = 128 bytes used. Addresses 0x00–0x7F = exactly 128 bytes, exactly 4 aligned sectors. Units: bytes/bytes = dimensionless efficiency. ✓
Example 2 — Cell B: Sequential lekin misaligned start
Forecast: Same data size (128 bytes) — lekin kya window ko sector boundaries ke across shift karne se ek extra transaction ki cost aayegi?
- Address function. . Kyun? Offset 8, ka multiple nahi hai, toh 128-byte window ek extra sector boundary straddle karti hai.
- Sectors touched. Sabse chota address sector . Sabse bada sector . Kyun? Span ab sector 0 se sector 4 tak pahunchta hai.
- Distinct sectors count karo. Sectors sab touch hote hain . Kyun? 128-byte window 5th sector mein thodi ghus gayi; hume usse bhi fetch karna padega.
- Efficiency. . Kyun? Humne 160 bytes fetch kiye lekin sirf 128 use kiye — misalignment ne hume 20% cost kiya.
Figure walkthrough: Example 1 ke map se compare karo. Coral bars 8 bytes right shift ho gaye hain, toh pehle do bars ab sector 0 mein hain jabki aakhri bar ek fifth sector (sector 4, mostly unused space ke saath grey-mint drawn) mein spill karta hai. Woh single extra half-empty sector hi woh 20% hai jo tumne khoya.
Verify: span = 0x08–0x84 (132 = 0x84). sectors. Efficiency . ✓
Example 3 — Cell C: Small stride (2 elements)
Forecast: Hum ab har doosra float skip kar rahe hain. Hum 128 bytes use karte hain lekin woh double span mein spread hain. guess karo.
- Address function. . Kyun? Stride 2 floats = consecutive threads ke beech 8 bytes.
- Sectors. , ke saath run karta hai. Kyun? Ab sirf 4 threads ek sector share karte hain (8 nahi), toh hum zyada sectors mein spill karte hain.
- Count. leta hai . Kyun? Distinct sector count hi transactions set karta hai.
- Efficiency. . Kyun? Har fetched sector ka aadha woh data hai jo humne skip kiya.
Verify: span 0x00–0xF8 (248 bytes); 8 sectors. 256 bytes mein se 128 use kiye = 50%. ✓
Example 4 — Cell D: Stride = exactly ek sector
Forecast: Har thread exactly ek sector jump karta hai. Kya koi do threads kabhi ek sector share karte hain?
- Address function. . Kyun? Yeh tipping point hai jahan stride ke barabar hai.
- Sectors. — har thread ke liye ek alag sector. Kyun? Kyunki stride exactly ke barabar hai, koi do threads kabhi ek hi sector mein land nahi kar sakte.
- Count. Sectors . Kyun? 32 distinct sectors yahan master number hai.
- Efficiency. . Kyun? Har 32 fetched bytes mein se sirf 4 use hote hain.
Verify: 32 distinct sectors, 1024 bytes fetched, 128 used → 12.5%. Yeh parent note ka exact "strided" number hai. ✓
Example 5 — Cell E: Huge stride (worst case, limiting value)
Forecast: Jab stride ≥ ho, aur zyada stride se nuksaan nahi hota — tum already ek transaction per thread par ho. Confirm karo.
- Address function. . Kyun? Hum test kar rahe hain ki se zyada extra stride cheezein aur kharab karti hai ya nahi.
- Sectors. — sectors sab distinct. Kyun? Koi bhi stride 32 distinct sectors guarantee karta hai. Usse aage, extra stride sirf unhe spread karta hai; count 32 par saturate ho jaata hai.
- Count. (32-thread warp ke liye maximum possible). Kyun? 32-thread warp at most 32 distinct sectors touch kar sakta hai — yeh woh ceiling hai.
- Efficiency. — Example 4 se identical. Kyun? Same transaction count → same efficiency; floor reach ho gayi.
Verify: , efficiency , Example 4 se match → floor confirmed. ✓
Example 6 — Cell F: Reversed / permuted order
Forecast: Kya threads addresses visit karne ka order matter karta hai, ya sirf addresses ka set?
- Address function. . Thread 0 reads
0x7C, thread 31 reads0x00. Kyun? Hum test karna chahte hain ki coalescing permutation ki parwah karta hai ya nahi. - Addresses ka set. Jab range karta hai, range karta hai — Example 1 jaisa same set . Kyun? Coalescing warp ke touch kiye distinct sectors count karta hai, yeh nahi ki unhe kaun touch karta hai.
- Sectors. Exactly , toh . Kyun? Example 1 jaisa same sector set → same count.
- Efficiency. — Example 1 se identical. Kyun? Same → same efficiency.
Recall Kya thread-to-address permutation coalescing change karta hai?
Warp ke andar permutation free hai — sirf sectors ka set matter karta hai ::: Sahi. Reverse, shuffle, ya interleave karo — jab tak 32 addresses same sectors cover karein, unchanged hai. Hardware transactions issue karne se pehle addresses collect karta hai.
Verify: same sector set Example 1 ki tarah → , 100%. ✓
Example 7 — Cell G: Degenerate — sab threads same address read karte hain
Forecast: 32 threads, ek address. Minimum possible kya hai?
- Address function. , constant (zero slope — degenerate "flat" ramp). Kyun? Yeh boundary case hai jahan address function ki stride 0 hai.
- Sectors. sab threads ke liye → single sector . Kyun? 32 saare addresses identical hain, toh woh ek se zyada sector mein spread nahi ho sakte.
- Count. . Hardware sector 0 ek baar fetch karta hai aur value broadcast karta hai saare 32 threads ko. Kyun? Ek distinct sector → ek transaction, ek warp load ki absolute minimum cost.
- Yahan efficiency ka matlab (broadcast footnote). Hamare page-wide numerator "bytes used " assume karta hai ki 32 threads 32 distinct elements chahte hain. Broadcast mein woh same element chahte hain, toh actually sirf distinct useful bytes hain, 128 nahi. Formula literally plug karne par milega — ek nonsense >100% number jo sirf isliye aata hai kyunki numerator identical requests over-count karta hai. Toh is ek degenerate cell ke liye hum iske bajaaye honest operational headline report karte hain: , minimum possible transaction count, single fetched sector saare 32 threads reuse karte hain. Broadcast sabse achi pattern hai, koi 400%-efficient nahi. Yeh step kyun? Is footnote ke bina reader figure trust kar leta ya sochta ki model toot gaya; numerator ka assumption (distinct elements) name karna exactly woh hai jo paradox resolve karta hai.
Verify: single distinct sector → , minimum. Distinct useful bytes ; literal formula flagged nonsense deta, isliye hum report karte hain. ✓
Example 8a — Cell H: Real-world word problem (row-major matrix)
Forecast: Kaun sa layout traversal — column down ya row across — warp ko consecutive addresses deta hai?
- Column access address function. bytes. Kyun? Row-major matlab consecutive rows 1024 floats = 4096 bytes apart hain.
- Column sectors. — sab distinct → , efficiency . Yeh Example 5 disguise mein hai! Kyun? Row-major storage mein column ka stride = row length ≫ sector → worst case. (Yeh upar slope-regime bullets mein referenced column-access case hai.)
- Row access address function. → Example 1 jaisa identical → , . Kyun? Thread ke saath varying last index stride 1 deta hai → perfect coalescing.
- Design conclusion. Row-major array ke liye, consecutive threads ko consecutive columns read karwao (last index
threadIdx.xke saath vary karta hai). Columns efficiently process karne ke liye, pehle shared memory mein transpose karo. Yeh step kyun? Steps 2 aur 3 ne prove kiya ki row traversal column traversal se kam transactions karta hai; woh decide karna kya karna hai measured fact ke saath hi arithmetic ko engineering rule mein turn karta hai — warna derivation actionable takeaway ke bina khatam ho jaati.
Verify: column: , 12.5%; row: , 100%. Row-over-column speedup transactions mein. ✓
Example 8b — Cell J: Mixed / irregular warp (koi single stride nahi)
Forecast: Warp aadha achha, aadha kharab hai. Kya achhe 16 kharab 16 ko rescue karte hain, ya total bas add up ho jaata hai?
- Dono halves ke address functions.
- Threads : → bytes
0,4,...,60. - Threads : → bytes
4000, 4256, 4512, .... Kyun? Ek mixed warp mein koi closed-form single slope nahi hoti, toh hume har sub-pattern alag count karni padti hai, phir sectors ka union lena padta hai.
- Threads : → bytes
- Contiguous half ke sectors. Bytes
0..60→ sectors . Yeh 2 sectors hain. Kyun? 16 floats = 64 bytes exactly two aligned sectors span karte hain — good half locally coalesced hai. - Scattered half ke sectors. ke liye → sectors sab distinct → 16 sectors. Kyun? Yahan stride 256 bytes hai, toh 16 mein se har ek apne sector mein land karta hai (locally Example 5 behaviour).
- Union aur count. Dono sector sets disjoint hain (0–1 vs 125–245), toh . Kyun? Total transactions = poore warp ke distinct sectors; good half kharab half ko rescue nahi karta, lekin apni cost low rakhta hai.
- Efficiency. . Kyun? Sirf 16 contiguous reads dense the; 16 scattered reads ne efficiency neeche kheechi lekin poori tarah 12.5% floor tak nahi.
Verify: contiguous half → 2 sectors; scattered half → 16 sectors; disjoint union → 18; efficiency . 12.5% floor aur 100% ceiling ke beech, jaisa ek mixed pattern hona chahiye. ✓
Example 9 — Cell I: Exam twist — Array of Structs (hidden stride)
Forecast: Struct 12 bytes ka hai, toh x fields 12 bytes apart hain — 4 nahi. Kya woh 32-byte sectors mein neat pack hota hai?
- Address function. . Threads
0, 12, 24, 36, ...par read karte hain. Kyun? Struct size stride ban jaata hai jab tum ek thread per field touch karte ho. - Sectors touched. . Span evaluate karo: sector . Smallest sector 0 hai. Kya sab hit hote hain? Step sector per thread, toh consecutive threads kabhi poora sector skip nahi karte → haan, sectors sab touch hote hain. Kyun? Count karne se pehle confirm karna padega ki range mein koi sector skip toh nahi hua.
- Count. . Kyun? 12 distinct sectors master number hai.
- Efficiency. .
Kyun? Humne
yaurzfields fetch kiye jo humne kabhi use nahi kiye — har sector ka 2/3 waste.
Verify: span 0–372 bytes → sectors 0..11 = 12 sectors; efficiency . ✓
Ek nazar mein summary
Recall Har example ke liye
fill karo (hamara fixed B model) Ex1 seq → 4 · Ex2 misaligned → 5 · Ex3 stride2 → 8 · Ex4 stride8 → 32 · Ex5 huge → 32 · Ex6 reversed → 4 · Ex7 broadcast → 1 · Ex8a column → 32 / row → 4 · Ex8b mixed → 18 · Ex9 AoS → 12 ::: Shape yaad rakho: gentle stride = kam transactions; stride ≥ sector = 32 (floor); mixed = parts ka sum.
Recall Kisi bhi bade uniform stride ke liye efficiency 12.5% par capped kyun hai, isse neeche kyun nahi?
Kyunki warp mein sirf 32 threads hain, toh at most 32 distinct sectors hain ::: Ek transaction per thread ke saath tum bytes fetch karte ho aur use karte ho; ratio single-element-per-thread uniform-stride pattern ke liye hard floor hai.
Prerequisites & neighbours: 6.2.01-GPU-execution-model (warps & SIMT), 6.2.09-Memory-hierarchy (jahan sectors aur L2 rehte hain, aur caching ek bure pattern ko kyun soften karta hai but fix nahi karta), 6.2.04-Shared-memory-and-synchronization (transpose fix), 6.3.02-Roofline-model (bandwidth kyun matter karta hai), 5.1.05-Cache-coherence-protocols (line/sector granularity origins), aur Hinglish walkthrough 6.2.08 Coalesced memory access (Hinglish).