Question bank — Vector - SIMD instructions (SSE, AVX, NEON)
6.1.11 · D5· Hardware › Parallelism & Multicore › Vector - SIMD instructions (SSE, AVX, NEON)
Yeh page SIMD ke ideas par attack karta hai, arithmetic par nahi. Neeche har item ek one-line question hai jisme ::: se answer reveal hota hai. Answer cover karo, pehle apna guess commit karo, phir check karo. Agar koi galat ho, jo misconception usne target kiya wahi exactly woh hai jo real code mein tumhe bite karega.
Shuru karne se pehle, teen words jinpar hum poore time lean karenge — plain language mein earn kiye gaye taaki kuch bhi unexplained na lage:
Yeh teen pictures dhyan mein rakho; neeche har trap unhi mein se ek ka disguise hai. Parent note Data Paralelism, Loop Vectorization, aur Flynn's Taxonomy woh background hai jiske against tumhara test ho raha hai.
True or false — justify karo
SIMD speedup deta hai kyunki CPU vector instructions ke dauran faster run karta hai
Ek 256-bit AVX register hamesha 8 elements process karta hai
Agar aapke array mein 1000 elements hain aur hai, toh SIMD exactly 125 vector instructions karta hai
SIMD ek tarah ka MIMD hai Flynn's Taxonomy mein
Unaligned loads (_mm_loadu_ps) hamesha crash karte hain
_mm_load_ps hai jo ek unaligned pointer par fault karta hai.Ek dot product ka w-lane multiply aapko directly answer de deta hai
Register width ko 128 se 256 bits tak double karna hamesha real-world throughput double karta hai
Saturating add aur normal add sirf tab differ karte hain jab values overflow hoti hain
SIMD require karta hai ki saare lanes mein same value ho
Error dhundho
float* p = malloc(16); _mm_load_ps(p); — kya unsafe hai?
malloc 16-byte alignment guarantee nahi karta, isliye aligned _mm_load_ps fault kar sakta hai; aligned_alloc(16, ...) ya unaligned _mm_loadu_ps use karo.Ek loop for (i=0; i<n; i+=8) do_avx(a+i); jisme n=10 hai — kya break hota hai?
a[8..15] read karta hai, array se aage chala jata hai aur kuch skip nahi karta lekin garbage read karta hai; aapko last 2 elements ke liye ek scalar cleanup loop chahiye, ya n=10 koi 8 ka multiple nahi hai.ADDPS ek array of doubles par use kiya — kya galat hai?
ADDPS packed single (32-bit floats) hai. 64-bit doubles par aapko ADDPD (packed double) use karna chahiye, warna aap har double ke bits ko do garbage floats ki tarah reinterpret karoge.if (a[i]>0) b[i]++; else b[i]--; ko plain SIMD se vectorise karna — yeh kyun stall karta hai?
8 products ko ek vaddps se sum karna aur lane 0 read karna — kya missing hai?
vaddps do vectors ko lane-wise add karta hai, yeh ek single vector ke 8 lanes ko ek mein collapse nahi karta. Aapko phir bhi horizontal adds / extracts chahiye lanes ke across reduce karne ke liye pehle lane 0 total hold kare.vmovaps [c], xmm0 jahan c ek buffer mein 17-byte offset hai — kya flaw hai?
vmovups par switch karo.Why questions
Vector instructions ki count mein ceiling kyun use hoti hai?
Alignment kyun matter karta hai agar unaligned loads kaam karte hain?
Horizontal reduction inherently vertical multiply se kam efficient kyun hai?
SIMD naturally Loop Vectorization aur Auto-vectorization ke saath pair kyun karta hai lekin recursion ke saath nahi?
Memory bandwidth, lane count nahi, aksar true speedup ceiling kyun hoti hai?
Memory mein data ko contiguously pack karna SIMD ke liye kyun matter karta hai?
movaps consecutive addresses se elements grab karta hai; agar elements scattered hain, toh aapko unhe ek ek karke gather karna padega (ya costly gather instructions use karne padenge), one-load advantage destroy ho jata hai.SIMD multicore parallelism ke same kyun nahi hai?
Edge cases
Jab ho (single element) toh SIMD kitna speedup deta hai?
Jab exactly ho (e.g. 8 elements, ) toh ka kya hota hai?
Agar ho (ek element ek full vector se zyada), toh hidden cost kya hai?
Ek saturating 8-bit add boundary par kya karta hai?
Kya ek zero-length array () ko SIMD loop mein feed karna safe hai?
n>0 check kare; vector instructions sahi hai, lekin ek naive for(i=0; i<n; i+=w) already body skip kar deta hai, jabki ek fixed unrolled call out of bounds read kar leta.Jab saare lanes mein identical values hon, kya SIMD tab bhi help karta hai?
vdupq_n/_mm_set1) jo ek scalar se lanes fill karta hai woh sahi tool hai, common jab ek constant jaise brightness har pixel mein add karte hain.Recall One-line self-test
Woh ek sentence jo upar har trap unlock karta hai ::: SIMD same operation karta hai different data par, ek instruction wide — isliye jo bhi sameness (branches), width (element size), contiguity (alignment/gather), ya count (remainders) tod deta hai wahi jagah yeh fail hota hai.