Repository navigation
Expand file tree
/
Copy pathBusInterconnect.cpp
More file actions
243 lines (213 loc) · 6.55 KB
/
Copy pathBusInterconnect.cpp
File metadata and controls
243 lines (213 loc) · 6.55 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "BusInterconnect.h"
#include <unistd.h>
BusInterconnect::BusInterconnect(Ram& sharedMem, int numPEs, vector<Cache*>& caches)
: sharedMemory(sharedMem),
dataTransmitted(numPEs, 0),
caches(caches),
numInvalidations(0),
numReadRequests(0),
numReadResponses(0),
numWriteRequests(0),
numWriteResponses(0) {}
BusInterconnect::~BusInterconnect()
{
{
lock_guard<mutex> lock(queue_mutex);
stopBus = true;
}
queue_cv.notify_all();
if (busThread.joinable())
{
busThread.join();
}
}
future<uint64_t> BusInterconnect::enqueueRead(Cache& cache, int i, int j, int peId, int adderss)
{
Request req;
req.peID = peId;
req.type = READ;
req.address = adderss;
// No se necesita data para un read
future<uint64_t> fut = req.promise.get_future();
{
lock_guard<mutex> lock(queue_mutex);
requestQueue.push_back(move(req));
}
queue_cv.notify_one();
if(cache.get_first(i,j) == 0){
assignMESIState(cache, i, j, EXCLUSIVE, READ);
} else{
assignMESIState(cache, i, j, SHARED, READ);
}
//std::cout<<fut.get()<< " lol"<<std::endl;
return fut;
}
void BusInterconnect::enqueueWrite(Cache& cache, int i, int j,int peId, int adderss, uint64_t data)
{
Request req;
req.peID = peId;
req.type = WRITE;
req.address = adderss;
req.data = data;
// No se necesita promise para un WRITE
{
lock_guard<mutex> lock(queue_mutex);
requestQueue.push_back(move(req));
}
assignMESIState(cache,i, j, MODIFIED, WRITE);
queue_cv.notify_one();
}
void BusInterconnect::alwaysWriteOnMemory(int i, int j, int peId, int address, uint64_t data)
{
// CAMBIAR A DEQUE, PARA QUE EL WRITE-BACK OCURRA DE PRIMERO SIEMPRE
// REVISAR LOS METODOS POP_FRONT y PUSH_FRONT
Request req;
req.peID = peId;
req.type = WRITE;
req.address = address;
req.data = data;
{
lock_guard<mutex> lock(queue_mutex);
requestQueue.push_front(move(req));
}
queue_cv.notify_one();
}
void BusInterconnect::notifyOtherCaches(Cache &cache, int i, int j)
{
assignMESIState(cache, i, j, MODIFIED, WRITE);
}
void BusInterconnect::processRequests()
{
int contador = 0;
while (true)
{
sleep(1);
contador++;
unique_lock<mutex> lock(queue_mutex);
queue_cv.wait(lock, [this] { return !requestQueue.empty() || stopBus; });
if (stopBus && requestQueue.empty())
{
break;
}
// Obtener la respuesta de la queue
Request req = move(requestQueue.front());
requestQueue.pop_front();
lock.unlock();
// Procesa la solicitud
if (req.type == READ)
{
{
lock_guard<mutex> busLock(bus_mutex);
numReadRequests++;
uint64_t data = sharedMemory.read_mem(req.address);
numReadResponses++;
dataTransmitted[req.peID] += sizeof(data);
req.promise.set_value(data);
}
}
else if(req.type == WRITE)
{
{
lock_guard<mutex> busLock(bus_mutex);
numWriteRequests++;
sharedMemory.write_mem(req.address, req.data);
numWriteResponses++;
dataTransmitted[req.peID] += sizeof(req.data);
}
}
}
}
void BusInterconnect::assignMESIState(Cache& cache, int i, int j, MESIState newState, OperationType operationType)
{
MESIState currentState = cache.get_state(i,j);
cout<<"el estado actual de PE"<< cache.get_id()<< " es " << currentState <<endl;
switch (newState)
{
case MODIFIED:
if((currentState == SHARED || currentState == INVALID) && operationType == WRITE)
{
int address = cache.get_address(i,j);
for (auto& other_cache : caches)
{
if(other_cache->get_id() != cache.get_id()) {
if (other_cache->is_in_cache(address))
{
other_cache->set_state(other_cache->get_index(address).first, other_cache->get_index(address).second,INVALID);
numInvalidations++;
cout<<"SE INVALIDARON LOS CACHES: "<<other_cache->get_id()<<endl;
}
}
}
cache.set_state(i, j, MODIFIED);
} else if ((currentState == EXCLUSIVE || currentState == INVALID) && operationType == WRITE)
{
cache.set_state(i, j, MODIFIED);
} else if (currentState == MODIFIED)
{
cache.set_state(i, j, MODIFIED);
}
cout<<"el nuevo estado es MODIFIED"<<endl;
break;
case EXCLUSIVE:
if (currentState == INVALID && operationType == READ)
{
int address = cache.get_address(i,j);
cache.set_state(i, j, EXCLUSIVE);
cout<<"el nuevo estado es EXCLUSIVE"<<endl;
for (auto& other_cache : caches) {
if(other_cache->get_id() != cache.get_id()) {
if (other_cache->is_in_cache(cache.get_address(i, j))) {
cache.set_state(i, j, SHARED);
if (other_cache->get_state(other_cache->get_index(address).first,other_cache->get_index(address).second) == MODIFIED) {
other_cache->Writeback(other_cache->get_index(address).first,other_cache->get_index(address).second);
}
other_cache->set_state(i, j, SHARED);
cout << "el nuevo estado es SHARED" << endl;
break;
}
}
}
}
break;
case SHARED:
if (currentState == INVALID && operationType == READ)
{
int address = cache.get_address(i,j);
for (auto& other_cache : caches)
{
if(other_cache->get_id() != cache.get_id()) {
if (other_cache->is_in_cache(address) && other_cache->get_state(other_cache->get_index(address).first,other_cache->get_index(address).second) != INVALID)
{
other_cache->set_state(other_cache->get_index(address).first,other_cache->get_index(address).second, SHARED);
cout<<"PASARON A SHARED LOS CACHES: "<<other_cache->get_id()<<endl;
} else if (other_cache->get_state(other_cache->get_index(address).first,other_cache->get_index(address).second) == MODIFIED )
{
other_cache->Writeback(other_cache->get_index(address).first,other_cache->get_index(address).second);
}
}
}
cache.set_state(i, j, SHARED);
}
cout<<"el nuevo estado es SHARED*"<<endl;
break;
case INVALID:
cache.set_state(i, j, INVALID);
cout<<"el nuevo estado es INVALID"<<endl;
break;
default:
break;
}
}
void BusInterconnect::registerInvalidation(int peId)
{
lock_guard<mutex> lock(bus_mutex);
numInvalidations++;
}
int BusInterconnect::getNumInvalidations() const { return numInvalidations; }
int BusInterconnect::getNumReadRequests() const { return numReadRequests; }
int BusInterconnect::getNumReadResponses() const { return numReadResponses; }
int BusInterconnect::getNumWriteRequests() const { return numWriteRequests; }
int BusInterconnect::getNumWriteResponses() const{ return numWriteResponses; }
uint64_t BusInterconnect::getDataTransmitted(int peId) const {
return dataTransmitted[peId];
}