Worked examples — Warp divergence penalties
6.2.11 · D3· Hardware › GPU Architecture › Warp divergence penalties
Yeh page parent topic ke liye worked-examples ki zameen hai. Parent ne tumhe dikhaya tha ki divergence kya hota hai; yahan hum har tarah ke case ko grind karte hain jo ek warp tumhare samne rakh sakta hai, ek number at a time. Agar koi symbol aata hai, toh hum use use karne se pehle define karte hain.
Recall Woh do numbers jo sab kuch decide karte hain
Pehle yeh padho — neeche ke saare examples sirf inhi do lines ka application hain.
- Maano un cycles ki sankhya hai ("cycle" matlab ek instruction jo hardware ko issue ki gayi) jo path ko chahiye.
- Ideal time (koi divergence nahi) — saare paths overlap karte hain, sabse slow wala pace set karta hai.
- Diverged time — paths ek ke baad ek chalte hain.
- Efficiency .
Ek "path" ek distinct straight-line route hai code ke through jise warp mein kam se kam ek thread actually leta hai. Jo threads ek hi route lete hain woh ek path share karte hain — unka koi extra cost nahi.
Matrix se pehle, poore idea ki ek picture — taaki baad ke har example ka ek ghar ho.

Figure dekho: horizontal axis time in cycles hai. Upar wali bar mein saare 32 threads saath chalte hain (ek path). Neeche wali bar mein warp do groups mein split ho gaya — dhyaan do ki doosra group tabhi start kar sakta hai jab pehla group khatam ho jaye. Woh waiting gap hi penalty hai. Is page par koi cheez us gap se zyada complicated nahi hai.
Scenario matrix
Warp divergence ke baare mein har problem inhi cells mein se kisi ek mein aati hai. Hum har ek cell ko kam se kam ek worked example ke saath hit karenge.
| Cell | Case class | Isme kya khaas hai |
|---|---|---|
| C1 | Even 2-way split (if/else, half–half) | baseline; do paths, dono non-trivial |
| C2 | Uneven split (1 thread vs 31) | "few threads = cheap?" trap ko test karta hai |
| C3 | Zero divergence (saare threads ek hi path) | degenerate achha case, efficiency = 1 |
| C4 | Full divergence (har thread ka apna path) | worst case, limit |
| C5 | Multi-way else if chain |
paths, unequal lengths |
| C6 | Loop / while divergence |
trip-count har thread mein alag hoti hai |
| C7 | Predication decision (branch ya predicate?) | limiting comparison vs |
| C8 | Nested ifs → orthogonal paths | path count multiply hoti hai |
| C9 | Real-world word problem (ray tracing materials) | C4/C5 ko data par apply karta hai |
| C10 | Exam twist (warp-boundary re-alignment) | C3 tak pahunchne ke liye restructure karo |
Cross-links jo padhte waqt kaam aa sakte hain: 6.2.8-SIMT-execution-model (kyun ek instruction 32 lanes drive karta hai), 6.2.9-Warp-scheduling (kaise idle warps latency hide karte hain), 8.4.2-Branch-prediction (CPU cousin), 10.1.3-CUDA-optimization-patterns (scale par fixes).
Worked examples
Example 1 — Cell C1 (even 2-way split)
Forecast: Aage padhne se pehle efficiency guess karo. Zyaatar log "50% kyunki aadhe ek taraf jaate hain" kehte hain. Yeh soch pakad ke rakho.
- Distinct paths identify karo. Do paths actually li ja rahi hain: A () aur B (). Yeh step kyun? Cost liye gaye paths par depend karta hai, nahi ki kitne threads har ek lete hain — yahi 6.2.8-SIMT-execution-model ka poora lesson hai.
- Diverged time cycles. Yeh step kyun? Else-group wait karta hai jab tak if-group chalti hai, phir uske baad chalti hai (figure s01 mein gap dekho).
- Ideal time cycles. Yeh step kyun? Agar koi diverge nahi karta, toh saare 32 lanes overlap karte hain; akela slow wala path clock set karta hai.
- Efficiency . Yeh step kyun? Jo hum le sakte the aur jo hum liya uska ratio.
Verify: Efficiency aur satisfy karni chahiye. Humne paaya, jo mein hai. Note karo yeh 50% nahi hai — "half–half" intuition galat thi kyunki dono paths ki alag lengths hain. ✔
Example 2 — Cell C2 (uneven split, "few threads" trap)
Forecast: Tempting galat jawab hai cycles — "sirf ek thread expensive kaam karta hai, toh amortise karo." Guess karo ki yeh sahi hai ya nahi.
- Distinct paths liye gaye: do — ek 100-cycle path (1 thread) aur ek 50-cycle path (31 threads). Yeh step kyun? Ek bhi diverging thread ek poora doosra path create karta hai.
- Sahi diverged cost cycles. Yeh step kyun? Hardware poore warp ke liye 100-cycle path chalata hai (31 lanes masked off hain lekin phir bhi idle-waiting hain), phir 50-cycle path. Thread count kabhi cost ko divide nahi karta.
- Naive (galat) estimate cycles. Yeh step kyun? Hum ise sirf ek myth ke roop mein expose karne ke liye compute karte hain.
- Ideal ; efficiency .
Verify: Sahi cost 150 vs naive 53.125 — inमें nearly ka fark hai, confirming karta hai ki myth dangerous hai. Efficiency . ✔
Example 3 — Cell C3 (zero divergence, degenerate good case)
Forecast: Padhne se pehle guess karo: kabhi bhi possible sabse acchi efficiency kya hai?
- Distinct paths liye gaye: sirf ek — path B. Path A kisi bhi thread ke liye kabhi enter nahi ki jaati, toh is warp ke liye exist nahi karti. Yeh step kyun? Ek nahi li gayi branch free hai; hardware iske upar mask all-off ke saath skip karta hai, koi cycles issue nahi hote.
- Diverged time cycles. Ideal .
- Efficiency .
Verify: Jab , . Yeh degenerate ceiling hai — tum 100% se behtar kabhi nahi ho sakte. Yeh 10.1.3-CUDA-optimization-patterns mein har mitigation ka goal confirm karta hai: par collapse karo. ✔
Example 4 — Cell C4 (full divergence, limit)
Forecast: Efficiency ko ek simple fraction ke roop mein guess karo.
- Distinct paths ki sankhya (sab alag). Yeh step kyun? actually walk ki gayi routes ka count hai; yahan yeh warp size par max ho jaata hai.
- Diverged time cycles. Yeh step kyun? 32 serial executions, har ek cycles.
- Ideal time cycles (sab equal hain).
- Efficiency . Yeh step kyun? Yeh 32-thread warp ka theoretical floor hai — parent note mein quote kiya gaya "1/32nd of peak."
Verify: General case: agar saare paths ki equal length hai, efficiency . ke saath: . ✔
Example 5 — Cell C5 (multi-way else if, unequal lengths)
Forecast: Kaunsa single path ideal time ko dominate karta hai? Step 3 se pehle guess karo.
- Paths liye gaye: charon present hain (har type mein ≥1 thread hai), toh . Yeh step kyun? Thread counts (8,10,8,6) humein batate hain ki kaunse paths live hain, cost nahi.
- Diverged time cycles.
- Ideal time cycles —
exppath dominate karta hai. Yeh step kyun? Agar koi diverge nahi karta, toh akela longest live path clock set karta hai. - Efficiency .
Verify: kyunki floor . Parent note ke Example 2 se exactly match karta hai. ✔
Example 6 — Cell C6 (loop divergence)

Forecast: Warp tab khatam hoga jab aakhri thread khatam hoga. Guess karo: kya total counts ke sum par depend karta hai ya counts ke max par?
- Loop back-edge ek branch hai. Har iteration mein, woh threads jinke liye hai drop out ho jaate hain (masked off) lekin warp loop karta rehta hai. Yeh step kyun? Figure s02 dekho: active-lane count har iteration mein shrink karti hai lekin loop tab tak exit nahi kar sakta jab tak sabse lamba bar (count = 20) khatam nahi ho jaata.
- Total cycles (ek cycle per iteration, sabse slow thread par warp-serialized). Yeh step kyun? Warp apne sabse zyada der tak chalne wale thread ka hostage hai.
- Kiya gaya useful work thread-iterations.
- Utilization . Yeh step kyun? Hardware kar sakta tha lane-iterations, unमें se sirf 240 real work thi.
Verify: , aur hamesha kyunki . Do short (count-3) threads lane-iterations waste karte hain — loss ka ek bada hissa. ✔
Example 7 — Cell C7 (branch vs predication — limiting comparison)
Forecast: Chhote branches ke liye, guess karo ki predication kabhi harti hai ya nahi.
- Predicated cost.
aaurbdono har thread ke liye unconditionally compute hote hain: cycles, bina kisi branch/mask-stack overhead ke. Yeh step kyun? Predication branch ko poori tarah hata deta hai — koi divergence stack push/pop nahi. - Branch (diverged) cost. Kyunki yeh half–half diverge hota hai: , jahan branch/reconverge overhead hai (). Yeh step kyun? Wahi serialized path sum, saath mein divergence stack manage karne ki cost.
- Compare. . Predication exactly se jeetta hai. Yeh step kyun? Jab dono paths chhote hain aur dono divergence ke underaise bhi chalenge, branch machinery skip karna pure profit hai.
- Jahan predication harti hai: agar ek path lamba hai aur often not taken (jaise Example 3 ka C3 case), toh branching hardware ko nahi li gayi path poori tarah skip karne deta hai; predication use force karke chalata. Predicate sirf short, both-usually-needed paths ke liye karo.
Verify: , with . Short-branch case ke liye inequality strict hai. ✔ (Dekho 6.2.12-Occupancy-optimization kyun branches hatana divergence stack bhi free karta hai.)
Example 8 — Cell C8 (nested ifs → orthogonal paths multiply karte hain)
Forecast: Worst case mein kitne distinct paths hain? 2? 3? 4?
- Distinct paths count karo. Outer test 2 groups mein split karta hai; har group ka inner test phir split karta hai. Worst case: live paths. Yeh step kyun? Orthogonal (independent) conditions path count ko multiply karte hain — nesting additive nahi hoti.
- Diverged time cycles. Yeh step kyun? Char serialized paths, ek ke baad ek.
- Ideal time cycles.
- Efficiency .
Verify: ke saath, floor ; . Naive "ek extra branch → 3 paths" undercount karta hai; actual ise aur bura banata hai. ✔
Example 9 — Cell C9 (real-world: ray-tracing materials)
Forecast: Guess karo ki sorting humein 100% tak pahuncha sakti hai ya nahi.
- As-is, paths live: , costs . Yeh step kyun? Har material present hai ⇒ har path serialized.
- Diverged time cycles; ideal .
- Efficiency (a) .
- Sorting ke baad: ek warp ab sirf ek material rakhta hai ⇒ ⇒ diverged ideal. Efficiency (b) all-glass warp ke liye (aur 100% har uniform warp ke liye). Yeh step kyun? Sorting C4→C3 collapse hai: similar kaam ko warp size tak bucket karo, exactly wahi fix jo 9.3.5-Parallel-algorithmsdesign aur 10.1.3-CUDA-optimization-patterns mein hai.
Verify: (a) ; (b) koi bhi single-path warp deta hai. Sorting yahan throughput almost double kar deti hai (). ✔
Example 10 — Cell C10 (exam twist: warp boundary par re-align karo)
Forecast: idx % 2 har warp ke andar split karta hai. Compute karne se pehle (a) guess karo.
- As-is: kisi bhi warp mein, even aur odd lanes dono present hain ⇒ dono paths live, .
Yeh step kyun?
idx % 2warp ke andar alternate karta hai — guaranteed divergence. - Diverged ; ideal . Efficiency (a) .
- Rewrite:
(idx/32)%2ek warp ke saare 32 lanes mein constant hai (woh ek hiidx/32share karte hain). Toh ek warp poori tarah X ya poori tarah Y hai ⇒ . Yeh step kyun? Branch key ko warp granularity ke saath align karna intra-warp divergence khatam kar deta hai — 6.2.10-Memory-coalescing ke sibling idea ka core mitigation (32 ke hisaab se group karo). - Efficiency (b): ek pure-X warp ; ek pure-Y warp .
Verify: (a) . (b) single-path ⇒ 1.0. Re-alignment efficiency 58.3% se 100% tak le jaata hai. ✔
Recall Quick self-test (answers reveal karo)
Do paths, 5 aur 5 cycles, half–half. Diverged time? ::: cycles. …aur uski efficiency? ::: . Ek thread 90-cyc path par, 31 ek 10-cyc path par — cost? ::: cycles (amortised nahi). Saare 32 threads ek hi 8-cyc path lete hain — efficiency? ::: (). 32-thread warp ke liye equal paths ke saath worst-case efficiency? ::: . Loop jisme — utilization? ::: .