-
Notifications
You must be signed in to change notification settings - Fork 3
/
tuner.py
executable file
·119 lines (109 loc) · 3.66 KB
/
tuner.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
#!/usr/bin/python3
from __future__ import print_function
#import adddeps # fix sys.path
import os
import math
import opentuner
from opentuner import ConfigurationManipulator
from opentuner import IntegerParameter
from opentuner.search.manipulator import LogIntegerParameter
from opentuner.search.manipulator import PowerOfTwoParameter
from opentuner.search.manipulator import SwitchParameter
from opentuner.search.manipulator import BooleanParameter
from opentuner import MeasurementInterface
from opentuner import Result
class Tuner(MeasurementInterface):
run_cmd = ""
filename = ""
params = [];
def manipulator(self):
"""
Define the search space by creating a
ConfigurationManipulator
"""
manipulator = ConfigurationManipulator()
for i, param in enumerate(self.params):
if param[0] == "None":
continue
if param[0] == "Int":
manipulator.add_parameter(IntegerParameter(i, int(param[1]), int(param[2])))
continue
if param[0] == "Log":
manipulator.add_parameter(LogIntegerParameter(i, int(param[1]), int(param[2])))
continue
if param[0] == "Pow":
manipulator.add_parameter(PowerOfTwoParameter(i, 2 ** int(param[1]), 2 ** int(param[2])))
continue
if param[0] == "Switch":
manipulator.add_parameter(SwitchParameter(i, int(param[1])))
continue
if param[0] == "Bool":
manipulator.add_parameter(BooleanParameter(i))
continue
return manipulator
def run(self, desired_result, input, limit):
"""
Compile and run a given configuration then
return performance
"""
cfg = desired_result.configuration.data
f = open(self.filename, mode="w")
for i, param in enumerate(self.params):
if param[0] == "None":
f.write(param[1] + "\n")
continue
f.write(str(cfg[i]) + "\n")
f.close()
print(cfg)
run_result = self.call_program(self.run_cmd, limit=self.args.test_timeout)
print(run_result)
if run_result['returncode'] != 0:
return Result(time=float('inf'))
return Result(time=run_result['time'])
def save_final_config(self, configuration):
cfg = configuration.data
f = open(self.filename, mode="w")
print("### Results ###")
for i, param in enumerate(self.params):
if param[0] == "None":
f.write(param[1] + "\n")
continue
f.write(str(cfg[i]) + "\n")
if param[0] == "Int" or param[0] == "Log":
print(str(param[1]) + " < " + str(cfg[i]) + " < " + str(param[2]))
continue
if param[0] == "Pow":
print(str(2 ** int(param[1])) + " < " + str(cfg[i]) + " < " + str(2 ** int(param[2])))
continue
if param[0] == "Bool" or param[0] == "Switch":
print(cfg[i])
continue
f.close()
if __name__ == '__main__':
argparser = opentuner.default_argparser()
argparser.add_argument('command', help='command to run the program')
argparser.add_argument('header', help='file including struct of parameters')
argparser.add_argument('--test-timeout', type=int, default=60, help='terminate test after given seconds')
args = argparser.parse_args()
Tuner.run_cmd = args.command
Tuner.filename = "_" + os.path.basename(args.header) + "_setting.txt"
f = open(args.header)
lines = f.readlines();
fparam = 0
for line in lines:
words = line.split()
if not words:
continue
if words[0] == "//" and words[1] == "Param":
fparam = 1
continue
if words[0] == "//" and words[1] == "end":
break
if not fparam:
continue
param = words.index("//")
assert param != -1
print(words[param+1:])
Tuner.params += [words[param+1:]]
f.close()
Tuner.main(args)