forked from tancheng/CGRA-Mapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCGRANode.cpp
581 lines (504 loc) · 14.6 KB
/
CGRANode.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
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
/*
* ======================================================================
* CGRANode.cpp
* ======================================================================
* CGRA tile implementation.
*
* Author : Cheng Tan
* Date : July 16, 2019
*/
#include "CGRANode.h"
#include <stdio.h>
#define SINGLE_OCCUPY 0 // A single-cycle opt is in the FU
#define START_PIPE_OCCUPY 1 // A multi-cycle opt starts in the FU
#define END_PIPE_OCCUPY 2 // A multi-cycle opt ends in the FU
#define IN_PIPE_OCCUPY 3 // A multi-cycle opt is occupying the FU
//CGRANode::CGRANode(int t_id) {
// m_id = t_id;
// m_currentCtrlMemItems = 0;
// m_canStore = false;
// m_canLoad = false;
//}
CGRANode::CGRANode(int t_id, int t_x, int t_y) {
m_id = t_id;
m_currentCtrlMemItems = 0;
m_disabled = false;
m_canStore = false;
m_canLoad = false;
m_supportComplex = false;
m_x = t_x;
m_y = t_y;
m_neighbors = NULL;
m_occupiableInLinks = NULL;
m_occupiableOutLinks = NULL;
// new list<list<pair<DFGNode*, int>>*>();//DFGNode*[1];
// m_dfgNodes = new DFGNode*[1];
// m_fuOccupied = new int[1];
m_regs_duration = NULL;
m_regs_timing = NULL;
// used for parameterizable CGRA functional units
m_canCall = true;
m_canAdd = true;
m_canMul = true;
m_canShift = true;
m_canPhi = true;
m_canSel = true;
m_canCmp = true;
m_canMAC = true;
m_canLogic = true;
m_canBr = true;
m_canReturn = true;
}
// FIXME: should handle the case that the data is maintained in the registers
// for multiple cycles.
void CGRANode::allocateReg(CGRALink* t_link, int t_cycle, int t_duration, int t_II) {
int reg_id = t_link->getDirectionID(this);
allocateReg(reg_id, t_cycle, t_duration, t_II);
}
void CGRANode::allocateReg(int t_port_id, int t_cycle, int t_duration, int t_II) {
bool allocated = false;
for (int i=0; i<m_registerCount; ++i) {
bool reg_occupied = false;
for (int cycle=t_cycle; cycle<m_cycleBoundary; cycle+=t_II) {
for (int d=0; d<t_duration; ++d) {
if (cycle+d<m_cycleBoundary and m_regs_duration[cycle+d][i] != -1)
reg_occupied = true;
}
}
for (int cycle=t_cycle; cycle>=0; cycle-=t_II) {
for (int d=0; d<t_duration; ++d) {
if (m_regs_duration[cycle+d][i] != -1)
reg_occupied = true;
}
}
if (reg_occupied == false) {
cout<<"[DEBUG] in allocateReg() t_cycle: "<<t_cycle<<"; i: "<<i<<" CGRA node: "<<this->getID()<<"; link: "<<t_port_id<<" duration "<<t_duration<<"\n";
for (int cycle=t_cycle; cycle<m_cycleBoundary; cycle+=t_II) {
m_regs_timing[cycle][i] = t_port_id;
for (int d=0; d<t_duration; ++d) {
if (cycle+d<m_cycleBoundary) {
// assert(m_regs_duration[cycle+d][i] == -1);
m_regs_duration[cycle+d][i] = t_port_id;
}
}
}
for (int cycle=t_cycle; cycle>=0; cycle-=t_II) {
m_regs_timing[cycle][i] = t_port_id;
for (int d=0; d<t_duration; ++d) {
m_regs_duration[cycle+d][i] = t_port_id;
}
}
allocated = true;
break;
}
}
cout<<"[DEBUG] done reg allocation"<<endl;
//assert(allocated);
}
int* CGRANode::getRegsAllocation(int t_cycle) {
return m_regs_timing[t_cycle];
}
void CGRANode::setCtrlMemConstraint(int t_ctrlMemConstraint) {
m_ctrlMemSize = t_ctrlMemConstraint;
}
void CGRANode::setRegConstraint(int t_registerConstraint) {
m_registerCount = t_registerConstraint;
}
void CGRANode::setID(int t_id) {
m_id = t_id;
}
void CGRANode::setLocation(int t_x, int t_y) {
m_x = t_x;
m_y = t_y;
}
int CGRANode::getID() {
return m_id;
}
void CGRANode::attachInLink(CGRALink* t_link) {
m_inLinks.push_back(t_link);
}
void CGRANode::attachOutLink(CGRALink* t_link) {
m_outLinks.push_back(t_link);
}
list<CGRALink*>* CGRANode::getInLinks() {
return &m_inLinks;
}
list<CGRALink*>* CGRANode::getOutLinks() {
return &m_outLinks;
}
list<CGRANode*>* CGRANode::getNeighbors() {
if (m_neighbors != NULL)
return m_neighbors;
m_neighbors = new list<CGRANode*>();
for (CGRALink* link: m_outLinks)
m_neighbors->push_back(link->getConnectedNode(this));
return m_neighbors;
}
void CGRANode::constructMRRG(int t_CGRANodeCount, int t_II) {
m_cycleBoundary = t_CGRANodeCount*t_II*t_II;
m_currentCtrlMemItems = 0;
m_registers.clear();
// Delete all these local arrays to avoid memory leakage.
if (m_dfgNodesWithOccupyStatus.size() > 0) {
for (list<pair<DFGNode*, int>>* opts: m_dfgNodesWithOccupyStatus) {
opts->clear();
}
}
m_dfgNodesWithOccupyStatus.clear();
// m_dfgNodesWithOccupyStatus = new list<list<pair<DFGNode*, int>>*>();
for (int i=0; i<m_cycleBoundary; ++i) {
m_dfgNodesWithOccupyStatus.push_back(new list<pair<DFGNode*, int>>());
}
m_regs_duration = new int*[m_cycleBoundary];
m_regs_timing = new int*[m_cycleBoundary];
for (int i=0; i<m_cycleBoundary; ++i) {
m_regs_duration[i] = new int[m_registerCount];
m_regs_timing[i] = new int[m_registerCount];
for (int j=0; j<m_registerCount; ++j) {
m_regs_duration[i][j] = -1;
m_regs_timing[i][j] = -1;
}
}
}
bool CGRANode::canSupport(DFGNode* t_opt) {
if (m_disabled)
return false;
// Check whether this CGRA node supports the required functionality.
if ((t_opt->isLoad() and !canLoad()) or
(t_opt->isStore() and !canStore()) or
(t_opt->isReturn() and !canReturn()) or
(t_opt->isCall() and !canCall()) or
(t_opt->isVectorized() and !supportVectorization()) or
(t_opt->hasCombined() and !supportComplex()) or
(t_opt->isAdd() and !canAdd()) or
(t_opt->isMul() and !canMul()) or
(t_opt->isPhi() and !canPhi()) or
(t_opt->isSel() and !canSel()) or
(t_opt->isMAC() and !canMAC()) or
(t_opt->isLogic() and !canLogic()) or
(t_opt->isBranch() and !canBr()) or
(t_opt->isCmp() and !canCmp()) ){
return false;
}
return true;
}
bool CGRANode::canOccupy(DFGNode* t_opt, int t_cycle, int t_II) {
if (m_disabled)
return false;
// Check whether this CGRA node supports the required functionality.
if (!canSupport(t_opt)) {
return false;
}
// Check whether the limit of config mem is reached.
if (m_currentCtrlMemItems + 1 > m_ctrlMemSize) {
return false;
}
// Handle multi-cycle execution and pipelinable operations.
if (not t_opt->isMultiCycleExec()) {
// Single-cycle opt:
for (int cycle=t_cycle%t_II; cycle<m_cycleBoundary; cycle+=t_II) {
for (pair<DFGNode*, int> p: *(m_dfgNodesWithOccupyStatus[cycle])) {
if (p.second != IN_PIPE_OCCUPY) {
return false;
}
}
}
} else {
// Multi-cycle opt.
for (int cycle=t_cycle%t_II; cycle<m_cycleBoundary; cycle+=t_II) {
// Check start cycle.
for (pair<DFGNode*, int> p: *(m_dfgNodesWithOccupyStatus[cycle])) {
// Multi-cycle opt's start cycle overlaps with single-cycle opt' cycle.
if (p.second == SINGLE_OCCUPY) {
return false;
}
// Multi-cycle opt's start cycle overlaps with multi-cycle opt's start cycle.
else if (p.second == START_PIPE_OCCUPY) {
return false;
}
// Multi-cycle opt's start cycle overlaps with multi-cycle opt with the same type:
else if ((p.second == IN_PIPE_OCCUPY or p.second == END_PIPE_OCCUPY) and
(t_opt->shareFU(p.first)) and
(not t_opt->isPipelinable() or not p.first->isPipelinable())) {
return false;
}
}
if (cycle+t_opt->getExecLatency()-1 >= m_cycleBoundary) {
break;
}
// Check end cycle.
for (pair<DFGNode*, int> p: *(m_dfgNodesWithOccupyStatus[cycle+t_opt->getExecLatency()-1])) {
// Multi-cycle opt's end cycle overlaps with single-cycle opt' cycle.
if (p.second == SINGLE_OCCUPY) {
return false;
}
// Multi-cycle opt's end cycle overlaps with multi-cycle opt's end cycle.
else if (p.second == END_PIPE_OCCUPY) {
return false;
}
// Multi-cycle opt's end cycle overlaps with multi-cycle opt with the same type:
else if ((p.second == IN_PIPE_OCCUPY or p.second == START_PIPE_OCCUPY) and
(t_opt->shareFU(p.first)) and
(not t_opt->isPipelinable() or not p.first->isPipelinable())) {
return false;
}
}
}
}
return true;
}
bool CGRANode::isOccupied(int t_cycle, int t_II) {
for (int cycle=t_cycle; cycle<m_cycleBoundary; cycle+=t_II) {
for (pair<DFGNode*, int> p: *(m_dfgNodesWithOccupyStatus[cycle])) {
// if (m_fuOccupied[cycle])
if (p.second == START_PIPE_OCCUPY or p.second == SINGLE_OCCUPY) {
return true;
}
}
}
return false;
}
void CGRANode::setDFGNode(DFGNode* t_opt, int t_cycle, int t_II,
bool t_isStaticElasticCGRA) {
int interval = t_II;
if (t_isStaticElasticCGRA) {
interval = 1;
}
for (int cycle=t_cycle%interval; cycle<m_cycleBoundary; cycle+=interval) {
if (not t_opt->isMultiCycleExec()) {
m_dfgNodesWithOccupyStatus[cycle]->push_back(make_pair(t_opt, SINGLE_OCCUPY));
} else {
m_dfgNodesWithOccupyStatus[cycle]->push_back(make_pair(t_opt, START_PIPE_OCCUPY));
for (int i=1; i<t_opt->getExecLatency()-1; ++i) {
if (cycle+i < m_cycleBoundary) {
m_dfgNodesWithOccupyStatus[cycle+i]->push_back(make_pair(t_opt, IN_PIPE_OCCUPY));
}
}
int lastCycle = cycle+t_opt->getExecLatency()-1;
if (lastCycle < m_cycleBoundary) {
m_dfgNodesWithOccupyStatus[lastCycle]->push_back(make_pair(t_opt, END_PIPE_OCCUPY));
}
}
}
cout<<"[DEBUG] setDFGNode "<<t_opt->getID()<<" onto CGRANode "<<getID()<<" at cycle: "<<t_cycle<<"\n";
++m_currentCtrlMemItems;
t_opt->setMapped();
}
DFGNode* CGRANode::getMappedDFGNode(int t_cycle) {
for (pair<DFGNode*, int> p: *(m_dfgNodesWithOccupyStatus[t_cycle])) {
if (p.second == SINGLE_OCCUPY or p.second == END_PIPE_OCCUPY) {
return p.first;
}
}
return NULL;
}
bool CGRANode::containMappedDFGNode(DFGNode* t_node, int t_II) {
for (int c=0; c<2*t_II; ++c) {
for (pair<DFGNode*, int> p: *(m_dfgNodesWithOccupyStatus[c])) {
if (t_node == p.first) {
return true;
}
}
}
return false;
}
void CGRANode::configXbar(CGRALink*, int, int)
{
}
void CGRANode::addRegisterValue(float t_value) {
m_registers.push_back(t_value);
}
list<CGRALink*>* CGRANode::getOccupiableInLinks(int t_cycle, int t_II) {
if (m_occupiableInLinks == NULL)
m_occupiableInLinks = new list<CGRALink*>();
m_occupiableInLinks->clear();
for (CGRALink* link: m_inLinks) {
if (link->canOccupy(t_cycle, t_II)) {
m_occupiableInLinks->push_back(link);
}
}
return m_occupiableInLinks;
}
list<CGRALink*>* CGRANode::getOccupiableOutLinks(int t_cycle, int t_II) {
if (m_occupiableOutLinks == NULL)
m_occupiableOutLinks = new list<CGRALink*>();
m_occupiableOutLinks->clear();
for (CGRALink* link: m_outLinks) {
if (link->canOccupy(t_cycle, t_II)) {
m_occupiableOutLinks->push_back(link);
}
}
return m_occupiableOutLinks;
}
int CGRANode::getAvailableRegisterCount() {
return (m_registerCount - m_registers.size());
}
CGRALink* CGRANode::getInLink(CGRANode* t_node) {
for (CGRALink* link: m_inLinks) {
if (link->getSrc() == t_node) {
return link;
}
}
// will definitely return one inlink
assert(0);
}
CGRALink* CGRANode::getOutLink(CGRANode* t_node) {
for (CGRALink* link: m_outLinks) {
if (link->getDst() == t_node)
return link;
}
return NULL;
// will definitely return one outlink
// assert(0);
}
int CGRANode::getMinIdleCycle(DFGNode* t_dfgNode, int t_cycle, int t_II) {
int tempCycle = t_cycle;
while (tempCycle < m_cycleBoundary) {
if (canOccupy(t_dfgNode, tempCycle, t_II))
return tempCycle;
++tempCycle;
}
return m_cycleBoundary;
}
int CGRANode::getCurrentCtrlMemItems() {
return m_currentCtrlMemItems;
}
// TODO: will support precision-based operations (e.g., fadd, fmul, etc).
bool CGRANode::enableFunctionality(string t_func) {
if (t_func.compare("store") == 0) {
enableStore();
} else if (t_func.compare("load") == 0) {
enableLoad();
} else if (t_func.compare("return") == 0) {
enableReturn();
} else if (t_func.compare("call") == 0) {
enableCall();
} else if (t_func.compare("complex") == 0) {
enableComplex();
} else {
return false;
}
return true;
}
void CGRANode::enableReturn() {
m_canReturn = true;
}
void CGRANode::enableStore() {
m_canStore = true;
}
void CGRANode::enableLoad() {
m_canLoad = true;
}
void CGRANode::enableCall() {
m_canCall = true;
}
void CGRANode::enableComplex() {
m_supportComplex = true;
}
void CGRANode::enableVectorization() {
m_supportVectorization = true;
}
void CGRANode::enableAdd() {
m_canAdd = true;
}
void CGRANode::enableMul() {
m_canMul = true;
}
void CGRANode::enableShift() {
m_canShift = true;
}
void CGRANode::enablePhi() {
m_canPhi = true;
}
void CGRANode::enableSel() {
m_canSel = true;
}
void CGRANode::enableCmp() {
m_canCmp = true;
}
void CGRANode::enableMAC() {
m_canMAC = true;
}
void CGRANode::enableLogic() {
m_canLogic = true;
}
void CGRANode::enableBr() {
m_canBr = true;
}
bool CGRANode::supportComplex() {
return m_supportComplex;
}
bool CGRANode::supportVectorization() {
return m_supportVectorization;
}
bool CGRANode::canCall() {
return m_canCall;
}
bool CGRANode::canReturn() {
return m_canReturn;
}
bool CGRANode::canStore() {
return m_canStore;
}
bool CGRANode::canLoad() {
return m_canLoad;
}
bool CGRANode::canAdd() {
return m_canAdd;
}
bool CGRANode::canMul() {
return m_canMul;
}
bool CGRANode::canShift() {
return m_canShift;
}
bool CGRANode::canPhi() {
return m_canPhi;
}
bool CGRANode::canSel() {
return m_canSel;
}
bool CGRANode::canCmp() {
return m_canCmp;
}
bool CGRANode::canMAC() {
return m_canMAC;
}
bool CGRANode::canLogic() {
return m_canLogic;
}
bool CGRANode::canBr() {
return m_canBr;
}
int CGRANode::getX() {
return m_x;
}
int CGRANode::getY() {
return m_y;
}
void CGRANode::disable() {
m_disabled = true;
for (CGRALink* link: m_inLinks) {
link->disable();
}
for (CGRALink* link: m_outLinks) {
link->disable();
}
}
void CGRANode::disableAllFUs() {
m_canReturn = false;
m_canStore = false;
m_canLoad = false;
m_canCall = false;
m_canAdd = false;
m_canMul = false;
m_canShift = false;
m_canPhi = false;
m_canSel = false;
m_canCmp = false;
m_canMAC = false;
m_canLogic = false;
m_canBr = false;
m_supportComplex = false;
m_supportVectorization = false;
}