|
6 | 6 |
|
7 | 7 | (require (only-in "../eval/compile.rkt" exprs->batch fn->ival-fn make-initial-repeats) |
8 | 8 | (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) |
9 | 15 | "../eval/machine.rkt" |
10 | 16 | "../eval/main.rkt" |
11 | 17 | (only-in "../ops/core.rkt" new-ival) |
12 | 18 | "../ops/all.rkt") |
13 | 19 |
|
14 | 20 | (provide baseline-compile |
15 | 21 | 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) |
131 | 23 |
|
132 | 24 | ; ----------------------------------------- COMPILATION ---------------------------------------------- |
133 | 25 | (define (baseline-compile exprs vars discs) |
|
189 | 81 | (define max-precision (rival-machine-max-precision machine)) |
190 | 82 | (define start-prec (+ (discretization-target (vector-ref discs (- (vector-length discs) 1))) 10)) |
191 | 83 | ; Load arguments |
192 | | - (baseline-machine-load machine (vector-map ival-real pt)) |
| 84 | + (rival-machine-load machine (vector-map ival-real pt)) |
193 | 85 | (let loop ([prec start-prec] |
194 | 86 | [iter 0]) |
195 | 87 | (set-rival-machine-iteration! machine iter) |
|
205 | 97 | [else (loop (* 2 prec) (+ iter 1))]))) |
206 | 98 |
|
207 | 99 | (define (baseline-analyze machine rect [hint #f]) |
208 | | - (baseline-machine-load machine rect) |
| 100 | + (rival-machine-load machine rect) |
209 | 101 | (set-rival-machine-iteration! machine 0) |
210 | 102 | (define-values (good? done? bad? stuck? fvec) |
211 | 103 | (baseline-machine-full machine (or hint (rival-machine-default-hint machine)))) |
|
243 | 135 | (cond |
244 | 136 | [(and (ival-lo-fixed? reg) (ival-hi-fixed? reg)) (vector-set! vuseful i #f)] |
245 | 137 | [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)))] |
247 | 139 | #:when (>= arg varc)) |
248 | 140 | (vector-set! vuseful (- arg varc) #t))])) |
249 | 141 |
|
|
253 | 145 | [best-known-precision (in-vector vbest-precs)] |
254 | 146 | [constant? (in-vector vinitial-repeats)] |
255 | 147 | [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))) |
257 | 149 | ; When instr is a constant instruction - keep tracks of old precision with vbest-precs vector |
258 | 150 | (define no-need-to-reevaluate |
259 | 151 | (and constant? |
|
269 | 161 | new-prec)) ; record new best precision for the constant instruction |
270 | 162 | (vector-set! vrepeats n repeat))) |
271 | 163 |
|
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))) |
282 | 171 |
|
283 | 172 | (define (baseline-machine-full machine vhint) |
284 | 173 | (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)) |
0 commit comments