2015-12-02 57 views
1

我試圖實現一個使用最近最少使用的替換技術的集合關聯緩存。到目前爲止,我的代碼低估了緩存命中的數量,我不知道爲什麼。下面發佈的是我的函數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 

回答

0

不知道這是否是此代碼中的唯一問題,但您似乎在年齡和方式編號之間混淆。通過分配maxAge = j,您可以在您的年齡var中添加一個任意方式的數字(這會干擾查找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; 
} 

(用正確的初始化,當然邊界檢查)