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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3943 - (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 :     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 3649 of SOME x => x
36 :     | NONE => raise Fail(concat["mapIndex(_, V ", Int.toString id, "): out of bounds"])
37 :     (* end case *))
38 :    
39 : jhr 3746 fun expand {avail, mapp, body, lowArgs} = let
40 : jhr 3653 fun gen (mapp, body) = let
41 :     (*********sumexpression ********)
42 :     fun tb n = List.tabulate (n, fn e => e)
43 : jhr 3745 fun sumCheck (mapp, (E.V v, lb, ub) :: sumx, e) = let
44 : jhr 3653 fun sumloop mapp = gen (mapp, e)
45 :     fun sumI1 (left, (v, [i], lb1), [], rest) = let
46 : cchiw 3741 val mapp = IMap.insert (left, v, lb1+i)
47 : jhr 3653 val vD = gen (mapp, e)
48 :     in
49 :     rest@[vD]
50 :     end
51 :     | sumI1 (left, (v, i::es, lb1), [], rest) = let
52 :     val mapp = IMap.insert (left, v, i+lb1)
53 :     val vD = gen (mapp, e)
54 :     in
55 :     sumI1 (mapp, (v, es, lb1), [], rest@[vD])
56 :     end
57 : jhr 3745 | sumI1 (left, (v, [i], lb1), (E.V a, lb2, ub2) ::sx, rest) =
58 :     sumI1 (IMap.insert (left, v, lb1+i), (a, tb (ub2-lb2+1), lb2), sx, rest)
59 : jhr 3653 | sumI1 (left, (v, s::es, lb1), (E.V a, lb2, ub2) ::sx, rest) = let
60 :     val mapp = IMap.insert (left, v, s+lb1)
61 :     val xx = tb (ub2-lb2+1)
62 :     val rest' = sumI1 (mapp, (a, xx, lb2), sx, rest)
63 :     in
64 :     sumI1 (mapp, (v, es, lb1), (E.V a, lb2, ub2) ::sx, rest')
65 :     end
66 :     | sumI1 _ = raise Fail "None Variable-index in summation"
67 :     in
68 : jhr 3745 sumI1 (mapp, (v, tb (ub-lb+1), lb), sumx, [])
69 : jhr 3653 end
70 :     in
71 :     case body
72 :     of E.Value v => Mk.intToRealLit (avail, mapIndex (mapp, v))
73 :     | E.Const c => Mk.intToRealLit (avail, c)
74 :     | E.Delta(i, j) => Mk.delta (avail, mapp, i, j)
75 :     | E.Epsilon(i, j, k) => Mk.epsilon3 (avail, mapp, i, j, k)
76 :     | E.Eps2(i, j) => Mk.epsilon2 (avail, mapp, i, j)
77 : jhr 3728 | E.Tensor(id, ix) => Mk.tensorIndex (avail, mapp, List.nth(lowArgs, id), ix)
78 : jhr 3665 | E.Op1(op1, e1) => let
79 :     val arg = gen (mapp, e1)
80 :     in
81 :     case op1
82 :     of E.Neg => Mk.realNeg (avail, arg)
83 : jhr 3746 | E.Sqrt => Mk.realSqrt (avail, arg)
84 :     | E.Cosine => Mk.realCos (avail, arg)
85 :     | E.ArcCosine => Mk.realArcCos (avail, arg)
86 :     | E.Sine => Mk.realSin (avail, arg)
87 :     | E.ArcSine => Mk.realArcSin (avail, arg)
88 :     | E.Tangent => Mk.realTan (avail, arg)
89 :     | E.ArcTangent => Mk.realArcTan (avail, arg)
90 : jhr 3943 | _ => raise Fail "FIXME: unimplemented Op1 operator"
91 : jhr 3665 (*
92 :     | E.Exp
93 :     | E.PowInt of int
94 :     | E.PowReal of Rational.t
95 :     | E.PowEmb of sumrange list * int
96 :     *)
97 :     (* end case *)
98 :     end
99 : jhr 3661 | E.Op2(E.Sub, e1, e2) => Mk.realSub (avail, gen (mapp, e1), gen (mapp, e2))
100 :     | E.Opn(E.Add, es) =>
101 :     Mk.reduce (avail, Mk.realAdd, List.map (fn e => gen(mapp, e)) es)
102 :     | E.Opn(E.Prod, es) =>
103 :     Mk.reduce (avail, Mk.realMul, List.map (fn e => gen(mapp, e)) es)
104 : jhr 3653 | E.Op2(E.Div, e1 as E.Tensor (_, [_]), e2 as E.Tensor (_, [])) =>
105 :     gen (mapp, E.Opn(E.Prod, [E.Op2 (E.Div, E.Const 1, e2), e1]))
106 : jhr 3661 | E.Op2(E.Div, e1, e2) => Mk.realDiv (avail, gen (mapp, e1), gen (mapp, e2))
107 : jhr 3801 | E.Sum(sx, E.Opn(E.Prod, (img as E.Img _) :: (kargs as (E.Krn _ :: _)))) =>
108 : jhr 3728 FieldToLow.expand {
109 :     avail = avail, mapp = mapp,
110 : jhr 3801 sx = sx, img = img, krnargs = kargs,
111 : jhr 3745 args = lowArgs
112 : jhr 3728 }
113 : jhr 3661 | E.Sum(sumx, e) =>
114 :     Mk.reduce (avail, Mk.realAdd, sumCheck (mapp, sumx, e))
115 : jhr 3778 (* FIXME: need to add normalize *)
116 : jhr 3745 | E.Probe(E.Epsilon e1, e2) => gen(mapp,E.Epsilon e1)
117 :     | E.Probe(E.Eps2 e1, e2) => gen(mapp,E.Eps2 e1)
118 :     | E.Probe(E.Const e1, e2) => gen(mapp, E.Const e1)
119 :     | E.Probe(E.Delta e1, e2) => gen(mapp, E.Delta e1)
120 :     | E.Probe e => raise Fail("probe ein-exp: " ^ EinPP.expToString body)
121 : jhr 3653 | _ => raise Fail("unsupported ein-exp: " ^ EinPP.expToString body)
122 :     (*end case*)
123 :     end
124 :     in
125 :     gen (mapp, body)
126 :     end
127 : jhr 3646
128 :     end

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