-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparallel-driver.rkt
386 lines (346 loc) · 19.4 KB
/
parallel-driver.rkt
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
378
379
380
381
382
383
384
385
386
#lang racket
(require "path.rkt" "stat.rkt")
(provide parallel-driver%)
(define (get-free-mem)
(string->number
(list-ref
(string-split
(with-output-to-string (thunk (system "free | head -2 | tail -1"))))
3)))
(define parallel-driver%
(class object%
(super-new)
(init-field isa parser machine printer validator search-type mode
[window #f])
;; search = `solver, `stoch, `hybrid
;; mode = `linear, `binary, `syn, `opt
(public optimize)
(define (required-module x) (format "~a/~a-~a.rkt" isa isa x))
(define (get-class-name x) (format "~a-~a%" isa x))
;; Optimize code
(define (optimize-inner code-org live-out-org rootdir cores time-limit prog-size
assume input-file start-prog)
;;(raise "done")
(pretty-display (format "SEACH TYPE: ~a size=~a" search-type prog-size))
;;(define path (format "~a/driver" dir))
(system (format "rm -r ~a" rootdir))
(system (format "mkdir ~a" rootdir))
(pretty-display ">>> select code:")
(send printer print-syntax code-org)
(pretty-display (format ">>> live-out-org: ~a" live-out-org))
;; Use the fewest number of registers possible.
(define-values (code live-out map-back machine-config)
(send printer compress-state-space code-org live-out-org))
(pretty-display (format ">>> machine-config: ~a" machine-config))
(pretty-display (format ">>> live-out: ~a" live-out))
(pretty-display `(map-back ,map-back))
(pretty-display ">>> compressed-code:")
(send printer print-syntax code)
;; machine-config from compress-state-space is only accurate for reg but not memory. This will adjust the rest of the machine info.
;; (set! machine-config (send validator proper-machine-config
;; (send printer encode code) machine-config))
(pretty-display (format ">>> machine-config: ~a" machine-config))
(pretty-display (format ">>> live-out: ~a" live-out))
(define dir-id 0)
(define (optimize-partial prefix from to)
(pretty-display (format "OPTIMIZE-PARTIAL from = ~a, to = ~a" from to))
(define dir (format "~a/~a" rootdir dir-id))
(system (format "mkdir ~a" dir))
(define path (format "~a/driver" dir))
(set! dir-id (add1 dir-id))
(define (create-file id search-type mode)
(define (req file)
(format "(file \"~a/~a\")" srcpath (required-module file)))
(define required-files
(string-join
(map req (append '(parser machine printer
simulator-racket simulator-rosette
validator)
(match search-type
[`stoch '(stochastic)]
[`solver '(symbolic)]
[`enum '(forwardbackward enumerator inverse)])))))
(with-output-to-file
#:exists 'truncate (format "~a-~a.rkt" path id)
(thunk
(pretty-display (format "#lang racket"))
(pretty-display (format "(require ~a)" required-files))
(pretty-display (format "(define machine (new ~a [config ~a]))"
(get-class-name "machine")
(send printer set-config-string machine-config)))
(pretty-display (format "(define printer (new ~a [machine machine]))" (get-class-name "printer")))
(pretty-display (format "(define parser (new ~a))" (get-class-name "parser")))
(pretty-display (format "(define simulator-racket (new ~a [machine machine]))" (get-class-name "simulator-racket")))
(pretty-display (format "(define simulator-rosette (new ~a [machine machine]))" (get-class-name "simulator-rosette")))
(pretty-display (format "(define validator (new ~a [machine machine] [simulator simulator-rosette]))" (get-class-name "validator")))
(cond
[(equal? search-type `stoch)
(pretty-display
(format "(define search (new ~a [machine machine] [printer printer] [parser parser] [validator validator] [simulator simulator-racket] [syn-mode ~a]))"
(get-class-name "stochastic")
(equal? mode `syn)))]
[(equal? search-type `solver)
(pretty-display
(format "(define search (new ~a [machine machine] [printer printer] [parser parser] [validator validator] [simulator simulator-rosette] [syn-mode `~a]))"
(get-class-name "symbolic")
mode))]
[(equal? search-type `enum)
(pretty-display
(format "(define search (new ~a [machine machine] [printer printer] [parser parser] [validator validator] [simulator simulator-racket] [enumerator% ~a] [inverse% ~a] [syn-mode `~a]))"
(get-class-name "forwardbackward")
(get-class-name "enumerator")
(get-class-name "inverse")
mode))])
(pretty-display "(define prefix (send parser ir-from-string \"")
(send printer print-syntax prefix)
(pretty-display "\"))")
(pretty-display "(define code (send parser ir-from-string \"")
(send printer print-syntax (vector-copy code from to))
(pretty-display "\"))")
(pretty-display "(define postfix (send parser ir-from-string \"")
(send printer print-syntax (vector-copy code to (vector-length code)))
(pretty-display "\"))")
(pretty-display (format "(define encoded-prefix (send printer encode prefix))"))
(pretty-display (format "(define encoded-code (send printer encode code))"))
(pretty-display (format "(define encoded-postfix (send printer encode postfix))"))
(when start-prog
(pretty-display "(define start-code (send parser ir-from-string \"")
(send printer print-syntax start-prog)
(pretty-display "\"))")
(pretty-display (format "(define encoded-start-code (send printer encode start-code))"))
)
(pretty-display
(format "(send search superoptimize encoded-code ~a \"~a-~a\" ~a ~a #:assume ~a #:input-file ~a #:start-prog ~a #:prefix encoded-prefix #:postfix encoded-postfix)"
(send printer output-constraint-string live-out)
path id time-limit prog-size
(send printer output-assume-string assume)
(if input-file (string-append "\"" input-file "\"") #f)
(if start-prog "encoded-start-code" #f)
))
;;(pretty-display "(dump-memory-stats)"
)))
(define (run-file id)
(define out-port
(open-output-file (format "~a-~a.log" path id) #:exists 'truncate))
(define-values (sp o i e)
(subprocess out-port #f out-port (find-executable-path "racket") (format "~a-~a.rkt" path id)))
sp)
(define (kill-all)
(for ([sp (append processes-stoch processes-solver processes-enum)]
[id cores])
(when (equal? (subprocess-status sp) 'running)
(subprocess-kill sp #f))))
(define t (current-seconds))
(define (get-stats)
(define stats
(for/list ([id cores])
(let ([name (format "~a-~a.stat" path id)])
(and (file-exists? name)
(create-stat-from-file name printer)))))
(with-handlers*
([exn? (lambda (e) (pretty-display "Error: print stat"))])
(when (> cores-stoch 0)
(print-stat-all (filter identity (take stats cores-stoch)) printer))
)
(define-values (cost len time id) (get-best-info dir))
(pretty-display (format "current-time:\t~a" (- (current-seconds) t)))
(when cost
(pretty-display "=============== SUMMARY ===============")
(pretty-display (format "cost:\t~a" cost))
(pretty-display (format "len:\t~a" len))
(pretty-display (format "time:\t~a" time))
(pretty-display id)))
(define cores-stoch
(cond
[(equal? search-type `stoch) cores]
[(equal? search-type `hybrid) (min 3 (floor (* (/ 2 6) cores)))] ;;(min 3 (floor (* (/ 2 6) cores)))]
[else 0]))
(define cores-enum
(cond
[(equal? search-type `enum) cores]
[(equal? search-type `hybrid) (floor (* (/ 3 6) cores))] ;;(floor (* (/ 3 6) cores))]
[else 0]
))
(define cores-solver
(cond
[(equal? search-type `solver) cores]
[(equal? search-type `hybrid) (- cores cores-stoch cores-enum)]
[else 0]
))
(newline)
(pretty-display "SEARCH INSTANCES")
(pretty-display "----------------")
(pretty-display (format "stoch:\t~a instances" cores-stoch))
(pretty-display (format "sym:\t~a instances" cores-solver))
(pretty-display (format "enum:\t~a instances" cores-enum))
(newline)
;; STEP 1: create file & run
(define-syntax-rule (create-and-run id mode search-type)
(begin
(create-file id search-type mode)
(run-file id)
))
(define processes-stoch
(if (equal? search-type `hybrid)
(let ([n (min 3 cores-stoch)])
(pretty-display (format "ID ~a-~a: stoch (optimize)" 0 (sub1 n)))
(when (> cores-stoch n)
(pretty-display (format "ID ~a-~a: stoch (synthesize)" n (sub1 cores-stoch))))
(append (for/list ([id n])
(create-and-run id `opt `stoch))
(for/list ([id (- cores-stoch n)])
(create-and-run (+ n id) `syn `stoch))))
;; (begin
;; (when (> cores-stoch 0)
;; (pretty-display (format "ID ~a-~a: stoch (optimize)" 0 (sub1 cores-stoch))))
;; (for/list ([id cores-stoch]) (create-and-run id `opt `stoch)))
(for/list ([id cores-stoch]) (create-and-run id mode `stoch))))
(define processes-solver
(cond
[(or (equal? search-type `hybrid)
(and (equal? search-type `solver) (equal? mode `partial)))
(define n1 (if (equal? search-type `solver) 1 0))
(define n2 (floor (* (/ 8 16) cores-solver)))
(define n3 1);(floor (* (/ 4 16) cores-solver)))
(define n4 (if (< (+ n1 n2 n3) cores-solver) (ceiling (* (/ 2 16) cores-solver)) 0))
(define n5 (if (< (+ n1 n2 n3 n4) cores-solver) (ceiling (* (/ 1 16) cores-solver)) 0))
(set! n3 (- cores-solver n1 n2 n4 n5))
;;(pretty-display `(sym ,n1 ,n2 ,n3 ,n4 ,n5))
(when (> n1 0) (pretty-display (format "ID ~a-~a: sym (no-decomposition)" cores-stoch (sub1 (+ cores-stoch n1)))))
(when (> n2 0) (pretty-display (format "ID ~a-~a: sym (window=L)" (+ cores-stoch n1) (sub1 (+ cores-stoch n1 n2)))))
(when (> n3 0) (pretty-display (format "ID ~a-~a: sym (window=2L)" (+ cores-stoch n1 n2) (sub1 (+ cores-stoch n1 n2 n3)))))
(when (> n4 0) (pretty-display (format "ID ~a-~a: sym (window=3L)" (+ cores-stoch n1 n2 n3) (sub1 (+ cores-stoch n1 n2 n3 n4)))))
(when (> n5 0) (pretty-display (format "ID ~a-~a: sym (window=4L)" (+ cores-stoch n1 n2 n3 n4) (sub1 (+ cores-stoch n1 n2 n3 n4 n5)))))
(append (for/list ([i n1]) (create-and-run (+ cores-stoch i) `linear `solver))
(for/list ([i n2]) (create-and-run (+ cores-stoch n1 i) `partial1 `solver))
(for/list ([i n3]) (create-and-run (+ cores-stoch n1 n2 i) `partial2 `solver))
(for/list ([i n4]) (create-and-run (+ cores-stoch n1 n2 n3 i) `partial3 `solver))
(for/list ([i n5]) (create-and-run (+ cores-stoch n1 n2 n3 n4 i) `partial4 `solver))
)
]
[else
(for/list ([id cores-solver]) (create-and-run id mode `solver))]))
(define processes-enum
(cond
[(or (equal? search-type `hybrid)
(and (equal? search-type `enum) (equal? mode `partial)))
(define n1 (min 1 cores-enum))
(define n2 (floor (* (/ 8 16) cores-enum)))
(define n3 (min 1 cores-enum))
(define n4 (if (< (+ n1 n2 n3) cores-enum) (ceiling (* (/ 2 16) cores-enum)) 0))
(define n5 (if (< (+ n1 n2 n3 n4) cores-enum) (ceiling (* (/ 1 16) cores-enum)) 0))
(set! n3 (- cores-enum n1 n2 n4 n5))
;;(pretty-display `(enum ,n1 ,n2 ,n3 ,n4 ,n5))
(when (> n1 0) (pretty-display (format "ID ~a-~a: enum (no-decomposition)" (+ cores-stoch cores-solver) (sub1 (+ cores-stoch cores-solver n1)))))
(when (> n2 0) (pretty-display (format "ID ~a-~a: enum (window=L)" (+ cores-stoch cores-solver n1) (sub1 (+ cores-stoch cores-solver n1 n2)))))
(when (> n3 0) (pretty-display (format "ID ~a-~a: enum (window=2L)" (+ cores-stoch cores-solver n1 n2) (sub1 (+ cores-stoch cores-solver n1 n2 n3)))))
(when (> n4 0) (pretty-display (format "ID ~a-~a: enum (window=3L)" (+ cores-stoch cores-solver n1 n2 n3) (sub1 (+ cores-stoch cores-solver n1 n2 n3 n4)))))
(when (> n5 0) (pretty-display (format "ID ~a-~a: enum (window=4L)" (+ cores-stoch cores-solver n1 n2 n3 n4) (sub1 (+ cores-stoch cores-solver n1 n2 n3 n4 n5)))))
(append (for/list ([i n1]) (create-and-run (+ cores-stoch cores-solver i) `linear `enum))
(for/list ([i n2]) (create-and-run (+ cores-stoch cores-solver n1 i) `partial1 `enum))
(for/list ([i n3]) (create-and-run (+ cores-stoch cores-solver n1 n2 i) `partial2 `enum))
(for/list ([i n4]) (create-and-run (+ cores-stoch cores-solver n1 n2 n3 i) `partial3 `enum))
(for/list ([i n5]) (create-and-run (+ cores-stoch cores-solver n1 n2 n3 n4 i) `partial4 `enum))
)
]
[else
(for/list ([id cores-enum]) (create-and-run id mode `enum))]))
(newline)
(define (result)
(define limit (if (string? time-limit)
(string->number time-limit)
time-limit))
(define (update-stats)
(sleep 10)
(when (and (< (- (current-seconds) t) limit));(> (get-free-mem) 1000000))
(for ([id (length processes-stoch)]
[sp processes-stoch])
(unless (equal? (subprocess-status sp) 'running)
(pretty-display (format "driver-~a is dead." id))))
(for ([id (length processes-solver)]
[sp processes-solver])
(unless (equal? (subprocess-status sp) 'running)
(pretty-display (format "driver-~a is dead." (+ cores-stoch id)))))
(for ([id (length processes-enum)]
[sp processes-enum])
(unless (equal? (subprocess-status sp) 'running)
(pretty-display (format "driver-~a is dead." (+ cores-stoch cores-solver id)))))
(get-stats)
(update-stats)))
(with-handlers*
([exn:break? (lambda (e) (kill-all) (sleep 5))])
(update-stats)
(kill-all)))
;; STEP 2: wait until timeout or optimal program is found.
(result)
;; STEP 3: get best output & print
(get-stats)
(if (file-exists? (format "~a/best.s" dir))
(send parser ir-from-file (format "~a/best.s" dir))
(vector-copy code from to)))
(define code-len (vector-length code))
(define window-size (if window window (send machine window-size)))
(define rounds (ceiling (/ code-len window-size)))
(define size (ceiling (/ code-len rounds)))
(define output-code (vector))
(define mid-positions (make-vector rounds))
(for ([round rounds])
(let ([new-code (optimize-partial output-code (* round size)
(min (* (add1 round) size) code-len))])
(vector-set! mid-positions round
(+ (vector-length output-code)
(quotient (vector-length new-code) 2)))
(set! output-code (vector-append output-code new-code))))
(when (> rounds 1)
(pretty-display `(mid-positions ,mid-positions))
(define small-size window-size);(floor (* (/ 6 10) window-size)))
(define gap1 (- (vector-ref mid-positions 1) (vector-ref mid-positions 0)))
(when (< gap1 small-size)
(vector-set!
mid-positions 0
(max 0 (- (vector-ref mid-positions 1) small-size))))
(define gap2 (- (vector-ref mid-positions (- rounds 1))
(vector-ref mid-positions (- rounds 2))))
(when (< gap2 small-size)
(vector-set!
mid-positions (- rounds 1)
(min (vector-length output-code)
(+ (vector-ref mid-positions (- rounds 2)) small-size))))
(pretty-display `(mid-positions ,mid-positions))
(set! code output-code)
(set! output-code (vector-copy code 0 (vector-ref mid-positions 0)))
(newline)
(pretty-display (format ">>> another round"))
(send printer print-syntax code)
(for ([round (sub1 rounds)])
(let ([new-code
(optimize-partial output-code
(vector-ref mid-positions round)
(vector-ref mid-positions (add1 round)))])
(set! output-code (vector-append output-code new-code))))
(set! output-code (vector-append
output-code
(vector-copy code (vector-ref mid-positions (sub1 rounds)))))
)
(let ([username (string-trim (read-line (first (process "whoami"))))])
(system (format "pkill -u ~a java" username))
(system (format "pkill -u ~a z3" username)))
(let ([decompressed-code (send printer decompress-state-space output-code map-back)])
(newline)
(pretty-display "OUTPUT")
(pretty-display "------")
(send printer print-syntax decompressed-code)
decompressed-code)
)
(define (optimize code-org live-out
#:assume [assume #f]
#:dir [dir "output"]
#:cores [cores 8]
#:time-limit [time-limit 3600]
#:size [size #f]
#:input-file [input-file #f]
#:start-prog [start-prog #f])
(if (> (vector-length code-org) 0)
(optimize-inner code-org live-out dir cores time-limit size assume input-file start-prog)
code-org))
))