Top Banner
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
55

Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

Dec 30, 2015

Download

Documents

Owen Clark
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 2: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project 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):

Page 3: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

3

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

•Orientation(A, B, C) =

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

Page 4: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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 !!

Page 5: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 6: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 7: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 8: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

8

Functional program

COMPILERCOMPILER

numerical analysis

Staged geometric predicate

Page 9: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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)

Page 10: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 11: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

11

OverviewOverview

•Introduction

•Arbitrary precision arithmetic and computing in phases

•Stage compilation

•Performance

•Conclusions and Future Work

OverviewOverview

Page 12: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 13: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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.

Page 14: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 15: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 16: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 17: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

17

Reusing resultsReusing results

Page 18: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

18

Reusing resultsReusing results

Page 19: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

19

Reusing results Reusing results

Page 20: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

20

Reusing resultsReusing results

Page 21: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

21

OverviewOverview

•Introduction

•Arbitrary precision arithmetic and computing in phases

•Stage compilation

•Performance

•Conclusion and Future Work

OverviewOverview

Page 22: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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.

Page 23: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 24: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

24

Stage 1Stage 1•Helper code:

-

-modules for arbitrary precision arithmetic

•Compiler output:

A

B

C

D

Page 25: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

25

Stage 2Stage 2

B

A C

D

Page 26: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

26

OverviewOverview

•Introduction

•Arbitrary precision arithmetic and computing in phases

•Stage compilation

•Performance

•Conclusion and Future Work

OverviewOverview

Page 27: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 28: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 29: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 30: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 31: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

31

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

Page 32: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

32

Source LanguageSource Language

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

•Assignments: val x = some expression

•Nested anonymous functions (staging)

Page 33: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

33

A

B

C

D

Compiling a Compiling a ProgramProgram

Page 34: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

34

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

A

reused

Page 35: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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.

Page 36: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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.

Page 37: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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.

Page 38: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

+

-

-

Page 39: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

39

Phase A CompilationPhase A Compilation

•Judgment .

•Selected rules

Page 40: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

40

Phase B CompilationPhase B Compilation

•Judgment .

•Selected rules

Page 41: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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.

Page 42: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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.

Page 43: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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!

Page 44: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

44

ArithmeticArithmetic

Rational numbers

+ exact

- slow

Floating-point numbers

- inexact

+ fast

Page 45: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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,

Page 46: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 47: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 48: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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.

Page 49: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

49

Bounding Error of Bounding Error of Composite ExpressionsComposite Expressions

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

Page 50: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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.

Page 51: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

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

Page 52: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

52

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

•Target Standard ML program:

Page 53: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

53

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

•Assignments: val x = some expression

•Nested anonymous functions

Page 54: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

54

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

Page 55: Automatic Generation of Staged Geometric Predicates Aleksandar Nanevski, Guy Blelloch and Robert Harper PSCICO project pscico.

55

OverviewOverview

•Introduction

•Arbitrary precision arithmetic and computing in phases

•Multi-stage filters

•Performance

•Conclusion and Future Work