Computer Science और IT (कंप्यूटर साइंस)‹›⌂ 0:00 पढ़ाई

Module 3 · 23 मिनट पढ़ाई · मुफ़्त chapter

Instruction pipelining और hazards

संक्षेप में

k-stage pipeline, n instructions को (k + n − 1) cycles में चलाती है, इसलिए speed-up nk/(k + n − 1), k के पास पहुँचता है। Hazards से stalls होते हैं: forwarding ज़्यादातर data stalls हटा देती है पर load-use जोड़ी को एक stall फिर भी चाहिए, और branches CPI में frequency गुणा penalty जोड़ती हैं।

इसे 3D में देखेंInstruction pipeline as an assembly line: stages, stalls, forwarding and branches. घुमाएँ, अंदर से काटकर देखें, या Watch दबाएँ।
आप किस exam की तैयारी कर रहे हैं? हम आपके exam के practice और पिछले साल के सवाल दिखाएँगे।

शून्य से शुरू: tiffin की line

सोचिए, किसी school की रसोई में 500 tiffins पैक होने हैं। अगर एक ही रसोइया हर डिब्बा पूरा भरता है, यानी चावल, दाल, सब्ज़ी, रोटी और ढक्कन लगाकर ही अगला डिब्बा उठाता है, और हर चीज़ में एक मिनट लगता है, तो हर डिब्बे में पाँच मिनट लगते हैं। तब सभी 500 डिब्बों में 2,500 मिनट लगेंगे।

अब पाँच सहायकों को line में खड़ा कीजिए, हर एक एक चीज़ डालकर डिब्बा आगे बढ़ा देता है। पहला डिब्बा बाहर आने में अब भी पाँच मिनट लेता है, लेकिन उसके बाद हर मिनट एक भरा डिब्बा निकलता है। अब 500 डिब्बों में लगभग 504 मिनट लगते हैं, यानी लगभग पाँच गुना तेज़।

Processor भी instructions के साथ यही करता है, और इस तरकीब को pipelining कहते हैं। हर instruction को stages में बाँटा जाता है। जब एक instruction execute हो रही होती है, तब अगली decode हो रही होती है और उसके बाद वाली fetch हो रही होती है।

यह क्यों ज़रूरी है? आज का processor लगभग हर clock cycle में एक instruction पूरी कर पाता है, और इसकी मुख्य वजह pipelining ही है। इसकी दिक़्क़त यह है कि कुछ instructions को दूसरों का इंतज़ार करना पड़ता है, और हर इंतज़ार रफ़्तार घटाता है। यह chapter इसी दिक़्क़त को समझता है।

Five-stage pipeline

Classic pipeline में पाँच stages होते हैं, और हर stage एक clock cycle लेता है। IF memory से instruction लाता है, ID उसे decode करता है और उसके registers पढ़ता है, और EX ALU का काम करता है। MEM loads और stores के लिए data memory पढ़ता या लिखता है, और WB result को register file में लिखता है।

Stages के बीच pipeline latches होते हैं, जैसे IF/ID और ID/EX registers, जो हर instruction के अधूरे results रखते हैं। इनकी वजह से एक ही cycle में हर stage अलग instruction पर काम कर पाता है, और instructions आपस में नहीं मिलतीं।

ऊपर का space-time diagram मुख्य बातें एक नज़र में दिखा देता है। पहली instruction को अब भी 5 cycles चाहिए, जिसे latency कहते हैं, लेकिन cycle 5 से हर cycle में एक instruction पूरी होती है, जिसे throughput कहते हैं। चार instructions 20 की जगह 8 cycles में पूरी हो जाती हैं।

यह क्यों ज़रूरी है? GATE का लगभग हर pipeline सवाल यही diagram बनाकर हल होता है। इसका इतना अभ्यास कीजिए कि stalls और branches को इसमें बिना सोचे रख सकें।

Speed-up और throughput numericals

मान लीजिए pipeline में kk stages हैं, cycle time τ\tau है, और यह बिना stall के nn instructions चलाती है। पहली instruction kk cycles लेती है और बाकी n−1n - 1 instructions में से हर एक बस एक cycle और जोड़ती है।

Tpipe=(k+n−1)τ,Tnonpipe=nkτ,S=nkk+n−1T_{\mathit{pipe}} = (k + n - 1)\tau, \qquad T_{\mathit{nonpipe}} = nk\tau, \qquad S = \frac{nk}{k + n - 1}

nn बड़ा होने पर SS, kk के पास पहुँचता है, इसलिए kk-stage pipeline ज़्यादा से ज़्यादा kk गुना तेज़ हो सकती है। Throughput, हर second 1τ\dfrac{1}{\tau} instructions के पास पहुँचता है। Efficiency Sk\dfrac{S}{k} है, यानी stage slots का वह हिस्सा जो काम का काम कर रहा है।

हल किया उदाहरण 1। τ\tau = 2 ns वाली 5-stage pipeline 100 instructions चलाती है। तब Tpipe=(5+99)×2=208T_{\mathit{pipe}} = (5 + 99) \times 2 = 208 ns और Tnonpipe=100×5×2=1000T_{\mathit{nonpipe}} = 100 \times 5 \times 2 = 1000 ns है, इसलिए S=1000208≈4.81S = \dfrac{1000}{208} \approx 4.81।

असली stages बराबर नहीं होते। Clock को सबसे धीमे stage के हिसाब से चलना पड़ता है, और हर latch अपनी delay जोड़ता है। इसलिए cycle time, सबसे बड़ी stage delay और latch delay का जोड़ है। Non-pipelined machine में latches नहीं होते, इसलिए उसका समय stage delays का सीधा जोड़ है।

हल किया उदाहरण 2। पाँच stages 150, 120, 160, 140 और 130 ps लेते हैं, और हर latch 10 ps जोड़ता है। इसलिए τ=160+10=170\tau = 160 + 10 = 170 ps है, जबकि एक non-pipelined instruction 150+120+160+140+130=700150 + 120 + 160 + 140 + 130 = 700 ps लेती है। 1,000 instructions के लिए समय और speed-up नीचे दिए हैं।

Tpipe=(5+999)×170=170680 ps,S=1000×700170680≈4.10T_{\mathit{pipe}} = (5 + 999) \times 170 = 170680 \text{ ps}, \qquad S = \frac{1000 \times 700}{170680} \approx 4.10

यह क्यों ज़रूरी है? 4.10 और आदर्श 5 के बीच का फ़र्क़ असमान stages और latch delays से आता है, और designers stages को संतुलित करके इसे घटाते हैं। GATE के numericals ठीक यही चार संख्याएँ पूछते हैं: cycle time, कुल समय, speed-up और throughput।

Structural, data और control hazards

आदर्श pipeline मानती है कि हर instruction हर cycle में अपने अगले stage में जा सकती है। जो भी चीज़ इसे तोड़े, उसे hazard कहते हैं। Hazard की वजह से pipeline को stall करना पड़ता है, यानी उसमें एक ख़ाली slot जुड़ जाता है, जिसे bubble कहते हैं। Hazards तीन तरह के होते हैं।

Structural hazard तब होता है जब एक ही cycle में दो instructions को एक ही hardware चाहिए। जैसे, memory का एक ही port हो, तो अपने MEM stage वाला load और अपने IF stage वाली बाद की instruction, दोनों memory माँगते हैं। अलग instruction और data caches रखने से यह आम मामला ख़त्म हो जाता है।

Data hazard तब होता है जब किसी instruction को ऐसा मान चाहिए जो पिछली instruction ने अभी बनाया नहीं है। Dependences तीन तरह की होती हैं, और इनके नाम दो accesses के क्रम से रखे गए हैं।

  • Read after write (RAW): दूसरी instruction वह register पढ़ती है जिसे पहली लिखती है, जैसे ADD R1, R2, R3 के बाद SUB R4, R1, R5, और यह असली dependence है।
  • Write after read (WAR): दूसरी instruction वह register लिखती है जिसे पहली पढ़ती है, जो data का असली बहाव नहीं, बस नाम का टकराव है।
  • Write after write (WAW): दोनों instructions एक ही register लिखती हैं, इसलिए उनका क्रम बनाए रखना पड़ता है।

सरल in-order five-stage pipeline में registers, ID में पढ़े और WB में लिखे जाते हैं, और यह हमेशा program के क्रम में होता है। इसलिए वहाँ WAR और WAW से गड़बड़ नहीं होती, और सिर्फ़ RAW से stalls होते हैं; बाकी दो out-of-order machines में मायने रखते हैं।

Control hazard branches से आता है। Branch taken होगी या नहीं, यह पता चलने से पहले ही pipeline अगली instructions fetch कर लेती है। अगर अनुमान ग़लत निकले, तो उन instructions को फेंकना पड़ता है।

यह क्यों ज़रूरी है? हर hazard का अपना इलाज है: structural hazard के लिए ज़्यादा hardware, data hazard के लिए forwarding और control hazard के लिए prediction। GATE अक्सर code का एक टुकड़ा देकर उसकी हर RAW dependence गिनवाता है।

Stalls और forwarding

ADD R1, R2, R3 के ठीक बाद SUB R4, R1, R5 लीजिए। ADD, R1 को अपने WB stage में, यानी cycle 5 में लिखती है, लेकिन SUB, R1 को अपने ID stage में, यानी cycle 3 में पढ़ना चाहती है। मान लीजिए register file cycle के पहले आधे में लिखी जाती है और दूसरे आधे में पढ़ी जाती है। तब SUB, R1 को जल्दी से जल्दी cycle 5 में पढ़ पाएगी, इसलिए उसे दो stall cycles चाहिए।

Forwarding, जिसे bypassing भी कहते हैं, इनमें से ज़्यादातर stalls हटा देती है। ADD का result उसके EX stage के आख़िर में ही EX/MEM latch में बन चुका होता है। अतिरिक्त wires उसे cycle 4 में सीधे SUB के ALU input तक पहुँचा देते हैं। EX/MEM और MEM/WB latches से forwarding हो, तो लगातार चलने वाली ALU instructions को कोई stall नहीं चाहिए।

Loads इसका अपवाद हैं। LOAD R1, 0(R2) के बाद ADD R4, R1, R5 में load हुआ मान MEM के आख़िर में, यानी cycle 4 में मिलता है, लेकिन ADD को वह अपने EX की शुरुआत में, यानी cycle 4 में ही चाहिए। इसलिए forwarding के साथ भी एक stall टाला नहीं जा सकता, और इसे load-use hazard कहते हैं।

हल किया उदाहरण। Forwarding वाली five-stage pipeline पर पाँच instructions चलती हैं, और एक load-use जोड़ी ही अकेला stall पैदा करती है। कुल cycles k+n−1+stalls=5+5−1+1=10k + n - 1 + \text{stalls} = 5 + 5 - 1 + 1 = 10 हैं, जबकि आदर्श हालत में 9 होते।

Compilers load-use stalls को scheduling से घटाते हैं, यानी load और उसके इस्तेमाल के बीच के slot में कोई स्वतंत्र instruction खिसका देते हैं। अगर ऐसी कोई instruction न हो, तो hardware ख़ुद bubble डाल देता है।

यह क्यों ज़रूरी है? Forwarding सस्ती है और ज़्यादातर data stalls हटा देती है, इसीलिए हर असली pipeline में यह होती है। GATE के सवाल बताते हैं कि forwarding है या नहीं और register file कैसे पढ़ी और लिखी जाती है, और यही दो lines जवाब तय करती हैं।

Stalls के साथ CPI, branch penalty और prediction

आदर्श pipeline का CPI (cycles per instruction) 1 होता है। Stalls इसमें जुड़ते हैं, इसलिए असरदार CPI, एक और हर instruction के औसत stall cycles का जोड़ है। Stages बराबर हों, तो हर instruction में kk cycles लेने वाली non-pipelined machine पर speed-up सीधे निकल आता है।

CPI=1+stall cycles per instruction,S=kCPI\mathit{CPI} = 1 + \text{stall cycles per instruction}, \qquad S = \frac{k}{\mathit{CPI}}

Branch taken है या नहीं, यह उसके resolve होने पर ही पता चलता है, जो अक्सर EX में होता है। इस बीच उसके बाद fetch हुई instructions, branch taken होने पर बेकार हो जाती हैं, और बर्बाद हुए cycles की संख्या branch penalty pp है। अगर instructions का ff हिस्सा branches हैं और pipeline हमेशा रुकती है, तो CPI=1+fp\mathit{CPI} = 1 + fp; 20 प्रतिशत branches और p=3p = 3 हो, तो CPI 1.6 हो जाता है।

Predict-not-taken अगली instructions fetch करता रहता है और penalty सिर्फ़ तब चुकाता है जब branch taken हो। Static तरीके एक तय नियम मानते हैं, जैसे पीछे जाने वाली branches को taken मानना, क्योंकि loops पीछे की ओर branch करते हैं। Dynamic तरीके इतिहास याद रखते हैं। 2-bit saturating counter लगातार दो ग़लत अनुमानों के बाद ही अपना अनुमान बदलता है, इसलिए loop से एक बार बाहर निकलने पर loop का अगला दौर नहीं बिगड़ता।

Delayed branch एक अलग रास्ता लेता है। Branch के ठीक बाद की instruction, जो delay slot में होती है, हमेशा चलती है। Compiler उसमें कोई काम की instruction रख देता है, ताकि कुछ भी बर्बाद न हो।

हल किया उदाहरण। Forwarding वाली five-stage pipeline में 30 प्रतिशत instructions loads हैं, और 40 प्रतिशत loads के ठीक बाद उस मान को इस्तेमाल करने वाली instruction आती है। साथ ही 20 प्रतिशत instructions branches हैं, जिनकी predict-not-taken में penalty p=2p = 2 है, और 60 प्रतिशत branches taken होती हैं।

CPI=1+0.3×0.4×1+0.2×0.6×2=1+0.12+0.24=1.36\mathit{CPI} = 1 + 0.3 \times 0.4 \times 1 + 0.2 \times 0.6 \times 2 = 1 + 0.12 + 0.24 = 1.36

उसी clock पर हर instruction में 5 cycles लेने वाली non-pipelined machine पर speed-up 51.36≈3.68\dfrac{5}{1.36} \approx 3.68 है।

यह क्यों ज़रूरी है? आम code में लगभग हर पाँच में से एक instruction branch होती है, इसलिए कई pipelines में सबसे बड़ा अकेला नुक़सान branch penalty का होता है। गहरी pipelines में pp बढ़ जाता है, इसीलिए clock तेज़ होने के साथ अच्छा prediction और ज़रूरी होता जाता है।

Descriptive lens: लिखित paper यह कैसे पूछते हैं

Engineering services, IT officer पदों और राज्य Assistant Programmer परीक्षाओं के लिखित paper इस topic को चार तरह से पूछते हैं:

  1. Space-time diagram से pipelining समझाना और speed-up का formula निकालना।
  2. Hazards को हर एक के उदाहरण के साथ वर्गों में बाँटना और बताना कि हर एक कैसे हटता है।
  3. Code के किसी टुकड़े का विश्लेषण करना, उसकी dependences पहचानना और forwarding के साथ और बिना stalls गिनना।
  4. Prediction और delayed branch जैसे branch सँभालने के तरीकों पर चर्चा करना।

अभ्यास सवाल (हमारे अपने, पिछले वर्षों के सवाल नहीं)।

  1. nn instructions के लिए kk-stage pipeline का speed-up निकालिए और दिखाइए कि यह कभी kk से ज़्यादा नहीं हो सकता। (10 अंक)
  2. Structural, data और control hazards को एक-एक उदाहरण के साथ समझाइए, और हर एक का hardware या software इलाज बताइए। (10 अंक)
  3. LOAD R1, 0(R2); ADD R3, R1, R4; SUB R5, R3, R6; STORE R5, 0(R7) के क्रम के लिए forwarding के साथ और बिना five-stage timing बनाइए और cycles गिनिए। (15 अंक)

सवाल 1 का answer plan।

  • Cycle time τ\tau वाली kk-stage pipeline को परिभाषित कीजिए और लिखिए कि कोई stall नहीं है और सभी stages बराबर हैं।
  • Space-time diagram से दिखाइए कि पहली instruction kk cycles के बाद पूरी होती है और बाद की हर instruction एक cycle जोड़ती है।
  • Pipelined समय को (k+n−1)τ(k + n - 1)\tau और non-pipelined समय को nkτnk\tau लिखिए।
  • दोनों का भाग करके S=nkk+n−1S = \dfrac{nk}{k + n - 1} निकालिए।
  • nn बढ़ने पर limit लेकर दिखाइए कि SS, kk के पास पहुँचता है पर उससे आगे नहीं जाता, क्योंकि भाजक k+n−1k + n - 1 हमेशा कम से कम nn होता है।
  • अंत में लिखिए कि असमान stages, latch delays और hazards असली speed-up को kk से नीचे रखते हैं।

Quick revision

  • kk-stage pipeline एक साथ kk instructions चलाती है, जिससे throughput बढ़ता है पर latency नहीं घटती।
  • पाँच stages हैं IF, ID, EX, MEM और WB, और इनके बीच latches होते हैं।
  • Pipelined समय (k+n−1)τ(k + n - 1)\tau है, और speed-up nkk+n−1\dfrac{nk}{k + n - 1}, kk की ओर जाता है।
  • Stages असमान हों, तो cycle time सबसे धीमे stage और latch delay का जोड़ है।
  • Hazards हैं structural, data और control, और सरल in-order pipeline में सिर्फ़ RAW data hazards से stall होता है।
  • Forwarding से ALU से ALU वाले stalls हट जाते हैं, लेकिन load-use जोड़ी को फिर भी एक stall चाहिए।
  • असरदार CPI, एक और हर instruction के stall cycles का जोड़ है, और branch इसमें fpfp जोड़ती है।
  • Predict-not-taken सिर्फ़ taken branches पर penalty चुकाता है, और 2-bit counter दो ग़लत अनुमानों के बाद बदलता है।

ख़ुद को परखें

इस chapter पर exam जैसे सवाल। जवाब चुनिए, फिर उसकी व्याख्या देखिए। 13 में से 0 हल किए

  1. Pipelining मुख्य रूप से क्या सुधारती है?

  2. Classic five-stage pipeline में registers के मान किस stage में पढ़े जाते हैं?

  3. 2 ns cycle वाली 5-stage pipeline बिना stall के 100 instructions चलाती है। कुल समय कितना है?

  4. Instructions की संख्या बहुत बड़ी होने पर kk-stage pipeline का speed-up किसके पास पहुँचता है?

  5. Stage delays 150, 120, 160, 140 और 130 ps हैं और हर latch 10 ps जोड़ता है। Pipeline का cycle time कितना है?

  6. ADD R1, R2, R3 के बाद SUB R4, R1, R5 से कौन-सी dependence बनती है?

  7. एक ही cycle में दो instructions को memory का एक ही port चाहिए, तो कौन-सा hazard होता है?

  8. पूरी forwarding के साथ, LOAD के ठीक बाद load हुए मान को इस्तेमाल करने वाली instruction को कितने stalls चाहिए?

  9. Forwarding न हो, और registers cycle के पहले आधे में लिखे और दूसरे आधे में पढ़े जाएँ, तो ठीक पिछली instruction का result इस्तेमाल करने वाली instruction को कितने stalls चाहिए?

  10. 30% instructions loads हैं, उनमें से 40% एक cycle का load-use stall देती हैं, और कोई दूसरा stall नहीं है। CPI कितना है?

  11. एक संतुलित 5-stage pipeline का असरदार CPI 1.25 है। उसी clock पर non-pipelined machine पर इसका speed-up कितना है?

  12. 20% instructions branches हैं, penalty 3 cycles है, और pipeline हर branch पर रुकती है। CPI कितना है?

  13. 2-bit saturating branch predictor अपना prediction कब बदलता है?

पिछले साल के सवाल (PYQ)

UPSC CSE के पिछले साल के सवाल यहाँ तभी दिखेंगे, जब हर सवाल को official question paper से शब्दशः मिला लिया जाएगा।

AI के साथ सीखें

ऐसा tutor जो बताने की जगह पूछता है, ऐसा साथी जो दूसरा पक्ष रखता है, और यह chapter आपके अपने ज़िले के उदाहरणों से।

Tutor को बताइए कहाँ अटके हैं। वह सवालों से रास्ता दिखाएगा और कुछ कोशिशों के बाद जवाब देगा।

🙋 Doubt पूछें या feedback दें

कहीं अटके, या कोई गलती दिखी? ExamHike team को लिखिए। आम तौर पर एक दिन में जवाब आता है, और जवाब आपके My progress page पर दिखेगा।

Doubt पूछने या feedback देने के लिए login करें →

अक्सर पूछे जाने वाले सवाल

k-stage pipeline का speed-up k से ज़्यादा क्यों नहीं हो सकता?

क्योंकि speed-up nk/(k + n − 1), n बढ़ने पर सिर्फ़ k के पास पहुँचता है, और stalls, latches और असमान stages इसे और नीचे रखते हैं।

असमान stages में pipeline का cycle time कैसे निकलता है?

सबसे धीमे stage की delay लीजिए और उसमें latch delay जोड़िए।

Load-use hazard क्या है?

यह तब होता है जब कोई instruction ठीक पिछली instruction का load किया मान इस्तेमाल करे; forwarding के साथ भी इसमें एक stall cycle लगता है।

Branches, CPI पर कैसे असर डालती हैं?

CPI, 1 और branch fraction गुणा penalty का जोड़ बन जाता है, और predict-not-taken में सिर्फ़ taken branches गिनी जाती हैं।

इस module के और chapter: Computer organisation और architecture

Official स्रोत

इस chapter के तथ्य इन official स्रोतों से मिलाकर जाँचे गए हैं।

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Computer Organization and Architecture (IIT Madras, 106106166): pipelining and hazards