Skip to content

Commit 2951533

Browse files
authored
Merge pull request #1400 from herbie-fp/cache-accelerators
Add a cache in front of every Rival accelerator
2 parents 90f0ede + d7b7b57 commit 2951533

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/syntax/generators.rkt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
ffi/unsafe)
66

77
(require "../core/rival.rkt"
8+
"../config.rkt"
89
"types.rkt")
910

1011
(provide from-rival
@@ -24,14 +25,26 @@
2425

2526
; ----------------------- RIVAL GENERATOR ---------------------------
2627

27-
(define-generator ((from-rival) spec ctx)
28+
(define/reset caches
29+
'()
30+
(lambda ()
31+
(for ([cache (caches)])
32+
(hash-clear! cache))))
33+
34+
(define-generator ((from-rival #:cache? [cache? #t]) spec ctx)
2835
(define compiler (make-real-compiler (list spec) (list ctx)))
2936
(define fail ((representation-bf->repr (context-repr ctx)) +nan.bf))
30-
(lambda pt
37+
(define (compute . pt)
3138
(define-values (_ exs) (real-apply compiler (list->vector pt)))
3239
(if exs
3340
(first exs)
34-
fail)))
41+
fail))
42+
(cond
43+
[cache?
44+
(define cache (make-hash))
45+
(caches (cons cache (caches)))
46+
(lambda pt (hash-ref! cache pt (lambda () (apply compute pt))))]
47+
[else compute]))
3548

3649
; ----------------------- FFI GENERATOR -----------------------------
3750

www/doc/2.3/platforms.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,10 @@ <h3>Hard-to-emulate Operations</h3>
409409
operation's specification. Compilation is usually <em>much</em>
410410
slower than with a native floating-point implementation, but for
411411
unusual operations that are difficult to implement otherwise, it can
412-
still allow compilation to proceed.</p>
412+
still allow compilation to proceed. There is an
413+
optional <code>#:cache?</code> argument to <code>from-rival</code>,
414+
on by default. The cache makes Herbie much faster but uses a lot of
415+
memory.</p>
413416

414417
<p>Note that <code>from-rival</code> implementations are always
415418
"correctly-rounded", meaning as accurate as possible. Most targets

0 commit comments

Comments
 (0)