-
Notifications
You must be signed in to change notification settings - Fork 0
/
rheosched
executable file
·285 lines (240 loc) · 8.23 KB
/
rheosched
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
#!/usr/bin/env python
'''
usage:
rheosched -f <schedule_json> [-n n_interpolation_points] [-t interpolation_type] -w <hw_ver> -d <fill_depth> [<other run args>]
'''
import sys
import json
import enum
import os
import time
from copy import deepcopy
class InterpolationType(enum.Enum):
Linear = enum.auto()
Log = enum.auto()
class SchedulePoint:
def __init__(self, o):
self.name = None
self.params = []
for k,v in o.items():
setattr(self, k, v)
self.params = self.check_params(self.params)
if not isinstance(self.params, list):
self.params = [self.params]
def check_params(self, param):
if isinstance(param, str):
return float(param[:-1])*0.01*1024
elif isinstance(param, list):
return [self.check_params(p) for p in param]
else:
return param
def as_args(self):
raise Exception()
class ScheduleSetterPoint(SchedulePoint):
def as_args(self):
rv = ['-s', self.name]
if not self.params:
params = False
if isinstance(self.params, (list, tuple)):
params = ','.join([str(p) for p in self.params])
else:
params = str(self.params)
if params:
rv.extend(['-ps', params])
return rv
class ScheduleControllerPoint(SchedulePoint):
def as_args(self):
rv = ['-c', self.name]
if not self.params:
params = False
if isinstance(self.params, list):
params = ','.join([str(p) for p in self.params])
else:
params = str(self.params)
if params:
rv.extend(['-pc', params])
return rv
class Schedule:
def __init__(self, sched_path):
self.n_interpolation_points = 3
self.interpolation_type = InterpolationType.Linear
self.args = list()
with open(sched_path) as f:
data = json.load(f)
self.points = list()
for pt in data:
t = str(pt['type'])
del pt['type']
if t == 'controller':
self.points.append(ScheduleControllerPoint(pt))
elif t == 'setter':
self.points.append(ScheduleSetterPoint(pt))
elif t == 'params':
self.update_params(pt)
def get_sched_points(self):
controller = None
setter = None
scheduled_points = list()
for point in self.points:
if isinstance(point, ScheduleSetterPoint):
if setter is not None and controller is not None:
setters = self.interp_between(setter, point)
for s in setters:
scheduled_points.append( (s, controller ) )
setter = setters[-1]
else:
setter = point
if controller is not None:
scheduled_points.append( (setter, controller) )
elif isinstance(point, ScheduleControllerPoint):
controller = point
return scheduled_points
def get_argset(self, extra_args=None):
head = ['sudo','./rheometer', 'run', '--quiet']
tail = self.args
argset = list()
for point in self.get_sched_points():
arglist = list()
arglist.extend(head)
for p in point:
arglist.extend(p.as_args())
arglist.extend(tail)
if extra_args:
arglist.extend(extra_args)
arglist = [f'"{arg}"' if '(' in arg else arg for arg in arglist]
argset.append(arglist)
return argset
def interp_between(self, c1, c2):
assert c1.name == c2.name
assert len(c1.params) == len(c2.params)
paramslist_transposed = list()
for (p1, p2) in zip(c1.params, c2.params):
rng = (p2 - p1)/self.n_interpolation_points
l = list()
for i in range(self.n_interpolation_points):
p = p1 + rng*(i+1)
l.append(p)
paramslist_transposed.append(l)
paramslist = list(zip(*paramslist_transposed))
rv = list()
for params in paramslist:
o = deepcopy(c1)
o.params = params
rv.append(o)
return rv
def update_params(self, o):
for k,v in o.items():
if k == 'interpolation_type':
self.interpolation_type = InterpolationType.Linear if v == 'linear' else InterpolationType.Log
elif k == 'n_interpolation_points':
self.n_interpolation_points = v
else:
self.args.append(f'--{k}')
self.args.append(f'{v}')
def parse_time(tstr):
mult = 1.0
if 's' in tstr:
tstr = tstr[:-1]
elif 'm' in tstr:
mult = 60.0
tstr = tstr[:-1]
v = float(tstr)
return v*mult # time in seconds
def get_tstr(arglist):
tstr = None
for k, v in zip(arglist, arglist[1:]):
if k in ['--length', '-l']:
tstr = v
if tstr is None:
raise Exception('Could not find length spec "-l" or "--length" in args! This is a required param.')
return tstr
def fmt_test(test):
if test > 60.0:
return f'{test/60:.1f} minutes'
else:
return f'{int(test)} seconds'
def get_discrepancy():
try:
with open('.disc.txt') as f:
return float(f.readline())
except:
return 0.0
def set_discrepency(v):
with open('.disc.txt', 'w') as f:
f.write(f'{v}\n')
def usage():
print('')
print(' rheosched')
print('')
print(' rheomter run scheduler. Runs rheometer over a range of control')
print(' schemes and set points as laid out in a schedule file.')
print('')
print('')
print(' Usage:')
print('')
print(' ./rheosched <schedule.json> [schedule options] -w <hw_var> -l <length> -d <depth> [-t tag]')
print(' ./rheosched --help')
print('')
print(' Required arguments (-w, -l, and -d) may be given in the schedule')
print(' JSON, but any values given on the command line will override them.')
print('')
print(' Schedule settings may be set in the JSON file, but again will')
print(' be overridden by command line values.')
print('')
print('')
print(' Schedule Options:')
print('')
print(' --interp-number <number>')
print(' Sets the number of interpolation points to use when generating')
print(' the schedule. If not given in the JSON, the default is 3.')
print('')
print(' --interp-linear')
print(' Set interpolation method to linear. This is the default.')
print('')
print(' --interp-log')
print(' set interpolation method to log.')
print('')
if __name__ == "__main__":
dummy = False
args = sys.argv[1:]
if len(args) < 1:
print('Syntax error: must specify at least a schedule file.')
usage()
exit(1)
schedule = Schedule(args[0])
args = args[1:]
extra_args = list()
for i, (key, val) in enumerate(zip(args, args[1:])):
if key == '--interp-number':
schedule.n_interpolation_points = int(val)
elif key == '--interp-linear':
schedule.interpolation_type = InterpolationType.Linear if val == 'linear' else InterpolationType.Log
elif key == '--help':
usage()
exit()
elif key == '--dummy':
dummy = not dummy
else:
extra_args = args[i:]
break
argset = schedule.get_argset(extra_args)
l = len(argset)
discrepency = get_discrepancy()
rv = 0
for i, al in enumerate(argset):
# time estimate in seconds
test = parse_time(get_tstr(al)) + discrepency
title = f'({i+1}/{l}) This run will take {fmt_test(test)}. There are {fmt_test((l-i)*test)} remaining in total.'
print(title)
command = ' '.join(al)
print(command)
before = time.time()
if not dummy:
rv = os.system(command)
dt = time.time() - before
discrepency = discrepency + dt - test
if rv:
print("Schedule interrupted!")
sys.exit(rv)
if not dummy:
set_discrepency(discrepency)