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

1 : jhr 328 (* probe.sml
2 :     *
3 :     * COPYRIGHT (c) 2010 The Diderot Project (http://diderot.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *
6 :     * Expansion of probe operations in the HighIL to MidIL translation.
7 :     *)
8 :    
9 :     structure Probe : sig
10 :    
11 :     val expand : MidIL.Var.var * FieldDef.field_def * HighIL.Var.var
12 :     -> HighIL.assign list
13 :    
14 :     end = struct
15 :    
16 :     structure SrcIL = HighIL
17 :     structure SrcOp = SrcIL.Op
18 :     structure DstIL = MidIL
19 :     structure DstOp = DstIL.Op
20 :     structure VMap = SrcIL.Var.Map
21 :    
22 :     (* a tree representation of nested iterations over the image space, where the
23 :     * height of the tree corresponds to the number of dimensions and at each node
24 :     * we have as many children as there are iterations.
25 :     *)
26 :     structure IT =
27 :     struct
28 :     datatype ('nd, 'lf) iter_tree
29 :     = LF of 'lf
30 :     | ND of ('nd * ('nd, 'lf) iter_tree list)
31 :    
32 :     fun create (depth, width, ndAttr, f, lfAttr, init) = let
33 :     fun mk (d, i, arg) = if (d < depth)
34 :     then ND(ndAttr arg, List.tabulate(width, fn j => mk(d+1, j, f(j, arg))))
35 :     else LF(lfAttr arg)
36 :     in
37 :     mk (0, 0, init)
38 :     end
39 :    
40 :     fun map (nd, lf) t = let
41 :     fun mapf (LF x) = LF(lf x)
42 :     | mapf (ND(i, kids)) = ND(nd i, List.map mapf kids)
43 :     in
44 :     mapf t
45 :     end
46 :    
47 :     fun foldr f init t = let
48 :     fun fold (LF x, acc) = f(x, acc)
49 :     | fold (ND(_, kids), acc) = List.foldr fold acc kids
50 :     in
51 :     fold t
52 :     end
53 :    
54 :     end
55 :     (* generate a new variable indexed by dimension *)
56 :     local
57 :     val dimNames = Vector.fromList[ "x", "y", "z" ];
58 :     in
59 :     fun newVar_dim (prefix, d) =
60 :     DstIL.Var.new (prefix ^ Vector.sub(dimNames, d))
61 :    
62 :     fun assign (x, rator, args) = (x, DstIL.OP(rator, args))
63 :     fun cons (x, args) = (x, DstIL.CONS args)
64 :     fun realLit (x, r) = (x, DstIL.LIT(Literal.Float(FloatLit.fromInt i)))
65 :     fun intLit (x, i) = (x, DstIL.LIT(Literal.Int(IntInf.fromInt i)))
66 :    
67 :     (* generate code for probing the field (D^k (v * h)) at pos *)
68 :     fun probe (result, (k, v, h), pos) = let
69 :     val ImageInfo.ImgInfo{dim, ty=([], ty), ...} = v
70 :     val dimTy = DstOp.VecTy dim
71 :     val s = Kernel.support h
72 :     val sTy = DstOp.VecTy(2*s)
73 :     (* generate the transform code *)
74 :     val x = DstIL.Var.new "x" (* image-space position *)
75 :     val f = DstIL.Var.new "f"
76 :     val nd = DstIL.Var.new "nd"
77 :     val n = DstIL.Var.new "n"
78 :     val transformCode = [
79 :     assign(x, DstIL.Transform v, [pos]),
80 :     assign(nd, DstIL.Floor dim, [x]),
81 :     assign(f, DstIL.Sub dimTy, [x, nd]),
82 :     assign(n, DstOp.TruncToInt dim, [nd])
83 :     ]
84 :     (* generate code to load the voxel data *)
85 :     val voxIter = let
86 :     fun f (i, (offsets, id)) = (i - (s - 1) :: offsets, i::id)
87 :     fun g (offsets, id) = {
88 :     offsets = offsets,
89 :     vox = DstIL.Var.new(String.concat("v" :: List.map Int.toString id))
90 :     }
91 :     in
92 :     IT.create (depth, width, fn _ => (), f, g, ([], []))
93 :     end
94 :     val loadCode = let
95 :     fun genCode ({offsets, vox}, code) = let
96 :     fun computeIndices (_, []) = ([], [])
97 :     | computeIndices (i, offset::offsets) = let
98 :     val index = newVar_dim("i", i)
99 :     val t1 = DstIL.Var.new "t1"
100 :     val t2 = DstIL.Var.new "t2"
101 :     val (indices, code) = computeIndices (i+1, offsets)
102 :     val code =
103 :     intLit(t1, offset) ::
104 :     assign(t2, DstOp.Select i, [n]) ::
105 :     assign(index, DstOp.Add(DstOp.IntTy), [t1, t2]) ::
106 :     code
107 :     val indices = index::indices
108 :     in
109 :     (indices, code)
110 :     end
111 :     val (indices, indicesCode) = computeIndices (0, ~(s-1) :: offsets)
112 :     val a = DstIL.Var.new "a"
113 :     in
114 :     indicesCode :: [
115 :     assign(a, VoxelAddress v, indices),
116 :     assign(vox, LoadVoxels(ty, 2*s))
117 :     ] @ code
118 :     end
119 :     in
120 :     IT.foldr genCode [] voxIter
121 :     end
122 :     val voxVars = IT.foldr (fn ({vox, ...}, vs) => vox::vs) [] voxIter
123 :     (* generate the code for computing the convolution coefficients *)
124 :     val convCoeffs = Vector.tabulate (dim,
125 :     fn d => Vector.tabulate (k+1,
126 :     fn 0 => newVar_dim("h", d)
127 :     | 1 => newVar_dim("dh", d)
128 :     | i => newVar_dim(concat["d", Int.toString i, "h"], d)))
129 :     fun coefficient (d, k) = Vector.sub(Vector.sub(convCoeffs, d), k)
130 :     fun genCoeffCode d = if (d < dim)
131 :     then let
132 :     val code = genCoeffCode (d+1)
133 :     val fd = newVar_dim "f"
134 :     val a = DstIL.Var.new "a"
135 :     val tmps = List.tabulate(2*s,
136 :     fn i => (DstIL.Var.new("t"^Int.toString i), s - (i+1)))
137 :     fun mkArg ((t, n), code) = let
138 :     val t' = DstIL.Var.new "r"
139 :     in
140 :     realLit (t', n) ::
141 :     assign (t, DstIL.Add DstIL.realTy, [fd, t']) ::
142 :     code
143 :     end
144 :     val coeffCode =
145 :     cons(a, List.map #1 tmps) ::
146 :     List.tabulate (k+1, fn i =>
147 :     assign(coefficient(d, i), EvalKernel(2*s, h, i), [a]))
148 :     in
149 :     assign(fd, DstOp.Select d, f) ::
150 :     (List.foldr mkArg (coeffCode @ code) tmps)
151 :     end
152 :     else []
153 :     val coeffCode = genCoeffCode
154 :     (* generate the reduction code *)
155 :     fun genReduce (d, IT.ND(kids), code) =
156 :     if (d < dim)
157 :     then List.foldr (fn (nd, code) => genReduce(d+1, nd, code)) code kids
158 :     else let (* the kids will all be leaves *)
159 :     val vv = DstIL.Var.new "vv"
160 :     fun getVox (IT.LF{vox, offsets}) = vox
161 :     val hh = coefficient (d, 0) (* FIXME: what is the right value for k? *)
162 :     in
163 :     cons (vv, List.map getVox kids) ::
164 :     assign (t, DstIL.Dot, [hh, vv]) :: code
165 :     end
166 :     val reduceCode = genReduce (1, voxIter, [])
167 :     in
168 :     transformCode @ loadCode @ coeffCode @ reduceCode
169 :     end
170 :    
171 :     end
172 :    
173 :     fun expand (result, FieldDef.CONV(0, img, h), pos) = let
174 :     fun expand' (result, FieldDef.CONV(k, v, h)) = let
175 :     val x = DstIL.Var.new "x"
176 :     val xformStm = (x, DstIL.OP(DstOp.Transform img, [pos']))
177 :     in
178 :     probe (result, (k, v, h), x) @ [xformStm]
179 :     end
180 :     | expand' (result, FieldDef.NEG fld) = let
181 :     val r = DstIL.Var.new "value"
182 :     val stms = expand' (r, fld)
183 :     in
184 :     expand' (r, fld) @ [assign(r, DstOp.Neg ty, [r])]
185 :     end
186 :     | expand' (result, FieldDef.SUM(fld1, dlf2)) = raise Fail "expandInside: SUM"
187 :     in
188 :     List.rev (expand (result, fld))
189 :     end
190 :    
191 :     end

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