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
|
// Copyright (c) 2009-2013, Tor M. Aamodt, Dongdong Li, Ali Bakhoda
// The University of British Columbia
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright notice, this
// list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
// Neither the name of The University of British Columbia nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <sstream>
#include <fstream>
#include <limits>
#include "gputrafficmanager.hpp"
#include "interconnect_interface.hpp"
#include "globals.hpp"
GPUTrafficManager::GPUTrafficManager( const Configuration &config, const vector<Network *> &net)
:TrafficManager(config, net)
{
// The total simulations equal to number of kernels
_total_sims = 0;
_input_queue.resize(_subnets);
for ( int subnet = 0; subnet < _subnets; ++subnet) {
_input_queue[subnet].resize(_nodes);
for ( int node = 0; node < _nodes; ++node ) {
_input_queue[subnet][node].resize(_classes);
}
}
}
GPUTrafficManager::~GPUTrafficManager()
{
}
void GPUTrafficManager::Init()
{
_time = 0;
_sim_state = running;
_ClearStats( );
}
void GPUTrafficManager::_RetireFlit( Flit *f, int dest )
{
_deadlock_timer = 0;
assert(_total_in_flight_flits[f->cl].count(f->id) > 0);
_total_in_flight_flits[f->cl].erase(f->id);
if(f->record) {
assert(_measured_in_flight_flits[f->cl].count(f->id) > 0);
_measured_in_flight_flits[f->cl].erase(f->id);
}
if ( f->watch ) {
*gWatchOut << GetSimTime() << " | "
<< "node" << dest << " | "
<< "Retiring flit " << f->id
<< " (packet " << f->pid
<< ", src = " << f->src
<< ", dest = " << f->dest
<< ", hops = " << f->hops
<< ", flat = " << f->atime - f->itime
<< ")." << endl;
}
if ( f->head && ( f->dest != dest ) ) {
ostringstream err;
err << "Flit " << f->id << " arrived at incorrect output " << dest;
Error( err.str( ) );
}
if((_slowest_flit[f->cl] < 0) ||
(_flat_stats[f->cl]->Max() < (f->atime - f->itime)))
_slowest_flit[f->cl] = f->id;
_flat_stats[f->cl]->AddSample( f->atime - f->itime);
if(_pair_stats){
_pair_flat[f->cl][f->src*_nodes+dest]->AddSample( f->atime - f->itime );
}
if ( f->tail ) {
Flit * head;
if(f->head) {
head = f;
} else {
map<int, Flit *>::iterator iter = _retired_packets[f->cl].find(f->pid);
assert(iter != _retired_packets[f->cl].end());
head = iter->second;
_retired_packets[f->cl].erase(iter);
assert(head->head);
assert(f->pid == head->pid);
}
if ( f->watch ) {
*gWatchOut << GetSimTime() << " | "
<< "node" << dest << " | "
<< "Retiring packet " << f->pid
<< " (plat = " << f->atime - head->ctime
<< ", nlat = " << f->atime - head->itime
<< ", frag = " << (f->atime - head->atime) - (f->id - head->id) // NB: In the spirit of solving problems using ugly hacks, we compute the packet length by taking advantage of the fact that the IDs of flits within a packet are contiguous.
<< ", src = " << head->src
<< ", dest = " << head->dest
<< ")." << endl;
}
// GPGPUSim: Memory will handle reply, do not need this
#if 0
//code the source of request, look carefully, its tricky ;)
if (f->type == Flit::READ_REQUEST || f->type == Flit::WRITE_REQUEST) {
PacketReplyInfo* rinfo = PacketReplyInfo::New();
rinfo->source = f->src;
rinfo->time = f->atime;
rinfo->record = f->record;
rinfo->type = f->type;
_repliesPending[dest].push_back(rinfo);
} else {
if(f->type == Flit::READ_REPLY || f->type == Flit::WRITE_REPLY ){
_requestsOutstanding[dest]--;
} else if(f->type == Flit::ANY_TYPE) {
_requestsOutstanding[f->src]--;
}
}
#endif
if(f->type == Flit::READ_REPLY || f->type == Flit::WRITE_REPLY ){
_requestsOutstanding[dest]--;
} else if(f->type == Flit::ANY_TYPE) {
ostringstream err;
err << "Flit " << f->id << " cannot be ANY_TYPE" ;
Error( err.str( ) );
}
// Only record statistics once per packet (at tail)
// and based on the simulation state
if ( ( _sim_state == warming_up ) || f->record ) {
_hop_stats[f->cl]->AddSample( f->hops );
if((_slowest_packet[f->cl] < 0) ||
(_plat_stats[f->cl]->Max() < (f->atime - head->itime)))
_slowest_packet[f->cl] = f->pid;
_plat_stats[f->cl]->AddSample( f->atime - head->ctime);
_nlat_stats[f->cl]->AddSample( f->atime - head->itime);
_frag_stats[f->cl]->AddSample( (f->atime - head->atime) - (f->id - head->id) );
if(_pair_stats){
_pair_plat[f->cl][f->src*_nodes+dest]->AddSample( f->atime - head->ctime );
_pair_nlat[f->cl][f->src*_nodes+dest]->AddSample( f->atime - head->itime );
}
}
if(f != head) {
head->Free();
}
}
if(f->head && !f->tail) {
_retired_packets[f->cl].insert(make_pair(f->pid, f));
} else {
f->Free();
}
}
int GPUTrafficManager::_IssuePacket( int source, int cl )
{
return 0;
}
//TODO: Remove stype?
void GPUTrafficManager::_GeneratePacket(int source, int stype, int cl, int time, int subnet, int packet_size, const Flit::FlitType& packet_type, void* const data, int dest)
{
assert(stype!=0);
// Flit::FlitType packet_type = Flit::ANY_TYPE;
int size = packet_size; //input size
int pid = _cur_pid++;
assert(_cur_pid);
int packet_destination = dest;
bool record = false;
bool watch = gWatchOut && (_packets_to_watch.count(pid) > 0);
// In GPGPUSim, the core specified the packet_type and size
#if 0
if(_use_read_write[cl]){
if(stype > 0) {
if (stype == 1) {
packet_type = Flit::READ_REQUEST;
size = _read_request_size[cl];
} else if (stype == 2) {
packet_type = Flit::WRITE_REQUEST;
size = _write_request_size[cl];
} else {
ostringstream err;
err << "Invalid packet type: " << packet_type;
Error( err.str( ) );
}
} else {
PacketReplyInfo* rinfo = _repliesPending[source].front();
if (rinfo->type == Flit::READ_REQUEST) {//read reply
size = _read_reply_size[cl];
packet_type = Flit::READ_REPLY;
} else if(rinfo->type == Flit::WRITE_REQUEST) { //write reply
size = _write_reply_size[cl];
packet_type = Flit::WRITE_REPLY;
} else {
ostringstream err;
err << "Invalid packet type: " << rinfo->type;
Error( err.str( ) );
}
packet_destination = rinfo->source;
time = rinfo->time;
record = rinfo->record;
_repliesPending[source].pop_front();
rinfo->Free();
}
}
#endif
if ((packet_destination <0) || (packet_destination >= _nodes)) {
ostringstream err;
err << "Incorrect packet destination " << packet_destination
<< " for stype " << packet_type;
Error( err.str( ) );
}
if ( ( _sim_state == running ) ||
( ( _sim_state == draining ) && ( time < _drain_time ) ) ) {
record = _measure_stats[cl];
}
int subnetwork = subnet;
// ((packet_type == Flit::ANY_TYPE) ?
// RandomInt(_subnets-1) :
// _subnet[packet_type]);
if ( watch ) {
*gWatchOut << GetSimTime() << " | "
<< "node" << source << " | "
<< "Enqueuing packet " << pid
<< " at time " << time
<< "." << endl;
}
for ( int i = 0; i < size; ++i ) {
Flit * f = Flit::New();
f->id = _cur_id++;
assert(_cur_id);
f->pid = pid;
f->watch = watch | (gWatchOut && (_flits_to_watch.count(f->id) > 0));
f->subnetwork = subnetwork;
f->src = source;
f->ctime = time;
f->record = record;
f->cl = cl;
f->data = data;
_total_in_flight_flits[f->cl].insert(make_pair(f->id, f));
if(record) {
_measured_in_flight_flits[f->cl].insert(make_pair(f->id, f));
}
if(gTrace){
cout<<"New Flit "<<f->src<<endl;
}
f->type = packet_type;
if ( i == 0 ) { // Head flit
f->head = true;
//packets are only generated to nodes smaller or equal to limit
f->dest = packet_destination;
} else {
f->head = false;
f->dest = -1;
}
switch( _pri_type ) {
case class_based:
f->pri = _class_priority[cl];
assert(f->pri >= 0);
break;
case age_based:
f->pri = numeric_limits<int>::max() - time;
assert(f->pri >= 0);
break;
case sequence_based:
f->pri = numeric_limits<int>::max() - _packet_seq_no[source];
assert(f->pri >= 0);
break;
default:
f->pri = 0;
}
if ( i == ( size - 1 ) ) { // Tail flit
f->tail = true;
} else {
f->tail = false;
}
f->vc = -1;
if ( f->watch ) {
*gWatchOut << GetSimTime() << " | "
<< "node" << source << " | "
<< "Enqueuing flit " << f->id
<< " (packet " << f->pid
<< ") at time " << time
<< "." << endl;
}
_input_queue[subnet][source][cl].push_back( f );
}
}
void GPUTrafficManager::_Step()
{
bool flits_in_flight = false;
for(int c = 0; c < _classes; ++c) {
flits_in_flight |= !_total_in_flight_flits[c].empty();
}
if(flits_in_flight && (_deadlock_timer++ >= _deadlock_warn_timeout)){
_deadlock_timer = 0;
cout << "WARNING: Possible network deadlock.\n";
}
vector<map<int, Flit *> > flits(_subnets);
for ( int subnet = 0; subnet < _subnets; ++subnet ) {
for ( int n = 0; n < _nodes; ++n ) {
Flit * const f = _net[subnet]->ReadFlit( n );
if ( f ) {
if(f->watch) {
*gWatchOut << GetSimTime() << " | "
<< "node" << n << " | "
<< "Ejecting flit " << f->id
<< " (packet " << f->pid << ")"
<< " from VC " << f->vc
<< "." << endl;
}
g_icnt_interface->WriteOutBuffer(subnet, n, f);
}
g_icnt_interface->Transfer2BoundaryBuffer(subnet, n);
Flit* const ejected_flit = g_icnt_interface->GetEjectedFlit(subnet, n);
if (ejected_flit) {
if(ejected_flit->head)
assert(ejected_flit->dest == n);
if(ejected_flit->watch) {
*gWatchOut << GetSimTime() << " | "
<< "node" << n << " | "
<< "Ejected flit " << ejected_flit->id
<< " (packet " << ejected_flit->pid
<< " VC " << ejected_flit->vc << ")"
<< "from ejection buffer." << endl;
}
flits[subnet].insert(make_pair(n, ejected_flit));
if((_sim_state == warming_up) || (_sim_state == running)) {
++_accepted_flits[ejected_flit->cl][n];
if(ejected_flit->tail) {
++_accepted_packets[ejected_flit->cl][n];
}
}
}
// Processing the credit From the network
Credit * const c = _net[subnet]->ReadCredit( n );
if ( c ) {
#ifdef TRACK_FLOWS
for(set<int>::const_iterator iter = c->vc.begin(); iter != c->vc.end(); ++iter) {
int const vc = *iter;
assert(!_outstanding_classes[n][subnet][vc].empty());
int cl = _outstanding_classes[n][subnet][vc].front();
_outstanding_classes[n][subnet][vc].pop();
assert(_outstanding_credits[cl][subnet][n] > 0);
--_outstanding_credits[cl][subnet][n];
}
#endif
_buf_states[n][subnet]->ProcessCredit(c);
c->Free();
}
}
_net[subnet]->ReadInputs( );
}
// GPGPUSim will generate/inject packets from interconnection interface
#if 0
if ( !_empty_network ) {
_Inject();
}
#endif
for(int subnet = 0; subnet < _subnets; ++subnet) {
for(int n = 0; n < _nodes; ++n) {
Flit * f = NULL;
BufferState * const dest_buf = _buf_states[n][subnet];
int const last_class = _last_class[n][subnet];
int class_limit = _classes;
if(_hold_switch_for_packet) {
list<Flit *> const & pp = _input_queue[subnet][n][last_class];
if(!pp.empty() && !pp.front()->head &&
!dest_buf->IsFullFor(pp.front()->vc)) {
f = pp.front();
assert(f->vc == _last_vc[n][subnet][last_class]);
// if we're holding the connection, we don't need to check that class
// again in the for loop
--class_limit;
}
}
for(int i = 1; i <= class_limit; ++i) {
int const c = (last_class + i) % _classes;
list<Flit *> const & pp = _input_queue[subnet][n][c];
if(pp.empty()) {
continue;
}
Flit * const cf = pp.front();
assert(cf);
assert(cf->cl == c);
assert(cf->subnetwork == subnet);
if(f && (f->pri >= cf->pri)) {
continue;
}
if(cf->head && cf->vc == -1) { // Find first available VC
OutputSet route_set;
_rf(NULL, cf, -1, &route_set, true);
set<OutputSet::sSetElement> const & os = route_set.GetSet();
assert(os.size() == 1);
OutputSet::sSetElement const & se = *os.begin();
assert(se.output_port == -1);
int vc_start = se.vc_start;
int vc_end = se.vc_end;
int vc_count = vc_end - vc_start + 1;
if(_noq) {
assert(_lookahead_routing);
const FlitChannel * inject = _net[subnet]->GetInject(n);
const Router * router = inject->GetSink();
assert(router);
int in_channel = inject->GetSinkPort();
// NOTE: Because the lookahead is not for injection, but for the
// first hop, we have to temporarily set cf's VC to be non-negative
// in order to avoid seting of an assertion in the routing function.
cf->vc = vc_start;
_rf(router, cf, in_channel, &cf->la_route_set, false);
cf->vc = -1;
if(cf->watch) {
*gWatchOut << GetSimTime() << " | "
<< "node" << n << " | "
<< "Generating lookahead routing info for flit " << cf->id
<< " (NOQ)." << endl;
}
set<OutputSet::sSetElement> const sl = cf->la_route_set.GetSet();
assert(sl.size() == 1);
int next_output = sl.begin()->output_port;
vc_count /= router->NumOutputs();
vc_start += next_output * vc_count;
vc_end = vc_start + vc_count - 1;
assert(vc_start >= se.vc_start && vc_start <= se.vc_end);
assert(vc_end >= se.vc_start && vc_end <= se.vc_end);
assert(vc_start <= vc_end);
}
if(cf->watch) {
*gWatchOut << GetSimTime() << " | " << FullName() << " | "
<< "Finding output VC for flit " << cf->id
<< ":" << endl;
}
for(int i = 1; i <= vc_count; ++i) {
int const lvc = _last_vc[n][subnet][c];
int const vc =
(lvc < vc_start || lvc > vc_end) ?
vc_start :
(vc_start + (lvc - vc_start + i) % vc_count);
assert((vc >= vc_start) && (vc <= vc_end));
if(!dest_buf->IsAvailableFor(vc)) {
if(cf->watch) {
*gWatchOut << GetSimTime() << " | " << FullName() << " | "
<< " Output VC " << vc << " is busy." << endl;
}
} else {
if(dest_buf->IsFullFor(vc)) {
if(cf->watch) {
*gWatchOut << GetSimTime() << " | " << FullName() << " | "
<< " Output VC " << vc << " is full." << endl;
}
} else {
if(cf->watch) {
*gWatchOut << GetSimTime() << " | " << FullName() << " | "
<< " Selected output VC " << vc << "." << endl;
}
cf->vc = vc;
break;
}
}
}
}
if(cf->vc == -1) {
if(cf->watch) {
*gWatchOut << GetSimTime() << " | " << FullName() << " | "
<< "No output VC found for flit " << cf->id
<< "." << endl;
}
} else {
if(dest_buf->IsFullFor(cf->vc)) {
if(cf->watch) {
*gWatchOut << GetSimTime() << " | " << FullName() << " | "
<< "Selected output VC " << cf->vc
<< " is full for flit " << cf->id
<< "." << endl;
}
} else {
f = cf;
}
}
}
if(f) {
assert(f->subnetwork == subnet);
int const c = f->cl;
if(f->head) {
if (_lookahead_routing) {
if(!_noq) {
const FlitChannel * inject = _net[subnet]->GetInject(n);
const Router * router = inject->GetSink();
assert(router);
int in_channel = inject->GetSinkPort();
_rf(router, f, in_channel, &f->la_route_set, false);
if(f->watch) {
*gWatchOut << GetSimTime() << " | "
<< "node" << n << " | "
<< "Generating lookahead routing info for flit " << f->id
<< "." << endl;
}
} else if(f->watch) {
*gWatchOut << GetSimTime() << " | "
<< "node" << n << " | "
<< "Already generated lookahead routing info for flit " << f->id
<< " (NOQ)." << endl;
}
} else {
f->la_route_set.Clear();
}
dest_buf->TakeBuffer(f->vc);
_last_vc[n][subnet][c] = f->vc;
}
_last_class[n][subnet] = c;
_input_queue[subnet][n][c].pop_front();
#ifdef TRACK_FLOWS
++_outstanding_credits[c][subnet][n];
_outstanding_classes[n][subnet][f->vc].push(c);
#endif
dest_buf->SendingFlit(f);
if(_pri_type == network_age_based) {
f->pri = numeric_limits<int>::max() - _time;
assert(f->pri >= 0);
}
if(f->watch) {
*gWatchOut << GetSimTime() << " | "
<< "node" << n << " | "
<< "Injecting flit " << f->id
<< " into subnet " << subnet
<< " at time " << _time
<< " with priority " << f->pri
<< "." << endl;
}
f->itime = _time;
// Pass VC "back"
if(!_input_queue[subnet][n][c].empty() && !f->tail) {
Flit * const nf = _input_queue[subnet][n][c].front();
nf->vc = f->vc;
}
if((_sim_state == warming_up) || (_sim_state == running)) {
++_sent_flits[c][n];
if(f->head) {
++_sent_packets[c][n];
}
}
#ifdef TRACK_FLOWS
++_injected_flits[c][n];
#endif
_net[subnet]->WriteFlit(f, n);
}
}
}
//Send the credit To the network
for(int subnet = 0; subnet < _subnets; ++subnet) {
for(int n = 0; n < _nodes; ++n) {
map<int, Flit *>::const_iterator iter = flits[subnet].find(n);
if(iter != flits[subnet].end()) {
Flit * const f = iter->second;
f->atime = _time;
if(f->watch) {
*gWatchOut << GetSimTime() << " | "
<< "node" << n << " | "
<< "Injecting credit for VC " << f->vc
<< " into subnet " << subnet
<< "." << endl;
}
Credit * const c = Credit::New();
c->vc.insert(f->vc);
_net[subnet]->WriteCredit(c, n);
#ifdef TRACK_FLOWS
++_ejected_flits[f->cl][n];
#endif
_RetireFlit(f, n);
}
}
flits[subnet].clear();
// _InteralStep here
_net[subnet]->Evaluate( );
_net[subnet]->WriteOutputs( );
}
++_time;
assert(_time);
if(gTrace){
cout<<"TIME "<<_time<<endl;
}
}
|