27
© JAS 2005 7-1 CS321 Functional Programming 2 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations that are performed in an order specified by the programmer (a control path). In a data flow system a program is a set of operations performed in an order determined by the data dependencies (and the availability of resources). This latter approach is clearly more appropriate to a functional style of programming than the conventional approach.

CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

Embed Size (px)

Citation preview

Page 1: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-1

CS321 Functional Programming 2

Implementation using the Data Flow Approach

• In a conventional control flow system a program is a set of operations that are performed in an order specified by the programmer (a control path).

• In a data flow system a program is a set of operations performed in an order determined by the data dependencies (and the availability of resources).

• This latter approach is clearly more appropriate to a functional style of programming than the conventional approach.

Page 2: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-2

CS321 Functional Programming 2

Data Flow programs may be represented graphically

op

……………

……………

It can be shown that a maximum of two inputs and two outputsAre required

Page 3: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-3

CS321 Functional Programming 2

The basic idea is that data values (tokens) flow along the arcs in a graph built up from nodes.

Whenever a node has all (necessary) inputs present it is said to be enabled.

It then fires by absorbing its inputs and generating output values on its output arcs.

Nodes can fire in parallel

Page 4: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-4

CS321 Functional Programming 2

x

Constant generator

Copy

+

Typical operators

=

Page 5: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-5

CS321 Functional Programming 2

Switch

controlT F

control T F

Merge

Gates

FcontrolTcontrol

Page 6: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-6

CS321 Functional Programming 2

A conditional expression

control

T F

T Fcontrol

predicate

then path else path

Page 7: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-7

CS321 Functional Programming 2

Applying functions

fac

APPLY

argument

definitionof function result

or closure

APPLY

(2nd)argument

Page 8: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-8

CS321 Functional Programming 2

A factorial function1

>=

F

1

fac -

1

T

*

F

APPLY

1

1

1

2

22

F

F

F2

2 2

1

F

F

Page 9: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-9

CS321 Functional Programming 2

A factorial function1

>=

F

1

fac -

1

T

*

F

APPLY1

Page 10: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-10

CS321 Functional Programming 2

A factorial function1

>=

F

1

fac -

1

T

*

F

APPLY

1

1

1

1

11

T

T

T

T

T

1 1

Page 11: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-11

CS321 Functional Programming 2

A factorial function1

>=

F

1

fac -

1

T

*

F

APPLY

1

21

F

F

22

2

Page 12: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-12

CS321 Functional Programming 2

Encoding the graph

Each node can have

• A name (ID)

• An operation

• A list of inputs

• A list of outputs

id op inputs outputs

a + b1 c1 d2 -

Page 13: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-13

CS321 Functional Programming 2

A factorial function1

>=

F

1

fac -

1

T

*

F

APPLY

(a,1,-,-,b1,-)

ab

(b,>=,a1,c1,d1,-)

c

(c,copy,inp,-,b2,e2) d e

f g

h i

j k

m n

o p

q

(d,copy,b1,-,j1,e1)(e,F-gate,d2,c2,f1,-)(f,copy,f1,-,m1,i1)(g,1,-,-,i2,-)(h,fac,-,-,k1,-)(i,-,f2,g1,k2,-)(j,copy,d1,-,o1,p1)(k,apply,h1,i1,m2,-)(m,1,-,-,o2,-)(n,*,f1,k1,p2,-)(o,T-gate,j1,m1,q1,-)(p,F-gate,j2,n1,q2,-)(q,join,o1,p1,out,-)

Page 14: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-14

CS321 Functional Programming 2

Note that naming both the inputs and the outputs is unnecessary – the node generating the values can determine which node needs them, but the node needs to know how many inputs it needs

Also note that in all the examples given we never had 2 inputs and 2 outputs

It is therefore possible that the size of these instruction packets could be reduced

Page 15: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-15

CS321 Functional Programming 2

Execution Cycle (Data-Driven)

FOR any instruction

DO IF instruction has all its required inputs THEN execute the instruction

Page 16: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-16

CS321 Functional Programming 2

Executing the apply node

IF the first input is a single parameter functionTHEN generate a copy of the function

create a new copy of each instr in the fn assign unique id's and modify output list

FOR each output instr in the fn DO set output list to appropriate values

from output list of apply node place 2nd input value into input record of 1st instr in functionELSE IF 1st input is closure requiring 1 parameter

THEN generate copy of function pass 2nd input and closure to appropriate input

records ELSE generate new closure with 2nd input value

Page 17: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-17

CS321 Functional Programming 2

A machine architecture to implement this execution cycle

pool ofpackets

processors

Page 18: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-18

CS321 Functional Programming 2

Processing units

Communication network

Instruction selection mechanism

Program store

instructions

Executable instructions

Executable instructions Values/closures

Values/closures

Page 19: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-19

CS321 Functional Programming 2

The Manchester Architecture

instructionstore

processingunits

result queue

switch

matchingstore

tasks results

operandpairs

input

output

interfaceto realworld and

other layers

route forinputs tosingle operandinstructions

memory/control unit

Page 20: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-20

CS321 Functional Programming 2

The Alice Architecture

processors

Network –originallyslotted ring

Page 21: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-21

CS321 Functional Programming 2

As well as this data-driven approach it is possible to envisage a demand-driven approach

Instead of the arrival of data driving the execution we imagine a request for output being received at the bottom of the graph

Page 22: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-22

CS321 Functional Programming 2

A factorial function1

>=

F

1

fac -

1

T

*

F

APPLY

Page 23: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-23

CS321 Functional Programming 2

Execution Cycle (Demand-Driven )

FOR any instruction whose output has been requestedDO IF instruction has all required inputs THEN execute instruction ELSE CASE type of instr OF primitive, copy switch: send request to all instrs in source list gate: IF control input is present THEN IF control matches gate THEN send request to 2nd instr

ELSE no action ELSE request 1st instr in source list

apply: send request to 1st instr in source list

Page 24: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-24

CS321 Functional Programming 2

Executing the apply node

IF the first input is a single parameter fnTHEN generate copy of fn adjusting input and output listsELSE IF first input is closure requiring 1 parameter THEN generate copy of fn ELSE generate closure, remembering id of 2nd instr

Page 25: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-25

CS321 Functional Programming 2

Data Flow as implementation of a functional language

Every node represents a function

output = node (input1, input2)

Functional expressions can be represented by networks of nodes

Basic property of data flow is no variables – single assignment – or zero assignment

This is mirrored in functional languages

Demand-driven = Lazy; Data-Driven = Eager/Applicative Order

Page 26: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations

© JAS 2005 7-26

CS321 Functional Programming 2

Data Flow Languages

Manchester Lapse

MIT VAL

Irivine Id

Japan Valid ()

Lawrence Livermore SISAL

Page 27: CS321 Functional Programming 2 © JAS 2005 7-1 Implementation using the Data Flow Approach In a conventional control flow system a program is a set of operations