5.2.5 · D4 · HinglishProcessor Datapath & Pipelining

ExercisesStructural hazards

4,118 words19 min read↑ Read in English

5.2.5 · D4 · Hardware › Processor Datapath & Pipelining › Structural hazards

Yeh page ek self-test ladder hai. Har rung pichle se mushkil hai: pehle tum sirf structural hazard ko pehchannte ho, aur end mein ek ke around design karte ho. Har problem ka ek fully worked solution ek collapsible box mein chhupa hai — pehle khud try karo, phir reveal karo.

Prerequisites, agar koi line shaky lage: parent note, Pipelining Fundamentals, Pipeline Stalls. Contrast partners: Data Hazards, Control Hazards.


Woh do formulas jis par yeh poora page chalti hai

Kisi bhi exercise se pehle, chalte hain (scratch se) woh sirf do facts banate hain jo neeche har problem reuse karta hai. Yahan kisi aur note se kuch assume nahi kiya gaya.

Recall Model

ke edge cases (Level 5 se pehle padho)

  • ya : koi real hazard nahi, ideal pipeline.
  • : har instruction ek baar stall karta hai, throughput half.
  • Agar ho toh? Bilkul allowed — iska matlab sirf yeh hai ki average instruction ek se zyada bubble khata hai (e.g. gives ). CPI pipeline depth tak grow kar sakta hai (full serialisation); yeh kabhi se neeche nahi jaata kyunki .
  • Negative stalls impossible hain: aur definition se hain (tum un-stall nahi kar sakte), isliye hamesha. Agar koi formula tumhe de toh sign error ki hai.

Level 1 — Recognition

Kya tum ek structural hazard ko doosron se alag kar sakte ho, aur resource conflict spot kar sakte ho?

Exercise 1.1

Chaar instructions (I1–I4) ek 5-stage pipeline (IF, ID, EX, MEM, WB) mein run hoti hain ek aisi machine pe jisme ek unified memory hai (ek single port jo IF aur MEM ke beech share hota hai). Neeche timing chart dekho.

Figure — Structural hazards
Figure 1 — Rows instructions I1–I4 hain; columns clock cycles 1–8 hain; har labelled box woh stage hai jo us instruction ne us cycle mein occupy ki. Do red boxes ek hi column mein hain (cycle 4): I1 ka MEM aur I4 ka IF. Ek red arrow unhe single memory port par ladte pair ke roop mein mark karta hai.

Question: Kis cycle mein structural hazard hota hai, aur kaunse do stages kaunse resource ke liye ladte hain?

Recall Solution

Chart ko column by column padho (har column ek clock cycle hai). Structural hazard tab hota hai jab do stages same column mein same physical unit maangti hain — woh exactly Figure 1 mein red boxes ki pair hai.

  • Cycle 4: I1 MEM mein hai (data ke liye memory touch kar raha hai) aur I4 IF mein hai (apni instruction fetch karne ke liye memory touch kar raha hai).
  • Single memory port unme se sirf ek ko serve kar sakta hai.

Answer: Hazard cycle 4 mein hai; conflict I1 ke MEM stage aur I4 ke IF stage ke beech hai, dono single memory port maang rahe hain.

Exercise 1.2

Har situation ko Structural, Data, ya Control hazard classify karo:

  • (a) ADD R1,R2,R3 ke baad SUB R4,R1,R5 — SUB ko R1 chahiye ADD ke write karne se pehle.
  • (b) Ek single ALU ko EX-stage add aur IF-stage PC+4 dono same cycle mein chahiye.
  • (c) Ek BEQ (branch) EX mein hai aur processor abhi nahi jaanta ki agle instruction ya branch target ko fetch karna hai.
Recall Solution
  • (a) Data hazard — yeh information/order ke baare mein hai (ek value produce hoti phir consume hoti), shared hardware ka box nahi.
  • (b) Structural hazard — do stages ek hi physical adder ek cycle mein chahte hain.
  • (c) Control hazarddecision ki aage kya fetch karna hai woh resolve nahi hua.

Rule of thumb: agar tum ise hardware unit ka ek aur copy khareed kar fix kar sako, toh structural tha.


Level 2 — Application

CPI formula mein numbers plug karo aur performance predict karo.

Exercise 2.1

Ek processor mein hai. Ek memory structural hazard pipeline ko har load/store ke liye 1 cycle stall karta hai. Loads/stores sab instructions ka hissa hain. aur ideal pipeline ke versus khoyi performance ka fraction nikalo.

Recall Solution

Page ke upar wala boxed model use karo, . Pehle har symbol:

  • woh fraction of instructions hai jo hazard trigger karte hain.
  • cycle stall penalty per hazard hai.

Ab page ke upar se derive kiya " at equal clock" result use karo. Same machine, same clock, isliye hazarded pipeline ka ideal se ratio hai:

Answer: ; performance lost slower.

Exercise 2.2

Same machine, lekin designer ek separate data cache add karta hai, toh IF aur MEM kabhi collide nahi karte — memory hazard eliminate ho gaya. Lekin ek naya hazard aata hai: EX aur PC-increment ke beech share hone wala single ALU instructions pe 1 cycle stall karta hai. Naya kya hai, aur kya redesign ne help ki?

Recall Solution

Boxed model mein, memory hazard eliminate karna uska ko set karta hai, toh woh term disappear ho jaati hai. Sirf ALU term bachi hai:

Purane se compare karo. Same clock phir, toh use karo; new design ka old se speedup unke CPIs ka ratio hai:

Answer: ; redesign machine ko lagbhag 24% faster banata hai. Haan, help ki.


Level 3 — Analysis

Pipeline diagram padho, khud stalls insert karo, aur cycles count karo.

Exercise 3.1

Ek single-memory-port machine pe, ek load instruction ka MEM stage us instruction ke IF se collide karta hai jo usse 3 slots peeche hai (kyunki MEM stage 4 hai aur IF stage 1: woh line up karte hain jab instructions 3 apart hon). Hum 8 back-to-back loads run karte hain.

Assume karo: har MEM–IF collision younger instruction (jo IF mein hai) ko exactly 1 cycle stall karta hai. Abhi ke liye start-up fill ignore karo; sirf hazard ki wajah se insert hue extra stall cycles count karo 8 loads mein.

Figure — Structural hazards
Figure 2 — Aath loads mein se pehle paanch (L1–L5), rows = instructions, columns = clock cycles. Har row ka MEM box red mein draw kiya gaya hai kyunki woh memory access hai jo baad wali instruction ke IF ko block karega. Figure ke andar caption line rule batata hai: har red MEM stage apne follower ke IF ko +1 cycle stall karta hai.

Question: Total kitne stall cycles insert hote hain, aur in 8 instructions pe resulting kya hai?

Recall Solution

Ideal issue mein "8" kyun aata hai. Ek pipeline mein, ek naya instruction har clock pe IF mein enter karta hai aur, bina hazards ke, exactly ek instruction har clock pe finish hota hai. Isliye 8 instructions issue karna ideally 8 issue slots leta hai — ek cycle each. Yahan se woh bare aata hai (yeh ideal-issue count hai, kisi bhi bubble se pehle; start-up fill exclude hai jaise problem ne kaha).

Har stall ka cause: jab ek load MEM pe pahunchta hai (Figure 2 mein ek red box), us instruction ko jo us same cycle mein IF enter karna chahti hai wait karna padta hai. Pehla load (L1) cycle 4 mein MEM pe pahunchta hai jab L4 IF chahta hai — toh L4 stall karta hai. Baad ke har load similarly ek stall push karta hai apne peeche wale instruction pe.

Window ke andar counting: Loads L1…L8 mein se har ek chahta hai ek MEM/IF collision generate kare apne follower ke saath. Last load L8 ka in 8 ke andar koi follower nahi hai — uska induced stall ek aisi instruction pe padega jo is window ke bahar hai — isliye woh yahan koi stall contribute nahi karta. Bacha collision L1…L7 se → 7 stall cycles.

Answer: Window ke andar 7 stall cycles; .

Model se sanity check: steady state mein (infinite stream) har load stalls karta hai, toh gives . Humara windowed se thoda neeche hai kyunki L8 ka stall window ke bahar spill ho gaya — exactly wahi jo hum expect karte hain. ✓

Exercise 3.2

Ab maan lo 8 instructions mein se sirf aadhi loads hain (baaki 4 register-only ADDs hain jo kabhi MEM mein memory touch nahi karte). Loads positions I1, I3, I5, I7 pe hain. Har load abhi bhi apne follower ko 1 cycle stall karta hai (aur har load ka follower is baar window ke andar hai). 8 instructions pe CPI compute karo.

Recall Solution

I1, I3, I5, I7 pe loads → 4 loads, har ek apne peeche wale instruction (I2, I4, I6, I8 respectively — sab window ke andar) pe 1 stall induce karta hai. Toh 4 stall cycles.

Boxed model se cross-check, , :

Answer: 4 stalls; .


Level 4 — Synthesis

Multiple hazard sources combine karo aur design trade-offs ke baare mein reason karo.

Exercise 4.1

Ek design team same pipeline pe teen independent structural hazards measure karti hai (woh same instruction mein overlap nahi karte, isliye unke stall cycles simply add ho jaate hain). Table mein, us hazard ki frequency hai (instructions ka fraction jo ise trigger karte hain) aur uska stall penalty cycles mein hai:

Source Frequency Penalty
Unified memory (IF vs MEM) 0.30 1
Shared ALU (EX vs PC+4) 0.05 1
Single register write port 0.02 1

compute karo. Phir: memory hazard fix karna (split I/D caches) 10 units area leta hai; ALU hazard fix karna (ek PC adder add karo) 1 unit leta hai; write port fix karna 4 units leta hai. Tumhare paas 5 area units ka budget hai. Kaunse fixes tumhare budget mein maximum performance gain karte hain?

Recall Solution

Baseline CPI — kyunki teeno hazards kabhi same instruction ko hit nahi karte, unke per-instruction stall contributions simply ideal ke upar add ho jaate hain (yeh boxed model hai kaafi saare terms ke saath):

"Benefit = " kyun. Kisi hazard ko remove karna CPI sum se exactly uska term delete karta hai — toh CPI jo tum save karte ho hazard fix karke woh precisely hai. Yahi fix ka benefit hai. Yahan har hai, isliye benefit .

"Benefit per area" sahi metric kyun hai. Ek hard area budget ke saath tum chahte ho spend ki gayi har area unit pe maximum CPI remove ho — classic bang-for-buck ratio . Fixes ko usse rank karo:

Fix Area CPI removed () Benefit per area
Memory 10 0.30 0.030
ALU 1 0.05 0.050
Write port 4 0.02 0.005

Budget = 5. Memory fix (area 10) seedha unaffordable hai. 5 units ke andar ALU (1) + write port (4) = exactly 5 units le sakte ho:

  • CPI removed → new .

Alternative: sirf ALU (1 unit) 0.05 remove karta hai → , 4 units unused chhod ke. ALU + write port dono lena pura budget spend karta hai aur tak pahunchta hai, 5 units mein best achievable.

Answer: ALU adder + register write port kharido (total area 5), tak pahunchte hain. Memory fix, though woh sabse bada single stall remove karta hai, budget mein fit nahi hota.

Exercise 4.2

Team do poori architectures ke beech decide kar rahi hai:

  • A (Harvard): alag instruction aur data memories — zero memory structural hazards, lekin 12 area units aur higher static power.
  • B (Von Neumann + interlock): ek memory + stall logic — memory hazard , , sirf 2 area units.

Agar dono same clock pe run karte hain aur hai, toh kaunsi memory-hazard frequency pe architecture B ki performance A ki performance ke equal hogi? (Assume A mein hai.)

Recall Solution

A: . B: . Same clock, isliye (page ke upar ke result se) equal performance matlab equal CPI:

Interpretation: nonzero hazard frequency ke saath, B hamesha hazard-free Harvard machine se slower rahega. B sirf A se tab tie karta hai jab ho — yaani practically kabhi nahi. Toh choice purely performance (A) vs area/power (B) hai: B tabhi justified hai jab 10 extra area units aur power guaranteed slowdown (at ) se zyada matter kare.

Answer: ; B kabhi A se speed mein match nahi kar sakta, toh B strictly area/power reasons ke liye chuno.


Level 5 — Mastery

Design karo aur defend karo; limits aur corner cases ke baare mein reason karo.

Exercise 5.1

Tumhare haath ek superscalar core aaya hai jo 2 instructions per cycle issue karta hai. Uske register file mein 3 read ports aur 1 write port hai. Ek cycle mein do co-issued instructions dono 2-source, 1-destination ALU ops hain. (a) Kya read-port structural hazard hai? (b) Write-port hazard? (c) Dual 2-source/1-dest issue ke liye ports par kabhi stall na karne ki minimum port counts batao.

Recall Solution

Ek cycle mein demand do 2-source, 1-destination instructions ke liye jo saath issue ki gayi hain:

  • Reads needed: har op 2 source registers read karta hai, do ops → register reads ek cycle mein.
  • Writes needed: har op 1 destination write karta hai, do ops → register writes jab dono saath WB pe pahunchte hain.

(a) Read-port hazard? File mein 3 read ports hain lekin cycle 4 demand karta hai. , toh haan, read-port structural hazard hai — file physically 4 source operands ek saath deliver nahi kar sakta, toh ek instruction stall karna padega (ya issue ek op tak narrow karna padega us cycle mein).

(b) Write-port hazard? File mein 1 write port hai lekin do destinations same WB cycle mein commit karna chahte hain. , toh haan, write-port structural hazard hai — do writes serialise karne padte hain, ek ko stall karte hue.

(c) Kabhi stall na karne ke liye minimum ports dual 2-source/1-dest issue ke liye: sab sources ke liye enough read ports () aur sab destinations ke liye enough write ports (). Toh register file ko 4 read ports aur 2 write ports chahiye (generally, read aur write ports -wide issue of 2-source/1-dest ops ke liye).

Answer: (a) haan — 4 reads chahiye, 3 hain; (b) haan — 2 writes chahiye, 1 hai; (c) 4 read ports, 2 write ports. Yahi reason hai ki wider Superscalar Processors itna zyada pay karte hain — port count issue width ke saath scale karta hai, aur har extra port area aur power add karta hai.

Exercise 5.2 (Corner cases — har sign cover karo)

Model ke liye (jahan = hazard frequency, = stall penalty), degenerate/limiting inputs ke baare mein reason karo:

  • (a) : machine kaisi dikhti hai?
  • (b) : CPI kya hai, aur pipeline diagram kaisa dikhta hai?
  • (c) : kya yeh hazard hai?
  • (d) ek 5-stage pipe pe: interpret karo — pipelining ke saath effectively kya hua?
Recall Solution
  • (a) : kabhi koi hazard nahi, ideal pipeline, ek instruction har cycle mein finish hota hai. Yeh Harvard/split-cache dream hai.
  • (b) : har instruction exactly ek cycle stall karta hai. Diagram har instruction ke baad ek bubble hai — throughput half (Ex 3.1 ke steady-state limit 2.0 se match karta hai ✓).
  • (c) : ek "hazard" jo zero cycles cost kare woh hazard hai hi nahi — resource conflict for free resolve ho gaya (e.g. ek cheap duplicated unit). Frequency irrelevant ho jaati hai.
  • (d) ek 5-stage pipe pe : har instruction 4 cycles wait karta hai, toh instruction tabhi start hoti hai jab instruction fully drain ho jaaye. Yeh serial execution hai — pipelining ka overlap completely destroy ho gaya. CPI pipeline depth ke equal hai, worst case. Note yahan, jo bilkul valid hai: average instruction ek se zyada bubble khata hai.

Answer: (a) ideal, CPI 1; (b) CPI 2, har instruction ke baad ek bubble; (c) real hazard nahi, CPI 1; (d) CPI 5 = fully serialised, pipeline ka koi benefit nahi baca.


Recall Rapid self-check (right side chhupa lo)

Structural hazard define hota hai sharing se … ::: same cycle mein physical hardware resource share karna. Hazards ke saath CPI formula ::: . Performance kyun hai ::: time per instruction cycle time; performance uska reciprocal hai, isliye equal clock pe bada CPI = slower. Ex 2.1 answer ::: CPI = 1.30, lagbhag 23% slower. Ex 3.2 answer ::: CPI = 1.5. Ex 4.1 best-within-budget CPI ::: 1.30 (ALU + write-port fixes). Dual 2-source/1-dest issue ke liye kitne ports chahiye ::: 4 read, 2 write. on 5 stages pe limiting CPI ::: 5 (fully serial).

Related: Parent: Structural hazards · Pipeline Stalls · Superscalar Processors · Memory Hierarchy · 5.2.05 Structural hazards (Hinglish)