6.1.11 · D3 · HinglishParallelism & Multicore

Worked examplesVector - SIMD instructions (SSE, AVX, NEON)

2,725 words12 min read↑ Read in English

6.1.11 · D3 · Hardware › Parallelism & Multicore › Vector - SIMD instructions (SSE, AVX, NEON)

Yeh page parent SIMD note ke liye ek drill sheet hai. Wahan humne seekha ki SIMD matlab hai ek instruction, ek saath kai data elements, aur vector width ke liye speedup roughly hota hai. Yahan hum us idea ko stress-test karte hain har us awkward case ke against jo ek real program throw kar sakta hai — clean case, leftover case, overflow case, branchy case, aur exam trick.

Symbols se pehle: do words ko re-anchor karte hain taaki neeche kuch bhi mystery na rahe.

Figure — Vector - SIMD instructions (SSE, AVX, NEON)

Scenario matrix

Har SIMD problem actually yeh question hai ki data tray ke saath kaise align hota hai. Yeh cases ka poora grid hai — neeche ke chhe worked examples mein se har ek ek ya zyada cells fill karta hai.

Cell Jo scenario yeh test karta hai Kahan problem hoti hai Worked in
C1 , ka exact multiple hai "Clean" ideal case Ex 1
C2 , ka multiple nahi hai (remainder tail) , scalar cleanup Ex 2
C3 (ek tray se bhi kam elements) Degenerate: phir bhi ek instruction lagti hai Ex 3
C4 (empty input) Limiting case: speedup undefined Ex 3
C5 Reduction (lanes ke paas sum, element-wise nahi) Horizontal add, log-tree cost Ex 4
C6 Chhote integers par overflow / saturation clamp hona chahiye, wrap nahi Ex 5
C7 Loop ke andar data-dependent branch Masking; SIMD haar bhi sakta hai Ex 6
C8 Real-world word problem Image filter ke liye frame budget Ex 7
C9 Exam twist: mixed element sizes / alignment Trick numbers, woh gotcha Ex 8

Example 1 — C1: Clean multiple

Forecast: Aage padhne se pehle — ek number jot karo. Kitne vector adds, aur kya speedup?

  1. find karo. Yeh step kyun? Elements mein tray size woh hai jo kaam divide karta hai. Ek 256-bit register 32-bit per float . Toh .
  2. Vector instructions count karo. Kyun? Har instruction ek tray process karta hai. Count exactly — koi ceiling rounding nahi kyunki 8000, 8 ka clean multiple hai.
  3. Speedup. Kyun? Scalar adds karta hai; SIMD 1000 karta hai. Toh .

Verify: Units check — instructions dimensionless counts hain. floats processed, se match karta hai. ✓


Example 2 — C2: Remainder tail

Forecast: Pehle cleanup count guess karo — kya yeh 3 hai? Kuch aur?

  1. Full trays. Kyun? Integer division batata hai ki 8 ke kitne complete groups fit hote hain. full vector adds jo elements cover karte hain.
  2. Remainder. Yeh step kyun? Jo bacha hai woh tray fill nahi kar sakta, toh woh scalar code pe jaata hai. leftover elements → 3 scalar adds.
  3. Total instruction-equivalents. Kyun? Fairly compare karne ke liye hum dono parts count karte hain: vector + scalar "adds" worth of work.
  4. Real speedup. Kyun? Scalar baseline adds hai. .
Figure — Vector - SIMD instructions (SSE, AVX, NEON)

Verify: ✓. Aur (ceiling formula) — note karo yeh tail ko ek padded vector op count karta hai, slightly-optimistic deta hai. Dono views consistent hain; scalar-tail view (7.98) zyada honest hai.


Example 3 — C3 & C4: Ek tray se kam, aur empty

Forecast: Kya 5 elements phir bhi ek poori instruction cost karte hain? Speedup 8 se upar hai ya neeche?

  1. Case (a), instruction count. Ceiling kyun? Aap "5/8 of an instruction" issue nahi kar sakte — hardware ek poora addps run karta hai aur simply 3 empty cups ignore (ya mask) karta hai. .
  2. Case (a), speedup. Kyun? Scalar = 5 adds; SIMD = 1. . 8 ki ceiling se neeche kyunki 3 lanes waste hue.
  3. Case (b), empty input. Kyun care karein? Limiting behaviour: se instructions milte hain. Speedup undefined hai — accelerate karne ke liye kuch nahi hai.

Verify: (a) ✓ (speedup = width times utilization). (b) Koi bhi code guard kare aur loop bilkul skip kare; division by zero nahi hogi kyunki loop body kabhi run nahi karta.


Example 4 — C5: Reduction (lanes ke paas sum)

Forecast: Reduction stages ki number guess karo: 8? 3? 7?

  1. Tree kyun, straight line kyun nahi? Ek vector lane-to-lane parallel add karta hai, toh hum 8 values ko halves mein fold karte hain: . Har fold ek vector add hai.
  2. Stages count karo. kyun? 8 se repeatedly halving ke liye folds chahiye. Yahi key gain hai — ek scalar loop ko sequential adds chahiye.
Figure — Vector - SIMD instructions (SSE, AVX, NEON)
  1. Total AVX op count. 1 add kyun? Ek vmulps saare 8 products produce karta hai, phir 3 reduce-adds. vector ops.
  2. Scalar baseline. 15 kyun? 8 multiplies + 7 adds scalar ops.
  3. Speedup. se kaafi neeche kyunki reductions mein woh unavoidable -tree overhead hota hai.
Recall Reductions full speedup kyun kabhi nahi hit karte

Question: Dot product 8-wide SIMD par 8× ki jagah sirf ~4× kyun hai ::: Element-wise multiply perfectly parallelizes (8×), lekin lanes ke paas sum karne ke liye extra folding steps chahiye jo ek scalar loop apne single accumulator ke andar hide kar leta hai — toh wins dilute ho jaate hain.

Verify: stages ✓; scalar ops ✓; AVX ops ✓; ✓.


Example 5 — C6: Overflow vs. saturation

Forecast: 8 bits mein wrap hone par kya banta hai?

  1. True sum. Kyun? , jo 8-bit ceiling 255 se zyada hai.
  2. Wrapping result. Modulo 256 kyun? Ek 8-bit register values hold karta hai; overflow se wrap hota hai. . Ek bright pixel almost black ho jaata hai — yeh ek visible bug hai.
  3. Saturating result. Clamp kyun? vqadd (q = saturating) koi bhi cheez jo 255 se zyada ho usse 255 tak clamp kar deta hai. Toh pixel 255 (pure white) ban jaata hai — physically sensible answer.

Verify: ; ✓; ✓. Register phir bhi ek instruction mein 16 lanes hold karta hai, chahe koi bhi add variant choose kiya jaaye.


Example 6 — C7: Branchy loop

Forecast: Dono paths same kaam karte hain — lekin kiske paas kam total ops hain? Apni gut trust karo.

  1. Scalar cost. Per element 2 kyun? Ek compare, ek add-or-sub. Total ops.
  2. SIMD instruction count. Per tray 4 kyun? SIMD branch nahi kar sakta, toh yeh dono outcomes compute karta hai aur select karta hai: compare, inc-all, dec-all, blend = 4 vector ops per 8 elements. Trays ki count . Total vector ops.
  3. Fairly compare karo. "Yahan phir bhi jeetta hai" kyun note karo? , toh SIMD does win op-count par is clean, predictable branch ke liye.
  4. Parent ne warn kyun kiya. Kyunki SIMD ne 2× useful kaam kiya (har lane mein dono inc aur dec) — utilization sirf 50% hai. Agar branches rare ya unpredictable hain, toh scalar branch prediction is doubled work ko beat kar sakti hai. Lesson: SIMD-with-masking kaam karta hai, lekin iska speedup wasted lanes se throttle hota hai.

Verify: Scalar ✓; SIMD ✓; ratio (yahan 4× edge, lekin 50% wasted computation ke saath).


Example 7 — C8: Real-world word problem

Forecast: Kya filter frame budget ka zyada hissa khaata hai, ya ek chhoti si sliver?

  1. Pixels per frame. Multiply kyun? Image ek grid hai. pixels.
  2. NEON instructions per frame. 16 se divide kyun? Har vqaddq_u8 16 pixels brighten karta hai. instructions (clean multiple — , koi tail nahi).
  3. Instructions per second. kyun? 60 frames har second. instructions/s.
  4. Time per frame. Kyun? 1 ns each par: .
  5. Budget ka fraction. 16.67 ms kyun? Ek 60 fps frame ms ka hota hai. Fraction .

Verify: ✓; ✓; ✓; ✓. Units: ns/ms cancel hokar pure fraction ban jaata hai ✓. Filter almost free hai — budget decoding aur display ke liye bachta hai.


Example 8 — C9: Exam twist (mixed sizes + alignment)

Forecast: Array size aur address dono traps hain. Solve karne se pehle unhe spot karo.

  1. find karo — pehla trap. 4 kyun nahi? Students float examples se assume karte hain. Lekin yeh 16-bit ints hain: . Toh .
  2. Tail ke saath instruction count. Kyun? full vectors jo elements cover karte hain; remainder scalar ops. Toh 512 vector + 2 scalar.
  3. Alignment — doosra trap. Address bits kyun check karein? Aligned loads ko 16 se divisible address chahiye. decimal mein; . Aligned nahimovaps fault karega. Aapko unaligned load movups use karna hoga (slower) ya 16-byte boundary par re-allocate karna hoga.

Verify: ✓; , tail ✓; toh aligned load illegal ✓.


Aage kahan jaana hai

  • Ex 6 mein masking idea Data Paralelism ka core hai aur isliye Loop Vectorization aur Auto-vectorization unpredictable branches ko touch karne se inkar karte hain.
  • Ex 8 mein alignment trap directly Cache Optimization (64-byte cache lines) se connect hota hai.
  • SIMD Flynn's Taxonomy ke "SI-MD" box mein baithta hai; jab arrays bahut bade ho jaate hain, aap GPU Architecture par graduate karte ho. Jab woh chhote rehte hain lekin independent hote hain, Instruction-Level Parallelism slack pick up karta hai.
Recall Self-test

Clean ka ideal speedup 8-wide AVX par ::: exactly 8 , ke liye vector instructions plus scalar tail ::: 1250 vector + 3 scalar 8-bit wrapping add mein ::: 24 saturating add ke saath ::: 255 grayscale frame brighten karne ke liye NEON instructions ::: 129600