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

SCM Repository

[diderot] Annotation of /branches/vis15/src/compiler/high-opt/eps-util.sml
ViewVC logotype

Annotation of /branches/vis15/src/compiler/high-opt/eps-util.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3520 - (view) (download)

1 : jhr 3515 (* eps-util.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     (* Helper functions for normalize-ein_exp
10 :     * does the rewriting for Epsilon and deltas
11 :     *)
12 :     structure EpsUtil : sig
13 :    
14 :     val epsToDels : Ein.ein_exp list
15 :     -> bool * Ein.ein_exp * Ein.sumrange list * Ein.ein_exp list * Ein.ein_exp list
16 :    
17 :     val reduceDelta : Ein.ein_exp list * Ein.ein_exp list * Ein.ein_exp list
18 :     -> bool * Ein.ein_exp
19 :    
20 :     val matchEps : Ein.mu list * Ein.index_id list -> bool
21 :    
22 :     end = struct
23 :    
24 :     structure E = Ein
25 :    
26 :     fun err str = raise Fail (String.concat["Ill-formed EIN Operator",str])
27 :    
28 :     (* check to see if a pair of Epsilon expressions can be converted to deltas *)
29 :     fun doubleEps (E.Epsilon(a,b,c), E.Epsilon(d,e,f)) = let
30 :     fun createDeltas (s, t, u, v) =
31 :     (* FIXME: what does the following comment mean? *)
32 :     (*If multiple summation ids are the same, it might not remove from the summation queue*)
33 :     if (s=u andalso t=v) orelse (s=v andalso t=u)
34 :     then NONE
35 :     else let
36 :     val d1 = [E.Delta(E.V s, E.V u), E.Delta(E.V t, E.V v)]
37 :     val d2 = [E.Delta(E.V s, E.V v), E.Delta(E.V t, E.V u)]
38 :     in
39 :     SOME(d1, d2)
40 :     end
41 :     in
42 :     if (a=d) then createDeltas(b, c, e, f)
43 :     else if (a=e) then createDeltas(b, c, f, d)
44 :     else if (a=f) then createDeltas(b, c, d, e)
45 :     else if (b=d) then createDeltas(c, a, e, f)
46 :     else if (b=e) then createDeltas(c, a, f, d)
47 :     else if (b=f) then createDeltas(c, a, d, e)
48 :     else if (c=d) then createDeltas(a, b, e, f)
49 :     else if (c=e) then createDeltas(a, b, f, d)
50 :     else if (c=f) then createDeltas(a, b, d, e)
51 :     else NONE
52 :     end
53 :     | doubleEps _ = raise Fail "None Epsilon Argument"
54 :    
55 :    
56 :     (* distributeEps (epsAll, sumexp)
57 :     * ???
58 :     *)
59 :     (*distributeEps:ein_exp list* ein_exp list
60 :     -> int*ein_exp list * ein_exp list *ein_exp list *sum_index_id list
61 :     * ein_exp list
62 :     *epsAll is a list of epsilons
63 :     *sumExp is a summation expression with epsilon
64 :     * Before this function, there is call to filter all the epislons
65 :     * in a product expression.
66 :     *Sometimes a matching epislon can be hidden in a SummationExp, so that makes the search a little more complicated
67 :     *The arguments to this function are either
68 :     *(1) a list of epsilons in epsAll
69 :     *(2) or a single item in both epsAll and sumExp.
70 :     *In case (1), We check two epislons at a time for a match
71 :     *If there is match then we return unused epsilon list, and rewriten deltas pairs
72 :     *In case (2) We check the embedded eps for a match and if we find one return sum_ids and the rest of the product
73 :     *)
74 :     fun distributeEps (epsAll, sumexp) = let
75 :     fun distEps ([], _) = NONE
76 :     | distEps ([e1], eps) = (case sumexp
77 :     of [E.Sum(sx, E.Opn(E.Prod, e2::ps))] => (case doubleEps(e1, e2)
78 :     of SOME(d1, d2) => SOME(eps, d1, d2, sx, ps)
79 :     | NONE => NONE
80 :     (* end case *))
81 :     | _ => NONE
82 :     (* end case *))
83 :     | distEps (e1::e2::current,eps) = (case doubleEps(e1, e2)
84 :     of SOME(d1, d2) => SOME(eps@current, d1, d2, [], sumexp)
85 :     | _ => distEps(e2::current, eps@[e1])
86 :     (* end case *))
87 :     in
88 :     distEps (epsAll, [])
89 :     end
90 :    
91 :    
92 :     (*epsToDels:ein_exp list
93 :     -> int*ein_exp * sum_index_id*ein_exp list *ein_exp list
94 :     *Looks at ein_exp list and searches for epsilons
95 :     *calls above function to look for transition
96 :     *return: change, e', sx
97 :     *)
98 :     fun epsToDels exps = let
99 : jhr 3520 val (epsAll, rest, sumexp) = EinFilter.filterEps exps
100 : jhr 3515 in
101 :     case distributeEps(epsAll, sumexp)
102 :     of NONE => (false, E.Const 0, [], epsAll, rest@sumexp)
103 :     | SOME(epsUnused, d1, d2, sx, ps) => let
104 :     val a = E.Opn(E.Prod, epsUnused@d1@rest@ps)
105 :     val b = E.Opn(E.Prod, epsUnused@d2@rest@ps)
106 :     in
107 :     (true, E.Op2(E.Sub, a, b), sx, [], [])
108 :     end
109 :     (*end case *)
110 :     end
111 :    
112 :     (*reduceDelta:ein_exp*ein_exp* ein_exp list ->int *ein_exp
113 :     *Apply deltas to tensors/fields
114 :     *returns change or rewriten ein_exp
115 :     *
116 :     * distPart:mu list*mu*mu* mu list
117 :     * looks for match with index_id and delta
118 :     * current delta is Delta_{i,j},Differentiation has index_ids pd
119 :     *
120 :     * distribute:int*ein_exp list*ein_exp list *ein_exp list*ein_exp list
121 :     * distribute deltas of ein_exp list
122 :     *)
123 :     fun reduceDelta (eps, dels, es)=let
124 :     fun appDel (changed, [], beta, mu, nu) = (changed, beta, mu@nu)
125 :     | appDel (changed, a::alpha, beta, [], nu) = appDel(changed, alpha, beta@[a], nu, [])
126 :     | appDel (changed, a::alpha, beta, E.Delta(i, j)::mu, nu) =
127 :     if (a = j)
128 :     then appDel(true, alpha, beta@[i], mu, nu)
129 :     else appDel(changed, a::alpha, beta, mu, nu@[E.Delta(i, j)])
130 :     (* *)
131 :     (* FIXME: instead of appending singletons, build results in reverse order! *)
132 :     fun distribute (changed, [], rest, mu) = (changed, E.Opn(E.Prod, eps@mu@rest))
133 :     | distribute (changed, p1::ps, rest, mu) = (case p1
134 :     of E.Tensor(id, alpha) => let
135 :     val (changed, alpha_betas, nu) = appDel(changed, alpha, [], mu, [])
136 :     in
137 :     distribute(changed, ps, rest@[E.Tensor(id, alpha_betas)], nu)
138 :     end
139 :     | E.Field(id, alpha) => let
140 :     val (changed, alpha_betas, nu) = appDel(changed, alpha, [], mu, [])
141 :     in
142 :     distribute(changed, ps, rest@[E.Field(id, alpha_betas)], nu)
143 :     end
144 :     | E.Conv(v, alpha, h, dx) => let
145 :     val (changed, alpha_betas, nu1) = appDel(changed, alpha, [] ,mu, [])
146 :     val (changed, dx_betas, nu2) = appDel(changed, dx, [], nu1, [])
147 :     in
148 :     distribute(changed, ps, rest@[E.Conv(v,alpha_betas, h ,dx_betas)], nu2)
149 :     end
150 :     | E.Probe(E.Conv(v, alpha, h, dx), t) => let
151 :     val (changed, alpha_betas, nu1) = appDel(changed, alpha, [], mu, [])
152 :     val (changed, dx_betas, nu2) = appDel(changed, dx, [], nu1, [])
153 :     in
154 :     distribute(
155 :     changed, ps, rest@[E.Probe(E.Conv(v,alpha_betas, h ,dx_betas),t)], nu2)
156 :     end
157 :     | E.Apply(E.Partial d, e) => let
158 :     val (changed, d_betas, nu) = appDel(changed,d,[],mu,[])
159 :     in
160 :     distribute(changed, ps, rest@[E.Apply(E.Partial d_betas,e)],nu)
161 :     end
162 :     | _ => distribute(changed, ps, rest@[p1], mu)
163 :     (* end case *))
164 :     in
165 :     distribute (false, es, [], dels)
166 :     end
167 :    
168 :     (*matchEps:int*mu list *mu list * index_id list->int
169 :     *looks to see if two indices in delta(px) matches 2 in epsilon ids
170 :     *if so returns 1 so the ein_exp becomes 0
171 :     *)
172 :     (* FIXME: rewrite once we understand what this is supposed to be doing *)
173 :     fun matchEps (mus, epsx) = let
174 :     fun match (2, _, _, _) = true (* matched two *)
175 :     | match (_, _, _, []) = false
176 :     | match (0, _, _, [_]) = false
177 :     | match (num, [], rest, eps::epsx) = match (num, rest, [], epsx)
178 :     | match (num, E.V p :: px, rest, eps::epsx) =
179 :     if (p = eps)
180 :     then match (num+1, rest@px, [], epsx)
181 :     else match (num, px, rest@[E.V p], eps::epsx)
182 :     | match (num, p::px, rest, eps) = match (num, px, rest, eps)
183 :     in
184 :     match (0, mus, [], epsx)
185 :     end
186 :    
187 :     end

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