-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.cpp
More file actions
1505 lines (1324 loc) · 52.5 KB
/
router.cpp
File metadata and controls
1505 lines (1324 loc) · 52.5 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "router.h"
#include "sim.h"
#include "queue.h"
#include "stb_ds.h"
#include <stdarg.h>
#include <stdio.h>
#include <assert.h>
#include <random>
#include <climits>
TrafficDesc::TrafficDesc(int terminal_count)
: type(TRF_UNIFORM_RANDOM), dests(terminal_count)
{
}
RandomGenerator::RandomGenerator(int terminal_count, double mean_interval)
: def(), rd(), uni_dist(0, terminal_count - 1),
exp_dist(1.0 / mean_interval)
{
// TODO: seed?
}
template <typename T> T &Router::get_device() const
{
if (deterministic) {
return rand_gen.def;
} else {
return rand_gen.rd;
}
}
void debugf(Router *r, const char *fmt, ...)
{
if (r->verbose) {
char s[IDSTRLEN];
printf("[@%3ld] [%s] ", curr_time(r->eventq), id_str(r->id, s));
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
}
void warnf(Router *r, const char *fmt, ...)
{
char s[IDSTRLEN];
printf("[@%3ld] [%s] ", curr_time(r->eventq), id_str(r->id, s));
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
Event tick_event_from_id(Id id)
{
return (Event){id, router_tick};
}
Channel::Channel(EventQueue *eq, long dl, const Connection conn)
: conn(conn), eventq(eq), delay(dl), buf_credit()
{
queue_init(buf, dl + CHANNEL_SLACK);
}
Channel::~Channel()
{
while (!queue_empty(buf)) {
TimedFlit front = queue_front(buf);
delete front.flit;
queue_pop(buf);
}
while (!buf_credit.empty()) {
TimedCredit front = buf_credit.front();
delete front.credit;
buf_credit.pop_front();
}
queue_free(buf);
}
void channel_put(Channel *ch, Flit *flit)
{
TimedFlit tf = {curr_time(ch->eventq) + ch->delay, flit};
assert(!queue_full(ch->buf));
queue_put(ch->buf, tf);
reschedule(ch->eventq, ch->delay, tick_event_from_id(ch->conn.dst.id));
ch->load_count += queue_len(ch->buf);
}
void channel_put_credit(Channel *ch, Credit *credit)
{
TimedCredit tc = {curr_time(ch->eventq) + ch->delay, credit};
ch->buf_credit.push_back(tc);
reschedule(ch->eventq, ch->delay, tick_event_from_id(ch->conn.src.id));
}
Flit *channel_get(Channel *ch)
{
TimedFlit front = queue_front(ch->buf);
if (!queue_empty(ch->buf) && curr_time(ch->eventq) >= front.time) {
assert(curr_time(ch->eventq) == front.time && "stale flit!");
Flit *flit = front.flit;
queue_pop(ch->buf);
return flit;
} else {
return NULL;
}
}
Credit *channel_get_credit(Channel *ch)
{
TimedCredit front = ch->buf_credit.front();
if (!ch->buf_credit.empty() && curr_time(ch->eventq) >= front.time) {
assert(curr_time(ch->eventq) == front.time && "stale flit!");
Credit *credit = front.credit;
ch->buf_credit.pop_front();
return credit;
} else {
return NULL;
}
}
void print_conn(const char *name, Connection conn)
{
printf("%s: %d.%d.%d -> %d.%d.%d\n", name, conn.src.id.type,
conn.src.id.value, conn.src.port, conn.dst.id.type,
conn.dst.id.value, conn.dst.port);
}
Connection conn_find_forward(Topology *t, RouterPortPair out_port)
{
ptrdiff_t idx = hmgeti(t->forward_hash, out_port);
if (idx == -1)
return not_connected;
else
return t->forward_hash[idx].value;
}
Connection conn_find_reverse(Topology *t, RouterPortPair in_port)
{
ptrdiff_t idx = hmgeti(t->reverse_hash, in_port);
if (idx == -1)
return not_connected;
else
return t->reverse_hash[idx].value;
}
Flit::Flit(enum FlitType t, int vc, int src, int dst, PacketId pid, long flitnum)
: type(t), vc_num(vc), packet_id(pid), flitnum(flitnum)
{
route_info.src = src;
route_info.dst = dst;
}
void flit_destroy(Flit *flit)
{
free(flit);
}
// 's' should be at least IDSTRLEN large.
char *flit_str(const Flit *flit, char *s)
{
// FIXME: Rename IDSTRLEN!
if (flit) {
snprintf(s, IDSTRLEN, "{s%d.p%ld.f%ld}", flit->route_info.src,
flit->packet_id.id, flit->flitnum);
} else {
*s = '\0';
}
return s;
}
char *globalstate_str(enum GlobalState state, char *s)
{
switch (state) {
case STATE_IDLE:
snprintf(s, IDSTRLEN, "I");
break;
case STATE_ROUTING:
snprintf(s, IDSTRLEN, "R");
break;
case STATE_VCWAIT:
snprintf(s, IDSTRLEN, "V");
break;
case STATE_ACTIVE:
snprintf(s, IDSTRLEN, "A");
break;
case STATE_CREDWAIT:
snprintf(s, IDSTRLEN, "C");
break;
}
return s;
}
InputUnit::InputUnit(int vc_count, int bufsize)
{
vcs.reserve(vc_count);
for (int i = 0; i < vc_count; i++) {
vcs.emplace_back(bufsize);
}
}
InputUnit::VC::VC(int bufsize)
{
buf = NULL;
queue_init(buf, bufsize * 2);
}
InputUnit::VC::~VC()
{
while (!queue_empty(buf)) {
Flit *flit = queue_front(buf);
delete flit;
queue_pop(buf);
}
queue_free(buf);
if (st_ready) {
delete st_ready;
}
}
OutputUnit::OutputUnit(int vc_count, int bufsize)
{
vcs.reserve(vc_count);
for (int i = 0; i < vc_count; i++) {
vcs.emplace_back(bufsize);
}
}
OutputUnit::VC::VC(int bufsize) : credit_count(bufsize)
{
// buf_credit = NULL;
// queue_init(buf_credit, bufsize * 2); // FIXME: unnecessarily big.
}
OutputUnit::VC::~VC()
{
// queue_free(buf_credit);
}
Router::Router(Sim &sim, EventQueue *eq, Stat *st, bool verbose, Id id,
int radix, int vc_count, TopoDesc td, TrafficDesc trd,
RandomGenerator &rg, long packet_len, Channel **in_chs,
Channel **out_chs, long input_buf_size)
: sim(sim), eventq(eq), stat(st), verbose(verbose), id(id), radix(radix),
vc_count(vc_count), top_desc(td), traffic_desc(trd), rand_gen(rg),
packet_len(packet_len), input_buf_size(input_buf_size),
src_last_grant_output(0), dst_last_grant_input(0),
va_last_grant_input(radix * vc_count, 0),
va_last_grant_output(radix * vc_count, 0),
sa_last_grant_input(radix * vc_count, 0), sa_last_grant_output(radix, 0)
{
// Can only segregate VCs into classes if we do have multiple VCs.
vc_class_count = (vc_count > 1) ? 2 : 1;
// Copy channel list
input_channels = NULL;
output_channels = NULL;
for (long i = 0; i < arrlen(in_chs); i++)
arrput(input_channels, in_chs[i]);
for (long i = 0; i < arrlen(out_chs); i++)
arrput(output_channels, out_chs[i]);
// Source queues are supposed to be infinite in size, but since our
// queue implementation does not support dynamic extension, let's just
// assume a fixed, arbitrary massive size for its queue. Nonurgent TODO.
source_queue = NULL;
if (is_src(id)) {
queue_init(source_queue, 10000);
}
input_units.reserve(radix);
output_units.reserve(radix);
for (int port = 0; port < radix; port++) {
input_units.emplace_back(vc_count, input_buf_size);
output_units.emplace_back(vc_count, input_buf_size);
}
if (is_src(id) || is_dst(id)) {
assert(input_units.size() == 1);
assert(output_units.size() == 1);
// There are no route computation stages for terminal nodes, so set the
// routed ports and allocated VCs for each IU/OU statically here.
for (int i = 0; i < vc_count; i++) {
input_units[0].vcs[i].route_port = TERMINAL_PORT;
input_units[0].vcs[i].output_vc = 0; // unnecessary?
output_units[0].vcs[i].input_port = TERMINAL_PORT;
output_units[0].vcs[i].input_vc = 0; // unnnecessary
}
}
}
Router::~Router()
{
if (source_queue) {
while (!queue_empty(source_queue)) {
Flit *flit = queue_front(source_queue);
delete flit;
queue_pop(source_queue);
}
queue_free(source_queue);
}
arrfree(input_channels);
arrfree(output_channels);
}
void router_reschedule(Router *r)
{
if (r->reschedule_next_tick) {
reschedule(r->eventq, 1, tick_event_from_id(r->id));
}
}
// Compute route on a ring that is laid along a single dimension.
// Expects that src_id and dst_id is on the same ring.
// Appends computed route after 'path'. Does NOT put the final routing to the
// terminal node.
static void source_route_compute_dimension(Router *r, TopoDesc td,
int src_id, int dst_id,
int direction,
std::vector<int> &path)
{
int total = td.k;
int src_id_xyz = torus_id_xyz_get(src_id, td.k, direction);
int dst_id_xyz = torus_id_xyz_get(dst_id, td.k, direction);
int cw_dist = (dst_id_xyz - src_id_xyz + total) % total;
if ((total % 2) == 0 && cw_dist == (total / 2)) {
int dice = r->rand_gen.uni_dist(r->rand_gen.rd);
int to_larger = (dice % 2 == 0) ? 1 : 0;
// Adaptive routing
// int first_hop_id = r->output_channels[TERMINAL_PORT]->conn.dst.id.value;
// Router *first_hop = r->sim.routers[first_hop_id].get();
// int credit_for_larger =
// first_hop->output_units[get_output_port(direction, 1)].credit_count;
// int credit_for_smaller =
// first_hop->output_units[get_output_port(direction, 0)].credit_count;
// // printf("credit_for_larger=%d, smaller=%d\n", credit_for_larger,
// // credit_for_smaller);
// to_larger = 1;
// if (credit_for_smaller > credit_for_larger) {
// to_larger = 0;
// }
// printf("adaptive routed to %d\n", get_output_port(direction, to_larger));
// FIXME VC vs. Wormhole
// to_larger = 1;
for (int i = 0; i < cw_dist; i++) {
path.push_back(get_output_port(direction, to_larger));
}
} else if (cw_dist <= (total / 2)) {
// Clockwise
for (int i = 0; i < cw_dist; i++) {
path.push_back(get_output_port(direction, 1));
}
} else {
// Counterclockwise
// TODO: if CW == CCW, pick random
for (int i = 0; i < total - cw_dist; i++) {
path.push_back(get_output_port(direction, 0));
}
}
}
// Source-side all-in-one route computation.
// Returns an stb array containing the series of routed output ports.
std::vector<int> source_route_compute(Router *r, TopoDesc td, int src_id, int dst_id)
{
std::vector<int> path{};
// Dimension-order routing. Order is XYZ.
int last_src_id = src_id;
for (int dir = 0; dir < td.r; dir++) {
int interim_id = torus_align_id(td.k, last_src_id, dst_id, dir);
// printf("%s: from %d to %d\n", __func__, last_src_id, interim_id);
source_route_compute_dimension(r, td, last_src_id, interim_id, dir, path);
last_src_id = interim_id;
}
// Enter the final destination node.
path.push_back(TERMINAL_PORT);
return path;
}
// Tick a router. This function does all of the work that a router has to
// process in a single cycle, i.e. all pipeline stages and statistics update.
// This simplifies the event system by streamlining event types into a single
// one, the 'tick event', and letting us to only have to consider the
// chronological order between them.
void router_tick(Router *r)
{
// Make sure this router has not been already ticked in this cycle.
if (curr_time(r->eventq) == r->last_tick) {
// debugf(r, "WARN: double tick! curr_time=%ld, last_tick=%ld\n",
// curr_time(r->eventq), r->last_tick);
r->stat->double_tick_count++;
return;
}
r->reschedule_next_tick = false;
// Different tick actions for different types of node.
if (is_src(r->id)) {
source_generate(r);
// Source nodes also needs to manage credit in order to send flits at
// the right time.
credit_update(r);
fetch_credit(r);
} else if (is_dst(r->id)) {
destination_consume(r);
fetch_flit(r);
} else {
// Process each pipeline stage.
// Stages are processed in reverse dependency order to prevent coherence
// bug. E.g., if a flit succeeds in route_compute() and advances to the
// VA stage, and then vc_alloc() is called, it would then get processed
// again in the same cycle.
switch_traverse(r);
switch_alloc(r);
vc_alloc(r);
route_compute(r);
credit_update(r);
fetch_credit(r);
fetch_flit(r);
// Self-tick autonomously unless all input ports are empty.
// FIXME: redundant?
// int empty = 1;
// for (int i = 0; i < radix; i++) {
// if (!input_units[i].buf.empty()) {
// empty = 0;
// break;
// }
// }
// if (!empty) {
// reschedule_next_tick = 1;
// }
}
// Update the global state of each input/output unit.
update_states(r);
// Do the rescheduling at here once to prevent flooding the event queue.
router_reschedule(r);
r->last_tick = curr_time(r->eventq);
}
///
/// Pipeline stages
///
void source_generate(Router *r)
{
// Before entering the source queue.
if (!queue_full(r->source_queue) &&
(r->eventq->curr_time() >= r->sg.next_packet_start ||
!r->sg.packet_finished)) {
//
// Flit generation.
//
int dest = -1;
if (r->traffic_desc.type == TRF_UNIFORM_RANDOM) {
while (true) {
dest = r->rand_gen.uni_dist(r->rand_gen.rd);
// Retry until an ID different than mine comes up.
if (dest != r->id.value) {
break;
}
}
debugf(r, "Uniform random: dest=%ld\n", dest);
} else if (r->traffic_desc.type == TRF_DESIGNATED) {
dest = r->traffic_desc.dests[r->id.value];
} else {
assert(false);
}
PacketId packet_id{r->id.value, r->sg.packet_counter};
Flit *flit = new Flit{FLIT_BODY, 0, r->id.value, dest,
packet_id, r->sg.flitnum};
if (r->sg.packet_finished) {
// Head flit
//
if (r->eventq->curr_time() != r->sg.next_packet_start) {
debugf(r,
"WARN: Head flit not generated at the scheduled "
"time=%ld!\n",
r->sg.next_packet_start);
}
//
// Source-side route computation.
//
flit->type = FLIT_HEAD;
flit->route_info.path = source_route_compute(
r, r->top_desc, flit->route_info.src, flit->route_info.dst);
assert(flit->route_info.path.size() > 0);
// Hop count: exclude the last hop to terminal.
r->stat->hop_count_sum += (flit->route_info.path.size() - 1);
r->stat->packet_gen_count++;
if (r->verbose) {
debugf(r, "Source route computation: %d -> %d : {",
flit->route_info.src, flit->route_info.dst);
for (size_t i = 0; i < flit->route_info.path.size(); i++) {
printf("%d,", flit->route_info.path[i]);
}
printf("}\n");
}
r->sg.flitnum++;
// Set the time the next packet is generated.
//
// Fixed interval:
// r->sg.next_packet_start = r->eventq->curr_time() + r->packet_len;
//
// Poisson process:
double next_packet_start_frac =
static_cast<double>(r->eventq->curr_time()) +
static_cast<double>(r->packet_len) +
r->rand_gen.exp_dist(r->rand_gen.rd);
r->sg.next_packet_start = std::lround(next_packet_start_frac);
// if (r->sg.next_packet_start < r->eventq->curr_time() + r->packet_len) {
// printf("nah\n");
// r->sg.next_packet_start = r->eventq->curr_time() + r->packet_len;
// }
// debugf(r, "scheduling at %ld\n", r->sg.next_packet_start);
schedule(r->eventq, r->sg.next_packet_start,
tick_event_from_id(r->id));
// Record packet generation time.
PacketTimestamp ts{.gen = r->eventq->curr_time(), .arr = -1};
auto result = r->stat->packet_ledger.insert({flit->packet_id, ts});
assert(result.second);
r->sg.packet_finished = false;
} else if (r->sg.flitnum == r->packet_len - 1) {
// Tail flit
flit->type = FLIT_TAIL;
r->sg.flitnum = 0;
r->sg.packet_finished = true;
r->sg.packet_counter++;
} else {
// Body flit
r->sg.flitnum++;
}
if (!r->sg.packet_finished) {
r->reschedule_next_tick = true;
}
queue_put(r->source_queue, flit);
char s[IDSTRLEN];
debugf(r, "Flit generated: %s\n", flit_str(flit, s));
debugf(r, "Source queue len=%ld\n", queue_len(r->source_queue));
} else if (queue_full(r->source_queue)) {
debugf(r, "WARN: source queue full!\n");
}
// After exiting the source queue.
if (!queue_empty(r->source_queue)) {
Flit *ready_flit = queue_front(r->source_queue);
int ovc_num = r->src_last_grant_output;
if (ready_flit->type == FLIT_HEAD) {
// Deadlock avoidance with datelines: always start at the VCs with
// class 0.
const int ovc_class = 0; /* always */
// Round-robin VC arbitration
int vc_per_class = (r->vc_count / r->vc_class_count);
int ovc_in_class = (r->src_last_grant_output + 1) % vc_per_class;
for (int i = 0; i < r->vc_class_count; i++) {
ovc_num = ovc_class * vc_per_class + ovc_in_class;
OutputUnit::VC &ovc = r->output_units[TERMINAL_PORT].vcs[ovc_num];
// Select the first one that has credits.
if (ovc.credit_count > 0) {
r->src_last_grant_output = ovc_num;
break;
}
ovc_in_class = (ovc_in_class + 1) % vc_per_class;
}
}
OutputUnit::VC &ovc = r->output_units[TERMINAL_PORT].vcs[ovc_num];
if (ovc.credit_count > 0) {
queue_pop(r->source_queue);
// Make sure to mark the VC number in the flit.
ready_flit->vc_num = ovc_num;
Channel *och = r->output_channels[TERMINAL_PORT];
channel_put(och, ready_flit);
debugf(r, "Source credit decrement, credit=%d->%d\n",
ovc.credit_count, ovc.credit_count - 1);
ovc.credit_count--;
assert(ovc.credit_count >= 0);
r->flit_depart_count++;
char s[IDSTRLEN], s2[IDSTRLEN];
auto dst_pair = och->conn.dst;
debugf(r, "Flit sent via VC%d: %s, to {%s, %d}\n", ovc_num,
flit_str(ready_flit, s), id_str(dst_pair.id, s2),
dst_pair.port);
// Infinitely generate flits.
// TODO: Set and control generation rate.
r->reschedule_next_tick = 1;
} else {
debugf(r, "Credit stall!\n");
}
}
}
void destination_consume(Router *r)
{
// Round-robin input VC selection. Destination node should never block, so
// keep searching for a non-empty input VC in the single cycle.
InputUnit::VC *ivc = NULL;
char s[IDSTRLEN], s2[IDSTRLEN];
bool has_nonempty_ivc = false;
int ivc_num = (r->dst_last_grant_input + 1) % r->vc_count;
for (int i = 0; i < r->vc_count; i++) {
ivc = &r->input_units[TERMINAL_PORT].vcs[ivc_num];
if (!queue_empty(ivc->buf)) {
has_nonempty_ivc = true;
r->dst_last_grant_input = ivc_num;
break;
}
ivc_num = (ivc_num + 1) % r->vc_count;
}
if (!has_nonempty_ivc) {
// Ideally, the destination node should have never even been scheduled
// in this case.
return;
}
assert(!queue_empty(ivc->buf));
Flit *flit = queue_front(ivc->buf);
if (flit->type == FLIT_HEAD) {
// First, check if this flit is correctly destined to this node.
assert(flit->route_info.dst == r->id.value);
// Record packet arrival time.
// debugf(r, "Finding packet ID=%ld,%ld\n", flit->packet_id.src,
// flit->packet_id.id);
auto f = r->stat->packet_ledger.find(flit->packet_id);
if (f == r->stat->packet_ledger.end()) {
printf("src=%ld, id=%ld not found\n", flit->packet_id.src,
flit->packet_id.id);
}
assert(f != r->stat->packet_ledger.end() &&
"Packet not recorded upon generation!");
f->second.arr = r->eventq->curr_time();
long arr = f->second.arr;
long gen = f->second.gen;
long latency = arr - gen;
// debugf(r, "Deleting packet ID=%ld,%ld\n",
// flit->packet_id.src, flit->packet_id.id);
r->stat->packet_ledger.erase(flit->packet_id);
r->stat->latency_sum += latency;
r->stat->packet_arrive_count++;
debugf(r,
"Packet arrived: %s, latency=%ld (arr=%ld, gen=%ld). "
"mapsize=%ld\n",
flit_str(flit, s), latency, arr, gen,
r->stat->packet_ledger.size());
}
debugf(r, "Destination buf size=%zd\n", queue_len(ivc->buf));
debugf(r, "Flit arrived via VC%d: %s\n", ivc_num, flit_str(flit, s));
r->flit_arrive_count++;
queue_pop(ivc->buf);
assert(queue_empty(ivc->buf));
Channel *ich = r->input_channels[TERMINAL_PORT];
std::vector<long> vc_nums{ivc_num};
// false: VC vs. Wormhole showcase mode
if (true || (r->id.value != 22)) {
Credit *credit = new Credit{vc_nums};
channel_put_credit(ich, credit);
RouterPortPair src_pair = ich->conn.src;
RouterPortPair dst_pair = ich->conn.dst;
debugf(r, "Credit sent via VC%d from {%s, %d} to {%s, %d}\n", ivc_num,
id_str(dst_pair.id, s), dst_pair.port, id_str(src_pair.id, s2),
src_pair.port);
}
// Self-tick autonomously unless all input ports are empty.
r->reschedule_next_tick = true;
delete flit;
}
void fetch_flit(Router *r)
{
for (int iport = 0; iport < r->radix; iport++) {
Channel *ich = r->input_channels[iport];
Flit *flit = channel_get(ich);
if (!flit) {
continue;
}
InputUnit::VC &ivc = r->input_units[iport].vcs[flit->vc_num];
char s[IDSTRLEN];
debugf(r, "Fetched flit %s via VC%d, buf[%d][%d].size()=%zd\n",
flit_str(flit, s), flit->vc_num, iport, flit->vc_num,
queue_len(ivc.buf));
// If the buffer was empty, this is the only place to kickstart the
// pipeline.
if (queue_empty(ivc.buf)) {
// debugf(r, "fetch_flit: buf was empty\n");
// If the input unit state was also idle (empty != idle!), set
// the stage to RC.
if (ivc.next_global == STATE_IDLE) {
// Idle -> RC transition
ivc.next_global = STATE_ROUTING;
ivc.stage = PIPELINE_RC;
}
r->reschedule_next_tick = true;
}
assert(!queue_full(ivc.buf));
queue_put(ivc.buf, flit);
assert(queue_len(ivc.buf) <= r->input_buf_size &&
"Input buffer overflow!");
}
}
void fetch_credit(Router *r)
{
for (int oport = 0; oport < r->radix; oport++) {
Channel *och = r->output_channels[oport];
Credit *credit = channel_get_credit(och);
if (credit) {
debugf(r, "Fetched credit, oport=%d\n", oport);
for (auto vc_num : credit->vc_nums) {
OutputUnit::VC &ovc = r->output_units[oport].vcs[vc_num];
// In any time, there should be at most 1 credit in the buffer.
assert(!ovc.buf_credit);
ovc.buf_credit = true;
r->reschedule_next_tick = true;
}
delete credit;
}
}
}
void credit_update(Router *r)
{
for (int oport = 0; oport < r->radix; oport++) {
for (int ovc_num = 0; ovc_num < r->vc_count; ovc_num++) {
OutputUnit::VC &ovc = r->output_units[oport].vcs[ovc_num];
if (ovc.buf_credit) {
debugf(r, "CU: credit=%d->%d (oport=%d)\n",
ovc.credit_count, ovc.credit_count + 1, oport);
assert(ovc.input_port != -1);
assert(ovc.input_vc != -1);
// Upon credit update, the input and output unit receiving this
// credit may or may not be in the CreditWait state. If they
// are, make sure to switch them back to the active state so
// that they can proceed in the SA stage.
//
// This can otherwise be implemented in the SA stage itself,
// switching the stage to Active and simultaneously commencing
// to the switch allocation. However, this implementation seems
// to defeat the purpose of the CreditWait stage. This
// implementation is what I think of as a more natural one.
InputUnit::VC &ivc =
r->input_units[ovc.input_port].vcs[ovc.input_vc];
if (ovc.credit_count == 0) {
if (ovc.next_global == STATE_CREDWAIT) {
assert(ivc.next_global == STATE_CREDWAIT);
ivc.next_global = STATE_ACTIVE;
ovc.next_global = STATE_ACTIVE;
}
r->reschedule_next_tick = true;
// debugf(r, "credit update with kickstart! (iport=%d)\n",
// ovc.input_port);
} else {
// debugf(r, "credit update, but no kickstart
// (credit=%d)\n",
// ovc.credit_count);
}
ovc.credit_count++;
// queue_pop(ovc.buf_credit);
// assert(queue_empty(ovc.buf_credit));
ovc.buf_credit = false;
} else {
// dbg() << "No credit update, oport=" << oport << std::endl;
}
}
}
}
void route_compute(Router *r)
{
for (int iport = 0; iport < r->radix; iport++) {
for (int ivc_num = 0; ivc_num < r->vc_count; ivc_num++) {
InputUnit::VC &ivc = r->input_units[iport].vcs[ivc_num];
if (ivc.global == STATE_ROUTING) {
assert(!queue_empty(ivc.buf));
Flit *flit = queue_front(ivc.buf);
assert(flit->type == FLIT_HEAD);
assert(flit->route_info.idx < flit->route_info.path.size());
ivc.route_port = flit->route_info.path[flit->route_info.idx];
// ivc.output_vc will be set in the VA stage.
char s[IDSTRLEN];
debugf(r, "RC: success for %s (idx=%zu, oport=%d)\n",
flit_str(flit, s), flit->route_info.idx, ivc.route_port);
flit->route_info.idx++;
// RC -> VA transition
ivc.next_global = STATE_VCWAIT;
ivc.stage = PIPELINE_VA;
r->reschedule_next_tick = true;
}
}
}
}
#if 0
// This function expects the given output VC to be in the Idle state.
int vc_arbit_round_robin(Router *r, int out_port)
{
// Debug: print contenders
{
std::vector<int> v;
for (int i = 0; i < r->radix; i++) {
InputUnit *iu = &r->input_units[i];
InputUnit::VC *ivc = &iu->vcs[0 /*FIXME*/];
if (ivc->global == STATE_VCWAIT && ivc->route_port == out_port)
v.push_back(i);
}
if (!v.empty()) {
debugf(r, "VA: competing for oport %d from iports {", out_port);
for (size_t i = 0; i < v.size(); i++)
printf("%d,", v[i]);
printf("}\n");
}
}
int iport = (r->va_last_grant_output[out_port] + 1) % r->radix;
for (int i = 0; i < r->radix; i++) {
InputUnit *iu = &r->input_units[iport];
InputUnit::VC *ivc = &iu->vcs[0 /*FIXME*/];
if (ivc->global == STATE_VCWAIT && ivc->route_port == out_port) {
// XXX: is VA stage and VCWait state the same?
assert(ivc->stage == PIPELINE_VA);
r->va_last_grant_output[out_port] = iport;
return iport;
}
iport = (iport + 1) % r->radix;
}
// Indicates that there was no request for this VC.
return -1;
}
// This function expects the given output VC to be in the Active state.
int sa_arbit_round_robin(Router *r, int out_port)
{
int iport = (r->sa_last_grant_output[out_port] + 1) % r->radix;
for (int i = 0; i < r->radix; i++) {
InputUnit *iu = &r->input_units[iport];
InputUnit::VC *ivc = &iu->vcs[0 /*FIXME*/];
// We should check for queue non-emptiness, as it is possible for active
// input units to have no flits in them because of contention in the
// upstream router.
if (ivc->stage == PIPELINE_SA && ivc->route_port == out_port &&
ivc->global == STATE_ACTIVE && !queue_empty(ivc->buf)) {
// debugf(r, "SA: granted oport %d to iport %d\n", out_port, iport);
r->sa_last_grant_output[out_port] = iport;
return iport;
} else if (ivc->stage == PIPELINE_SA && ivc->route_port == out_port &&
ivc->global == STATE_CREDWAIT) {
debugf(r, "Credit stall! port=%d\n", ivc->route_port);
}
iport = (iport + 1) % r->radix;
}
// Indicates that there was no request for this VC.
return -1;
}
#endif
static size_t alloc_vector_pos(size_t grant_size, size_t input_vc,
size_t output_vc)
{
return (input_vc * grant_size) + output_vc;
}
// Returns the index of the granted input.
// Accepts a large concatenation of all (xVC) request and grant vectors, and
// modifies the grant vectors in-place.
//
// 'req_size': number of requests/grants waiting for allocation.
// 'grant_size': number of resources available for grant.
// 'which': for which req (if is_input_stage == 1) or grant (if is_input_stage
// == 0) should I arbitrate at this iteration?
// 'is_input_stage': true of this is input arbitration, false if output
// arbitration.
//
// Returns the VC (global index) whose request won the grant.
size_t round_robin_arbitration(size_t req_size, size_t grant_size, size_t which, bool is_input_stage,
size_t last_grant,
const std::vector<bool> &request_vectors,
std::vector<bool> &grant_vectors)
{
size_t arbit_size = is_input_stage ? grant_size : req_size;
// Clear the grant vector first.
for (size_t i = 0; i < arbit_size; i++) {
if (is_input_stage) {
grant_vectors[alloc_vector_pos(grant_size, which, i)] = false;
} else {
grant_vectors[alloc_vector_pos(grant_size, i, which)] = false;
}
}
size_t candidate = (last_grant + 1) % arbit_size;
for (size_t i = 0; i < arbit_size; i++) {
size_t cand_pos;
if (is_input_stage) {
cand_pos = alloc_vector_pos(grant_size, which, candidate);
} else {
cand_pos = alloc_vector_pos(grant_size, candidate, which);
}
if (request_vectors[cand_pos]) {
grant_vectors[cand_pos] = true;
return cand_pos;
}
candidate = (candidate + 1) % arbit_size;
}
// Indicates that there was no request.
return -1;
}
// Only makes sense for the output arbitration.
size_t age_based_arbitration(size_t req_size, size_t grant_size, size_t which,
bool is_input_stage, size_t last_grant,
const std::vector<bool> &request_vectors,
std::vector<bool> &grant_vectors,
const std::vector<long> &age_vector)
{
assert(!is_input_stage);
size_t arbit_size = is_input_stage ? grant_size : req_size;
// Clear the grant vector first.
for (size_t i = 0; i < arbit_size; i++) {
if (is_input_stage) {
grant_vectors[alloc_vector_pos(grant_size, which, i)] = false;
} else {
grant_vectors[alloc_vector_pos(grant_size, i, which)] = false;
}
}
// Find the request with oldest packet.
long min_birth = LONG_MAX;
size_t min_so_far = 0;
long howmany = 0;
for (size_t i = 0; i < arbit_size; i++) {
size_t cand_pos;
if (is_input_stage) {
cand_pos = alloc_vector_pos(grant_size, which, i);
} else {
cand_pos = alloc_vector_pos(grant_size, i, which);
}