Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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.

2 CS321 Functional Programming 2 © JAS 2005 7-2 Data Flow programs may be represented graphically op …………… It can be shown that a maximum of two inputs and two outputs Are required

3 CS321 Functional Programming 2 © JAS 2005 7-3 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

4 CS321 Functional Programming 2 © JAS 2005 7-4 x Constant generator Copy + Typical operators =

5 CS321 Functional Programming 2 © JAS 2005 7-5 Switch control T F T F Merge Gates F control T

6 CS321 Functional Programming 2 © JAS 2005 7-6 A conditional expression control T F T F predicate then path else path

7 CS321 Functional Programming 2 © JAS 2005 7-7 Applying functions fac APPLY argument definition of function result or closure APPLY (2nd)argument

8 CS321 Functional Programming 2 © JAS 2005 7-8 A factorial function 1 >= F 1 fac - 1 T * F APPLY 1 1 1 2 2 2 F F F 2 2 2 1 F F

9 CS321 Functional Programming 2 © JAS 2005 7-9 A factorial function 1 >= F 1 fac - 1 T * F APPLY 1

10 CS321 Functional Programming 2 © JAS 2005 7-10 A factorial function 1 >= F 1 fac - 1 T * F APPLY 1 1 1 1 1 1 T T T T T 1 1

11 CS321 Functional Programming 2 © JAS 2005 7-11 A factorial function 1 >= F 1 fac - 1 T * F APPLY 1 2 1 F F 2 2 2

12 CS321 Functional Programming 2 © JAS 2005 7-12 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+b1c1d2 -

13 CS321 Functional Programming 2 © JAS 2005 7-13 A factorial function 1 >= F 1 fac - 1 T * F APPLY (a,1,-,-,b1,-) a b (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,-)

14 CS321 Functional Programming 2 © JAS 2005 7-14 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

15 CS321 Functional Programming 2 © JAS 2005 7-15 Execution Cycle (Data-Driven) FOR any instruction DO IF instruction has all its required inputs THEN execute the instruction

16 CS321 Functional Programming 2 © JAS 2005 7-16 Executing the apply node IF the first input is a single parameter function THEN 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 2 nd input value into input record of 1 st instr in function ELSE IF 1 st input is closure requiring 1 parameter THEN generate copy of function pass 2 nd input and closure to appropriate input records ELSE generate new closure with 2 nd input value

17 CS321 Functional Programming 2 © JAS 2005 7-17 A machine architecture to implement this execution cycle pool of packets processors

18 CS321 Functional Programming 2 © JAS 2005 7-18 Processing units Communication network Instruction selection mechanism Program store instructions Executable instructions Values/closures

19 CS321 Functional Programming 2 © JAS 2005 7-19 The Manchester Architecture instruction store processing units result queue switch matching store tasks results operand pairs input output interface to real world and other layers route for inputs to single operand instructions memory/control unit

20 CS321 Functional Programming 2 © JAS 2005 7-20 The Alice Architecture processors Network – originally slotted ring

21 CS321 Functional Programming 2 © JAS 2005 7-21 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

22 CS321 Functional Programming 2 © JAS 2005 7-22 A factorial function 1 >= F 1 fac - 1 T * F APPLY

23 CS321 Functional Programming 2 © JAS 2005 7-23 Execution Cycle (Demand-Driven ) FOR any instruction whose output has been requested DO 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 2 nd instr ELSE no action ELSE request 1 st instr in source list apply: send request to 1 st instr in source list

24 CS321 Functional Programming 2 © JAS 2005 7-24 Executing the apply node IF the first input is a single parameter fn THEN generate copy of fn adjusting input and output lists ELSE IF first input is closure requiring 1 parameter THEN generate copy of fn ELSE generate closure, remembering id of 2 nd instr

25 CS321 Functional Programming 2 © JAS 2005 7-25 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

26 CS321 Functional Programming 2 © JAS 2005 7-26 Data Flow Languages ManchesterLapse MITVAL IrivineId JapanValid ( ) Lawrence LivermoreSISAL

27


Download ppt "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."

Similar presentations


Ads by Google