6.2.5 · HinglishGPU Architecture

Warps and warp scheduling

3,399 words15 min readRead in English

6.2.5 · Hardware › GPU Architecture

Overview

Warps GPU execution ki fundamental unit hain NVIDIA GPUs mein, jo ek aisi thread group ko represent karti hain jo same instruction ko lockstep mein execute karti hain. Warp scheduling ko samajhna GPU performance optimize karne ke liye critical hai kyunki GPU scheduler warp granularity par operate karta hai, individual threads par nahi.

Figure — Warps and warp scheduling

Core Concepts

Warps Thread Blocks Se Kaise Map Hote Hain

Thread indices se derivation:

Ek 3D thread block ke liye jiska dimensions (blockDim.x, blockDim.y, blockDim.z) hai, threads ko warps mein row-major order mein assign kiya jaata hai:

Yeh kyun matter karta hai: Agar blockDim.x = 64 hai, toh threads 0-31 warp 0 banate hain, threads 32-63 warp 1 banate hain. Agar blockDim.x = 17 hai, toh threads 0-31 warp 0 banate hain, lekin sirf 17 threads active hain—baaki 15 slots waste hote hain. Isliye block dimensions 32 ke multiples hone chahiye.


Warp Scheduling Mechanics

The Scheduling Problem

Ek SM multiple thread blocks simultaneously hold kar sakta hai (registers, shared memory, aur max blocks/SM se limited). Har block multiple warps contribute karta hai. Agar ek SM 2048 threads hold kar sakta hai aur uske paas 64 warps resident hain, toh scheduler ko decide karna hota hai ki aage kaun sa warp execute hoga.

Zero-overhead switching kyun kaam karta hai, iska derivation:

Jab warp W₁ memory par stall hota hai, scheduler usi cycle mein warp W₂ par immediately switch kar leta hai. Yeh isliye possible hai kyunki:

  1. Register files partitioned hain: Har warp ka dedicated register space hota hai (koi context save/restore nahi chahiye)
  2. Program counters per-warp hain: Warp switch karna = ek pointer update karna (1 cycle)
  3. Instruction buffers alag hain: Har warp ki next instruction pre-fetch ho jaati hai

Latency hiding capacity:

Agar ek memory operation cycles leti hai aur har warp instructions mein ek memory operation karta hai, toh aapko zaroori hai:

Kyun? Agar cycles aur instructions hain memory ops ke beech, toh SM ko busy rakhne ke liye 400/10 = 40 warps chahiye jab ek warp wait kar raha ho.

Scheduling Policies

Modern GPUs in policies ke variations use karte hain:

  1. Round-robin with priority: Eligible warps mein cycle karo, long-stalled warps ko prioritize karo
  2. Greedy-then-oldest (GTO): Ek warp ko tab tak execute karo jab tak woh stall na ho, phir sabse purana stalled warp pick karo
  3. Two-level scheduling: Un warps ko prioritize karo jo cache mein hit karenge

Warp Divergence

Divergence penalty ka derivation:

Ek if-else consider karo jahan threads if branch lete hain aur else branch lete hain.

Divergence ke bina (sab threads same path lete hain):

Divergence ke saath (maano aur ):

Worst case: Dono branches ki equal cost hai aur equally populated hain:

Yeh kyun hota hai: Warp if branch ko ek mask ke saath execute karta hai (jo threads else lete hain unhe disable karta hai), phir else branch ko opposite mask ke saath execute karta hai. Dono paths sequentially run karte hain chahe sirf 1 thread ko hi koi path chahiye ho.


Warp Scheduling ke Liye Optimize Karna

1. Occupancy Maximize Karo (Reason ke Saath)

Per SM constraints:

  • Max threads: 2048 (architecture ke hisaab se alag)
  • Max blocks: 32 (alag ho sakta hai)
  • Max warps: 64 (compute capability 7.0+)
  • Register file size: jaise 65,536 registers
  • Shared memory: jaise 64 KB (L1 cache ke saath configurable split)

Occupancy formula:

\frac{\text{Threads per Block} \times \text{Blocks per SM}}{\text{Max Threads per SM}}, \frac{\text{Blocks per SM}}{\text{Max Blocks per SM}}, \frac{\text{Warps per SM}}{\text{Max Warps per SM}} \right)$$ Subject to:

\text{Registers per Block} \times \text{Blocks per SM} \leq \text{Total Registers}

2. Divergence Minimize Karo

  • Warp-wide operations use karo: __ballot_sync(), __shfl_sync()
  • Conditionals is tarah structure karo ki poore warps same path lein
  • Data structures ko warp boundaries par pad karo

3. Instruction Mix Balance Karo

Instruction throughput type ke hisaab se alag hota hai:

  • FP32: typically 1 op/cycle/SM
  • FP64: 1/2 rate (ya consumer GPUs par 1/32)
  • Special functions (sin, cos): 1/4 rate

Agar sab warps ko FP64 chahiye, toh schedulers limited FP64 units ke liye compete karte hain → FP32 units ka underutilization.

Recall Ek 12-Saal-Ke Bachche Ko Samjhao

Socho tumhara teacher chahta hai ki class math problems solve kare. Har student ko individually call karne ki jagah (jo slow hai!), woh tumhe 32 ke teams mein group karta hai. Har team ko same problem milta hai, aur sab ek saath apni copy par kaam karte hain. Wahi warp hai!

Teacher (warp scheduler) ghoom-ghoom kar check karta hai kaun si teams ready hain. Agar Team A library se book ka wait kar rahi hai (memory access), woh immediately Team B ki help karne jaata hai. Jab tak woh kai teams mein cycle karta hai, Team A ki book aa jaati hai, isliye koi time waste nahi hota.

Lekin yahan ek tricky part hai: agar Team A ke problem mein likha hai "even IDs wale addition karo, odd IDs wale subtraction karo," toh team ko split hona padta hai. Pehle even-ID students solve karte hain jab odd-ID students idle baithe hain. Phir woh switch karte hain. Team ko double time lagta hai kyunki woh saath mein kaam nahi kar sakte. Wahi divergence hai!

Seekh: apni poori warp team ko ek saath same cheez karte rakhne se maximum speed milti hai.


Connections

  • SIMT-vs-SIMD - GPU SIMT CPU SIMD se kaise alag hai
  • Thread-Blocks-and-Grids - Warps larger hierarchy mein kaise fit hote hain
  • GPU-Memory-Hierarchy - Memory latency hide karne ke liye warp scheduling kyun matter karti hai
  • Branch-Divergence-Patterns - Common divergence scenarios aur solutions
  • Occupancy-vs-Performance - 100% occupancy hamesha optimal kyun nahi hoti
  • Cooperative-Groups - Modern warp-synchronous programming primitives
  • Register-Pressure - Register usage occupancy aur warp count kaise limit karta hai

#flashcards/hardware

GPU architecture mein warp kya hota hai? :: 32 consecutive threads ka ek group jo SIMT fashion mein ek saath same instruction execute karta hai. Warps NVIDIA GPUs mein fundamental scheduling unit hain.

Warp ID thread indices se kaise calculate hota hai?
Linear thread index ko 32 se divide karo (floor division). Linear index = threadIdx.z * blockDim.x * blockDim.y + threadIdx.y * blockDim.x + threadIdx.x, phir warpId = linearIdx / 32.
Thread block dimensions 32 ke multiples kyun hone chahiye?
Wasted warp slots se bachne ke liye. Agar blockDim 32 ka multiple nahi hai, toh last warp mein inactive threads honge jo resources consume karte hain lekin koi kaam nahi karte.
Warp divergence kya hoti hai?
Jab ek hi warp ke threads alag-alag execution paths lete hain (alag branches). GPU ko divergent paths serialize karne padte hain, har path ko kuch threads masked off karke execute karna padta hai, jisse performance kam hoti hai.
Warp divergence ki worst-case performance penalty kya hai?
2× slowdown jab dono branches ki equally costly aur equally populated hon. Warp dono branches sequentially execute karta hai ek path ki jagah.
Warp scheduling memory latency kaise hide karta hai?
Jab ek warp memory par stall hota hai (100s of cycles), scheduler immediately kisi doosre ready warp par zero overhead ke saath switch kar leta hai. Kaafi resident warps ke saath, SM busy rehta hai jab stalled warps wait karte hain.
GPU occupancy kya hoti hai?
Active warps ka maximum warps per SM se ratio. Formula: Active Warps / Max Warps per SM. Zyada occupancy latency hiding ke liye zyada warps provide karti hai.
Occupancy ko kya factors limit karte hain?
Registers per thread, shared memory per block, threads per block, max blocks per SM, aur max warps per SM. Sabse restrictive limit actual occupancy determine karti hai.
Warp context switching ka zero overhead kyun hota hai?
Register files per warp partitioned hain, program counters per-warp hain, aur instructions pre-fetch ho jaati hain. Warps switch karna sirf ek pointer update karta hai, koi context save/restore nahi chahiye.
Warp schedulers aur cores mein kya farq hai?
Warp schedulers decide karte hain ki aage kaun sa warp execute hoga. Cores (CUDA cores) actually instructions execute karte hain. Modern SMs mein multiple schedulers hote hain (jaise 4) jo har cycle mein multiple warps se instructions issue karte hain.
256-thread block mein kitne warps hote hain?
256 / 32 = 8 warps. Har block ke threads ko 32 consecutive threads ke groups mein divide kiya jaata hai.
Ek warp scheduling ke liye eligible kya banata hai?
Warp ko stalled nahi hona chahiye (koi memory wait, dependency wait, ya synchronization barrier nahi) AUR resources available hone chahiye (execution units, memory ports).
threadIdx.x % 2 branching performance ke liye kyun buri hai?
Yeh har warp mein divergence cause karta hai kyunki har warp mein dono even (16) aur odd (16) threadIdx values hoti hain. Har warp ko dono branches serially execute karni padti hain.
Latency L hide karne ke liye warps ki formula kya hai?
Warps needed ≥ (Number of schedulers × L) / Instructions between stalls. 4 schedulers aur 400-cycle latency ke liye 10 instructions ke saath memory ops ke beech: 4 × 400 / 10 = 160 warps worth of work.
Warp-synchronous operation kya hoti hai?
Operations jo ek warp ke sab threads mein simultaneously act karti hain, jaise __shfl_sync() data exchange ke liye ya __ballot_sync() voting ke liye, explicit synchronization ki zaroorat ke bina.

Concept Map

executes in

means

enables

drives

is the

selects

has

issue concurrently

divided by 32 gives

assigns thread to

avoids

caused by

Warp: 32 threads

SIMT execution

Lockstep same instruction

Amortize control overhead

GPU throughput focus

Warp Scheduler in SM

Scheduling quantum

Multiple schedulers per SM

Linear thread index

Warp ID = floor of index div 32

Block dims multiple of 32

Wasted slots if not aligned