-
Notifications
You must be signed in to change notification settings - Fork 1
/
router.cpp
298 lines (252 loc) · 9.87 KB
/
router.cpp
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
/************************************************************************
Author: Jingcao Hu ([email protected])
File name: router.cpp
Date Created: <Tue Oct 14 13:40:48 2003>
Last Modified: <Thu Dec 16 18:50:06 2004>
Description:
************************************************************************/
#include "common.hpp"
#include "router.hpp"
#include "msg.hpp"
#include "global_val.hpp"
#include <iostream>
Router::Router(pNetwork n, Position p) : Addressee(p),
sw(param.n_of_switch_ports),
arbiter(param.arbitration_delay,
param.n_of_switch_ports) {
if (param.extreme_verbose)
cout << "[I] Building router for location " << pos << endl;
network = n;
in_ports.clear();
out_ports.clear();
clear_contention();
for (unsigned int i=0; i<network->get_num_of_ports(); i++) {
Direction d = (Direction) i;
pIn_port a_in_port = new In_port(this, d);
pOut_port a_out_port = new Out_port(this, d);
in_ports.push_back(a_in_port);
out_ports.push_back(a_out_port);
}
arbiter.bind(this);
sw.bind(this);
// bind switch ends with the input/output channels
assert(sw.get_size() == in_ports.size() * in_ports[0]->get_size());
assert(sw.get_size() == out_ports.size() * out_ports[0]->get_size());
int index = 0;
for (vector<pIn_port>::iterator iter=in_ports.begin(); iter<in_ports.end(); iter++) {
for (unsigned int i=0; i<(*iter)->get_size(); i++) {
pSw_input_end an_end = sw.get_input_end(index++);
pInput_channel a_chan = (pInput_channel) (*iter)->get_channel(i);
an_end->bind(a_chan);
a_chan->bind(an_end);
}
}
index = 0;
for (vector<pOut_port>::iterator iter=out_ports.begin(); iter<out_ports.end(); iter++) {
for (unsigned int i=0; i<(*iter)->get_size(); i++) {
pSw_output_end an_end = sw.get_output_end(index++);
pOutput_channel a_chan = (pOutput_channel) (*iter)->get_channel(i);
an_end->bind(a_chan);
a_chan->bind(an_end);
}
}
// UYO monitor the routers
if (param.dump_input_buffer||param.dump_output_buffer)
this->set_monitor();
else
monitor_router = 0;
// set up the switch's ebit metric using default value
sw.set_ebit(param.xbar_ebit);
// create orion power module
if (param.orion_power.use_orion_power_model)
power_module = new Power_module(param.n_of_ports-param.n_of_extra_links);
else
power_module = NULL;
}
Router::~Router() {
if (param.extreme_verbose)
cout << "[I] Deleting router for location " << pos << endl;
for (vector<pIn_port>::iterator iter=in_ports.begin(); iter<in_ports.end(); iter++)
delete (*iter);
for (vector<pOut_port>::iterator iter=out_ports.begin(); iter<out_ports.end(); iter++)
delete (*iter);
if (power_module)
delete power_module;
}
void Router::clear_contention(void){
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
contention[i][j]=false;
}
void Router::receivecontention(int srcRouter,int dir,bool con){
contention[srcRouter][dir]=con;
if(con==true)
int k=0;
}
void Router::set_monitor(void) { //UYO
monitor_router = 1;
for (vector<pIn_port>::iterator iter=in_ports.begin(); iter<in_ports.end(); iter++) {
for (unsigned int i=0; i<(*iter)->get_size(); i++) {
pInput_channel a_channel = (pInput_channel) (*iter)->get_channel(i);
a_channel->monitoring=1;
}
}
//begin rtg
for (vector<pOut_port>::iterator iter=out_ports.begin(); iter<out_ports.end(); iter++) {
for (unsigned int i=0; i<(*iter)->get_size(); i++) {
pOutput_channel a_channel = (pOutput_channel) (*iter)->get_channel(i);
a_channel->monitoring=1;
}
}
//end rtg
}
void Router::update_prediction(void) { // UYO:
// First read the input_channels of the down_stream routers to see how much they can accept
vector <Direction> connection_status;
unsigned int common_pool = 0;
Direction dir = (Direction) 0;
if (param.availability_thresh > 0) {
for (vector<pOut_port>::iterator iter=out_ports.begin(); iter<out_ports.end(); iter++) {
dir = (*iter)->get_direction();
if (dir != local) {
for (unsigned int i=0; i<(*iter)->get_size(); i++) {
pOutput_channel myOut_chan = (pOutput_channel) (*iter)->get_channel(i); // Output channel
pRouter downRouter = get_router(dir);
if (downRouter == NULL)
break;
if (reverse(dir) == invalid_dir)
break;
pInput_channel downIn_chan = downRouter->get_in_channel(reverse(dir));
unsigned int acceptable = downIn_chan->get_can_accept();
// printf("Acceptable is:%d\n",acceptable);
myOut_chan->update_can_output(acceptable);
Direction conn_input = arbiter.get_current_granted_channel_dir(dir);
if (conn_input != invalid_dir) {
connection_status.push_back(conn_input);
}
else
common_pool += acceptable;
}
} // if (dir != local)
} // for (vector...
} // ~(param.basic_cont)
// Knowing how much each out_channel can output, we now update how much each input channel can accept
for (vector<pIn_port>::iterator iter=in_ports.begin(); iter<in_ports.end(); iter++) {
for (unsigned int i=0; i<(*iter)->get_size(); i++) {
dir = (*iter)->get_direction();
pInput_channel a_channel = (pInput_channel) (*iter)->get_channel(i);
unsigned int empty_slots = a_channel->num_of_empty_buffer_slots();
for (vector<Direction>::iterator iter1=connection_status.begin(); iter1<connection_status.end(); iter1++) {
if ((*iter1)==dir) { // Input channel dir is connected to (*iter)
pOutput_channel connect_to = a_channel->get_receiver();
empty_slots += connect_to->get_can_output();
break;
}
}
unsigned int from_common_pool = (unsigned int) common_pool/(param. n_of_ports-1);
a_channel->update_can_accept(empty_slots+from_common_pool);
dir = (Direction) (dir+1);
}
} // outer for
} // update_prediction
void Router::set_power_module(int s) { // UYO
power_module = NULL;
this->power_module = new Power_module(s);
}
void Router::set_input_channel_buffer_size(int s) {
for (vector<pIn_port>::iterator iter=in_ports.begin(); iter<in_ports.end(); iter++)
(*iter)->set_channel_buffer_size(s);
}
void Router::set_input_channel_buffer_size(int s, Direction dir) {
if (dir == local) {
cerr << "[W] Overriding local input buffer depth is not allowed. Action skipped." << endl;
return;
}
pIn_port p = get_in_port(dir);
p->set_channel_buffer_size(s);
}
void Router::set_output_channel_buffer_size(int s) {
for (vector<pOut_port>::iterator iter=out_ports.begin(); iter<out_ports.end(); iter++)
(*iter)->set_channel_buffer_size(s);
}
void Router::set_routing_scheme(Routing_scheme rs) {
for (vector<pIn_port>::iterator iter=in_ports.begin(); iter<in_ports.end(); iter++)
(*iter)->set_routing_scheme(rs);
}
void Router::set_routing_table(char * table_string) { //UYO
for(vector<pIn_port>::iterator iter=in_ports.begin(); iter<in_ports.end(); iter++)
(*iter)->set_routing_table(table_string);
/* cout <<"@NODE " << (this->pos.x)*param.n_of_rows+(this->pos.y) <<"\n";
cout <<"routing_scheme table ";
for (int i=0; i<strlen(table_string); i++)
cout << table_string[i]; //<<" ";
cout <<"\n"; */
}
// find the index-th input virtual channel in the dir direction of the router
pInput_channel Router::get_in_channel(Direction dir, int index) {
pIn_port a_port = in_ports[dir];
return ((pInput_channel) a_port->get_channel(index));
}
pOutput_channel Router::get_out_channel(Direction dir, int index) {
pOut_port a_port = out_ports[dir];
return ((pOutput_channel) a_port->get_channel(index));
}
pOutput_channel Router::get_link_user_channel(Direction dir) {
pOut_port a_port = out_ports[dir];
printf("You're at:"); print_position(); printf("Direction is: %d\n",dir);
return ((pOutput_channel) a_port->get_link_user_channel());
}
// return the router which is on the dir-side of the router
// return 0 if there does not exist a router (for instance,
// at the edge of the mesh
pRouter Router::get_router(Direction dir) {
Position a_pos = pos;
switch (dir) {
case north:
a_pos.y ++;
break;
case south:
a_pos.y --;
break;
case east:
a_pos.x ++;
break;
case west:
a_pos.x --;
break;
case local:
return this;
default:
return 0;
}
if (network->get_topology() == mesh) {
if (a_pos.x >= network->get_num_of_cols() || a_pos.x < 0)
return 0;
if (a_pos.y >= network->get_num_of_rows() || a_pos.y < 0)
return 0;
return network->get_router(a_pos);
}
else if (network->get_topology() == torus) {
// increase to avoid negative numbers
a_pos.x += network->get_num_of_cols();
a_pos.y += network->get_num_of_rows();
a_pos.x = a_pos.x % network->get_num_of_cols();
a_pos.y = a_pos.y % network->get_num_of_rows();
return network->get_router(a_pos);
}
else {
cerr << ERRO_ARCH_NOT_SUPPORTED;
return 0;
}
}
void Router::in_ports_tick(void) {
for (vector<pIn_port>::iterator iter=in_ports.begin(); iter<in_ports.end(); iter++)
(*iter)->tick();
}
void Router::out_ports_tick(void) {
for (vector<pOut_port>::iterator iter=out_ports.begin(); iter<out_ports.end(); iter++)
(*iter)->tick();
}
void Router::arbiter_tick(void) {
arbiter.tick();
}