Download presentation
Presentation is loading. Please wait.
Published byOwen Clark Modified over 9 years ago
1
Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project http://www.cs.cmu.edu/~pscico
2
2 Geometric Predicates does C lie left/right/on the line AB? does D lie in/out/on the circle ABC? Orientation test ( in convex hull ) Incircle test ( in Delaunay triangulation ) C A B A B C D Programs need to test relative positions of points based on their coordinates. Simple examples (in 2D):
3
3 Geometric Predicates (cont’d) Orientation(A, B, C) = We only consider the sign of an expression involving +, - and £
4
4 Convex Hull Miscomputed A B C A B C Using Orientation Predicate in exact arithmetic Using Orientation Predicate in floating-point arithmetic D E D E !! Floating point arithmetic errs inconsistently !!
5
5 Floating-point Filters let F = E (X) in floating point if F > error bound then 1 else if –F > error bound then –1 else increase precision and repeat or switch to exact arithmetic Get correct sign (-1, 0 or 1) of an exact expression E using floating-point! “filters out” the easy cases If the correct result is 0, must go to exact phase
6
6 Current Methods Interval arithmetic (R.E. Moore, U. Kulisch) +little modification to the source program -does not perform well in low degrees Single-phase filters (Fortune & Van Wyk, LEDA) +compiler available -surpassed by multi-phase filters
7
7 Current Methods (cont’d) Multi-phase filters (J. Shewchuk) four phases of increasing precision +re-use of results from earlier phases +between 8-35% slower than fp implementation -requires estimating rounding errors -involved and hard to implement: Shewchuk’s Insphere C implementation >500 lines of code -only existing are 2D and 3D Orientation and Insphere predicates, but we need more
8
8 Functional program COMPILER numerical analysis Staged geometric predicate
9
9 Contributions Source language expression involving +, -, £ arithmetic to be perceived as exact nested anonymous functions (staging) Compiler to ML (or C) multi-phase target code error bounds estimated automatically formally specified (so, can argue correctness)
10
10 Staging A C To test a set of points for position with respect to line AC: -compute as much as possible knowing only A and C -return the remaining work as function -apply this function to each of the points
11
11 Overview Introduction Arbitrary precision arithmetic and computing in phases Stage compilation Performance Conclusions and Future Work Overview
12
12 Notation Exact Floating point +-£©ª Operations: Precision p = #bits in mantissa Machine Epsilon - if operation result is 1.0, then the rounding error is smaller than
13
13 Error recovery Theorem. (Knuth) If x = a © b, the rounding error of x can be recovered by floating-point operations. If is the rounding error, and c = x ª a then So exactly.
14
14 How to Increase Precision? (D. Priest) Sorted list of fp-numbers arbitrary precision. + 1.01010 £ 2 120 1.10100 £ 2 67 1.00111 £ 2 10 Knuth’s theorem: Pair (x, ) twice the precision. Expansion = 1010100000110100000010011100000...000.............00 list of magnitude decreasing non- overlapping floats
15
15 Phases and re-use (J. Shewchuk) Arbitrary precision arithmetic phases can reuse the results of their predecessors. Example: Let a 1 – b 1 = x 1 + 1 and a 2 – b 2 = x 2 + 2. Expand E as
16
16 Phases and re-use (cont’d) Strategy for finding the sign of Evaluate E in phases, increasing precision on demand. Example: floating point phase exact phase
17
17 Reusing results
18
18 Reusing results
19
19 Reusing results
20
20 Reusing results
21
21 Overview Introduction Arbitrary precision arithmetic and computing in phases Stage compilation Performance Conclusion and Future Work Overview
22
22 Source Language Basic arithmetic operations: +, -, £, unary -, sq Assignments: val x = some expression Nested anonymous functions Example: Implicit sign test at the last assignment.
23
23 Stage compilation Every stage compiled into: -floating-point code (phase A) -code for estimating the rounding error Later phases (B, C and D) “suspended” until explicitly executed
24
24 Stage 1 Helper code: - -modules for arbitrary precision arithmetic Compiler output: A B C D
25
25 Stage 2 B A C D
26
26 Overview Introduction Arbitrary precision arithmetic and computing in phases Stage compilation Performance Conclusion and Future Work Overview
27
27 Performance Experiments to match Shewchuk’s hand-generated predicates Target language C No staging! Uniform Random Point Distribution Shewchuk’s version Automatically generated version Slowdown Orient2D0.208 ms0.249 ms1.20 Orient3D0.707 ms0.772 ms1.09 InCircle6.440 ms5.600 ms0.87 InSphere16.430 ms39.220 ms2.39
28
28 Performance (cont’d) 2D Delaunay Triangulation Shewchuk’s version Automatically generated version Slowdown uniform random 1187 ms1410 ms1.19 tilted grid 2060 ms3678 ms1.78 co-circular1190 ms1578 ms1.33 Between 1 and 2.4 times slower than hand-generated code Possibility for improvement with a better translation to C
29
29 Future Work Extend the language: -multiplication by 2 n doesn’t introduce errors. -summation of more than 2 elements can be done quicker. Evaluate the effects of staging. Allow better control over the FP arithmetic in SML. -processor flags -precision of internal FP registers Design a language that can compile and run the predicates -run-time code generation and meta-programming -application: robust solid modeler
30
30 Conclusions Automated conversion of exact predicates into floating point code: preserves correctness of computation despite rounding compile-time error analysis nested anonymous functions (staging) performance of generated predicates close to hand-generated code Formal specification of the compiler. More control needed over floating point in SML. -processor flags -precision of internal FP registers Compiler can be found at http://www.cs.cmu.edu/~pscico
31
31 Operations on Expansions ( arbitrary precision arithmetic ) Example: Adding two expansions
32
32 Source Language Basic arithmetic operations: +, -, £, sq, unary – Assignments: val x = some expression Nested anonymous functions (staging)
33
33 A B C D Compiling a Program
34
34 Compiling a Program (cont’d) A reused
35
35 Here goes example for Orient2D in SML or C type pnt=real*real fun orient(A:pnt, B:pnt, C:pnt)= let val (a1, a2) = A val (b1, b2) = B val (c1, c2) = C val d = (c1-a1)*(c2-b2) - (c2-a2)*(c1-b1) in if d > 0 then Left else if d < 0 then Right else On end Nothing! They are just often mistaken for real numbers.
36
36 What’s Wrong With Floating Point Numbers? Floats are unevenly distributed on the real axis, thus introducing rounding errors in the computation.
37
37 What’s Wrong? (cont’d) Example: Assume precision of 2 decimal digits. Denote © the operation of (rounded) addition on floats. Floats are a subset but NOT a subtype of rationals and reals.
38
38 Types of Numbers Real numbers Usually assumed when developing or proving algorithms Basic operations not computable + - Rational numbers Closed under basic operations Slow + - Floating-point numbers Fast Not closed under basic arithmetic operations (rounding errors) Do not satisfy usual laws like associativity, distributivity + - -
39
39 Phase A Compilation Judgment. Selected rules
40
40 Phase B Compilation Judgment. Selected rules
41
41 Operations on Expansions Example: Adding a double to an expansion.
42
42 Overlapping and Machine . 10101010…000.10 10001010…01011. 10101010…000.10…..10001010…01011 p bits and x do not overlap · 2 -p x. In IEEE double precision, p=53.
43
43 Phases of Increasing Precision let R = F (X) in floating point if abs(R) > estimated error bound then sign(R) else increase precision and repeat or switch to exact arithmetic floating point phase intermediate phases exact phase Finitely many intermediate phases!
44
44 Arithmetic Rational numbers + exact - slow Floating-point numbers - inexact + fast
45
45 Non-overlapping. 0000……………0.00 0000……………000. 10101010…000.10 10001010…01011. 10101010…000.10…..10001010…01011 The rounded sum x and its rounding error do not overlap. Mathematically,
46
46 Expansions and Compiler let R = F (X) in floating point if R > error bound then 1 else if –R > error bound then –1 else increase precision and repeat or switch to exact arithmetic “filters out” the easy cases compilation Exact expression F Use expansions for more precise computations
47
47 Phases and Compiler let R = F (X) in floating point if R > error bound then 1 else if –R > error bound then –1 else R2 = phase B if abs(R2) > error bound then sign(R2) else R3 = phase C if abs(R3) > error bound then sign(R3) else R4 = phase D; (* exact phase *) sign (R4) compilation Exact expression F
48
48 Bounding the Rounding Error Floating-point operations are correctly rounded. Consequence: for any operation ¢ 2 {+, -, £ } Notice: is static part of the error while |x ¯ y| is dynamic.
49
49 Bounding Error of Composite Expressions If X 1 = x 1 § 1 p 1 and X 2 = x 2 § 2 p 2 then
50
50 Error Bounds and Compilation Static error bound -Obtained EXACTLY in compile-time. -Rounded and then emitted into the target program as a floating-point constant. Dynamic error bound -Code must be generated for its computation and emitted into the target program.
51
51 Error Bounds and Compilation (cont’d) let R = F (X) in floating point dynamic_error = D (X) error = static_error * dynamic_error if R > error then 1 else if –R > error then –1 else jump to phases B, C and D compilation Exact expression F
52
52 Compilation example Source expression c – (a + b) 2 Target Standard ML program:
53
53 Source Language Basic arithmetic operations: +, -, £, sq, unary - Assignments: val x = some expression Nested anonymous functions
54
54 Staging (cont’d)
55
55 Overview Introduction Arbitrary precision arithmetic and computing in phases Multi-stage filters Performance Conclusion and Future Work
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.