简体   繁体   English

我们如何计算这段代码片段中缓存的读取/未命中次数?

[英]how do we calculate the number of reads/misses of the cache in this code snippet?

Given this code snippet from this textbook that I am currently studying.鉴于我目前正在学习的这本教科书中的这段代码片段。 Randal E. Bryant, David R. O'Hallaron - Computer Systems. Randal E. Bryant, David R. O'Hallaron - 计算机系统。 A Programmer's Perspective [3rd ed.] (2016, Pearson) (global edition, so the book's exercises could be wrong.) A Programmer's Perspective [3rd ed.] (2016, Pearson) (全球版,所以书中的练习可能是错误的。)

for (i = 31; i >= 0; i--) {
    for (j = 31; j >= 0; j--) {
        total_x += grid[i][j].x;
    }
}

for (i = 31; i >= 0; i--) {
    for (j = 31; j >= 0; j--) {
         total_y += grid[i][j].y;
    }
}

and this is the information given这是给出的信息

The heart of the recent hit game SimAquarium is a tight loop that calculates the average position of 512 algae.最近热门游戏 SimAquarium 的核心是一个计算 512 藻类平均位置的紧密循环。 You are evaluating its cache performance on a machine with a 2,048-byte direct-mapped data cache with 32-byte blocks (B = 32).您正在一台具有 2,048 字节直接映射数据缓存和 32 字节块 (B = 32) 的机器上评估其缓存性能。

 struct algae_position { int x; int y; }; struct algae_position grid[32][32]; int total_x = 0, total_y = 0; int i, j;

You should also assume the following:您还应该假设以下几点:

  • sizeof(int) = 4.大小(整数)= 4。
  • grid begins at memory address 0.网格从内存地址 0 开始。
  • The cache is initially empty.缓存最初是空的。
  • The only memory accesses are to the entries of the array grid.唯一的内存访问是对数组网格的条目。
  • Variables i, j, total_x, and total_y are stored in registers变量 i、j、total_x 和 total_y 存储在寄存器中

The book gives the following questions as practice:本书给出了以下问题作为练习:

A. What is the total number of reads?    
Answer given : 2048  
B. What is the total number of reads that miss in the cache?
Answer given : 1024    
C. What is the miss rate?  
Answer given: 50%
  1. I'm guessing for A, the answer is derived from 32*32 *2 ?我猜 A,答案是从32*32 *2导出的? 32*32 for the dimensions of the matrix and 2 because there are 2 separate loops for x and y vals.矩阵的维度为32*32 ,因为 x 和 y val 有 2 个单独的循环,所以为 2。 Is this correct?这样对吗? How should the total number of reads be counted?应该如何计算reads的总数?

  2. How do we calculate the total number of misses that happen in the cache and the miss rate?我们如何计算缓存中发生的未命中总数和未命中率? I read that the miss rate is (1- hit-rate)我读到未命中率为(1-命中率)

Question A问题 A

You are correct about 32 x 32 x 2 reads.您对 32 x 32 x 2 读取是正确的。

Question B问题 B

The loops counts down from 31 towards 0 but that doesn't matter for this question.循环从 31 倒数到 0,但这对这个问题无关紧要。 The answer is the same for loops going from 0 to 31. Since that is a bit easier to explain I'll assume increasing loop counters.对于从 0 到 31 的循环,答案是相同的。由于这更容易解释,我将假设增加循环计数器。

When you read grid[0][0] , you'll get a cache miss.当您读取grid[0][0] ,您会遇到缓存未命中。 This will bring grid[0][0] , grid[0][1] , grid[0][2] and grid[0][3] into the cache.这会将grid[0][0]grid[0][1]grid[0][2]grid[0][3]带入缓存。 This is because each element is 2x4 = 8 bytes and the block size is 32. In other words: 32 / 8 = 4 grid elements in one block.这是因为每个元素是 2x4 = 8 字节,块大小是 32。换句话说:32 / 8 = 4 个网格元素在一个块中。

So the next cache miss is for grid[0][4] which again will bring the next 4 grid elements into the cache.因此,下一次缓存未命中是针对grid[0][4] ,这将再次将接下来的 4 个网格元素带入缓存。 And so on... like:等等......比如:

miss
hit
hit
hit
miss
hit
hit
hit
miss
hit
hit
hit
...

So in the first loop you simply have:因此,在第一个循环中,您只需:

"Number of grid elements" divided by 4.

or或者

32 * 32 / 4 = 256

In general in the first loop:通常在第一个循环中:

Misses = NumberOfElements / (BlockSize / ElementSize)

so here:所以在这里:

Misses = 32*32 / (32 / 8) = 256

Since the cache size is only 2048 and the whole grid is 32 x 32 x 8 = 8192, nothing read into the cache in the first loop will generate cache hit in the second loop.由于缓存大小仅为 2048,整个网格为 32 x 32 x 8 = 8192,因此在第一个循环中读入缓存的任何内容都不会在第二个循环中产生缓存命中。 In other words - both loops will have 256 misses.换句话说 - 两个循环都会有 256 次未命中。

So the total number of cache misses are 2 x 256 = 512.所以缓存未命中的总数是 2 x 256 = 512。

Also notice that there seem to be a bug in the book.还要注意,书中似乎有一个错误。

Here:这里:

The heart of the recent hit game SimAquarium is a tight loop that calculates the
average position of 512 algae.
                    ^^^
                    Hmmm... 512 elements...

Here:这里:

for (i = 31; i >= 0; i--) {
    for (j = 31; j >= 0; j--) {
         ^^^^^^
         hmmm... 32 x 32 is 1024

So the loop access 1024 elements but the text says 512. So something is wrong in the book.所以循环访问了 1024 个元素,但文本显示为 512。所以这本书有问题。

Question C问题 C

Miss rate = 512 misses / 2048 reads = 25 %

note:笔记:

Being very strict we cannot say for sure that the element size is two times the integer size.非常严格,我们不能肯定地说元素大小是整数大小的两倍。 The C standard allow that structs contain padding. C 标准允许结构包含填充。 So in principle there could be 8 bytes padding in the struct (ie element size being 16) and that would give the results that the book says.所以原则上结构中可能有 8 个字节的填充(即元素大小为 16),这将给出书中所说的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM