-
Notifications
You must be signed in to change notification settings - Fork 0
/
caproject.py
328 lines (323 loc) · 9.69 KB
/
caproject.py
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
import itertools
import re
import time
#-----------------------------------------------------------------------------------------------
def find(fin,sublist,inst):
inst=set(inst)
target=[]
templist=[]
count=0
for each in fin:
count+=1
each=set(each)
inst=set(inst)
new=inst.difference(each)
templist=list(itertools.combinations(new,len(sublist)))
tar1=list(each)
for i in templist:
tar1=list(each)
for j in list(i):
tar1.append(j)
target.append(tar1)
return target
#-----------------------------------------------------------------------------------------------
def out_of_order(l1,l2):
fin=[]
fin=list(itertools.combinations(l2,len(l1[0])))
for i in range(1,len(l1)):
fin=find(fin,l1[i],l2)
l_1=[]
for l in l1:
for i in l:
l_1.append(i)
final=[]
for i in range(0,len(fin)):
temp=[]
for j in range(0,len(l2)):
temp.append(0)
for j in range(0,len(fin[i])):
temp[fin[i][j]-1]=l_1[j]
final.append(temp)
return final
#-----------------------------------------------------------------------------------------------
#Declaring variables
list1=['add','addi','sub','subi','multi','bne','beq','bnz']
list2=['lw','la','li','move']
list3=['sw','sa','sb']
read=['lw','la','add']
datatypes=['.asciiz','.byte','.word','.text','.data','.globl','syscall',':']
fname=raw_input("Enter file name: ")
instruction=["NULL"]
variables={}
operations=["NULL"]
counter=0
#Function returns first item of list
#---------------------------------------------
def getkey(item):
return item[0]
#---------------------------------------------
#Reading the file line by line
#---------------------------------------------
with open(fname,"r") as fpointer:
#---------------------------------------------
for line in fpointer:
flag=1
for j in datatypes:
if line.find(j)!=-1:
flag=0
break
#Check if line is comment
#---------------------------------------------
if line[0]=='#' or line[0]=='\n':
continue
#Check if line is assembler instruction
#---------------------------------------------
elif flag==0:
continue
#---------------------------------------------
else:
counter+=1
div=line.split(",")
instruction.append(div[0].split()[0])
#pre-processing
#---------------------------------------------
div[0]=div[0].split()[1]
div[-1]=div[-1].split('#')[0]
for i in range(0,len(div)):
div[i]=div[i].strip()
p=re.match(r'\d*\({1}\${1}\w+\W{1}',div[i])
if p is not None:
div[i]=div[i].split('(')[1].split(')')[0]
div[i]=div[i].strip()
#variables and instructions
#---------------------------------------------
operations.append([instruction[counter]])
for i in div:
operations[counter].append(i)
for i in range(0,len(div)):
k=len(div)-i-1
if not variables.has_key(div[k]):
#print div[k]
#---------------------------------------------
if instruction[counter] in list1:
if k is 0:
variables[div[k]]=[[counter,div[1],div[2]]]
else:
variables[div[k]]=[[counter]]
elif instruction[counter] in list2:
if k is 0:
variables[div[k]]=[[counter,div[1]]]
else:
variables[div[k]]=[[counter]]
elif instruction[counter] in list3:
if k is 1:
variables[div[k]]=[[counter,div[0]]]
else:
variables[div[k]]=[[counter]]
else:
if instruction[counter] in list1:
if k is 0:
prev=variables[div[k]]
prev.append([counter,div[1],div[2]])
variables[div[k]]=prev
else:
prev=variables[div[k]]
prev.append([counter])
variables[div[k]]=prev
elif instruction[counter] in list2:
if k is 0:
prev=variables[div[k]]
prev.append([counter,div[1]])
variables[div[k]]=prev
else:
prev=variables[div[k]]
prev.append([counter])
variables[div[k]]=prev
elif instruction[counter] in list3:
if k is 1:
prev=variables[div[k]]
prev.append([counter,div[0]])
variables[div[k]]=prev
else:
prev=variables[div[k]]
prev.append([counter])
variables[div[k]]=prev
#---------------------------------------------
l=[]
exclude=['$zero']
for x in variables.iterkeys():
if x in exclude or x.isdigit() or str(x)[0]!='$':
l.append(x)
#---------------------------------------------
for i in l:
del variables[i]
#Dependencies calculated
#---------------------------------------------
raw=[]
prev="NULL"
a=['sw','sb']
b=['la','lb','lw','move','li']
prev1="NULL"
first="NULL"
waw=[]
war=[]
#---------------------------------------------
for x in variables.iterkeys():
for l in variables[x]:
if len(l)<2 or (len(l)==2 and instruction[l[0]] in a):
first=l[0]
if prev!="NULL":
if first!=prev:
raw.append(sorted([first,prev])) #prev=>write,first=>read
if len(l)>2 or (len(l)==2 and instruction[l[0]] in b):
prev=l[0]
first1=l[0]
if prev1!="NULL":
if first1!=prev1:
waw.append(sorted([first1,prev1]))
if first !="NULL" and first!=prev:
war.append(sorted([prev,first]))
prev1=first1
prev="NULL"
prev1="NULL"
first="NULL"
#---------------------------------------------
print "Calculating dependencies..."
time.sleep(1)
raw=sorted(raw,key=getkey)
waw=sorted(waw,key=getkey)
war=sorted(war,key=getkey)
print "The dependencies calculated are:"
print "Read after write: ",raw
print "Write after write: ",waw
print "Write after read: ",war
#---------------------------------------------
depset=[]
for c in war:
c=sorted(c)
if c not in depset:
depset.append(c)
for c in raw:
c=sorted(c)
if c not in depset:
depset.append(c)
for b in waw:
b=sorted(b)
if b not in depset:
depset.append(b)
depset=sorted(depset,key=getkey)
#-----------------------------------------------------------------------------------------------
s=raw_input("Enter number of pipelines:\n1. One\n2. Infinite\n ")
if s=='1':
chunk=[]
cout=0
for i in depset:
flag=0
for k in chunk:
if i[1] in k:
k.add(i[0])
flag=1
if i[0] in k:
k.add(i[1])
flag=1
if flag==0:
chunk.append(set(i))
for i in range(0,len(chunk)-1):
for j in range(i+1,len(chunk)):
if len(chunk[i]&chunk[j])>0:
chunk[j]=chunk[i]|chunk[j]
chunk[i].add(-1)
chunk2=[]
for i in chunk:
if -1 not in i:
chunk2.append(i)
chunk=chunk2
print chunk
#---------------------------------------------
a=[]
for i in range(1,len(instruction)):
a.append(i)
#---------------------------------------------
#In order execution CPI
stall=0
print "Trying in order execution first:"
for j in range(1,len(a)):
if sorted([a[j-1],a[j]]) in raw:
if instruction[a[j-1]]=='lw':
stall+=1
elif instruction[a[j-1]]=='sw':
stall+=2
k=float(len(a)+stall)/len(a)
print "CPI for in-order execution is : ",k
time.sleep(1)
#---------------------------------------------
#Out of order execution for different permutations
print "Trying out-of-order execution for different permutations of the instructions(without violating dependencies)"
#---------------------------------------------
fin=out_of_order(chunk,a)
print "Found ",len(fin)," different out-of-order executions"
#---------------------------------------------
mincpi=999999999999.0
count=0
time.sleep(1)
#---------------------------------------------
for i in fin:
count+=1
print "For execution ",count,": ",
stall=0
for j in range(1,len(i)):
if sorted([i[j-1],i[j]]) in raw:
if instruction[i[j-1]]=='lw':
stall+=1
elif instruction[i[j-1]]=='sw':
stall+=2
k=float(len(i)+stall)/len(i)
print "CPI = ",k
if mincpi>k:
mincpi=k
#---------------------------------------------
time.sleep(1)
print "The minimum CPI found is : ",mincpi
#------------------------------------------------------------------------------------------------------
elif s=='2':
print "Trying execution in parallel pipelines whenever possible"
timer=[0]
time.sleep(1)
executed=[]
#---------------------------------------------
for i in range(1,len(instruction)):
timer.append(0)
flag=0
temp=0
if len(executed)>0:
for j in executed:
if sorted([i,j]) in raw:
if instruction[j]=='lw':
temp=2+timer[j]
flag=1
elif instruction[j]=='sw':
temp=1+timer[j]
flag=1
else:
temp=1+timer[j]
flag=1
elif sorted([i,j]) in war:
temp=timer[j]+0
flag=1
elif sorted([i,j]) in waw:
temp=timer[j]+1
flag=1
if timer[i]<temp:
timer[i]=temp
if flag==0:
timer[i]=5
executed.append(i)
else:
timer[i]=5
executed.append(i)
#---------------------------------------------
print "Execution calculated:"
for i in range(1,len(instruction)):
print "Instruction ",i," enters at t=",timer[i]-5
print "CPI = ",float(max(timer)-5+1)/(len(instruction)-1)
#----------------------------------------------------------------------------------------------------------