免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4036 | 回复: 6
打印 上一主题 下一主题

cpu的两个问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-25 20:14 |只看该作者 |倒序浏览
cpu在cache中取指或取数时,若未命中,就会去读内存。请问读内存时,是只读需要的数据呢,还是和读磁盘一样也要进行预读?另外,586以上的cpu,其cache是不是采用哈佛结构,有代码和数据两个cache。谢谢

论坛徽章:
0
2 [报告]
发表于 2007-01-26 08:31 |只看该作者
我记得cache的读取是按块读的,或者叫做cacheline吧。
ia32似乎是指令/数据cache分开的,不过不知道是不是叫做哈佛结构。

论坛徽章:
0
3 [报告]
发表于 2007-01-26 10:00 |只看该作者
谢谢mingyanguo 兄。

[ 本帖最后由 gta 于 2007-1-26 10:07 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2007-01-26 10:07 |只看该作者
原帖由 gta 于 2007-1-26 10:00 发表于 3楼  
谢谢mingyanguo 兄。再问一下,pentium的cache line是多大呢

不客气。
这个大小我记不住,找找手册看看吧。
手头资料不方便。

论坛徽章:
0
5 [报告]
发表于 2007-01-27 11:26 |只看该作者
AMD64 的 cache 是这样的:

Cache 的结构就像一个矩阵。
行为 set , 列为 way
一个 4 way 的 cache 组织中,一个 set 有 4 个 cache line 组成
每个 cache line 由 3 个部分组成: tag 域、data 域 和 other information 域
每个 cache line 为 64 bytes。

未命名.GIF (17.76 KB, 下载次数: 45)

未命名.GIF

论坛徽章:
0
6 [报告]
发表于 2007-01-27 11:33 |只看该作者
虚拟地址经过 MMU 处理后的物理地址,为分为三个部分。

index 域:得出 cache 的 set 值,如上图所求,从 index 得出 set 为 2
tag 域:物理地址的 tag 分别与 set 中的每个 way 的 cache line 的 tag 进行比较,直到匹配(hit)。在每个 way 进行搜索是通过一个 n:1 的乘法器得出每个 way 的地点。
offset 域:当 hit 时,通过 offset 域索引出 cache line 中 data 域的具体数据。

论坛徽章:
0
7 [报告]
发表于 2007-01-27 12:10 |只看该作者
Intel 提供了4条 cache 预读指令:
prefetchnta、prefetcht0、prefetcht1 以及 prefetch2

AMD 增加了两条指令:
prefetch 和 prefetchw,这两条是 AMD 自已的 3D NOW 指令。

下面是示例代码:

c code:
  1. #define num 65536
  2. #define ARY_SIZE (num * 8)

  3. double array_a[num]
  4. double array_b[num]
  5. double array_c[num]

  6. int i;
  7. for ( i = 0; i < num; i++) {
  8.        array_a[i] = array_b[i] * array_c[i];
  9. }
复制代码



汇编码:
  1.      mov edx, (-num)                        ; Use biased index.
  2.        mov eax, OFFSET array_a          ; Get address of array_a.
  3.        mov ebx, OFFSET array_b          ; Get address of array_b.
  4.        mov ecx, OFFSET array_c           ; Get address of array_c.
  5. loop:
  6.        prefetchw [eax+256]                 ; Four cache lines ahead                  
  7.        prefetch [ebx+256]                    ; Four cache lines ahead
  8.        prefetch [ecx+256]                     ; Four cache lines ahead

  9.        fld QWORD PTR [ebx+edx*8+ARR_SIZE]                      ; b[i]
  10.        fmul QWORD PTR [ecx+edx*8+ARR_SIZE]                    ; b[i] * c[i]
  11.        fstp QWORD PTR [eax+edx*8+ARR_SIZE]                    ; a[i] = b[i] * c[i]
  12.        fld QWORD PTR [ebx+edx*8+ARR_SIZE+8]                  ; b[i+1]
  13.        fmul QWORD PTR [ecx+edx*8+ARR_SIZE+8]                ; b[i+1] * c[i+1]
  14.        fstp QWORD PTR [eax+edx*8+ARR_SIZE+8]                ; a[i+1] = b[i+1] * c[i+1]
  15.        fld QWORD PTR [ebx+edx*8+ARR_SIZE+16]                ; b[i+2]
  16.        fmul QWORD PTR [ecx+edx*8+ARR_SIZE+16]              ; b[i+2]*c[i+2]
  17.        fstp QWORD PTR [eax+edx*8+ARR_SIZE+16]              ; a[i+2] = [i+2] * c[i+2]
  18.        fld QWORD PTR [ebx+edx*8+ARR_SIZE+24]                ; b[i+3]
  19.        fmul QWORD PTR [ecx+edx*8+ARR_SIZE+24]              ; b[i+3] * c[i+3]
  20.        fstp QWORD PTR [eax+edx*8+ARR_SIZE+24]              ; a[i+3] = b[i+3] * c[i+3]
  21.        fld QWORD PTR [ebx+edx*8+ARR_SIZE+32]                ; b[i+4]
  22.        fmul QWORD PTR [ecx+edx*8+ARR_SIZE+32]              ; b[i+4] * c[i+4]
  23.        fstp QWORD PTR [eax+edx*8+ARR_SIZE+32]              ; a[i+4] = b[i+4] * c[i+4]
  24.        fld QWORD PTR [ebx+edx*8+ARR_SIZE+40]                ; b[i+5]
  25.        fmul QWORD PTR [ecx+edx*8+ARR_SIZE+40]              ; b[i+5] * c[i+5]
  26.        fstp QWORD PTR [eax+edx*8+ARR_SIZE+40]              ; a[i+5] = b[i+5] * c[i+5]
  27.        fld QWORD PTR [ebx+edx*8+ARR_SIZE+48]                ; b[i+6]
  28.        fmul QWORD PTR [ecx+edx*8+ARR_SIZE+48]              ; b[i+6] * c[i+6]
  29.        fstp QWORD PTR [eax+edx*8+ARR_SIZE+48]              ; a[i+6] = b[i+6] * c[i+6]
  30.        fld QWORD PTR [ebx+edx*8+ARR_SIZE+56]                 ; b[i+7]
  31.        fmul QWORD PTR [ecx+edx*8+ARR_SIZE+56]               ; b[i+7] * c[i+7]
  32.        fstp QWORD PTR [eax+edx*8+ARR_SIZE+56]               ; a[i+7] = b[i+7] * c[i+7]

  33.        add edx, 8                                                                      ; Compute next 8 products
  34.        jnz loop                                                                          ; until none left.
复制代码



代码中将数据装载进 cache 的 4 个 set  
4 way 结构,每个 cache line 为 64 bytes,   4 * 64 byte 共 256 bytes。

[ 本帖最后由 mik 于 2007-1-27 12:11 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP