+ All Categories
Home > Documents > Concurrency Runtime Finishing Erlang

Concurrency Runtime Finishing Erlang

Date post: 23-Feb-2016
Category:
Upload: darcie
View: 57 times
Download: 4 times
Share this document with a friend
Description:
Christian Schulte [email protected] Software and Computer Systems School of Information and Communication Technology KTH – Royal Institute of Technology Stockholm, Sweden. Concurrency Runtime Finishing Erlang. ID1218 Lecture 06 2009-11-16. Overview. Concurrent MiniErlang - PowerPoint PPT Presentation
Popular Tags:
97
CONCURRENCY RUNTIME FINISHING ERLANG ID1218 Lecture 06 2009-11-16 Christian Schulte [email protected] Software and Computer Systems School of Information and Communication Technology KTH – Royal Institute of Technology Stockholm, Sweden
Transcript
Page 1: Concurrency Runtime Finishing Erlang

CONCURRENCYRUNTIMEFINISHING ERLANG

ID1218 Lecture 06 2009-11-16

Christian [email protected]

Software and Computer SystemsSchool of Information and Communication

TechnologyKTH – Royal Institute of Technology

Stockholm, Sweden

Page 2: Concurrency Runtime Finishing Erlang

Overview Concurrent MiniErlang

sketch how it really computes builds on MiniErlang

Analyzing runtime asymptotic complexity, big-O notation analyzing recursive functions

Wrapping up the Erlang part will return later as point for comparison

L06, 2009-11-16ID1218, Christian Schulte

2

Page 3: Concurrency Runtime Finishing Erlang

Processes With State

L06, 2009-11-16

3

ID1218, Christian Schulte

Page 4: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

4

Processes Receive messages

To be useful they need to maintain state changing over time

Model: process is modeled by functionstate message state

Page 5: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

5

Process States

s0 s1 s2 …a a a

receivemessages

Page 6: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

6

Defining a Process With State Process

how it reacts to messages how it transforms the state from which initial state it starts

Additionally process might compute by sending messages

to other processes

Page 7: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

7

Example: Logic of a Cell Process

cell({assign,New},S) -> New;cell({access,PID},S) -> PID ! {self(),S}, S.

Captures how the process transforms state how it acts to incoming messages

Page 8: Concurrency Runtime Finishing Erlang

A Generic Process With Stateserve(F,InS) -> receive Msg -> OutS = F(Msg,InS), serve(F,OutS) end.cellserver() -> spawn(fun () -> serve(fun cell/2,0) end). Can be started

with function that performs state transitions initial value for state

L06, 2009-11-16ID1218, Christian Schulte

8

Page 9: Concurrency Runtime Finishing Erlang

Making The Server Smarterserve(F,InS) -> receive kill -> void; … Msg -> OutS = F(Msg,InS), serve(F,OutS) end. Add generic functionality to server

L06, 2009-11-16ID1218, Christian Schulte

9

Page 10: Concurrency Runtime Finishing Erlang

A Reconfigurable Server Behavior of server defined by

generic message receipt specific state transformation plus

communication Idea: make server reconfigurable

change how state is transformed adapt state to possibly new representation

L06, 2009-11-16ID1218, Christian Schulte

10

Page 11: Concurrency Runtime Finishing Erlang

A Reconfigurable Serverserve(F,InS) -> receive kill -> void; {update,NewF,Adapt} -> serve(NewF,Adapt(InS)); … Msg -> OutS = F(Msg,InS), serve(F,OutS) end.

L06, 2009-11-16ID1218, Christian Schulte

11

Page 12: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

12

Buffered Cellsbcell({assign,V},{_,B}) -> {V,B};bcell({access,PID},{V,B}) -> PID ! {self(),V}, {V,B};bcell({save},{V,_}) -> {V,V};bcell({restore},{_,B}) -> {B,B}.

Features states {V,B} with value V and buffer B

Page 13: Concurrency Runtime Finishing Erlang

Reconfiguring the Server> C=cellserver().…> C ! {assign,4}.…> C ! {update, fun bcell/2, fun (Old) -> {Old,0} end}.…> C ! {save}. …

L06, 2009-11-16ID1218, Christian Schulte

13

Page 14: Concurrency Runtime Finishing Erlang

Coordination

L06, 2009-11-16

14

ID1218, Christian Schulte

Page 15: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

15

Protocols Protocol: rules for sending and receiving

messages programming with processes

Examples broadcasting messages to group of processes choosing a process

Important properties of protocols safety liveness

Page 16: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

16

Choosing a Process Example: choosing the best lift, connection, … More general: seeking agreement

coordinate execution

General idea: Master

send message to all slaves containing reply PID select one slave: accept all other slaves: reject

Slaves answer by replying to master PID wait for decision from master

Page 17: Concurrency Runtime Finishing Erlang

Master: Blueprintdecide(SPs) -> Rs=collect(SPs,propose), {SP,SPr}=choose(SPs,Rs), SP ! {self(),accept}, broadcast(SPr,reject}. Generic:

collecting and broadcasting Specific:

choose single process from processes based on replies Rs

L06, 2009-11-16ID1218, Christian Schulte

17

Page 18: Concurrency Runtime Finishing Erlang

Slave: Blueprintslave() -> receive {M,propose} -> R=…, M ! {self(),R}, receive {M,accept} -> …; {M,reject} -> … end end

L06, 2009-11-16ID1218, Christian Schulte

18

Page 19: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

19

Avoiding Deadlock Master can only proceed, after all slaves

answered will not process any more messages until then receiving messages in collect

Slave can only proceed, after master answered

will not process any more messages until then What happens if multiple masters for

same slaves?

Page 20: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

20

Deadlock

M

N

B

A

C

D

Master Slave

Page 21: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

21

Deadlock

M

N

B

A

C

D

Master Slave

Page 22: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

22

Deadlock

M

N

B

A

C

D

Master Slave

Page 23: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

23

Deadlock

M

N

B

A

C

D

Master Slave

blocks!

Page 24: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

24

Deadlock

M

N

B

A

C

D

Master Slave

blocks!

blocks!

Page 25: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

25

Deadlock

M

N

B

A

C

D

Master Slave

blocks!

blocks!

M blocks A, waits for B N blocks B, waits for A Deadlock!!!!

Page 26: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

26

Avoiding Deadlock Force all masters to send in order:

First A, then B, then C, … Guarantee: If A available, all others will be available difficult: what if dynamic addition and removal of lists does not work with low-latency collect

low-latency: messages can arrive in any order high-latency: receipt imposes strict order

Use an adaptor access to slaves through one single master slaves send message to single master problem: potential bottleneck

Page 27: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

27

Adaptor

M

N

B

A

C

D

Master Slave

Page 28: Concurrency Runtime Finishing Erlang

Adaptoradaptor() -> receive {decide,Client,PIDs} -> decide(PIDs), Client ! self(), adaptor() end.% A is PID of adaptor processndecide(A,PIDs) -> A ! {decide,self(),PIDs}, receive A -> void end.

Run single dedicated adaptor process master blocks until decision process has terminated

L06, 2009-11-16ID1218, Christian Schulte

28

Page 29: Concurrency Runtime Finishing Erlang

Liveness Properties Important property of concurrent programs

liveness An event/activity might fail to be live

other activities consume all CPU power message box is flooded (denial of services) activities have deadlocked …

Difficult: all possible interactions with other processes must guarantee liveness

reasoning of all possible interactions

L06, 2009-11-16ID1218, Christian Schulte

29

Page 30: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

30

Summary Protocols for coordinating processes Can lead to deadlocks Simple structure best Important: liveness guarantees

Page 31: Concurrency Runtime Finishing Erlang

Process State Reconsidered

L06, 2009-11-16

31

ID1218, Christian Schulte

Page 32: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

32

Creating a Cell Processcell() -> spawn(fun cell/2,0).

Cell process initialized with zero as state

Page 33: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

33

A Simple Taskinc(C) -> C ! {access,self()}, receive {C,N} -> C ! {assign,N+1} end.

Increment the cell’s content by one get the old value put the new value

Does this work? NO! NO! Why?

Page 34: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

34

A Simple Task Screwed…C=cell(),P1=spawn(fun () inc(C) end),P2=spawn(fun () inc(C) end),…

We insist on result being 2! sometimes: 2 sometimes: 1

Why?

Page 35: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

35

Execution Sketch A: Good Process 1

C receives {access,P1} value got: 0 Process 1

C receives {assign,1} value put: 1 Process 2

C receives {access,P2} value got: 1

Process 2 C receives {assign,2} value put: 2

Page 36: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

36

Execution Sketch A: Bad Process 1

C receives {access,P1} value got: 0 Process 2

C receives {access,P2} value got: 0

Process 1 C receives {assign,1} value put: 1

Process 2 C receives {assign,1} value put: 1

Page 37: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

37

What Is Needed We need to avoid that multiple access

and assign operations get out of order We need to combine access and assign

into one operation we can not guarantee that not interrupted we can guarantee that state is consistent we can guarantee that no other message is

processed in between operation is called atomic

Page 38: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

38

Cell With Exchangecell({assign,New},S) -> New;cell({access,PID},S) -> PID ! {self(),S}, S;cell({exchange,PID},S) -> PID ! {self(),S}, receive {PID,NewS} -> NewS end.

Page 39: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

39

Incrementing Rectifiedinc(C) -> C ! {exchange,self()}, receive {C,N} -> C ! {self(),N+1} end

Important aspect no message will be received in between!

Page 40: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

40

State and Concurrency Difficult to guarantee that state is

maintained consistently in a concurrent setting

Typically needed: atomic execution of several statements together

Processes guarantee atomic execution

Page 41: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

41

Other Approaches Atomic exchange

lowlevel hardware: test-and-set

Locks: allow only one thread in a “critical section”

monitor: use a lock together with a process generalizations to single writer/multiple reader

Page 42: Concurrency Runtime Finishing Erlang

Locking Processes Idea that is used most often in

languages which rely on state Before state can be manipulated: lock

must be acquired for example, one lock per object

If process has acquired lock: can perform operations

If process is done, lock is released

L06, 2009-11-16ID1218, Christian Schulte

42

Page 43: Concurrency Runtime Finishing Erlang

A Lockable Processoutside(F,InS) -> receive {acquire,PID} -> PID ! {ok,self()}, inside(F,InS,PID) end.inside(F,InS,PID) -> receive {release,PID} -> outside(F,InS) {PID,Msg} -> inside(F,F(Msg,InS)); end.

L06, 2009-11-16ID1218, Christian Schulte

43

Page 44: Concurrency Runtime Finishing Erlang

Safety Properties Maintain state of processes in consistent

fashion do not violate invariants to not compute incorrect results

Guarantee safety by atomic execution exclusion of other processes ("mutual

exclusion")

L06, 2009-11-16ID1218, Christian Schulte

44

Page 45: Concurrency Runtime Finishing Erlang

Concurrent MiniErlang (CME)

L06, 2009-11-16ID1218, Christian Schulte

45

Page 46: Concurrency Runtime Finishing Erlang

Processes in CME Computation state is a collection of processes

P1 P2 … Pn every process must be reduced eventually (fairness) no order among processes (set of processes)

A process is an extended MiniErlang machineEs ; Vs ; n ; Ms

expression stack Es value stack Vs process identifier (PID) n mailbox Ms

L06, 2009-11-16ID1218, Christian Schulte

46

Page 47: Concurrency Runtime Finishing Erlang

CME: Values and Expressions Values now also include PIDs

V := int | [] | [ V1 | V2 ] | n PIDs cannot not appear in expressions

Expressions E := … | self() | E1 ! E2 | spawn(F)| E1 , E2 | receive C1; …; Cn end

InstructionsCONS, CALL, SEND

L06, 2009-11-16ID1218, Christian Schulte

47

Page 48: Concurrency Runtime Finishing Erlang

Sequential CompositionE1,E2Es ; Vs ; n ; Ms Pr → E1E2Es ; Vs ; n ; Ms Pr

Decompose into individual statements obey order of statements

To be read: pick fairly any process that matches this pattern

L06, 2009-11-16ID1218, Christian Schulte

48

Page 49: Concurrency Runtime Finishing Erlang

Self PIDself()Es ; Vs ; n ; Ms Pr → Es ; nVs ; n ; Ms Pr

Pushes process' own PID on the value stack

L06, 2009-11-16ID1218, Christian Schulte

49

Page 50: Concurrency Runtime Finishing Erlang

Process Spawningspawn(F)Es ; Vs ; n ; Ms Pr → Es ; mVs ; n ; Ms

F(); ; m ; Pr

Create new process with expression stack that executes call to F an empty value stack a fresh (not used elsewhere) PID m an empty mailbox

L06, 2009-11-16ID1218, Christian Schulte

50

Page 51: Concurrency Runtime Finishing Erlang

Process Termination; V ; n ; Ms Pr → Pr

Delete process without expression to be evaluated

single value on stack is ignored

L06, 2009-11-16ID1218, Christian Schulte

51

Page 52: Concurrency Runtime Finishing Erlang

Message SendingE1!E2Es ; Vs ; n ; Ms Pr → E1E2SENDEs ; Vs ; n ; Ms Pr

First evaluate destination, then message After that perform message sending

L06, 2009-11-16ID1218, Christian Schulte

52

Page 53: Concurrency Runtime Finishing Erlang

SEND InstructionSENDEs ; V1mVs ; n ; Ms Es’ ; Vs’ ; m ; Ms’ Pr

→ Es ; V1Vs ; n ; Ms Es’ ; Vs’ ; m ; Ms’V1 Pr

Instruction sends and evaluates to message Semantics is stronger than Erlang behavior

realistic: model messages in transit with FIFO

L06, 2009-11-16ID1218, Christian Schulte

53

Page 54: Concurrency Runtime Finishing Erlang

Message Selectionselect(C1; …; Cn, MMs, Ns) = s(Bi),

NsMs if Ci = Hi -> Bi is first matching clause,

match(Hi, M)=s select(C1; …; Cn, MMs, Ns) =

select(C1; …; Cn, Ms, NsM) if no clause matches

select(C1; …; Cn, , Ns) = no

Traverses messages in mailbox in order constructs mailbox with all messages but

received one

L06, 2009-11-16ID1218, Christian Schulte

54

Page 55: Concurrency Runtime Finishing Erlang

Message Receiptreceive C1; …; Cn endEs ; Vs ; n ; Ms

Pr → EEs ; Vs ; n ; Ns Pr

if select(C1; …; Cn, Ms, ) = E, Ns

Process can only receive if matching message is found

L06, 2009-11-16ID1218, Christian Schulte

55

Page 56: Concurrency Runtime Finishing Erlang

CME Machine Initial configuration:

single process with expression we want to evaluate on expression stack

Final configuration: no process can be reduced not common, computations continue forever

L06, 2009-11-16ID1218, Christian Schulte

56

Page 57: Concurrency Runtime Finishing Erlang

ME and CME Differences Expressions in ME can be replaced by

the value they can be evaluated to declarative: independent reasoning referential transparency

Not true in CME message sending as side-effect concurrently coordinating: reason on

interaction

L06, 2009-11-16ID1218, Christian Schulte

57

Page 58: Concurrency Runtime Finishing Erlang

Runtime Efficiency

L06, 2009-11-16

58

ID1218, Christian Schulte

Page 59: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

59

What Are the Questions? Runtime efficiency

what is it we are interested in? why are we interested? how to make statements on runtime and

memory?

Clearly “faster” is better we have to know how fast our programs

compute we have to know how much memory our

programs require we have to know how “good” a program is

Page 60: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

60

Statements on Runtime Run your program for some example

data sets on some computer, make a statement

statement valid, if data set gets larger? statement valid, if executed on different

computer? statement valid, if executed on same computer

but slightly different configuration?

Testing is insufficient!

Page 61: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

61

Runtime Guarantees Testing can never yield a guarantee on

runtime!

We need a form to express runtime guarantees

capture size of input data capture different computers/configurations

Page 62: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

62

Runtime Guarantees Express runtime in relation to input size

T(n) runtime function where n is size of input

for example integers argument lists length of list

Page 63: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

63

Runtime Guarantees We need to ignore marginal difference

between runtime functions

difference for small input sizesonly consider for n ≥ n0

difference by constantc1 T(n) same as c2 T(n)

Page 64: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

64

Asymptotic Complexity Asymptotic complexity is such a runtime

guarantee: bestupper bound

on runtimeup to a constant factorfor sufficiently large inputs

Discuss runtime by using “big-oh” notation

Page 65: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

65

Big-Oh Notation Assume

T(n) runtime, n size of input f(n) function on non-negative integers

T(n) is of O(f(n)) “T(n) is of order f(n)”iff T(n) c f(n) for all n≥n0

for some c (whatever computer) for some n0 (sufficiently large)

Page 66: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

66

Big-Oh Notation

T(n) is of O(f(n)) “T(n) is of order f(n)”iff T(n) c f(n) for all n≥n0

for some c (whatever computer) for some n0 (sufficiently large)

cn

n0 n

T(n)f(n) = n, c=2

Page 67: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

67

Big-Oh Notation T(n) is of O(f(n)) sometimes written as

T(n) = O(f(n)) T(n) O(f(n))

Examples T(n) = 4n + 42 is O(n) T(n) = 5n + 42 is O(n) T(n) = 7n + 11 is O(n) T(n) = 4n + n3 is O(n3) T(n) = n100 + 2n + 8 is O(2n)

Page 68: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

68

Big-Oh Notation Often one just says for

O(n) linear complexity O(n2) quadratic complexity O(n3) cubic complexity O(log n) logarithmic complexity O(2n) exponential complexity

Page 69: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

69

Summary Formulate statements on runtime as

runtime guaranteesasymptotic complexity

for large input independent of computer

Expressed in big-oh notation abstracts away behavior of function for small

numbers abstracts away constants captures asymptotic behavior of functions

Page 70: Concurrency Runtime Finishing Erlang

Determining Complexity

L06, 2009-11-16

70

ID1218, Christian Schulte

Page 71: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

71

Approach Take MiniErlang program Take execution time for each expression Give equations for runtime of functions Solve equations Determine asymptotic complexity

Page 72: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

72

Simplification Here We will only consider programs with one

function Generalization to many functions

straightforward complication: solving equation

Page 73: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

73

Execution Times for Expressions

Give inductive definition based on function definition and structure of expressions

Function definition pattern matching and guards

Simple expression values and list construction

More involved expression function call recursive function call leads to recursive

equation often called: recurrence equation

Page 74: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

74

Execution Time: T(E) Value

T(V) = cvalue List construction

T([E1|E2]) = ccons + T(E1) + T(E2)

Time T(E) needed for executing expression E

how MiniErlang machine executes E

Page 75: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

75

Execution Time: T(E) Value

T(V) = c List construction

T([E1|E2]) = c + T(E1) + T(E2)

Time T(E) needed for executing expression E

how MiniErlang machine executes E

to ease notation, just forget subscript

Page 76: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

76

Function Call For a function F define a function

TF(n) for its runtimeand determine

size of input for call to F input for a function

Page 77: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

77

Function CallT(F(E1, ..., Ek)) = ccall + T(E1) + ... + T(Ek)

+ TF(size(IF({1, ..., k})))

input arguments IF({1, ..., k}) input arguments for F

size size(IF({1, ..., k}))size of input for F

Page 78: Concurrency Runtime Finishing Erlang

Function Definition Assume function F defined by clauses

H1 -> B1; …; Hk -> Bk.

TF(n) = cselect + max { T(B1), …, T(Bk) }

L06, 2009-11-16ID1218, Christian Schulte

78

Page 79: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

79

Example: app/2app([],Ys) -> Ys;app([X|Xr],Ys) -> [X|app(Xr,Ys)].

What do we want to computeTapp(n)

Knowledge needed input argument first argument size function length of list

Page 80: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

80

Computing Tapp =Ta

let’s use the whiteboard…

Page 81: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

81

Append: Recurrence Equation Analysis yields

Tapp(0) = c1

Tapp(n) = c2 + Tapp(n-1) Solution to recurrence is

Tapp(n) = c1 + c2 n Asymptotic complexity

Tapp(n) is of O(n) “linear complexity”

Page 82: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

82

Recurrence Equations Analysis in general yields a system

T(n) defined in terms of T(m1), …, T(mk)for m1, …, mk < n

T(0), T(1), … values for certain n

Possibilities solve recurrence equation (difficult in general) lookup asymptotic complexity for common case

Page 83: Concurrency Runtime Finishing Erlang

L06, 2009-11-16 ID1218, Christian Schulte 83

Common Recurrence Equations

T(n) Asymptotic Complexityc + T(n–1) O(n)

c1 + c2n + T(n–1) O(n2)

c + T(n/2) O(log n)

c1 + c2n + T(n/2) O(n)

c + 2T(n/2) O(n)

c + 2T(n-1) O(2n)

c1 + c2n + T(n/2) O(n log n)

Page 84: Concurrency Runtime Finishing Erlang

Example: Naïve Reverse rev([]) -> [];rev([X|Xr]) -> app(rev(Xr),[X]).

Analysis yieldsTrev(0) = c1

Trev(n) = c2 + Tapp(n-1) + Trev(n-1)= c2 + c3(n-1) + Trev(n-1)

Asymptotic complexityTrev(n) is of O(n2) “quadratic complexity”

L06, 2009-11-16ID1218, Christian Schulte

84

Page 85: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

85

What Is to be Done? Size functions and input functions Recurrence equation Finding asymptotic complexity

…understanding of programs might be

necessary…

Page 86: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

86

Obs! Making computations iterative can

change runtime! Can improve:

Naïve reverse reverseO(n2) O(n)

Can be worse:appending lists appending lists

O(n) O(n2)

Page 87: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

87

Judging Asymptotic Complexity What does it mean?

good/bad? even optimal?

Consider size of computed output if size is of same order: perfect? otherwise:

O(n log n) is very good (optimal for sorting) check book on algorithms

Very difficult problem!

Page 88: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

88

Hard Problems Computer science is tough business Most optimization, combinatorial

problems are hard (non-tractable) problems

Graph coloring minimal number of colors no connected node has same color used in: compilation, frequency allocation, …

Travelling salesman

Page 89: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

89

Hard Problems NP problems: easy (polynomial

complexity) for checking whether a certain candidate is a solution to problem

much harder: finding a solution

NP-complete problems: as hard as any other NP-complete problem

strong suspicion: no polynomial algorithm for finding a solution exists

examples: graph coloring, satisfiability testing, cryptography, …

Page 90: Concurrency Runtime Finishing Erlang

L06, 2009-11-16ID1218, Christian Schulte

90

Summary: Runtime Efficiency Asymptotic complexity gives runtime

guarantee abstract from constants for all possible input given in Big-Oh notation

Computing asymptotic complexity

Think about meaning of asymptotic complexity

Page 91: Concurrency Runtime Finishing Erlang

Summary: Part 1

L06, 2009-11-16

91

ID1218, Christian Schulte

Page 92: Concurrency Runtime Finishing Erlang

Functional Programming in Erlang

Functions defined by clauses clauses have patterns and guards

Execution evaluates expressions declarative

Expressions: values and function call Pattern matching for clause selection

powerful for symbolic computation

L06, 2009-11-16ID1218, Christian Schulte

92

Page 93: Concurrency Runtime Finishing Erlang

MiniErlang Model for functional Erlang that captures

memory consumption in particular stack space for function calls

MiniErlang machine gives operational semantics

Identifies iterative computations run with constant stack space captures tail recursion

Model for runtime analysis

L06, 2009-11-16ID1218, Christian Schulte

93

Page 94: Concurrency Runtime Finishing Erlang

Functional Programming Techniques

Pattern matching for execution control Accumulators

make more functions tail recursive Higher-order functions

pass functions as parameters separate generic from specific aspects

L06, 2009-11-16ID1218, Christian Schulte

94

Page 95: Concurrency Runtime Finishing Erlang

Concurrent Programming in Erlang

Message-passing between concurrent processes

fair execution of processes fully buffered message delivery processes with identity

Programmable receipt of messages in particular: pick messages by sending process with possibility of timeout

L06, 2009-11-16ID1218, Christian Schulte

95

Page 96: Concurrency Runtime Finishing Erlang

Concurrent Programming Techniques

Sending messages: asynchronous, synchronous

Communication patterns asynchronous broadcasting synchronous broadcasting: low latency versus order choosing processes

Avoiding deadlocks order recipients, introduce concurrency adaptors

Essential properties liveness safety

L06, 2009-11-16ID1218, Christian Schulte

96

Page 97: Concurrency Runtime Finishing Erlang

Other Things in Erlang Exception handling Distributed programming Fault-tolerant programming

L06, 2009-11-16ID1218, Christian Schulte

97


Recommended