Costing by construction Greg Michaelson School of Mathematical & Computer Sciences Heriot-Watt University 26/8/13 CPA 2013 Napier 1
Summary • how can we use cost information (e.g. WCET, space) to guide software construction? • mini-Hume – compilation to stack machine – cost model • box calculus • augmenting box calculus with costs • costulator 26/8/13 CPA 2013 Napier 2
Overview • mature WCET/space costs models for many languages • analysis tools are whole program – apply after not during initial software construction • can we see cost implications at every stage as we construct software? 26/8/13 CPA 2013 Napier 3
Hume • with Kevin Hammond (St Andrews) • formally motivated language for resource aware system construction • concurrent finite state boxes joined by wires • box transitions from patterns to expressions • rich polymorphic types 26/8/13 CPA 2013 Napier 4
Hume • strong separation of: – coordination : between boxes & environment – expression : within boxes • strong formal foundations – semantics + type system • tool chain via abstract machine to code • amortised cost models instantiated for concrete platforms 26/8/13 CPA 2013 Napier 5
Hume • Turing complete - too big for this presentation • mini-Hume – integers types only – no functions – no if/case – no * (ignore) pattern or expression 26/8/13 CPA 2013 Napier 6
mini-Hume box gen in (n) out (n’,r) match (x) -> (x+1,x); n gen box double x -> (x+1,x) in (x) out (y) n’ r match (n) -> (2*n); stream output to “std_out”; x double n -> 2*n wire gen (gen.n’initially 0) y (gen.n,double.x); wire double output (gen.r) (output); 26/8/13 CPA 2013 Napier 7
mini-Hume prog -> coord ; [prog] coord -> box | wire | stream box -> box id in ( vars ) out ( vars ) match ( patt ) -> ( exp ) | ... vars -> var| var , vars patt -> int | var | patt , patt exp -> int | var | exp op exp | exp , exp wire -> wire id ( ins ) ( outs ) ins -> var | var.var [ initially int] | ins , ins outs -> var | var . var | outs , outs stream -> stream id [ from / to ] “ path ” 26/8/13 CPA 2013 Napier 8
Execution model forever - execute for each box find pattern matching inputs bind pattern variables evaluate expression to produce outputs - super step for each box copy outputs to associated inputs 26/8/13 CPA 2013 Napier 9
Stack machine PUSHI integer stack[sp++] = integer VAR identifier allocate next memory address PUSHM identifier stack[sp++] = mem[addr( identifier )] POPM identifier mem[addr( identifier )] = stack[--sp] POP sp-- ADD stack[sp-2] = stack[sp-2]+stack[sp-1]; sp-- SUB stack[sp-2] = stack[sp-2]-stack[sp-1]; sp-- MULT stack[sp-2] = stack[sp-2]*stack[sp-1]; sp-- DIV stack[sp-2] = stack[sp-2]/stack[sp-2]; sp-- LABEL label JNEG label if(stack[sp--]<0) goto label JZERO label if(stack[sp--]==0) goto label JPOS label if(stack[sp--]>0) goto label JNZERO label if(stack[sp--]!=0) goto label JMP label goto label 26/8/13 CPA 2013 Napier 10
Compilation - memory box -> box id in ( vars ) out ( vars ) ... ==> VAR id I1 VAR id I2 ... VAR id O1 VAR id O2 ... stream -> stream id [ from / to ] “ path ” ==> VAR id 26/8/13 CPA 2013 Napier 11
Compilation - box box -> box id in ( vars ) out ( vars ) match ( patt ) -> ( exp ) | ... ==> LABEL id 1: << patt 1 >> << exp 1 >> JMP id END ... LABEL id N +1 : LABEL id END: 26/8/13 CPA 2013 Napier 12
Compilation - pattern • for box id patt i -> int ==> PUSHM id I i PUSHI int SUB JNZERO idi +1 patt i -> patt 1 , patt 2 ==> << patt 1 >> << patt 2 >> 26/8/13 CPA 2013 Napier 13
Compilation - expression • for box id exp i -> int ==> PUSH int exp i -> var i ==> PUSHM id I i exp i -> exp 1 op exp 2 ==> << exp1 >> << exp2 >> << op >> 26/8/13 CPA 2013 Napier 14
Compilation - expression <<+>> ==> ADD <<->> ==> SUB <<*>> ==> MULT <</>> ==> DIV • for box id exp i -> exp 1 , exp 2 ==> << exp 1 >> POPM id O i << exp 2 >> 26/8/13 CPA 2013 Napier 15
Compilation - wire super step wire -> wire id ( ins ) ( outs ) ==> • for out i var 1 .var 2 ==> PUSHM id O i POPM input wired to var 1 .var 2 var (of stream) ==> PUSHM id O i POPM var 26/8/13 CPA 2013 Napier 16
Compilation - initially • for wire id, out i var 1 .var 2 initially int ==> PUSHI int POPM id I i 26/8/13 CPA 2013 Napier 17
Compilation - program memory inititially LABEL _MAIN box wire superstep SHOW GOTO _MAIN 26/8/13 CPA 2013 Napier 18
Compilation - links LABEL incd0 - pattern 1 box incd VAR incdI0 - s - box/patt 0 LABEL incd1 in (s,n) out (s',n',r) VAR incdI1 - n PUSHM incdI0 ... match VAR incdO0 – s’ PUSHI 0 - (pattern 2) (0,x) -> (1,x+1,x+1) | VAR incdO1 – n’ SUB LABEL incd2 (1,x) -> (0,x+1,2*x); VAR incdO2 - r JNZERO incd1 VAR output - exp 0 - super step stream output to PUSHI 1 LABEL incdEND "std_out"; - initially POPM incdO0 PUSHM incdO0 PUSHI 0 PUSHM incdI1 POPM incdI0 wire incd POPM incdI0 PUSHI 1 PUSHM incdO1 (incd.s' initially 0, PUSHI 0 ADD POPM incdI1 incd.n' initially 0) POPM incdI1 POPM incdO1 PUSHM incdO2 (incd.s,incd.n,output); PUSHM incdI1 POPM output - execute PUSHI 1 LABEL _MAIN ADD - loop POPM incdO2 SHOW JMP incdEND JMP _MAIN 26/8/13 CPA 2013 Napier 19
Cost model • box costs in execution box -> box id in ( vars ) out ( vars ) match - max (( Σ cost(patt i ) )+ cost (exp i ))+1 - JMP ( patts ) -> ( exps ) | ... patt -> int - 4 – PUSHM,PUSHI,SUB,JNZ var - 0 patt 1 , patt 2 - cost(patt 1 ) + cost(patt 2 ) exp -> int - 1 - PUSHI var - 1 - PUSHM exp 1 op exp 2 - cost(exp 1 )+cost(exp 2 )+1 - op exp 1 , exp 2 - cost(exp 1 )+1+cost(exp 2 ) - POPM 26/8/13 CPA 2013 Napier 20
Cost model • wire costs in super step wire -> wire id ( ins ) ( outs ) - cost(ins)+cost(outs) ins -> var - 2 – PUSHM,POPM var.var [ initially int] - 2[+2] – PUSHM,POPM [+PUSHI,POPM] ins 1 , ins 2 - cost(ins 1 )+cost(ins 2 ) outs -> var - 2 – PUSHM,POPM var . var - 2 – PUSHM,POPM outs 1 , outs 2 - cost(outs 1 )+cost(outs 2 ) stream -> stream id to “path” - 1 - POPM 26/8/13 CPA 2013 Napier 21
Example gen: space 3 box gen VAR genI0 LABEL double0 in (n) pattern 0 exp 7 VAR genO0 out (n’,r) VAR genO1 PUSHI 2 total cost 7 match (x) -> (x+1,x); VAR doubleI0 PUSHM doubleI0 VAR doubleO0 MULT double: space 2 box double VAR output POPM doubleO0 in (x) pattern 0 exp 5 JMP doubleEND out (y) PUSHI 0 LABEL double1 total cost 5 match (n) -> (2*n); POPM genI0 LABEL doubleEND output: space 1 stream output to LABEL _MAIN PUSHM genO0 “std_out”; POPM genI0 superstep 1 LABEL gen0 PUSHM genO1 wire gen gen: initially 2 PUSHM genI0 POPM doubleI0 (gen.n’initially 0) superstep 4 PUSHI 1 PUSHM doubleO0 (gen.n,double.x); ADD POPM genO0 POPM output wire double double: initially 0 PUSHM genI0 (gen.r)(output); superstep 2 POPM genO1 SHOW JMP genEND JMP _MAIN LABEL gen1 LABEL genEND 26/8/13 CPA 2013 Napier 22
Box calculus • with Gudmund Grov (Heriot-Watt) • based on BMF, fold/unfold, FP etc • rules to: – introduce/eliminate boxes/wires – split/join boxes horizontally/vertically • NB rules affect coordination and expressions layers 26/8/13 CPA 2013 Napier 23
Box calculus x -> x x -> f (g x) (x,y) -> (f x,g y) x -> g x x -> f x y -> g y y -> f y horizontal split/join identity vertical split/join 26/8/13 CPA 2013 Napier 24
Box calculus x -> x x -> x x -> (x,y) (x,y) -> x output introduction/elimination input introduction/elimination 26/8/13 CPA 2013 Napier 25
Example: divide & conquer (x) <- (conquer (process f (divide x))) process f x y = (f x,f y) 26/8/13 CPA 2013 Napier 26
Example: divide & conquer (x) <- (divide x) (x,y) <- (conquer (process f (x,y)) • vertical split 26/8/13 CPA 2013 Napier 27
Example: divide & conquer (x) <- (divide x) (x,y) <- (process f (x,y)) (x’,y’) <- (conquer (x’,y’)) • vertical split 26/8/13 CPA 2013 Napier 28
Example: divide & conquer (x) <- (divide x) (x,y) <- (f x,f y) (x’,y’) <- (conquer (x’,y’)) • unfold 26/8/13 CPA 2013 Napier 29
Example: divide & conquer (x) <- (divide x) (x’) <- (f x) (y’) <- (f y) (x’’,y’’) <- (conquer (x’’,y’’)) • horizontal split 26/8/13 CPA 2013 Napier 30
Costing by construction • augment rules with cost judgements • construct software from scratch – use rules to justify each step – show cost impact of each rule application 26/8/13 CPA 2013 Napier 31
Box calculus + costs x -> x x -> f (g x) +/- cost patt x +/- cost patt y +/- 2 - super step +/- cost exp x from g x +/- 2 - super step +/- 2 - super step from x to y x -> g x +/- 2 - super step to x y -> f y identity vertical split/join 26/8/13 CPA 2013 Napier 32
Recommend
More recommend