how processor caches work

注:  这篇文章是对Igor Ostrovsky博客gallery-of-processor-cache-effects的总结学习。

    原文介绍了高速缓存在程序中的工作方式,尤其是cache line的介绍醍醐灌顶。文中还举了很多通过例子来解释处理器是如何读取cache的,也让人印象深刻。

    这篇文章总结是用英语写的,一来是希望借此锻炼一下我的英语实用能力,第二则是原文中很多词汇,我不知如何恰当的翻译成汉语,直接实用英语读起来更通顺。

 

 

this is a summery of reading the blog of Igor Ostrovsky.

 

 

example1:        cache line

int[] arr = new int[64 * 1024 * 1024];

// Loop 1
for (int i = 0; i < arr.Length; i++) arr[i] *= 3;

// Loop 2
for (int i = 0; i < arr.Length; i += 16) arr[i] *= 3;

  the two loops cost 80 and 78ms respectively on the same machine. 

  the running time of this loops is dominated by the memory accesses to array, not the integer multiplications.

for (int i = 0; i < arr.Length; i += K) arr[i] *= 3;

    the running times of tloop for different step values(K) is:

    Today's CPUs donot access memory byte by byte. Instead, they fetch memory in chunks of 64 bytes, which is called cache lines.

    So proper alignment of the data is important for reducing the touches of the cache lines.

 

 example2. cache size

    L1 caches are per-core

    L2 caches are shared between pairs of cores.

    In this test environment, we use the core with a 32KB data cache, a32KB instruction cache and a 4MB L2 data cache.

int steps = 64 * 1024 * 1024; // Arbitrary number of steps
int lengthMod = arr.Length - 1;
for (int i = 0; i < steps; i++)
{
    arr[(i * 16) & lengthMod]++; // (x & lengthMod) is equal to (x % arr.Length) 
}    

 

the process time changs with array size:

   

    example3:

int steps = 256 * 1024 * 1024;
int[] a = new int[2];

// Loop 1
for (int i=0; i<steps; i++) { a[0]++; a[0]++; }

// Loop 2
for (int i=0; i<steps; i++) { a[0]++; a[1]++; }

 

    loop2 is twice faster than loop1

    which can be explained in the perspective of pipe line, a write-read hazard occurs in loop1.

    example4:

public static long UpdateEveryKthByte(byte[] arr, int K)
{
    Stopwatch sw = Stopwatch.StartNew();
    const int rep = 1024*1024; // Number of iterations – arbitrary

int p = 0;
    for (int i = 0; i < rep; i++)
    {
        arr[p]++;
        p += K;
        if (p >= arr.Length) p = 0;
    }

    sw.Stop();
    return sw.ElapsedMilliseconds;
}

 

    generally speaking, 16-way set associative cache are adopted in L2 cache.

that is to say, each cache line can be stored in any one of 16 particular slots in the cache.

4MB = 16K * 64Bytes

16Bytes for each cache line and 16 slots for each set and 4K set.

example5:

    false cache line sharing 

    multi-cores machine, caches must be used safely. That means when one processor modifies a value in its cache, other processors cannot operate the same vaule anymore. Sine cache are worked in cache lines, the entire cache line will be invalidated in all caches.

 

转载于:https://www.cnblogs.com/leohan2013/p/3310733.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值