6.2.6 · D3 · HinglishGPU Architecture

Worked examplesThread blocks and grids

3,793 words17 min read↑ Read in English

6.2.6 · D3 · Hardware › GPU Architecture › Thread blocks and grids

Yeh page parent topic ka drill hall hai. Hum block/grid machinery ke har tarah ke situation ko dhundhenge — clean divisions, ragged remainders, empty inputs, 2D tiling, occupancy walls — aur har ek ko zero se work karenge. Agar neeche koi bhi word naya lagta hai, toh parent note ne usse pehle build kiya hai; yahan hum use use karte hain jab tak yeh muscle memory na ban jaaye.

Kuch bhi karne se pehle, ek reminder us single formula ka jis par sab kuch tika hua hai:

Figure — Thread blocks and grids

Figure s01 dekho. Har row ek block hai; left side ke row labels ("block 0"…"block 3") unhe naam dete hain. Red seat (labelled globalID = 15) ek specific thread hai. Uska global number nikaalane ke liye hum red vertical arrow follow karte hain — uske upar ke do full rows ko jump karte hue (yaani ) — aur phir red horizontal arrow follow karte hain apne column tak step karte hue (). Yeh jumping-then-stepping ka ek kaam hi poora chapter hai.


Scenario matrix

Jo bhi problem tumhe milegi woh in cells mein se ek mein hogi. Baad ke worked examples mein se har ek mein (Cell A2) jaisi tag hogi taaki tum dekh sako ki poori grid cover ho rahi hai.

# Case class Tricky kya hai Covered by
A1 Exact fit block size se divisible Koi leftover thread nahi; guard kabhi fire nahi karta Example 1
A2 Ragged fit divisible nahi Last block mein idle threads hain; ceiling + guard chahiye Example 2
A3 Degenerate: Empty input Example 3
A4 Degenerate: Single element, phir bhi ek poora block Example 3
A5 one block Grid bilkul 1 block tak round up hoti hai Example 3
B1 2D tiling — exact x,y se row/col; row-major flatten karo Example 4
B2 2D tiling — ragged in ONE dim Guard sirf ek axis par fire karta hai (exact x, ragged y) Example 5
B3 2D tiling — ragged in BOTH dims Guard row aur col dono par corner mein fire karta hai Example 6
C1 Limiting behaviour — block size max (1024) Kam, mote blocks; occupancy risk Example 7
C2 Occupancy wall — shared memory blocks/SM ko limit karta hai Memory, threads nahi, bottleneck hai Example 8
D1 Word problem — real workload sizing "2M pixels process karo" ko ek launch mein translate karo Example 9
D2 Exam twist — reverse the formula globalID diya hai, block & thread recover karo Example 10

Worked Examples


Recall

Recall Ceiling division kyun, plain division kyun nahi?

Plain integer division round down karta hai, ek partial last block drop kar deta hai aur tail elements unprocessed chhod deta hai ::: ceiling round up karta hai taaki har element cover ho.

Recall "Guard" exactly kise protect karta hai?

if (i < N) / if (row < M && col < N) check last (padded) block ki extra threads ko aisi memory read ya write karne se rokta hai jo exist nahi karti ::: yeh isliye aati hain kyunki grid hamesha whole blocks tak round up hoti hai.

Recall Ragged launch mein idle threads kahan rehti hain?

Hamesha last block mein (1D) ya padded edge/corner blocks mein (2D); unke global indices data size se zyada hote hain, toh guard fire hota hai ::: yeh fully-interior blocks mein nahi aati.

Recall One-dim vs. two-dim ragged: kitni guard sub-conditions fire hoti hain?

Agar sirf ek axis ragged hai, toh sirf us axis ki sub-condition (row < M ya col < N) fire hoti hai ::: agar dono axes ragged hain, toh corner block dono sub-conditions ek saath trip karta hai.

Recall Occupancy threads ki jagah memory se kab limit hoti hai?

Jab (ek floor) se chhota hota hai ::: hardware sabhi floored resource limits ka minimum leta hai.

Recall globalID ko (block, thread) mein kaise invert karte hain?

aur ::: kyunki globalID jahan .

Related deep threads: Warp Execution, Shared Memory, Thread Synchronization, Streaming Multiprocessors, Memory Coalescing, Parallel Algorithm Design, CUDA Programming Model.