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

SCM Repository

[diderot] Annotation of /trunk/src/compiler/high-to-mid/probe.sml
ViewVC logotype

Annotation of /trunk/src/compiler/high-to-mid/probe.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 435 - (view) (download)

1 : jhr 328 (* probe.sml
2 :     *
3 : jhr 435 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 : jhr 328 * All rights reserved.
5 :     *
6 :     * Expansion of probe operations in the HighIL to MidIL translation.
7 :     *)
8 :    
9 :     structure Probe : sig
10 :    
11 : jhr 349 val expand : MidIL.var * FieldDef.field_def * MidIL.var -> MidIL.assign list
12 : jhr 328
13 :     end = struct
14 :    
15 :     structure SrcIL = HighIL
16 : jhr 334 structure SrcOp = HighOps
17 : jhr 328 structure DstIL = MidIL
18 : jhr 391 structure DstTy = MidILTypes
19 : jhr 334 structure DstOp = MidOps
20 : jhr 349 structure DstV = DstIL.Var
21 : jhr 328 structure VMap = SrcIL.Var.Map
22 : jhr 349 structure IT = Shape
23 : jhr 328
24 :     (* generate a new variable indexed by dimension *)
25 : jhr 394 fun newVar_dim (prefix, d, ty) =
26 :     DstV.new (prefix ^ Partials.axisToString(Partials.axis d), ty)
27 : jhr 328
28 :     fun assign (x, rator, args) = (x, DstIL.OP(rator, args))
29 :     fun cons (x, args) = (x, DstIL.CONS args)
30 : jhr 334 fun realLit (x, i) = (x, DstIL.LIT(Literal.Float(FloatLit.fromInt i)))
31 : jhr 328 fun intLit (x, i) = (x, DstIL.LIT(Literal.Int(IntInf.fromInt i)))
32 :    
33 : jhr 349 (* generate code for a evaluating a single element of a probe operation *)
34 :     fun probeElem {
35 :     dim, (* dimension of space *)
36 :     h, s, (* kernel h with support s *)
37 :     n, f, (* Dst vars for integer and fractional components of position *)
38 :     voxIter (* iterator over voxels *)
39 :     } (result, pdOp) = let
40 : jhr 394 val vecsTy = DstTy.VecTy(2*s) (* vectors of coefficients cover support of kernel *)
41 :     val vecDimTy = DstTy.VecTy dim
42 : jhr 349 (* generate the variables that hold the convolution coefficients *)
43 :     val convCoeffs = let
44 :     val Partials.D l = pdOp
45 : jhr 353 fun mkVar (_, []) = []
46 :     | mkVar (i, d::dd) = (case d
47 : jhr 394 of 0 => newVar_dim("h", i, vecsTy) :: mkVar(i+1, dd)
48 :     | 1 => newVar_dim("dh", i, vecsTy) :: mkVar(i+1, dd)
49 :     | _ => newVar_dim(concat["d", Int.toString d, "h"], i, vecsTy) :: mkVar(i+1, dd)
50 : jhr 353 (* end case *))
51 : jhr 349 in
52 : jhr 353 mkVar (0, l)
53 : jhr 349 end
54 : jhr 353 val _ = print(concat["probeElem: ", Partials.partialToString pdOp, " in ", Int.toString(List.length convCoeffs), "D space\n"])
55 : jhr 349 (* for each dimension, we evaluate the kernel at the coordinates for that axis *)
56 :     val coeffCode = let
57 :     fun gen (x, k, (d, code)) = let
58 :     val d = d-1
59 : jhr 394 val fd = newVar_dim ("f", d, DstTy.realTy)
60 :     val a = DstV.new ("a", vecsTy)
61 :     val tmps = List.tabulate(2*s,
62 :     fn i => (DstV.new("t"^Int.toString i, DstTy.realTy), s - (i+1)))
63 : jhr 349 fun mkArg ((t, n), code) = let
64 : jhr 394 val t' = DstV.new ("r", DstTy.realTy)
65 : jhr 349 in
66 :     realLit (t', n) ::
67 : jhr 391 assign (t, DstOp.Add DstTy.realTy, [fd, t']) ::
68 : jhr 349 code
69 :     end
70 :     val code =
71 : jhr 353 cons(a, List.map #1 tmps) ::
72 : jhr 349 assign(x, DstOp.EvalKernel(2*s, h, k), [a]) ::
73 : jhr 353 code
74 :     val code =
75 : jhr 420 assign(fd, DstOp.Select(DstTy.VecTy dim, d), [f]) ::
76 : jhr 353 List.foldr mkArg code tmps
77 : jhr 349 in
78 : jhr 353 (d, code)
79 : jhr 349 end
80 :     val Partials.D l = pdOp
81 :     in
82 :     #2 (ListPair.foldr gen (dim, []) (convCoeffs, l))
83 :     end
84 :     (* generate the reduction code *)
85 : jhr 353 fun genReduce (result, [hh], IT.LF{vox, offsets}, code) =
86 :     assign (result, DstOp.Dot(2*s), [vox, hh]) :: code
87 : jhr 349 | genReduce (result, hh::r, IT.ND(_, kids), code) = let
88 : jhr 394 val tv = DstV.new ("tv", vecsTy)
89 :     val tmps = List.tabulate(2*s, fn i => DstV.new("t"^Int.toString i, DstTy.realTy))
90 : jhr 349 fun lp ([], [], code) = code
91 :     | lp (t::ts, kid::kids, code) = genReduce(t, r, kid, lp(ts, kids, code))
92 :     val code = cons(tv, tmps) :: assign(result, DstOp.Dot(2*s), [hh, tv]) :: code
93 :     in
94 :     lp (tmps, kids, code)
95 :     end
96 : jhr 353 | genReduce _ = raise Fail "genReduce"
97 : jhr 349 val reduceCode = genReduce (result, convCoeffs, voxIter, [])
98 :     in
99 :     coeffCode @ reduceCode
100 :     end
101 :    
102 : jhr 328 (* generate code for probing the field (D^k (v * h)) at pos *)
103 :     fun probe (result, (k, v, h), pos) = let
104 :     val ImageInfo.ImgInfo{dim, ty=([], ty), ...} = v
105 :     val s = Kernel.support h
106 : jhr 394 val vecsTy = DstTy.VecTy(2*s) (* vectors of coefficients cover support of kernel *)
107 :     val vecDimTy = DstTy.VecTy dim
108 : jhr 328 (* generate the transform code *)
109 : jhr 394 val x = DstV.new ("x", vecDimTy) (* image-space position *)
110 :     val f = DstV.new ("f", vecDimTy)
111 :     val nd = DstV.new ("nd", vecDimTy)
112 :     val n = DstV.new ("n", DstTy.IVecTy dim)
113 : jhr 328 val transformCode = [
114 : jhr 334 assign(x, DstOp.Transform v, [pos]),
115 :     assign(nd, DstOp.Floor dim, [x]),
116 : jhr 394 assign(f, DstOp.Sub vecDimTy, [x, nd]),
117 : jhr 328 assign(n, DstOp.TruncToInt dim, [nd])
118 :     ]
119 : jhr 349 (* generate the shape of the differentiation tensor with variables representing
120 :     * the elements
121 :     *)
122 :     val diffIter = let
123 :     val partial = Partials.partial dim
124 :     fun f (i, axes) = Partials.axis i :: axes
125 : jhr 394 fun g axes = let
126 :     val r = DstV.new(
127 :     String.concat("r" :: List.map Partials.axisToString axes),
128 :     DstTy.realTy)
129 :     in
130 :     (r, partial axes)
131 :     end
132 : jhr 349 in
133 : jhr 374 IT.create (k, dim, fn _ => (), f, g, [])
134 : jhr 349 end
135 : jhr 374 val _ = let
136 :     val indentWid = ref 2
137 :     fun inc () = (indentWid := !indentWid + 2)
138 :     fun dec () = (indentWid := !indentWid - 2)
139 :     fun indent () = print(CharVector.tabulate(!indentWid, fn _ => #" "))
140 :     fun nd () = (indent(); print "ND\n");
141 :     fun lf (x, partial) = (
142 :     indent(); print(concat["LF(", DstV.toString x, ", ", Partials.partialToString partial, ")\n"]))
143 :     fun pr (Shape.ND(attr, kids)) = (nd attr; inc(); List.app pr kids; dec())
144 :     | pr (Shape.LF attr) = lf attr
145 :     in
146 :     print "diffIter:\n";
147 :     pr diffIter
148 :     end
149 : jhr 353 (* generate code to load the voxel data; since we a vector load operation to load the
150 :     * fastest dimension, the height of the tree is one less than the dimension of space.
151 :     *)
152 : jhr 328 val voxIter = let
153 :     fun f (i, (offsets, id)) = (i - (s - 1) :: offsets, i::id)
154 :     fun g (offsets, id) = {
155 : jhr 353 offsets = ~(s-1) :: offsets,
156 : jhr 394 vox = DstV.new(String.concat("v" :: List.map Int.toString id), vecsTy)
157 : jhr 328 }
158 :     in
159 : jhr 334 IT.create (dim-1, 2*s, fn _ => (), f, g, ([], []))
160 : jhr 328 end
161 : jhr 353 val _ = let
162 :     val indentWid = ref 2
163 :     fun inc () = (indentWid := !indentWid + 2)
164 :     fun dec () = (indentWid := !indentWid - 2)
165 :     fun indent () = print(CharVector.tabulate(!indentWid, fn _ => #" "))
166 :     fun nd () = (indent(); print "ND\n");
167 :     fun lf {offsets, vox} = (
168 :     indent(); print "LF{offsets = ["; print(String.concatWith "," (List.map Int.toString offsets));
169 :     print "], vox = "; print(DstV.toString vox); print "}\n")
170 :     fun pr (Shape.ND(attr, kids)) = (nd attr; inc(); List.app pr kids; dec())
171 :     | pr (Shape.LF attr) = lf attr
172 :     in
173 : jhr 374 print "voxIter:\n";
174 : jhr 353 pr voxIter
175 :     end
176 : jhr 328 val loadCode = let
177 :     fun genCode ({offsets, vox}, code) = let
178 :     fun computeIndices (_, []) = ([], [])
179 :     | computeIndices (i, offset::offsets) = let
180 : jhr 394 val index = newVar_dim("i", i, DstTy.intTy)
181 :     val t1 = DstV.new ("t1", DstTy.intTy)
182 :     val t2 = DstV.new ("t2", DstTy.intTy)
183 : jhr 328 val (indices, code) = computeIndices (i+1, offsets)
184 :     val code =
185 :     intLit(t1, offset) ::
186 : jhr 421 assign(t2, DstOp.Select(DstTy.IVecTy dim, i), [n]) ::
187 : jhr 391 assign(index, DstOp.Add(DstTy.intTy), [t1, t2]) ::
188 : jhr 328 code
189 :     val indices = index::indices
190 :     in
191 :     (indices, code)
192 :     end
193 : jhr 353 val (indices, indicesCode) = computeIndices (0, offsets)
194 : jhr 394 val a = DstV.new ("a", DstTy.AddrTy)
195 : jhr 328 in
196 : jhr 349 indicesCode @ [
197 : jhr 334 assign(a, DstOp.VoxelAddress v, indices),
198 : jhr 349 assign(vox, DstOp.LoadVoxels(ty, 2*s), [a])
199 : jhr 328 ] @ code
200 :     end
201 :     in
202 :     IT.foldr genCode [] voxIter
203 :     end
204 : jhr 349 (* generate code to evaluate and construct the result tensor *)
205 :     val probeElem = probeElem {dim = dim, h = h, s = s, n = n, f = f, voxIter = voxIter}
206 :     fun genProbe (result, IT.ND(_, kids as (IT.LF _)::_), code) = let
207 :     (* the kids will all be leaves *)
208 :     fun genProbeCode (IT.LF arg, code) = probeElem arg @ code
209 :     fun getProbeVar (IT.LF(t, _)) = t
210 :     in
211 : jhr 374 List.foldr genProbeCode (cons (result, List.map getProbeVar kids) :: code) kids
212 : jhr 349 end
213 :     | genProbe (result, IT.ND(_, kids), code) = let
214 : jhr 394 val tmps = List.tabulate(dim, fn i => DstV.new("t"^Int.toString i, DstTy.realTy))
215 : jhr 374 val code = cons(result, tmps) :: code
216 : jhr 349 fun lp ([], [], code) = code
217 :     | lp (t::ts, kid::kids, code) = genProbe(t, kid, lp(ts, kids, code))
218 :     in
219 :     lp (tmps, kids, code)
220 :     end
221 : jhr 352 | genProbe (result, IT.LF(t, pdOp), code) = (* for scalar fields *)
222 :     probeElem (result, pdOp) @ code
223 : jhr 349 val probeCode = genProbe (result, diffIter, [])
224 : jhr 328 in
225 : jhr 349 transformCode @ loadCode @ probeCode
226 : jhr 328 end
227 :    
228 : jhr 349 fun expand (result, fld, pos) = let
229 : jhr 353 fun expand' (result, FieldDef.CONV(k, v, h)) = probe (result, (k, v, h), pos)
230 : jhr 349 (* should push negation down to probe operation
231 : jhr 328 | expand' (result, FieldDef.NEG fld) = let
232 : jhr 349 val r = DstV.new "value"
233 : jhr 328 val stms = expand' (r, fld)
234 : jhr 349 val ty = ??
235 : jhr 328 in
236 :     expand' (r, fld) @ [assign(r, DstOp.Neg ty, [r])]
237 :     end
238 : jhr 349 *)
239 : jhr 328 | expand' (result, FieldDef.SUM(fld1, dlf2)) = raise Fail "expandInside: SUM"
240 :     in
241 : jhr 353 expand' (result, fld)
242 : jhr 328 end
243 :    
244 :     end

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