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

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/compiler/cl-target/cl-target.sml
ViewVC logotype

Annotation of /branches/pure-cfg/src/compiler/cl-target/cl-target.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1398 - (view) (download)

1 : jhr 1315 (* cl-target.sml
2 : lamonts 1244 *
3 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     * All rights reserved.
5 :     *)
6 :    
7 :     structure CLTarget : TARGET =
8 :     struct
9 :    
10 :     structure IL = TreeIL
11 :     structure V = IL.Var
12 :     structure Ty = IL.Ty
13 :     structure CL = CLang
14 :     structure RN = RuntimeNames
15 : jhr 1273 structure ToCL = TreeToCL
16 : lamonts 1305 structure N = CNames
17 : lamonts 1244
18 : jhr 1358 (* translate TreeIL types to shadow types *)
19 :     fun shadowTy ty = (case ty
20 : jhr 1363 of Ty.BoolTy => CL.T_Named "cl_bool"
21 : jhr 1358 | Ty.StringTy => raise Fail "unexpected string type"
22 :     | Ty.IVecTy 1 => CL.T_Named(RN.shadowIntTy ())
23 :     | Ty.IVecTy n => raise Fail "unexpected int vector type"
24 :     | Ty.TensorTy[] => CL.T_Named(RN.shadowRealTy ())
25 :     | Ty.TensorTy[n] => CL.T_Named(RN.shadowVecTy n)
26 :     | Ty.TensorTy[n, m] => CL.T_Named(RN.shadowMatTy(n,m))
27 :     | Ty.ImageTy(ImageInfo.ImgInfo{dim, ...}) => CL.T_Named(RN.shadowImageTy dim)
28 :     | _ => raise Fail(concat["TreeToC.trType(", Ty.toString ty, ")"])
29 :     (* end case *))
30 :    
31 : jhr 1343 (* helper functions for specifying parameters in various address spaces *)
32 :     fun clParam (spc, ty, x) = CL.PARAM([spc], ty, x)
33 :     fun globalParam (ty, x) = CL.PARAM(["__global"], ty, x)
34 :     fun constantParam (ty, x) = CL.PARAM(["__constant"], ty, x)
35 :     fun localParam (ty, x) = CL.PARAM(["__local"], ty, x)
36 :     fun privateParam (ty, x) = CL.PARAM(["__private"], ty, x)
37 :    
38 : jhr 1315 (* C variable translation *)
39 :     structure TrCVar =
40 : lamonts 1305 struct
41 :     type env = CL.typed_var TreeIL.Var.Map.map
42 :     fun lookup (env, x) = (case V.Map.find (env, x)
43 :     of SOME(CL.V(_, x')) => x'
44 : jhr 1315 | NONE => raise Fail(concat["TrCVar.lookup(_, ", V.name x, ")"])
45 : lamonts 1305 (* end case *))
46 :     (* translate a variable that occurs in an l-value context (i.e., as the target of an assignment) *)
47 :     fun lvalueVar (env, x) = (case V.kind x
48 : jhr 1315 of IL.VK_Global => CL.mkIndirect(CL.mkVar RN.globalsVarName, lookup(env, x))
49 : lamonts 1305 | IL.VK_State strand => CL.mkIndirect(CL.mkVar "selfOut", lookup(env, x))
50 :     | IL.VK_Local => CL.mkVar(lookup(env, x))
51 :     (* end case *))
52 :     (* translate a variable that occurs in an r-value context *)
53 :     fun rvalueVar (env, x) = (case V.kind x
54 : jhr 1315 of IL.VK_Global => CL.mkIndirect(CL.mkVar RN.globalsVarName, lookup(env, x))
55 : lamonts 1305 | IL.VK_State strand => CL.mkIndirect(CL.mkVar "selfIn", lookup(env, x))
56 :     | IL.VK_Local => CL.mkVar(lookup(env, x))
57 :     (* end case *))
58 : jhr 1285 end
59 :    
60 :     structure ToC = TreeToCFn (TrCVar)
61 :    
62 :     type var = CL.typed_var
63 : lamonts 1244 type exp = CL.exp
64 :     type stm = CL.stm
65 :    
66 : jhr 1313 (* OpenCL specific types *)
67 :     val clIntTy = CL.T_Named "cl_int"
68 : jhr 1279 val clProgramTy = CL.T_Named "cl_program"
69 :     val clKernelTy = CL.T_Named "cl_kernel"
70 :     val clCmdQueueTy = CL.T_Named "cl_command_queue"
71 :     val clContextTy = CL.T_Named "cl_context"
72 :     val clDeviceIdTy = CL.T_Named "cl_device_id"
73 :     val clPlatformIdTy = CL.T_Named "cl_platform_id"
74 :     val clMemoryTy = CL.T_Named "cl_mem"
75 : jhr 1313 val globPtrTy = CL.T_Ptr(CL.T_Named RN.globalsTy)
76 : jhr 1279
77 : jhr 1307 (* variable or field that is mirrored between host and GPU *)
78 :     type mirror_var = {
79 :     hostTy : CL.ty, (* variable type on Host (i.e., C type) *)
80 : jhr 1358 shadowTy : CL.ty, (* host-side shadow type of GPU type *)
81 : jhr 1307 gpuTy : CL.ty, (* variable's type on GPU (i.e., OpenCL type) *)
82 :     var : CL.var (* variable name *)
83 :     }
84 :    
85 : lamonts 1244 datatype strand = Strand of {
86 : jhr 1261 name : string,
87 :     tyName : string,
88 : jhr 1307 state : mirror_var list ref,
89 : jhr 1358 output : (Ty.ty * CL.var) option ref, (* the strand's output variable (only one for now) *)
90 : lamonts 1271 code : CL.decl list ref,
91 : jhr 1273 init_code: CL.decl ref
92 : lamonts 1244 }
93 :    
94 :     datatype program = Prog of {
95 : jhr 1307 name : string, (* stem of source file *)
96 : jhr 1261 double : bool, (* true for double-precision support *)
97 :     parallel : bool, (* true for multithreaded (or multi-GPU) target *)
98 :     debug : bool, (* true for debug support in executable *)
99 : jhr 1307 globals : mirror_var list ref,
100 : jhr 1261 topDecls : CL.decl list ref,
101 :     strands : strand AtomTable.hash_table,
102 : lamonts 1305 initially : CL.decl ref,
103 : jhr 1333 numDims: int ref, (* number of dimensions in initially iteration *)
104 : jhr 1273 imgGlobals: (string * int) list ref,
105 :     prFn: CL.decl ref
106 :     }
107 : lamonts 1244
108 :     datatype env = ENV of {
109 : jhr 1261 info : env_info,
110 :     vMap : var V.Map.map,
111 :     scope : scope
112 : lamonts 1244 }
113 :    
114 :     and env_info = INFO of {
115 : jhr 1261 prog : program
116 : lamonts 1244 }
117 :    
118 :     and scope
119 :     = NoScope
120 :     | GlobalScope
121 :     | InitiallyScope
122 : jhr 1261 | StrandScope of TreeIL.var list (* strand initialization *)
123 :     | MethodScope of TreeIL.var list (* method body; vars are state variables *)
124 : lamonts 1244
125 : jhr 1273 (* the supprted widths of vectors of reals on the target. *)
126 :     (* FIXME: for OpenCL 1.1, 3 is also valid *)
127 :     fun vectorWidths () = [2, 4, 8, 16]
128 : lamonts 1244
129 :     (* tests for whether various expression forms can appear inline *)
130 : jhr 1261 fun inlineCons n = (n < 2) (* vectors are inline, but not matrices *)
131 :     val inlineMatrixExp = false (* can matrix-valued expressions appear inline? *)
132 : lamonts 1244
133 :     (* TreeIL to target translations *)
134 :     structure Tr =
135 :     struct
136 : jhr 1261 fun fragment (ENV{info, vMap, scope}, blk) = let
137 : jhr 1308 val (vMap, stms) = (case scope
138 : jhr 1326 of GlobalScope => ToC.trFragment (vMap, blk)
139 : jhr 1321 (* NOTE: if we move strand initialization to the GPU, then we'll have to change the following code! *)
140 : jhr 1326 | InitiallyScope => ToC.trFragment (vMap, blk)
141 :     | _ => ToCL.trFragment (vMap, blk)
142 :     (* end case *))
143 : jhr 1261 in
144 :     (ENV{info=info, vMap=vMap, scope=scope}, stms)
145 :     end
146 : jhr 1315 fun block (ENV{vMap, scope, ...}, blk) = let
147 : jhr 1326 fun saveState cxt stateVars trAssign (env, args, stm) = (
148 :     ListPair.foldrEq
149 :     (fn (x, e, stms) => trAssign(env, x, e)@stms)
150 :     [stm]
151 :     (stateVars, args)
152 :     ) handle ListPair.UnequalLengths => (
153 :     print(concat["saveState ", cxt, ": length mismatch; ", Int.toString(List.length args), " args\n"]);
154 :     raise Fail(concat["saveState ", cxt, ": length mismatch"]))
155 :     in
156 :     case scope
157 : jhr 1315 (* NOTE: if we move strand initialization to the GPU, then we'll have to change the following code! *)
158 : jhr 1326 of StrandScope stateVars =>
159 : jhr 1353 ToCL.trBlock (vMap, saveState "StrandScope" stateVars ToCL.trAssign, blk)
160 : jhr 1326 | MethodScope stateVars =>
161 :     ToCL.trBlock (vMap, saveState "MethodScope" stateVars ToCL.trAssign, blk)
162 :     | InitiallyScope => ToCL.trBlock (vMap, fn (_, _, stm) => [stm], blk)
163 :     | _ => ToC.trBlock (vMap, fn (_, _, stm) => [stm], blk)
164 :     (* end case *)
165 :     end
166 : jhr 1273 fun exp (ENV{vMap, ...}, e) = ToCL.trExp(vMap, e)
167 : lamonts 1244 end
168 :    
169 :     (* variables *)
170 :     structure Var =
171 :     struct
172 : jhr 1358 fun mirror (ty, name) = {
173 :     hostTy = ToC.trType ty,
174 :     shadowTy = shadowTy ty,
175 :     gpuTy = ToCL.trType ty,
176 :     var = name
177 :     }
178 : jhr 1273 fun name (ToCL.V(_, name)) = name
179 : jhr 1307 fun global (Prog{globals, imgGlobals, ...}, name, ty) = let
180 : jhr 1358 val x = mirror (ty, name)
181 : jhr 1307 fun isImgGlobal (Ty.ImageTy(ImageInfo.ImgInfo{dim, ...}), name) =
182 :     imgGlobals := (name,dim) :: !imgGlobals
183 :     | isImgGlobal _ = ()
184 : jhr 1261 in
185 : jhr 1307 globals := x :: !globals;
186 :     isImgGlobal (ty, name);
187 :     ToCL.V(#gpuTy x, name)
188 : jhr 1261 end
189 : jhr 1273 fun param x = ToCL.V(ToCL.trType(V.ty x), V.name x)
190 : jhr 1261 fun state (Strand{state, ...}, x) = let
191 : jhr 1307 val ty = V.ty x
192 : jhr 1358 val x' = mirror (ty, V.name x)
193 : jhr 1261 in
194 :     state := x' :: !state;
195 : jhr 1307 ToCL.V(#gpuTy x', #var x')
196 : jhr 1261 end
197 : lamonts 1244 end
198 :    
199 :     (* environments *)
200 :     structure Env =
201 :     struct
202 :     (* create a new environment *)
203 : jhr 1261 fun new prog = ENV{
204 :     info=INFO{prog = prog},
205 :     vMap = V.Map.empty,
206 :     scope = NoScope
207 :     }
208 : lamonts 1244 (* define the current translation context *)
209 : jhr 1261 fun setScope scope (ENV{info, vMap, ...}) = ENV{info=info, vMap=vMap, scope=scope}
210 :     val scopeGlobal = setScope GlobalScope
211 :     val scopeInitially = setScope InitiallyScope
212 :     fun scopeStrand (env, svars) = setScope (StrandScope svars) env
213 :     fun scopeMethod (env, svars) = setScope (MethodScope svars) env
214 : lamonts 1244 (* bind a TreeIL varaiable to a target variable *)
215 : jhr 1261 fun bind (ENV{info, vMap, scope}, x, x') = ENV{
216 :     info = info,
217 :     vMap = V.Map.insert(vMap, x, x'),
218 :     scope = scope
219 :     }
220 : lamonts 1244 end
221 :    
222 :     (* programs *)
223 :     structure Program =
224 :     struct
225 : jhr 1278 fun new {name, double, parallel, debug} = (
226 : jhr 1261 RN.initTargetSpec double;
227 : jhr 1286 CNames.initTargetSpec double;
228 : jhr 1261 Prog{
229 : jhr 1307 name = name,
230 : jhr 1261 double = double, parallel = parallel, debug = debug,
231 : jhr 1331 globals = ref [],
232 : jhr 1261 topDecls = ref [],
233 :     strands = AtomTable.mkTable (16, Fail "strand table"),
234 : jhr 1307 initially = ref(CL.D_Comment["missing initially"]),
235 : jhr 1332 numDims = ref 0,
236 : jhr 1307 imgGlobals = ref[],
237 :     prFn = ref(CL.D_Comment(["No Print Function"]))
238 : jhr 1261 })
239 : jhr 1357
240 : jhr 1261 (* register the code that is used to register command-line options for input variables *)
241 :     fun inputs (Prog{topDecls, ...}, stm) = let
242 :     val inputsFn = CL.D_Func(
243 :     [], CL.voidTy, RN.registerOpts,
244 :     [CL.PARAM([], CL.T_Ptr(CL.T_Named RN.optionsTy), "opts")],
245 :     stm)
246 :     in
247 :     topDecls := inputsFn :: !topDecls
248 :     end
249 :    
250 : jhr 1286 (* register the global initialization part of a program *)
251 : jhr 1307 fun init (Prog{topDecls, ...}, init) = let
252 : jhr 1331 val globalsDecl = CL.mkAssign(CL.E_Var RN.globalsVarName,
253 : jhr 1358 CL.mkApply("malloc", [CL.mkSizeof(CL.T_Named RN.globalsTy)]))
254 : jhr 1331 val initFn = CL.D_Func(
255 : lamonts 1316 [], CL.voidTy, RN.initGlobals, [],
256 : jhr 1358 CL.mkBlock[
257 : lamonts 1398 globalsDecl,
258 :     CL.mkCall(RN.initGlobalsHelper, [CL.mkVar RN.globalsVarName])
259 :     ])
260 : jhr 1357 val initHelperFn = CL.D_Func(
261 : jhr 1358 [], CL.voidTy, RN.initGlobalsHelper,
262 : lamonts 1398 [CL.PARAM([], globPtrTy, RN.globalsVarName)],
263 : jhr 1286 init)
264 : jhr 1307 val shutdownFn = CL.D_Func(
265 :     [], CL.voidTy, RN.shutdown,
266 :     [CL.PARAM([], CL.T_Ptr(CL.T_Named RN.worldTy), "wrld")],
267 :     CL.S_Block[])
268 :     in
269 : jhr 1357 topDecls := shutdownFn :: initFn :: initHelperFn :: !topDecls
270 : jhr 1307 end
271 : jhr 1357
272 : jhr 1333 (* create and register the initially function for a program *)
273 : jhr 1307 fun initially {
274 : jhr 1332 prog = Prog{name=progName, strands, initially, numDims, ...},
275 : jhr 1307 isArray : bool,
276 :     iterPrefix : stm list,
277 :     iters : (var * exp * exp) list,
278 :     createPrefix : stm list,
279 :     strand : Atom.atom,
280 :     args : exp list
281 :     } = let
282 :     val name = Atom.toString strand
283 :     val nDims = List.length iters
284 :     val worldTy = CL.T_Ptr(CL.T_Named N.worldTy)
285 :     fun mapi f xs = let
286 :     fun mapf (_, []) = []
287 :     | mapf (i, x::xs) = f(i, x) :: mapf(i+1, xs)
288 :     in
289 :     mapf (0, xs)
290 :     end
291 :     val baseInit = mapi (fn (i, (_, e, _)) => (i, CL.I_Exp e)) iters
292 :     val sizeInit = mapi
293 :     (fn (i, (CL.V(ty, _), lo, hi)) =>
294 :     (i, CL.I_Exp(CL.mkBinOp(CL.mkBinOp(hi, CL.#-, lo), CL.#+, CL.E_Int(1, ty))))
295 :     ) iters
296 :     (* code to allocate the world and initial strands *)
297 :     val wrld = "wrld"
298 :     val allocCode = [
299 :     CL.mkComment["allocate initial block of strands"],
300 :     CL.mkDecl(CL.T_Array(CL.int32, SOME nDims), "base", SOME(CL.I_Array baseInit)),
301 :     CL.mkDecl(CL.T_Array(CL.uint32, SOME nDims), "size", SOME(CL.I_Array sizeInit)),
302 :     CL.mkDecl(worldTy, wrld,
303 : lamonts 1341 SOME(CL.I_Exp(CL.E_Apply(RN.allocInitially, [
304 : jhr 1307 CL.mkVar "ProgramName",
305 :     CL.mkUnOp(CL.%&, CL.E_Var(N.strandDesc name)),
306 :     CL.E_Bool isArray,
307 :     CL.E_Int(IntInf.fromInt nDims, CL.int32),
308 :     CL.E_Var "base",
309 :     CL.E_Var "size"
310 :     ]))))
311 :     ]
312 : lamonts 1316 (* create the loop nest for the initially iterations
313 : jhr 1307 val indexVar = "ix"
314 :     val strandTy = CL.T_Ptr(CL.T_Named(N.strandTy name))
315 :     fun mkLoopNest [] = CL.mkBlock(createPrefix @ [
316 :     CL.mkDecl(strandTy, "sp",
317 :     SOME(CL.I_Exp(
318 :     CL.E_Cast(strandTy,
319 :     CL.E_Apply(N.inState, [CL.E_Var "wrld", CL.E_Var indexVar]))))),
320 : jhr 1315 CL.mkCall(N.strandInit name,
321 : jhr 1326 CL.E_Var RN.globalsVarName :: CL.E_Var "sp" :: args),
322 : jhr 1307 CL.mkAssign(CL.E_Var indexVar, CL.mkBinOp(CL.E_Var indexVar, CL.#+, CL.E_Int(1, CL.uint32)))
323 :     ])
324 :     | mkLoopNest ((CL.V(ty, param), lo, hi)::iters) = let
325 :     val body = mkLoopNest iters
326 :     in
327 :     CL.mkFor(
328 :     [(ty, param, lo)],
329 :     CL.mkBinOp(CL.E_Var param, CL.#<=, hi),
330 :     [CL.mkPostOp(CL.E_Var param, CL.^++)],
331 :     body)
332 :     end
333 :     val iterCode = [
334 :     CL.mkComment["initially"],
335 :     CL.mkDecl(CL.uint32, indexVar, SOME(CL.I_Exp(CL.E_Int(0, CL.uint32)))),
336 :     mkLoopNest iters
337 : lamonts 1316 ] *)
338 : jhr 1307 val body = CL.mkBlock(
339 :     iterPrefix @
340 :     allocCode @
341 :     [CL.mkReturn(SOME(CL.E_Var "wrld"))])
342 : lamonts 1316 val initFn = CL.D_Func([], worldTy, N.initially, [], body)
343 : jhr 1307 in
344 : jhr 1333 numDims := nDims;
345 : jhr 1307 initially := initFn
346 :     end
347 : jhr 1281
348 : lamonts 1305 (***** OUTPUT *****)
349 : jhr 1307 fun genStrandPrint (Strand{name, tyName, state, output, code,...}) = let
350 : jhr 1326 (* the print function *)
351 : jhr 1382 val prFnName = concat[name, "Print"]
352 : jhr 1307 val prFn = let
353 : jhr 1326 val params = [
354 :     CL.PARAM([], CL.T_Ptr(CL.T_Named "FILE"), "outS"),
355 : jhr 1358 CL.PARAM([], CL.T_Ptr(CL.T_Num(RawTypes.RT_UInt8)),"status"),
356 :     CL.PARAM([], CL.intTy,"numStrands"),
357 : jhr 1326 CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "self")
358 :     ]
359 :     val SOME(ty, x) = !output
360 : jhr 1358 val outState = CL.mkSelect(CL.mkSubscript(CL.mkVar "self", CL.E_Var "i"), x)
361 : jhr 1326 val prArgs = (case ty
362 :     of Ty.IVecTy 1 => [CL.E_Str(!N.gIntFormat ^ "\n"), outState]
363 :     | Ty.IVecTy d => let
364 :     val fmt = CL.mkStr(
365 :     String.concatWith " " (List.tabulate(d, fn _ => !N.gIntFormat))
366 :     ^ "\n")
367 :     val args = List.tabulate (d, fn i => ToC.ivecIndex(outState, d, i))
368 :     in
369 :     fmt :: args
370 :     end
371 :     | Ty.TensorTy[] => [CL.mkStr "%f\n", outState]
372 :     | Ty.TensorTy[d] => let
373 :     val fmt = CL.mkStr(
374 :     String.concatWith " " (List.tabulate(d, fn _ => "%f"))
375 :     ^ "\n")
376 :     val args = List.tabulate (d, fn i => ToC.vecIndex(outState, d, i))
377 :     in
378 :     fmt :: args
379 :     end
380 :     | _ => raise Fail("genStrand: unsupported output type " ^ Ty.toString ty)
381 :     (* end case *))
382 : jhr 1358 val forBody = CL.mkIfThen(
383 :     CL.mkBinOp(CL.mkSubscript(CL.E_Var "status",CL.E_Var "i"), CL.#==, CL.E_Var "DIDEROT_STABILIZE"),
384 :     CL.mkBlock([CL.mkCall("fprintf", CL.mkVar "outS" :: prArgs)]))
385 :     val body = CL.mkFor(
386 : lamonts 1346 [(CL.intTy, "i", CL.mkInt 0)],
387 : lamonts 1347 CL.mkBinOp(CL.E_Var "i", CL.#<, CL.E_Var "numStrands"),
388 : lamonts 1346 [CL.mkPostOp(CL.E_Var "i", CL.^++)],
389 :     forBody)
390 : jhr 1326 in
391 : jhr 1358 CL.D_Func(["static"], CL.voidTy, prFnName, params, body)
392 : jhr 1326 end
393 : jhr 1307 in
394 : jhr 1326 prFn
395 : jhr 1307 end
396 : lamonts 1305
397 : jhr 1307 fun genStrandTyDef (targetTy, Strand{tyName, state,...}) =
398 : jhr 1261 (* the type declaration for the strand's state struct *)
399 :     CL.D_StructDef(
400 : jhr 1307 List.rev (List.map (fn x => (targetTy x, #var x)) (!state)),
401 :     tyName)
402 : jhr 1358
403 : jhr 1307 (* generates the opencl buffers for the image data *)
404 : lamonts 1398 fun getGlobalDataBuffers (globals, imgGlobals, contextVar, errVar) = let
405 : jhr 1344 val globalBuffErr = "error creating OpenCL global buffer"
406 :     fun errorFn msg = CL.mkIfThen(CL.mkBinOp(CL.E_Var errVar, CL.#!=, CL.E_Var "CL_SUCCESS"),
407 :     CL.mkBlock([CL.mkCall("fprintf",[CL.E_Var "stderr", CL.E_Str msg]),
408 :     CL.mkCall("exit",[CL.mkInt 1])]))
409 : lamonts 1398 val shadowTypeDecl = CL.mkDecl(CL.T_Named(RN.shadowGlobalsTy),RN.shadowGlaobalsName,NONE)
410 :     val globalImagesToShadowStms = List.map (fn (var,nDims) =>
411 :     CL.mkCall((RN.shadowImageFunc nDims), [
412 :     CL.mkVar contextVar,
413 :     CL.mkUnOp(CL.%&,CL.mkSelect(CL.mkVar(RN.shadowGlaobalsName),var)),
414 :     CL.mkIndirect(CL.mkVar(RN.globalsVarName),var)
415 :     ])) imgGlobals
416 :     (* Converts only the non-image-field types into their corresponding global shadow types *)
417 :     fun convertToShadow ((global:mirror_var)::rest,(imgGlobal,nDim)::restImages) =
418 :     if (#var global) = imgGlobal then
419 :     convertToShadow(rest,restImages)
420 :     else
421 :     CL.mkAssign(CL.mkSelect(CL.mkVar(RN.shadowGlaobalsName),#var global),
422 :     CL.mkIndirect(CL.mkVar(RN.globalsVarName), #var global))::
423 :     convertToShadow(rest,(imgGlobal,nDim)::restImages)
424 :     | convertToShadow ([], _) = []
425 :     | convertToShadow ((global:mirror_var)::rest, []) =
426 :     CL.mkAssign(CL.mkSelect(CL.mkVar(RN.shadowGlaobalsName),#var global),
427 :     CL.mkIndirect(CL.mkVar(RN.globalsVarName), #var global))::
428 :     convertToShadow(rest,[])
429 :    
430 :     val globalToShadowStms = convertToShadow(globals,imgGlobals)
431 : jhr 1358 val globalBufferDecl = CL.mkDecl(clMemoryTy,concat[RN.globalsVarName,"_cl"],NONE)
432 : jhr 1307 val globalBuffer = CL.mkAssign(CL.mkVar(concat[RN.globalsVarName,"_cl"]),
433 :     CL.mkApply("clCreateBuffer", [
434 :     CL.mkVar contextVar,
435 : lamonts 1398 CL.mkVar "CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR",
436 :     CL.mkSizeof(CL.T_Named RN.shadowGlobalsTy),
437 :     CL.mkUnOp(CL.%&,CL.mkVar RN.shadowGlaobalsName),
438 : jhr 1307 CL.mkUnOp(CL.%&,CL.mkVar errVar)
439 :     ]))
440 : lamonts 1341 fun genDataBuffers ([],_,_,_) = []
441 :     | genDataBuffers ((var,nDims)::globals, contextVar, errVar,errFn) = let
442 : jhr 1326 val hostVar = CL.mkIndirect(CL.mkVar RN.globalsVarName, var)
443 : lamonts 1398 val size = CL.mkIndirect(hostVar, "dataSzb")
444 : jhr 1326 in
445 : lamonts 1398 CL.mkDecl(clMemoryTy, RN.addBufferSuffixData var ,NONE) ::
446 : jhr 1326 CL.mkAssign(CL.mkVar(RN.addBufferSuffixData var),
447 :     CL.mkApply("clCreateBuffer", [
448 :     CL.mkVar contextVar,
449 : lamonts 1398 CL.mkVar "CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR",
450 : jhr 1326 size,
451 :     CL.mkIndirect(hostVar, "data"),
452 :     CL.mkUnOp(CL.%&,CL.mkVar errVar)
453 : jhr 1344 ])) ::
454 : jhr 1358 errFn(concat["error in creating ",RN.addBufferSuffixData var, " global buffer"]) ::
455 :     genDataBuffers(globals,contextVar,errVar,errFn)
456 : jhr 1326 end
457 : jhr 1307 in
458 : lamonts 1398 [shadowTypeDecl] @ globalImagesToShadowStms @ globalToShadowStms
459 :     @ [globalBufferDecl, globalBuffer,errorFn(globalBuffErr)]
460 :     @ genDataBuffers(imgGlobals,contextVar,errVar,errorFn)
461 : jhr 1307 end
462 : lamonts 1264
463 : jhr 1309 (* generates the kernel arguments for the image data *)
464 : jhr 1313 fun genGlobalArguments (globals, count, kernelVar, errVar) = let
465 : jhr 1343 val globalArgErr = "error creating OpenCL global argument"
466 :     fun errorFn msg = CL.mkIfThen(CL.mkBinOp(CL.E_Var errVar, CL.#!=, CL.E_Var "CL_SUCCESS"),
467 : jhr 1344 CL.mkBlock([CL.mkCall("fprintf",[CL.E_Var "stderr", CL.E_Str msg]),
468 :     CL.mkCall("exit",[CL.mkInt 1])]))
469 : lamonts 1398 val globalArgument = CL.mkExpStm(CL.mkAssignOp(CL.mkVar errVar,CL.&=,
470 : jhr 1326 CL.mkApply("clSetKernelArg",
471 :     [CL.mkVar kernelVar,
472 :     CL.mkPostOp(CL.E_Var count, CL.^++),
473 :     CL.mkApply("sizeof",[CL.mkVar "cl_mem"]),
474 :     CL.mkUnOp(CL.%&,CL.mkVar(concat[RN.globalsVarName,"_cl"]))])))
475 : lamonts 1341 fun genDataArguments ([],_,_,_,_) = []
476 : lamonts 1398 | genDataArguments ((var,nDims)::globals,count,kernelVar,errVar,errFn) =
477 : lamonts 1341 CL.mkExpStm(CL.mkAssignOp(CL.mkVar errVar,CL.$=,
478 : jhr 1326 CL.mkApply("clSetKernelArg",
479 :     [CL.mkVar kernelVar,
480 :     CL.mkPostOp(CL.E_Var count, CL.^++),
481 :     CL.mkApply("sizeof",[CL.mkVar "cl_mem"]),
482 :     CL.mkUnOp(CL.%&,CL.mkVar(RN.addBufferSuffixData var))]))) ::
483 : jhr 1344 errFn(concat["error in creating ",RN.addBufferSuffixData var, " argument"]) ::
484 : lamonts 1341 genDataArguments (globals,count,kernelVar,errVar,errFn)
485 : jhr 1326 in
486 : lamonts 1341 [globalArgument,errorFn(globalArgErr)] @ genDataArguments(globals, count, kernelVar, errVar,errorFn)
487 : jhr 1326 end
488 : lamonts 1264
489 : lamonts 1305 (* generates the globals buffers and arguments function *)
490 : lamonts 1398 fun genGlobalBuffersArgs (globals,imgGlobals) = let
491 : jhr 1273 (* Delcare opencl setup objects *)
492 :     val errVar = "err"
493 :     val imgDataSizeVar = "image_dataSize"
494 : jhr 1315 val params = [
495 : lamonts 1305 CL.PARAM([],CL.T_Named("cl_context"), "context"),
496 : jhr 1307 CL.PARAM([],CL.T_Named("cl_kernel"), "kernel"),
497 : jhr 1344 CL.PARAM([],CL.T_Named("cl_command_queue"), "cmdQ"),
498 : jhr 1307 CL.PARAM([],CL.T_Named("int"), "argStart")
499 : jhr 1273 ]
500 : lamonts 1398 val clGlobalBuffers = getGlobalDataBuffers(globals,!imgGlobals, "context", errVar)
501 : jhr 1313 val clGlobalArguments = genGlobalArguments(!imgGlobals, "argStart", "kernel", errVar)
502 : jhr 1307 (* Body put all the statments together *)
503 : jhr 1314 val body = CL.mkDecl(clIntTy, errVar, SOME(CL.I_Exp(CL.mkInt 0)))
504 : jhr 1326 :: clGlobalBuffers @ clGlobalArguments
505 : jhr 1313 in
506 : jhr 1307 CL.D_Func([],CL.voidTy,RN.globalsSetupName,params,CL.mkBlock(body))
507 :     end
508 : lamonts 1305
509 : jhr 1307 (* generate the data and global parameters *)
510 :     fun genKeneralGlobalParams ((name,tyname)::rest) =
511 : jhr 1343 globalParam (CL.T_Ptr(CL.voidTy), RN.addBufferSuffixData name) ::
512 : jhr 1307 genKeneralGlobalParams rest
513 :     | genKeneralGlobalParams [] = []
514 :    
515 : jhr 1331 (* generate the main kernel function for the .cl file *)
516 : jhr 1307 fun genKernelFun (strand, nDims, globals, imgGlobals) = let
517 :     val Strand{name, tyName, state, output, code,...} = strand
518 :     val fName = RN.kernelFuncName;
519 :     val inState = "strand_in"
520 : lamonts 1341 val outState = "strand_out"
521 : jhr 1345 val tempVar = "tmp"
522 : jhr 1307 val params = [
523 : lamonts 1346 CL.PARAM(["__global"], CL.T_Ptr(CL.T_Named tyName), "selfIn"),
524 :     CL.PARAM(["__global"], CL.T_Ptr(CL.T_Named tyName), "selfOut"),
525 : jhr 1358 CL.PARAM(["__global"], CL.T_Ptr(CL.T_Num(RawTypes.RT_UInt8)), "strandStatus"),
526 : lamonts 1346 CL.PARAM(["__global"], CL.intTy, "width"),
527 : jhr 1358 CL.PARAM(["__global"], globPtrTy, RN.globalsVarName)
528 : jhr 1307 ] @ genKeneralGlobalParams(!imgGlobals)
529 :     val thread_ids = if nDims = 1
530 : jhr 1331 then [
531 :     CL.mkDecl(CL.intTy, "x",
532 :     SOME(CL.I_Exp(CL.mkApply(RN.getGlobalThreadId,[CL.mkInt 0]))))
533 :     ]
534 :     else if nDims = 2
535 :     then [
536 :     CL.mkDecl(CL.intTy, "x",
537 :     SOME(CL.I_Exp(CL.mkApply(RN.getGlobalThreadId,[CL.mkInt 0])))),
538 :     CL.mkDecl(CL.intTy, "y",
539 :     SOME(CL.I_Exp(CL.mkApply(RN.getGlobalThreadId,[CL.mkInt 1]))))
540 :     ]
541 :     else raise Fail "nDims > 2"
542 : jhr 1307 val strandDecl = [
543 : lamonts 1341 CL.mkDecl(CL.T_Ptr(CL.T_Named (concat["__global ",tyName])), inState, NONE),
544 :     CL.mkDecl(CL.T_Ptr(CL.T_Named (concat["__global ",tyName])), outState, NONE),
545 : jhr 1344 CL.mkDecl(CL.T_Ptr(CL.T_Named (concat["__global ",tyName])), tempVar, NONE)
546 : jhr 1331 ]
547 : lamonts 1398 val imageDataDecl = CL.mkDecl(CL.T_Named(RN.imageDataType),RN.globalImageDataName,NONE)
548 :     val imageDataStms = List.map (fn (x,_) =>
549 :     CL.mkAssign(CL.mkSelect(CL.mkVar(RN.globalImageDataName),RN.imageDataName x),
550 :     CL.mkVar(RN.addBufferSuffixData x))) (!imgGlobals)
551 :     val barrierCode = CL.mkIfThen(CL.mkBinOp(CL.E_Var "status",CL.#==,CL.E_Var "DIDEROT_ACTIVE"),
552 :     CL.mkBlock ([CL.mkAssign(CL.E_Var tempVar, CL.E_Var inState),
553 :     CL.mkAssign(CL.E_Var inState, CL.E_Var outState),
554 :     CL.mkAssign(CL.E_Var outState, CL.E_Var tempVar)]))
555 : jhr 1358 val barrierStm = CL.mkCall("barrier",[CL.E_Var "CLK_LOCAL_MEM_FENCE"])
556 : lamonts 1346 val index = if nDims = 1 then
557 :     CL.mkStr "x"
558 :     else
559 : jhr 1358 CL.mkBinOp(
560 : jhr 1332 CL.mkBinOp(CL.mkVar "x", CL.#*, CL.mkVar "width"), CL.#+, CL.mkVar "y")
561 : lamonts 1346
562 :     val strandObjects =
563 :     [ CL.mkAssign(CL.mkVar inState, CL.mkBinOp(CL.mkVar "selfIn",CL.#+,index)),
564 :     CL.mkAssign(CL.mkVar outState, CL.mkBinOp(CL.mkVar "selfOut",CL.#+,index))
565 : jhr 1358 ]
566 : lamonts 1346
567 : jhr 1358 val stabalizeStm = CL.mkAssign(CL.mkSubscript(CL.mkVar "strandStatus",index),
568 :     CL.E_Var "status")
569 : lamonts 1346 val status = CL.mkDecl(CL.intTy, "status", SOME(CL.I_Exp(CL.mkSubscript(CL.mkVar "strandStatus",index))))
570 : jhr 1333 val strandInitStm = CL.mkCall(RN.strandInit name, [
571 : lamonts 1398 CL.mkVar RN.globalsVarName,
572 :     CL.mkVar inState,
573 :     CL.mkVar "x",
574 : jhr 1333 (* FIXME: if nDims = 1, then "y" is not defined! the arguments to this call should really come from
575 :     * the initially code!
576 :     *)
577 : lamonts 1398 CL.mkVar "y"])
578 : jhr 1333 val local_vars = thread_ids
579 : lamonts 1398 @ [imageDataDecl]
580 :     @ imageDataStms
581 : jhr 1333 @ strandDecl
582 :     @ strandObjects
583 :     @ [strandInitStm,status]
584 : lamonts 1351 val while_exp = CL.mkBinOp(CL.mkVar "status",CL.#==, CL.mkVar RN.kActive)
585 : lamonts 1398 val whileBody = CL.mkBlock ([
586 : jhr 1307 CL.mkAssign(CL.mkVar "status",
587 :     CL.mkApply(RN.strandUpdate name,
588 : lamonts 1398 [CL.mkVar inState,
589 :     CL.mkVar outState,
590 :     CL.mkVar RN.globalsVarName,
591 :     CL.mkVar RN.globalImageDataName]))] @ [barrierCode,barrierStm] )
592 : jhr 1307 val whileBlock = [CL.mkWhile(while_exp, whileBody)]
593 : lamonts 1346 val body = CL.mkBlock(local_vars @ whileBlock @ [stabalizeStm])
594 : jhr 1307 in
595 :     CL.D_Func(["__kernel"], CL.voidTy, fName, params, body)
596 :     end
597 : jhr 1358
598 :     (* generate a global structure type definition from the list of globals *)
599 :     fun genGlobalStruct (targetTy, globals, tyName) = let
600 : jhr 1307 val globs = List.map (fn (x : mirror_var) => (targetTy x, #var x)) globals
601 :     in
602 : jhr 1358 CL.D_StructDef(globs, tyName)
603 : jhr 1307 end
604 : lamonts 1398
605 :     (* generate a global structure type definition from the image data of the image globals *)
606 :     fun genImageDataStruct (imgGlobals, tyName) = let
607 :     val globs = List.map (fn (x,_) => (CL.T_Ptr(CL.imageDataTy "__global"),(RN.imageDataName x))) imgGlobals
608 :     in
609 :     CL.D_StructDef(globs, tyName)
610 :     end
611 : lamonts 1341
612 : jhr 1307 fun genGlobals (declFn, targetTy, globals) = let
613 :     fun doVar (x : mirror_var) = declFn (CL.D_Var([], targetTy x, #var x, NONE))
614 :     in
615 :     List.app doVar globals
616 :     end
617 : lamonts 1264
618 : jhr 1326 fun genStrandDesc (Strand{name, output, ...}) = let
619 :     (* the strand's descriptor object *)
620 :     val descI = let
621 :     fun fnPtr (ty, f) = CL.I_Exp(CL.mkCast(CL.T_Named ty, CL.mkVar f))
622 :     val SOME(outTy, _) = !output
623 :     in
624 :     CL.I_Struct[
625 :     ("name", CL.I_Exp(CL.mkStr name)),
626 :     ("stateSzb", CL.I_Exp(CL.mkSizeof(CL.T_Named(N.strandTy name)))),
627 : jhr 1315 (*
628 : jhr 1326 ("outputSzb", CL.I_Exp(CL.mkSizeof(ToC.trTy outTy))),
629 : jhr 1315 *)
630 : jhr 1326 ("update", fnPtr("update_method_t", "0")),
631 : jhr 1382 ("print", fnPtr("print_method_t", name ^ "Print"))
632 : jhr 1326 ]
633 :     end
634 :     val desc = CL.D_Var([], CL.T_Named N.strandDescTy, N.strandDesc name, SOME descI)
635 :     in
636 :     desc
637 :     end
638 : jhr 1315
639 :     (* generate the table of strand descriptors *)
640 : jhr 1326 fun genStrandTable (declFn, strands) = let
641 :     val nStrands = length strands
642 :     fun genInit (Strand{name, ...}) = CL.I_Exp(CL.mkUnOp(CL.%&, CL.E_Var(N.strandDesc name)))
643 :     fun genInits (_, []) = []
644 :     | genInits (i, s::ss) = (i, genInit s) :: genInits(i+1, ss)
645 :     in
646 :     declFn (CL.D_Var([], CL.int32, N.numStrands,
647 :     SOME(CL.I_Exp(CL.E_Int(IntInf.fromInt nStrands, CL.int32)))));
648 :     declFn (CL.D_Var([],
649 :     CL.T_Array(CL.T_Ptr(CL.T_Named N.strandDescTy), SOME nStrands),
650 :     N.strands,
651 :     SOME(CL.I_Array(genInits (0, strands)))))
652 :     end
653 : jhr 1315
654 : jhr 1308 fun genSrc (baseName, prog) = let
655 : jhr 1326 val Prog{name,double, globals, topDecls, strands, initially, imgGlobals, numDims, ...} = prog
656 : jhr 1307 val clFileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "cl"}
657 :     val cFileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"}
658 :     val clOutS = TextIO.openOut clFileName
659 :     val cOutS = TextIO.openOut cFileName
660 :     val clppStrm = PrintAsCL.new clOutS
661 :     val cppStrm = PrintAsC.new cOutS
662 : jhr 1321 val progName = name
663 : jhr 1307 fun cppDecl dcl = PrintAsC.output(cppStrm, dcl)
664 :     fun clppDecl dcl = PrintAsCL.output(clppStrm, dcl)
665 :     val strands = AtomTable.listItems strands
666 :     val [strand as Strand{name, tyName, code, init_code, ...}] = strands
667 :     in
668 : jhr 1273 (* Generate the OpenCl file *)
669 :     clppDecl (CL.D_Verbatim([
670 :     if double
671 :     then "#define DIDEROT_DOUBLE_PRECISION"
672 :     else "#define DIDEROT_SINGLE_PRECISION",
673 :     "#define DIDEROT_TARGET_CL",
674 : lamonts 1305 "#include \"Diderot/cl-diderot.h\""
675 : jhr 1273 ]));
676 : jhr 1358 clppDecl (genGlobalStruct (#gpuTy, !globals, RN.globalsTy));
677 : lamonts 1398 clppDecl (genImageDataStruct(!imgGlobals,RN.imageDataType));
678 : jhr 1307 clppDecl (genStrandTyDef(#gpuTy, strand));
679 : jhr 1326 clppDecl (!init_code);
680 : jhr 1273 List.app clppDecl (!code);
681 : lamonts 1328 clppDecl (genKernelFun (strand, !numDims, globals, imgGlobals));
682 :     (* Generate the Host C file *)
683 : jhr 1273 cppDecl (CL.D_Verbatim([
684 :     if double
685 :     then "#define DIDEROT_DOUBLE_PRECISION"
686 :     else "#define DIDEROT_SINGLE_PRECISION",
687 :     "#define DIDEROT_TARGET_CL",
688 :     "#include \"Diderot/diderot.h\""
689 :     ]));
690 : jhr 1326 cppDecl (CL.D_Var(["static"], CL.charPtr, "ProgramName",
691 :     SOME(CL.I_Exp(CL.mkStr progName))));
692 : jhr 1358 cppDecl (genGlobalStruct (#hostTy, !globals, RN.globalsTy));
693 :     cppDecl (genGlobalStruct (#shadowTy, !globals, RN.shadowGlobalsTy));
694 :     (* FIXME: does this really need to be a global? *)
695 :     cppDecl (CL.D_Var(["static"], globPtrTy, RN.globalsVarName, NONE));
696 : jhr 1308 cppDecl (genStrandTyDef (#hostTy, strand));
697 : jhr 1307 cppDecl (genStrandPrint strand);
698 : jhr 1273 List.app cppDecl (List.rev (!topDecls));
699 : lamonts 1398 cppDecl (genGlobalBuffersArgs (!globals,imgGlobals));
700 : jhr 1326 List.app (fn strand => cppDecl (genStrandDesc strand)) strands;
701 :     genStrandTable (cppDecl, strands);
702 : jhr 1307 cppDecl (!initially);
703 :     PrintAsC.close cppStrm;
704 :     PrintAsCL.close clppStrm;
705 :     TextIO.closeOut cOutS;
706 :     TextIO.closeOut clOutS
707 :     end
708 : lamonts 1264
709 : jhr 1358 (* output the code to the filesystem. The string is the basename of the source file *)
710 : jhr 1307 fun generate (basename, prog as Prog{double, parallel, debug, ...}) = let
711 :     fun condCons (true, x, xs) = x::xs
712 :     | condCons (false, _, xs) = xs
713 :     (* generate the C compiler flags *)
714 :     val cflags = ["-I" ^ Paths.diderotInclude, "-I" ^ Paths.teemInclude]
715 :     val cflags = condCons (parallel, #pthread Paths.cflags, cflags)
716 :     val cflags = if debug
717 :     then #debug Paths.cflags :: cflags
718 :     else #ndebug Paths.cflags :: cflags
719 :     val cflags = #base Paths.cflags :: cflags
720 :     (* generate the loader flags *)
721 :     val extraLibs = condCons (parallel, #pthread Paths.extraLibs, [])
722 :     val extraLibs = Paths.teemLinkFlags @ #base Paths.extraLibs :: extraLibs
723 :     val extraLibs = #cl Paths.extraLibs :: extraLibs
724 :     val rtLib = TargetUtil.runtimeName {
725 :     target = TargetUtil.TARGET_CL,
726 :     parallel = parallel, double = double, debug = debug
727 :     }
728 :     val ldOpts = rtLib :: extraLibs
729 :     in
730 : lamonts 1341 genSrc (basename, prog);
731 : jhr 1307 RunCC.compile (basename, cflags);
732 :     RunCC.link (basename, ldOpts)
733 : jhr 1273 end
734 : lamonts 1244
735 : jhr 1273 end
736 : lamonts 1264
737 : lamonts 1244 (* strands *)
738 :     structure Strand =
739 :     struct
740 : jhr 1261 fun define (Prog{strands, ...}, strandId) = let
741 :     val name = Atom.toString strandId
742 :     val strand = Strand{
743 :     name = name,
744 :     tyName = RN.strandTy name,
745 :     state = ref [],
746 :     output = ref NONE,
747 : lamonts 1271 code = ref [],
748 : jhr 1273 init_code = ref (CL.D_Comment(["no init code"]))
749 : jhr 1261 }
750 :     in
751 :     AtomTable.insert strands (strandId, strand);
752 :     strand
753 :     end
754 : lamonts 1244
755 :     (* return the strand with the given name *)
756 : jhr 1261 fun lookup (Prog{strands, ...}, strandId) = AtomTable.lookup strands strandId
757 : lamonts 1244
758 :     (* register the strand-state initialization code. The variables are the strand
759 :     * parameters.
760 :     *)
761 : jhr 1308 fun init (Strand{name, tyName, code, init_code, ...}, params, init) = let
762 : jhr 1261 val fName = RN.strandInit name
763 :     val params =
764 : jhr 1343 globalParam (globPtrTy, RN.globalsVarName) ::
765 :     globalParam (CL.T_Ptr(CL.T_Named tyName), "selfOut") ::
766 : jhr 1273 List.map (fn (ToCL.V(ty, x)) => CL.PARAM([], ty, x)) params
767 : jhr 1261 val initFn = CL.D_Func([], CL.voidTy, fName, params, init)
768 :     in
769 : jhr 1273 init_code := initFn
770 : jhr 1261 end
771 : jhr 1358
772 : lamonts 1244 (* register a strand method *)
773 : lamonts 1271 fun method (Strand{name, tyName, code,...}, methName, body) = let
774 : jhr 1358 val fName = concat[name, "_", methName]
775 : jhr 1261 val params = [
776 : jhr 1343 globalParam (CL.T_Ptr(CL.T_Named tyName), "selfIn"),
777 :     globalParam (CL.T_Ptr(CL.T_Named tyName), "selfOut"),
778 : lamonts 1398 globalParam (CL.T_Ptr(CL.T_Named (RN.globalsTy)), RN.globalsVarName),
779 :     CL.PARAM([],CL.T_Named(RN.imageDataType),RN.globalImageDataName)
780 : jhr 1261 ]
781 :     val methFn = CL.D_Func([], CL.int32, fName, params, body)
782 :     in
783 : jhr 1273 code := methFn :: !code
784 : jhr 1261 end
785 :    
786 : jhr 1273 fun output (Strand{output, ...}, ty, ToCL.V(_, x)) = output := SOME(ty, x)
787 : lamonts 1244
788 :     end
789 :    
790 :     end
791 :    
792 :     structure CLBackEnd = CodeGenFn(CLTarget)

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