1, background info

5 stages in CPU pipeline: IF, ID, EX, MM, WB

IF – Instruction Fetch

ID – Instruction Decode

EX – Execute

MM – Memory

WB – Write Back

2, what is data hazard and how does it happen

Data hazards occur when instructions that exhibit data dependency modify data in different stages of a pipeline.

For example, we write a register and then read it. The write and read operations are dependent; they are related by the same register.

In a pipeline, if read happens before the write can finish, it’s very likely that it would not read back a correct value.

In this case, data hazard happens.

It’s not difficult to imagine that any 2 operations involving write may cause data hazard.

We can categorize data hazard into 3 kinds:

(1) read after write (RAW);

(2) write after write (WAW);

(3) write after read (WAR);

3, how to prevent data hazard

(1) pipeline bubbling

That is, stall the pipeline until hazard is resolved.

	add r1, r2, r3
add r4, r1, r5 Cycles----->
________________________ Instructions
|_IF_|_ID_|_EX_|_MM_|_WB_|______________ add r1, r2, r3
|_IF_|_x_x_x_x_|_ID_|_EX_|_MM_|_WB_| add r4, r1, r5
stall cycles

Usually, NOP operationg is inserted during stall time.

As instructions are fetched, control logic determines whether a hazard could/will occur. If this is true, then the control logic insert NOPs into the pipeline.

If the number of NOPs equals the number of stages in the pipeline, the processor has been cleared of all instructions and can proceed free from hazards.

(2) out-of-order execution

This would be introduced heavily later.

(3) operand forwarding/bypass

See below examle.

Instruction 0: Register 1 = 6Instruction 1: Register 1 = 3Instruction 2: Register 2 = Register 1 + 7 = 10

Instruction 2 would need to use Register 1. If Instruction 2 is executed before Instruction 1 is finished, it may get a wrong result.

However, we can see that:

a) the output of Instruction 1 (which is 3) can be used by subsequent instructions before the value 3 is committed to/stored in Register 1.

b) there is no wait to commit/store the output of Instruction 1 in Register 1 (in this example, the output is 3) before making that output available to the subsequent instruction (in this case, Instruction 2).

So it can be done that:

Instruction 2 uses the correct (the more recent) value of Register 1: the commit/store was made immediately and not pipelined.

This is forwarding/bypass.

4, how does forwarding/bypss work

With forwarding enabled, the Instruction Decode/Execution (ID/EX) stage of the pipeline now has two inputs: the value read from the register specified (in this example, the value 6 from Register 1), and the new value of Register 1 (in this example, this value is 3) which is sent from the next stage Instruction Execute/Memory Access(EX/MM). Added control logic is used to determine which input to use.

A forwarding vs. no forwarding example as followed:

ADD A B C  #A=B+C
SUB D C A #D=C-A

data hazard in CPU pipeline的更多相关文章

  1. [Machine Learning with Python] Data Preparation through Transformation Pipeline

    In the former article "Data Preparation by Pandas and Scikit-Learn", we discussed about a ...

  2. antidependence and data hazard

    See below example. ADDD  F6, F0, F8 SUBD   F8, F10, F14 Some article would say that “ There’s an ant ...

  3. SSIS Data Flow 的 Execution Tree 和 Data Pipeline

    一,Execution Tree 执行树是数据流组件(转换和适配器)基于同步关系所建立的逻辑分组,每一个分组都是一个执行树的开始和结束,也可以将执行树理解为一个缓冲区的开始和结束,即缓冲区的整个生命周 ...

  4. verilog实现16位五级流水线的CPU带Hazard冲突处理

    verilog实现16位五级流水线的CPU带Hazard冲突处理 该文是基于博主之前一篇博客http://www.cnblogs.com/wsine/p/4292869.html所增加的Hazard处 ...

  5. [DE] Pipeline for Data Engineering

    How to build an ML pipeline for Data Science 垃圾信息分类 Ref:Develop a NLP Model in Python & Deploy I ...

  6. 以Excel 作为Data Source,将data导入db

    将Excel作为数据源,将数据导入db,是SSIS的一个简单的应用,下图是示例Excel,数据列是code和name 第一部分,Excel中的数据类型是数值类型 1,使用SSDT创建一个package ...

  7. One EEG preprocessing pipeline - EEG-fMRI paradigm

    The preprocessing pipeline of EEG data from EEG-fMRI paradigm differs from that of regular EEG data, ...

  8. Java底层实现 - CPU术语

    1.内存屏障(memory barriers)是一组处理器指令,用于实现对内存操作的顺序限制 2.缓冲行(cache line)CPU高速缓存中可以分配的最小存储单位.处理器填写缓存行时 会加载整个缓 ...

  9. CPU和GPU性能对比

    计算20000次10000点的fft,分别使用CPU和GPU,得 the running time of cpu is : 2.3696s the running time of gpu is : 0 ...

随机推荐

  1. 5天玩转C#并行和多线程编程 —— 第一天 认识Parallel 转载 https://www.cnblogs.com/yunfeifei/p/3993401.html

    5天玩转C#并行和多线程编程系列文章目录 5天玩转C#并行和多线程编程 —— 第一天 认识Parallel 5天玩转C#并行和多线程编程 —— 第二天 并行集合和PLinq 5天玩转C#并行和多线程编 ...

  2. sql基础学习

    学习参考网站:http://www.runoob.com/sql/sql-tutorial.html 一.SQL命令 1.SELECT 语句 用于从数据库中选取数据. select column_na ...

  3. Android Telephony分析(五) ---- TelephonyRegistry详解

    本文紧接着上一篇文章<Android Telephony分析(四) —- TelephonyManager详解 >的1.4小节.从TelephonyRegistry的大部分方法中: 可以看 ...

  4. 03、request 模块高级

    requests高级部分 代理 cookie 验证码的识别 模拟登陆 代理 代理概念:代理服务器. 作用:接受请求==>请求转发. 代理和爬虫之间的关联: 可以使用请求转发的机制使得目的服务器接 ...

  5. NEO4J亿级数据全文索引构建优化

    NEO4J亿级数据全文索引构建优化 一.数据量规模(亿级) 二.构建索引的方式 三.构建索引发生的异常 四.全文索引代码优化 1.Java.lang.OutOfMemoryError 2.访问数据库时 ...

  6. C#下面的次幂表达

    嗯,一个错误.不能用x^y表达,要用math.pow(x,y).

  7. vue echart例——柱状图及高度自适应

    1.安装 npm install echarts -s 2.例——点击tab切换时柱状图重绘,高度根据返回数据设置. <template> <div> <ul id=&q ...

  8. c++隐藏运行exe黑窗口

    加入main方法隐藏窗口 HWND hwndDOS = GetForegroundWindow(); ShowWindow(hwndDOS, SW_HIDE);

  9. 【学术篇】SDOI2010 古代猪文

    这里可能包含传送门 又双叒叕数论大杂烩... 定理什么我都不会证 题目很长很啰嗦 但是题意很显然... 化完式子之后就是这么个东东:\(G^{\sum_{k|n}C_k^{\frac{n}{k}}}\ ...

  10. PHP算法之字符串转换整数 (atoi)

    请你来实现一个 atoi 函数,使其能将字符串转换成整数. 首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止. 当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之 ...