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

SCM Repository

[diderot] Annotation of /branches/chiw17/src/compiler/mid-to-low/ein-to-scalar.sml
ViewVC logotype

Annotation of /branches/chiw17/src/compiler/mid-to-low/ein-to-scalar.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5014 - (view) (download)

1 : jhr 3646 (* ein-to-scalar.sml
2 :     *
3 :     * Generate LowIR scalar computations that implement Ein expressions.
4 :     *
5 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
6 :     *
7 :     * COPYRIGHT (c) 2016 The University of Chicago
8 :     * All rights reserved.
9 :     *)
10 :    
11 :     structure EinToScalar : sig
12 :    
13 : jhr 3728 (* expand a scalar-valued Ein operator application to LowIR code; return the LowIR
14 :     * vaiable that holds the result of the application (the assignments will be added
15 :     * to avail).
16 :     *)
17 :     val expand : {
18 : jhr 4317 avail : AvailRHS.t, (* the generated LowIR assignments *)
19 :     mapp : int IntRedBlackMap.map, (* mapping from deBruijn indices to argument IDs *)
20 :     body : Ein.ein_exp, (* the EIN operator body *)
21 :     lowArgs : LowIR.var list (* corresponding LowIR arguments *)
22 :     } -> LowIR.var
23 : jhr 3646
24 :     end = struct
25 :    
26 :     structure IR = LowIR
27 :     structure Ty = LowTypes
28 :     structure Op = LowOps
29 :     structure Var = LowIR.Var
30 :     structure E = Ein
31 : jhr 3648 structure Mk = MkLowIR
32 : jhr 3646 structure IMap = IntRedBlackMap
33 :    
34 : jhr 3653 fun mapIndex (mapp, id) = (case IMap.find(mapp, id)
35 : jhr 4317 of SOME x => x
36 :     | NONE => raise Fail(concat["mapIndex(_, V ", Int.toString id, "): out of bounds"])
37 :     (* end case *))
38 : jhr 3649
39 : jhr 3746 fun expand {avail, mapp, body, lowArgs} = let
40 : cchiw 5014 (*
41 :     val i = Mk.lookupMu (mapp, E.V 0)
42 :     val _ = print(String.concat["\nin direction i:",Int.toString(i)])*)
43 :     (*
44 :     val j = Mk.lookupMu (mapp, E.V 1)
45 :     val _ = print(String.concat["\nin direction i:",Int.toString(i),"-",Int.toString(j)])
46 :     *)
47 : jhr 4317 fun gen (mapp, body) = let
48 :     (*********sumexpression ********)
49 :     fun tb n = List.tabulate (n, fn e => e)
50 :     fun sumCheck (mapp, (v, lb, ub) :: sumx, e) = let
51 :     fun sumloop mapp = gen (mapp, e)
52 :     fun sumI1 (left, (v, [i], lb1), [], rest) = let
53 :     val mapp = IMap.insert (left, v, lb1+i)
54 :     val vD = gen (mapp, e)
55 :     in
56 :     rest@[vD]
57 :     end
58 :     | sumI1 (left, (v, i::es, lb1), [], rest) = let
59 :     val mapp = IMap.insert (left, v, i+lb1)
60 :     val vD = gen (mapp, e)
61 :     in
62 :     sumI1 (mapp, (v, es, lb1), [], rest@[vD])
63 :     end
64 :     | sumI1 (left, (v, [i], lb1), (a, lb2, ub2) ::sx, rest) =
65 :     sumI1 (IMap.insert (left, v, lb1+i), (a, tb (ub2-lb2+1), lb2), sx, rest)
66 :     | sumI1 (left, (v, s::es, lb1), (a, lb2, ub2) ::sx, rest) = let
67 :     val mapp = IMap.insert (left, v, s+lb1)
68 :     val xx = tb (ub2-lb2+1)
69 :     val rest' = sumI1 (mapp, (a, xx, lb2), sx, rest)
70 :     in
71 :     sumI1 (mapp, (v, es, lb1), (a, lb2, ub2) ::sx, rest')
72 :     end
73 :     | sumI1 _ = raise Fail "None Variable-index in summation"
74 :     in
75 : jhr 3745 sumI1 (mapp, (v, tb (ub-lb+1), lb), sumx, [])
76 : jhr 4317 end
77 :     in
78 :     case body
79 :     of E.Value v => Mk.intToRealLit (avail, mapIndex (mapp, v))
80 :     | E.Const c => Mk.intToRealLit (avail, c)
81 :     | E.Delta(i, j) => Mk.delta (avail, mapp, i, j)
82 :     | E.Epsilon(i, j, k) => Mk.epsilon3 (avail, mapp, i, j, k)
83 :     | E.Eps2(i, j) => Mk.epsilon2 (avail, mapp, i, j)
84 :     | E.Tensor(id, ix) => Mk.tensorIndex (avail, mapp, List.nth(lowArgs, id), ix)
85 : cchiw 4555 | E.Zero _ => Mk.intToRealLit (avail, 0)
86 : jhr 4317 | E.Op1(op1, e1) => let
87 :     val arg = gen (mapp, e1)
88 :     in
89 :     case op1
90 :     of E.Neg => Mk.realNeg (avail, arg)
91 :     | E.Sqrt => Mk.realSqrt (avail, arg)
92 :     | E.Cosine => Mk.realCos (avail, arg)
93 :     | E.ArcCosine => Mk.realArcCos (avail, arg)
94 :     | E.Sine => Mk.realSin (avail, arg)
95 :     | E.ArcSine => Mk.realArcSin (avail, arg)
96 :     | E.Tangent => Mk.realTan (avail, arg)
97 :     | E.ArcTangent => Mk.realArcTan (avail, arg)
98 : cchiw 3969 | E.Exp => Mk.realExp (avail, arg)
99 : jhr 4317 | E.PowInt n => Mk.intPow (avail, arg, n)
100 : cchiw 5001 | E.Abs => Mk.realAbs(avail, arg)
101 : jhr 4317 (* end case *)
102 :     end
103 :     | E.Op2(E.Sub, e1, e2) => Mk.realSub (avail, gen (mapp, e1), gen (mapp, e2))
104 :     | E.Opn(E.Add, es) =>
105 :     Mk.reduce (avail, Mk.realAdd, List.map (fn e => gen(mapp, e)) es)
106 :     | E.Opn(E.Prod, es) =>
107 :     Mk.reduce (avail, Mk.realMul, List.map (fn e => gen(mapp, e)) es)
108 :     | E.Op2(E.Div, e1 as E.Tensor (_, [_]), e2 as E.Tensor (_, [])) =>
109 :     gen (mapp, E.Opn(E.Prod, [E.Op2 (E.Div, E.Const 1, e2), e1]))
110 :     | E.Op2(E.Div, e1, e2) => Mk.realDiv (avail, gen (mapp, e1), gen (mapp, e2))
111 :     | E.Sum(sx, E.Opn(E.Prod, (img as E.Img _) :: (kargs as (E.Krn _ :: _)))) =>
112 :     FieldToLow.expand {
113 :     avail = avail, mapp = mapp,
114 :     sx = sx, img = img, krnargs = kargs,
115 :     args = lowArgs
116 :     }
117 :     | E.Sum(sumx, e) =>
118 :     Mk.reduce (avail, Mk.realAdd, sumCheck (mapp, sumx, e))
119 :     | E.Probe(E.Epsilon e1, e2) => gen(mapp,E.Epsilon e1)
120 :     | E.Probe(E.Eps2 e1, e2) => gen(mapp,E.Eps2 e1)
121 :     | E.Probe(E.Const e1, e2) => gen(mapp, E.Const e1)
122 :     | E.Probe(E.Delta e1, e2) => gen(mapp, E.Delta e1)
123 :     | E.Probe e => raise Fail("probe ein-exp: " ^ EinPP.expToString body)
124 :     | E.Field _ => raise Fail("field should have been replaced: " ^ EinPP.expToString body)
125 : cchiw 5014 | E.Poly(id, alpha, n, []) => let
126 :     val t = E.Tensor(id, alpha)
127 :     val ts = List.tabulate (n, fn _ => t)
128 :     in gen(mapp, E.Opn(E.Prod, ts)) end
129 :     | E.Poly(id, [], 1, [vx]) => gen(mapp, E.Const 1) (* derivative of a scalar? *)
130 :     | E.Poly(id, [E.C c], n, [vx]) => let
131 : cchiw 5010 val dx = Mk.lookupMu (mapp, vx)
132 : cchiw 5014 val _ = print(String.concat["\n\t Poly1_", Int.toString(c),"^",Int.toString(n),"-dx",Int.toString(dx)])
133 :     val ec = E.Const n
134 :     val t = E.Tensor(id, [E.C c])
135 :     val ts = List.tabulate (n-1, fn _ => t)
136 :     val e = if(dx=c)
137 :     then (case n
138 :     of 1 => E.Const 1
139 :     | _ => E.Opn(E.Prod, ec::ts)
140 :     (* end case *))
141 :     else E.Const 0
142 : cchiw 5010 (* here need to implement partial derivative in respect to an axis *)
143 :     in gen(mapp, e) end
144 : jhr 4317 | _ => raise Fail("unsupported ein-exp: " ^ EinPP.expToString body)
145 :     (*end case*)
146 :     end
147 :     in
148 :     gen (mapp, body)
149 :     end
150 : jhr 3646
151 :     end

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