6.2.13 · HinglishGPU Architecture

CUDA programming model basics

3,124 words14 min readRead in English

6.2.13 · Hardware › GPU Architecture

CUDA kya hai?

CUDA (Compute Unified Device Architecture) NVIDIA ka parallel computing platform hai jo programmers ko GPU ki power general-purpose computation ke liye use karne deta hai, sirf graphics ke liye nahi.

YEH kyun exist karta hai: Graphics cards lakhon pixels ko parallel mein render karne ke liye evolve hui. NVIDIA ne realize kiya ki yeh massive parallelism scientific, AI, aur data problems ko bhi solve kar sakta hai, agar woh programmers ko direct access de dein.

YEH kya provide karta hai: Ek C/C++ jaisi language extension + runtime libraries + compiler (nvcc) jo tumhe GPU ke liye code likhne deta hai.

YEH kaise kaam karta hai: Tum special functions (kernels) likhte ho jo GPU par run hote hain. Tum kaam ko threads, blocks, aur grids mein organize karte ho. GPU scheduler inhe apne hardware par distribute karta hai.

Figure — CUDA programming model basics

CUDA Execution Model: First Principles Se

Parallelism mein Structure kyun Zaroori hai

Fundamental problem: Agar tumhare paas 10,000 tasks aur 10,000 workers hain, tumhe chahiye:

  1. Ek tarika identify karne ka ki kaun sa worker kaun sa task karta hai
  2. Ek tarika coordinate karne ka jab workers ko information share karni ho
  3. Ek tarika map karne ka logical work organization ko physical hardware par

CUDA ise teen-level hierarchy se solve karta hai:

Memory Hierarchy

Memory kyun matter karta hai: Data move karna expensive hai. Ek memory access math operation se 100× zyada time le sakta hai. CUDA multiple memory types provide karta hai jisme alag-alag speed/scope tradeoffs hain.

Apna Pehla CUDA Kernel Likhna

Vector Addition: CUDA ka "Hello World"

Problem: Do arrays add karo: C[i] = A[i] + B[i] i = 0 se N-1 tak

CPU version (sequential):

for (int i = 0; i < N; i++) {
    C[i] = A[i] + B[i];
}
// Time: O(N) - har iteration mein ek element

CUDA version (parallel):

Boundary Check: if (idx < N) kyun?

Problem: Agar N = 1000 aur threadsPerBlock = 256 hai:

  • 4 blocks × 256 threads = 1024 threads total chahiye
  • Lekin hamare paas sirf 1000 elements hain
  • Aakhri 24 threads (idx 1000-1023) extra hain

Check ke bina: Yeh threads A[1000], A[1001], ... access karte → out of bounds! → crash ya garbage data

Check ke saath: Extra threads kuch nahi karte (idle rehte hain). Thoda performance cost, disaster se bachata hai.

Synchronization aur Cooperation

Block ke Andar Thread Synchronization

Kyun zaroori hai: Ek block ke threads resources share karte hain (shared memory). Synchronization ke bina, race conditions hoti hain.

Derivation - Reduction O(log N) kyun hai: Har iteration kaam ko half karta hai. 256 threads ke liye: 255 sequential additions ki jagah 8 iterations.

Global Synchronization

Key constraint: __syncthreads() sirf ek block ke andar kaam karta hai. Alag-alag blocks ke threads kernel execution ke dauran synchronize nahi kar sakte.

Kyun: Blocks kisi bhi order mein execute ho sakte hain, ya alag SMs par concurrently bhi. Inter-block synchronization allow karna deadlocks create karta.

Blocks ke paar synchronize kaise karein: Alag kernels launch karo. GPU guarantee karta hai ki kernel 1 ke saare threads kernel 2 ke kisi bhi thread se pehle finish ho jayenge.

Performance Considerations

Memory Coalescing

Coalesced access: Ek warp (32 consecutive threads) ke threads consecutive memory addresses access karte hain → GPU ek transaction mein combine karta hai.

Uncoalesced access: Threads scattered addresses access karte hain → GPU kai choti transactions karta hai → 10-100× slower.

Occupancy

Occupancy active warps aur SM par maximum possible warps ka ratio hai.

Kyun matter karta hai: Zyada occupancy → zyada warps available → better latency hiding.

Occupancy limit karne wale factors:

  1. Registers per thread: Zyada registers → kam threads fit hote hain
  2. Shared memory per block: Zyada shared memory → kam blocks fit hote hain
  3. Block size: Bahut chota → resources waste; bahut bada → SM par blocks limit karta hai

Typical goal: 50-75% occupancy aksar enough hota hai. 100% hamesha acchi performance ke liye zaroori nahi hai.

Recall Feynman: 12-saal ke bachche ko samjhao

Socho tumhare paas 10,000 problems wali ek badi math worksheet hai. Agar tum khud karo, toh bahut time lagega—ek problem ek time par.

Ab socho tumhare paas 10,000 students wala ek classroom hai, aur tum har student ko exactly ek problem dete ho. Sab ek saath kaam karte hain, aur achanak poori worksheet utne hi time mein ho jaati hai jitna ek problem solve karne mein lagta hai!

Yahi GPU karta hai. "Students" threads hain—chote workers. CUDA woh tarika hai jisse tum unhe batate ho ki kaun sa problem solve karna hai.

Tum students ko groups (blocks) mein organize karte ho jo tables par baithe hain. Ek hi table par baithe students ek whiteboard (shared memory) share kar sakte hain milkar kaam karne ke liye. Lekin alag tables par baithe students tab tak baat nahi kar sakte jab tak sab done na ho jayein.

Tumhe dhyan rakhna hoga: agar tumhare paas 10,000 problems hain lekin 10,240 students hain, toh aakhri 240 ke paas solve karne ke liye koi problem nahi hai. Tumhe unhe bolna hoga "is baar bahar baitho" warna woh imaginary problems solve karne ki koshish karenge aur sab garbad kar denge!

Connections

  • GPU Architecture Overview - CUDA ke liye hardware foundation
  • Streaming Multiprocessors - Physical execution units jo blocks run karte hain
  • Memory Hierarchy - Detailed memory performance analysis
  • Thread Warps and SIMT - Threads hardware par actually kaise execute hote hain
  • Parallel Algorithm Design - Problems decompose karne ki strategies
  • OpenCL vs CUDA - Alternative programming models
  • Deep Learning Frameworks - TensorFlow/PyTorch hood ke neeche CUDA use karte hain

Flashcards

#flashcards/hardware

CUDA kernel kya hota hai? :: Ek function jo GPU par run hota hai, bahut saare threads dwara parallel mein execute kiya jaata hai. __global__ keyword se declare kiya jaata hai.

1D mein global thread index ka formula kya hai?
idx = blockIdx.x * blockDim.x + threadIdx.x

CUDA kernels mein if (idx < N) kyun zaroori hai? :: Kyunki hum fixed size ke blocks launch karte hain (jaise 256 threads), hamare paas data elements se zyada threads ho sakte hain. Extra threads ko out-of-bounds memory access karne se rokna zaroori hai.

CUDA mein host aur device mein kya fark hai?
Host = CPU aur system RAM. Device = GPU aur VRAM. Inke alag memory spaces hain jinhein explicit copies ki zaroorat hoti hai.
<<numBlocks, threadsPerBlock>>> kya specify karta hai?
Kernel launch configuration: kitne blocks launch karne hain aur har block mein kitne threads.
__syncthreads() kis kaam aata hai?
Threads ko ek block ke andar synchronize karne ke liye. Ensure karta hai ki saare threads barrier tak pahunch jayein pehle koi aage badhe. Tab use hota hai jab threads shared memory ke zariye data share karte hain.
Kya alag-alag blocks ke threads kernel execution ke dauran synchronize kar sakte hain?
Nahi. Blocks independently execute hote hain aur kisi bhi order mein run ho sakte hain. Blocks ke paar synchronize karne ka ek hi tarika hai: alag kernels launch karo.
Memory coalescing kya hai?
Jab ek warp ke threads consecutive memory addresses access karte hain, GPU requests ko ek transaction mein combine karta hai. Non-coalesced access kai alag transactions cause karta hai aur bahut slow hota hai.
__shared__ memory kya hai?
Ek block ke saare threads dwara shared fast on-chip memory. Global memory se bahut faster (~5 cycles vs ~400 cycles) lekin size mein limited (48-164KB per SM).
CUDA mein warp kya hota hai?
32 threads ka ek group jo lockstep mein execute hota hai (SIMT). NVIDIA GPUs par fundamental execution unit.
N elements ke liye blocks ki zaroorat kaisi calculate karte hain?
numBlocks = (N + threadsPerBlock - 1) / threadsPerBlock (integer arithmetic use karke ceiling division)
CUDA ke execution hierarchy ke teen levels kya hain?
Grid → Blocks → Threads. Grid GPU par map hota hai, blocks SMs par map hote hain, threads CUDA cores par map hote hain.
threadsPerBlock ke liye 256 common choice kyun hai?
Yeh 32 (warp size) ka multiple hai, SM par 4-8 blocks allow karta hai, aur 1024 max thread limit se kaafi neeche rehta hai. Occupancy ke liye achha balance hai.
GPU occupancy kya hota hai?
Active warps aur SM par maximum possible warps ka ratio. Zyada occupancy warp switching ke zariye better latency hiding enable karta hai.
CUDA mein sabse fast memory type kaun si hai?
Registers (per-thread, ~1 cycle access), phir shared memory (~5 cycles), phir L1/L2 cache, phir global memory (~400 cycles).

Concept Map

enables

provides

launches

runs on

organized as

contains

contains

maps to

maps to

maps to

grouped into

computes

CUDA platform

General-purpose GPU compute

Kernel function

Host CPU and RAM

Device GPU and VRAM

Grid

Blocks

Threads

Whole GPU

Streaming multiprocessors

CUDA cores

Warp of 32 in SIMT

globalIdx = blockIdx x blockDim + threadIdx