Skip to content

Commit 4dfcda0

Browse files
committed
Use rival functions for most of baseline, just not precision adjust
1 parent 6dd26c8 commit 4dfcda0

File tree

5 files changed

+27
-255
lines changed

5 files changed

+27
-255
lines changed

eval/adjust.rkt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
racket/match)
88

99
(provide backward-pass
10-
make-hint)
10+
make-hint
11+
drop-self-pointer)
1112

1213
; Hint is a vector with len(ivec) elements which
1314
; guides Rival on which instructions should not be executed

eval/run.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
rival-machine-run
1414
rival-machine-return
1515
rival-machine-adjust
16+
rival-machine-record
1617
apply-instruction) ; for compile.rkt
1718

1819
(define (rival-machine-load machine args)

infra/run-baseline.rkt

Lines changed: 20 additions & 249 deletions
Original file line numberDiff line numberDiff line change
@@ -6,128 +6,20 @@
66

77
(require (only-in "../eval/compile.rkt" exprs->batch fn->ival-fn make-initial-repeats)
88
(only-in "../eval/machine.rkt" *rival-max-precision* *rival-profile-executions*)
9+
(only-in "../eval/run.rkt"
10+
rival-machine-load
11+
rival-machine-run
12+
rival-machine-return
13+
rival-machine-record)
14+
(only-in "../eval/adjust.rkt" drop-self-pointer make-hint)
915
"../eval/machine.rkt"
1016
"../eval/main.rkt"
1117
(only-in "../ops/core.rkt" new-ival)
1218
"../ops/all.rkt")
1319

1420
(provide baseline-compile
1521
baseline-analyze
16-
baseline-apply
17-
baseline-profile)
18-
19-
(define (make-hint machine old-hint)
20-
(define args (rival-machine-arguments machine))
21-
(define ivec (rival-machine-instructions machine))
22-
(define rootvec (rival-machine-outputs machine))
23-
(define vregs (rival-machine-registers machine))
24-
25-
(define varc (vector-length args))
26-
(define vhint (make-vector (vector-length ivec) #f))
27-
(define converged? #t)
28-
29-
; helper function
30-
(define (vhint-set! idx val)
31-
(when (>= idx varc)
32-
(vector-set! vhint (- idx varc) val)))
33-
34-
; roots always should be executed
35-
(for ([root-reg (in-vector rootvec)])
36-
(vhint-set! root-reg #t))
37-
(for ([instr (in-vector ivec (- (vector-length ivec) 1) -1 -1)]
38-
[hint (in-vector vhint (- (vector-length vhint) 1) -1 -1)]
39-
[o-hint (in-vector old-hint (- (vector-length old-hint) 1) -1 -1)]
40-
[n (in-range (- (vector-length vhint) 1) -1 -1)]
41-
#:when hint)
42-
(define hint*
43-
(match o-hint
44-
[(? ival? _) o-hint] ; instr is already "hinted" by old hint, no children are to be recomputed
45-
[(? integer? ref) ; instr is already "hinted" by old hint,
46-
(define idx (list-ref instr ref)) ; however, one child needs to be recomputed
47-
(when (>= idx varc)
48-
(vhint-set! idx #t))
49-
o-hint]
50-
[#t
51-
(case (object-name (car instr))
52-
[(ival-assert)
53-
(match-define (list _ bool-idx) instr)
54-
(define bool-reg (vector-ref vregs bool-idx))
55-
(match* ((ival-lo bool-reg) (ival-hi bool-reg) (ival-err? bool-reg))
56-
; assert and its children should not be reexecuted if it is true already
57-
[(#t #t #f) (ival-bool #t)]
58-
; assert and its children should not be reexecuted if it is false already
59-
[(#f #f #f) (ival-bool #f)]
60-
[(_ _ _) ; assert and its children should be reexecuted
61-
(vhint-set! bool-idx #t)
62-
(set! converged? #f)
63-
#t])]
64-
[(ival-if)
65-
(match-define (list _ cond tru fls) instr)
66-
(define cond-reg (vector-ref vregs cond))
67-
(match* ((ival-lo cond-reg) (ival-hi cond-reg) (ival-err? cond-reg))
68-
[(#t #t #f) ; only true path should be executed
69-
(vhint-set! tru #t)
70-
2]
71-
[(#f #f #f) ; only false path should be executed
72-
(vhint-set! fls #t)
73-
3]
74-
[(_ _ _) ; execute both paths and cond as well
75-
(vhint-set! cond #t)
76-
(vhint-set! tru #t)
77-
(vhint-set! fls #t)
78-
(set! converged? #f)
79-
#t])]
80-
[(ival-fmax)
81-
(match-define (list _ arg1 arg2) instr)
82-
(define cmp (ival-> (vector-ref vregs arg1) (vector-ref vregs arg2)))
83-
(match* ((ival-lo cmp) (ival-hi cmp) (ival-err? cmp))
84-
[(#t #t #f) ; only arg1 should be executed
85-
(vhint-set! arg1 #t)
86-
1]
87-
[(#f #f #f) ; only arg2 should be executed
88-
(vhint-set! arg2 #t)
89-
2]
90-
[(_ _ _) ; both paths should be executed
91-
(vhint-set! arg1 #t)
92-
(vhint-set! arg2 #t)
93-
(set! converged? #f)
94-
#t])]
95-
[(ival-fmin)
96-
(match-define (list _ arg1 arg2) instr)
97-
(define cmp (ival-> (vector-ref vregs arg1) (vector-ref vregs arg2)))
98-
(match* ((ival-lo cmp) (ival-hi cmp) (ival-err? cmp))
99-
[(#t #t #f) ; only arg2 should be executed
100-
(vhint-set! arg2 #t)
101-
2]
102-
[(#f #f #f) ; only arg1 should be executed
103-
(vhint-set! arg1 #t)
104-
1]
105-
[(_ _ _) ; both paths should be executed
106-
(vhint-set! arg1 #t)
107-
(vhint-set! arg2 #t)
108-
(set! converged? #f)
109-
#t])]
110-
[(ival-< ival-<= ival-> ival->= ival-== ival-!= ival-and ival-or ival-not)
111-
(define cmp (vector-ref vregs (+ varc n)))
112-
(match* ((ival-lo cmp) (ival-hi cmp) (ival-err? cmp))
113-
; result is known
114-
[(#t #t #f) (ival-bool #t)]
115-
; result is known
116-
[(#f #f #f) (ival-bool #f)]
117-
[(_ _ _) ; all the paths should be executed
118-
(define srcs (rest instr))
119-
(for-each (λ (x) (vhint-set! x #t)) srcs)
120-
(set! converged? #f)
121-
#t])]
122-
[else ; at this point we are given that the current instruction should be executed
123-
(define srcs
124-
(drop-self-pointers (rest instr)
125-
(+ n
126-
varc))) ; then, children instructions should be executed as well
127-
(for-each (λ (x) (vhint-set! x #t)) srcs)
128-
#t])]))
129-
(vector-set! vhint n hint*))
130-
(values vhint converged?))
22+
baseline-apply)
13123

13224
; ----------------------------------------- COMPILATION ----------------------------------------------
13325
(define (baseline-compile exprs vars discs)
@@ -189,7 +81,7 @@
18981
(define max-precision (rival-machine-max-precision machine))
19082
(define start-prec (+ (discretization-target (vector-ref discs (- (vector-length discs) 1))) 10))
19183
; Load arguments
192-
(baseline-machine-load machine (vector-map ival-real pt))
84+
(rival-machine-load machine (vector-map ival-real pt))
19385
(let loop ([prec start-prec]
19486
[iter 0])
19587
(set-rival-machine-iteration! machine iter)
@@ -205,7 +97,7 @@
20597
[else (loop (* 2 prec) (+ iter 1))])))
20698

20799
(define (baseline-analyze machine rect [hint #f])
208-
(baseline-machine-load machine rect)
100+
(rival-machine-load machine rect)
209101
(set-rival-machine-iteration! machine 0)
210102
(define-values (good? done? bad? stuck? fvec)
211103
(baseline-machine-full machine (or hint (rival-machine-default-hint machine))))
@@ -243,7 +135,7 @@
243135
(cond
244136
[(and (ival-lo-fixed? reg) (ival-hi-fixed? reg)) (vector-set! vuseful i #f)]
245137
[useful?
246-
(for ([arg (in-list (drop-self-pointers (cdr instr) (+ i varc)))]
138+
(for ([arg (in-list (drop-self-pointer (cdr instr) (+ i varc)))]
247139
#:when (>= arg varc))
248140
(vector-set! vuseful (- arg varc) #t))]))
249141

@@ -253,7 +145,7 @@
253145
[best-known-precision (in-vector vbest-precs)]
254146
[constant? (in-vector vinitial-repeats)]
255147
[n (in-naturals)])
256-
(define tail-registers (drop-self-pointers (cdr instr) (+ n varc)))
148+
(define tail-registers (drop-self-pointer (cdr instr) (+ n varc)))
257149
; When instr is a constant instruction - keep tracks of old precision with vbest-precs vector
258150
(define no-need-to-reevaluate
259151
(and constant?
@@ -269,136 +161,15 @@
269161
new-prec)) ; record new best precision for the constant instruction
270162
(vector-set! vrepeats n repeat)))
271163

272-
(baseline-machine-record machine
273-
'adjust
274-
-1
275-
(* iter 1000)
276-
(- (current-inexact-milliseconds) start-time)
277-
(- (current-memory-use 'cumulative) start-memory)
278-
iter)))
279-
280-
(define (drop-self-pointers tail-regs n)
281-
(filter (λ (x) (not (equal? x n))) tail-regs))
164+
(rival-machine-record machine
165+
'adjust
166+
-1
167+
(* iter 1000)
168+
(- (current-inexact-milliseconds) start-time)
169+
(- (current-memory-use 'cumulative) start-memory)
170+
iter)))
282171

283172
(define (baseline-machine-full machine vhint)
284173
(baseline-machine-adjust machine)
285-
(baseline-machine-run machine vhint)
286-
(baseline-machine-return machine))
287-
288-
(define (baseline-machine-load machine args)
289-
(vector-copy! (rival-machine-registers machine) 0 args))
290-
291-
(define (baseline-machine-run machine vhint)
292-
(define ivec (rival-machine-instructions machine))
293-
(define varc (vector-length (rival-machine-arguments machine)))
294-
(define vregs (rival-machine-registers machine))
295-
(define precisions (rival-machine-precisions machine))
296-
(define repeats (rival-machine-repeats machine))
297-
(define initial-repeats (rival-machine-initial-repeats machine))
298-
(define iter (rival-machine-iteration machine))
299-
(define first-iter? (zero? (rival-machine-iteration machine)))
300-
301-
(for ([instr (in-vector ivec)]
302-
[n (in-naturals varc)]
303-
[precision (in-vector precisions)]
304-
[repeat (in-vector (if first-iter? initial-repeats repeats))]
305-
[hint (in-vector vhint)]
306-
#:unless (or (not hint) repeat))
307-
(define out
308-
(match hint
309-
[#t
310-
(define start-time (current-inexact-milliseconds))
311-
(define start-memory (current-memory-use 'cumulative))
312-
(define res
313-
(parameterize ([bf-precision precision])
314-
(apply-instruction instr vregs)))
315-
(define name (object-name (car instr)))
316-
(define time (- (current-inexact-milliseconds) start-time))
317-
(define memory (- (current-memory-use 'cumulative) start-memory))
318-
(baseline-machine-record machine name n precision time memory iter)
319-
res]
320-
[(? integer? _) (vector-ref vregs (list-ref instr hint))]
321-
[(? ival? _) hint]))
322-
(vector-set! vregs n out)))
323-
324-
(define (apply-instruction instr regs)
325-
;; By special-casing the 0-3 instruction case,
326-
;; we avoid any allocation in the common case.
327-
;; We could add more cases if we want wider instructions.
328-
;; At some extreme, vector->values plus call-with-values
329-
;; becomes the fastest option.
330-
(match instr
331-
[(list op) (op)]
332-
[(list op a) (op (vector-ref regs a))]
333-
[(list op a b) (op (vector-ref regs a) (vector-ref regs b))]
334-
[(list op a b c) (op (vector-ref regs a) (vector-ref regs b) (vector-ref regs c))]
335-
[(list op args ...) (apply op (map (curryr vector-ref regs) args))]))
336-
337-
(define (baseline-machine-return machine)
338-
(define discs (rival-machine-discs machine))
339-
(define vregs (rival-machine-registers machine))
340-
(define rootvec (rival-machine-outputs machine))
341-
(define ovec (make-vector (vector-length rootvec)))
342-
(define good? #t)
343-
(define done? #t)
344-
(define bad? #f)
345-
(define stuck? #f)
346-
(define fvec
347-
(for/vector #:length (vector-length rootvec)
348-
([root (in-vector rootvec)]
349-
[disc (in-vector discs)]
350-
[n (in-naturals)])
351-
(define out (vector-ref vregs root))
352-
(define lo ((discretization-convert disc) (ival-lo out)))
353-
(define hi ((discretization-convert disc) (ival-hi out)))
354-
(define distance ((discretization-distance disc) lo hi))
355-
(unless (= distance 0)
356-
(set! done? #f)
357-
(when (and (ival-lo-fixed? out) (ival-hi-fixed? out))
358-
(set! stuck? #t)))
359-
(cond
360-
[(ival-err out) (set! bad? #t)]
361-
[(ival-err? out) (set! good? #f)])
362-
lo))
363-
(values good? (and good? done?) bad? stuck? fvec))
364-
365-
; ---------------------------------------- PROFILING -------------------------------------------------
366-
(define (baseline-profile machine param)
367-
(match param
368-
['iteration (rival-machine-iteration machine)]
369-
['precision (bf-precision)]
370-
['instructions (vector-length (rival-machine-instructions machine))]
371-
['executions
372-
(define profile-ptr (rival-machine-profile-ptr machine))
373-
(define profile-instruction (rival-machine-profile-instruction machine))
374-
(define profile-number (rival-machine-profile-number machine))
375-
(define profile-time (rival-machine-profile-time machine))
376-
(define profile-memory (rival-machine-profile-memory machine))
377-
(define profile-precision (rival-machine-profile-precision machine))
378-
(define profile-iteration (rival-machine-profile-iteration machine))
379-
(begin0 (for/vector #:length profile-ptr
380-
([instruction (in-vector profile-instruction 0 profile-ptr)]
381-
[number (in-vector profile-number 0 profile-ptr)]
382-
[precision (in-vector profile-precision 0 profile-ptr)]
383-
[time (in-flvector profile-time 0 profile-ptr)]
384-
[memory (in-vector profile-memory 0 profile-ptr)]
385-
[iter (in-vector profile-iteration 0 profile-ptr)])
386-
(make-execution instruction number precision time memory iter))
387-
(set-rival-machine-profile-ptr! machine 0))]))
388-
389-
(define (baseline-machine-record machine name number precision time memory iter)
390-
(define profile-ptr (rival-machine-profile-ptr machine))
391-
(define profile-instruction (rival-machine-profile-instruction machine))
392-
(when (< profile-ptr (vector-length profile-instruction))
393-
(define profile-number (rival-machine-profile-number machine))
394-
(define profile-time (rival-machine-profile-time machine))
395-
(define profile-memory (rival-machine-profile-memory machine))
396-
(define profile-precision (rival-machine-profile-precision machine))
397-
(define profile-iteration (rival-machine-profile-iteration machine))
398-
(vector-set! profile-instruction profile-ptr name)
399-
(vector-set! profile-number profile-ptr number)
400-
(vector-set! profile-memory profile-ptr memory)
401-
(vector-set! profile-precision profile-ptr precision)
402-
(vector-set! profile-iteration profile-ptr iter)
403-
(flvector-set! profile-time profile-ptr time)
404-
(set-rival-machine-profile-ptr! machine (add1 profile-ptr))))
174+
(rival-machine-run machine vhint)
175+
(rival-machine-return machine))

main.rkt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@
108108
[baseline-apply
109109
(->* (rival-machine? (vectorof value?))
110110
((or/c (vectorof any/c) boolean?))
111-
(vectorof any/c))]
112-
[baseline-profile (-> rival-machine? symbol? any/c)])
111+
(vectorof any/c))])
113112
(struct-out discretization)
114113
(struct-out exn:rival)
115114
(struct-out exn:rival:invalid)

time.rkt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@
9494
(define exs (vector-ref (baseline-apply baseline-machine (list->vector (map bf pt))) 1))
9595
(list 'valid exs))))
9696
(define baseline-apply-time (- (current-inexact-milliseconds) baseline-start-apply))
97-
(define baseline-precision (baseline-profile baseline-machine 'precision))
98-
(define baseline-executions (baseline-profile baseline-machine 'executions))
99-
(define baseline-iteration (baseline-profile baseline-machine 'iteration))
97+
(define baseline-precision (bf-precision))
98+
(define baseline-executions (rival-profile baseline-machine 'executions))
99+
(define baseline-iteration (rival-profile baseline-machine 'iterations))
100100

101101
; Store histograms data
102102
(when (> baseline-iteration 0)

0 commit comments

Comments
 (0)