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 3349 - (view) (download)

1 : jhr 328 (* probe.sml
2 :     *
3 : jhr 3349 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 : jhr 328 * All rights reserved.
7 :     *
8 :     * Expansion of probe operations in the HighIL to MidIL translation.
9 :     *)
10 :    
11 :     structure Probe : sig
12 :    
13 : jhr 1116 val expand : {
14 :     result : MidIL.var, (* result variable for probe *)
15 :     img : MidIL.var, (* probe image argument *)
16 :     v : ImageInfo.info, (* summary info about image *)
17 :     h : Kernel.kernel, (* reconstruction kernel *)
18 :     k : int, (* number of levels of differentiation *)
19 :     pos : MidIL.var (* probe position argument *)
20 :     } -> MidIL.assign list
21 : jhr 328
22 :     end = struct
23 :    
24 :     structure SrcIL = HighIL
25 : jhr 334 structure SrcOp = HighOps
26 : jhr 328 structure DstIL = MidIL
27 : jhr 391 structure DstTy = MidILTypes
28 : jhr 334 structure DstOp = MidOps
29 : jhr 349 structure DstV = DstIL.Var
30 : jhr 328 structure VMap = SrcIL.Var.Map
31 : jhr 349 structure IT = Shape
32 : jhr 328
33 :     (* generate a new variable indexed by dimension *)
34 : jhr 394 fun newVar_dim (prefix, d, ty) =
35 :     DstV.new (prefix ^ Partials.axisToString(Partials.axis d), ty)
36 : jhr 328
37 :     fun assign (x, rator, args) = (x, DstIL.OP(rator, args))
38 : jhr 1116 fun cons (x, args) = (x, DstIL.CONS(DstV.ty x, args))
39 : jhr 2356 fun realLit (x, i) = (x, DstIL.LIT(Literal.Float(FloatLit.fromInt(IntInf.fromInt i))))
40 : jhr 328 fun intLit (x, i) = (x, DstIL.LIT(Literal.Int(IntInf.fromInt i)))
41 :    
42 : jhr 349 (* generate code for a evaluating a single element of a probe operation *)
43 :     fun probeElem {
44 :     dim, (* dimension of space *)
45 :     h, s, (* kernel h with support s *)
46 :     n, f, (* Dst vars for integer and fractional components of position *)
47 :     voxIter (* iterator over voxels *)
48 :     } (result, pdOp) = let
49 : jhr 1116 val vecsTy = DstTy.vecTy(2*s) (* vectors of coefficients cover support of kernel *)
50 :     val vecDimTy = DstTy.vecTy dim
51 :     (* generate the variables that hold the convolution coefficients. The
52 :     * resulting list is in slowest-to-fastest axes order.
53 :     *)
54 : jhr 349 val convCoeffs = let
55 :     val Partials.D l = pdOp
56 : jhr 1116 fun mkVar (_, [], coeffs) = coeffs
57 :     | mkVar (i, d::dd, coeffs) = (case d
58 :     of 0 => mkVar(i+1, dd, newVar_dim("h", i, vecsTy) :: coeffs)
59 :     | 1 => mkVar(i+1, dd, newVar_dim("dh", i, vecsTy) :: coeffs)
60 :     | _ => mkVar(i+1, dd, newVar_dim(concat["d", Int.toString d, "h"], i, vecsTy) :: coeffs)
61 : jhr 353 (* end case *))
62 : jhr 349 in
63 : jhr 1116 mkVar (0, l, [])
64 : jhr 349 end
65 : jhr 1116 (* for each dimension in space, we evaluate the kernel at the coordinates for that axis.
66 :     * the coefficients are
67 :     * h_{s-i} (f - i) for 1-s <= i <= s
68 :     *)
69 : jhr 349 val coeffCode = let
70 :     fun gen (x, k, (d, code)) = let
71 : jhr 1116 (* note that for 1D images, the f vector is a scalar *)
72 :     val fd = if (dim > 1)
73 :     then newVar_dim ("f", d, DstTy.realTy)
74 :     else f
75 : jhr 394 val a = DstV.new ("a", vecsTy)
76 : jhr 1116 (* note that we reverse the order of the list since the convolution
77 :     * space is flipped from the image space and we want the voxel vector
78 :     * to be in increasing address order.
79 :     *)
80 :     val tmps = List.rev(List.tabulate(2*s,
81 :     fn i => (DstV.new("t"^Int.toString i, DstTy.realTy), i - s)))
82 :     fun mkArg ((t, 0), code) = (t, DstIL.VAR fd) :: code
83 :     | mkArg ((t, n), code) = let
84 :     val (rator, n) = if (n < 0) then (DstOp.Sub, ~n) else (DstOp.Add, n)
85 : jhr 394 val t' = DstV.new ("r", DstTy.realTy)
86 : jhr 349 in
87 :     realLit (t', n) ::
88 : jhr 1116 assign (t, rator DstTy.realTy, [fd, t']) ::
89 : jhr 349 code
90 :     end
91 :     val code =
92 : jhr 353 cons(a, List.map #1 tmps) ::
93 : jhr 349 assign(x, DstOp.EvalKernel(2*s, h, k), [a]) ::
94 : jhr 353 code
95 : jhr 1116 val code = List.foldr mkArg code tmps
96 :     val code = if (dim > 1)
97 : jhr 1640 then assign(fd, DstOp.Index(DstTy.vecTy dim, d), [f]) :: code
98 : jhr 1116 else code
99 : jhr 349 in
100 : jhr 1116 (d+1, code)
101 : jhr 349 end
102 :     val Partials.D l = pdOp
103 :     in
104 : jhr 1116 (* we iterate from fastest to slowest axis *)
105 :     #2 (ListPair.foldr gen (0, []) (convCoeffs, List.rev l))
106 : jhr 349 end
107 : jhr 1116 (* generate the reduction code in reverse order *)
108 : jhr 353 fun genReduce (result, [hh], IT.LF{vox, offsets}, code) =
109 :     assign (result, DstOp.Dot(2*s), [vox, hh]) :: code
110 : jhr 349 | genReduce (result, hh::r, IT.ND(_, kids), code) = let
111 : jhr 394 val tv = DstV.new ("tv", vecsTy)
112 :     val tmps = List.tabulate(2*s, fn i => DstV.new("t"^Int.toString i, DstTy.realTy))
113 : jhr 349 fun lp ([], [], code) = code
114 :     | lp (t::ts, kid::kids, code) = genReduce(t, r, kid, lp(ts, kids, code))
115 :     val code = cons(tv, tmps) :: assign(result, DstOp.Dot(2*s), [hh, tv]) :: code
116 :     in
117 :     lp (tmps, kids, code)
118 :     end
119 : jhr 353 | genReduce _ = raise Fail "genReduce"
120 : jhr 349 val reduceCode = genReduce (result, convCoeffs, voxIter, [])
121 :     in
122 :     coeffCode @ reduceCode
123 :     end
124 :    
125 : jhr 1116 fun doVoxelSample (result, v, k, s, diffIter, {h, n, f, img}, offset) = let
126 :     val stride = ImageInfo.stride
127 :     val dim = ImageInfo.dim v
128 :     val vecsTy = DstTy.vecTy(2*s) (* vectors of coefficients cover support of kernel *)
129 :     (* generate code to load the voxel data; since we use a vector load operation to load the
130 : jhr 353 * fastest dimension, the height of the tree is one less than the dimension of space.
131 :     *)
132 : jhr 328 val voxIter = let
133 :     fun f (i, (offsets, id)) = (i - (s - 1) :: offsets, i::id)
134 :     fun g (offsets, id) = {
135 : jhr 353 offsets = ~(s-1) :: offsets,
136 : jhr 394 vox = DstV.new(String.concat("v" :: List.map Int.toString id), vecsTy)
137 : jhr 328 }
138 :     in
139 : jhr 334 IT.create (dim-1, 2*s, fn _ => (), f, g, ([], []))
140 : jhr 328 end
141 :     val loadCode = let
142 :     fun genCode ({offsets, vox}, code) = let
143 :     fun computeIndices (_, []) = ([], [])
144 :     | computeIndices (i, offset::offsets) = let
145 : jhr 394 val index = newVar_dim("i", i, DstTy.intTy)
146 :     val t1 = DstV.new ("t1", DstTy.intTy)
147 :     val t2 = DstV.new ("t2", DstTy.intTy)
148 : jhr 328 val (indices, code) = computeIndices (i+1, offsets)
149 : jhr 1116 val code = if (dim > 1)
150 :     then
151 :     intLit(t1, offset) ::
152 : jhr 1640 assign(t2, DstOp.Index(DstTy.iVecTy dim, i), [n]) ::
153 : jhr 1116 assign(index, DstOp.Add(DstTy.intTy), [t1, t2]) ::
154 :     code
155 :     else
156 :     intLit(t1, offset) ::
157 :     assign(index, DstOp.Add(DstTy.intTy), [t1, n]) ::
158 :     code
159 : jhr 328 val indices = index::indices
160 :     in
161 :     (indices, code)
162 :     end
163 : jhr 353 val (indices, indicesCode) = computeIndices (0, offsets)
164 : jhr 1116 val a = DstV.new ("a", DstTy.AddrTy v)
165 : jhr 328 in
166 : jhr 349 indicesCode @ [
167 : jhr 1116 assign(a, DstOp.VoxelAddress(v, offset), img::indices),
168 :     assign(vox, DstOp.LoadVoxels(v, 2*s), [a])
169 : jhr 328 ] @ code
170 :     end
171 :     in
172 :     IT.foldr genCode [] voxIter
173 :     end
174 : jhr 349 (* generate code to evaluate and construct the result tensor *)
175 :     val probeElem = probeElem {dim = dim, h = h, s = s, n = n, f = f, voxIter = voxIter}
176 :     fun genProbe (result, IT.ND(_, kids as (IT.LF _)::_), code) = let
177 :     (* the kids will all be leaves *)
178 :     fun genProbeCode (IT.LF arg, code) = probeElem arg @ code
179 :     fun getProbeVar (IT.LF(t, _)) = t
180 :     in
181 : jhr 374 List.foldr genProbeCode (cons (result, List.map getProbeVar kids) :: code) kids
182 : jhr 349 end
183 : jhr 1116 | genProbe (result, IT.ND(ty, kids), code) = let
184 :     (* FIXME: the type of the tmps depends on the types of the kids *)
185 :     val tmps = List.tabulate(dim, fn i => DstV.new("t"^Int.toString i, ty))
186 : jhr 374 val code = cons(result, tmps) :: code
187 : jhr 349 fun lp ([], [], code) = code
188 :     | lp (t::ts, kid::kids, code) = genProbe(t, kid, lp(ts, kids, code))
189 :     in
190 :     lp (tmps, kids, code)
191 :     end
192 : jhr 352 | genProbe (result, IT.LF(t, pdOp), code) = (* for scalar fields *)
193 :     probeElem (result, pdOp) @ code
194 : jhr 1116 val probeCode = if (k > 0)
195 :     then let
196 :     (* for gradients, etc. we have to transform back to world space *)
197 :     val ty = DstV.ty result
198 :     val tensor = DstV.new("tensor", ty)
199 :     val xform = assign(result, DstOp.TensorToWorldSpace(v, ty), [img, tensor])
200 :     in
201 :     genProbe (tensor, diffIter, [xform])
202 :     end
203 :     else genProbe (result, diffIter, [])
204 : jhr 328 in
205 : jhr 450 (* FIXME: for dim > 1 and k > 1, we need to transform the result back into world space *)
206 : jhr 1116 loadCode @ probeCode
207 : jhr 328 end
208 :    
209 : jhr 1116 (* generate code for probing the field (D^k (v * h)) at pos *)
210 :     fun expand {result, img, v, h, k, pos} = let
211 :     val dim = ImageInfo.dim v
212 :     val s = Kernel.support h
213 :     val vecsTy = DstTy.vecTy(2*s) (* vectors of coefficients to cover support of kernel *)
214 :     val vecDimTy = DstTy.vecTy dim
215 :     (* generate the transform code *)
216 :     val x = DstV.new ("x", vecDimTy) (* image-space position *)
217 :     val f = DstV.new ("f", vecDimTy)
218 :     val nd = DstV.new ("nd", vecDimTy)
219 : jhr 1640 val n = DstV.new ("n", DstTy.iVecTy dim)
220 : jhr 1116 val toImgSpaceCode = [
221 :     assign(x, DstOp.PosToImgSpace v, [img, pos]),
222 :     assign(nd, DstOp.Floor dim, [x]),
223 :     assign(f, DstOp.Sub vecDimTy, [x, nd]),
224 :     assign(n, DstOp.RealToInt dim, [nd])
225 :     ]
226 :     (* generate the shape of the differentiation tensor with variables representing
227 :     * the elements
228 :     *)
229 : jhr 1393 fun diffIter () = let
230 : jhr 1116 val partial = Partials.partial dim
231 :     fun f (i, (_::dd, axes)) = (dd, Partials.axis i :: axes)
232 :     fun labelNd (_::dd, _) = DstTy.tensorTy dd
233 :     fun labelLf (_, axes) = let
234 :     val r = DstV.new(
235 :     String.concat("r" :: List.map Partials.axisToString axes),
236 :     DstTy.realTy)
237 :     in
238 :     (r, partial axes)
239 :     end
240 :     in
241 :     IT.create (k, dim, labelNd, f, labelLf, (List.tabulate(k, fn _ => dim), []))
242 : jhr 328 end
243 : jhr 1116 val vars = {h=h, n=n, f=f, img=img}
244 : jhr 328 in
245 : jhr 1116 case ImageInfo.voxelShape v
246 : jhr 1393 of [] => toImgSpaceCode @ doVoxelSample (result, v, k, s, diffIter(), vars, 0)
247 : jhr 2356 (*
248 : jhr 1116 | [d] => let
249 : jhr 2356 (* the result will be a vector of k-order tensors; each element of the vector
250 :     * has the sampleTy = tensor[dim,...,dim].
251 :     *)
252 :     val sampleTy = DstTy.TensorTy(List.tabulate(k, fn _ => dim))
253 : jhr 1116 fun doSamples (offset, xs, code) = if (offset < 0)
254 :     then code @ [cons(result, xs)]
255 :     else let
256 : jhr 1393 val res = DstV.new ("probe" ^ Int.toString offset, sampleTy)
257 :     val code = doVoxelSample (res, v, k, s, diffIter(), vars, offset) @ code
258 : jhr 1116 in
259 :     doSamples (offset-1, res::xs, code)
260 :     end
261 :     in
262 :     toImgSpaceCode @ doSamples (d-1, [], [])
263 :     end
264 : jhr 2356 *)
265 :     | shape => let
266 :     (* the result will be a d1 x d2 matrix of k-order tensors; each element of the matrix
267 :     * has the sampleTy = tensor[dim,...,dim].
268 :     *)
269 :     val sampeShape = List.tabulate(k, fn _ => dim)
270 :     fun gen (y, d::dd, offset, code) = let
271 :     val prefix = DstV.name y
272 :     val xs = List.tabulate(d,
273 :     fn i => DstV.new(
274 :     concat[prefix, "_", Int.toString i],
275 :     DstTy.TensorTy(dd @ sampeShape)))
276 :     val code = cons(y, xs) :: code
277 :     in
278 :     List.foldr (fn (x, (off, cd)) => gen(x, dd, off, cd)) (offset, code) xs
279 :     end
280 :     | gen (y, [], offset, code) =
281 :     (offset-1, doVoxelSample (y, v, k, s, diffIter(), vars, offset) @ code)
282 :     val (_, code) = gen (result, shape, ImageInfo.stride v - 1, [])
283 :     in
284 :     toImgSpaceCode @ code
285 :     end
286 : jhr 1116 (* end case *)
287 : jhr 328 end
288 :    
289 :     end

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