108
Two Notions of Beauty in Programming Robert Harper Computer Science Department Carnegie Mellon University IU CSD Distinguished Lecture Series November 2013

Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

  • Upload
    vudang

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Two Notions of Beauty in Programming

Robert Harper

Computer Science DepartmentCarnegie Mellon University

IU CSD Distinguished Lecture SeriesNovember 2013

Page 2: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Thanks

Thanks to IU CSD for the invitation!

This talk represents work with Guy E. Blelloch at Carnegie Mellon.

And with Ph.D. students John Greiner and Daniel Spoonhower.

Page 3: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Two Sources of Beauty in Programs

For me beauty in a program arises from two sources:

• Structure: code as an expression of an idea.

• Efficiency: code as instructions for a computer.

This has given rise to two theories of computation.

• Logical: compositionality (human effort).

• Combinatorial: efficiency (machine effort).

Oddly, these are largely disparate communities!

Page 4: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Two Sources of Beauty in Programs

For me beauty in a program arises from two sources:

• Structure: code as an expression of an idea.

• Efficiency: code as instructions for a computer.

This has given rise to two theories of computation.

• Logical: compositionality (human effort).

• Combinatorial: efficiency (machine effort).

Oddly, these are largely disparate communities!

Page 5: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Two Sources of Beauty in Programs

For me beauty in a program arises from two sources:

• Structure: code as an expression of an idea.

• Efficiency: code as instructions for a computer.

This has given rise to two theories of computation.

• Logical: compositionality (human effort).

• Combinatorial: efficiency (machine effort).

Oddly, these are largely disparate communities!

Page 6: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Reconciling the Two Theories

Historically,

• The logical side neglects efficiency in favor of structure.

• The combinatorial side neglects structure in favor of efficiency.

Prospectively,

• The logical side should pay more attention to efficiency.

• The combinatorial side should pay more attention to structure.

The λ-calculus is the key!

Page 7: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Reconciling the Two Theories

Historically,

• The logical side neglects efficiency in favor of structure.

• The combinatorial side neglects structure in favor of efficiency.

Prospectively,

• The logical side should pay more attention to efficiency.

• The combinatorial side should pay more attention to structure.

The λ-calculus is the key!

Page 8: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Reconciling the Two Theories

Historically,

• The logical side neglects efficiency in favor of structure.

• The combinatorial side neglects structure in favor of efficiency.

Prospectively,

• The logical side should pay more attention to efficiency.

• The combinatorial side should pay more attention to structure.

The λ-calculus is the key!

Page 9: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

The Great Rift

“On the fact that the Atlantic Ocean has two sides.” [EWD]

• American theory ≈ combinatorial theory.

• Euro-theory ≈ semantics and logic.

Both have had a big influence on practice:

• Efficient algorithms for a broad range of problems.

• Language design and verification tools.

Yet these two “theories” operate largely in isolation!

Page 10: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

The Great Rift

“On the fact that the Atlantic Ocean has two sides.” [EWD]

• American theory ≈ combinatorial theory.

• Euro-theory ≈ semantics and logic.

Both have had a big influence on practice:

• Efficient algorithms for a broad range of problems.

• Language design and verification tools.

Yet these two “theories” operate largely in isolation!

Page 11: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

The Great Rift

“On the fact that the Atlantic Ocean has two sides.” [EWD]

• American theory ≈ combinatorial theory.

• Euro-theory ≈ semantics and logic.

Both have had a big influence on practice:

• Efficient algorithms for a broad range of problems.

• Language design and verification tools.

Yet these two “theories” operate largely in isolation!

Page 12: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

American Theory

Algorithm analysis is based on machine models:

• Turing machine (TM) or Random Access Machine (RAM).

• Low-level: no abstraction, no composition.

• Allegedly, close to the hardware.

Machine models provide natural complexity measures:

• Time = number of instructions.

• Space = tape or memory usage.

Asymptotics smoothes over differences among models.

Page 13: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

American Theory

In practice algorithms are described using C-like notation.

• Clearer than TM or RAM code.

• Analyze compiled code, rather than source code.

An improvement, but still very limited:

• ephemeral data structures.

• manual memory management.

• poor composability.

• no abstraction.

Page 14: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Euro Theory

Euro theory is based on language models:

• Church’s (typed and untyped) λ-calculus.

• High-level: abstraction, composition are fundamental.

• Platform-independent.

Language models support composition via variables:

• If φ true ` ψ true, then if φ true, then ψ true.

• If x : σ ` N : τ , then if M : σ, then [M/x ]N : τ .

The λ-calculus is an elegant theory of composition.

Page 15: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Euro Theory

Languages based on λ-calculus stress

• persistent data structures.

• automatic memory management.

• strong composability.

• abstract types.

But there is relatively little emphasis on efficiency.

• No clear complexity measures.

• Few analytic results (but see Okasaki’s CMU Ph.D.).

Page 16: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

A (Tendentious) Thesis

Traditional imperative methods of programming are obsolete.

• Tedious to program, a nightmare to maintain.

• Largely incompatible with parallelism.

Functional methods are destined to dominate.

• Support verification and composition.

• Naturally accommodate parallelism.

The way forward is to synthesize Euro- and American theory.

Page 17: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

An Iatrogenic Disorder

Consider the AHU Quicksort Algorithm:

• Naturally parallel: recursive calls are independent.

• Elegantly high-level: uses only a sequence abstraction.

An imperative reformulation on a PRAM mutilates the algorithm:

• Manual storage allocation and mutation.

• Manual processor allocation for scheduling.

• Concurrency control for mutation.

What should be a matter of efficiency becomes a matter ofcorrectness!

Page 18: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

AHU Quicksort

Page 19: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics

To elevate the level of discourse we require a cost semantics.

• Define the abstract cost of execution of a language.

• Defines the parallel and sequential complexity.

Algorithm analysis is conducted at the level of the code we write.

• Cost semantics assigns a measure to each execution.

• Analyze asymptotic complexity in terms of this measure.

Page 20: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics

The abstract cost is validated by a provable implementation.

• Transform abstract cost into concrete cost on a machine.

• Account for platform characteristics such as number ofprocessors, cache hierarchy, and interconnect.

An end-to-end asymptotics with a clear separation of concerns.

• High-level, composable development and reasoning.

• Low-level implementation on hardware platforms.

So simple we teach it to first-year undergraduates!

Page 21: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics

The abstract cost is validated by a provable implementation.

• Transform abstract cost into concrete cost on a machine.

• Account for platform characteristics such as number ofprocessors, cache hierarchy, and interconnect.

An end-to-end asymptotics with a clear separation of concerns.

• High-level, composable development and reasoning.

• Low-level implementation on hardware platforms.

So simple we teach it to first-year undergraduates!

Page 22: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for Time

Associate a cost graph to the evaluation of a program.

• Dynamic, fully accurate record of data dependencies.

• Not a static analysis or approximation!

Example: function application.

e1 ⇓

g1

λx .e e2 ⇓

g2

v2 [v2/x ]e ⇓

g

v

e1(e2) ⇓

(g1⊗g2)⊕1⊕g

v

Page 23: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for Time

Associate a cost graph to the evaluation of a program.

• Dynamic, fully accurate record of data dependencies.

• Not a static analysis or approximation!

Example: function application.

e1 ⇓g1 λx .e e2 ⇓g2 v2 [v2/x ]e ⇓g v

e1(e2) ⇓

(g1⊗g2)⊕1⊕g

v

Page 24: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for Time

Associate a cost graph to the evaluation of a program.

• Dynamic, fully accurate record of data dependencies.

• Not a static analysis or approximation!

Example: function application.

e1 ⇓g1 λx .e e2 ⇓g2 v2 [v2/x ]e ⇓g v

e1(e2) ⇓(g1⊗g2)⊕1⊕g v

Page 25: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Graphs

Series-parallel cost graphs:

• 1: one unit of computation.

• g1 ⊕ g2: g2 depends on result of g1.

• g1 ⊗ g2: g1 and g2 are independent.

Application cost (g1 ⊗ g2)⊕ 1⊕ g specifies that

• Function and argument are evaluated in parallel.

• Function call costs one unit.

• Function execution depends on the function and argument.

Page 26: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Graphs

Series-parallel cost graphs:

• 1: one unit of computation.

• g1 ⊕ g2: g2 depends on result of g1.

• g1 ⊗ g2: g1 and g2 are independent.

Application cost (g1 ⊗ g2)⊕ 1⊕ g specifies that

• Function and argument are evaluated in parallel.

• Function call costs one unit.

• Function execution depends on the function and argument.

Page 27: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Graphs

Series-parallel cost graphs:

• 1: one unit of computation.

• g1 ⊕ g2: g2 depends on result of g1.

• g1 ⊗ g2: g1 and g2 are independent.

Application cost (g1 ⊗ g2)⊕ 1⊕ g specifies that

• Function and argument are evaluated in parallel.

• Function call costs one unit.

• Function execution depends on the function and argument.

Page 28: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Graphs

Series-parallel cost graphs:

• 1: one unit of computation.

• g1 ⊕ g2: g2 depends on result of g1.

• g1 ⊗ g2: g1 and g2 are independent.

Application cost (g1 ⊗ g2)⊕ 1⊕ g specifies that

• Function and argument are evaluated in parallel.

• Function call costs one unit.

• Function execution depends on the function and argument.

Page 29: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Graphs

Series-parallel cost graphs:

• 1: one unit of computation.

• g1 ⊕ g2: g2 depends on result of g1.

• g1 ⊗ g2: g1 and g2 are independent.

Application cost (g1 ⊗ g2)⊕ 1⊕ g specifies that

• Function and argument are evaluated in parallel.

• Function call costs one unit.

• Function execution depends on the function and argument.

Page 30: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Graphs

Series-parallel cost graphs:

• 1: one unit of computation.

• g1 ⊕ g2: g2 depends on result of g1.

• g1 ⊗ g2: g1 and g2 are independent.

Application cost (g1 ⊗ g2)⊕ 1⊕ g specifies that

• Function and argument are evaluated in parallel.

• Function call costs one unit.

• Function execution depends on the function and argument.

Page 31: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Graphs

11

10

5 8 9

743

62

1

Page 32: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics

Operations on sequences have similar cost semantics:

e ⇓

g

λx .e e ′ ⇓

g ′

[v1, . . . , vn]

[v1/x ]e ⇓

g1

v ′1 . . . [vn/x ]e ⇓

gn

v ′n

map(e; e ′) ⇓

(g⊗g ′)⊕(⊗

i gi )⊕1

[v ′1, . . . , v′n]

To map a function over a sequence,

• Evaluate the function and the sequence in parallel, and then

• Apply the function to each element in parallel.

• Create a new sequence of results.

Page 33: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics

Operations on sequences have similar cost semantics:

e ⇓g λx .e e ′ ⇓g ′[v1, . . . , vn]

[v1/x ]e ⇓

g1

v ′1 . . . [vn/x ]e ⇓

gn

v ′n

map(e; e ′) ⇓

(g⊗g ′)⊕(⊗

i gi )⊕1

[v ′1, . . . , v′n]

To map a function over a sequence,

• Evaluate the function and the sequence in parallel, and then

• Apply the function to each element in parallel.

• Create a new sequence of results.

Page 34: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics

Operations on sequences have similar cost semantics:

e ⇓g λx .e e ′ ⇓g ′[v1, . . . , vn]

[v1/x ]e ⇓g1 v ′1 . . . [vn/x ]e ⇓gn v ′nmap(e; e ′) ⇓

(g⊗g ′)⊕(⊗

i gi )⊕1

[v ′1, . . . , v′n]

To map a function over a sequence,

• Evaluate the function and the sequence in parallel, and then

• Apply the function to each element in parallel.

• Create a new sequence of results.

Page 35: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics

Operations on sequences have similar cost semantics:

e ⇓g λx .e e ′ ⇓g ′[v1, . . . , vn]

[v1/x ]e ⇓g1 v ′1 . . . [vn/x ]e ⇓gn v ′nmap(e; e ′) ⇓(g⊗g ′)⊕(

⊗i gi )⊕1 [v ′1, . . . , v

′n]

To map a function over a sequence,

• Evaluate the function and the sequence in parallel, and then

• Apply the function to each element in parallel.

• Create a new sequence of results.

Page 36: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Work and Span

The work w(g) of a cost graph g is the size of g .

• w(1) = 1, w(g1 ⊗ g2) = w(g1 ⊕ g2) = w(g1) + w(g2).

• Measures the sequential time complexity.

The span d(g) of a cost graph g is the critical path length of g .

• d(1) = 1, d(g1 ⊗ g2) = max(d(g1), d(g2)),d(g1 ⊕ g2) = d(g1) + d(g2).

• Measures the parallel time complexity.

Page 37: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Graphs

11

10

5 8 9

743

62

1

Work = 11, Span = 6

Page 38: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Mergesort

fun merge xs ys =

case (xs, ys) of

([], ys) ⇒ ys

| (xs,[]) ⇒ xs

| (x::xs’, y::ys’) ⇒case x<y of

true ⇒ x :: merge xs’ ys

| false ⇒ y :: merge xs ys’

fun sort [] = []

| sort [x] = [x]

| sort xs =

let val (ys, zs) = split xs

in merge (sort ys, sort zs) end

Page 39: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Mergesort

The work (sequential time) is optimal, O(n log n) for n items.

The span (parallel time) is sensitive to the data structure:

• For lists, O(n), because splitting is slow.

• For trees, O(log3 n), using rebalancing.

The parallelizability ratio, w/d , is O(n/ log2 n) for trees.

The correctness of the parallel implementation is never in question!

Page 40: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Provable Implementation

Brent’s Principle: A computation with work w and span d can beimplemented on a p-processor PRAM in time O(max(w/p, d)).

• Work in chunks of p as much as possible.

• Number of processors is chosen at run-time.

• Proof is constructive: exhibits a scheduler.

Parallelizability ratio determines which factor dominates.

Page 41: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-DFS Schedule

A schedule is a pebbling of the cost graph.

• Given p > 0 pebbles.

• Goal: move a pebble from the start to the end node.

• Move: when all predecessors are pebbled, then pick them upand pebble the successor.

A pebbling strategy is an algorithm for pebbling a cost graph.

• p-DFS: depth-first search, p visits at a time.

• p-BFS: breadth-first search, p visits at a time.

• p-WS: work-stealing schedule.

Page 42: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-DFS Schedule

11

10

5 8 9

743

62

1

Page 43: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-DFS Schedule

11

10

5 8 9

743

62

1

Page 44: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-DFS Schedule

11

10

5 8 9

743

62

1

Page 45: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-DFS Schedule

11

10

5 8 9

743

62

1

Page 46: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-DFS Schedule

11

10

5 8 9

743

62

1

Page 47: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-DFS Schedule

11

10

5 8 9

743

62

1

Page 48: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-DFS Schedule

11

10

5 8 9

743

62

1

Page 49: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-DFS Schedule

11

10

5 8 9

743

62

1

Page 50: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-WS Schedule

11

10

5 8 9

743

62

1

Page 51: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-WS Schedule

11

10

5 8 9

743

62

1

Page 52: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-WS Schedule

11

10

5 8 9

743

62

1

Page 53: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-WS Schedule

11

10

5 8 9

743

62

1

Page 54: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-WS Schedule

11

10

5 8 9

743

62

1

Page 55: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-WS Schedule

11

10

5 8 9

743

62

1

Page 56: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

2-WS Schedule

11

10

5 8 9

743

62

1

Page 57: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Scheduling and Space

Key idea: measure the number of deviations from sequential order.

• Each deviation implies an interaction with the scheduler.

• Deviations incur cache misses.

Thm (Spoonhower): Space for scheduling is proportional tonumber of deviations.

Thm (Spoonhower, et al.): For parallel futures a work-stealingscheduler incurs expected O(p d + t d) deviations on p processorswith t touches.

Page 58: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Visualization of Cost Graphs

Red edges mark live roots at high-water mark.

Page 59: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Introductory CS at CMU

Introductory curriculum emphasizes:

• Parallelism as the general case, sequential being degenerate.

• Verification by rigorous proof.

The best way to achieve this is functional programming.

• 2nd semester: parallel FP, abstraction, verification.

• 3rd semester: parallel data structures and algorithms using FP.

Seewww.cs.cmu.edu/~15150/previous-semesters/2012-spring

andwww.cs.cmu.edu/afs/cs/academic/class/15210-s12/www/.

Page 60: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Fallacies Refuted

It is often alleged that machine models are “realistic”.

• Manual storage allocation.

• Manual scheduling.

• Primary and secondary storage effects.

But research developments have shown

• Automatic storage management is faster and more robust.

• Automatic scheduling is practical and efficient.

Even memory hierarchy effects can be accounted for cleanly andelegantly using cost semantics.

Page 61: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

IO Efficiency

Aggarwal and Vitter introduced the IO Model:

• Distinguish primary from secondary memory.

• Cache size M = k × B words.

• Evaluate algorithm efficiency in terms of M and B.

Main result: k-way merge sort is optimal for the IO model:

O(n/B logM/B(n/B))

(Not cache-oblivious: k is proportional to M/B.)

Page 62: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

IO Efficiency

A&V’s results can be matched in a purely functional model.

• No manual memory management.

• Natural functional programming.

Key idea: temporal locality implies spatial locality.

• Allocation order determines proximity.

• Reloading of migrated objects preserves proximity.

• Control stack specially managed to avoid cache contention.

Page 63: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

Cost semantics makes storage explicit:

σ @ e ⇓n σ′ @ v

Store σ has three components:

• Unbounded main memory with blocks of size B.

• Read cache of size M = k × B.

• Linearly ordered allocation cache of size M.

In-cache operations are zero cost; reads and evictions are unit cost.

Page 64: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

Cost semantics makes storage explicit:

σ @ e ⇓n σ′ @ v

Store σ has three components:

• Unbounded main memory with blocks of size B.

• Read cache of size M = k × B.

• Linearly ordered allocation cache of size M.

In-cache operations are zero cost; reads and evictions are unit cost.

Page 65: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

Cost semantics makes storage explicit:

σ @ e ⇓n σ′ @ v

Store σ has three components:

• Unbounded main memory with blocks of size B.

• Read cache of size M = k × B.

• Linearly ordered allocation cache of size M.

In-cache operations are zero cost; reads and evictions are unit cost.

Page 66: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

Cost semantics makes storage explicit:

σ @ e ⇓n σ′ @ v

Store σ has three components:

• Unbounded main memory with blocks of size B.

• Read cache of size M = k × B.

• Linearly ordered allocation cache of size M.

In-cache operations are zero cost; reads and evictions are unit cost.

Page 67: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

Cost semantics makes storage explicit:

σ @ e ⇓n σ′ @ v

Store σ has three components:

• Unbounded main memory with blocks of size B.

• Read cache of size M = k × B.

• Linearly ordered allocation cache of size M.

In-cache operations are zero cost; reads and evictions are unit cost.

Page 68: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

(Simplified) Cost Semantics

σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1 σ1 @ e1 ⇓

n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e

σ′′1 @ app(l ′1;−) ↑n′′′1

R σ2 @ k2

σ′′1 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2 σ′2 @ [l ′2/x ]e ⇓n

′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Page 69: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

(Simplified) Cost Semantics

σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1 σ1 @ e1 ⇓

n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e σ′′1 @ app(l ′1;−) ↑n

′′′1

R σ2 @ k2

σ′′1 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2 σ′2 @ [l ′2/x ]e ⇓n

′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Page 70: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

(Simplified) Cost Semantics

σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1 σ1 @ e1 ⇓

n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e σ′′1 @ app(l ′1;−) ↑n

′′′1

R σ2 @ k2

σ′′1 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2

σ′2 @ [l ′2/x ]e ⇓n′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Page 71: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

(Simplified) Cost Semantics

σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1 σ1 @ e1 ⇓

n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e σ′′1 @ app(l ′1;−) ↑n

′′′1

R σ2 @ k2

σ′′1 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2 σ′2 @ [l ′2/x ]e ⇓n

′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Page 72: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Provable Implementation for IO

Thm (Blelloch & H) An evaluation of cost n may be implementedon a stack machine with cache of size 4×M + B with cachecomplexity k × n for some small constant k .

• Sleator, et al.: LRU eviction policy is 2-competitive with ICM.

• Appel: cost of copying GC is asymptotically free.

• B&H: Stack management induces small constant overhead.

Thus, the cost semantics is a valid basis for IO analysis.

Page 73: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Provable Implementation for IO

Thm (Blelloch & H) An evaluation of cost n may be implementedon a stack machine with cache of size 4×M + B with cachecomplexity k × n for some small constant k .

• Sleator, et al.: LRU eviction policy is 2-competitive with ICM.

• Appel: cost of copying GC is asymptotically free.

• B&H: Stack management induces small constant overhead.

Thus, the cost semantics is a valid basis for IO analysis.

Page 74: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Provable Implementation for IO

Thm (Blelloch & H) An evaluation of cost n may be implementedon a stack machine with cache of size 4×M + B with cachecomplexity k × n for some small constant k .

• Sleator, et al.: LRU eviction policy is 2-competitive with ICM.

• Appel: cost of copying GC is asymptotically free.

• B&H: Stack management induces small constant overhead.

Thus, the cost semantics is a valid basis for IO analysis.

Page 75: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Provable Implementation for IO

Thm (Blelloch & H) An evaluation of cost n may be implementedon a stack machine with cache of size 4×M + B with cachecomplexity k × n for some small constant k .

• Sleator, et al.: LRU eviction policy is 2-competitive with ICM.

• Appel: cost of copying GC is asymptotically free.

• B&H: Stack management induces small constant overhead.

Thus, the cost semantics is a valid basis for IO analysis.

Page 76: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Provable Implementation for IO

Thm (Blelloch & H) An evaluation of cost n may be implementedon a stack machine with cache of size 4×M + B with cachecomplexity k × n for some small constant k .

• Sleator, et al.: LRU eviction policy is 2-competitive with ICM.

• Appel: cost of copying GC is asymptotically free.

• B&H: Stack management induces small constant overhead.

Thus, the cost semantics is a valid basis for IO analysis.

Page 77: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Merge, Revisited

fun merge nil ys = ys

| merge xs nil = xs

| merge (xs as x::xs’) (ys as y::ys’) =

case compare x y of

LESS ⇒ !a::merge xs’ ys

| GTEQ ⇒ !b::merge xs ys’

Page 78: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Merge, Revisited

fun merge nil ys = ys

| merge xs nil = xs

| merge (xs as x::xs’) (ys as y::ys’) =

case compare x y of

LESS ⇒ !a::merge xs’ ys

| GTEQ ⇒ !b::merge xs ys’

Page 79: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Merge, Revisited

A data structure is compact iff it may be traversed in time O(n/B).

Thm: For compact inputs xs and ys the call merge xs ys hascache complexity O(n/B).

• Recurs down lists allocating only stack n frames: O(n/B).

• Returns allocating n list cells: O(n/B).

Copying operations !a and !b are needed to ensure compactness(locality).

Page 80: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Stack Management

The main complication is accounting for the control stack.

• For map stack space may be amortized against allocation ofthe result.

• But this is not always possible!

Consider non-tail recursive factorial:

fun fact 0 = 1

| fact n = n * fact (n-1)

Without accounting for stack, we would predict O(1) cost, but thetrue cost is O(n/B).

Page 81: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Stack Management

The main complication is accounting for the control stack.

• For map stack space may be amortized against allocation ofthe result.

• But this is not always possible!

Consider non-tail recursive factorial:

fun fact 0 = 1

| fact n = n * fact (n-1)

Without accounting for stack, we would predict O(1) cost, but thetrue cost is O(n/B).

Page 82: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Stack Management

The main complication is accounting for the control stack.

• For map stack space may be amortized against allocation ofthe result.

• But this is not always possible!

Consider non-tail recursive factorial:

fun fact 0 = 1

| fact n = n * fact (n-1)

Without accounting for stack, we would predict O(1) cost, but thetrue cost is O(n/B).

Page 83: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

The cost semantics must be enhanced to allocate frames:

σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1 σ1 @ e1 ⇓n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e σ′′1 @ app(l ′1;−) ↑n

′′′1

R σ2 @ k2

σ2 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2 σ′2 @ [l ′2/x ]e ⇓n

′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Modifications:

• Frames are never read, but just allocated for their effect.

• Root set R records live data in the control stack.

Page 84: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

The cost semantics must be enhanced to allocate frames:σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1

σ1 @ e1 ⇓n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e σ′′1 @ app(l ′1;−) ↑n

′′′1

R σ2 @ k2

σ2 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2 σ′2 @ [l ′2/x ]e ⇓n

′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Modifications:

• Frames are never read, but just allocated for their effect.

• Root set R records live data in the control stack.

Page 85: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

The cost semantics must be enhanced to allocate frames:σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1 σ1 @ e1 ⇓

n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e σ′′1 @ app(l ′1;−) ↑n

′′′1

R σ2 @ k2

σ2 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2 σ′2 @ [l ′2/x ]e ⇓n

′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Modifications:

• Frames are never read, but just allocated for their effect.

• Root set R records live data in the control stack.

Page 86: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

The cost semantics must be enhanced to allocate frames:σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1 σ1 @ e1 ⇓

n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e

σ′′1 @ app(l ′1;−) ↑n′′′1

R σ2 @ k2

σ2 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2 σ′2 @ [l ′2/x ]e ⇓n

′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Modifications:

• Frames are never read, but just allocated for their effect.

• Root set R records live data in the control stack.

Page 87: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

The cost semantics must be enhanced to allocate frames:σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1 σ1 @ e1 ⇓

n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e σ′′1 @ app(l ′1;−) ↑n

′′′1

R σ2 @ k2

σ2 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2 σ′2 @ [l ′2/x ]e ⇓n

′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Modifications:

• Frames are never read, but just allocated for their effect.

• Root set R records live data in the control stack.

Page 88: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

The cost semantics must be enhanced to allocate frames:σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1 σ1 @ e1 ⇓

n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e σ′′1 @ app(l ′1;−) ↑n

′′′1

R σ2 @ k2

σ2 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2

σ′2 @ [l ′2/x ]e ⇓n′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Modifications:

• Frames are never read, but just allocated for their effect.

• Root set R records live data in the control stack.

Page 89: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

The cost semantics must be enhanced to allocate frames:σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1 σ1 @ e1 ⇓

n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e σ′′1 @ app(l ′1;−) ↑n

′′′1

R σ2 @ k2

σ2 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2 σ′2 @ [l ′2/x ]e ⇓n

′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Modifications:

• Frames are never read, but just allocated for their effect.

• Root set R records live data in the control stack.

Page 90: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

The cost semantics must be enhanced to allocate frames:σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1 σ1 @ e1 ⇓

n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e σ′′1 @ app(l ′1;−) ↑n

′′′1

R σ2 @ k2

σ2 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2 σ′2 @ [l ′2/x ]e ⇓n

′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Modifications:

• Frames are never read, but just allocated for their effect.

• Root set R records live data in the control stack.

Page 91: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Cost Semantics for IO

The cost semantics must be enhanced to allocate frames:σ @ app(−; e2) ↑n1R∪locs(e1) σ1 @ k1 σ1 @ e1 ⇓

n′1R∪{k1} σ

′1 @ l ′1

σ′1 @ l ′1 ↓n′′1 σ′′1 @λx .e σ′′1 @ app(l ′1;−) ↑n

′′′1

R σ2 @ k2

σ2 @ e2 ⇓n2R∪{k2} σ′2 @ l ′2 σ′2 @ [l ′2/x ]e ⇓n

′2

R σ′ @ l ′

σ @ app(e1; e2) ⇓n1+n′1+n′′1 +n′′′1 +n2+n′2

R σ′ @ l ′

Modifications:

• Frames are never read, but just allocated for their effect.

• Root set R records live data in the control stack.

Page 92: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Stack Management

Stack frames are allocated in the nursery.

• May exist solely within nursery.

• May migrate to secondary memory.

Dedicate a cache block of B frames in primary memory.

• Not influenced by frames in nursery.

• Specially managed read cache for stack frames.

Page 93: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Stack Management

Stack frames are allocated in the nursery.

• May exist solely within nursery.

• May migrate to secondary memory.

Dedicate a cache block of B frames in primary memory.

• Not influenced by frames in nursery.

• Specially managed read cache for stack frames.

Page 94: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Stack Management

Typical Stack Deep Recursion

(secondary)

(cache) (cache)

(secondary)

(secondary)

(nursery)

(nursery)

Page 95: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Stack Management

Stack cache block may be evicted up to B times.

• Newer frames may overflow nursery.

• Reading evicted frames replaces stack cache.

Amortize cost of eviction over allocation of newer frames.

• Put $3 on each frame block as it is migrated to secondary.

• Use $1 for migration.

• Use $1 for initial load.

• Use $1 for reload of evicted block.

Page 96: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Stack Management

Stack cache block may be evicted up to B times.

• Newer frames may overflow nursery.

• Reading evicted frames replaces stack cache.

Amortize cost of eviction over allocation of newer frames.

• Put $3 on each frame block as it is migrated to secondary.

• Use $1 for migration.

• Use $1 for initial load.

• Use $1 for reload of evicted block.

Page 97: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Summary

Cost semantics supports analysis of complexity of high-level code.

• No need for “pseudo-code”.

• Avoid reasoning about compilation.

Costs can be chosen to reflect different notions of complexity:

• Sequential and parallel time [B & Greiner 96].

• Space effects of scheduling [Spoonhower, B, Gibbons, & H09].

• Memory hierarchy effects [B& H 13].

Page 98: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Summary

Cost semantics supports analysis of complexity of high-level code.

• No need for “pseudo-code”.

• Avoid reasoning about compilation.

Costs can be chosen to reflect different notions of complexity:

• Sequential and parallel time [B & Greiner 96].

• Space effects of scheduling [Spoonhower, B, Gibbons, & H09].

• Memory hierarchy effects [B& H 13].

Page 99: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Summary

Cost semantics supports analysis of complexity of high-level code.

• No need for “pseudo-code”.

• Avoid reasoning about compilation.

Costs can be chosen to reflect different notions of complexity:

• Sequential and parallel time [B & Greiner 96].

• Space effects of scheduling [Spoonhower, B, Gibbons, & H09].

• Memory hierarchy effects [B& H 13].

Page 100: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Summary

Cost semantics supports analysis of complexity of high-level code.

• No need for “pseudo-code”.

• Avoid reasoning about compilation.

Costs can be chosen to reflect different notions of complexity:

• Sequential and parallel time [B & Greiner 96].

• Space effects of scheduling [Spoonhower, B, Gibbons, & H09].

• Memory hierarchy effects [B& H 13].

Page 101: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Summary

Cost semantics supports analysis of complexity of high-level code.

• No need for “pseudo-code”.

• Avoid reasoning about compilation.

Costs can be chosen to reflect different notions of complexity:

• Sequential and parallel time [B & Greiner 96].

• Space effects of scheduling [Spoonhower, B, Gibbons, & H09].

• Memory hierarchy effects [B& H 13].

Page 102: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Summary

Cost semantics supports analysis of complexity of high-level code.

• No need for “pseudo-code”.

• Avoid reasoning about compilation.

Costs can be chosen to reflect different notions of complexity:

• Sequential and parallel time [B & Greiner 96].

• Space effects of scheduling [Spoonhower, B, Gibbons, & H09].

• Memory hierarchy effects [B& H 13].

Page 103: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Summary

λ-calculus provides a logical model of computation.

• Inherently compositional.

• Mathematically elegant.

Cost semantics integrates the combnatorial aspects:

• Enrich the tools available to algorithms designers.

• Extend complexity analysis to mathematically elegantlanguages.

What’s not to like?

Page 104: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Summary

λ-calculus provides a logical model of computation.

• Inherently compositional.

• Mathematically elegant.

Cost semantics integrates the combnatorial aspects:

• Enrich the tools available to algorithms designers.

• Extend complexity analysis to mathematically elegantlanguages.

What’s not to like?

Page 105: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Summary

λ-calculus provides a logical model of computation.

• Inherently compositional.

• Mathematically elegant.

Cost semantics integrates the combnatorial aspects:

• Enrich the tools available to algorithms designers.

• Extend complexity analysis to mathematically elegantlanguages.

What’s not to like?

Page 106: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Summary

λ-calculus provides a logical model of computation.

• Inherently compositional.

• Mathematically elegant.

Cost semantics integrates the combnatorial aspects:

• Enrich the tools available to algorithms designers.

• Extend complexity analysis to mathematically elegantlanguages.

What’s not to like?

Page 107: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Summary

λ-calculus provides a logical model of computation.

• Inherently compositional.

• Mathematically elegant.

Cost semantics integrates the combnatorial aspects:

• Enrich the tools available to algorithms designers.

• Extend complexity analysis to mathematically elegantlanguages.

What’s not to like?

Page 108: Two Notions of Beauty in Programmingrwh/talks/IU13.pdf · Combinatorial: e ciency (machine e ort). Oddly, these are largely disparate communities! Two Sources of Beauty in Programs

Summary

λ-calculus provides a logical model of computation.

• Inherently compositional.

• Mathematically elegant.

Cost semantics integrates the combnatorial aspects:

• Enrich the tools available to algorithms designers.

• Extend complexity analysis to mathematically elegantlanguages.

What’s not to like?