Home My Page Projects Code Snippets Project Openings diderot
 Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Diff of /branches/lamont/src/compiler/simplify/reduction_lift.sml
 [diderot] / branches / lamont / src / compiler / simplify / reduction_lift.sml Diff of /branches/lamont/src/compiler/simplify/reduction_lift.sml

revision 2245, Sun Mar 3 12:51:51 2013 UTC revision 2246, Sun Mar 3 14:51:31 2013 UTC
# Line 3  Line 3
3   * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)   * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
5   *   *
6   * Lift reduction operations to global scope.   * Lift reduction operations into its own block.
7   *   *
8   * NOTE: this process can be streamlined as follows:   * NOTE: this process can be streamlined as follows:
9   *      1) Find the reduction expressions   *      1) Find the reduction expressions
10   *      2) create new global variables for each reduction expression and place the computation in the global reduction block.   *      2) Move the reduction and prior statements needed for the reduction into the global reduction block.
11   *)   *)
12
13  structure ReductionLift : sig  structure ReductionLift : sig
# Line 33  Line 33
33      fun reductionToString(r) = (case r      fun reductionToString(r) = (case r
34       of S.R_Max => "max"       of S.R_Max => "max"
35        | S.R_Min => "min"        | S.R_Min => "min"
36        | S.R_Or => "or"        | S.R_Exists => "exists"
37        | S.R_And => "and"        | S.R_All => "all"
| S.R_Xor => "xor"
38        | S.R_Product => "product"        | S.R_Product => "product"
39          | S.R_Mean => "mean"
40          | S.R_Variance => "variance"
41        | S.R_Sum => "sum")        | S.R_Sum => "sum")
42
43      fun transform (errStrm,prog as S.Program{globals,globalInit as S.Block(globalInitStms), init, strands,inputs,...}) = let      fun transform (errStrm,prog as S.Program{globals,globalInit as S.Block(globalInitStms), init, strands,globalBlock,inputs,...}) = let
44
45      val globalReduc = ref []      val globalReduc = ref []
val extraGlobals = ref []
val extraGlobalInit = ref []
46
47      fun reduceStrand(S.Strand{name, params, state,stateInit,methods}) =      fun reduceStrand(S.Strand{name, params, state,stateInit,methods}) =
48              S.Strand{              S.Strand{
# Line 60  Line 59
59      and reduceBlock (S.Block stms) = S.Block(List.rev (reduceStmts(stms,[])))      and reduceBlock (S.Block stms) = S.Block(List.rev (reduceStmts(stms,[])))
60
61      and reduceStmts ((stm::stms),stms') = (case stm      and reduceStmts ((stm::stms),stms') = (case stm
62            of S.S_Foreach(v,e,b) => reduceStmts(stms,S.S_Foreach(v,reduceExp e, reduceBlock b)::stms')            of S.S_Foreach(v,e,b) => reduceStmts(stms,S.S_Foreach(v, e, reduceBlock b)::stms')
63             | S.S_IfThenElse(v,b1,b2) => reduceStmts(stms,S.S_IfThenElse(v,reduceBlock b1, reduceBlock b2)::stms')             | S.S_IfThenElse(v,b1,b2) => reduceStmts(stms,S.S_IfThenElse(v,reduceBlock b1, reduceBlock b2)::stms')
64             | S.S_Assign (v,e) => reduceStmts(stms,S.S_Assign(v,reduceExp e)::stms')             | S.S_Assign (lhs,rhs) => (case reduceAssign (lhs,rhs)
65                      of NONE => reduceStmts(stms,stms')
66                       | SOME rhs' => reduceStmts(stms,S.S_Assign(lhs,rhs')::stms')
67                     (* end case *))
68             | _ => reduceStmts(stms,stm::stms'))             | _ => reduceStmts(stms,stm::stms'))
69        | reduceStmts ([],stms') = stms'        | reduceStmts ([],stms') = stms'
70
71
72      and reduceExp(e) = (case e      and reduceAssign(lhs,rhs) = (case rhs
73          of S.E_Reduction(r,sv,stms,x'',ty) => let          of S.E_Reduction(r,sv,reduceStms,x'',ty) => let
74             fun initReduction(r) = (case r             fun initReduction(r) = (case r
75              of S.R_And => S.E_Lit(Literal.Bool(true))              of S.R_All => S.E_Lit(Literal.Bool(true))
76               | S.R_Or => S.E_Lit(Literal.Bool(false))               | S.R_Exists => S.E_Lit(Literal.Bool(false))
| S.R_Xor => S.E_Lit(Literal.Bool(false))
77               | S.R_Min => S.E_Lit(Literal.Float(FloatLit.posInf))               | S.R_Min => S.E_Lit(Literal.Float(FloatLit.posInf))
78               | S.R_Max => S.E_Lit(Literal.Float(FloatLit.negInf))               | S.R_Max => S.E_Lit(Literal.Float(FloatLit.negInf))
79               | S.R_Product => S.E_Lit(Literal.Float(FloatLit.fromInt 1))               | S.R_Product => S.E_Lit(Literal.Float(FloatLit.fromInt 1))
80               | S.R_Sum => S.E_Lit(Literal.Float (FloatLit.fromInt 0))               | S.R_Sum => S.E_Lit(Literal.Float (FloatLit.fromInt 0))
81                 | S.R_Mean => S.E_Lit(Literal.Float (FloatLit.fromInt 0))
82                 | S.R_Variance => S.E_Lit(Literal.Float (FloatLit.fromInt 0))
83               (* end case *))               (* end case *))
84
85             val x' = newGlobalR(reductionToString(r),ty)             val stm = S.S_Assign(lhs,S.E_Reduction(r,sv,reduceStms,x'',ty))
val stm = S.S_Assign(x',S.E_Reduction(r,sv,stms,x'',ty))
86             in             in
87               extraGlobalInit := S.S_Assign(x',initReduction(r)) :: (!extraGlobalInit);                globalReduc := (reduceStms @ [stm])@(!globalReduc);
88                extraGlobals := x'::(!extraGlobals);                NONE
globalReduc := (stms @ [stm])@(!globalReduc);
S.E_Var(x')
89             end             end
90           | _ => e)           | _ => SOME(rhs))
91
92          val prog' =          val prog' =
93            S.Program{            S.Program{
94                  strands = List.map reduceStrand strands ,                  strands = strands,
95                  globals = globals @ (!extraGlobals),                  globals = globals,
96                  globalInit = S.Block(globalInitStms @(!extraGlobalInit)),                  globalInit = globalInit,
97          globalReduc = S.Block(!globalReduc),          globalBlock = reduceBlock(globalBlock),
98            globalReduce = S.Block(!globalReduc),
99                  init = init,                  init = init,
100          inputs = inputs          inputs = inputs
101              }              }

Legend:
 Removed from v.2245 changed lines Added in v.2246

 root@smlnj-gforge.cs.uchicago.edu ViewVC Help Powered by ViewVC 1.0.0