Write Combining

原文链接:http://mechanical-sympathy.blogspot.com/2011/07/write-combining.html(有墙移动到墙内)

Modern CPUs employ lots of techniques to counteract the latency cost of going to main memory. These days CPUs can process hundreds of instructions in the time it takes to read or write data to the DRAM memory banks.

The major tool used to hide this latency is multiple layers of SRAM cache. In addition, SMP systems employ message passing protocols to achieve coherence between caches. Unfortunately CPUs are now so fast that even these caches cannot keep up at times. So to further hide this latency a number of less well known buffers are used.

This article explores “write combining store buffers” and how we can write code that uses them effectively.

CPU caches are effectively unchained hash maps where each bucket is typically 64-bytes. This is known as a “cache line”. The cache line is the effective unit of memory transfer. For example, an address A in main memory would hash to map to a given cache line C.

If a CPU needs to work with an address which hashes to a line that is not already in cache, then the existing line that matches that hash needs to be evicted so the new line can take its place. For example if we have two addresses which both map via the hashing algorithm to the same cache line, then the old one must make way for the new cache line.

When a CPU executes a store operation it will try to write the data to the L1 cache nearest to the CPU. If a cache miss occurs at this stage the CPU goes out to the next layer of cache. At this point on an Intel, and many other, CPUs a technique known as “write combining” comes into play.

While the request for ownership of the L2 cache line is outstanding the data to be stored is written to one of a number of cache line sized store buffers on the processor itself. These on chip buffers allow the CPU to continue processing instructions while the cache sub-system gets ready to receive and process the data. The biggest advantage comes when the data is not present in any of the other cache layers.

These buffers become very interesting when subsequent writes happen to require the same cache line. The subsequent writes can be combined into the buffer before it is committed to the L2 cache. These 64-byte buffers maintain a 64-bit field which has the corresponding bit set for each byte that is updated to indicate what data is valid when the buffer is transferred to the outer caches.

Hang on I hear you say. What happens if the program wants to read some of the data that has been written to a buffer? Well our hardware friends have thought of that and they will snoop the buffers before they read the caches.

What does all this mean for our programs?

If we can fill these buffers before they are transferred to the outer caches then we will greatly improve the effective use of the transfer bus at every level. How do we do this? Well programs spend most of their time in loops doing work.

There are a limited number of these buffers, and they differ by CPU model. For example on an Intel CPU you are only guaranteed to get 4 of them at one time. What this means is that within a loop you should not write to more than 4 distinct memory locations at one time or you will not benefit from the write combining effect.

What does this look like in code?


01 import static java.lang.System.out;
02 
03 public final class WriteCombining
04{
05     private static final int ITERATIONS = Integer.MAX_VALUE;
06     private static final int ITEMS = 1 << 24;
07     private static final int MASK = ITEMS - 1;
08 
09     private static final byte[] arrayA = new byte[ITEMS];
10     private static final byte[] arrayB = new byte[ITEMS];
11     private static final byte[] arrayC = new byte[ITEMS];
12     private static final byte[] arrayD = new byte[ITEMS];
13     private static final byte[] arrayE = new byte[ITEMS];
14     private static final byte[] arrayF = new byte[ITEMS];
15 
16     public static void main(final String[] args)
17     {
18         for (int i = 1; i <= 3; i++)
19         {
20             out.println(i + " SingleLoop duration (ns) = " + runCaseOne());
21             out.println(i + " SplitLoop  duration (ns) = " + runCaseTwo());
22         }
23 
24         int result = arrayA[1] + arrayB[2] + arrayC[3] +
25                      arrayD[4] + arrayE[5] + arrayF[6];
26         out.println("result = " + result);
27     }
28 
29     public static long runCaseOne()
30     {
31         long start = System.nanoTime();
32 
33         int i = ITERATIONS;
34         while (--i != 0)
35         {
36             int slot = i & MASK;
37             byte b = (byte)i;
38             arrayA[slot] = b;
39             arrayB[slot] = b;
40             arrayC[slot] = b;
41             arrayD[slot] = b;
42             arrayE[slot] = b;
43             arrayF[slot] = b;
44         }
45 
46         return System.nanoTime() - start;
47     }
48 
49     public static long runCaseTwo()
50     {
51         long start = System.nanoTime();
52 
53         int i = ITERATIONS;
54         while (--i != 0)
55         {
56             int slot = i & MASK;
57             byte b = (byte)i;
58             arrayA[slot] = b;
59             arrayB[slot] = b;
60             arrayC[slot] = b;
61         }
62 
63         i = ITERATIONS;
64         while (--i != 0)
65         {
66             int slot = i & MASK;
67             byte b = (byte)i;
68             arrayD[slot] = b;
69             arrayE[slot] = b;
70             arrayF[slot] = b;
71         }
72 
73         return System.nanoTime() - start;
74     }
75}

This program on my Windows 7 64-bit Intel Core i7 860 @ 2.8 GHz system produces the following output:


11 SingleLoop duration (ns) = 14019753545
21 SplitLoop  duration (ns) = 8972368661
32 SingleLoop duration (ns) = 14162455066
42 SplitLoop  duration (ns) = 8887610558
53 SingleLoop duration (ns) = 13800914725
63 SplitLoop  duration (ns) = 7271752889

To spell it out, if we write to 6 array locations (memory addresses) inside one loop we see that the program takes significantly longer than if we split the work up, and write first to 3 array locations, then to the other 3 locations sequentially.

By splitting the loop we do much more work yet the program completes in much less time! Welcome to the magic of “write combining”. By using our knowledge of the CPU architecture to fill those buffers properly we can use the underlying hardware to accelerate our code by a factor of two.

Don’t forget that with hyper-threading you can have 2 threads in competition for these buffers on the same core. 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值