Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  ·...

42
Cost Semantics for Space Usage in a Parallel Language Daniel Spoonhower Carnegie Mellon University (Joint work with Guy Blelloch & Robert Harper) DAMP – 16 Jan 2007

Transcript of Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  ·...

Page 1: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Cost Semantics for Space Usage ina Parallel Language

Daniel Spoonhower

Carnegie Mellon University

(Joint work with Guy Blelloch & Robert Harper)

DAMP – 16 Jan 2007

Page 2: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Understanding How Programs Compute

Interested in intensional behavior of programs

I more than just final result

I e.g. time & space required

State-of-the-art = compile, run, & profile

6 architecture specific (e.g. # cores)

6 dependent on configuration (e.g. scheduler)

6 compilers for functional languages are complex(e.g. closure, CPS conversion)

16 Jan 2007 DAMP ’07 Cost Semantics for Space 2

Page 3: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Understanding How Programs Compute

Interested in intensional behavior of programs

I more than just final result

I e.g. time & space required

State-of-the-art = compile, run, & profile

6 architecture specific (e.g. # cores)

6 dependent on configuration (e.g. scheduler)

6 compilers for functional languages are complex(e.g. closure, CPS conversion)

16 Jan 2007 DAMP ’07 Cost Semantics for Space 2

Page 4: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Understanding How Programs Compute

Interested in intensional behavior of programs

I more than just final result

I e.g. time & space required

State-of-the-art = compile, run, & profile

6 architecture specific (e.g. # cores)

6 dependent on configuration (e.g. scheduler)

6 compilers for functional languages are complex(e.g. closure, CPS conversion)

16 Jan 2007 DAMP ’07 Cost Semantics for Space 2

Page 5: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Motivating Example: Quicksort

Assume fine-grained parallelism

I pairs < e1 || e2 > may evaluate in parallel

I schedule determined by compiler & run-time

fun qsort xs =

case xs of nil => nil

| x::xs =>

append <qsort (filter (le x) xs) ||

x::(qsort (filter (gt x) xs))>

16 Jan 2007 DAMP ’07 Cost Semantics for Space 4

Page 6: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Quicksort: High-Water Mark for Heap

16 Jan 2007 DAMP ’07 Cost Semantics for Space 5

Page 7: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Approach

Talk Outline

Cost Semantics

I define execution costs for high-level language

I account for parallelism & space

Provable Implementation

I make parallelism explicit

I translate to lower-level language

I prove costs are preserved at each step

I consider scheduler, GC implementation

16 Jan 2007 DAMP ’07 Cost Semantics for Space 6

Page 8: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Approach Talk Outline

Cost Semantics

I define execution costs for high-level language

I account for parallelism & space

Provable Implementation

I make parallelism explicit

I translate to lower-level language

I prove costs are preserved at each step

I consider scheduler, GC implementation

16 Jan 2007 DAMP ’07 Cost Semantics for Space 6

Page 9: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Background: Cost Semantics

A cost semantics is a dynamic semantics

I i.e. execution model for high-level language

Yields a cost metric, some abstract measure of cost

I e.g. steps of evaluation, upper bound on space

We will a consider a cost model thataccounts for parallelism and space.

16 Jan 2007 DAMP ’07 Cost Semantics for Space 7

Page 10: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Background: Cost Semantics

A cost semantics is a dynamic semantics

I i.e. execution model for high-level language

Yields a cost metric, some abstract measure of cost

I e.g. steps of evaluation, upper bound on space

We will a consider a cost model thataccounts for parallelism and space.

16 Jan 2007 DAMP ’07 Cost Semantics for Space 7

Page 11: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Source Language

Consider a pure, functional language.

I includes functions, pairs, and booleans

Pair components evaluated in parallel.

I denoted < e1 || e2 >

Values are disjoint from source language.

I values are labeled to make sharing explicite.g. (v1, v2)

`

16 Jan 2007 DAMP ’07 Cost Semantics for Space 8

Page 12: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Parallel Cost Semantics

Cost semantics is a big-step (evaluation) semantics

I yields two graphs: computation and heap

I sequential, unique result per program

e ⇓ v ; g ; h

Expression e evaluates to value v with computationgraph g and heap graph h.

16 Jan 2007 DAMP ’07 Cost Semantics for Space 9

Page 13: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Computation Graphs

Track control dependencies using a DAG withdistinguished start and end nodes.

g = (nstart , nend , E )

1 [n] g1 ⊕ g2 g1 ⊗ g2

16 Jan 2007 DAMP ’07 Cost Semantics for Space 10

Page 14: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Computation Graphs

Track control dependencies using a DAG withdistinguished start and end nodes.

g = (nstart , nend , E )

1 [n] g1 ⊕ g2 g1 ⊗ g2

16 Jan 2007 DAMP ’07 Cost Semantics for Space 10

Page 15: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Heap Graphs

Track heap dependenciesusing a directed graph

h = E

I nodes shared withcorresponding g

I edges run inopposite direction

16 Jan 2007 DAMP ’07 Cost Semantics for Space 11

Page 16: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Heap Graphs

Track heap dependenciesusing a directed graph

h = E

I nodes shared withcorresponding g

I edges run inopposite direction

16 Jan 2007 DAMP ’07 Cost Semantics for Space 11

Page 17: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Using Cost Graphs

Cost graphs are tools for programmers.

I relate execution costs to source code

I later: simulate runtime behavior

Many concrete metrics possible

I considered maximum heap size in example

I impact of GC: measure overhead, latency

However, this reasoning is only valid if theimplementation respects these costs.

16 Jan 2007 DAMP ’07 Cost Semantics for Space 12

Page 18: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Using Cost Graphs

Cost graphs are tools for programmers.

I relate execution costs to source code

I later: simulate runtime behavior

Many concrete metrics possible

I considered maximum heap size in example

I impact of GC: measure overhead, latency

However, this reasoning is only valid if theimplementation respects these costs.

16 Jan 2007 DAMP ’07 Cost Semantics for Space 12

Page 19: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Provable Implementation

Guaranteed to faithfully mirror high-level costs

I “implementation” = lower-level semantics

Costs ⇒ contract for lower-level implementations

I e.g. environment trimming, tail calls

I can guide concrete implementation on hardware

This work: transition semantics defines parallelism

I several (non-)deterministic versions

I can incorporate specific scheduling algorithms

16 Jan 2007 DAMP ’07 Cost Semantics for Space 13

Page 20: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Provable Implementation

Guaranteed to faithfully mirror high-level costs

I “implementation” = lower-level semantics

Costs ⇒ contract for lower-level implementations

I e.g. environment trimming, tail calls

I can guide concrete implementation on hardware

This work: transition semantics defines parallelism

I several (non-)deterministic versions

I can incorporate specific scheduling algorithms

16 Jan 2007 DAMP ’07 Cost Semantics for Space 13

Page 21: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Transition Semantics

Non-deterministic, parallel, small step semantics

I parallel construct for in-progress computations

(expressions) e ::= . . . | let par d in e(declarations) d ::= x = e | d1 and d2

I declarations simulate a call “stack”

I allows unbounded parallelism, e.g.

d1 7−→ d ′1 d2 7−→ d ′2(d1 and d2) 7−→ (d ′1 and d ′2)

16 Jan 2007 DAMP ’07 Cost Semantics for Space 14

Page 22: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Transition Semantics

Non-deterministic, parallel, small step semantics

I parallel construct for in-progress computations

(expressions) e ::= . . . | let par d in e(declarations) d ::= x = e | d1 and d2

I declarations simulate a call “stack”

I allows unbounded parallelism, e.g.

d1 7−→ d ′1 d2 7−→ d ′2(d1 and d2) 7−→ (d ′1 and d ′2)

16 Jan 2007 DAMP ’07 Cost Semantics for Space 14

Page 23: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Schedules

Define a schedule of g as any covering traversalfrom nstart to nend .

I ordering must respect control dependencies

Definition (Schedule)

A schedule of a graph g = (nstart , nend , E ) is asequence of sets of nodes N0, . . . , Nk such thatnstart 6∈ N0, nend ∈ Nk , and for all i ∈ [0, k),

I Ni ⊆ Ni+1, and

I for all n ∈ Ni+1, pred(n) ⊆ Ni .

16 Jan 2007 DAMP ’07 Cost Semantics for Space 15

Page 24: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Schedules

Define a schedule of g as any covering traversalfrom nstart to nend .

I ordering must respect control dependencies

Definition (Schedule)

A schedule of a graph g = (nstart , nend , E ) is asequence of sets of nodes N0, . . . , Nk such thatnstart 6∈ N0, nend ∈ Nk , and for all i ∈ [0, k),

I Ni ⊆ Ni+1, and

I for all n ∈ Ni+1, pred(n) ⊆ Ni .

16 Jan 2007 DAMP ’07 Cost Semantics for Space 15

Page 25: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Theorem

Every schedule corresponds to a sequence ofderivations in the transition semantics.

Theorem

If e ⇓ v ; g ; h then,

N0, . . . , Nk is a schedule of g⇔

there exists a sequence of k transitionse 7−→ . . . 7−→ v such that i ∈ [0, k],

roots(Ni ; h) = labels(ei).

16 Jan 2007 DAMP ’07 Cost Semantics for Space 16

Page 26: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Measuring Space Usage

GC roots determined byheap graph h and schedule

I roots = edges thatcross schedule frontier

Reachable values deter-mined by reachability in h.

16 Jan 2007 DAMP ’07 Cost Semantics for Space 17

Page 27: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Measuring Space Usage (con’t)

Note that edges in h correspond to directdependencies as well as “possible last uses.”

e1 ⇓ false`1; g1; h1 e3 ⇓ v3; g3; h3 (n fresh)

if e1 then e2 else e3 ⇓ v3; 1⊕ g1 ⊕ [n]⊕ 1⊕ g3

h1 ∪ h3 ∪ {(n, `1)} ∪ {(n, `)}`∈labels(e2)

Heap graphs have a “static” character

I necessary to simulate GC decisions

16 Jan 2007 DAMP ’07 Cost Semantics for Space 18

Page 28: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Measuring Space Usage (con’t)

Note that edges in h correspond to directdependencies as well as “possible last uses.”

e1 ⇓ false`1; g1; h1 e3 ⇓ v3; g3; h3 (n fresh)

if e1 then e2 else e3 ⇓ v3; 1⊕ g1 ⊕ [n]⊕ 1⊕ g3

h1 ∪ h3 ∪ {(n, `1)} ∪ {(n, `)}`∈labels(e2)

Heap graphs have a “static” character

I necessary to simulate GC decisions

16 Jan 2007 DAMP ’07 Cost Semantics for Space 18

Page 29: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Measuring Space Usage (con’t)

Note that edges in h correspond to directdependencies as well as “possible last uses.”

e1 ⇓ false`1; g1; h1 e3 ⇓ v3; g3; h3 (n fresh)

if e1 then e2 else e3 ⇓ v3; 1⊕ g1 ⊕ [n]⊕ 1⊕ g3

h1 ∪ h3 ∪ {(n, `1)} ∪ {(n, `)}`∈labels(e2)

Heap graphs have a “static” character

I necessary to simulate GC decisions

16 Jan 2007 DAMP ’07 Cost Semantics for Space 18

Page 30: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Measuring Space Usage (con’t)

Note that edges in h correspond to directdependencies as well as “possible last uses.”

e1 ⇓ false`1; g1; h1 e3 ⇓ v3; g3; h3 (n fresh)

if e1 then e2 else e3 ⇓ v3; 1⊕ g1 ⊕ [n]⊕ 1⊕ g3

h1 ∪ h3 ∪ {(n, `1)} ∪ {(n, `)}`∈labels(e2)

Heap graphs have a “static” character

I necessary to simulate GC decisions

16 Jan 2007 DAMP ’07 Cost Semantics for Space 18

Page 31: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Measuring Space Usage (con’t)

Note that edges in h correspond to directdependencies as well as “possible last uses.”

e1 ⇓ false`1; g1; h1 e3 ⇓ v3; g3; h3 (n fresh)

if e1 then e2 else e3 ⇓ v3; 1⊕ g1 ⊕ [n]⊕ 1⊕ g3

h1 ∪ h3 ∪ {(n, `1)} ∪ {(n, `)}`∈labels(e2)

Heap graphs have a “static” character

I necessary to simulate GC decisions

16 Jan 2007 DAMP ’07 Cost Semantics for Space 18

Page 32: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Measuring Space Usage (con’t)

Note that edges in h correspond to directdependencies as well as “possible last uses.”

e1 ⇓ false`1; g1; h1 e3 ⇓ v3; g3; h3 (n fresh)

if e1 then e2 else e3 ⇓ v3; 1⊕ g1 ⊕ [n]⊕ 1⊕ g3

h1 ∪ h3 ∪ {(n, `1)} ∪ {(n, `)}`∈labels(e2)

Heap graphs have a “static” character

I necessary to simulate GC decisions

16 Jan 2007 DAMP ’07 Cost Semantics for Space 18

Page 33: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Scheduling Algorithms

Transition semantics (above) allowed all possibleparallel executions.

Given finite processors, which sub-expressionsshould be evaluated?

E.g. depth- and breadth-first & work stealing

I DF and BF traversals of cost graph g

Formalized as deterministic transition semantics

I abstract presentation: no queues, &c.

16 Jan 2007 DAMP ’07 Cost Semantics for Space 19

Page 34: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Scheduling Algorithms

Transition semantics (above) allowed all possibleparallel executions.

Given finite processors, which sub-expressionsshould be evaluated?

E.g. depth- and breadth-first & work stealing

I DF and BF traversals of cost graph g

Formalized as deterministic transition semantics

I abstract presentation: no queues, &c.

16 Jan 2007 DAMP ’07 Cost Semantics for Space 19

Page 35: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Quicksort: Revisited

append <qsort (filter (le x) xs) ||

x::(qsort (filter (gt x) xs))>

16 Jan 2007 DAMP ’07 Cost Semantics for Space 21

Page 36: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Quicksort: Revisited

append <qsort (filter (le x) xs) ||

x::(qsort (filter (gt x) xs))>

16 Jan 2007 DAMP ’07 Cost Semantics for Space 23

Page 37: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Quicksort: Revisited

let val (ls, gs) = <filter (le x) xs ||

filter (gt x) xs>

in

append <qsort ls || x::(qsort gs)>

end

16 Jan 2007 DAMP ’07 Cost Semantics for Space 25

Page 38: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Quicksort: Revisited

let val (ls, gs) = <filter (le x) xs ||

filter (gt x) xs>

in

append <qsort ls || x::(qsort gs)>

end

16 Jan 2007 DAMP ’07 Cost Semantics for Space 25

Page 39: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Quicksort: Revisited

let val (ls, gs) = <filter (le x) xs ||

filter (gt x) xs>

in

append <qsort ls || x::(qsort gs)>

end

(via inlining)

append <qsort (filter (le x) xs) ||

x::(qsort (filter (gt x) xs))>

16 Jan 2007 DAMP ’07 Cost Semantics for Space 27

Page 40: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Related Work

Greiner & Blelloch measure time and space together[ICFP ’96, TOPLAS ’99]

I upper bounds based on size and depth of DAG

Minamide shows CPS conversion preserves spaceusage [HOOTS ’99]

I constant overhead independent of program

Gustavsson & Sands give laws for reasoning aboutprogram transformations in Haskell [HOOTS ’99]

I formalize “safe for space” as cost semantics

16 Jan 2007 DAMP ’07 Cost Semantics for Space 28

Page 41: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Future Work

Empirical evaluation

I full-scale implementation, predict & measureperformance (different GCs, schedulers)

I killer app?

Language extensions

I static discipline to help control (or at leastmake explicit) performance costs

I e.g. distinguish implementations of quicksort

16 Jan 2007 DAMP ’07 Cost Semantics for Space 29

Page 42: Cost Semantics for Space Usage in a Parallel Language./spoons/talks/damp07-slides.pdf ·  · 2007-03-12Cost Semantics for Space Usage in a Parallel Language ... A cost semantics

Summary

Functional programming:

I traditionally, easy to reason about result

I . . . but hard to reason about performance

In this work, we have

I related parallelism & space usage to source

I proved costs preserved by implementation

I considered effects of scheduler, collector

Ongoing: reason about performance in parallel ML

16 Jan 2007 DAMP ’07 Cost Semantics for Space 30