Reflective Parallel Programming Nicholas D. Matsakis, Thomas R. - - PowerPoint PPT Presentation

reflective parallel programming
SMART_READER_LITE
LIVE PREVIEW

Reflective Parallel Programming Nicholas D. Matsakis, Thomas R. - - PowerPoint PPT Presentation

Reflective Parallel Programming Nicholas D. Matsakis, Thomas R. Gross ETH Zurich 1 Friday, June 18, 2010 Reflective Parallelism Reflection : Ability for a program to reason about its own structure Reflective Parallelism : Ability for


slide-1
SLIDE 1

Reflective Parallel Programming

Nicholas D. Matsakis, Thomas R. Gross ETH Zurich

1

Friday, June 18, 2010

slide-2
SLIDE 2

Reflective Parallelism

  • Reflection: Ability for a program to reason

about its own structure

  • Reflective Parallelism: Ability for a program to

reason about its own schedule.

  • Schedule: the (partial) order in which

parallel tasks execute.

2

Friday, June 18, 2010

slide-3
SLIDE 3

Reflection Example

3

A B

Friday, June 18, 2010

slide-4
SLIDE 4

Reflection Example

3

A B

Parallel Tasks or Threads

Friday, June 18, 2010

slide-5
SLIDE 5

Reflection Example

3

A B

Unordered tasks?

Parallel Tasks or Threads

Friday, June 18, 2010

slide-6
SLIDE 6

Reflection Example

4

A B

A must end before B starts?

Friday, June 18, 2010

slide-7
SLIDE 7

Reflective Parallelism

5

  • Reflective queries should

return results that hold for all executions

  • Reflection also allows

interaction

  • Add scheduling

constraints, etc. A B

Friday, June 18, 2010

slide-8
SLIDE 8

Static Evaluation

  • When possible, should be able to analyze

schedule statically.

  • Only partial schedule known at compile

time.

6

Friday, June 18, 2010

slide-9
SLIDE 9

Applications

  • Data-race detection
  • Schedule visualization
  • Testing frameworks
  • ...and more

7

Friday, June 18, 2010

slide-10
SLIDE 10

Outline

  • What is reflective parallel programming?
  • Why do we need a new model?
  • Intervals model
  • Example: Data-race detection

8

Friday, June 18, 2010

slide-11
SLIDE 11

Outline

  • What is reflective parallel programming?
  • Why do we need a new model?
  • Intervals model
  • Example: Data-race detection

8

Friday, June 18, 2010

slide-12
SLIDE 12

Traditional Threading

  • Traditional APIs use operational primitives:
  • start, join a thread
  • wait for a signal, acquire a lock
  • Program schedule not defined in advance
  • Can only query after execution!

9

Friday, June 18, 2010

slide-13
SLIDE 13

Thread[] threads = new Thread[N]; for(int i = 0; i < N; i++) { threads[i] = new Thread(...); threads[i].start(); } for(int i = 0; i < N; i++) threads[i].join();

Difficult to Analyze Statically

10

Friday, June 18, 2010

slide-14
SLIDE 14

Thread[] threads = new Thread[N]; for(int i = 0; i < N; i++) { threads[i] = new Thread(...); threads[i].start(); } for(int i = 0; i < N; i++) threads[i].join();

Difficult to Analyze Statically

10

Friday, June 18, 2010

slide-15
SLIDE 15

Thread[] threads = new Thread[N]; for(int i = 0; i < N; i++) { threads[i] = new Thread(...); threads[i].start(); } for(int i = 0; i < N; i++) threads[i].join();

Difficult to Analyze Statically

10

Friday, June 18, 2010

slide-16
SLIDE 16

Thread[] threads = new Thread[N]; for(int i = 0; i < N; i++) { threads[i] = new Thread(...); threads[i].start(); } for(int i = 0; i < N; i++) threads[i].join();

Difficult to Analyze Statically

10

Friday, June 18, 2010

slide-17
SLIDE 17

Thread[] threads = new Thread[N]; for(int i = 0; i < N; i++) { threads[i] = new Thread(...); threads[i].start(); } for(int i = 0; i < N; i++) threads[i].join();

Difficult to Analyze Statically

10

Friday, June 18, 2010

slide-18
SLIDE 18

Thread[] threads = new Thread[N]; for(int i = 0; i < N; i++) { threads[i] = new Thread(...); threads[i].start(); } for(int i = 0; i < N; i++) threads[i].join();

Difficult to Analyze Statically

10

Friday, June 18, 2010

slide-19
SLIDE 19

Thread[] threads = new Thread[N]; for(int i = 0; i < N; i++) { threads[i] = new Thread(...); threads[i].start(); } for(int i = 0; i < N; i++) threads[i].join();

Difficult to Analyze Statically

10

Friday, June 18, 2010

slide-20
SLIDE 20

Thread[] threads = new Thread[N]; for(int i = 0; i < N; i++) { threads[i] = new Thread(...); threads[i].start(); } for(int i = 0; i < N; i++) threads[i].join();

Difficult to Analyze Statically

10

Have all threads been joined?

Friday, June 18, 2010

slide-21
SLIDE 21

Reverse Engineering is Risky

11

Write buffer Lock / Unlock Lock / Unlock Read buffer

Friday, June 18, 2010

slide-22
SLIDE 22

Reverse Engineering is Risky

11

Write buffer Lock / Unlock Lock / Unlock Read buffer

Friday, June 18, 2010

slide-23
SLIDE 23

Reverse Engineering is Risky

11

Write buffer Lock / Unlock Lock / Unlock Read buffer

Observed: Wr happened before Rd

Friday, June 18, 2010

slide-24
SLIDE 24

Reverse Engineering is Risky

11

Write buffer Lock / Unlock Lock / Unlock Read buffer

Observed: Wr happened before Rd Conclusion: Wr happens before Rd?

Friday, June 18, 2010

slide-25
SLIDE 25

Reverse Engineering is Risky

11

Write buffer Lock / Unlock Lock / Unlock Read buffer

Observed: Wr happened before Rd Conclusion: Wr happens before Rd? Past performance is no guarantee of future results.

Friday, June 18, 2010

slide-26
SLIDE 26

Summary

12

  • Traditional model unsuitable for reflection
  • Cannot know schedule in advance
  • Difficult to analyze statically
  • Can draw false conclusions

Friday, June 18, 2010

slide-27
SLIDE 27

Outline

  • What is reflective parallel programming?
  • Why do we need a new model?
  • Intervals model
  • Example: Data-race detection

13

Friday, June 18, 2010

slide-28
SLIDE 28

Outline

  • What is reflective parallel programming?
  • Why do we need a new model?
  • Intervals model
  • Example: Data-race detection

13

Friday, June 18, 2010

slide-29
SLIDE 29

Intervals Approach

  • Schedule is a first-class entity
  • Users builds desired schedule through

declarative methods

  • Runtime executes simultaneously
  • Schedule can be queried during execution

14

Friday, June 18, 2010

slide-30
SLIDE 30

Schedule Model

15

Intervals represent asynchronous tasks

  • r group of tasks.

Friday, June 18, 2010

slide-31
SLIDE 31

Schedule Model

15

Intervals represent asynchronous tasks

  • r group of tasks.

Interval a = interval { ... };

Friday, June 18, 2010

slide-32
SLIDE 32

Schedule Model

15

Intervals represent asynchronous tasks

  • r group of tasks.

Interval a = interval { ... };

Friday, June 18, 2010

slide-33
SLIDE 33

Schedule Model

15

Intervals represent asynchronous tasks

  • r group of tasks.

Interval a = interval { ... };

Friday, June 18, 2010

slide-34
SLIDE 34

Schedule Model

15

Intervals represent asynchronous tasks

  • r group of tasks.

Interval a = interval { ... };

Friday, June 18, 2010

slide-35
SLIDE 35

Schedule Model

16

Start Point Points represent the moments in time when the interval begins or ends execution. End Point

Friday, June 18, 2010

slide-36
SLIDE 36

Schedule Model

17

Happens-Before Edges partially order points. a.end.addHb(b.start);

a b

Friday, June 18, 2010

slide-37
SLIDE 37

Schedule Model

17

Happens-Before Edges partially order points. a.end.addHb(b.start);

a b

Friday, June 18, 2010

slide-38
SLIDE 38

Schedule Model

17

Happens-Before Edges partially order points. a.end.addHb(b.start);

a b

Friday, June 18, 2010

slide-39
SLIDE 39

Schedule Model

18

End → Start Start → Start Start → End

Friday, June 18, 2010

slide-40
SLIDE 40

Schedule Model

18

End → Start Start → Start Start → End

Friday, June 18, 2010

slide-41
SLIDE 41

Schedule Model

18

End → Start Start → Start Start → End

Friday, June 18, 2010

slide-42
SLIDE 42

Schedule Model

18

End → Start Start → Start Start → End

Friday, June 18, 2010

slide-43
SLIDE 43

Schedule Model

19

Lock lock = context.newLock(); Locks allow intervals to be sequential but unordered.

Friday, June 18, 2010

slide-44
SLIDE 44

Schedule Model

20

Intervals may hold lock(s) for their duration. a.addLock(theLock); b.addLock(theLock);

b (theLock) a (theLock)

Friday, June 18, 2010

slide-45
SLIDE 45

Schedule Model

20

Intervals may hold lock(s) for their duration. a.addLock(theLock); b.addLock(theLock);

b (theLock) a (theLock)

Friday, June 18, 2010

slide-46
SLIDE 46

Schedule Model

20

Intervals may hold lock(s) for their duration. a.addLock(theLock); b.addLock(theLock);

b (theLock) a (theLock)

Friday, June 18, 2010

slide-47
SLIDE 47

Schedule Model

20

Intervals may hold lock(s) for their duration. a.addLock(theLock); b.addLock(theLock);

b (theLock) a (theLock)

Friday, June 18, 2010

slide-48
SLIDE 48

Schedule Model

21

Interval inter = ...; // add edges, locks inter.ready(); Invoked by creator of inter when initial dependencies have been added.

Friday, June 18, 2010

slide-49
SLIDE 49

Schedule Model

21

Interval inter = ...; // add edges, locks inter.ready(); Invoked by creator of inter when initial dependencies have been added.

Friday, June 18, 2010

slide-50
SLIDE 50

Schedule Model

21

Interval inter = ...; // add edges, locks inter.ready(); Invoked by creator of inter when initial dependencies have been added.

Friday, June 18, 2010

slide-51
SLIDE 51

Summary

22

  • Schedule Model
  • Intervals represent tasks
  • Points represents moments in time
  • Happens-before edges order points
  • Locks permit mutual exclusion of tasks

Friday, June 18, 2010

slide-52
SLIDE 52

Querying the Schedule

23

a (lock) b a.end.hb(b.start)? a.locks(lock)? b.end.hb(a.start)? b.locks(lock)?

Friday, June 18, 2010

slide-53
SLIDE 53

Querying the Schedule

23

a (lock) b a.end.hb(b.start)? a.locks(lock)? b.end.hb(a.start)? b.locks(lock)?

?

Friday, June 18, 2010

slide-54
SLIDE 54

Querying the Schedule

23

a (lock) b a.end.hb(b.start)? a.locks(lock)? b.end.hb(a.start)? b.locks(lock)?

?

Friday, June 18, 2010

slide-55
SLIDE 55

Querying the Schedule

23

a (lock) b a.end.hb(b.start)? true a.locks(lock)? b.end.hb(a.start)? b.locks(lock)?

?

Friday, June 18, 2010

slide-56
SLIDE 56

Querying the Schedule

23

a (lock) b a.end.hb(b.start)? true a.locks(lock)? b.end.hb(a.start)? b.locks(lock)?

Friday, June 18, 2010

slide-57
SLIDE 57

Querying the Schedule

23

a (lock) b a.end.hb(b.start)? true a.locks(lock)? b.end.hb(a.start)? b.locks(lock)?

Friday, June 18, 2010

slide-58
SLIDE 58

Querying the Schedule

23

a (lock) b a.end.hb(b.start)? true a.locks(lock)? b.end.hb(a.start)? b.locks(lock)? true

Friday, June 18, 2010

slide-59
SLIDE 59

Querying the Schedule

23

a (lock) b a.end.hb(b.start)? true a.locks(lock)? b.end.hb(a.start)? b.locks(lock)? true

?

Friday, June 18, 2010

slide-60
SLIDE 60

Querying the Schedule

23

a (lock) b a.end.hb(b.start)? true a.locks(lock)? b.end.hb(a.start)? b.locks(lock)? true false

?

Friday, June 18, 2010

slide-61
SLIDE 61

Querying the Schedule

23

a (lock) b a.end.hb(b.start)? true a.locks(lock)? b.end.hb(a.start)? b.locks(lock)? true false

Friday, June 18, 2010

slide-62
SLIDE 62

Querying the Schedule

23

a (lock) b a.end.hb(b.start)? true a.locks(lock)? b.end.hb(a.start)? b.locks(lock)? true false false

Friday, June 18, 2010

slide-63
SLIDE 63

Monotonicity

24

  • Edges and locks can only be added, not

removed

  • Necessary for static analysis:
  • Compiler knows that edges and locks it

sees cannot be removed at runtime

Friday, June 18, 2010

slide-64
SLIDE 64

Outline

  • What is reflective parallel programming?
  • Why do we need a new model?
  • Intervals model
  • Example: Data-race detection

25

Friday, June 18, 2010

slide-65
SLIDE 65

Outline

  • What is reflective parallel programming?
  • Why do we need a new model?
  • Intervals model
  • Example: Data-race detection

25

Friday, June 18, 2010

slide-66
SLIDE 66

Data Race Detection

  • Key Idea:
  • User defines conditions in which a field

can be accessed

  • Use the reflective API to determine

whether conditions are met

26

Friday, June 18, 2010

slide-67
SLIDE 67

Locked Fields

27

class TheClass { final Lock theLock; @GuardedBy(theLock) String theString; ... }

Friday, June 18, 2010

slide-68
SLIDE 68

Locked Fields

27

class TheClass { final Lock theLock; @GuardedBy(theLock) String theString; ... }

Friday, June 18, 2010

slide-69
SLIDE 69

Locked Fields

27

class TheClass { final Lock theLock; @GuardedBy(theLock) String theString; ... }

Friday, June 18, 2010

slide-70
SLIDE 70

Locked Fields

27

class TheClass { final Lock theLock; @GuardedBy(theLock) String theString; ... }

Friday, June 18, 2010

slide-71
SLIDE 71

Locked Fields

27

class TheClass { final Lock theLock; @GuardedBy(theLock) String theString; ... }

Friday, June 18, 2010

slide-72
SLIDE 72

Dynamic Checking

28

void setString() { assert current.locks(theLock); theString = "..."; }

Friday, June 18, 2010

slide-73
SLIDE 73

Dynamic Checking

28

void setString() { assert current.locks(theLock); theString = "..."; }

Friday, June 18, 2010

slide-74
SLIDE 74

Dynamic Checking

28

void setString() { assert current.locks(theLock); theString = "..."; }

Friday, June 18, 2010

slide-75
SLIDE 75

Dynamic Checking

28

void setString() { assert current.locks(theLock); theString = "..."; }

Friday, June 18, 2010

slide-76
SLIDE 76

Static Checking

29

void staticCheck() { Interval x = interval { assert current.locks(theLock); theString = "..."; } x.addLock(theLock); x.ready(); }

Friday, June 18, 2010

slide-77
SLIDE 77

Static Checking

29

void staticCheck() { Interval x = interval { assert current.locks(theLock); theString = "..."; } x.addLock(theLock); x.ready(); }

Friday, June 18, 2010

slide-78
SLIDE 78

Static Checking

29

void staticCheck() { Interval x = interval { assert current.locks(theLock); theString = "..."; } x.addLock(theLock); x.ready(); }

Friday, June 18, 2010

slide-79
SLIDE 79

Static Checking

29

void staticCheck() { Interval x = interval { assert current.locks(theLock); theString = "..."; } x.addLock(theLock); x.ready(); }

Friday, June 18, 2010

slide-80
SLIDE 80

Static Checking

29

void staticCheck() { Interval x = interval { assert current.locks(theLock); theString = "..."; } x.addLock(theLock); x.ready(); }

x

Friday, June 18, 2010

slide-81
SLIDE 81

Static Checking

29

void staticCheck() { Interval x = interval { assert current.locks(theLock); theString = "..."; } x.addLock(theLock); x.ready(); }

x

Friday, June 18, 2010

slide-82
SLIDE 82

Static Checking

29

void staticCheck() { Interval x = interval { assert current.locks(theLock); theString = "..."; } x.addLock(theLock); x.ready(); }

x (theLock)

Friday, June 18, 2010

slide-83
SLIDE 83

Static Checking

29

void staticCheck() { Interval x = interval { assert current.locks(theLock); theString = "..."; } x.addLock(theLock); x.ready(); }

x (theLock)

Friday, June 18, 2010

slide-84
SLIDE 84

Static Checking

29

void staticCheck() { Interval x = interval { assert current.locks(theLock); theString = "..."; } x.addLock(theLock); x.ready(); }

x (theLock)

Friday, June 18, 2010

slide-85
SLIDE 85

Static Checking

29

void staticCheck() { Interval x = interval { assert current.locks(theLock); theString = "..."; } x.addLock(theLock); x.ready(); }

x (theLock)

Friday, June 18, 2010

slide-86
SLIDE 86

Guard Objects

  • Our compiler automatically enforces these

kind of checks using guard objects

  • Guard object defines methods that check

each read and write for validity

  • When possible, checks are performed

statically

30

Friday, June 18, 2010

slide-87
SLIDE 87

Guard Object Annotations

31

class TheClass { final Lock theLock; @GuardedBy(theLock) String theString; ... }

Friday, June 18, 2010

slide-88
SLIDE 88

Guard Object Annotations

31

class TheClass { final Lock theLock; @GuardedBy(theLock) String theString; ... }

Friday, June 18, 2010

slide-89
SLIDE 89

Guard Object Annotations

31

class TheClass { final Lock theLock; @GuardedBy(theLock) String theString; ... }

Friday, June 18, 2010

slide-90
SLIDE 90

Custom Guards

32

  • Example Conditions
  • Written only by one interval
  • Dynamic monitoring
  • Lock only on writes, not reads
  • Select lock dynamically

Friday, June 18, 2010

slide-91
SLIDE 91

Summary

  • User defines conditions to access a field by

writing code against the reflective API

  • Compiler runs checks statically if possible
  • Runtime can run checks with live schedule

33

Friday, June 18, 2010

slide-92
SLIDE 92

Related Work

  • Smalltalk
  • Reflective objects for stack frames, etc
  • Debuggers and other tools require no

special support from VM

  • Traditional threading model
  • More in the paper

34

Friday, June 18, 2010

slide-93
SLIDE 93

Conclusion

  • Reflective parallelism empowers users:
  • Custom tools for safety checking and

monitoring

  • Reflective parallelism as foundation for

static analysis:

  • Seamless integration of static and

dynamic checks

35

Friday, June 18, 2010

slide-94
SLIDE 94

Thank You

  • Intervals library is available for download:
  • http://intervals.inf.ethz.ch

36

Friday, June 18, 2010

slide-95
SLIDE 95

Spare Slides

37

Friday, June 18, 2010

slide-96
SLIDE 96

Schedule Model

38

Hierarchical Structure

Friday, June 18, 2010

slide-97
SLIDE 97

Illegal Additions

39

  • Schedule is being built and executed

simultaneously

  • Certain additions are illegal

Friday, June 18, 2010

slide-98
SLIDE 98

Adding Edges

40

void method( Interval a, Interval b) { a.end.addHb(b.start); } a b

Friday, June 18, 2010

slide-99
SLIDE 99

Adding Edges

40

void method( Interval a, Interval b) { a.end.addHb(b.start); } a b

What if b had already begun execution?

Friday, June 18, 2010

slide-100
SLIDE 100

Adding Edges

41

void method( Interval a, Interval b) { a.end.addHb(b.start); } a b

If method is part of c, b cannot have started.

c

Friday, June 18, 2010

slide-101
SLIDE 101

Adding Edges

41

void method( Interval a, Interval b) { a.end.addHb(b.start); } a b

If method is part of c, b cannot have started.

c

Friday, June 18, 2010

slide-102
SLIDE 102

// Signal this thread is done sync[id]++; // Wait for neighbors; while(sync[id-1] < sync[id]) ; while(sync[id+1] < sync[id]) ;

Point to Point

42

Friday, June 18, 2010

slide-103
SLIDE 103

// Signal this thread is done sync[id]++; // Wait for neighbors; while(sync[id-1] < sync[id]) ; while(sync[id+1] < sync[id]) ;

Point to Point

42

Friday, June 18, 2010

slide-104
SLIDE 104

// Signal this thread is done sync[id]++; // Wait for neighbors; while(sync[id-1] < sync[id]) ; while(sync[id+1] < sync[id]) ;

Point to Point

42

Friday, June 18, 2010

slide-105
SLIDE 105

// Signal this thread is done sync[id]++; // Wait for neighbors; while(sync[id-1] < sync[id]) ; while(sync[id+1] < sync[id]) ;

Point to Point

42

Friday, June 18, 2010

slide-106
SLIDE 106

// Signal this thread is done sync[id]++; // Wait for neighbors; while(sync[id-1] < sync[id]) ; while(sync[id+1] < sync[id]) ;

Point to Point

42

Friday, June 18, 2010

slide-107
SLIDE 107

// Signal this thread is done sync[id]++; // Wait for neighbors; while(sync[id-1] < sync[id]) ; while(sync[id+1] < sync[id]) ;

Point to Point

42

Complex patterns are even worse.

Friday, June 18, 2010

slide-108
SLIDE 108

Quake Lock

43

  • Game map

represented as tree

  • Lock depends on

location in volume tree

Friday, June 18, 2010

slide-109
SLIDE 109

Quake Lock

43

  • Game map

represented as tree

  • Lock depends on

location in volume tree

Friday, June 18, 2010

slide-110
SLIDE 110

Quake Lock

43

  • Game map

represented as tree

  • Lock depends on

location in volume tree

Friday, June 18, 2010

slide-111
SLIDE 111

Quake Lock

43

  • Game map

represented as tree

  • Lock depends on

location in volume tree

Friday, June 18, 2010

slide-112
SLIDE 112

Mirrors

44

  • Mirrors allow many implementations
  • Compile-time approximations / previews
  • Programs on a different machine

Friday, June 18, 2010

slide-113
SLIDE 113

Guard Interface

45

class Guard { void checkRead(Interval inter); void checkWrite(Interval inter); }

Friday, June 18, 2010

slide-114
SLIDE 114

Guard Interface

45

class Guard { void checkRead(Interval inter); void checkWrite(Interval inter); }

Friday, June 18, 2010

slide-115
SLIDE 115

Guard Interface

45

class Guard { void checkRead(Interval inter); void checkWrite(Interval inter); }

Friday, June 18, 2010

slide-116
SLIDE 116

Reflective Parallelism with Intervals

  • Query and manipulate program

schedule both statically and during execution

  • “Roll your own” data-race

detectors and other tools

46

Friday, June 18, 2010

slide-117
SLIDE 117

Reflective Parallelism with Intervals

  • Query and manipulate program

schedule both statically and during execution

  • “Roll your own” data-race

detectors and other tools

46

Friday, June 18, 2010

slide-118
SLIDE 118

Reflective Parallelism with Intervals

  • Query and manipulate program

schedule both statically and during execution

  • “Roll your own” data-race

detectors and other tools

46

Friday, June 18, 2010