/
TProcessingElement.cpp
executable file
·399 lines (319 loc) · 10.4 KB
/
TProcessingElement.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
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
/*****************************************************************************
TProcessingElement.cpp -- Processing Element (PE) implementation
*****************************************************************************/
/* Copyright 2005-2007
Maurizio Palesi <mpalesi@diit.unict.it>
Fabrizio Fazzino <fabrizio.fazzino@diit.unict.it>
Davide Patti <dpatti@diit.unict.it>
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "TProcessingElement.h"
//---------------------------------------------------------------------------
int TProcessingElement::randInt(int min, int max)
{
return min + (int)((double)(max-min+1) * rand()/(RAND_MAX+1.0));
}
//---------------------------------------------------------------------------
void TProcessingElement::rxProcess()
{
if(reset.read())
{
ack_rx.write(0);
current_level_rx = 0;
}
else
{
if(req_rx.read()==1-current_level_rx)
{
TFlit flit_tmp = flit_rx.read();
if(TGlobalParams::verbose_mode > VERBOSE_OFF)
{
cout << sc_simulation_time() << ": ProcessingElement[" << local_id << "] \033[22;32mRECEIVING\033[22;30m " << flit_tmp << endl;
}
current_level_rx = 1-current_level_rx; // Negate the old value for Alternating Bit Protocol (ABP)
}
ack_rx.write(current_level_rx);
}
}
//---------------------------------------------------------------------------
void TProcessingElement::txProcess()
{
if(reset.read())
{
req_tx.write(0);
current_level_tx = 0;
transmittedAtPreviousCycle = false;
}
else
{
TPacket packet;
if (canShot(packet))
{
packet_queue.push(packet);
transmittedAtPreviousCycle = true;
}
else
transmittedAtPreviousCycle = false;
if(ack_tx.read() == current_level_tx)
{
if(!packet_queue.empty())
{
TFlit flit = nextFlit(); // Generate a new flit
if(TGlobalParams::verbose_mode > VERBOSE_OFF)
{
cout << sc_time_stamp().to_double()/1000 << ": ProcessingElement[" << local_id << "] \033[22;31mSENDING\033[22;30m " << flit << endl;
}
flit_tx->write(flit); // Send the generated flit
current_level_tx = 1-current_level_tx; // Negate the old value for Alternating Bit Protocol (ABP)
req_tx.write(current_level_tx);
}
}
}
}
//---------------------------------------------------------------------------
TFlit TProcessingElement::nextFlit()
{
TFlit flit;
TPacket packet = packet_queue.front();
flit.src_id = packet.src_id;
flit.dst_id = packet.dst_id;
flit.timestamp = packet.timestamp;
flit.sequence_no = packet.size - packet.flit_left;
flit.hop_no = 0;
// flit.payload = DEFAULT_PAYLOAD;
if(packet.size == packet.flit_left)
flit.flit_type = FLIT_TYPE_HEAD;
else if(packet.flit_left == 1)
flit.flit_type = FLIT_TYPE_TAIL;
else
flit.flit_type = FLIT_TYPE_BODY;
packet_queue.front().flit_left--;
if(packet_queue.front().flit_left == 0)
packet_queue.pop();
return flit;
}
//---------------------------------------------------------------------------
bool TProcessingElement::canShot(TPacket& packet)
{
bool shot;
double threshold;
if (TGlobalParams::traffic_distribution != TRAFFIC_TABLE_BASED)
{
if (!transmittedAtPreviousCycle)
threshold = TGlobalParams::packet_injection_rate;
else
threshold = TGlobalParams::probability_of_retransmission;
shot = (((double)rand())/RAND_MAX < threshold);
if (shot)
{
switch(TGlobalParams::traffic_distribution)
{
case TRAFFIC_RANDOM:
packet = trafficRandom();
break;
case TRAFFIC_TRANSPOSE1:
packet = trafficTranspose1();
break;
case TRAFFIC_TRANSPOSE2:
packet = trafficTranspose2();
break;
case TRAFFIC_BIT_REVERSAL:
packet = trafficBitReversal();
break;
case TRAFFIC_SHUFFLE:
packet = trafficShuffle();
break;
case TRAFFIC_BUTTERFLY:
packet = trafficButterfly();
break;
default:
assert(false);
}
}
}
else
{ // Table based communication traffic
if (never_transmit)
return false;
double now = sc_time_stamp().to_double()/1000;
bool use_pir = (transmittedAtPreviousCycle == false);
vector<pair<int,double> > dst_prob;
double threshold = traffic_table->getCumulativePirPor(local_id, (int)now, use_pir, dst_prob);
double prob = (double)rand()/RAND_MAX;
shot = (prob < threshold);
if (shot)
{
for (unsigned int i=0; i<dst_prob.size(); i++)
{
if (prob < dst_prob[i].second)
{
packet.make(local_id, dst_prob[i].first, now, getRandomSize());
break;
}
}
}
}
return shot;
}
//---------------------------------------------------------------------------
TPacket TProcessingElement::trafficRandom()
{
TPacket p;
p.src_id = local_id;
double rnd = rand()/(double)RAND_MAX;
double range_start = 0.0;
//cout << "\n " << sc_time_stamp().to_double()/1000 << " PE " << local_id << " rnd = " << rnd << endl;
int max_id = (TGlobalParams::mesh_dim_x * TGlobalParams::mesh_dim_y)-1;
// Random destination distribution
do
{
p.dst_id = randInt(0, max_id);
// check for hotspot destination
for (uint i = 0; i<TGlobalParams::hotspots.size(); i++)
{
//cout << sc_time_stamp().to_double()/1000 << " PE " << local_id << " Checking node " << TGlobalParams::hotspots[i].first << " with P = " << TGlobalParams::hotspots[i].second << endl;
if (rnd>=range_start && rnd < range_start + TGlobalParams::hotspots[i].second)
{
if (local_id != TGlobalParams::hotspots[i].first)
{
//cout << sc_time_stamp().to_double()/1000 << " PE " << local_id <<" That is ! " << endl;
p.dst_id = TGlobalParams::hotspots[i].first;
}
break;
}
else
range_start+=TGlobalParams::hotspots[i].second; // try next
}
} while(p.dst_id==p.src_id);
p.timestamp = sc_time_stamp().to_double()/1000;
p.size = p.flit_left = getRandomSize();
return p;
}
//---------------------------------------------------------------------------
TPacket TProcessingElement::trafficTranspose1()
{
TPacket p;
p.src_id = local_id;
TCoord src,dst;
// Transpose 1 destination distribution
src.x = id2Coord(p.src_id).x;
src.y = id2Coord(p.src_id).y;
dst.x = TGlobalParams::mesh_dim_x-1-src.y;
dst.y = TGlobalParams::mesh_dim_y-1-src.x;
fixRanges(src, dst);
p.dst_id = coord2Id(dst);
p.timestamp = sc_time_stamp().to_double()/1000;
p.size = p.flit_left = getRandomSize();
return p;
}
//---------------------------------------------------------------------------
TPacket TProcessingElement::trafficTranspose2()
{
TPacket p;
p.src_id = local_id;
TCoord src,dst;
// Transpose 2 destination distribution
src.x = id2Coord(p.src_id).x;
src.y = id2Coord(p.src_id).y;
dst.x = src.y;
dst.y = src.x;
fixRanges(src, dst);
p.dst_id = coord2Id(dst);
p.timestamp = sc_time_stamp().to_double()/1000;
p.size = p.flit_left = getRandomSize();
return p;
}
//---------------------------------------------------------------------------
void TProcessingElement::setBit(int &x, int w, int v)
{
int mask = 1 << w;
if (v == 1)
x = x | mask;
else if (v == 0)
x = x & ~mask;
else
assert(false);
}
//---------------------------------------------------------------------------
int TProcessingElement::getBit(int x, int w)
{
return (x >> w) & 1;
}
//---------------------------------------------------------------------------
inline double TProcessingElement::log2ceil(double x)
{
return ceil(log(x)/log(2.0));
}
//---------------------------------------------------------------------------
TPacket TProcessingElement::trafficBitReversal()
{
int nbits = (int)log2ceil((double)(TGlobalParams::mesh_dim_x*TGlobalParams::mesh_dim_y));
int dnode = 0;
for (int i=0; i<nbits; i++)
setBit(dnode, i, getBit(local_id, nbits-i-1));
TPacket p;
p.src_id = local_id;
p.dst_id = dnode;
p.timestamp = sc_time_stamp().to_double()/1000;
p.size = p.flit_left = getRandomSize();
return p;
}
//---------------------------------------------------------------------------
TPacket TProcessingElement::trafficShuffle()
{
int nbits = (int)log2ceil((double)(TGlobalParams::mesh_dim_x*TGlobalParams::mesh_dim_y));
int dnode = 0;
for (int i=0; i<nbits-1; i++)
setBit(dnode, i+1, getBit(local_id, i));
setBit(dnode, 0, getBit(local_id, nbits-1));
TPacket p;
p.src_id = local_id;
p.dst_id = dnode;
p.timestamp = sc_time_stamp().to_double()/1000;
p.size = p.flit_left = getRandomSize();
return p;
}
//---------------------------------------------------------------------------
TPacket TProcessingElement::trafficButterfly()
{
int nbits = (int)log2ceil((double)(TGlobalParams::mesh_dim_x*TGlobalParams::mesh_dim_y));
int dnode = 0;
for (int i=1; i<nbits-1; i++)
setBit(dnode, i, getBit(local_id, i));
setBit(dnode, 0, getBit(local_id, nbits-1));
setBit(dnode, nbits-1, getBit(local_id, 0));
TPacket p;
p.src_id = local_id;
p.dst_id = dnode;
p.timestamp = sc_time_stamp().to_double()/1000;
p.size = p.flit_left = getRandomSize();
return p;
}
//---------------------------------------------------------------------------
void TProcessingElement::fixRanges(const TCoord src, TCoord& dst)
{
// Fix ranges
if(dst.x<0) dst.x=0;
if(dst.y<0) dst.y=0;
if(dst.x>=TGlobalParams::mesh_dim_x) dst.x=TGlobalParams::mesh_dim_x-1;
if(dst.y>=TGlobalParams::mesh_dim_y) dst.y=TGlobalParams::mesh_dim_y-1;
}
//---------------------------------------------------------------------------
int TProcessingElement::getRandomSize()
{
return randInt(TGlobalParams::min_packet_size,
TGlobalParams::max_packet_size);
}
//---------------------------------------------------------------------------