Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert...

55
Automatic Generation Automatic Generation of Staged Geometric of Staged Geometric Predicates Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project http://www.cs.cmu.edu/~pscico

Transcript of Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert...

Automatic Generation of Automatic Generation of Staged Geometric Staged Geometric PredicatesPredicates

Aleksandar Nanevski, Guy Blelloch and Robert Harper

PSCICO project

http://www.cs.cmu.edu/~pscico

2

Geometric PredicatesGeometric 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

AB

CD

•Programs need to test relative positions of points based on their coordinates.

•Simple examples (in 2D):

3

Geometric Predicates Geometric Predicates (cont’d)(cont’d)

•Orientation(A, B, C) =

•We only consider the sign of an expression involving +, - and £

4

Convex Hull MiscomputedConvex 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

Floating-point FiltersFloating-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

Current MethodsCurrent 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

Current Methods (cont’d)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

Functional program

COMPILERCOMPILER

numerical analysis

Staged geometric predicate

9

ContributionsContributions

•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

StagingStaging

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

OverviewOverview

•Introduction

•Arbitrary precision arithmetic and computing in phases

•Stage compilation

•Performance

•Conclusions and Future Work

OverviewOverview

12

NotationNotation

ExactExact Floating Floating pointpoint

++

--

££

©©

ªª

• Operations:

• Precision p = #bits in mantissa

• Machine Epsilon

- if operation result is 1.0, then the rounding error is smaller than

13

Error recoveryError 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

How to Increase How to Increase Precision?Precision?

• (D. Priest) Sorted list of fp-numbers arbitrary precision.

+

1.01010£21

20

1.10100£26

7

1.00111£21

0

•Knuth’s theorem: Pair (x, ) twice the precision.

Expansion =

1010100000

1101000000

1001110000

0...0 00.............00

list of magnitude decreasing non-overlapping floats

15

Phases and re-usePhases and re-use• (J. Shewchuk) Arbitrary precision arithmetic phases can reuse the results of their predecessors.

Example:

Let a1 – b1 = x1 + 1 and a2 – b2 = x2 + 2 . Expand E as

16

Phases and re-use Phases and re-use (cont’d)(cont’d)

• Strategy for finding the sign of

•Evaluate E in phases, increasing precision on demand.

•Example: floating point phase

exact phase

17

Reusing resultsReusing results

18

Reusing resultsReusing results

19

Reusing results Reusing results

20

Reusing resultsReusing results

21

OverviewOverview

•Introduction

•Arbitrary precision arithmetic and computing in phases

•Stage compilation

•Performance

•Conclusion and Future Work

OverviewOverview

22

Source LanguageSource Language•Basic arithmetic operations: +, -, £, unary -, sq

•Assignments: val x = some expression

•Nested anonymous functions

•Example:

•Implicit sign test at the last assignment.

23

Stage compilationStage 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

Stage 1Stage 1•Helper code:

-

-modules for arbitrary precision arithmetic

•Compiler output:

A

B

C

D

25

Stage 2Stage 2

B

A C

D

26

OverviewOverview

•Introduction

•Arbitrary precision arithmetic and computing in phases

•Stage compilation

•Performance

•Conclusion and Future Work

OverviewOverview

27

PerformancePerformance•Experiments to match Shewchuk’s hand-generated predicates

•Target language C

•No staging!

Uniform Random Point DistributionUniform Random Point Distribution

Shewchuk’s version

Automatically generated version

Slowdown

Orient2DOrient2D 0.208 ms 0.249 ms 1.20

Orient3DOrient3D 0.707 ms 0.772 ms 1.09

InCircleInCircle 6.440 ms 5.600 ms 0.87

InSphereInSphere 16.430 ms 39.220 ms 2.39

28

Performance (cont’d)Performance (cont’d)

2D Delaunay Triangulation2D Delaunay Triangulation

Shewchuk’s version

Automatically generated version

Slowdown

uniform uniform randomrandom

1187 ms 1410 ms 1.19

tilted gridtilted grid 2060 ms 3678 ms 1.78

co-circularco-circular 1190 ms 1578 ms 1.33

•Between 1 and 2.4 times slower than hand-generated code

•Possibility for improvement with a better translation to C

29

Future WorkFuture Work

•Extend the language:

-multiplication by 2n 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

ConclusionsConclusions•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

Operations on Expansions Operations on Expansions ((arbitrary precision arithmeticarbitrary precision arithmetic))Example: Adding two expansions

32

Source LanguageSource Language

•Basic arithmetic operations: +, -, £, sq, unary –

•Assignments: val x = some expression

•Nested anonymous functions (staging)

33

A

B

C

D

Compiling a Compiling a ProgramProgram

34

Compiling a Program Compiling a Program (cont’d)(cont’d)

A

reused

35

Here goes example for Here goes example for Orient2D in SML or COrient2D 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 Onend

Nothing! They are just often mistaken for real numbers.

36

What’s Wrong With What’s Wrong With Floating Point Numbers?Floating Point Numbers?Floats are unevenly distributed on the real axis, thus introducing rounding errors in the computation.

37

What’s Wrong? (cont’d)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

Types of NumbersTypes of Numbers

Real numbers

Usually assumed when developing or proving algorithms

Basic operations not computable

+

-

Rational numbersClosed under basic operations

Slow

+

-

Floating-point numbersFast

Not closed under basic arithmetic operations (rounding errors)

Do not satisfy usual laws like associativity, distributivity

+

-

-

39

Phase A CompilationPhase A Compilation

•Judgment .

•Selected rules

40

Phase B CompilationPhase B Compilation

•Judgment .

•Selected rules

41

Operations on ExpansionsOperations on Expansions

Example: Adding a double to an expansion.

Title:sum1.epsCreator:fig2dev Version 3.2 Patchlevel 3cPreview:This EPS picture was not savedwith a preview included in it.Comment:This EPS picture will print to aPostScript printer, but not toother types of printers.

42

Overlapping and Machine Overlapping and Machine

10101010…000..10

10001010…01011

10101010…000..10…..10001010…01011

p bits

p bits

• and x do not overlap · 2-p x.

• In IEEE double precision, p=53.

43

Phases of Increasing Phases of Increasing PrecisionPrecision

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 phasesexact phase

•Finitely many intermediate phases!

44

ArithmeticArithmetic

Rational numbers

+ exact

- slow

Floating-point numbers

- inexact

+ fast

45

Non-overlappingNon-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

Expansions and CompilerExpansions 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

Phases and CompilerPhases 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

Bounding the Rounding Bounding the Rounding ErrorError

• Floating-point operations are correctly rounded.

• Consequence: for any operation ¢ 2 {+, -, £}

• Notice: is static part of the error while |x ¯ y| is dynamic.

49

Bounding Error of Bounding Error of Composite ExpressionsComposite Expressions

• If X1 = x1 § 1 p1 and X2 = x2 § 2 p2 then

50

Error Bounds and Error Bounds and CompilationCompilation

•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

Error Bounds and Error Bounds and Compilation (cont’d)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

Compilation exampleCompilation example•Source expression c – (a + b)2

•Target Standard ML program:

53

Source LanguageSource Language•Basic arithmetic operations: +, -, £, sq, unary -

•Assignments: val x = some expression

•Nested anonymous functions

54

Staging (cont’d)Staging (cont’d)

55

OverviewOverview

•Introduction

•Arbitrary precision arithmetic and computing in phases

•Multi-stage filters

•Performance

•Conclusion and Future Work