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
|
#include "booksim.hpp"
#include <iostream>
#include <assert.h>
#include "allocator.hpp"
#include "maxsize.hpp"
#include "pim.hpp"
#include "islip.hpp"
#include "loa.hpp"
#include "wavefront.hpp"
#include "selalloc.hpp"
//==================================================
// Allocator base class
//==================================================
Allocator::Allocator( const Configuration &config,
Module *parent, const string& name,
int inputs, int outputs ) :
Module( parent, name ), _inputs( inputs ), _outputs( outputs )
{
_inmatch = new int [_inputs];
_outmatch = new int [_outputs];
_outmask = new int [_outputs];
for ( int out = 0; out < _outputs; ++out ) {
_outmask[out] = 0; // active
}
}
Allocator::~Allocator( )
{
delete [] _inmatch;
delete [] _outmatch;
delete [] _outmask;
}
void Allocator::_ClearMatching( )
{
for ( int i = 0; i < _inputs; ++i ) {
_inmatch[i] = -1;
}
for ( int j = 0; j < _outputs; ++j ) {
_outmatch[j] = -1;
}
}
int Allocator::OutputAssigned( int in ) const
{
assert( ( in >= 0 ) && ( in < _inputs ) );
return _inmatch[in];
}
int Allocator::InputAssigned( int out ) const
{
assert( ( out >= 0 ) && ( out < _outputs ) );
return _outmatch[out];
}
void Allocator::MaskOutput( int out, int mask )
{
assert( ( out >= 0 ) && ( out < _outputs ) );
_outmask[out] = mask;
}
//==================================================
// DenseAllocator
//==================================================
DenseAllocator::DenseAllocator( const Configuration &config,
Module *parent, const string& name,
int inputs, int outputs ) :
Allocator( config, parent, name, inputs, outputs )
{
_request = new sRequest * [_inputs];
for ( int i = 0; i < _inputs; ++i ) {
_request[i] = new sRequest [_outputs];
}
Clear( );
}
DenseAllocator::~DenseAllocator( )
{
for ( int i = 0; i < _inputs; ++i ) {
delete [] _request[i];
}
delete [] _request;
}
void DenseAllocator::Clear( )
{
for ( int i = 0; i < _inputs; ++i ) {
for ( int j = 0; j < _outputs; ++j ) {
_request[i][j].label = -1;
}
}
}
int DenseAllocator::ReadRequest( int in, int out ) const
{
assert( ( in >= 0 ) && ( in < _inputs ) &&
( out >= 0 ) && ( out < _outputs ) );
return _request[in][out].label;
}
bool DenseAllocator::ReadRequest( sRequest &req, int in, int out ) const
{
assert( ( in >= 0 ) && ( in < _inputs ) &&
( out >= 0 ) && ( out < _outputs ) );
req = _request[in][out];
return( req.label != -1 );
}
void DenseAllocator::AddRequest( int in, int out, int label,
int in_pri, int out_pri )
{
assert( ( in >= 0 ) && ( in < _inputs ) &&
( out >= 0 ) && ( out < _outputs ) );
_request[in][out].label = label;
_request[in][out].in_pri = in_pri;
_request[in][out].out_pri = out_pri;
}
void DenseAllocator::RemoveRequest( int in, int out, int label )
{
assert( ( in >= 0 ) && ( in < _inputs ) &&
( out >= 0 ) && ( out < _outputs ) );
_request[in][out].label = -1;
}
void DenseAllocator::PrintRequests( ) const
{
cout << "requests for " << _fullname << endl;
for ( int i = 0; i < _inputs; ++i ) {
for ( int j = 0; j < _outputs; ++j ) {
cout << ( _request[i][j].label != -1 ) << " ";
}
cout << endl;
}
cout << endl;
}
//==================================================
// SparseAllocator
//==================================================
SparseAllocator::SparseAllocator( const Configuration &config,
Module *parent, const string& name,
int inputs, int outputs ) :
Allocator( config, parent, name, inputs, outputs )
{
_in_req = new list<sRequest> [_inputs];
_out_req = new list<sRequest> [_outputs];
}
SparseAllocator::~SparseAllocator( )
{
delete [] _in_req;
delete [] _out_req;
}
void SparseAllocator::Clear( )
{
for ( int i = 0; i < _inputs; ++i ) {
_in_req[i].clear( );
}
for ( int j = 0; j < _outputs; ++j ) {
_out_req[j].clear( );
}
_in_occ.clear( );
_out_occ.clear( );
}
int SparseAllocator::ReadRequest( int in, int out ) const
{
sRequest r;
if ( ! ReadRequest( r, in, out ) ) {
r.label = -1;
}
return r.label;
}
bool SparseAllocator::ReadRequest( sRequest &req, int in, int out ) const
{
bool found;
assert( ( in >= 0 ) && ( in < _inputs ) &&
( out >= 0 ) && ( out < _outputs ) );
list<sRequest>::const_iterator match;
match = _in_req[in].begin( );
while ( ( match != _in_req[in].end( ) ) &&
( match->port != out ) ) {
match++;
}
if ( match != _in_req[in].end( ) ) {
req = *match;
found = true;
} else {
found = false;
}
return found;
}
void SparseAllocator::AddRequest( int in, int out, int label,
int in_pri, int out_pri )
{
assert( ( in >= 0 ) && ( in < _inputs ) &&
( out >= 0 ) && ( out < _outputs ) );
list<sRequest>::iterator insert_point;
list<int>::iterator occ_insert;
sRequest req;
// insert into occupied inputs list if
// input is currently empty
if ( _in_req[in].empty( ) ) {
occ_insert = _in_occ.begin( );
while ( ( occ_insert != _in_occ.end( ) ) &&
( *occ_insert < in ) ) {
occ_insert++;
}
assert( ( occ_insert == _in_occ.end( ) ) ||
( *occ_insert != in ) );
_in_occ.insert( occ_insert, in );
}
// similarly for the output
if ( _out_req[out].empty( ) ) {
occ_insert = _out_occ.begin( );
while ( ( occ_insert != _out_occ.end( ) ) &&
( *occ_insert < out ) ) {
occ_insert++;
}
assert( ( occ_insert == _out_occ.end( ) ) ||
( *occ_insert != out ) );
_out_occ.insert( occ_insert, out );
}
// insert input request in order of it's output
insert_point = _in_req[in].begin( );
while ( ( insert_point != _in_req[in].end( ) ) &&
( insert_point->port < out ) ) {
insert_point++;
}
req.port = out;
req.label = label;
req.in_pri = in_pri;
req.out_pri = out_pri;
bool del = false;
bool add = true;
// For consistent behavior, delete the existing request
// if it is for the same output and has a higher
// priority
if ( ( insert_point != _in_req[in].end( ) ) &&
( insert_point->port == out ) ) {
if ( insert_point->in_pri < in_pri ) {
del = true;
} else {
add = false;
}
}
if ( add ) {
_in_req[in].insert( insert_point, req );
}
if ( del ) {
_in_req[in].erase( insert_point );
}
insert_point = _out_req[out].begin( );
while ( ( insert_point != _out_req[out].end( ) ) &&
( insert_point->port < in ) ) {
insert_point++;
}
req.port = in;
req.label = label;
if ( add ) {
_out_req[out].insert( insert_point, req );
}
if ( del ) {
// This should be consistent, but check for sanity
if ( ( insert_point == _out_req[out].end( ) ) ||
( insert_point->port != in ) ) {
Error( "Internal allocator error --- input and output requests non consistent" );
}
_out_req[out].erase( insert_point );
}
}
void SparseAllocator::RemoveRequest( int in, int out, int label )
{
assert( ( in >= 0 ) && ( in < _inputs ) &&
( out >= 0 ) && ( out < _outputs ) );
list<sRequest>::iterator erase_point;
list<int>::iterator occ_remove;
// insert input request in order of it's output
erase_point = _in_req[in].begin( );
while ( ( erase_point != _in_req[in].end( ) ) &&
( erase_point->port != out ) ) {
erase_point++;
}
assert( erase_point != _in_req[in].end( ) );
_in_req[in].erase( erase_point );
// remove from occupied inputs list if
// input is now empty
if ( _in_req[in].empty( ) ) {
occ_remove = _in_occ.begin( );
while ( ( occ_remove != _in_occ.end( ) ) &&
( *occ_remove != in ) ) {
occ_remove++;
}
assert( occ_remove != _in_occ.end( ) );
_in_occ.erase( occ_remove );
}
// similarly for the output
erase_point = _out_req[out].begin( );
while ( ( erase_point != _out_req[out].end( ) ) &&
( erase_point->port != in ) ) {
erase_point++;
}
assert( erase_point != _out_req[out].end( ) );
_out_req[out].erase( erase_point );
if ( _out_req[out].empty( ) ) {
occ_remove = _out_occ.begin( );
while ( ( occ_remove != _out_occ.end( ) ) &&
( *occ_remove != out ) ) {
occ_remove++;
}
assert( occ_remove != _out_occ.end( ) );
_out_occ.erase( occ_remove );
}
}
void SparseAllocator::PrintRequests( ) const
{
list<sRequest>::const_iterator iter;
cout << "input requests:" << endl;
for ( int input = 0; input < _inputs; ++input ) {
cout << " input " << input << " : ";
for ( iter = _in_req[input].begin( );
iter != _in_req[input].end( ); iter++ ) {
cout << iter->port << " ";
}
cout << endl;
}
cout << "output requests:" << endl;
for ( int output = 0; output < _outputs; ++output ) {
cout << " output " << output << " : ";
if ( _outmask[output] == 0 ) {
for ( iter = _out_req[output].begin( );
iter != _out_req[output].end( ); iter++ ) {
cout << iter->port << " ";
}
cout << endl;
} else {
cout << "masked" << endl;
}
}
}
//==================================================
// Global allocator allocation function
//==================================================
Allocator *Allocator::NewAllocator( const Configuration &config,
Module *parent, const string& name,
const string &alloc_type,
int inputs, int input_speedup,
int outputs, int output_speedup )
{
Allocator *a = 0;
if ( alloc_type == "max_size" ) {
a = new MaxSizeMatch( config, parent, name, inputs, outputs );
} else if ( alloc_type == "pim" ) {
a = new PIM( config, parent, name, inputs, outputs );
} else if ( alloc_type == "islip" ) {
a = new iSLIP_Sparse( config, parent, name, inputs, outputs );
} else if ( alloc_type == "loa" ) {
a = new LOA( config, parent, name, inputs, input_speedup, outputs, output_speedup );
} else if ( alloc_type == "wavefront" ) {
a = new Wavefront( config, parent, name, inputs, outputs );
} else if ( alloc_type == "select" ) {
a = new SelAlloc( config, parent, name, inputs, outputs );
}
return a;
}
|