/
main2.py
370 lines (244 loc) · 9.09 KB
/
main2.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
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
import boolfunction as bf
import bdd
import math
import basicfunctions as bas
import copy
from operator import itemgetter # to sort dictionary of weight values
from sys import exit
import cProfile
#------- file read ---------------------------------------------
#f = open('absp2.pla','r')
#f = open('absp_i28.pla','r') #i28
#f = open('blif_src/spla.pla','r') #i16
#f = open('blif_src/apex2.pla','r') #i39
#f = open('blif_src/seq.pla','r') #i41
#f = open('blif_src/ex1010.pla','r') #i10
#f = open('blif_src/pdc.pla','r') #i16
#f = open('blif_src/apex4.pla','r') #i9
#f = open('blif_src/misex3.pla','r') #i14
f = open('blif_src/ex5.pla','r') #i8
# define your k here
k = 3
# naive decomposition
decNaive = True
content = f.readlines()
f.close()
#------- splitting header / boundset / freeset ---------------------
content2 = []
equations = []
for line in content:
content2.append(line.split(' '))
for line in content2:
if line[0] == '.i':
inputs = int(line[1][:-1])
if line[0] == '.o':
outputs = int(line[1][:-1])
if line[0][0] != '.' and len(line) >= 2:
equations.append([ line[0] , line[1][:-1]])
#------- create minterms from blif equations ---------------------
outputs = 1 # comment if all outputs shall computed !!!!!
maxtermArray = []
variableOrderArray = []
for i in range(outputs):
maxtermArray.append(bf.Maxterm(i)) # i is index, increment for each output
for output in range(outputs): # for each output
equations_ONset = [] # only ON set equations
equations_NumberOfCares = [] # gives a number for each line, which indicates how many 0s or 1s are included
for line in equations:
if line[1][output] == '1': # select the line with an 1 at the end to create the minterm
#print line[0]
equations_ONset.append(line[0])
tempCareCounter = 0
for position in range(inputs):
if (line[0][position] == '0' or line[0][position] == '1'):
tempCareCounter += 1
equations_NumberOfCares.append(float(tempCareCounter))
#------- sort inputs depending on variable weights ----------------
weight_dic = {} # dictionary in the form of {'x1': 0.7337, 'x2': 0.1234, ... }
numberOfLines = float(len(equations_ONset))
# all values to zero
for i in range(inputs):
weight_dic['x' + str(i+1)]=0
# update weigth for each variable
for i in range(inputs):
for index, line in enumerate(equations_ONset):
if(line[i] == '0' or line[i] == '1'):
weight_dic['x' + str(i+1)] += 1.0/equations_NumberOfCares[index] * 1.0/numberOfLines
# sort weight_dic
weight_dic = sorted(weight_dic.items(), key=itemgetter(1)) # notice that dictionary becomes a list of tuples now
# weight list becomes a list of variable indeces in sorted order [9, 3, ...]
weight_dic_int = []
for var in weight_dic:
weight_dic_int.append(int(var[0][1:]))
# lowest value at the beginning -> must be reversed
weight_dic_int.reverse()
# add the initial order to an array
variableOrderArray.append(weight_dic_int)
# finally build the maxterm
for line in equations_ONset:
maxtermArray[output].addMinterm(bf.buildMinterm(line))
#-------- building tree -----------------------------------------
print "\n... creating tree",
resultTree = bdd.doShannon(maxtermArray[0],1, inputs, weight_dic_int) # must be done for every output !!!!
#cProfile.run("bdd.doShannon(maxtermArray[0],1, inputs)")
#print resultTree
print "\n\n... creating QRBDD"
resultTree.makeQRBDD()
print "\n\n ... updating level"
bdd.updateLevel(resultTree)
#bdd.doSifting(resultTree)
#cProfile.run("bdd.doSifting(resultTree)")
# update the variable order after sifting:
weight_dic_int=bdd.getVariableOrder(resultTree,[])
print "\nNumber of Nodes:",bdd.countNodes(resultTree)
'''
cutTrees = bdd.cutTreeAtHeight(resultTree, 2)
cnt = 0
for tree in cutTrees:
tree.dotPrint2("subtree" + str(cnt))
cnt += 1
'''
#cutTrees = bdd.cutTreeAtHeight(resultTree, 2)
#cnt = 0
#for tree in cutTrees:
# tree.dotPrint2("subtree" + str(cnt))
# cnt += 1
#bdd.transformToLUT([2,1],resultTree)
#--------create LUT structure ------------------------------------
# create ld(my) for every level (actually it is not ld(my), because every ld(my)=0 becomes ld(my)=1)
print "\n... calculating 'my' for each level"
setOfLdMy = []
for levels in range(1,inputs):
ldmy = int(math.ceil(math.log(bdd.getMy(resultTree,levels+1),2)))
# ldmy get modify if ldmy=0 -> necessary for doNaiveDecomp function
if(ldmy == 0):
ldmy = 1
setOfLdMy.append(ldmy)
# naive or smart decomposition?
if(decNaive == True):
print "\n... creating LUT structure"
# here comes the actual decomposition algorithm which returns just an array of arrays/integers and a array of integers with the cut position within the tree
lutstruc, cutHeights = resultTree.doNaiveDecomp(k, weight_dic_int, setOfLdMy)
print "\nChosen %s-LUT structure:" %k
print lutstruc
else:
# smarter decomposition with the help of a gain for each level here:
cutHeights = resultTree.doSmartDecomp(k, weight_dic_int, setOfLdMy)
print "\nCut positions:"
print cutHeights
ultimativeArray = bdd.encodeCutNodes(resultTree,cutHeights)
outputCore = bdd.getBLIF(ultimativeArray, resultTree)
#------write BLIF file ------------------------------------------
print "\n... creating BLIF file"
outputName = f.name.split('/')[-1].split('.')[0]+ '_%s_feasible' %k
outputContent = ".model " + outputName + "\n.inputs"
for inVar in range(1,inputs+1):
outputContent += " x%s" %inVar
outputContent += "\n.outputs y\n\n" + outputCore
print "\n########## BLIF #########\n\n" + outputContent
print "#########################"
outputFile = open(outputName + ".blif", 'w')
outputFile.write(outputContent)
outputFile.close()
# ------- PLOT --------------------------------------------------
print "\n\n... plotting tree"
resultTree.dotPrint2()
print "\nDone."
exit(1)
'''
#------write BLIF file ------------------------------------------
print "\n... creating BLIF file"
resultTree.dotPrint2()
outputContent = ".model " + f.name.split('/')[-1].split('.')[0] + '_%s_feasible\n.inputs' %k
for inVar in range(0,inputs):
outputContent += " x%s" %inVar
outputContent += "\n.outputs"
for outVar in range(0,outputs):
outputContent += " y%s" %outVar
# note: every list of 'lutstruc' has k elements except of the deepest lists; elements which arent integers but lists are at the end
templutstruc = lutstruc
# first iterate to deepest list
itdepth = 0
while (bas.count_lists(templutstruc) != 0):
templutstruc = templutstruc[k-1]
itdepth += 1
### INITIAL STEP
tempTree = copy.deepcopy(resultTree)
cutNodes = bdd.getArrayOfLvlNodes(resultTree, len(templutstruc)+1)
tempTree.cutTreeAtHeight(len(templutstruc), cutNodes)
print "Temptree:",tempTree
outputContent += "\n.names"
for initElem in range(len(templutstruc)):
outputContent += " x" + str(templutstruc[-1-initElem])
outputContent += " h" + str(itdepth)
outputContent += "\n" + bdd.bddToBlif(tempTree, 1)
treelvl = len(templutstruc)
itdepth -= 1
### ITERATION
base = 2 # = maximal ld(my), will be computed later
while (itdepth != -1):
# first get the next sub-LUT structure
templutstruc = lutstruc
for depthlvl in range(itdepth):
templutstruc = templutstruc[-1]
#print "\ntemplutstruc:", templutstruc
rootNodes = bdd.getArrayOfLvlNodes(resultTree, treelvl+1)
tempTree = copy.deepcopy(rootNodes[0])
cutNodes = bdd.getArrayOfLvlNodes(tempTree, k)
tempTree.cutTreeAtHeight(k-1, cutNodes)
print "Temptree:",tempTree
if(itdepth == 0):
if(bdd.bddToBlif(tempTree,base) == ' 1' or bdd.bddToBlif(tempTree,base)):
outputContent += "\n.names " + "h" + str(itdepth+1) + " y" + str(outputs-1)
else:
outputContent += "\n.names " + "h" + str(itdepth+1)
for itElem in range(k-1):
outputContent += " x" + str(templutstruc[-2-itElem])
outputContent += " y" + str(outputs-1)
else:
if(bdd.bddToBlif(tempTree, base) == ' 1'):
outputContent += "\n.names " + "h" + str(itdepth+1) + " h0" + str(itdepth)
else:
outputContent += "\n.names " + "h" + str(itdepth+1)
for itElem in range(k-1):
outputContent += " x" + str(templutstruc[-2-itElem])
outputContent += " h" + str(itdepth)
outputContent += '\n' + bdd.bddToBlif(tempTree, base)
treelvl += (k-1)
itdepth -=1
print "____________________________________"
print "BLIF file:"
print outputContent
print "\nDone."
## my for each level
#print "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
#print "'My' for each height:"
#for levels in range(1,inputs):
# my = bdd.getMy(resultTree,levels+1)
# print "Level",levels,":", my
# gain = levels - int(math.ceil(math.log(my,2)))
# print "Gain:", gain
'''
'''
# Example for shannon expansion
print "Maxterm: "
print maxtermArray[0]
print "First expansion with x1: "
# allocation
maxterm1 = copy.deepcopy(maxtermArray[0])
maxterm2 = copy.deepcopy(maxtermArray[0])
maxterm1.setLiteralTrue('x1')
maxterm2.setLiteralFalse('x1')
print "x1=1:", maxterm1
print "x1=0:", maxterm2
print maxterm2 == maxterm1
maxterm1.setLiteralTrue('x2')
print "x1=1:", maxterm1
maxterm1.setLiteralTrue('x3')
print "x1=1:", maxterm1
maxterm1.setLiteralTrue('x4')
print "x1=1:", maxterm1
print maxterm1 == 1
print maxterm1 == [1]
print maxterm1 == 1
'''