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

SCM Repository

[diderot] View of /branches/charisee/src/compiler/high-il/normalize-ein.sml
ViewVC logotype

View of /branches/charisee/src/compiler/high-il/normalize-ein.sml

Parent Directory Parent Directory | Revision Log Revision Log

Revision 2463 - (download) (annotate)
Fri Oct 11 03:18:53 2013 UTC (6 years, 2 months ago) by cchiw
File size: 18281 byte(s)
structure NormalizeEin = struct


    structure E = Ein
(*    structure P=Printer
    structure O =OrderEin*)

(*Flattens Add constructor: change, expression *)
fun mkAdd [e]=(1,e)
    | mkAdd(e)=let
    fun flatten((i, (E.Add l)::l'))= flatten(1,l@l')
        |flatten(i,((E.Const c):: l'))=
            if (c>0.0 orelse c<0.0) then let
                    val(b,a)=flatten(i,l') in (b,[E.Const c]@a) end
            else flatten(1,l')
        | flatten(i,[])=(i,[])
        | flatten (i,e::l') =  let
                    val(b,a)=flatten(i,l') in (b,[e]@a) end
     val (b,a)=flatten(0,e)
    in case a
     of [] => (1,E.Const(1.0))
                | [e] => (1,e)
                | es => (b,E.Add es)
                (* end case *)

fun mkProd [e]=(1,e)
    | mkProd(e)=let
    fun flatten(i,((E.Prod l)::l'))= flatten(1,l@l')
        |flatten(i,((E.Const c)::l'))=
                if(c>0.0 orelse  0.0>c) then (3,[E.Const 0.0])
                else flatten(i,l')
         | flatten(i,[])=(i,[])
         | flatten (i,e::l') =  let val(a,b)=flatten(i,l') in (a,[e]@b) end
     val (change,a)=flatten(0,e)
     in if(change=3) then (1,E.Const(0.0))
        else case a
        of [] => (1,E.Const(0.0))
        | [e] => (1,e)
        | es => (change, E.Prod es)
        (* end case *)

fun rmEpsIndex(_,_,[])=[]
| rmEpsIndex([],[],cs)=cs
| rmEpsIndex([],m ,e1::cs)=[e1]@rmEpsIndex(m,[],cs)
| rmEpsIndex(i::ix,rest ,(E.V c)::cs)=
    if(i=c) then rmEpsIndex(rest@ix,[],cs)
    else rmEpsIndex(ix,rest@[i],(E.V c)::cs)
(* Transform eps to deltas*)
fun epsToDels(E.Sum(count,E.Prod e))= let
    fun doubleEps((E.Epsilon (a,b,c))::(E.Epsilon(d,e,f))::es,eps,e3)=

        (*Function is called when eps are being changed to deltas*)
        fun createDeltas(i,s,t,u,v, e3)= let

            (*remove index from original index list*)
            val s'= rmEpsIndex([i,s,t,u,v],[],count)
            val s''=[E.V s, E.V t ,E.V u, E.V v]
            val deltas= E.Sub(
                    E.Sum(s'',E.Prod([E.Delta(E.V s,E.V u), E.Delta(E.V t,E.V v)] @e3)),
                    E.Sum(s'',E.Prod([E.Delta(E.V s,E.V v), E.Delta(E.V t,E.V u)]@e3)))
                in (case (eps,es,s')
                of ([],[],[]) =>(1,deltas)
                |([],_,[]) =>(1,E.Prod( es@[deltas]))
                |_ =>(1, E.Sum(s', E.Prod(eps@es@[deltas])))
        in if(a=d) then createDeltas(a,b,c,e,f, e3)
           else if(a=e) then createDeltas(a,b,c,f,d, e3)
           else if(a=f) then createDeltas(a,b,c,d,e, e3)
           else if(b=d) then createDeltas(b,c,a,e,f, e3)
           else if(b=e) then createDeltas(b,c,a,f,d,e3)
           else if(b=f) then createDeltas(b,c,a,d,e,e3)
           else if(c=d) then createDeltas(c,a,b,e,f,e3)
           else if(c=e) then createDeltas(c,a,b,f,d,e3)
           else if(c=f) then createDeltas(c,a,b,d,e,e3)
           else (0,E.Const 0.0)
    fun findeps(e,[])= (e,[])
      | findeps(e,(E.Epsilon eps)::es)=  findeps(e@[E.Epsilon eps],es)
      | findeps(e,es)= (e, es)
    fun dist([],eps,rest)=(0,eps,rest)
     | dist([e],eps,rest)=(0,eps@[e],rest)
     | dist(c1::current,eps,rest)=let
            val(i, exp)= doubleEps(c1::current,eps,rest)
        in  (case i of 1=>(i,[exp],[E.Const 2.0])
            |_=> dist(current, eps@[c1],rest))
    val (es,rest)=findeps([],e)

fun rmIndex(_,_,[])=[]
    | rmIndex([],[],cs)=cs
    | rmIndex([],m ,e1::cs)=[e1]@rmIndex(m,[],cs)
    | rmIndex(i::ix,rest ,c::cs)=
        if(i=c) then rmIndex(rest@ix,[],cs)
        else rmIndex(ix,rest@[i],c::cs)
(* Apply deltas to tensors/fields*)
fun reduceDelta(E.Sum(c,E.Prod p))=let
    fun findDeltas(dels,rest,E.Delta d::es)= findDeltas(dels@[E.Delta d], rest, es)
    | findDeltas(dels,rest,E.Epsilon eps::es)=findDeltas(dels,rest@[E.Epsilon eps],es)
    | findDeltas(dels,rest,es)=  (dels,rest,es)

    fun distribute(change,d,dels,[],done)=(change,dels@d,done)
    | distribute(change,[],[],e,done)=(change,[],done@e)
    | distribute(change,E.Delta(i,j)::ds,dels,E.Tensor(id,[tx])::es,done)=
        if(j=tx) then distribute(change@[j],dels@ds,[] ,es ,done@[E.Tensor(id,[i])])
        else distribute(change,ds,dels@[E.Delta(i,j)],E.Tensor(id,[tx])::es,done)
    | distribute(change,E.Delta(i,j)::ds,dels,E.Field(id,[tx])::es,done)=
        if(j=tx) then distribute(change@[j],dels@ds,[] ,es ,done@[E.Field(id,[i])])
        else distribute(change,ds,dels@[E.Delta(i,j)],E.Field(id,[tx])::es,done)
    | distribute(change,d,dels,e::es,done)=distribute(change,dels@d,[],es,done@[e])
    val (dels,eps,es)=findDeltas([],[],p)
    val (change,dels',done)=distribute([],dels,[],es,[])
    val index=rmIndex(change,[],c)
       (change, E.Sum(index,E.Prod (eps@dels'@done)))

fun mkApplySum(E.Apply(E.Partial d,E.Sum(c,e)))=(print "apply sum";case e
    of E.Tensor(a,[])=>(1,E.Const 0.0)
    | E.Const _ =>(1,E.Const 0.0)
    | E.Add l => (1,E.Add(List.map (fn e => E.Apply(E.Partial d, E.Sum(c,e))) l))
    | E.Sub(e2, e3) =>(1, E.Sub(E.Apply(E.Partial d, E.Sum(c,e2)), E.Apply(E.Partial d, E.Sum(c,e3))))
    | E.Prod [e1]=>(1,E.Apply(E.Partial d,E.Sum(c,e1)))
    | E.Prod(E.Tensor(a,[])::e1::[])=>(1,E.Prod[E.Tensor(a,[]),E.Apply(E.Partial d,E.Sum(c,e1))])
    | E.Prod(E.Tensor(a,[])::e2)=>(1,E.Prod[E.Tensor(a,[]),E.Apply(E.Partial d,E.Sum(c,E.Prod e2))])
    | E.Prod es=>(let
        fun prod [e1] =E.Apply(E.Partial d,e1)
        | prod (E.Epsilon eps1::es) = (E.Apply(E.Partial d, E.Prod (E.Epsilon eps1::es)))
        | prod (E.Delta e1::es) = (E.Apply(E.Partial d, E.Prod (E.Delta e1::es)))
        | prod (E.Prod e1::es)=prod(e1@es)
        | prod(e1::e2)=(let
            val l= prod(e2)
            val (_, a)= mkProd[e1,l]
            val lr=e2 @[E.Apply(E.Partial d,e1)]
            val(_,b) =mkProd lr
            in  E.Add[b,a]
        val chainrule=prod es
        in (1,E.Sum(c, chainrule)) end)
    |_=>(0,E.Apply(E.Partial d,E.Sum(c,e)))
    (* end case*))
fun mkApply2(E.Apply(E.Partial d,e))=(print "aa";case e
    of E.Tensor(a,[])=>(1,E.Const 0.0)
    | E.Const _ =>(1,E.Const 0.0)
    | E.Add l => (1,E.Add(List.map (fn e => E.Apply(E.Partial d, e)) l))
    | E.Sub(e2, e3) =>(1, E.Sub(E.Apply(E.Partial d, e2), E.Apply(E.Partial d, e3)))
    | E.Apply(E.Partial e1,e2)=>(1,E.Apply(E.Partial(d@e1), e2))
    | E.Prod [e1]=>(1,E.Apply(E.Partial d,e1))
    | E.Prod(E.Tensor(a,[])::e1::[])=>(1,E.Prod[E.Tensor(a,[]),E.Apply(E.Partial d,e1)])
    | E.Prod(E.Tensor(a,[])::e2)=>(1,E.Prod[E.Tensor(a,[]),E.Apply(E.Partial d,E.Prod e2)])
    | E.Prod es=> (let
        fun prod [e1] =(0,E.Apply(E.Partial d,e1))
        | prod (E.Epsilon eps1::es) = (0,E.Apply(E.Partial d, E.Prod (E.Epsilon eps1::es)))
        | prod (E.Delta e1::es) = (0,E.Apply(E.Partial d, E.Prod (E.Delta e1::es)))
         | prod (E.Prod e1::es)=prod(e1@es)
        | prod(E.Tensor t::e2)=(let
            val (_,l)= prod(e2) val m= E.Prod[E.Tensor t,l]
            val lr=e2 @[E.Apply(E.Partial d,E.Tensor t)] val(b,a) =mkProd lr
            in  (1,E.Add[a,m])
        | prod(E.Field f::e2)=(let
            val (_,l)= prod(e2) val m= E.Prod[E.Field f,l]
            val lr=e2 @[E.Apply(E.Partial d,E.Field f)] val(b,a) =mkProd lr
            in  (1,E.Add[a,m])
        | prod e = (0,E.Apply(E.Partial d, E.Prod e))

        val (a,b)= prod es
        in (a, b) end)
    |_=>(0,E.Apply(E.Partial d,e))
    (* end case*))
fun mkSumApply2(E.Sum(c,E.Apply(E.Partial d,e)))=(print "in here ";case e
    of E.Const _=>(1,E.Const 0.0)
    | E.Tensor(_,[])=> (1,E.Const 0.0) 
    | E.Field _=>(0,E.Sum(c,E.Apply(E.Partial d,e)))
    | E.Apply(E.Partial e1,e2)=>(1,E.Sum(c,E.Apply(E.Partial(d@e1),e2)))
    | E.Add l => (1,E.Add(List.map (fn e => E.Sum(c,E.Apply(E.Partial d, e))) l))
    | E.Sub(e2, e3) =>
                (print "sub";(1, E.Sub(E.Sum(c,E.Apply(E.Partial d, e2)), E.Sum(c,E.Apply(E.Partial d, e3)))))
     | E.Prod [e1]=>(print "one";(1,E.Sum(c,E.Apply(E.Partial d,e1))))
    | E.Prod(E.Tensor(a,[])::e2::[])=>("in scalar";(1, E.Prod[E.Tensor(a,[]),E.Sum(c,E.Apply(E.Partial d,e2))]))
    | E.Prod(E.Tensor(a,[])::e2)=>("in scalar";(1, E.Prod[E.Tensor(a,[]),E.Sum(c,E.Apply(E.Partial d,E.Prod e2))]))
    | E.Prod es =>(print "in prod";let
        fun prod (change,rest, sum,partial,[]) = (change,E.Sum(sum,E.Apply(E.Partial partial,E.Prod rest)))
        | prod (change,rest, sum,partial,E.Epsilon(i,j,k)::ps)= let
            fun matchprod(2,_,_,_)= 1 (*matched 2*)
            | matchprod(num,_,_,[])=0
            | matchprod(0,_,_,[eps])=0
            | matchprod(num,[],rest,eps::epsx)=
            | matchprod(num,E.V p::px,rest,eps::epsx)=
                if(p=eps) then (matchprod(num+1,rest@px,[],epsx))
                else matchprod(num,px,rest@[E.V p], eps::epsx)
            | matchprod(num,p::px,rest,eps)=
            val change'= matchprod(0,d,[],[i,j,k])
            in (case change'
                of 1 => (1,E.Const 0.0)
                | _ =>prod(change,rest@[E.Epsilon(i,j,k)],sum,partial,ps)
                (*end case*))
        | prod (change,rest, sum,partial,E.Delta(i,j)::ps)=let
            fun applyDelPartial([],_)=(0,[])
            | applyDelPartial(p::px,r)=
                if(j=p) then (1,r@[i]@px)
                else  applyDelPartial(px,r@[p])
            val (change',px)=applyDelPartial(d,[])
            in (case change'
                of 1 => (let val index=rmIndex([j],[],sum)
                    in prod(1,rest, index,px, ps) end )
                | _ => prod(change,rest@[E.Delta(i,j)], sum,partial, ps)
                (*end case*)) end
        | prod (change,rest,sum, partial,e::es)= prod(change,rest@[e],sum,partial,es)
        val (change,exp) = prod(0,[],c, d, es)
        | _=>(print "nope";(0,E.Sum(c,E.Apply(E.Partial d,e))))
        (* end case*))
    try E.Sum(c,e)=> E.Sum(c',e')
    ==>    E.Sum(c',E.Apply(d,e'))
        E.Apply(d,e')=> E.Apply(d',e'')
(*Apply normalize to each term in product list
or Apply normalize to tail of each list*)
fun normalize (Ein.EIN{params, index, body}) = let
      val changed = ref false
      fun rewriteBody body = (case body
             of E.Const _=> body
              | E.Tensor _ =>body
              | E.Field _=> body
              | E.Kernel _ =>body
              | E.Delta _ => body
              | E.Value _ =>body
              | E.Epsilon _=>body
              | E.Neg e => E.Neg(rewriteBody e)
              | E.Add es => let val (change,body')= mkAdd(List.map rewriteBody es)
                   in if (change=1) then ( changed:=true;body') else body' end
              | E.Sub (a,b)=>  E.Sub(rewriteBody a, rewriteBody b)
              | E.Div (a, b) => E.Div(rewriteBody a, rewriteBody b)     
              | E.Partial _=>body
              | E.Conv (V, alpha)=> E.Conv(rewriteBody V, alpha)
              | E.Probe(u,v)=>  E.Probe(rewriteBody u, rewriteBody v)
              | E.Image es => E.Image(List.map rewriteBody es)
              | E.Prod [e1] => rewriteBody e1
              | E.Prod(e1::(E.Add(e2))::e3)=>
                   (changed := true; E.Add(List.map (fn e=> E.Prod([e1, e]@e3)) e2))
              | E.Prod(e1::(E.Sub(e2,e3))::e4)=>
                   (changed :=true; E.Sub(E.Prod([e1, e2]@e4), E.Prod([e1,e3]@e4 )))
              | E.Prod [E.Partial r1,E.Conv(f,deltas)]=>
              | E.Prod (E.Partial r1::E.Conv(f,deltas)::ps)=>
                    let val (change,e)=mkProd([E.Conv(f,deltas@r1)]@ps)
                    in e end)
              | E.Prod[(E.Epsilon(e1,e2,e3)), E.Tensor(_,[E.V i1,E.V i2])]=>
                    if(e2=i1 andalso e3=i2) then (changed :=true;E.Const(0.0))
                    else body
             | E.Prod [E.Partial r1, E.Tensor(_,[])]=> (changed:=true;E.Const(0.0))
             | E.Prod(E.Partial r1::E.Partial r2::p)=>
             | E.Prod [E.Partial _, _] =>body
             | E.Prod (E.Partial p1::es)=> (let
                fun prod [e1] =E.Apply(E.Partial p1,e1)
                | prod(e1::e2)=(let
                    val l= prod(e2) val m= E.Prod[e1,l]
                    val lr=e2 @[E.Apply(E.Partial p1,e1)] val(b,a) =mkProd lr
                    in  E.Add[a,m]
                in (changed:=true;prod es) end)
              | E.Prod(E.Sum(c1,E.Prod(E.Epsilon e1::es1))::E.Sum(c2,E.Prod(E.Epsilon e2::es2))::es)=>let
                  val (change,e,rest)=epsToDels(E.Sum(c1@c2, E.Prod([E.Epsilon e1, E.Epsilon e2]@es1@es2@es)))
                  in(case (change,e, rest)
                    of (1,[e1],_)=> e1
                    | _=>body)
              | E.Prod(e::es)=>let
                    val e'=rewriteBody e
                    val e2=rewriteBody(E.Prod es)
                    val(a,b)=(case e2 of E.Prod p'=> mkProd([e']@p')
                        |_=>mkProd [e',e2])
                    in b 
              | E.Apply(E.Partial d,E.Sum(c,e))=>let
                    val(c,e')=mkApplySum(E.Apply(E.Partial d,E.Sum(c, rewriteBody e)))
                    val e''=(case e'
                        of E.Apply(d,E.Sum s)=>E.Apply(d,rewriteBody(E.Sum s))
                        |_=> e')
                in (print "bb";case c of 1=>(changed:=true;e'')
                    |_=> e'')end
              | E.Apply(E.Partial [],e)=> e
              | E.Apply(E.Partial p, e)=>let
                    val body'=E.Apply(E.Partial p, rewriteBody e)
                    val (c, e')=mkApply2(body')
                in (case c of 1=>(changed:=true;e')
                    | _ =>e') end
              | E.Apply(e1,e2)=>E.Apply(rewriteBody e1, rewriteBody e2)
              (* Sum *)
              | E.Sum([],e)=> (changed:=true;rewriteBody e)
              | E.Sum(_,E.Const c)=>(changed:=true;E.Const c)
              | E.Sum(c,(E.Add l))=> (changed:=true;E.Add(List.map (fn e => E.Sum(c,e)) l))
              | E.Sum(c,E.Sub(e1,e2))=>(changed:=true; E.Sub(E.Sum(c,e1),E.Sum(c,e2)))
              | E.Sum(c,E.Prod(E.Epsilon eps1::E.Epsilon eps2::ps))=>
                   let val (i,e,rest)=epsToDels(body)
                in (print "eps to dels \n ";case (i, e,rest)
                of (1,[e1],r) =>(print "changed\n";changed:=true;e1)
                |(0,eps,[])=>(print "non";body)
                            val p'=rewriteBody(E.Prod rest)
                            val p''= (case p' of E.Prod p=>p |e=>[e])
                            val(a,b)= mkProd (eps@p'')
                            in E.Sum(c,b) end
              | E.Sum(c1,E.Prod(E.Epsilon eps1::E.Sum(c2,E.Prod(E.Epsilon eps2::s2))::ps))=>let 
                    val (i, e, rest)=epsToDels(E.Sum(c1@c2, E.Prod([E.Epsilon eps1, E.Epsilon eps2]@ s2@ps)))
                  in (case (i,e,rest)
                of (1,[e1],_) =>(changed:=true; e1)
                    |_ => E.Sum(c1,rewriteBody( E.Prod(E.Epsilon eps1::E.Sum(c2,E.Prod(E.Epsilon eps2::s2))::ps)))
                    (* end case*))
              | E.Sum(c, E.Prod(E.Delta d::es))=>let
                    val (change,a)=reduceDelta(body)
                    val (change',body')=(case a
                        of E.Prod p=> mkProd p
                        |_=> (0,a))
                   in (case change of []=>body'|_=>(changed:=true;body')) end
              | E.Sum(c,E.Apply(E.Partial _,e))=>let
                    val (change,exp)=mkSumApply2(body)
                    val exp'=(case exp
                        of  E.Const c => E.Const c
                        | E.Sum(c',E.Apply(d',e'))  => (let
                            val s'=rewriteBody(E.Sum(c',e'))
                           in (case s'
                                of E.Sum([],e'')=> rewriteBody (E.Apply(d',e''))
                                | E.Sum(s'',e'') => E.Sum(s'',rewriteBody(E.Apply(d',e'')))
                                | _ => E.Apply(d',s'))

                        | _ =>exp
                        (* end case *))
                in (case change of 1=>(changed:=true;exp') |_=>exp')

              | E.Sum(c,e)=>E.Sum(c,rewriteBody e)

            (*end case*))

      fun loop body = let
            val body' = rewriteBody body
              if !changed
                then (changed := false;(*print(P.printbody body');*) print "\n => \n" ;loop body')
                else body'
    val b = loop body
    ((Ein.EIN{params=params, index=index, body=b}))

end (* local *)

ViewVC Help
Powered by ViewVC 1.0.0