zoukankan      html  css  js  c++  java
  • Lab 4: Cache Geometries

    这次的lab特别简单,直接贴代码了

    /*
    Coursera HW/SW Interface
    Lab 4 - Mystery Caches
    
    Mystery Cache Geometries (for you to keep notes):
    mystery0:
        block size =
        cache size =
        associativity =
    mystery1:
        block size =
        cache size =
        associativity =
    mystery2:
        block size =
        cache size =
        associativity =
    mystery3:
        block size =
        cache size =
        associativity =
    */
    
    #include <stdlib.h>
    #include <stdio.h>
    
    #include "mystery-cache.h"
    
    /*
     * NOTE: When using access_cache() you do not need to provide a "real"
     * memory addresses. You can use any convenient integer value as a
     * memory address, you should not be able to cause a segmentation
     * fault by providing a memory address out of your programs address
     * space as the argument to access_cache.
     */
    
    /*
       Returns the size (in B) of each block in the cache.
    */
    int get_block_size(void) {
      /* YOUR CODE GOES HERE */
      cache_init(16000, 4);
      flush_cache();
      int back = 0x0000ff00;
      access_cache(back);
      while(access_cache(back)) {
        back+= 1;
      }
    
      flush_cache();
      int front = 0x0000ff00;
      access_cache(front);
      while(access_cache(front)) {
        front-= 1;
      }
    
      return back-front-1;
      
    }
    
    /*
       Returns the size (in B) of the cache.
    */
    int get_cache_size(int size) {
      /* YOUR CODE GOES HERE */
      // printf("%d
    ", s);
      flush_cache();
      int start = 0x00FFff00;
      int i = size / 2;
      int p;
    
      do {
        i *= 2;
        p = start;
        flush_cache();
        access_cache(start);
        while(p < start + i) {
          p += size;
          access_cache(p);
        }
        if (!access_cache(start))
          break;
      } while(1);
    
      return i;
    }
    
    /*
       Returns the associativity of the cache.
    */
    int get_cache_assoc(int size) {
      /* YOUR CODE GOES HERE */
    
      return get_cache_size(size) / size; 
    }
    
    //// DO NOT CHANGE ANYTHING BELOW THIS POINT
    int main(void) {
      int size;
      int assoc;
      int block_size;
    
      /* The cache needs to be initialized, but the parameters will be
         ignored by the mystery caches, as they are hard coded.  You can
         test your geometry paramter discovery routines by calling
         cache_init() w/ your own size and block size values. */
      cache_init(0,0);
    
      block_size=get_block_size();
      size=get_cache_size(block_size);
      assoc=get_cache_assoc(size);
    
      printf("Cache block size: %d bytes
    ", block_size);
      printf("Cache size: %d bytes
    ", size);
      printf("Cache associativity: %d
    ", assoc);
    
      return EXIT_SUCCESS;
    }

     get_block_size 中代码的意思是找到下一行的起始点,上一行的终结点,然后两者相减再减1就是block size

    get_cache_size 中代码的意思是不断访问一行行cache,一旦访问到某一行时最初那行变为未访问状态,就表明这行block被替换了,说明已经经过了cache size。

    get_cache_assoc 与 get_cache_size 相同,不过每次加的都是 cache size,这样每次访问都只会在一组内访问,一旦最初那组出现未访问,就说明被替代了。这样就能得出一组的行数。

    2015-09-28

  • 相关阅读:
    C语言|博客作业02
    少走弯路的十条忠告
    怎么算是优秀的程序员写给工作2,3年了的同行
    .NET世界的M型化原文作者奚江华
    工作以后十不要 减少奋斗30年
    <转>[创业经验]程序员创业:我的软件推广成功之路
    一个程序员的C#命名规则<转>
    推荐奚江华著《圣殿祭祀ASP.NET 3.5 专家技术手册 C#篇及他的TW博客进入方法》
    C#算法
    使用 DataFormatString 属性来提供列中各项的自定义格式
  • 原文地址:https://www.cnblogs.com/whuyt/p/4843795.html
Copyright © 2011-2022 走看看