-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_r.java
382 lines (331 loc) · 11.8 KB
/
list_r.java
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
import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class list_r {
private ArrayList<V> vertices;
private int v_id;
public list_r() {
vertices = new ArrayList<V>();
vertices.add(new V(0, null, null) ); // Top Sink node
vertices.add(new V(-1, null, null) ); // Bottom Sink node
v_id = 1;
}
// Adds all vertices for specified section.
// This gets called after all the declarations have been instantiated in the verilog file.
// Variable "x" is the first line of statements in the input file'
public void addVertices(ArrayList<ArrayList<String>> lines, int x) {
// Adds new vertex for each statement
for(int i = x; i < lines.size(); i++) {
//ArrayList<String> line = tokenize(lines[i], " ");
ArrayList<String> line = lines.get(i);
// CHANGE BACK TO LINE
String operator = line.get(line.size() - 2);
vertices.add(new V(v_id, operator, line.get(0)) ); // Adds new vertex
v_id++;
}
}
// Searches for dependencies within the statements
public void addDependencies(ArrayList<ArrayList<String>> lines, int x) {
Set<String> set = new HashSet<String>(); // Set to hold variables already worked on (dependencies)
for(int i = x; i < lines.size(); i++ ) {
//ArrayList<String> line = tokenize(lines.get[i], " ");
ArrayList<String> line = lines.get(i);
// HAVE NOT ACCOUNTED FOR IF STATEMENTS YET
// Determine if case: o = sel ? i1 : i0... I have not accounted for this case yet
if(line.get(line.size()-2) != ":") {
String var0 = line.get(0);
String var1 = line.get(1);
String var2 = line.get(line.size()-1);
// If no dependency, add edge from sink node
if(!set.contains(var1) && !set.contains(var2)) {
vertices.get(0).addSuccessor(findV(var0)); // Adds successor to sink node
findV(var0).addPredecessors(vertices.get(0)); // Adds predecessor
set.add(var0);
}
/* Initial Time is time from CDFG. Needed to compute slack for List_R scheduling */
else {
if(set.contains(var1) && set.contains(var2)) {
findV(var1).addSuccessor(findV(var0)); // Adds successor
findV(var2).addSuccessor(findV(var0)); // Adds successor
findV(var0).addPredecessors(findV(var1)); // Adds predecessor
findV(var0).addPredecessors(findV(var2)); // Adds predecessor
findV(var0).setInitialTime(Math.max(findV(var1).getInitialTime() + findV(var1).getDelay(), findV(var2).getInitialTime() + findV(var2).getDelay()));
}
else if(set.contains(var1)) {
findV(var1).addSuccessor(findV(var0)); // Adds successor
findV(var0).addPredecessors(findV(var1)); // Adds predecessor
findV(var0).setInitialTime(findV(var1).getInitialTime() + findV(var1).getDelay());
}
else if(set.contains(var2)) {
findV(var2).addSuccessor(findV(var0)); // Adds successor
findV(var0).addPredecessors(findV(var2)); // Adds predecessor
findV(var0).setInitialTime(findV(var2).getInitialTime() + findV(var2).getDelay());
}
set.add(var0);
vertices.get(0).addSuccessor(findV(var0)); // Adds successor to sink node
}
}
}
// Adds dependencies from leaf to bottom Sink node
for(int j = 0; j < vertices.size(); j++) {
if(vertices.get(j).getSuccessorSize() == 0) { // If leaf, add successor to bottom sink node
vertices.get(j).addSuccessor(vertices.get(1));
}
vertices.get(1).addPredecessors(vertices.get(j)); // Adds all vertices as predecessor to bottom sink node
}
}
// Performs ALAP scheduling
public void alap(int latency) {
// Schedules the bottom sink node for upper latency bound + 1 and sets isScheduled to true
vertices.get(1).setTiming(latency + 1);
vertices.get(1).setScheduled(true);
// while all vertices are not scheduled, run the algorithm
while(!allVerticesScheduled()) {
// Iterate through vertices, find a vertex where all successors are scheduled
for(int i = 0; i < vertices.size(); i++) {
V currentV = vertices.get(i);
boolean valid = true; // boolean to check if all successors are scheduled
int lowestTime = 100; // For multiple successors, holds minimum time
if(currentV.getIsScheduled() == false) { // If vertex is not scheduled, check all it's successors to determine if their scheduled
ArrayList<V> successors = currentV.getSuccessors();
for(int j = 0; j < successors.size(); j++) {
if(successors.get(j).getIsScheduled() == false) { // If all successors are not scheduled, break
valid = false;
break;
}
else {
if(successors.get(j).getTiming() < lowestTime) {
lowestTime = successors.get(j).getTiming();
}
}
}
if(valid == true) {
if(lowestTime-vertices.get(i).getDelay() < 1 && i != 0) {
System.out.println("Latency not possible!");
return;
}
vertices.get(i).setTiming(lowestTime - vertices.get(i).getDelay());
vertices.get(i).setScheduled(true);
}
}
}
}
// Top sink node is always time 0
vertices.get(0).setTiming(0);
}
/* Runs list_r scheduling */
public void listR_Scheduling() {
int[] resources = new int[3];
resources[0] = 1; // Resources requirement for ALU
resources[1] = 1; // Resources requirement for Multiplier
resources[2] = 1; // Resources requirement for Divider/Modulo
// Unschedules all vertices besides top sink node
for(int x = 1; x < vertices.size(); x++) {
vertices.get(x).setScheduled(false);
}
vertices.get(0).setScheduled(true); // Sets top sink node to scheduled
vertices.get(1).setScheduled(true); // Sets top sink node to scheduled
int time_step = 1;
Set<V> unscheduled = new HashSet<V>();
while(!allVerticesScheduled()) {
//System.out.print("Unscheduled: " );
for(int i = 2; i < vertices.size(); i++) {
if(vertices.get(i).getInitialTime() == time_step && !vertices.get(i).getIsScheduled()) {
unscheduled.add(vertices.get(i));
//System.out.print(vertices.get(i).getName() + ", ");
}
}
//System.out.println();
/* Holds current # of resources at this time step */
int mult_count = 0;
int div_modulo_count = 0;
int alu_count = 0;
Iterator<V> it = unscheduled.iterator();
while(it.hasNext()) {
V curr = (V) it.next();
// If slack = 0, we must schedule now
if(findV(curr.getName()).getTiming() - time_step == 0 && allPredecessorsScheduled(findV(curr.getName()))) {
findV(curr.getName()).setFinalTime(time_step);
findV(curr.getName()).setScheduled(true);
it.remove();
unscheduled.remove(findV(curr.getName()));
/* Updates resources */
if(findV(curr.getName()).getOperation() == "*" ) {
mult_count++;
if(mult_count > resources[1]) {
resources[1]++;
}
}
else if(findV(curr.getName()).getOperation() == "/" || findV(curr.getName()).getOperation() == "%") {
div_modulo_count++;
if(div_modulo_count > resources[2]) {
resources[2]++;
}
}
else {
alu_count++;
if(alu_count > resources[0]) {
resources[0]++;
}
}
}
}
printScheduled();
/* Schedule operations requiring no additional resources */
while(mult_count < resources[1]) {
Iterator<V> it1 = unscheduled.iterator();
boolean found = false; // Variable to determine if any var is found
while(it1.hasNext()) {
V curr = (V) it1.next();
if(findV(curr.getName()).getDelay() == 2 ) {
findV(curr.getName()).setFinalTime(time_step);
findV(curr.getName()).setScheduled(true);
it1.remove();
unscheduled.remove(findV(curr.getName()));
mult_count++;
found = true;
break;
}
}
if(!found) {
break;
}
}
while(div_modulo_count < resources[2]) {
Iterator<V> it1 = unscheduled.iterator();
boolean found = false;
while(it1.hasNext()) {
V curr = (V) it1.next();
if(findV(curr.getName()).getDelay() == 3 ) {
findV(curr.getName()).setFinalTime(time_step);
findV(curr.getName()).setScheduled(true);
it1.remove();
unscheduled.remove(findV(curr.getName()));
div_modulo_count++;
found = true;
break;
}
}
if(!found) {
break;
}
}
while(alu_count < resources[0]) {
Iterator<V> it1 = unscheduled.iterator();
boolean found = false;
while(it1.hasNext()) {
V curr = (V) it1.next();
if(findV(curr.getName()).getDelay() == 1 ) {
findV(curr.getName()).setFinalTime(time_step);
findV(curr.getName()).setScheduled(true);
it1.remove();
unscheduled.remove(findV(curr.getName()));
alu_count++;
found = true;
break;
}
}
if(!found) {
break;
}
}
//printScheduled();
time_step++;
}
}
// Finds vertex by name
public V findV(String varName) {
for(int i = 0; i < vertices.size(); i++) {
if(vertices.get(i).getID() != 0 && vertices.get(i).getID() != -1 && vertices.get(i).varName.equals(varName)) {
return vertices.get(i);
}
}
return null;
}
public void printFinalTime() {
for(int i = 0; i < vertices.size(); i++) {
System.out.println(vertices.get(i).getName() + " is scheduled in time " + vertices.get(i).getFinalTime());
}
}
// Returns true if all vertices scheduled, false otherwise
public boolean allVerticesScheduled() {
for(int i = 0; i < vertices.size(); i++) {
if(vertices.get(i).getIsScheduled() == false) {
return false;
}
}
return true;
}
// Breaks up statement into array of words
public ArrayList<String> tokenize(String s, String tokenizer) {
int pos;
ArrayList<String> result = new ArrayList<String>();
while ( (pos = s.indexOf(tokenizer) ) != s.length()) {
result.add(s.substring(0, pos));
String temp = s.substring(0, pos + tokenizer.length());
s = s.replace(temp, "");
}
if (s != " ") {
result.add(s);
}
return result;
}
// True if all predecessors scheduled, false otherwise
public boolean allPredecessorsScheduled(V curr) {
for(int i = 0; i < curr.getPredecessors().size(); i++) {
if(!curr.getPredecessors().get(i).getIsScheduled()) {
return false;
}
}
return true;
}
public void printV() {
for(int i = 0; i < vertices.size(); i++) {
System.out.println("Vertex #" + i + ": " + vertices.get(i).getName() + ", " + vertices.get(i).getOperation());
}
}
public void printDependencies() {
for(int i = 0; i < vertices.size(); i++) {
System.out.print("Vertex " + vertices.get(i).getName() + " has the following dependencies: ");
for(int j = 0; j < vertices.get(i).getSuccessorSize(); j++) {
System.out.print(vertices.get(i).getSuccessors().get(j).getName() + ", ");
}
System.out.println();
}
}
public void printPredecessors() {
for(int i = 0; i < vertices.size(); i++) {
System.out.print("Vertex " + vertices.get(i).getName() + " has the following predecessors: ");
for(int j = 0; j < vertices.get(i).getPredecessors().size(); j++) {
System.out.print(vertices.get(i).getPredecessors().get(j).getName() + ", ");
}
System.out.println();
}
}
public void print_alap() {
for(int i = 0; i < vertices.size(); i++) {
System.out.println("Vertex " + vertices.get(i).getName() + " is scheduled in time " + vertices.get(i).getTiming());
}
}
public void printDelays() {
for(int i = 0; i < vertices.size(); i++) {
System.out.println(vertices.get(i).getDelay());
}
}
public void printScheduled() {
System.out.print("Scheduled: ");
for(int i = 0; i < vertices.size(); i++) {
if(vertices.get(i).getIsScheduled()) {
System.out.print(vertices.get(i).getName() + ", ");
}
}
System.out.println();
}
public void printInitialTimes() {
for(int i = 2; i < vertices.size(); i++) {
System.out.println(vertices.get(i).initialTime);
}
}
}