forked from davemenendez/alive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_prefixes.py
executable file
·377 lines (285 loc) · 10 KB
/
sample_prefixes.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
371
372
373
374
375
376
377
#!/usr/bin/env python
import loops
import argparse, itertools, sys, os, json
import logging, logging.config
import logutils.queue, Queue
import threading, multiprocessing
import random
logger = logging.getLogger(__name__)
def prefixes(length, max):
'Generate tuples of given length corresponding to unique prefixes of cycles'
if length < 1:
return
def _prefixes_after(length, max, min, prev):
if length < 1:
yield prev
return
for i in range(min,max):
if i in prev:
continue
prev2 = prev + (i,)
for p in _prefixes_after(length-1, max, min, prev2):
yield p
for i in range(0, max - length + 1):
for p in _prefixes_after(length-1, max, i+1, (i,)):
yield p
def perms(n,r):
assert n >= r
return reduce(lambda x,y: x*y, xrange(n,n-r,-1), 1)
def random_prefixes(length, max):
'''Generate unique sequence prefixes in random order.
Highly inefficient unless the space is very large compared to the
number of requsted prefixes.
'''
if length < 1: return
assert length <= max
done = set()
count = sum(perms(max-i-1,length-1) for i in xrange(max-length+1))
while count > 0:
head = random.randrange(max-length)
prefix = (head,) + tuple(random.sample(xrange(head+1,max), length-1))
if prefix not in done:
done.add(prefix)
count-=1
yield prefix
def prefix_generator(length, max, procs, prefix_queue, finished, seed=None):
'Thread worker which generates prefixes and sends them to a queue'
log = logger.getChild('prefix_generator')
log.debug('Prefix generator started')
random.seed(seed)
for p in random_prefixes(length, max):
log.debug('Generated %s', p)
prefix_queue.put(p)
log.debug('Prefixes generated')
prefix_queue.join() # wait until all the prefixes have been handled
log.debug('Prefixes handled')
finished.set() # tell main thread to stop spawning workers
# terminate workers
for i in range(procs):
prefix_queue.put(None)
log.debug('Generator complete')
def _search_after(opts, n, i, o, oz, counters, on_error=None):
if n < 1:
yield (o,tuple(opts[j] for j in oz))
return
counters2 = counters
for j in range(i,len(opts)):
if j in oz:
continue
os = oz + (j,)
for c in loops.all_bin_compositions(o, opts[j], on_error):
for r in _search_after(opts, n-1, i, c, os, counters2, on_error):
yield r
counters2 = None
if counters:
counters[n-1] += 1
if n > 1:
counters[n-2] = 0
def search_after_prefix(opts, n, prefix_idxs, counters, on_error=None):
prefix = tuple(opts[p] for p in prefix_idxs)
i = prefix_idxs[0]
if n == 0:
for p in loops.all_compositions(prefix):
yield (p, prefix)
return
for p in loops.all_compositions(prefix):
for r in _search_after(opts, n, i, p, prefix_idxs, counters, on_error):
yield r
def search(opts, n, counters, on_error=None):
'Generate candidate n-cycles'
if n == 1:
for i1,o in itertools.izip(itertools.count(), opts):
yield (o,(o,))
return
for i in range(0, len(opts)):
os = (i,)
for r in _search_after(opts, n-1, i+1, opts[i], os, counters, on_error):
yield r
status = '\rTested: {} SatChecks: {} Loops: {}'
def count_src(o):
return sum(1 for v in o.src.itervalues() if isinstance(v, loops.Instr))
INFO_FLDS = 6
SEQS, COMPS, SELFCOMPS, SAT_CHECKS, CYCLES, ERRORS = range(INFO_FLDS)
MAX_TESTS = 50000
def prefix_size(prefix, length, max):
min = prefix[0] + len(prefix)
return perms(max - min, length)
def partial_prefix_size(prefix, counters, max):
size = max - prefix[0] - len(prefix)
total = 0
for count in reversed(counters):
total = total * size + count
size -= 1
return total
def search_process(suite, length, limit, prefix_queue, result_queue, status_queue, log_config):
logging.config.dictConfig(log_config)
log = logger.getChild('search_process')
log.info('Worker thread started')
opts = loops.parse_transforms(open(suite).read())
log.debug('%s optimizations', len(opts))
info = [0] * INFO_FLDS
def count_error(e,o1,o2):
info[ERRORS] += 1
complete = True
while info[COMPS] < limit:
p = prefix_queue.get()
if p is None:
log.info('Worker exiting %s', info)
status_queue.put(info)
prefix_queue.task_done() # make sure this happens after putting the info
return
log.info('Checking prefix %s; %s remaining', p, limit-info[COMPS])
counters = [0] * length
for o,os in search_after_prefix(opts, length, p, counters, count_error):
o_src = count_src(o)
info[COMPS] += 1
if info[COMPS] % 1000 == 0:
log.info('Paths %s Comps %s Selfcomps %s SatChecks %s Loops %s Errors %s', *info)
for oo in loops.all_bin_compositions(o, o, count_error):
# TODO: can this just be compose?
info[SELFCOMPS] += 1
oo_src = count_src(oo)
if o_src < oo_src:
continue
info[SAT_CHECKS] += 1
if not loops.satisfiable(oo):
continue
info[CYCLES] += 1
# TODO: put found loops into a queue
result = 'Loop: {}\n{}\n\n{}'.format(o.name, '\n\n'.join(str(op) for op in os), o)
result_queue.put(result)
log.info(result)
if info[COMPS] >= limit:
complete = False
break
if complete:
psize = prefix_size(p, length, len(opts))
log.debug('Prefix %s size %s counters %s', p, psize, counters)
info[SEQS] += psize
else:
psize = partial_prefix_size(p, counters, len(opts))
info[SEQS] += psize
log.debug('Prefix %s size %s Info %s Counters %s', p, psize, info, counters)
prefix_queue.task_done()
log.info('Worker exiting %s', info)
status_queue.put(info)
def queue_printer(result_queue):
'Read strings from the queue. Terminate when we get None'
while True:
result = result_queue.get()
if result is None:
return
print '\n-----'
print result
# TODO: track child processes so we can handle exceptions gracefully
def search_manager(suite, comp_limit, prefix_length, length, max, procs, seed, log_config):
log = logger.getChild('search_manager')
log.info('Starting manager')
prefix_queue = multiprocessing.JoinableQueue(procs)
status_queue = multiprocessing.Queue()
result_queue = multiprocessing.Queue()
finished = threading.Event()
prefix_thread = threading.Thread(
target=prefix_generator,
args=(prefix_length, max - length, procs, prefix_queue, finished, seed))
prefix_thread.daemon = True # no cleanup needed if we get a KeyboardInterrupt
result_thread = threading.Thread(
target=queue_printer,
args=(result_queue,))
limit = MAX_TESTS if comp_limit > MAX_TESTS * procs else comp_limit // procs
for i in range(procs):
p = multiprocessing.Process(
target=search_process,
args=(suite, length, limit, prefix_queue, result_queue, status_queue, log_config))
p.start()
comp_limit -= limit
active = procs
total_info = [0] * INFO_FLDS
try:
prefix_thread.start()
result_thread.start()
# read from the result queue until the prefix generator completes
while active > 0:
r = status_queue.get(block=True)
log.debug('Got result %s; %s comps remain', r, comp_limit)
for i in range(INFO_FLDS):
total_info[i] += r[i]
log.info('Current totals %s', total_info)
if finished.is_set() or comp_list <= 0:
active -= 1
log.debug('%s workers active', active)
else:
limit = min(MAX_TESTS, comp_limit)
comp_limit -= limit
p = multiprocessing.Process(
target=search_process,
args=(suite, length, limit, prefix_queue, result_queue, status_queue, log_config))
p.start()
log.info('Final: Paths %s Comps %s SelfComps %s Sat_Checks %s Cycles %s Errors %s', *total_info)
finally:
log.debug('Closing result queue reader')
result_queue.put(None)
def count_opts(suite):
opts = loops.parse_transforms(open(suite).read())
return len(opts)
def main():
log_queue = multiprocessing.Queue()
log_config = {
'version': 1,
'disable_existing_handlers': True,
'handlers': {
'queue': {
'class': 'logutils.queue.QueueHandler',
'queue': log_queue
}
},
'loggers': {
__name__: { 'level': 'DEBUG' }
},
'root': {
'level': 'WARNING',
'handlers': ['queue']
}
}
h = logging.FileHandler(filename='sample_prefixes.log', mode='w')
#h = logging.StreamHandler()
f = logging.Formatter('%(asctime)s - %(levelname)-8s - %(name)s - %(message)s')
h.setFormatter(f)
ql = logutils.queue.QueueListener(log_queue, h)
ql.start()
logging.config.dictConfig(log_config)
parser = argparse.ArgumentParser()
parser.add_argument('prefix_length', type=int,
help='Length of prefix to spawn into worker processes')
parser.add_argument('suffix_length', type=int,
help='Length of cycles following the prefix')
parser.add_argument('file', type=str,
help='optimization suite to analyze')
parser.add_argument('-t', '--tests', type=int, default=1000000,
help='Number of sequences to test')
parser.add_argument('-p', '--procs', type=int,
help='Number of sub-processes', default=1)
parser.add_argument('--seed', type=str,
help='Seed for random number generator')
args = parser.parse_args()
# FIXME: Need to make sure zero-length prefixes work
if args.prefix_length < 1 or args.suffix_length < 0:
sys.stderr.write('cycle length must be positive\n')
exit(1)
if args.procs < 1:
sys.stderr.write('Must use at least one process')
exit(1)
if not os.path.exists(args.file):
sys.stderr.write(args.file + ': not found\n')
exit(1)
logger.info('Starting')
max = count_opts(args.file)
logger.info('Counted %s opts', max)
try:
search_manager(args.file, args.tests, args.prefix_length, args.suffix_length, max,
args.procs, args.seed, log_config)
finally:
logger.debug('Closing queue listener')
ql.stop()
if __name__ == '__main__':
main()