Cache 一致性协议
野生的简单模拟题。翻到文档的末尾,根据表格中的操作一个个处理就好了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int N, M;
int TYPES[130];
const char* typenames[4] = {
"Invalid",
"Exclusive",
"Shared",
"Modified"
};
inline void FlushOpt(int x){
if(x)
printf("FlushOpt %d\n", x);
if(!x)
for(int i=1; i<=N; i++){
if(TYPES[i] == 0){
}
else if(TYPES[i] == 1){
}
else if(TYPES[i] == 2){
}
else{
printf("MemoryWr %d\n", i);
TYPES[i] = 1;
}
}
}
inline void BusUpgr(int x){
printf("BusUpgr %d\n", x);
for(int i=1; i<=N; i++){
if(TYPES[i] == 0){
}
else if(TYPES[i] == 1){
}
else if(TYPES[i] == 2){
TYPES[i] = 0;
}
else{
}
}
}
inline bool BusRd(int x){
printf("BusRd %d\n", x);
bool sended = false;
for(int i=1; i<=N; i++){
if(TYPES[i] == 0){
}
else if(TYPES[i] == 1){
if(!sended){
sended = true;
FlushOpt(i);
}
TYPES[i] = 2;
}
else if(TYPES[i] == 2){
if(!sended){
sended = true;
FlushOpt(i);
}
TYPES[i] = 2;
}
else{
if(!sended){
sended = true;
FlushOpt(i);
}
printf("MemoryWr %d\n", i);
TYPES[i] = 2;
}
}
return sended;
}
inline bool BusRdX(int x){
printf("BusRdX %d\n", x);
bool sended = false;
for(int i=1; i<=N; i++){
if(TYPES[i] == 0){
}
else if(TYPES[i] == 1){
if(!sended){
sended = true;
FlushOpt(i);
}
TYPES[i] = 0;
}
else if(TYPES[i] == 2){
if(!sended){
sended = true;
FlushOpt(i);
}
TYPES[i] = 0;
}
else{
if(!sended){
sended = true;
FlushOpt(i);
}
printf("MemoryWr %d\n", i);
TYPES[i] = 0;
}
}
return sended;
}
inline void PrRd(int x){
if(TYPES[x] == 0){
bool have_cpu = BusRd(x);
TYPES[x] = (have_cpu ? 2 : 1);
if(TYPES[x] == 1)
printf("MemoryRd %d\n", x);
}
else if(TYPES[x] == 1){
}
else if(TYPES[x] == 2){
}
else{
}
}
inline void PrWr(int x){
if(TYPES[x] == 0){
bool have_cpu = BusRdX(x);
if(! have_cpu)
printf("MemoryRd %d\n", x);
TYPES[x] = 3;
}
else if(TYPES[x] == 1){
TYPES[x] = 3;
}
else if(TYPES[x] == 2){
BusUpgr(x);
TYPES[x] = 3;
}
else{
TYPES[x] = 3;
}
}
int main(){
scanf("%d%d", &N, &M);
while(M --){
char ch; int id;
scanf(" %c", &ch);
if(ch == 'F'){
FlushOpt(0);
continue;
}
scanf("%d", &id);
if(ch == 'R')
PrRd(id);
else if(ch == 'W')
PrWr(id);
else
puts(typenames[TYPES[id]]);
}
return 0;
}Cache 替换算法
由于之后的题目只有单个缓存组,所以只需要考虑是否为 Dirty,而不需要考虑到 MESI 协议的问题。
返回到替换算法的介绍,对于每一次询问,我们需要做的是:
- 寻找是否包含 Tag 相同的
Cacheline - 如果没有的话,根据算法找到
Victim并进行替换
前面六个算法的 Victim 找起来都相对容易,其中后四个只需要增加一个数组就可以完成维护。而 PLRU 算法需要我们维护一个 01-Trie 状物,但是考虑到结构的特殊性,我们依然可以使用一个数组维护信息,其中第 位左儿子为 ,右儿子为 ,储存机制和线段树类似。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstdint>
#include <set>
#include <queue>
using namespace std;
uint32_t REPLACE_ALGORITHM;
uint32_t Associativity;
static uint64_t random_next = 1;
uint32_t my_rand(void) {
random_next = random_next * 1103515245 + 12345;
return (uint32_t)(random_next/65536) % 32768;
}
struct CacheLine {
uint32_t data;
short type;
uint32_t tag;
CacheLine(){
data = 0;
type = 0;
tag = 0;
}
};
uint32_t memory_read(uint32_t address) {
return 0;
}
struct CacheSet {
vector<CacheLine> datas;
set<uint32_t> invalidList;
vector<uint32_t> addition;
uint32_t timeIndex;
queue<uint32_t> idxQueue;
CacheSet(){
datas.assign(Associativity, CacheLine());
invalidList.clear();
for(uint32_t i = 0; i < Associativity; i ++)
invalidList.insert(i);
if(REPLACE_ALGORITHM == 0)
return;
if(REPLACE_ALGORITHM != 1 && REPLACE_ALGORITHM != 2 && REPLACE_ALGORITHM != 8)
addition.assign(Associativity, 0);
else
addition.clear();
if(REPLACE_ALGORITHM == 2 || REPLACE_ALGORITHM == 8)
while(!idxQueue.empty())
idxQueue.pop();
timeIndex = 0;
}
uint32_t findLocation(uint32_t tag) {
if(REPLACE_ALGORITHM == 0)
return 0;
for(uint32_t i = 0; i < Associativity; i ++)
if(datas[i].type != 0 && datas[i].tag == tag)
return i;
if(REPLACE_ALGORITHM != 6 && invalidList.size())
return *invalidList.begin();
if(REPLACE_ALGORITHM == 1)
return my_rand() % Associativity;
else if(REPLACE_ALGORITHM == 2){
uint32_t ret = idxQueue.front();
idxQueue.pop(); return ret;
}
else if(REPLACE_ALGORITHM == 3){
pair<uint32_t, uint32_t> ret(1e9, 0);
for(uint32_t i = 0; i < Associativity; i ++)
ret = min(ret, make_pair(addition[i], i));
return ret.second;
}
else if(REPLACE_ALGORITHM == 4){
pair<uint32_t, uint32_t> ret(1e9, 0);
for(uint32_t i = 0; i < Associativity; i ++)
ret = min(ret, make_pair(addition[i], i));
return ret.second;
}
else if(REPLACE_ALGORITHM == 5){
uint32_t cnt = 0, ret = 0;
for(uint32_t i = 0; i < Associativity; i ++)
cnt = max(cnt, addition[i]);
for(uint32_t i = 0; i < Associativity; i ++)
if(addition[i] == cnt){
ret = i;
break;
}
return ret;
}
else if(REPLACE_ALGORITHM == 6){
uint32_t nodeDepth = Associativity;
uint32_t ret = 1;
while(nodeDepth != 1){
addition[ret-1] ^= 1;
ret = ((ret << 1) | (! addition[ret-1]));
nodeDepth >>= 1;
}
return ret - Associativity;
}
else if(REPLACE_ALGORITHM == 7){
// TO DO
}
return 0;
}
uint32_t getTime() {
return ++ timeIndex;
}
void fetchFromMemory(uint32_t loc, uint32_t tag) {
if(datas[loc].type == 0){
invalidList.erase(loc);
if(REPLACE_ALGORITHM == 2)
idxQueue.push(loc);
}
datas[loc].type = 1;
datas[loc].tag = tag;
datas[loc].data = memory_read(loc);
if(REPLACE_ALGORITHM == 3)
addition[loc] = 0;
else if(REPLACE_ALGORITHM == 4)
addition[loc] = getTime();
else if(REPLACE_ALGORITHM == 5)
addition[loc] = getTime();
else if(REPLACE_ALGORITHM == 6)
;
else if(REPLACE_ALGORITHM == 7){
// TO DO
}
else
;
}
uint32_t getData(uint32_t tag) {
uint32_t loc = findLocation(tag);
if(datas[loc].type != 0 && datas[loc].tag != tag){
printf("%d\n", loc);
// clear victim info
datas[loc] = CacheLine();
invalidList.insert(loc);
}
else
printf("-1\n");
if(datas[loc].type == 0)
fetchFromMemory(loc, tag);
if(REPLACE_ALGORITHM == 3)
++ addition[loc];
else if(REPLACE_ALGORITHM == 4)
addition[loc] = getTime();
else if(REPLACE_ALGORITHM == 5)
addition[loc] = getTime();
else if(REPLACE_ALGORITHM == 6) {
uint32_t nodeDepth = Associativity;
uint32_t ret = 1;
while(nodeDepth != 1){
uint32_t g = (loc & (nodeDepth >> 1)) != 0;
addition[ret - 1] = ! g;
ret = (ret << 1) | g;
nodeDepth >>= 1;
}
}
return datas[loc].data;
}
};
char tagStr[35];
int main(){
int N;
scanf("%u%u%d\n", &Associativity, &REPLACE_ALGORITHM, &N);
-- REPLACE_ALGORITHM;
CacheSet cl;
while(N --){
scanf(" %s", tagStr);
int tag = 0;
sscanf(tagStr, "%x", &tag);
cl.getData(tag);
}
return 0;
}只读 Cache 实现
将上面替换算法封装成一个 CacheSet 类,对应缓存系统中的 组。于是缓存系统就可以看作若干的 CacheSet 组合得来的数列。
对于一个访问需求来说,我们砍掉地址的后四位,然后取出末尾足够多的位数使得其可以定位到一个组,剩下的东西作为 tag 就可以直接传给替换算法了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstdint>
#include <set>
#include <queue>
#include <iostream>
using namespace std;
uint32_t memory_read(uint32_t addr);
uint32_t REPLACE_ALGORITHM;
uint32_t Associativity;
uint32_t SetCount;
static uint64_t random_next = 1;
uint32_t my_rand(void) {
random_next = random_next * 1103515245 + 12345;
return (uint32_t)(random_next/65536) % 32768;
}
struct CacheLine {
uint32_t data;
short type;
uint32_t tag;
CacheLine(){
data = 0;
type = 0;
tag = 0;
}
};
struct CacheSet {
vector<CacheLine> datas;
set<uint32_t> invalidList;
vector<uint32_t> addition;
uint32_t timeIndex;
queue<uint32_t> idxQueue;
int setIndex;
CacheSet(int setIndex): setIndex(setIndex){
datas.assign(Associativity, CacheLine());
invalidList.clear();
for(uint32_t i = 0; i < Associativity; i ++)
invalidList.insert(i);
if(REPLACE_ALGORITHM == 0)
return;
if(REPLACE_ALGORITHM != 1 && REPLACE_ALGORITHM != 2 && REPLACE_ALGORITHM != 8)
addition.assign(Associativity, 0);
else
addition.clear();
if(REPLACE_ALGORITHM == 2 || REPLACE_ALGORITHM == 8)
while(!idxQueue.empty())
idxQueue.pop();
timeIndex = 0;
}
uint32_t findLocation(uint32_t tag) {
if(REPLACE_ALGORITHM == 0)
return 0;
for(uint32_t i = 0; i < Associativity; i ++)
if(datas[i].type != 0 && datas[i].tag == tag)
return i;
if(REPLACE_ALGORITHM != 6 && invalidList.size())
return *invalidList.begin();
if(REPLACE_ALGORITHM == 1)
return my_rand() % Associativity;
else if(REPLACE_ALGORITHM == 2){
uint32_t ret = idxQueue.front();
idxQueue.pop(); return ret;
}
else if(REPLACE_ALGORITHM == 3){
pair<uint32_t, uint32_t> ret(1e9, 0);
for(uint32_t i = 0; i < Associativity; i ++)
ret = min(ret, make_pair(addition[i], i));
return ret.second;
}
else if(REPLACE_ALGORITHM == 4){
pair<uint32_t, uint32_t> ret(1e9, 0);
for(uint32_t i = 0; i < Associativity; i ++)
ret = min(ret, make_pair(addition[i], i));
return ret.second;
}
else if(REPLACE_ALGORITHM == 5){
uint32_t cnt = 0, ret = 0;
for(uint32_t i = 0; i < Associativity; i ++)
cnt = max(cnt, addition[i]);
for(uint32_t i = 0; i < Associativity; i ++)
if(addition[i] == cnt){
ret = i;
break;
}
return ret;
}
else if(REPLACE_ALGORITHM == 6){
uint32_t nodeDepth = Associativity;
uint32_t ret = 1;
while(nodeDepth != 1){
addition[ret-1] ^= 1;
ret = ((ret << 1) | (! addition[ret-1]));
nodeDepth >>= 1;
}
return ret - Associativity;
}
else if(REPLACE_ALGORITHM == 7){
// TO DO
}
return 0;
}
uint32_t getTime() {
return ++ timeIndex;
}
void fetchFromMemory(uint32_t loc, uint32_t tag) {
if(datas[loc].type == 0){
invalidList.erase(loc);
if(REPLACE_ALGORITHM == 2)
idxQueue.push(loc);
}
datas[loc].type = 1;
datas[loc].tag = tag;
uint32_t memoryLoc = tag;
memoryLoc = memoryLoc * SetCount + setIndex;
memoryLoc = memoryLoc * 4;
datas[loc].data = memory_read(memoryLoc);
if(REPLACE_ALGORITHM == 3)
addition[loc] = 0;
else if(REPLACE_ALGORITHM == 4)
addition[loc] = getTime();
else if(REPLACE_ALGORITHM == 5)
addition[loc] = getTime();
else if(REPLACE_ALGORITHM == 6)
;
else if(REPLACE_ALGORITHM == 7){
// TO DO
}
else
;
}
uint32_t getData(uint32_t address) {
uint32_t tag = address;
uint32_t loc = findLocation(tag);
if(datas[loc].type != 0 && datas[loc].tag != tag){
datas[loc] = CacheLine();
invalidList.insert(loc);
}
if(datas[loc].type == 0)
fetchFromMemory(loc, tag);
if(REPLACE_ALGORITHM == 3)
++ addition[loc];
else if(REPLACE_ALGORITHM == 4)
addition[loc] = getTime();
else if(REPLACE_ALGORITHM == 5)
addition[loc] = getTime();
else if(REPLACE_ALGORITHM == 6) {
uint32_t nodeDepth = Associativity;
uint32_t ret = 1;
while(nodeDepth != 1){
uint32_t g = (loc & (nodeDepth >> 1)) != 0;
addition[ret - 1] = ! g;
ret = (ret << 1) | g;
nodeDepth >>= 1;
}
}
return datas[loc].data;
}
};
struct CacheSystem {
vector<CacheSet> sets;
CacheSystem() {
sets.clear();
for(int i = 0; i < SetCount; i ++)
sets.emplace_back(i);
}
uint32_t get_data(uint32_t address) {
address >>= 2;
uint32_t scnt = address % SetCount, tg = address / SetCount;
return sets[scnt].getData(tg);
}
} root;
void cache_init(uint8_t associativity, uint8_t algorithm) {
Associativity = associativity;
SetCount = (1 << 14) / Associativity;
REPLACE_ALGORITHM = algorithm - 1;
root = CacheSystem();
}
uint32_t cache_read(uint32_t address) {
return root.get_data(address);
}读写 Cache 实现
相较于上一题来说只是加了一些特判(
我们给每一个 Cacheline 加上 dirty 属性,判断这个位置的值是否改了但是没有传回到内存,然后每一次写的时候根据传入的策略编写就好了。需要注意的是写缺失事件有一个状态会涉及到写命中事件,而不是简单的几个分支。
需要注意的是:这道题中,你需要把 dirty 写入的顺序放到后面。
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
uint32_t memory_read(uint32_t address);
void memory_write(uint32_t address, uint32_t data);
uint32_t REPLACE_ALGORITHM;
uint32_t Associativity;
uint32_t SetCount;
bool WriteHitPolicy, WriteMissPolicy;
static uint64_t random_next = 1;
uint32_t my_rand(void) {
random_next = random_next * 1103515245 + 12345;
return (uint32_t)(random_next / 65536) % 32768;
}
struct CacheLine {
uint32_t data;
short type;
uint32_t tag;
bool dirty;
CacheLine() {
data = 0;
type = 0;
tag = 0;
dirty = false;
}
};
struct CacheSet {
vector<CacheLine> datas;
set<uint32_t> invalidList;
vector<uint32_t> addition;
uint32_t timeIndex;
int setIndex;
CacheSet(int setIndex) : setIndex(setIndex) {
datas.assign(Associativity, CacheLine());
invalidList.clear();
for (uint32_t i = 0; i < Associativity; i++)
invalidList.insert(i);
if (REPLACE_ALGORITHM == 0)
return;
if (REPLACE_ALGORITHM != 1 && REPLACE_ALGORITHM != 8)
addition.assign(Associativity, 0);
else
addition.clear();
timeIndex = 0;
}
uint32_t findLocation(uint32_t tag, bool restricted = true) {
if (REPLACE_ALGORITHM == 0)
return 0;
for (uint32_t i = 0; i < Associativity; i++)
if (datas[i].type != 0 && datas[i].tag == tag)
return i;
if (REPLACE_ALGORITHM != 6 && invalidList.size())
return *invalidList.begin();
if (!restricted)
return 0;
if (REPLACE_ALGORITHM == 1)
return my_rand() % Associativity;
else if (REPLACE_ALGORITHM == 2) {
pair<uint32_t, uint32_t> ret(1e9, 0);
for (uint32_t i = 0; i < Associativity; i++)
ret = min(ret, make_pair(addition[i], i));
return ret.second;
} else if (REPLACE_ALGORITHM == 3) {
pair<uint32_t, uint32_t> ret(1e9, 0);
for (uint32_t i = 0; i < Associativity; i++)
ret = min(ret, make_pair(addition[i], i));
return ret.second;
} else if (REPLACE_ALGORITHM == 4) {
pair<uint32_t, uint32_t> ret(1e9, 0);
for (uint32_t i = 0; i < Associativity; i++)
ret = min(ret, make_pair(addition[i], i));
return ret.second;
} else if (REPLACE_ALGORITHM == 5) {
uint32_t cnt = 0, ret = 0;
for (uint32_t i = 0; i < Associativity; i++)
cnt = max(cnt, addition[i]);
for (uint32_t i = 0; i < Associativity; i++)
if (addition[i] == cnt) {
ret = i;
break;
}
return ret;
} else if (REPLACE_ALGORITHM == 6) {
uint32_t nodeDepth = Associativity;
uint32_t ret = 1;
while (nodeDepth != 1) {
addition[ret - 1] ^= 1;
ret = ((ret << 1) | (!addition[ret - 1]));
nodeDepth >>= 1;
}
return ret - Associativity;
} else if (REPLACE_ALGORITHM == 7) {
// TO DO
}
return 0;
}
uint32_t getTime() { return ++timeIndex; }
uint32_t getAddress(uint32_t tag) {
uint32_t memoryLoc = tag;
memoryLoc = memoryLoc * SetCount + setIndex;
memoryLoc = memoryLoc * 4;
return memoryLoc;
}
void fetchFromMemory(uint32_t loc, uint32_t tag) {
if (datas[loc].type == 0)
invalidList.erase(loc);
datas[loc].type = 1;
datas[loc].tag = tag;
datas[loc].data = memory_read(getAddress(tag));
if (REPLACE_ALGORITHM == 2)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 3)
addition[loc] = 0;
else if (REPLACE_ALGORITHM == 4)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 5)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 6)
;
else if (REPLACE_ALGORITHM == 7) {
// TO DO
} else
;
}
uint32_t getData(uint32_t tag) {
uint32_t loc = findLocation(tag);
bool mWriteTrack = false;
uint32_t mWriteAddress = 0;
uint32_t mWriteData = 0;
if (datas[loc].type != 0 && datas[loc].tag != tag) {
if (datas[loc].dirty) {
mWriteTrack = true;
mWriteAddress = getAddress(datas[loc].tag);
mWriteData = datas[loc].data;
datas[loc].dirty = false;
}
datas[loc] = CacheLine();
invalidList.insert(loc);
}
if (datas[loc].type == 0)
fetchFromMemory(loc, tag);
if (REPLACE_ALGORITHM == 3)
++addition[loc];
else if (REPLACE_ALGORITHM == 4)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 5)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 6) {
uint32_t nodeDepth = Associativity;
uint32_t ret = 1;
while (nodeDepth != 1) {
uint32_t g = (loc & (nodeDepth >> 1)) != 0;
addition[ret - 1] = !g;
ret = (ret << 1) | g;
nodeDepth >>= 1;
}
}
if (mWriteTrack)
memory_write(mWriteAddress, mWriteData);
return datas[loc].data;
}
void writeData(uint32_t tag, uint32_t data) {
uint32_t loc = findLocation(tag, WriteMissPolicy);
bool matchFail = datas[loc].type == 0;
bool mWriteTrack = false;
uint32_t mWriteAddress = 0;
uint32_t mWriteData = 0;
if (datas[loc].type != 0 && datas[loc].tag != tag) {
if (datas[loc].dirty && WriteMissPolicy) {
mWriteTrack = true;
mWriteAddress = getAddress(datas[loc].tag);
mWriteData = datas[loc].data;
datas[loc].dirty = false;
}
matchFail = true;
}
if (matchFail) {
if (WriteMissPolicy) {
fetchFromMemory(loc, tag);
if (WriteHitPolicy) {
datas[loc].data = data;
datas[loc].dirty = true;
} else {
datas[loc].data = data;
datas[loc].dirty = false;
memory_write(getAddress(tag), datas[loc].data);
}
} else {
memory_write(getAddress(tag), data);
}
} else {
if (WriteHitPolicy) {
datas[loc].data = data;
datas[loc].dirty = true;
} else {
datas[loc].data = data;
datas[loc].dirty = false;
memory_write(getAddress(tag), datas[loc].data);
}
}
if (!matchFail || WriteMissPolicy) {
if (REPLACE_ALGORITHM == 3)
++addition[loc];
else if (REPLACE_ALGORITHM == 4)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 5)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 6) {
uint32_t nodeDepth = Associativity;
uint32_t ret = 1;
while (nodeDepth != 1) {
uint32_t g = (loc & (nodeDepth >> 1)) != 0;
addition[ret - 1] = !g;
ret = (ret << 1) | g;
nodeDepth >>= 1;
}
}
}
if (mWriteTrack)
memory_write(mWriteAddress, mWriteData);
}
};
struct CacheSystem {
vector<CacheSet> sets;
CacheSystem() {
sets.clear();
for (int i = 0; i < SetCount; i++)
sets.emplace_back(i);
}
uint32_t get_data(uint32_t address) {
address >>= 2;
uint32_t scnt = address % SetCount, tg = address / SetCount;
return sets[scnt].getData(tg);
}
void write_data(uint32_t address, uint32_t data) {
address >>= 2;
uint32_t scnt = address % SetCount, tg = address / SetCount;
sets[scnt].writeData(tg, data);
}
} root;
void cache_init(uint8_t associativity, uint8_t algorithm,
uint8_t write_hit_policy, uint8_t write_miss_policy) {
Associativity = associativity;
SetCount = (1 << 14) / Associativity;
REPLACE_ALGORITHM = algorithm - 1;
WriteHitPolicy = write_hit_policy;
WriteMissPolicy = write_miss_policy;
root = CacheSystem();
}
uint32_t cache_read(uint32_t address) { return root.get_data(address); }
void cache_write(uint32_t address, uint32_t data) {
return root.write_data(address, data);
}LIRS 替换算法实现
激动人心的论文题!
论文的具体内容就不展开叙述了,这里讲一下实现的方法:
我们考虑定义一个 tag 的 IRR 表示在最近两次查询之间访问了几个 不同 的页面。这个数值越小,说明最近两次查询离得更近,数据也就越热。另外,定义一个 tag 的 Recency(R 值) 表示上一次出现的时间戳和现在的距离。
对于前面提到的算法,都只是利用了 R 值进行排序,所以会导致一些策略上的问题,而 LIRS 使用的方法是选择 IRR 比较大的一批。我们考虑对于所有的 tag,根据 IRR 值的多少分成两个部分,一个是 LIR,也就是 IRR 较小的热数据部分,一个是 HIR,也就是 IRR 较大的冷数据部分。其中,热数据一定在缓存中,而冷数据不一定。每一次替换中,我们只需要从 HIR 中取出一个即可。需要注意的是,我们会遗忘一些 HIR,使其变成未知块。这在之后会提到。
题目中给出了这两个集合的大小,但是考虑到 tag 可能会很多,缓存会塞不下,所以我们还需要将 HIR 细分为 缓存内 HIR 和 缓存外 HIR。
接下来考虑维护 LIR 和 HIR 两个集合。通过计算 IRR 的方式,我们可以很快算出这两个集合的内容,但是实际实现上会比较慢。我们尝试不直接计算 IRR,转而利用一些数据结构 近似 维护 IRR 的先后顺序。
我们引入一个栈 S 和一个队列 Q(这两个数据结构都需要随机访问、随机删除和置于头尾,所以建议 使用双向链表实现)。其中:
- S 储存了所有的
LIR、一部分很可能变成LIR的缓存内HIR,以及所有的缓存外HIR,并且保证越靠近栈底,距离上一次访问就越久。。 - Q 储存了所有的缓存内
HIR,且保证越靠近头,距离上一次访问就越久。
在每一次操作后,我们需要进行 栈剪枝,具体而言就是不断把 S 栈底的 HIR 元素移出,变为未知 tag,直到遇到 LIR。执行这一步一是为了方便取出最冷的热数据,二是把比所有 LIR 还要冷的数据剔除,因为其目前不可能成为 LIR。
对于每一次查询,我们考虑其状态:
-
这个 tag 是
LIR,此时其必然在缓存中,所以我们直接获取值。为了维护栈 S 的性质,我们需要把这个元素从 S 中移动到栈顶。 -
这个 tag 是 缓存内
HIR,此时我们也可以直接获取值。此时我们足以到这个HIR有可能会变成LIR,于是继续分类:-
这个 tag 在 S 中,此时由于栈剪枝的存在,我们知道它的 R 值必然比栈底的
LIR小,也就更有可能成为LIR。所以我们把这个 tag 变成LIR并移到栈顶,把栈底的LIR变成HIR。随后,我们在 Q 中把旧的HIR移出,把新的HIR放到末尾。 -
这个 tag 不在 S 中,说明这个 tag 可能成为
LIR,于是压入到 S 栈顶。
-
-
这个 tag 是 缓存外
HIR,也就意味着在 S 中,但是不在缓存内。此时我们将 Q 队列头部的元素取出,作为这次替换的Victim,并且根据其是否在 S 内变成缓存外HIR或者遗忘。根据上面的分析,我们把这个 tag 变成LIR并移到栈顶,把栈底的LIR变成HIR。随后,我们在 Q 中把新的HIR放到末尾。 -
这个 tag 是未知 tag,于是我们将 Q 队列头部的元素取出,作为这次替换的
Victim,并且根据其是否在 S 内变成缓存外HIR或者遗忘。然后,我们将这个 tag 设置为HIR,并放在 S 的栈顶和 Q 的末尾。
于是我们直接把这个算法实现好,然后放在替换算法里面就好了。
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;
uint32_t memory_read(uint32_t address);
void memory_write(uint32_t address, uint32_t data);
uint32_t REPLACE_ALGORITHM;
uint32_t Associativity;
uint32_t SetCount;
bool WriteHitPolicy, WriteMissPolicy;
static uint64_t random_next = 1;
uint32_t my_rand(void) {
random_next = random_next * 1103515245 + 12345;
return (uint32_t)(random_next / 65536) % 32768;
}
struct CacheLine {
uint32_t data;
short type;
uint32_t tag;
bool dirty;
CacheLine() {
data = 0;
type = 0;
tag = 0;
dirty = false;
}
};
struct LIRS_Manager {
struct LIRS_Info {
uint32_t tag;
uint32_t address;
LIRS_Info(uint32_t tag = 0, uint32_t address = 0)
: tag(tag), address(address){};
};
struct LIRS_LinkListNode {
LIRS_LinkListNode *prev, *next;
LIRS_Info data;
LIRS_LinkListNode(LIRS_Info data) : data(data) { prev = next = NULL; }
};
struct LIRS_LinkList {
LIRS_LinkListNode *begin, *end;
uint32_t _size;
unordered_map<uint32_t, LIRS_LinkListNode *> mp;
LIRS_LinkList() {
mp.clear();
_size = 0;
begin = end = NULL;
}
void add_element(LIRS_LinkListNode *lsn) { mp[lsn->data.tag] = lsn; }
void remove_element(LIRS_LinkListNode *lsn) { mp[lsn->data.tag] = NULL; }
void push_front(LIRS_LinkListNode *lsn) {
++_size;
add_element(lsn);
if (!begin)
begin = end = lsn;
else {
begin->prev = lsn;
lsn->next = begin;
begin = lsn;
}
}
void push_back(LIRS_LinkListNode *lsn) {
++_size;
add_element(lsn);
if (!begin)
begin = end = lsn;
else {
end->next = lsn;
lsn->prev = end;
end = lsn;
}
}
LIRS_Info front() { return begin->data; }
LIRS_Info back() { return end->data; }
void pop_front() {
--_size;
remove_element(begin);
if (begin == end)
begin = end = NULL;
else {
begin = begin->next;
begin->prev->next = NULL;
begin->prev = NULL;
}
}
void pop_back() {
--_size;
remove_element(end);
if (begin == end)
begin = end = NULL;
else {
end = end->prev;
end->next->prev = NULL;
end->next = NULL;
}
}
void erase(LIRS_LinkListNode *node) {
--_size;
remove_element(node);
if (begin == end)
begin = end = NULL;
else {
if (node->prev)
node->prev->next = node->next;
else
begin = node->next;
if (node->next)
node->next->prev = node->prev;
else
end = node->prev;
}
}
uint32_t size() { return _size; }
};
LIRS_LinkList S, Q;
unordered_map<uint32_t, int> tagState;
uint32_t L_lirs = 0, L_hirs = 0;
void init() {
S = LIRS_LinkList();
Q = LIRS_LinkList();
L_hirs = log2(Associativity);
L_lirs = Associativity - L_hirs;
}
LIRS_Manager() { init(); }
void stack_pruning() {
while (S.size() != 0 && tagState[S.front().tag] >= 2) {
if (tagState[S.front().tag] == 3)
tagState[S.front().tag] = 0;
S.pop_front();
}
}
uint32_t access(uint32_t tag, uint32_t tidx) {
int state = tagState[tag];
if (state == 0) {
// invalid
LIRS_LinkListNode *loc = Q.begin;
Q.erase(loc);
if (S.mp[loc->data.tag])
tagState[loc->data.tag] = 3;
else
tagState[loc->data.tag] = 0;
LIRS_LinkListNode data(LIRS_Info(tag, loc->data.address));
LIRS_LinkListNode data2 = data;
S.push_back(new LIRS_LinkListNode(data));
Q.push_back(new LIRS_LinkListNode(data2));
tagState[tag] = 2;
return loc->data.address;
} else if (state == 1) {
// LIR
LIRS_LinkListNode *loc = S.mp[tag];
S.erase(loc);
loc->prev = loc->next = NULL;
S.push_back(loc);
stack_pruning();
return loc->data.address;
} else if (state == 2) {
// resident HIR
if (S.mp[tag]) {
LIRS_LinkListNode *loc = S.mp[tag];
S.erase(loc);
loc->prev = loc->next = NULL;
tagState[tag] = 1;
S.push_back(loc);
Q.erase(Q.mp[tag]);
LIRS_LinkListNode *loc2 = S.begin;
S.erase(loc2);
loc2->prev = loc2->next = NULL;
tagState[loc2->data.tag] = 2;
Q.push_back(loc2);
stack_pruning();
return loc->data.address;
} else {
LIRS_LinkListNode *loc = Q.mp[tag];
Q.erase(loc);
loc->prev = loc->next = NULL;
Q.push_back(loc);
LIRS_LinkListNode dt(loc->data);
S.push_back(new LIRS_LinkListNode(dt));
return loc->data.address;
}
} else {
// non-resident HIR
LIRS_LinkListNode *loc = S.mp[tag];
S.erase(loc);
loc->prev = loc->next = NULL;
tagState[tag] = 1;
S.push_back(loc);
LIRS_LinkListNode *loc2 = S.begin;
S.erase(loc2);
loc2->prev = loc2->next = NULL;
tagState[loc2->data.tag] = 2;
Q.push_back(loc2);
if (S.mp[Q.begin->data.tag])
tagState[Q.begin->data.tag] = 3;
else
tagState[Q.begin->data.tag] = 0;
Q.pop_front();
stack_pruning();
return loc->data.address;
}
}
void insert(uint32_t tag, uint32_t address) {
// INFO tag type shoule not be non-resident HIR
if (S.mp[tag] || Q.mp[tag]) {
if (S.mp[tag])
S.mp[tag]->data.address = address;
if (Q.mp[tag])
Q.mp[tag]->data.address = address;
return;
}
if (S.size() < L_lirs) {
LIRS_LinkListNode data(LIRS_Info(tag, address));
S.push_back(new LIRS_LinkListNode(data));
tagState[tag] = 1;
} else {
LIRS_LinkListNode data(LIRS_Info(tag, address));
LIRS_LinkListNode data2 = data;
S.push_back(new LIRS_LinkListNode(data));
Q.push_back(new LIRS_LinkListNode(data2));
tagState[tag] = 2;
}
}
};
struct CacheSet {
vector<CacheLine> datas;
set<uint32_t> invalidList;
vector<uint32_t> addition;
LIRS_Manager lirs;
uint32_t timeIndex;
int setIndex;
CacheSet(int setIndex) : setIndex(setIndex) {
datas.assign(Associativity, CacheLine());
invalidList.clear();
for (uint32_t i = 0; i < Associativity; i++)
invalidList.insert(i);
if (REPLACE_ALGORITHM == 0)
return;
if (REPLACE_ALGORITHM != 1 && REPLACE_ALGORITHM != 8)
addition.assign(Associativity, 0);
else if (REPLACE_ALGORITHM == 8)
lirs.init();
else
addition.clear();
timeIndex = 0;
}
uint32_t findLocation(uint32_t tag, bool restricted = true) {
if (REPLACE_ALGORITHM == 0)
return 0;
for (uint32_t i = 0; i < Associativity; i++)
if (datas[i].type != 0 && datas[i].tag == tag) {
if (REPLACE_ALGORITHM == 7)
lirs.access(tag, getTime());
return i;
}
if (REPLACE_ALGORITHM != 6 && invalidList.size())
return *invalidList.begin();
if (!restricted)
return 0;
if (REPLACE_ALGORITHM == 1)
return my_rand() % Associativity;
else if (REPLACE_ALGORITHM == 2) {
pair<uint32_t, uint32_t> ret(1e9, 0);
for (uint32_t i = 0; i < Associativity; i++)
ret = min(ret, make_pair(addition[i], i));
return ret.second;
} else if (REPLACE_ALGORITHM == 3) {
pair<uint32_t, uint32_t> ret(1e9, 0);
for (uint32_t i = 0; i < Associativity; i++)
ret = min(ret, make_pair(addition[i], i));
return ret.second;
} else if (REPLACE_ALGORITHM == 4) {
pair<uint32_t, uint32_t> ret(1e9, 0);
for (uint32_t i = 0; i < Associativity; i++)
ret = min(ret, make_pair(addition[i], i));
return ret.second;
} else if (REPLACE_ALGORITHM == 5) {
uint32_t cnt = 0, ret = 0;
for (uint32_t i = 0; i < Associativity; i++)
cnt = max(cnt, addition[i]);
for (uint32_t i = 0; i < Associativity; i++)
if (addition[i] == cnt) {
ret = i;
break;
}
return ret;
} else if (REPLACE_ALGORITHM == 6) {
uint32_t nodeDepth = Associativity;
uint32_t ret = 1;
while (nodeDepth != 1) {
addition[ret - 1] ^= 1;
ret = ((ret << 1) | (!addition[ret - 1]));
nodeDepth >>= 1;
}
return ret - Associativity;
} else if (REPLACE_ALGORITHM == 7) {
return lirs.access(tag, getTime());
}
return 0;
}
uint32_t getTime() { return timeIndex; }
uint32_t getAddress(uint32_t tag) {
uint32_t memoryLoc = tag;
memoryLoc = memoryLoc * SetCount + setIndex;
memoryLoc = memoryLoc * 4;
return memoryLoc;
}
void fetchFromMemory(uint32_t loc, uint32_t tag) {
if (datas[loc].type == 0)
invalidList.erase(loc);
datas[loc].type = 1;
datas[loc].tag = tag;
datas[loc].data = memory_read(getAddress(tag));
if (REPLACE_ALGORITHM == 2)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 3)
addition[loc] = 0;
else if (REPLACE_ALGORITHM == 4)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 5)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 6)
;
else if (REPLACE_ALGORITHM == 7) {
lirs.insert(tag, loc);
} else
;
}
uint32_t getData(uint32_t tag) {
++timeIndex;
uint32_t loc = findLocation(tag);
bool mWriteTrack = false;
uint32_t mWriteAddress = 0;
uint32_t mWriteData = 0;
if (datas[loc].type != 0 && datas[loc].tag != tag) {
if (datas[loc].dirty) {
mWriteTrack = true;
mWriteAddress = getAddress(datas[loc].tag);
mWriteData = datas[loc].data;
datas[loc].dirty = false;
}
datas[loc] = CacheLine();
invalidList.insert(loc);
}
if (datas[loc].type == 0)
fetchFromMemory(loc, tag);
if (REPLACE_ALGORITHM == 3)
++addition[loc];
else if (REPLACE_ALGORITHM == 4)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 5)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 6) {
uint32_t nodeDepth = Associativity;
uint32_t ret = 1;
while (nodeDepth != 1) {
uint32_t g = (loc & (nodeDepth >> 1)) != 0;
addition[ret - 1] = !g;
ret = (ret << 1) | g;
nodeDepth >>= 1;
}
}
if (mWriteTrack)
memory_write(mWriteAddress, mWriteData);
return datas[loc].data;
}
void writeData(uint32_t tag, uint32_t data) {
++timeIndex;
uint32_t loc = findLocation(tag, WriteMissPolicy);
bool matchFail = datas[loc].type == 0;
bool mWriteTrack = false;
uint32_t mWriteAddress = 0;
uint32_t mWriteData = 0;
if (datas[loc].type != 0 && datas[loc].tag != tag) {
if (datas[loc].dirty && WriteMissPolicy) {
mWriteTrack = true;
mWriteAddress = getAddress(datas[loc].tag);
mWriteData = datas[loc].data;
datas[loc].dirty = false;
}
matchFail = true;
}
if (matchFail) {
if (WriteMissPolicy) {
fetchFromMemory(loc, tag);
if (WriteHitPolicy) {
datas[loc].data = data;
datas[loc].dirty = true;
} else {
datas[loc].data = data;
datas[loc].dirty = false;
memory_write(getAddress(tag), datas[loc].data);
}
} else {
memory_write(getAddress(tag), data);
}
} else {
if (WriteHitPolicy) {
datas[loc].data = data;
datas[loc].dirty = true;
} else {
datas[loc].data = data;
datas[loc].dirty = false;
memory_write(getAddress(tag), datas[loc].data);
}
}
if (!matchFail || WriteMissPolicy) {
if (REPLACE_ALGORITHM == 3)
++addition[loc];
else if (REPLACE_ALGORITHM == 4)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 5)
addition[loc] = getTime();
else if (REPLACE_ALGORITHM == 6) {
uint32_t nodeDepth = Associativity;
uint32_t ret = 1;
while (nodeDepth != 1) {
uint32_t g = (loc & (nodeDepth >> 1)) != 0;
addition[ret - 1] = !g;
ret = (ret << 1) | g;
nodeDepth >>= 1;
}
}
}
if (mWriteTrack)
memory_write(mWriteAddress, mWriteData);
}
};
struct CacheSystem {
vector<CacheSet> sets;
CacheSystem() {
sets.clear();
for (uint32_t i = 0; i < SetCount; i++)
sets.emplace_back(i);
}
uint32_t get_data(uint32_t address) {
address >>= 2;
uint32_t scnt = address % SetCount, tg = address / SetCount;
return sets[scnt].getData(tg);
}
void write_data(uint32_t address, uint32_t data) {
address >>= 2;
uint32_t scnt = address % SetCount, tg = address / SetCount;
sets[scnt].writeData(tg, data);
}
} root;
void cache_init(uint8_t associativity, uint8_t algorithm,
uint8_t write_hit_policy, uint8_t write_miss_policy) {
Associativity = associativity;
SetCount = (1 << 14) / Associativity;
REPLACE_ALGORITHM = algorithm - 1;
WriteHitPolicy = write_hit_policy;
WriteMissPolicy = write_miss_policy;
root = CacheSystem();
}
uint32_t cache_read(uint32_t address) { return root.get_data(address); }
void cache_write(uint32_t address, uint32_t data) {
return root.write_data(address, data);
}期待你的声音
本来想弄点人类迷惑行为大赏,但是只有两个人交了就没办法了。