-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.py
More file actions
executable file
·242 lines (196 loc) · 8.29 KB
/
Copy pathconfiguration.py
File metadata and controls
executable file
·242 lines (196 loc) · 8.29 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
'''
BenchRunner
Copyright (C) 2010 Simone Pellegrini
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from parameter import Parameter, OutOfBoundsExcection
from copy import deepcopy
import functools, math
from util import sobstitute, convert, avg, std, toStr
import os, threading
class Constrain:
def __init__(self, inv):
self.__constrain = inv
@property
def constrain(self):
return self.__constrain
def check(self, conf):
try:
con = sobstitute(self.__constrain, conf)
ret = eval(con)
if not ret:
print "Failed to satisfy constrain: {0}".format(con)
except KeyError, err:
# print ("Warning: invariant '{0}' couldn't be checked"
# "for the current configuration.").format(self.__constrain)
return True
except SyntaxError, err:
raise "SyntaxError in constrain '{0}' -> {1}".format(self.__constrain, con)
return ret
def __str__(self):
return str(self.__constrain)
def __eq__(self, other):
return self.constrain == other.constrain
def __hash__(self):
return hash(self.__constrain)
def intersect(param_list):
params = list(param_list)
if params is None or len(params) == 0:
return []
diff = set(params[0])
for it in params[1:]:
diff &= set(it)
return diff
class Configuration:
def __init__(self):
self.__parameters = {}
self.__order = []
self.__sync = threading.Condition()
self.__speedup = None
def __add__(self, conf):
# adding 2 configuration
# we check if there are parameters in common
if len(intersect([self.parameters.values(), conf.parameters.values()])) > 0:
# some parameters are in common, we cannot create the union config
raise "cannot sum"
# everything ok, we start copying the parameters
ret = Configuration()
# params = set(self.__parameters.values()[:]) | set(conf.parameters.values()[:])
for k in self.__order + conf.__order:
p = self.__parameters[k] if k in self.__order else conf.__parameters[k]
ret += deepcopy(p)
return ret
def __iadd__(self, param):
if isinstance(param, Parameter):
assert param.name not in self.__parameters.keys(), "Parameter {0} already in this configuration".format(param.name)
self.__parameters[param.name] = deepcopy(param)
self.__order.append(param.name)
# add to the dict obj
self.__dict__[param.name] = self.__parameters[param.name]
return self
def __isub__(self, param):
if isinstance(param, Parameter):
assert param.name in self.__parameters.keys(), "Parameter {0} not in this configuration".format(param.name)
del self.__parameters[param.name]
del self.__order[self.__order.index(param.name)]
# remove from dict obj
del self.__dict__[param.name]
return self
def __str__(self):
if len(self.__parameters) == 0:
return "";
keys = self.__order
max_name_lenght = max( list(map(lambda x: len(x), keys) )) + 1
func = lambda x : x if x < 80 else 80
max_val_lenght = func( max( list( map(lambda x: len(str(x[1].currValue())), self.__parameters.items()) )) + 1 )
ret = '{0:=^{1}}\n'.format('conf.', max_name_lenght + max_val_lenght + 1)
for key in keys:
ret += "{0:<{1}}: {2:>{3}}\n".format(key, max_name_lenght, self.__parameters[key], max_val_lenght)
ret += '{0:-^{1}}'.format('-',max_name_lenght + max_val_lenght + 1)
return ret
def __len__(self):
return len(self.__parameters)
def __nonzero__(self):
for i in self.__constrain:
if not i.check(self):
return False
return True
def parameter_keys(self):
return self.__order;
@property
def parameters(self):
return self.__parameters
def getSpeedup(self):
self.__sync.acquire()
if self.__speedup is None:
self.__sync.wait()
self.__sync.release()
return self.__speedup
def setSpeedup(self, val):
self.__sync.acquire()
self.__speedup = val
self.__sync.notify()
self.__sync.release()
def isEvaluated(self):
return self.__speedup is not None
def __deepcopy__(self, memo):
ret = Configuration()
for p in self.__parameters.values():
ret += deepcopy(p)
return ret
def setDefault(self):
for p in self.__parameters.values():
p.resetToDefault()
def check(self, constrain):
for inv in constrain:
if not inv.check(self):
return False
return True
def WriteBack(self, def_vals, data, final_out_file, elaborate):
if elaborate is None:
return
self.__sync.acquire()
data = [x for x in data if x is not None]
data = [x for x in data if len(x)]
# we remove the empty elements
file_name = sobstitute( elaborate.out_file_name.currValue(), self )
in_file_format = elaborate.in_file_format
res = {}
res['MIN'] = [0] * len(in_file_format)
res['MAX'] = [0] * len(in_file_format)
res['AVG'] = [0] * len(in_file_format)
res['STD'] = [0] * len(in_file_format)
if len(data):
data_ex = zip(*data);
for i in range(len(in_file_format)):
res['AVG'][i] = avg(data_ex[i])
res['MIN'][i] = min(data_ex[i])
res['MAX'][i] = max(data_ex[i])
for i in range(len(in_file_format)):
res['STD'][i] = std(data_ex[i], res['AVG'][i])
# print "MINS: " + str(res['MIN'])
# print "MAXS: " + str(res['MAX'])
# print "AVG : " + str(res['AVG'])
# print "STD : " + str(res['STD'])
vals = []
for param in elaborate.out_file_format:
if param in self.__parameters.keys():
vals.append( toStr(self.__parameters[param].currValue()) )
elif ( param.startswith('AVG(') or param.startswith('STD(') or
param.startswith('MIN(') or param.startswith('MAX(') ) and param.endswith(')'):
param_name = param[4:-1]
assert param_name in in_file_format.values
vals.append( toStr(res[param[:3]][in_file_format.values.index(param_name)]) )
self.__speedup = -1
idx = elaborate.speedup_idx.currValue()
if def_vals:
if def_vals['AVG'][idx] != 0 and res['AVG'][idx] != 0:
assert(idx > 0 or idx < 0)
if idx<0:
idx = (-idx)-1
self.__speedup = res['AVG'][idx]/def_vals['AVG'][idx]
else:
idx -= 1
self.__speedup = def_vals['AVG'][idx]/res['AVG'][idx]
vals.append( toStr(self.__speedup) )
open(file_name,'a').write(",".join( vals ) + "\n")
print "\n{0}".format(80 * "*")
if res['AVG'][idx] == 0:
print "* Configuration failed to execute! "
else:
print "* Average values: \n*\t%s" % ", ".join(map(lambda x: "{0:.3f}".format(x), res['AVG']))
if self.__speedup != -1:
print "* Speedup: \n*\t{0:.5f}".format(self.__speedup)
print "{0}".format(80 * "*")
self.__sync.notify()
self.__sync.release()
return res