-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhlsyn.cpp
More file actions
1356 lines (1238 loc) · 50.1 KB
/
hlsyn.cpp
File metadata and controls
1356 lines (1238 loc) · 50.1 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
//
// fds.cpp
// 574
// Force Directed Scheduler
//
// Created by Kristopher Rockowitz on 11/30/19.
// Copyright © 2019 r0. All rights reserved.
//
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <limits>
#include <cmath>
#include <cstdlib>
//#include <stdio.h>
//#include <iostream>
//#include <fstream>
//#include <sstream>
//#include <vector>
//#include <string>
//#include <algorithm>
//#include <iomanip>
//#include <stack>
//#include <limits>
//#include <cmath>
using namespace std;
// inputs, outputs, registers, wires are of this type
class Variable {
public:
string name; // a, b, c, etc.
string type; // s for signed, u for unsigned
int bits; // bits 1, 2, 8, 16, 32, 64
// constructor
Variable(){
this->name = "";
this->type = "";
this->bits = 0;
}
Variable(string name, string type, int bits){
this->name = name;
this->type = type;
this->bits = bits;
}
};
// class that holds processed operations
// TODO: modify to incorporate 'if' statements
class Operation {
public:
Variable result; // the leftmost variable aka result of the op in most cases
string equals; // the equals operator
Variable var1; // the first variable after the equals sign
Variable var2; // the second variable after the equals sign
Variable var3; // the third variable after the equals sign
string op1; // the first operator
string op2; // the second operator
string name; // name of the operation
string type; // type (s for signed, u for unsigned)
int bits; // bits 1, 2, 8, 16, 32, 64
Operation(){
this->result = Variable();
this->equals = "";
this->var1 = Variable();
this->var2 = Variable();
this->var3 = Variable();
this->op1 = "";
this->op2 = "";
this->name = "";
this->type = "";
this->bits = 0;
}
};
class Node{
public:
Operation operation; // add/sub, mult, logic, div/mod
vector<string> inputs; // max 2 inputs, pretty sure it has to be this way
vector<Node*> parent;
vector<Node*> children;
string output; // max 1 output
int asap; // this is the first element of timeframe for FDS
int alap; // this is the second element of timeframe for FDS
int mobility; // alap - asap. also fyi, width = mobility + 1
bool asapScheduled;
bool alapScheduled;
bool scheduled; // default false, helps to shrink each iteration of FDS
int selfForce; // SUM(DG(i) * deltaX(i)) where DG(i) is time probability and deltaX(i) is
int delay;
int num;
int fds;
Node(){
this->children = {};
this->inputs = {};
this->parent = {};
this->operation = Operation();
this->output = "";
this->alap = 0;
this->asap = 0;
this->mobility = 0;
this->scheduled = false;
this->asapScheduled = false;
this->alapScheduled = false;
this->selfForce = 0;
this->delay = 0;
this->num = -1;
this->fds = -1;
}
};
class Graph{ // will contain the vector of all the edges, where the edges have the children/parent information
public:
vector<Node> vertices; //a list of all nodes in this graph
Node noop;
Node sink; //I'm thinking these might be helpful
Graph(){
}
void printNodes(){
vector<Node>::iterator z;
for(z = this->vertices.begin(); z < this->vertices.end(); z++){
cout << "Node: " << z->num << " Op: " << z->operation.name << " ";
}
cout << endl;
}
//multiplies have 2 cycle delay, divide and modulo and 3 cycle delay, all others 1 cycle
void alap(int latency, bool* validCircuit) {
for(int i = latency; i >= 1; i--){
for(int j = this->vertices.size() - 1; j >= 0; j--) {//iterate backwards between nodes
if(!this->vertices.at(j).alapScheduled) {
string newCurrentOp = this->vertices.at(j).operation.name;
int delay = 0;
if(this->vertices.at(j).children.size() == 0) {//schedule, no children, bottom node
this->vertices.at(j).alapScheduled = true;
this->vertices.at(j).alap = (i - this->vertices.at(j).delay) + 1;
}
else {//has children, all of them must be scheduled
bool allChildrenScheduled = true;
int earliestSchedule = latency + 1;
for(int k = 0; k < this->vertices.at(j).children.size(); k++) {
if(!this->vertices.at(j).children.at(k)->alapScheduled) {
allChildrenScheduled = false;
break;
}
else {//if the child is scheduled, then we must record the earliest one of the children are scheduled
//this is because when scheduling the current node, there must be enough time for its operation to be completed
if(this->vertices.at(j).children.at(k)->alap < earliestSchedule) {
earliestSchedule = this->vertices.at(j).children.at(k)->alap;
}
}
}
if(allChildrenScheduled) {//now, we check the current operation to see if there was enough time between it
//and its earliest child
if(i < earliestSchedule) {
this->vertices.at(j).alap = (i - this->vertices.at(j).delay) + 1;
this->vertices.at(j).alapScheduled = true;
}
}
}
}
}
}
bool allScheduled = true;
for(int i = 0; i < this->vertices.size(); i++) {
if(!this->vertices.at(i).alapScheduled) {
allScheduled = false;
break;
}
}
if(!allScheduled) {
//quit program
*validCircuit = false;
}
}
void asap(int latency, bool* validCircuit) {
for(int i = 1; i <= latency; i++) {
for(int j = 0; j < this->vertices.size(); j++) {
if(!this->vertices.at(j).asapScheduled) {
if(this->vertices.at(j).parent.size() == 0) { //schedule, no parents, top node
this->vertices.at(j).asapScheduled = true;
this->vertices.at(j).asap = i;
}
else { //has parents, check if all of them are scheduled
bool allParentScheduled = true;
int latestSchedule = 0;
for(int k = 0; k < this->vertices.at(j).parent.size(); k++) {
if(!this->vertices.at(j).parent.at(k)->asapScheduled) {
allParentScheduled = false;
break;
}
else { //if the parent is scheduled, this records the latest a parent is scheduled
//because the latest one is what determines when the child should be scheduled
//use parent op name to find when the latest cycle is available
// string parentOp = this->vertices.at(j).parent.at(k)->operation.name;
int parentDelay = this->vertices.at(j).parent.at(k)->delay;
int tempLatest = 0;
tempLatest = this->vertices.at(j).parent.at(k)->asap + parentDelay;
if(tempLatest > latestSchedule) {
latestSchedule = tempLatest;
}
}
}
//now that we know all parents are scheduled, we can start checking if these nodes have
//the resources to be scheduled
if(allParentScheduled){
if(i>=latestSchedule) {
this->vertices.at(j).asapScheduled = true;
this->vertices.at(j).asap = i;
}
}
}
}
}
}
bool allScheduled = true;
for(int i = 0; i < this->vertices.size(); i++) {
if(!this->vertices.at(i).asapScheduled) {
allScheduled = false;
break;
}
}
if(!allScheduled) {
//quit program
*validCircuit = false;
}
}
void populateNodes(vector<Operation> ops) {
for(int i = 0; i < ops.size(); i++) {
Node curr;
Operation currentOp = ops.at(i);
curr.operation = currentOp;
if(currentOp.name.compare("MULT") == 0) {//2 cycle delay
curr.delay = 2;
}
else if(currentOp.name.compare("DIV") == 0) {//3 cycle delay
curr.delay = 3;
}
else if(currentOp.name.compare("MOD") == 0) {//3 cycle delay
curr.delay = 3;
}
else {
curr.delay = 1;
}
//TODO: add in the ternary operator check here, that's when the third variable will be used
curr.inputs.push_back(currentOp.var1.name);
//TODO: add the ! thing check
if (currentOp.op1.compare("!") == 0) {
//don't need var1 or var 2
}
else {
curr.inputs.push_back(currentOp.var2.name);
}
if (currentOp.op1.compare("?") == 0) {
//need var 3
curr.inputs.push_back(currentOp.var3.name);
}
curr.output = currentOp.result.name;
this->vertices.push_back(curr);
}
}
void cdfg() {
vector<int> visitedNodes = {}; //keep track of what nodes have been seen, recording their indexes
vector<string> visistedOutputs = {};
for(int i = 0; i < this->vertices.size(); i++) {
Node *current = &this->vertices.at(i);
for(int j = 0; j < current->inputs.size(); j++) { //this is usually 2 iterations
string inp = current->inputs.at(j);
for(int foundIndex = 0; foundIndex < visistedOutputs.size(); foundIndex++) { //now, go through the visited outputs to see if the input matches anything
if(inp == visistedOutputs.at(foundIndex)) {
current->parent.push_back(&this->vertices.at(visitedNodes.at(foundIndex)));
this->vertices.at(visitedNodes.at(foundIndex)).children.push_back(current);
break;
}
}
}
visitedNodes.push_back(i); //add index of where the node visited was in the for loop
visistedOutputs.push_back(current->output);
}
}
void schedule(int latency, bool* validCircuit) {
this->alap(latency, validCircuit);
this->asap(latency, validCircuit);
}
};
vector<string> ReadFile(string inputFile, bool *validCircuit){
// cout << "Reading File...\n";
vector<string> parsedFile;
ifstream netlistFile;
netlistFile.open(inputFile);
if(netlistFile){
string str;
while(getline(netlistFile, str)){
parsedFile.push_back(str);
}
}
if(netlistFile.peek() == std::ifstream::traits_type::eof()){
*validCircuit = false;
return parsedFile;
}
else{
// cout << "Unable to read file" << endl;
*validCircuit = false;
return parsedFile;
}
// cout << "Done reading file." << endl;
return parsedFile;
}
void PrintOperands(vector<Variable> &inputs, vector<Variable> &outputs, vector<Variable> &vars, vector<string>& operations){
cout << "Inputs:\n";
for(vector<Variable>::iterator i = inputs.begin(); i != inputs.end(); i++){
cout << distance(inputs.begin(), i)+1 << ". "<< (*i).name << " has bitwidth " << (*i).bits << "\n";
}
cout << "\nOutputs:\n";
for(vector<Variable>::iterator i = outputs.begin(); i != outputs.end(); i++){
cout << distance(outputs.begin(), i)+1 << ". "<< (*i).name << " has bitwidth " << (*i).bits << "\n";
}
cout << "\nVariables:\n";
for(vector<Variable>::iterator i = vars.begin(); i != vars.end(); i++){
cout << distance(vars.begin(), i)+1 << ". "<< (*i).name << " has bitwidth " << (*i).bits << "\n";
}
cout << "\nOperations:\n";
for(vector<string>::iterator i = operations.begin(); i != operations.end(); i++){
cout << (*i) << "\n";
}
}
void ParseLine(vector<Variable>& inputs, vector<Variable>& outputs, vector<Variable>& vars, vector<string>& operations, string line){
//parse into inputs, outputs, wires and operations
if(line.find("input")==0){
//input Int8 a, b, c
int BW = -1;
BW = atoi(&line[line.find("Int")+3]);
char U = line.at(line.find("Int")-1);
for(int i = line.find(' ',line.find("Int")); i< line.length(); i++){
if(line[i] == '/'){
if(line[i+1] == '/'){
i = line.length();
}
}
else if(line[i] != ' ' && line[i]!=',' && line[i] != '\t'){
Variable input;
int j = i;
while(line[j] != ',' && line[j] != '\0' && line[j]!= ' ' && line[j] != '\t'){
if((j-i)>0)
i = j;
input.name += line[j];
j++;
}
if(U == 'U'|| BW == 1){
input.type = "u";
}
else{
input.type = "s";
}
input.bits = BW;
inputs.push_back(input);
}
}
}
else if(line.find("output")==0){
int BW = -1;
BW = atoi(&line[line.find("Int")+3]);
char U = line.at(line.find("Int")-1);
for(int i = line.find(' ',line.find("Int")); i< line.length(); i++){
if(line[i] == '/'){
if(line[i+1] == '/'){
i = line.length();
}
}
else if(line[i] != ' ' && line[i]!=',' && line[i] != '\t'){
Variable output;
int j = i;
while(line[j] != ',' && line[j] != '\0' && line[j]!= ' ' && line[j] != '\t'){
if((j-i)>0)
i = j;
output.name += line[j];
j++;
}
if(U == 'U'|| BW == 1){
output.type = "u";
}
else{
output.type = "s";
}
output.bits = BW;
outputs.push_back(output);
}
}
}
else if(line.find("variable")==0){
int BW = -1;
BW = atoi(&line[line.find("Int")+3]);
char U = line.at(line.find("Int")-1);
for(int i = line.find(' ',line.find("Int")); i< line.length(); i++){
if(line[i] == '/'){
if(line[i+1] == '/'){
i = line.length();
}
}
else if(line[i] != ' ' && line[i]!=',' && line[i] != '\t'){
Variable var;
int j = i;
while(line[j] != ',' && line[j] != '\0' && line[j]!= ' ' && line[j] != '\t'){
if((j-i)>0)// check to skip rest of whole words
i = j;
var.name += line[j];
j++;
}
if(U == 'U' || BW == 1){
var.type = "u";
}
else{
var.type = "s";
}
var.bits = BW;
vars.push_back(var);
}
}
}
else if(line.find("=") != std::string::npos || line.find("{") != std::string::npos || line.find("}") != std::string::npos){
operations.push_back(line);
}
}
// search all the vectors and return the Variable, else empty Variable
Variable searchVariables(vector<Variable> in, vector<Variable> out, vector<Variable> vari, string find){
std::vector<Variable>::iterator it;
for(it = in.begin(); it != in.end(); ++it){
if(it->name == find){
return *it;
}
}
for(it = out.begin(); it != out.end(); ++it){
if(it->name == find){
return *it;
}
}
for(it = vari.begin(); it != vari.end(); ++it){
if(it->name == find){
return *it;
}
}
Variable var;
return var;
}
int maxInputBits(Operation op){
unsigned int max = 0;
if(op.var1.bits > max){
max = op.var1.bits;
}
if(op.var2.bits > max){
max = op.var2.bits;
}
if(op.var3.bits > max){
max = op.var3.bits;
}
return max;
}
// process the string operations and return the Operation vector
vector<Operation> processOperation(vector<string> operations, vector<Variable> inputs, vector<Variable> outputs, vector<Variable> vars, bool* validCircuit){
vector<Operation> temp;
// TODO: move this shit to a function
// create a stringstream to parse the elements of the operation line string
// iterate through the operation vector
int numIf = 0;
bool elseCond = false;
int elseIndex = -1;
vector<Variable> ifCond;
vector<bool> elseCondition;
for(unsigned int i = 0; i < operations.size(); i++){
stringstream opstream;
string result, equals, var1, op1, var2, op2, var3;
// throw the line into the stream
opstream << operations.at(i);
// stream into the variables
opstream >> result >> equals >> var1 >> op1 >> var2 >> op2 >> var3;
// create new operation with parsed operators
if(result == "if"){
numIf++;
Variable v = searchVariables(inputs, outputs, vars, var1);
ifCond.push_back(v);
elseCondition.push_back(false);
}
else if(result == "}" && i != operations.size()-1){
stringstream eop;
string e;
eop << operations.at(i+1);
eop >> e;
elseCond = e == "else";
if(!elseCond){
numIf--;
ifCond.erase(ifCond.end());
elseCondition.erase(elseCondition.end());
}
else{
elseCondition[elseCondition.size()-1] = true;
}
}
else if (result != "else" && result != "}"){
Operation oCond;
if(numIf > 0){
int s = ifCond.size();
Variable first = ifCond[ifCond.size()-1];
int idx = ifCond.size()-1;
while(idx > 0){
Variable second = ifCond[idx-1];
if(elseCondition[idx] == false && elseCondition[idx-1 == true]){
Variable e = Variable("e_" + to_string(i), "", 32);
vars.push_back(e);
Operation eo;
eo.result = e;
eo.equals = "=";
eo.op1 = "!";
eo.var1 = second;
eo.name = "NOT";
temp.push_back(eo);
second = e;
}
else if(elseCondition[idx] == true && idx == ifCond.size() -1){
Variable e = Variable("e_" + to_string(i), "", 32);
vars.push_back(e);
Operation eo;
eo.result = e;
eo.equals = "=";
eo.op1 = "!";
eo.var1 = first;
eo.name = "NOT";
temp.push_back(eo);
first = e;
}
Variable c = Variable("cond" + to_string(s) +"_"+to_string(i), "", 32);
vars.push_back(c);
Operation nestIf;
nestIf.result = c;
nestIf.equals = "=";
nestIf.var1 = first;
nestIf.op1 = "&&";
nestIf.name = "COMP";
nestIf.var2 = second;
temp.push_back(nestIf);
first = c;
idx--;
}
Variable v = searchVariables(inputs, outputs, vars, result);
if(v.bits == 0){
*validCircuit = false;
}
else{
oCond.result = v;
}
oCond.equals = "=";
oCond.var1 = first;
oCond.op1 = "?";
result = result + to_string(i);
Variable r = Variable(result, "", 32);
vars.push_back(r);
oCond.var2 = r;
oCond.op2 = ":";
oCond.var3 = v;
}
Operation o1;
o1.equals = equals;
o1.op1 = op1;
o1.op2 = op2;
// discover component and assign to name
if(op1 == "")
o1.name = "REG";
else if(op1 == "+" && var2 == "1")
o1.name = "INC";
else if(op1 == "+")
o1.name = "ADD";
else if(op1 == "-" && var2 == "1")
o1.name = "DEC";
else if(op1 == "-")
o1.name = "SUB";
else if(op1 == "*")
o1.name = "MUL";
else if(op1 == ">" || op1 == "<" || op1 == "==")
o1.name = "COMP";
else if(op1 == "?" && op2 == ":")
o1.name = "MUX2x1";
else if(op1 == ">>")
o1.name = "SHR";
else if(op1 == "<<")
o1.name = "SHL";
else if(op1 == "/")
o1.name = "DIV";
else if(op1 == "%")
o1.name = "MOD";
else if(op1 == "/")
o1.name = "DIV";
else{
*validCircuit = false;
}
Variable r1 = searchVariables(inputs, outputs, vars, result);
if(r1.bits == 0){
*validCircuit = false;
}
else{
o1.result = r1;
}
if(equals != "="){
*validCircuit = false;
}
// validate 'var1'
Variable v1 = searchVariables(inputs, outputs, vars, var1);
if(v1.bits == 0){
*validCircuit = false;
}
else{
if(o1.name == "MUX2x1"){
o1.var3 = v1;
}
else{
o1.var1 = v1;
}
}
Variable v2 = searchVariables(inputs, outputs, vars, var2);
if(v2.bits == 0 && o1.name != "INC" && o1.name != "REG" && o1.name != "DEC"){
*validCircuit = false;
}
else{
if(o1.name == "MUX2x1"){
o1.var1 = v2;
}
else{
o1.var2 = v2;
}
}
Variable v3 = searchVariables(inputs, outputs, vars, var3);
if(v3.bits != 0 && o1.name != "MUX2x1"){
*validCircuit = false;
}
else{
if(o1.name == "MUX2x1"){
o1.var2 = v3;
}
else{
o1.var3 = v3;
}
}
if(o1.name == "COMP"){
o1.bits = maxInputBits(o1);
}
else{
o1.bits = o1.result.bits;
}
temp.push_back(o1);
if(numIf > 0)
temp.push_back(oCond);
}
}
return temp;
}
// Self force function takes in a node, a time, and all the DGs. Iterates throught the timeframes and calculates all the negative probability components.
// Then with the time given, calcs the positive probability component and returns the self force.
// This works off the reduced equation: Self Force = DG(time) - Sum[DG(n) * prob(n)] where n is the interval of asap to alap.
float SelfForce(Node node, int time, vector<float> dg_mult, vector<float> dg_addsub, vector<float> dg_divmod, vector<float> dg_logic){
// loop through all time frames for a given node and get the negative part of self force. this represents the probability for all times in timeframe
float selfForce = 0.0;
// if the time given is outside of the asap to alap interval, the force is necessarily 0
if(time < node.asap || time > node.alap){
selfForce = 0.0;
}
else{
for(unsigned int n = node.asap; n <= node.alap; n++){
// calc 1/width for ease of use
float prob = 1/((float)node.alap - (float)node.asap + 1);
// operation type matters and so filter by operation type
if(node.operation.name == "MULT"){
// self force = DG(j) * deltaX(j) for alap - asap number of terms
selfForce -= dg_mult.at(n-1) * prob;
}
else if(node.operation.name == "DIV" || node.operation.name == "MOD"){
// self force = DG(j) * deltaX(j) for alap - asap number of terms
selfForce -= dg_divmod.at(n-1) * prob;
}
else if(node.operation.name == "ADD" || node.operation.name == "SUB"){
// self force = DG(j) * deltaX(j) for alap - asap number of terms
selfForce -= dg_addsub.at(n-1) * prob;
}
else if(node.operation.name == "COMP" || node.operation.name == "MUX2x1" || node.operation.name == "SHR" || node.operation.name == "SHL" || node.operation.name == "NOT"){
// self force = DG(j) * deltaX(j) for alap - asap number of terms
selfForce -= dg_logic.at(n-1) * prob;
}
}// end negative self force part
// add the positive component to self force. this represents the probability is 1 at the time given
if(node.operation.name == "MULT"){
selfForce += dg_mult.at(time-1);
}
else if(node.operation.name == "DIV" || node.operation.name == "MOD"){
selfForce += dg_divmod.at(time-1);
}
else if(node.operation.name == "ADD" || node.operation.name == "SUB"){
selfForce += dg_addsub.at(time-1);
}
else if(node.operation.name == "COMP" || node.operation.name == "MUX2x1" || node.operation.name == "SHR" || node.operation.name == "SHL" || node.operation.name == "NOT"){
selfForce += dg_logic.at(time-1);
}
}
return selfForce;
}
// calculates the predecessor forces for the given node
tuple<float, int> TotalForce(Node s_in, int size, vector<float> dg_mult, vector<float> dg_addsub, vector<float> dg_divmod, vector<float> dg_logic) {
// s is the node, time is the current scheduling time, size is the number of total nodes
// TODO: add functionality so that when pred/succ forces are calculated, they set all the timeframes of connected nodes
Node s = s_in;
tuple<float, int> out;
float lowestForce = numeric_limits<float>::max(); // create super high float so any min is lower
for(unsigned int n = s.asap; n <= s.alap; n++){
// create a changes Vector
vector<Node> changes;
int time = n;
// calc self Force
float selfForce = SelfForce(s, time, dg_mult, dg_addsub, dg_divmod, dg_logic);
cout << "Self: " << selfForce;
// Initially mark all verices as not visited
vector<bool> visited(size, false);
// Create a stack for DFS
stack<Node> stack;
// Push the current source node.
stack.push(s);
// level used for time calculation
int level = 0;
//intialize predecessor force
float predecessorForce = 0.0;
int x = 0;
while (!stack.empty())
{
// Pop a vertex from stack and print it
s = stack.top();
stack.pop();
if (!visited[s.num]){
// cout << s.num << " ";
visited[s.num] = true;
// if it's not the first node, evaluate whether pred force exists
if(x > 0){
float sf = 0.0;
// if the pred node has an alap that is greater than adj_time, calc self force at adj_time
int adj_time = time - level + 1;
if(s.alap >= adj_time){
sf = SelfForce(s, adj_time - 1, dg_mult, dg_addsub, dg_divmod, dg_logic);
}
else{
sf = 0.0;
}
predecessorForce += sf;
}
}
x++;
// increment the level every time the number of parents > 0
if(s.parent.size() > 0){
level++;
}
for (unsigned int i = 0; i < s.parent.size(); i++){
// check if the parent num is in the visisted array
if (!visited[s.parent.at(i)->num]){
stack.push(*s.parent.at(i));
}
}
}
cout << " Pred: " << predecessorForce;
// Initially mark all verices as not visited
vector<bool> visited2(size, false);
// Push the current source node.
stack.push(s);
// level used for time calculation
level = 0;
//intialize successor force
float successorForce = 0.0;
x = 0;
while (!stack.empty())
{
// Pop a vertex from stack and print it
s = stack.top();
stack.pop();
if (!visited[s.num]){
// cout << s.num << " ";
visited[s.num] = true;
// if it's not the first node, evaluate whether pred force exists
if(x > 0){
float sf = 0.0;
// if the pred node has an alap that is greater than adj_time, calc self force at adj_time
int adj_time = time + level - 1;
if(s.asap <= adj_time){
sf = SelfForce(s, adj_time + 1, dg_mult, dg_addsub, dg_divmod, dg_logic);
}
else{
sf = 0.0;
}
successorForce += sf;
}
}
x++;
// increment the level every time the number of parents > 0
if(s.children.size() > 0){
level++;
}
for (unsigned int i = 0; i < s.children.size(); i++){
// check if the parent num is in the visisted array
if (!visited[s.children.at(i)->num]){
stack.push(*s.children.at(i));
}
}
}
cout << " Succ: " << successorForce;
float totalForce = selfForce + predecessorForce + successorForce;
cout << " Total: " << totalForce << endl;
if (totalForce * 1000 < lowestForce * 1000){
lowestForce = totalForce;
out = make_tuple(totalForce, n);
}
s = s_in;
}// end for
return out;
}
void DG(Graph g, vector<float>&dg_mult, vector<float>&dg_addsub, vector<float>&dg_divmod, vector<float>&dg_logic){
// populate the DG for each component
vector<Node>::iterator i;
// loop over all vertices / nodes, same thing
for(i = g.vertices.begin(); i != g.vertices.end(); i++){
float mobility = i->alap - i->asap;
// examine timeframe [asap, alap] and calc probability
// find the operation type so you know which vector to add to
if(i->operation.name == "MULT"){
// loop over the time intervals this operation covers
// start at asap, end at alap
for(unsigned int j = i->asap; j <= i->alap; j++){
// accumulate the probability for this component on the total
// type probability for this specific time
dg_mult[j-1] += 1 / (mobility + 1);
}
}
if(i->operation.name == "DIV" || i->operation.name == "MOD"){
// loop over the time intervals this operation covers
// start at asap, end at alap
for(unsigned int j = i->asap; j <= i->alap; j++){
// accumulate the probability for this component on the total
// type probability for this specific time
dg_divmod[j-1] += 1 / (mobility + 1);
}
}
if(i->operation.name == "ADD" || i->operation.name == "SUB"){
// loop over the time intervals this operation covers
// start at asap, end at alap
for(unsigned int j = i->asap; j <= i->alap; j++){
// accumulate the probability for this component on the total
// type probability for this specific time
dg_addsub[j-1] += 1 / (mobility + 1);
}
}
if(i->operation.name == "COMP" || i->operation.name == "MUX2x1" || i->operation.name == "SHR" || i->operation.name == "SHL" || i->operation.name == "NOT"){
// loop over the time intervals this operation covers
// start at asap, end at alap
for(unsigned int j = i->asap; j <= i->alap; j++){
// accumulate the probability for this component on the total
// type probability for this specific time
dg_logic[j-1] += 1 / (mobility + 1);
}
}
} // end DG for
}
void printVector(vector<float> vec){
for(int x = 0; x < vec.size(); x++){
cout << "[" << vec.at(x) << "] ";
}
cout << endl;
}
// assume inputs are a graph G(V,E) and latency.
// assume each vertex has a field that gives it's ASAP, ALAP time.
void FDS(Graph &g, int latency){
// DG(i) variables (probabilities per type at a given time)
// intialized to 0 and size latency constraint lambda (time)
vector<Node>::iterator k;
int num_nodes = g.vertices.size();
cout << "FDS -> NODE COUNT: " << num_nodes << endl << endl;
// loop through all the nodes to get the foce for each.
for(k = g.vertices.begin(); k != g.vertices.end(); k++){
cout << "beginning node: " << k->num << " time frame: [" << k->asap << ", " << k->alap << "]"<< endl;
// 1) cacl timeframes
// 2) calc the operation and type probabilites
// 3) calc self forces, predecessor/successor forces and total forces
// 4) schedule the operation with the least force and update its timeframe
// create DGs
vector<float> dg_mult(latency, 0);
vector<float> dg_addsub(latency, 0);
vector<float> dg_divmod(latency, 0);
vector<float> dg_logic(latency, 0);
DG(g, dg_mult, dg_addsub, dg_divmod, dg_logic);
cout << "DG_MULT: ";
printVector(dg_mult);
cout << "DG_ADD/SUB: ";
printVector(dg_addsub);
cout << "DG_DIV/MOD: ";
printVector(dg_divmod);
cout << "DG_LOGICAL: ";
printVector(dg_logic);
// calc the forces
tuple<float, int> ft = TotalForce(*k, num_nodes, dg_mult, dg_addsub, dg_divmod, dg_logic);
cout << "lowest total force: " << get<0>(ft) << " at time: " << get<1>(ft) << endl;
// schedule the node
k->fds = k->asap = k->alap = get<1>(ft);
k->scheduled = true;
cout << endl <<"SCHEDULED -> NODE: " << k->num << " FDS: " << k->fds << " ASAP: " << k->asap << " ALAP: " << k->alap << endl << endl;
// change all times for nodes connected to scheduled node
vector<Node>::iterator k2;
// loop through all nodes
for(k2 = g.vertices.begin(); k2 != g.vertices.end(); k2++){
// Nodes that have this node as a parent
// if they have the scheduled node as a parent, and there is an asap overlap, restrict it to time +1
for(unsigned int p = 0; p < k2->parent.size(); p++){
// compare the parent nodes to the scheduled node
if(k2->parent.at(p)->num == k->num){
// update the parent's status to scheduled and time frames updated in this node
k2->parent.at(p)->alap = k2->parent.at(p)->asap = k2->parent.at(p)->fds = get<1>(ft);
// if the asap and scheduled nodes times overlap
if(k2->asap <= get<1>(ft)){
// restrict it to time + 1
k2->asap = get<1>(ft) + 1;
}
}
}
// Nodes that have this node as a child
for(unsigned int p = 0; p < k2->children.size(); p++){
// compare the children nodes to the scheduled node
if(k2->children.at(p)->num == k->num){
// update the parent's status to scheduled and time frames updated in this node
k2->children.at(p)->alap = k2->children.at(p)->asap = k2->children.at(p)->fds = get<1>(ft);
// if the alap and scheduled nodes times overlap
if(k2->alap >= get<1>(ft)){
// restrict it to time + 1
k2->alap = get<1>(ft) - 1;
}
}
}
}