-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcaHandler.cc
More file actions
300 lines (262 loc) · 10.8 KB
/
Copy pathcaHandler.cc
File metadata and controls
300 lines (262 loc) · 10.8 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
FOGSim, simulator for interconnection networks.
http://fuentesp.github.io/fogsim/
Copyright (C) 2017 University of Cantabria
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "caHandler.h"
#include "switch/switchModule.h"
#include "flit/flitModule.h"
caHandler::caHandler(switchModule * sw) {
int i, port, group;
m_sw = sw;
m_contention_counter = new int[g_ports];
m_potential_contention_counter = new int[g_ports];
m_accumulated_contention = new float[g_ports];
m_partial_counter = new int*[g_a_routers_per_group];
for (i = 0; i < g_a_routers_per_group; i++) {
m_partial_counter[i] = new int[g_a_routers_per_group * g_h_global_ports_per_router + 1];
for (group = 0; group < g_a_routers_per_group * g_h_global_ports_per_router + 1; group++) {
m_partial_counter[i][group] = 0;
}
}
m_sending_end_cycle_queue = new queue<float> [g_ports];
m_sending_end_cycle_globalport_queue = new queue<float> [g_a_routers_per_group * g_h_global_ports_per_router + 1];
for (port = 0; port < g_ports; port++) {
m_contention_counter[port] = 0;
m_potential_contention_counter[port] = 0;
m_accumulated_contention[port] = 0.0;
}
}
caHandler::~caHandler() {
delete[] m_contention_counter;
delete[] m_potential_contention_counter;
delete[] m_accumulated_contention;
for (int sw = 0; sw < g_a_routers_per_group; sw++) {
delete[] m_partial_counter[sw];
}
delete[] m_partial_counter;
delete[] m_sending_end_cycle_queue;
delete[] m_sending_end_cycle_globalport_queue;
}
/*
* Reads all buffer heads and the sending queue,
* and updates the contention counters.
*/
void caHandler::update() {
int port, cos, chan, group;
flitModule * flit = NULL;
caFlit* ca_flit;
assert(g_contention_aware);
/* Reset partial contention counters */
for (port = 0; port < g_ports; port++) {
updateContention(port);
for (group = 0; group < g_a_routers_per_group * g_h_global_ports_per_router + 1; group++) {
m_partial_counter[m_sw->aPos][group] = 0;
}
}
//check buffer heads
for (port = 0; port < g_ports; port++)
for (cos = 0; cos < g_cos_levels; cos++)
for (chan = 0; chan < g_channels; chan++) {
/* If buffer is not 'sending' and has
* a flit to be sent, count it */
if (m_sw->inPorts[port]->canSendFlit(cos, chan)) {
// look-up flit in the buffer head
m_sw->inPorts[port]->checkFlit(cos, chan, flit);
// increment minimal path output port counter
if (flit != NULL) {
if (g_misrouting_trigger == DUAL) {
// TODO: HACK! PENDING TO BE DONE DUE TO SOME PROBLEMS WITHIN MISROUTETYPE FUNCTION
/* CHECK THIS CAREFULLY, AS 'misrouteType' HAS BEEN MOVED
* TO A VIRTUAL baseRouting FUNCTION. IT MAY HAPPEN RESULT
* IS NOT 'NONE' EVEN IF CONGESTED RESTRICTION FORBIDS IT!!
*/
// MisrouteType mis = m_sw->routing->misrouteType(port, flit, m_sw->routing->min_outport(flit), chan);
// if (mis == NONE || mis == GLOBAL_MANDATORY)
// m_contention_counter[m_sw->routing->min_outport(flit)] += g_flit_size;
// else if (mis != GLOBAL_MANDATORY) {
// m_potential_contention_counter[m_sw->routing->min_outport(flit)] += g_flit_size;
// }
} else
m_contention_counter[m_sw->routing->minOutputPort(flit->destId)] += g_flit_size;
if (port < g_p_computing_nodes_per_router || port >= g_global_router_links_offset) {
/* If input port corresponds to an injection port or a global link, also account it for partial counter */
if (flit->destGroup != m_sw->hPos) {
m_partial_counter[m_sw->aPos][flit->destGroup] += g_flit_size;
assert(
m_partial_counter[m_sw->aPos][flit->destGroup]
< (g_flit_size
* (g_p_computing_nodes_per_router * g_injection_channels
+ g_h_global_ports_per_router * g_global_link_channels)));
}
}
}
}
}
/* Take packets that are currently being sent into
* account. If they're being sent through a global
* link, account them for partial counter. */
update_sending_end_cycle_queues();
for (port = 0; port < g_ports; port++) {
m_contention_counter[port] += g_flit_size * (int) m_sending_end_cycle_queue[port].size();
#if DEBUG
if ((int) m_sending_end_cycle_queue[port].size() > 0) {
cout << "cycle " << g_cycle << " CA update: SW " << m_sw->label << " already transmitting "
<< (int) m_sending_end_cycle_queue[port].size() << " (min would have been outPort " << port << " )"
<< endl;
}
#endif
}
for (group = 0; group < g_a_routers_per_group * g_h_global_ports_per_router + 1; group++) {
if (group == m_sw->hPos) continue;
m_partial_counter[m_sw->aPos][group] += g_flit_size * (int) m_sending_end_cycle_globalport_queue[group].size();
assert(
m_partial_counter[m_sw->aPos][group]
< (g_flit_size
* (g_p_computing_nodes_per_router * g_injection_channels
+ g_h_global_ports_per_router * g_global_link_channels)));
}
#if DEBUG
cout << "cycle " << g_cycle << " CA update SUMMARY: SW " << m_sw->label;
for (port = 0; port < g_ports; port++) {
cout << " P" << port << "=" << m_contention_counter[port] << "-->" << isThereContention(port);
}
cout << endl;
#endif
/* Share self partial counter with all other routers in same group every 100 cycles */
if (g_cycle % 100 == 0) {
for (port = g_local_router_links_offset; port < g_global_router_links_offset; port++) {
ca_flit = new caFlit(m_sw->aPos, m_partial_counter[m_sw->aPos], g_cycle + g_local_link_transmission_delay);
m_sw->routing->neighList[port]->receiveCaFlit(*ca_flit);
delete ca_flit;
}
}
/* Read all other router CA flits (and update correspondent partial counters) */
this->readIncomingCAFlits();
}
/* Reads incoming CA flits from all other routers in group
* (and update correspondent partial counters).
*/
void caHandler::readIncomingCAFlits() {
while ((!m_sw->incomingCa.empty()) && (m_sw->incomingCa.front().getArrivalCycle() <= g_internal_cycle)) {
for (int group = 0; group < g_a_routers_per_group * g_h_global_ports_per_router + 1; group++) {
m_partial_counter[m_sw->incomingCa.front().m_aPos][group] = m_sw->incomingCa.front().partial_counter[group];
}
m_sw->incomingCa.pop();
}
}
/*
* Deletes any entry in the queues corresponding to a flit
* that has completely being sent.
*/
void caHandler::update_sending_end_cycle_queues() {
int port, group;
assert(g_contention_aware);
for (port = 0; port < g_ports; port++) {
/* Pop any element that has already expired */
while (not (m_sending_end_cycle_queue[port].empty())
&& m_sending_end_cycle_queue[port].front() <= g_internal_cycle) {
m_sending_end_cycle_queue[port].pop();
}
}
for (group = 0; group < g_a_routers_per_group * g_h_global_ports_per_router + 1; group++) {
while (not m_sending_end_cycle_globalport_queue[group].empty()
&& m_sending_end_cycle_globalport_queue[group].front() <= g_internal_cycle) {
m_sending_end_cycle_globalport_queue[group].pop();
}
}
}
/*
* A flit has started to be transmitted, and needs to be
* stored in the sending queue corresponding to the
* minimal path.
*/
void caHandler::flitSent(flitModule * flit, int input, int length) {
int min_output = m_sw->routing->minOutputPort(flit->destId);
m_sending_end_cycle_queue[min_output].push(g_internal_cycle + length);
if (flit->destGroup != m_sw->hPos
&& (input < g_p_computing_nodes_per_router || input >= g_global_router_links_offset))
m_sending_end_cycle_globalport_queue[flit->destGroup].push(g_internal_cycle + length);
}
/*
* Returns port contention, defined as the number of
* PHITS that would be minimally routed through a
* specified output port.
*/
int caHandler::getContention(int output) const {
assert(output < g_ports);
assert(output >= 0);
if (g_misrouting_trigger == FILTERED)
/* If misrouting trigger is filtered, it needs to
* account current and previous contention values */
return m_accumulated_contention[output];
else
return m_contention_counter[output];
}
/*
* Updates the accummulated contention value (in PHITS)
* through a specified output port, to reflect its previous
* value.
*/
void caHandler::updateContention(int output) {
assert(output < g_ports);
assert(output >= 0);
if (g_misrouting_trigger == FILTERED)
m_accumulated_contention[output] = (1 - g_filtered_contention_regressive_coefficient)
* m_contention_counter[output]
+ (g_filtered_contention_regressive_coefficient * m_accumulated_contention[output]);
m_contention_counter[output] = 0;
m_potential_contention_counter[output] = 0;
}
bool caHandler::isThereGlobalContention(flitModule * flit) {
assert(g_misrouting_trigger == CA_REMOTE || g_misrouting_trigger == HYBRID_REMOTE);
if (flit->sourceSW != m_sw->label) return false; /* Only check global contention for injection packets */
if (flit->destGroup == m_sw->hPos) return false; /* Only allow misroute if destination is outside current group */
int contention = 0;
for (int sw = 0; sw < g_a_routers_per_group; sw++) {
contention += m_partial_counter[sw][flit->destGroup];
}
#if DEBUG
if (contention >= g_contention_aware_global_th * g_flit_size && m_sw->label == 0)
cout << "Contention in group " << flit->destGroup << " at cycle " << g_cycle << endl;
#endif
return (contention >= g_contention_aware_global_th * g_flit_size);
}
/*
* Returns true if there is contention in the link and therefore
* a misroute should be considered. Its behavior depends on the
* contention aware misrouting trigger employed.
*/
bool caHandler::isThereContention(int output) {
bool contention = false;
contention = getContention(output) >= g_contention_aware_th * g_flit_size;
if (g_contention_aware_local_th >= 0 && output < g_global_router_links_offset)
contention = getContention(output) >= g_contention_aware_local_th * g_flit_size;
return contention;
}
/*
* Set of functions to increase contention when a packet
* enters a queue, and to decrease it when the packet is
* egressed. Implies NOT increasing contention at header.
*/
void caHandler::increaseContention(int port) {
m_contention_counter[port] += g_flit_size;
assert(m_contention_counter[port] >= 0);
assert(!g_increaseContentionAtHeader);
}
void caHandler::decreaseContention(int port) {
m_contention_counter[port] -= g_flit_size;
assert(m_contention_counter[port] >= 0);
assert(!g_increaseContentionAtHeader);
}