gpt4 book ai didi

c++ - 直接映射缓存模拟器 C++ 中的错误

转载 作者:行者123 更新时间:2023-11-28 07:13:53 24 4
gpt4 key购买 nike

免责声明:这是一篇很长的文章,所以请男孩女孩们多多包涵。

大家好。我正在制作一个带有回写策略的直接映射缓存模拟器。我的代码中出现了一个小错误。请允许我向您展示代码,并在底部解释错误。

产生部分不正确输出的输入和输出文件:

input:
read 0x02000006
read 0x04000004
write 0x02000007
read 0x06000004
write 0x01000001
read 0x01000002
write 0x02000000
read 0x06000007
read 0x05000003
write 0x02000001
write 0x03000000
write 0x04000002
read 0x03000004
read 0x01000003
read 0x03000005
write 0x04000004
read 0x04000007
write 0x05000003
read 0x02000000
write 0x02000003
read 0x03000002
read 0x00700002
read 0x02000005
write 0x01000001
read 0x01000006
write 0x01000005
write 0x07000006
write 0x02000003
read 0x03000002
read 0x01000000
read 0x03000001
read 0x01000007



my output:
1024 8 DM 0.00 256 104 1
1024 16 DM 0.00 512 416 1
1024 32 DM 0.00 1024 832 1
1024 128 DM 0.00 4096 3328 1



what output should be:
1024 8 DM 0.16 216 96 1
1024 16 DM 0.16 432 192 1
1024 32 DM 0.16 864 384 1
1024 128 DM 0.16 3456 1536 1

标题:

#include <vector>
using namespace std;

struct input
{
bool dirtyBit;
int statusBit; //0 not in cache, 1 in cache
bool writeStatus; //write = 1 read = 0
int address;
};

class Cache
{

public:

vector<input> dataBase;
Cache(string);
~Cache();

void DirectMapped(int, int);

};

执行文件:

#include "Header.h"
#include <iostream>
#include <iomanip>

Cache::Cache(string infile)
{
ifstream in(infile);
string readWriteStatus;
int Addr;
while (in >> readWriteStatus >> hex >> Addr)
{
input contents;
if (readWriteStatus == "read")
contents.writeStatus = false;
else if (readWriteStatus == "write")
contents.writeStatus = true;
contents.address = Addr;
contents.dirtyBit = false;
contents.statusBit = 0;
dataBase.push_back(contents);
}
}

Cache::~Cache(){}

void Cache::DirectMapped(int cacheSize, int blockSize)
{
//initial stats needed
int blockCount = cacheSize/blockSize;
//clear out the cache
for (int i = 0; i < dataBase.size(); i++)
dataBase[i].statusBit = 0;
int hit = 0;
int MtoCBytes = 0;
int CtoMBytes = 0;
int apple=0;
for (int x = 0; x < dataBase.size(); x++)
{
int blockIndex = (dataBase[x].address/blockSize)%blockCount;
int blockOffset = (dataBase[x].address)%blockSize;
int tag = dataBase[x].address/cacheSize;

if(dataBase[x].statusBit == 1)
hit++;
else
{
if(dataBase[x].dirtyBit == true)
CtoMBytes +=blockSize;
dataBase[x].statusBit == 1;
MtoCBytes += blockSize;
}
if (dataBase[x].writeStatus == true && dataBase[x].writeStatus == true)
dataBase[x].dirtyBit = true;

}
for (int y = 0; y < dataBase.size(); y++)
{
if (dataBase[y].dirtyBit == true)
CtoMBytes +=blockSize;
}
float hitRatio = hit/dataBase.size();
cout << cacheSize << " " << blockSize << " DM ";
cout << setiosflags(ios::fixed|ios::showpoint) << setprecision(2) << hitRatio << " ";
cout << MtoCBytes << " " << CtoMBytes << " 1" << endl;
}

主要内容:

#include "Header.h"

int main(int argc, char *argv[])
{
string inputfile = "C:/Users/Christopher/Downloads/test1";
string infile = inputfile.append(".trace");
Cache myCache(infile);

// Parse Command Line Argument
// if(argc != 2)
// cout << "ERROR: Improper Number of Arguments" << endl;
// else
// {
int i = 1024;
for(int j = 8; j <= 128; j= j*2)
{
myCache.DirectMapped(i,j);
if (j==32)
j=j*2;
}

system ( "pause");
return 0;
}

好的,正如您所见,问题是输出困惑了。如果您查看植入文件的末尾,您可以看到乱七八糟的区域是命中率、内存到缓存和缓存到内存。

我知道缓存是如何工作的,我知道写策略是如何工作的,我知道这一切是如何工作的。我不知道为什么这段代码不起作用。我已经调试了一段时间,但没有这样的运气。

话虽如此,我确信这里有人可以帮助我。我确信问题出在名为 DirectMapped 的方法中。如果您设置它,代码将按此处运行。请帮助我,我将非常感激。

最佳答案

问题是这一行

float hitRatio = hit/dataBase.size();

hit是一个intdatabase.size()是一个int,那么结果就是一个 int。改成这样,

float hitRatio = ((float)hit)/dataBase.size();

看看有没有什么不同。

关于c++ - 直接映射缓存模拟器 C++ 中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20546798/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com