我正在尝试实现一个使用最近最少使用的替换技术的组关联缓存。到目前为止,我的代码低估了缓存命中的数量,我不确定原因。下面是我的函数setAssoc,它接受一个表示缓存关联性的int值,以及一个由一系列数据访问组成的向量对。
该函数使用两个2D数组,一个用于存储缓存块,另一个用于存储缓存中每个块的“年龄”。
对于这个特定的实现,不用担心标记位或任何类似性质的东西;只需使用地址除以块大小就足以确定块数量,然后使用以集合数量为模的块数量来确定集合数量就足够了。
任何关于我为什么不能准确预测正确的缓存命中数量的见解都是值得感谢的!
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发布于 2015-12-03 06:22:53
我不确定这是不是这段代码中唯一的问题,但是您似乎混淆了年龄和路数。通过分配maxAge = j,您可以在您的年龄变量中放置一个任意的路编号(这将干扰查找LRU路)。然后,您可以将其用作方式索引。
我建议将其分为两个变量:
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;
}(当然要进行适当的初始化和绑定检查)
https://stackoverflow.com/questions/34054137
复制相似问题