]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/evaluate.py
Fix utils.callsubprocess stdin
[linux-conf-perf.git] / scripts / evaluate.py
1 #!/usr/bin/env python3
2 import os
3 import sys
4
5 import numpy.linalg as nplag
6
7 from conf import conf
8 from conf import sf
9 import utils
10
11 def reduce_matrix_search_for_base_recurse(wset, columns, contains, ignore):
12         bases = []
13         for x in range(0, len(columns)):
14                 if x in contains or x in ignore:
15                         continue
16                 colide = False
17                 for i in range(0, len(wset)):
18                         if wset[i] == 1 and columns[x][i] == 1:
19                                 colide = True
20                                 break
21                 if not colide:
22                         newset = list(wset)
23                         onecount = 0
24                         for i in range(0, len(newset)):
25                                 newset[i] = newset[i] | columns[x][i]
26                                 if (newset[i] == 1):
27                                         onecount += 1
28                         contains.add(x)
29                         if onecount == len(newset):
30                                 bases.append(set(contains))
31                         else:
32                                 rbases = reduce_matrix_search_for_base_recurse(newset, columns, contains, ignore)
33                                 for rbase in rbases:
34                                         if not rbase in bases:
35                                                 bases.append(rbase)
36                         contains.remove(x)
37         return bases
38 0
39 def reduce_matrix_search_for_base(columns):
40         bases = []
41         ignore = []
42         for i in range(0, len(columns)):
43                 wset = list(columns[i])
44                 ignore.append(i)
45                 bases.extend(reduce_matrix_search_for_base_recurse(wset, columns, {i}, ignore))
46         return bases
47
48 def reduce_matrix_remove_symbol(A, symrow, indx):
49         del symrow[indx]
50         for i in range(0, len(A)):
51                 del A[i][indx]
52
53 def reduce_matrix(A, symrow, bases):
54         # Remove fixed symbols
55         i = len(A[0]) - 1
56         while i >= 0:
57                 strue = False
58                 sfalse = False
59                 for y in range(0, len(A)):
60                         if A[y][i] == 0:
61                                 sfalse = True
62                         else:
63                                 strue = True
64                 if (strue and not sfalse) or (sfalse and not strue):
65                         reduce_matrix_remove_symbol(A, symrow, i)
66                 i -= 1
67
68         # Remove duplicate symbols
69         i = len(A[0]) - 1
70         columns = []
71         while i >= 0:
72                 column = []
73                 for y in range(0, len(A)):
74                         column.append(A[y][i])
75                 if column in columns:
76                         reduce_matrix_remove_symbol(A, symrow,  i)
77                 else:
78                         columns.append(column)
79                 i -= 1
80
81         # Search for Bases
82         columnsr = []
83         for i in range(len(columns) - 1, -1,-1):
84                 columnsr.append(columns[i])
85         basesx = reduce_matrix_search_for_base(columnsr)
86         if bases:
87                 for base in basesx:
88                         bases[0].append(base)
89
90         # Generate new Base
91         if bases == [[]]:
92                 for x in range(0, len(A)):
93                         A[x].append(1)
94                 symrow.append(0)
95
96 def collect_data():
97         hashs = {}
98         for fl in os.listdir(sf(conf.result_folder)):
99                 if os.path.isfile(os.path.join(sf(conf.result_folder), fl)):
100                         hashs[fl] = [[], []]
101         try:
102                 hashs.pop('NoConfig')
103         except KeyError:
104                 pass
105
106         with open(sf(conf.config_map_file)) as f:
107                 for line in f:
108                         w = line.rstrip().split(sep=':')
109                         if not w[0] or not w[0] in hashs:
110                                 continue
111                         sol = utils.config_strtoint(w[1], False)
112                         hashs[w[0]][0] = sol
113         
114         for hash, data in hashs.items():
115                 with open(os.path.join(sf(conf.result_folder), hash)) as f:
116                         vec = []
117                         for ln in f:
118                                 vec.append(float(ln))
119                         hashs[hash][1] = vec
120         return hashs
121
122 def build_matrix(hashs):
123         A = []
124         B = []
125         for hash,data in hashs.items():
126                 A.append(data[0])
127                 B.append(data[1])
128         symrow = []
129         for y in range(0, len(A[0])):
130                 symrow.append([abs(A[0][y])])
131         for x in range(0, len(A)):
132                 for y in range(0, len(A[0])):
133                         if A[x][y] < 0:
134                                 A[x][y] = 0
135                         else:
136                                 A[x][y] = 1
137         return A, B, symrow
138
139 def evaluate():
140         print("Collect data...")
141         hashs = collect_data()
142
143         print('Build matrix...')
144         A, B, symrow = build_matrix(hashs)
145
146         # Reduce matrix A
147         print('Simplify matrix...')
148         bases = []
149         reduce_matrix(A, symrow, [bases])
150
151         # Calculate value
152         print('Figuring values...')
153         R = nplag.lstsq(A, B)
154
155         # Print result
156         print('--------------------')
157         utils.build_symbol_map()
158         for i in range(0, len(R[0])):
159                 if symrow[i] == 0:
160                         print("Base", end=' ')
161                 else:
162                         if len(bases) > 0:
163                                 if i in bases[0]:
164                                         print("Base", end=' ')
165                         elif len(bases) > 1:
166                                 for x in range(0, len(bases)):
167                                         if i in bases[x]:
168                                                 print("Base" + x, end=' ')
169                         for s in symrow[i]:
170                                 print(utils.smap[s], end=' ')
171                 print("=", end=' ')
172                 print(str(R[0][i]))
173
174
175 #################################################################################
176
177 if __name__ == '__main__':
178         evaluate()