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.
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.
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:Iterations=log2(blockDim.x)
Har iteration kaam ko half karta hai. 256 threads ke liye: 255 sequential additions ki jagah 8 iterations.
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.
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 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:
Registers per thread: Zyada registers → kam threads fit hote hain
Shared memory per block: Zyada shared memory → kam blocks fit hote hain
Block size: Bahut chota → resources waste; bahut bada → SM par blocks limit karta hai
Occupancy=Maximum Warps per SMActive Warps per SM
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!
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?
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.