-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparison.cpp
More file actions
156 lines (122 loc) · 3.92 KB
/
comparison.cpp
File metadata and controls
156 lines (122 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <string>
using namespace std;
// Cache parameters
int CACHE_SIZE = 1024; // 1KB cache
int BLOCK_SIZE = 64; // 32-byte blocks
int NUM_SETS = CACHE_SIZE / (BLOCK_SIZE); // direct-mapped cache, so 1 set per block
// Cache entry structure
struct CacheEntry {
int index;
int tag;
int counter;
};
// Cache statistics
int num_hits = 0;
int num_accesses = 0;
// Convert hex address to binary and extract tag, set, and offset
void parse_address(string address, int& tag, int& set, int& offset) {
// Convert hex address to binary
bitset<32> bits(stoul(address, nullptr, 16));
// Extract tag, set, and offset
tag = bits.to_ulong() / (BLOCK_SIZE * NUM_SETS);
set = (bits.to_ulong() / BLOCK_SIZE) % NUM_SETS;
offset = bits.to_ulong() % BLOCK_SIZE;
}
// Direct-mapped cache simulation
void direct_mapped_cache_sim() {
// Initialize cache with empty entries
vector<CacheEntry> cache(NUM_SETS);
// Open trace file
ifstream trace_file("gcc.trace");
// Parse each line in trace file
string line;
while (getline(trace_file, line)) {
// Ignore first character (load vs store)
line = line.substr(1);
// Parse address
int tag, set, offset;
parse_address(line.substr(0, 8), tag, set, offset);
// Check cache for hit
if (cache[set].tag == tag) {
num_hits++;
cache[set].counter++;
}
else {
cache[set].tag = tag;
cache[set].counter = 1;
}
num_accesses++;
}
// Calculate hit rate
float hit_rate = (float)num_hits / num_accesses;
// Print cache statistics
cout << "Direct-mapped cache statistics:" << endl;
cout << "Cache size: " << CACHE_SIZE << endl;
cout << "Block size: " << BLOCK_SIZE << endl;
cout << "Number of sets: " << NUM_SETS << endl;
cout << "Number of accesses: " << num_accesses << endl;
cout << "Number of hits: " << num_hits << endl;
cout << "Hit rate: " << hit_rate << endl;
}
// Fully associative cache simulation
void fully_associative_cache_sim() {
// Initialize cache with empty entries
vector<CacheEntry> cache(CACHE_SIZE);
// Open trace file
ifstream trace_file("gcc.trace");
// Parse each line in trace file
string line;
while (getline(trace_file, line)) {
// Ignore first character (load vs store)
line = line.substr(1);
// Parse address
int tag, set, offset;
parse_address(line.substr(0, 8), tag, set, offset);
// Check cache for hit
bool hit = false;
int lru_index = 0;
for (int i = 0; i < CACHE_SIZE; i++) {
if (cache[i].tag == tag) {
hit = true;
cache[i].counter++;
break;
}
if (cache[i].counter < cache[lru_index].counter) {
lru_index = i;
}
}
if (!hit) {
cache[lru_index].tag = tag;
cache[lru_index].counter = num_accesses;
}
num_accesses++;
if (hit) {
num_hits++;
}
}
// Calculate hit rate
float hit_rate = (float)num_hits / num_accesses;
// Print cache statistics
cout << "Fully associative cache statistics:" << endl;
cout << "Cache size: " << CACHE_SIZE << endl;
cout << "Block size: " << BLOCK_SIZE << endl;
cout << "Number of accesses: " << num_accesses << endl;
cout << "Number of hits: " << num_hits << endl;
cout << "Hit rate: " << hit_rate << endl;
}
int main() {
direct_mapped_cache_sim();
// Reset cache statistics
num_hits = 0;
num_accesses = 0;
// Fully associative cache parameters
CACHE_SIZE = 1024; // 1KB cache
BLOCK_SIZE = 64; // 32-byte blocks
NUM_SETS = 1; // fully associative cache, so only 1 set
fully_associative_cache_sim();
return 0;
}