-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.cpp
More file actions
283 lines (244 loc) · 9.34 KB
/
sim.cpp
File metadata and controls
283 lines (244 loc) · 9.34 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
#include "sim.h"
#include <stdio.h>
#include <assert.h>
void print_conn(const char *name, Connection conn);
Sim::Sim(bool verbose_mode, int debug_mode, Topology top, int terminal_count,
int router_count, int radix, int vc_count, double mean_interval, long input_buf_size)
: debug_mode(debug_mode), topology(top), traffic_desc(terminal_count),
rand_gen(terminal_count, mean_interval)
{
// Tornado pattern for 4-ring
// traffic_desc = {TRF_DESIGNATED, std::vector<int>(terminal_count)};
// traffic_desc.dests[0] = 2;
// traffic_desc.dests[1] = 3;
// traffic_desc.dests[2] = 0;
// traffic_desc.dests[3] = 1;
// VC vs. Wormhole pattern (6-ary 2-torus)
// traffic_desc = {TRF_DESIGNATED, std::vector<int>(terminal_count)};
// traffic_desc.dests[19] = 22;
// traffic_desc.dests[20] = 15;
channel_delay = 1; /* FIXME hardcoded */
packet_len = 4; /* FIXME hardcoded */
// Initialize the event system
eventq_init(&eventq);
// Initialize channels
channels.reserve(hmlen(top.forward_hash));
for (ptrdiff_t i = 0; i < hmlen(top.forward_hash); i++) {
Connection conn = top.forward_hash[i].value;
// printf("Found connection: %d.%d.%d -> %d.%d.%d\n", conn.src.id.type, conn.src.id.value,
// conn.src.port, conn.dst.id.type, conn.dst.id.value, conn.dst.port);
channels.emplace_back(&eventq, channel_delay, conn);
// channels.emplace_back(&eventq, channel_delay, conn);
}
channel_map = NULL;
for (size_t i = 0; i < channels.size(); i++) {
Channel *ch = &channels[i];
hmput(channel_map, ch->conn.uniq, ch);
}
// Initialize terminal nodes
for (int id = 0; id < terminal_count; id++) {
// Terminal nodes only have a single port. Also, destination nodes
// doesn't have output ports!
Channel **src_in_chs = NULL; // empty
Channel **src_out_chs = NULL;
Channel **dst_in_chs = NULL;
Channel **dst_out_chs = NULL; // empty
RouterPortPair src_rpp = {src_id(id), 0};
RouterPortPair dst_rpp = {dst_id(id), 0};
Connection src_conn = conn_find_forward(&top, src_rpp);
Connection dst_conn = conn_find_reverse(&top, dst_rpp);
assert(src_conn.src.port != -1 && "Source is not connected!");
assert(dst_conn.src.port != -1 && "Destination is not connected!");
long src_idx = hmgeti(channel_map, src_conn.uniq);
long dst_idx = hmgeti(channel_map, dst_conn.uniq);
assert(src_idx >= 0);
assert(dst_idx >= 0);
Channel *src_out_ch = channel_map[src_idx].value;
Channel *dst_in_ch = channel_map[dst_idx].value;
arrput(src_out_chs, src_out_ch);
arrput(dst_in_chs, dst_in_ch);
src_nodes.push_back(std::make_unique<Router>(
*this, &eventq, &stat, verbose_mode, src_id(id), 1, vc_count, top.desc,
traffic_desc, rand_gen, packet_len, src_in_chs, src_out_chs,
input_buf_size));
dst_nodes.push_back(std::make_unique<Router>(
*this, &eventq, &stat, verbose_mode, dst_id(id), 1, vc_count, top.desc,
traffic_desc, rand_gen, packet_len, dst_in_chs, dst_out_chs,
input_buf_size));
arrfree(src_in_chs);
arrfree(src_out_chs);
arrfree(dst_in_chs);
arrfree(dst_out_chs);
}
// Initialize router nodes
for (int id = 0; id < router_count; id++) {
Channel **in_chs = NULL;
Channel **out_chs = NULL;
for (int port = 0; port < radix; port++) {
RouterPortPair rpp = {rtr_id(id), port};
Connection output_conn = conn_find_forward(&top, rpp);
Connection input_conn = conn_find_reverse(&top, rpp);
assert(output_conn.src.port != -1);
assert(input_conn.src.port != -1);
long out_idx = hmgeti(channel_map, output_conn.uniq);
long in_idx = hmgeti(channel_map, input_conn.uniq);
assert(out_idx >= 0);
assert(in_idx >= 0);
Channel *out_ch = channel_map[out_idx].value;
Channel *in_ch = channel_map[in_idx].value;
arrput(out_chs, out_ch);
arrput(in_chs, in_ch);
}
routers.push_back(std::make_unique<Router>(
*this, &eventq, &stat, verbose_mode, rtr_id(id), radix, vc_count,
top.desc, traffic_desc, rand_gen, packet_len, in_chs, out_chs,
input_buf_size));
arrfree(in_chs);
arrfree(out_chs);
}
}
void sim_run_until(Sim *sim, long until)
{
long last_print_cycle = 0;
while (!eventq_empty(&sim->eventq)) {
// Terminate simulation if the specified time is expired
if (0 <= until && until < next_time(&sim->eventq)) {
break;
}
Event e = eventq_pop(&sim->eventq);
if (sim->eventq.curr_time() != last_print_cycle &&
sim->eventq.curr_time() % 100 == 0) {
printf("[@%3ld/%3ld]\n", sim->eventq.curr_time(), until);
last_print_cycle = sim->eventq.curr_time();
}
sim_process(sim, e);
}
}
// Returns 1 if the simulation is NOT terminated, 0 otherwise.
int sim_debug_step(Sim *sim)
{
char line[1024] = {0};
printf("(@%ld) > ", curr_time(&sim->eventq));
if (fgets(line, 100, stdin) == NULL)
return 0;
char *nl = strchr(line, '\n');
if (nl) *nl = '\0';
if (!strcmp(line, "q")) {
return 0;
} else if (strlen(line) == 0) {
return 1;
} else if (!strcmp(line, "n")) {
long until = curr_time(&sim->eventq);
sim_run_until(sim, until + 1);
return 1;
} else if (!strcmp(line, "p")) {
for (size_t i = 0; i < sim->routers.size(); i++) {
router_print_state(sim->routers[i].get());
}
return 1;
}
// Commands with arguments.
char *tok = strtok(line, " ");
if (!strcmp(tok, "c")) {
tok = strtok(NULL, " ");
if (!tok) {
printf("No argument given.\n");
return 1;
}
char *endptr;
long until = strtol(tok, &endptr, 10);
if (*endptr != '\0') {
printf("Invalid command.\n");
return 1;
}
sim_run_until(sim, until);
return 1;
}
// Command not understood.
printf("Unknown command.\n");
return 1;
}
// Run the simulator.
void sim_run(Sim *sim, long until)
{
if (sim->debug_mode) {
while (sim_debug_step(sim));
} else {
sim_run_until(sim, until);
}
}
void channel_xy_load(Sim *sim) {
// Determine the direction of this channel.
for (auto &ch : sim->channels) {
auto src = ch.conn.src.id;
auto dst = ch.conn.dst.id;
// Skip terminal channels.
if (is_src(src) || is_dst(src) || is_src(dst) || is_dst(dst)) {
continue;
}
int dimension = 0;
for (; dimension < sim->topology.desc.r; dimension++) {
int src_id = torus_id_xyz_get(ch.conn.src.id.value, sim->topology.desc.k, dimension);
int dst_id = torus_id_xyz_get(ch.conn.dst.id.value, sim->topology.desc.k, dimension);
if (src_id == dst_id) {
break;
}
}
printf("channel direction=%d, load=%ld\n", dimension, ch.load_count);
}
}
void sim_report(Sim *sim) {
char s[IDSTRLEN];
// Sample router for fetching topology info.
assert(!sim->routers.empty());
Router &r = *sim->routers[0].get();
printf("\n");
printf("==== SIMULATION RESULT ====\n");
printf("Topology: %d-ary %d-torus\n", sim->topology.desc.k, sim->topology.desc.r);
printf("Radix: %d\n", r.radix);
printf("# of VCs per channel: %d\n", r.vc_count);
printf("# of total cycle: %ld\n", curr_time(&sim->eventq));
printf("# of double ticks: %ld\n", sim->stat.double_tick_count);
printf("\n");
for (size_t i = 0; i < sim->src_nodes.size(); i++) {
Router *src = sim->src_nodes[i].get();
printf("[%s] ", id_str(src->id, s));
printf("# of flits departed: %ld\n", src->flit_depart_count);
}
for (size_t i = 0; i < sim->dst_nodes.size(); i++) {
Router *dst = sim->dst_nodes[i].get();
printf("[%s] ", id_str(dst->id, s));
printf("# of flits arrived: %ld\n", dst->flit_arrive_count);
}
printf("\n");
float interval_avg = static_cast<float>(curr_time(&sim->eventq)) /
(static_cast<float>(sim->stat.packet_gen_count) /
static_cast<float>(sim->src_nodes.size()));
printf("Average interval: %lf cycles\n", interval_avg);
float hop_count_avg = static_cast<float>(sim->stat.hop_count_sum) /
static_cast<float>(sim->stat.packet_gen_count);
printf("Average hop count: %lf hops\n", hop_count_avg);
float latency_avg = static_cast<float>(sim->stat.latency_sum) /
static_cast<float>(sim->stat.packet_arrive_count);
printf("Average latency: %lf\n", latency_avg);
// channel_xy_load(sim);
}
// Process an event.
void sim_process(Sim *sim, Event e)
{
if (is_src(e.id)) {
e.f(sim->src_nodes[e.id.value].get());
} else if (is_dst(e.id)) {
e.f(sim->dst_nodes[e.id.value].get());
} else if (is_rtr(e.id)) {
e.f(sim->routers[e.id.value].get());
} else {
assert(0);
}
}
void sim_destroy(Sim *sim)
{
hmfree(sim->channel_map);
// Stat
eventq_destroy(&sim->eventq);
}