首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关联Set Cache低估了命中率

关联Set Cache低估了命中率
EN

Stack Overflow用户
提问于 2015-12-03 06:14:09
回答 1查看 206关注 0票数 1

我正在尝试实现一个使用最近最少使用的替换技术的组关联缓存。到目前为止,我的代码低估了缓存命中的数量,我不确定原因。下面是我的函数setAssoc,它接受一个表示缓存关联性的int值,以及一个由一系列数据访问组成的向量对。

该函数使用两个2D数组,一个用于存储缓存块,另一个用于存储缓存中每个块的“年龄”。

对于这个特定的实现,不用担心标记位或任何类似性质的东西;只需使用地址除以块大小就足以确定块数量,然后使用以集合数量为模的块数量来确定集合数量就足够了。

任何关于我为什么不能准确预测正确的缓存命中数量的见解都是值得感谢的!

代码语言:javascript
复制
int setAssoc(int associativity, vector<pair<unsigned long long, int>>& memAccess){

  int blockNum, setNum;
  int hitRate = 0;
  int numOfSets = 16384 / (associativity * 32);

  int cache [numOfSets][associativity];//used to store blocks
  int age [numOfSets][associativity];//used to store ages
  int maxAge = 0;
  int hit;//use this to signal a hit in the cache

  //set up cache here
  for(int i = 0; i < numOfSets; i++){

    for(int j = 0; j < associativity; j++){

      cache[i][j] = -1;//initialize all blocks to -1
      age[i][j] = 0;//initialize all ages to 0


    }//end for int j

  }//end for int i




  for(int i = 0; i < memAccess.size(); i++){
    blockNum = int ((memAccess[i].first) / 32);
    setNum = blockNum % numOfSets;

    hit = 0;

    for(int j = 0; j < associativity; j++){
        age[setNum][j]++;//age each entry in the cache
        if(cache[setNum][j] == blockNum){
            hitRate++;//increment hitRate if block is in cache
            age[setNum][j] = 0;//reset age of block since it was just accessed
            hit = 1;
        }//end if
    }//end for int j

    if(!hit){
        for(int j = 0; j < associativity; j++){
            //loop to find the least recently used block
            if(age[setNum][j] > maxAge){
                maxAge = j;
            }//end if

        }//end for int j        
        cache[setNum][maxAge] = blockNum;
        age[setNum][maxAge] = 0;
    }



  }//end for int i

  return hitRate;
}//end setAssoc function
EN

回答 1

Stack Overflow用户

发布于 2015-12-03 06:22:53

我不确定这是不是这段代码中唯一的问题,但是您似乎混淆了年龄和路数。通过分配maxAge = j,您可以在您的年龄变量中放置一个任意的路编号(这将干扰查找LRU路)。然后,您可以将其用作方式索引。

我建议将其分为两个变量:

代码语言:javascript
复制
if(!hit){
    for(int j = 0; j < associativity; j++){
        //loop to find the least recently used block
        if(age[setNum][j] > maxAge){
            maxAgeWay = j;
            maxAge = age[setNum][j];

        }//end if

    }//end for int j        
    cache[setNum][maxAgeWay] = blockNum;
    age[setNum][maxAgeWay] = 0;
}

(当然要进行适当的初始化和绑定检查)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34054137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档