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

SCM Repository

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

Annotation of /branches/vis12/src/compiler/cl-target/cl-target.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2435 - (view) (download)

1 : jhr 1671 (* cl-target.sml
2 :     *
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 1853 structure N = CNames
16 : jhr 2102 structure ToC = TreeToC
17 : jhr 1671 structure ToCL = TreeToCL
18 :     structure HF = CLHeaderFrag
19 :     structure SF = CLSchedFrag
20 :    
21 :     (* revmap f l == List.rev(List.map f l) *)
22 :     fun revmap f = let
23 :     fun rmap ([], l) = l
24 :     | rmap (x::r, l) = rmap (r, f x :: l)
25 :     in
26 :     fn l => rmap (l, [])
27 :     end
28 :    
29 :     (* common arithmetic *)
30 :     fun #+# (a, b) = CL.mkBinOp(a, CL.#+, b)
31 :     fun #*# (a, b) = CL.mkBinOp(a, CL.#*, b)
32 :     infix 5 #+#
33 :     infix 6 #*#
34 :    
35 :     (* translate TreeIL types to shadow types *)
36 :     fun shadowTy ty = (case ty
37 :     of Ty.BoolTy => CL.T_Named "cl_uint"
38 :     | Ty.IntTy => CL.T_Named(RN.shadowIntTy ())
39 :     | Ty.TensorTy[] => CL.T_Named(RN.shadowRealTy ())
40 :     | Ty.TensorTy[n] => CL.T_Named(RN.shadowVecTy n)
41 :     | Ty.TensorTy[n, m] => CL.T_Named(RN.shadowMatTy(n,m))
42 : jhr 1793 | Ty.ImageTy info => CL.T_Named(RN.shadowImageTy(ImageInfo.dim info))
43 : jhr 1671 | _ => raise Fail(concat["TreeToC.trType(", Ty.toString ty, ")"])
44 :     (* end case *))
45 :    
46 :     (* FIXME: add comments that more clearly explain the difference between convertToShadow and
47 :     * convertStrandToShadow
48 :     *)
49 :     (* translate TreeIL types to shadow types *)
50 :     fun convertToShadow (ty, name) = (case ty
51 :     of Ty.IntTy => CL.mkAssign(
52 :     CL.mkSelect(CL.mkVar RN.shadowGlaobalsName, name),
53 :     CL.mkIndirect(CL.mkVar RN.globalsVarName, name))
54 :     | Ty.TensorTy[n]=> CL.mkCall(RN.convertToShadowVec n, [
55 :     CL.mkUnOp(CL.%&, CL.mkSelect(CL.mkVar RN.shadowGlaobalsName, name)),
56 :     CL.mkIndirect(CL.mkVar RN.globalsVarName, name)
57 :     ])
58 : jhr 1793 | Ty.ImageTy info => CL.mkCall(RN.shadowImageFunc(ImageInfo.dim info), [
59 : jhr 1671 CL.mkVar "context",
60 :     CL.mkUnOp(CL.%&, CL.mkSelect(CL.mkVar RN.shadowGlaobalsName, name)),
61 :     CL.mkIndirect(CL.mkVar RN.globalsVarName, name)
62 :     ])
63 :     | Ty.TensorTy[n, m] => CL.mkCall(RN.convertToShadowMat(m,n), [
64 :     CL.mkSelect(CL.mkVar RN.shadowGlaobalsName, name),
65 :     CL.mkIndirect(CL.mkVar RN.globalsVarName, name)
66 :     ])
67 :     | _ => CL.mkAssign(
68 :     CL.mkSelect(CL.mkVar RN.shadowGlaobalsName,name),
69 :     CL.mkIndirect(CL.mkVar RN.globalsVarName, name))
70 :     (* end case *))
71 :    
72 :     (* generate code to convert strand TreeIL types to shadow types *)
73 :     fun convertStrandToShadow (ty, name, selfIn, selfOut) = (case ty
74 :     of Ty.IntTy => CL.mkAssign(
75 :     CL.mkIndirect(CL.mkVar selfIn, name),
76 :     CL.mkIndirect(CL.mkVar selfOut, name))
77 :     | Ty.TensorTy[n]=> CL.mkCall(RN.convertToShadowVec n, [
78 :     CL.mkUnOp(CL.%&, CL.mkIndirect(CL.mkVar selfIn, name)),
79 :     CL.mkIndirect(CL.mkVar selfOut, name)
80 :     ])
81 :     | Ty.TensorTy[n, m] => CL.mkCall(RN.convertToShadowMat(m,n), [
82 :     CL.mkUnOp(CL.%&, CL.mkIndirect(CL.mkVar selfIn, name)),
83 :     CL.mkIndirect(CL.mkVar selfOut, name)
84 :     ])
85 :     | _ => CL.mkAssign(
86 :     CL.mkIndirect(CL.mkVar selfIn, name),
87 :     CL.mkIndirect(CL.mkVar selfOut, name))
88 :     (* end case *))
89 :    
90 :     (* helper functions for specifying parameters in various address spaces *)
91 : jhr 1853 fun clParam spc (ty, x) = CL.PARAM([spc], ty, x)
92 :     val globalParam = clParam "__global"
93 :     val constantParam = clParam "__constant"
94 :     val localParam = clParam "__local"
95 :     val privateParam = clParam "__private"
96 : jhr 1671
97 :     (* OpenCL global pointer type *)
98 :     fun globalPtr ty = CL.T_Qual("__global", CL.T_Ptr ty)
99 :    
100 :     (* lvalue/rvalue state variable *)
101 :     fun lvalueSV name = CL.mkIndirect(CL.mkVar "selfOut", name)
102 :     fun rvalueSV name = CL.mkIndirect(CL.mkVar "selfIn", name)
103 :    
104 :     type var = CL.typed_var
105 :     type exp = CL.exp
106 :     type stm = CL.stm
107 :    
108 :     (* OpenCL specific types *)
109 :     val clIntTy = CL.T_Named "cl_int"
110 :     val clProgramTy = CL.T_Named "cl_program"
111 :     val clKernelTy = CL.T_Named "cl_kernel"
112 :     val clCmdQueueTy = CL.T_Named "cl_command_queue"
113 :     val clContextTy = CL.T_Named "cl_context"
114 :     val clDeviceIdTy = CL.T_Named "cl_device_id"
115 :     val clPlatformIdTy = CL.T_Named "cl_platform_id"
116 :     val clMemoryTy = CL.T_Named "cl_mem"
117 :     val globPtrTy = CL.T_Ptr(CL.T_Named RN.globalsTy)
118 :     (* FIXME: what are these for? *)
119 :     datatype shadow_env = STRAND_SHADOW | GLOBAL_SHADOW
120 :    
121 :     (* variable or field that is mirrored between host and GPU *)
122 :     type mirror_var = {
123 :     (* FIXME: perhaps it would be cleaner to just track the TreeIL type of the variable? *)
124 :     hostTy : CL.ty, (* variable type on Host (i.e., C type) *)
125 :     shadowTy : CL.ty, (* host-side shadow type of GPU type *)
126 :     gpuTy : CL.ty, (* variable's type on GPU (i.e., OpenCL type) *)
127 :     hToS: stm, (* the statement that converts the variable to its *)
128 :     (* shadow representation *)
129 :     var : CL.var (* variable name *)
130 :     }
131 :    
132 :     datatype strand = Strand of {
133 :     name : string,
134 :     tyName : string,
135 :     state : mirror_var list,
136 :     output : (Ty.ty * CL.var), (* the strand's output variable (only one for now) *)
137 :     code : CL.decl list ref,
138 :     init_code: CL.decl ref
139 :     }
140 :    
141 :     datatype program = Prog of {
142 : jhr 2082 props : Properties.props,
143 : jhr 1671 globals : mirror_var list ref,
144 :     topDecls : CL.decl list ref,
145 :     strands : strand AtomTable.hash_table,
146 :     initially : CL.decl ref,
147 :     numDims: int ref, (* number of dimensions in initially iteration *)
148 :     imgGlobals: (string * int) list ref,
149 :     prFn: CL.decl ref,
150 :     outFn: CL.decl ref
151 :     }
152 :    
153 :     datatype env = ENV of {
154 :     info : env_info,
155 :     vMap : var V.Map.map,
156 :     scope : scope
157 :     }
158 :    
159 :     and env_info = INFO of {
160 :     prog : program
161 :     }
162 :    
163 :     and scope
164 :     = NoScope
165 :     | GlobalScope
166 :     | InitiallyScope
167 :     | StrandScope (* strand initialization *)
168 :     | MethodScope of StrandUtil.method_name (* method body; vars are state variables *)
169 :    
170 :     (* the supprted widths of vectors of reals on the target. *)
171 :     (* FIXME: for OpenCL 1.1, 3 is also valid *)
172 :     fun vectorWidths () = [2, 4, 8, 16]
173 :    
174 :     (* we do not support printing on the OpenCL target *)
175 :     val supportsPrinting = false
176 :    
177 :     (* tests for whether various expression forms can appear inline *)
178 :     fun inlineCons n = (n < 2) (* vectors are inline, but not matrices *)
179 :     val inlineMatrixExp = false (* can matrix-valued expressions appear inline? *)
180 :    
181 :     (* TreeIL to target translations *)
182 :     structure Tr =
183 :     struct
184 :     fun fragment (ENV{info, vMap, scope}, blk) = let
185 :     val (vMap, stms) = (case scope
186 :     of GlobalScope => ToC.trFragment (vMap, blk)
187 :     | InitiallyScope => ToC.trFragment (vMap, blk)
188 :     | _ => ToCL.trFragment (vMap, blk)
189 :     (* end case *))
190 :     in
191 :     (ENV{info=info, vMap=vMap, scope=scope}, stms)
192 :     end
193 :     fun block (ENV{vMap, scope, ...}, blk) = (case scope
194 :     of StrandScope => ToC.trBlock (vMap, blk)
195 :     | MethodScope name => ToCL.trBlock (vMap, blk)
196 :     | InitiallyScope => ToCL.trBlock (vMap, blk)
197 :     | _ => ToC.trBlock (vMap, blk)
198 :     (* end case *))
199 : nseltzer 1870 fun free (ENV{vMap, ...}, blk) = ToC.trFree (vMap, blk)
200 : jhr 1671 fun exp (ENV{vMap, ...}, e) = ToCL.trExp(vMap, e)
201 :     end
202 :    
203 :     (* variables *)
204 :     structure Var =
205 :     struct
206 :     fun mirror (ty, name, shadowEnv) = {
207 :     hostTy = ToC.trType ty,
208 :     shadowTy = shadowTy ty,
209 :     gpuTy = ToCL.trType ty,
210 :     hToS = case shadowEnv
211 :     of GLOBAL_SHADOW => convertToShadow (ty, name)
212 :     | STRAND_SHADOW => convertStrandToShadow(ty, name, "selfIn", "selfOut")
213 :     (* end case *),
214 :     var = name
215 :     }
216 :     fun name (ToCL.V(_, name)) = name
217 :     fun global (Prog{globals, imgGlobals, ...}, name, ty) = let
218 :     val x = mirror (ty, name, GLOBAL_SHADOW)
219 : jhr 1793 fun isImgGlobal (Ty.ImageTy info, name) =
220 :     imgGlobals := (name, ImageInfo.dim info) :: !imgGlobals
221 : jhr 1671 | isImgGlobal _ = ()
222 :     in
223 :     globals := x :: !globals;
224 :     isImgGlobal (ty, name);
225 :     ToCL.V(#gpuTy x, name)
226 :     end
227 :     fun param x = ToCL.V(ToCL.trType(V.ty x), V.name x)
228 :     end
229 :    
230 :     (* environments *)
231 :     structure Env =
232 :     struct
233 :     (* create a new environment *)
234 :     fun new prog = ENV{
235 :     info=INFO{prog = prog},
236 :     vMap = V.Map.empty,
237 :     scope = NoScope
238 :     }
239 :     (* bind a TreeIL varaiable to a target variable *)
240 :     fun bind (ENV{info, vMap, scope}, x, x') = ENV{
241 :     info = info,
242 :     vMap = V.Map.insert(vMap, x, x'),
243 :     scope = scope
244 :     }
245 : jhr 2102 (* define the current translation context *)
246 :     fun setScope (scope, glob, selfIn, selfOut) (ENV{info, vMap, ...}) = ENV{
247 :     info = info,
248 :     vMap = List.foldl
249 :     (fn ((x, x'), vm) => V.Map.insert(vm, x, x'))
250 :     vMap [
251 :     (PseudoVars.global, CL.V(CL.voidTy, glob)),
252 :     (PseudoVars.selfIn, CL.V(CL.voidTy, selfIn)),
253 :     (PseudoVars.selfOut, CL.V(CL.voidTy, selfOut))
254 :     ],
255 :     scope = scope
256 :     }
257 :     (* define the current translation context *)
258 :     val scopeGlobal = setScope (GlobalScope, RN.globalsVarName, "_bogus_", "_bogus_")
259 :     val scopeInitially =
260 :     setScope (InitiallyScope, RN.globalsVarName, "_bogus_", "_bogus_")
261 :     val scopeStrand = setScope (StrandScope, RN.globalsVarName, "selfIn", "selfOut")
262 :     fun scopeMethod (env, name) =
263 :     setScope (MethodScope name, RN.globalsVarName, "selfIn", "selfOut") env
264 : jhr 1671 end
265 :    
266 : jhr 1853 (* strands *)
267 :     structure Strand =
268 :     struct
269 :    
270 :     fun define (Prog{strands, ...}, strandId, state) = let
271 :     val name = Atom.toString strandId
272 :     (* the output state variable *)
273 :     val outputVar = (case List.filter IL.StateVar.isOutput state
274 :     of [] => raise Fail("no output specified for strand " ^ name)
275 :     | [x] => (IL.StateVar.ty x, IL.StateVar.name x)
276 :     | _ => raise Fail("multiple outputs in " ^ name)
277 :     (* end case *))
278 :     (* the state variables *)
279 :     val state = let
280 :     fun cvt x = Var.mirror (IL.StateVar.ty x, IL.StateVar.name x, STRAND_SHADOW)
281 :     in
282 :     List.map cvt state
283 :     end
284 :     val strand = Strand{
285 :     name = name,
286 :     tyName = RN.strandTy name,
287 :     state = state,
288 :     output = outputVar,
289 :     code = ref [],
290 :     init_code = ref (CL.D_Comment(["no init code"]))
291 :     }
292 :     in
293 :     AtomTable.insert strands (strandId, strand);
294 :     strand
295 :     end
296 :    
297 :     (* return the strand with the given name *)
298 :     fun lookup (Prog{strands, ...}, strandId) = AtomTable.lookup strands strandId
299 :    
300 :     (* register the strand-state initialization code. The variables are the strand
301 :     * parameters.
302 :     *)
303 :     fun init (Strand{name, tyName, code, init_code, ...}, params, init) = let
304 :     val fName = RN.strandInit name
305 :     val params =
306 :     clParam "" (CL.T_Ptr(CL.T_Named tyName), "selfOut") ::
307 :     List.map (fn (ToCL.V(ty, x)) => CL.PARAM([], ty, x)) params
308 :     val initFn = CL.D_Func([], CL.voidTy, fName, params, init)
309 :     in
310 :     init_code := initFn
311 :     end
312 :    
313 :     (* register a strand method *)
314 :     fun method (Strand{name, tyName, code,...}, methName, body) = let
315 :     val params = [
316 :     globalParam (CL.T_Ptr(CL.T_Named tyName), "selfIn"),
317 :     globalParam (CL.T_Ptr(CL.T_Named tyName), "selfOut"),
318 :     globalParam (CL.T_Ptr(CL.T_Named (RN.globalsTy)), RN.globalsVarName),
319 :     clParam "" (CL.T_Named(RN.imageDataType), RN.globalImageDataName)
320 :     ]
321 :     val (fName,resTy) = (case methName
322 :     of StrandUtil.Update => (RN.strandUpdate,CL.T_Named "StrandStatus_t")
323 :     | StrandUtil.Stabilize => (name ^ StrandUtil.nameToString methName, CL.voidTy)
324 :     (* end case *))
325 :     val methFn = CL.D_Func([], resTy, fName, params, body)
326 :     in
327 :     code := methFn :: !code
328 :     end
329 :    
330 :     end (* Strand *)
331 :    
332 : jhr 1671 (* programs *)
333 :     structure Program =
334 :     struct
335 : jhr 2076 fun new (tgt : TargetUtil.target_desc, props) = (
336 : jhr 1705 RN.initTargetSpec (#double tgt);
337 :     CNames.initTargetSpec {double = #double tgt, long = false};
338 : jhr 1671 Prog{
339 : jhr 2082 props = Properties.mkProps (tgt, props),
340 : jhr 1671 globals = ref [],
341 :     topDecls = ref [],
342 :     strands = AtomTable.mkTable (16, Fail "strand table"),
343 :     initially = ref(CL.D_Comment["missing initially"]),
344 :     numDims = ref 0,
345 :     imgGlobals = ref[],
346 :     prFn = ref(CL.D_Comment(["No Print Function"])),
347 :     outFn = ref(CL.D_Comment(["No Output Function"]))
348 :     })
349 :    
350 :     (* register the code that is used to register command-line options for input variables *)
351 : jhr 1803 fun inputs (Prog{topDecls, ...}, env, blk) = let
352 : jhr 1671 val inputsFn = CL.D_Func(
353 :     [], CL.voidTy, RN.registerOpts,
354 :     [CL.PARAM([], CL.T_Ptr(CL.T_Named RN.optionsTy), "opts")],
355 : jhr 1803 Tr.block (env, blk))
356 : jhr 1671 in
357 :     topDecls := inputsFn :: !topDecls
358 :     end
359 :    
360 :     (* register the global initialization part of a program *)
361 :     fun init (Prog{topDecls, ...}, init) = let
362 :     val globalsDecl = CL.mkAssign(CL.mkVar RN.globalsVarName,
363 :     CL.mkApply("malloc", [CL.mkSizeof(CL.T_Named RN.globalsTy)]))
364 :     val initFn = CL.D_Func(
365 :     [], CL.voidTy, RN.initGlobals, [],
366 :     CL.mkBlock[
367 :     globalsDecl,
368 :     CL.mkCall(RN.initGlobalsHelper, [CL.mkVar RN.globalsVarName])
369 :     ])
370 :     val initHelperFn = CL.D_Func(
371 :     [], CL.voidTy, RN.initGlobalsHelper,
372 :     [CL.PARAM([], globPtrTy, RN.globalsVarName)],
373 :     init)
374 :     val shutdownFn = CL.D_Func(
375 :     [], CL.voidTy, RN.shutdown,
376 :     [CL.PARAM([], CL.T_Ptr(CL.T_Named RN.worldTy), "wrld")],
377 :     CL.S_Block[])
378 :     in
379 :     topDecls := shutdownFn :: initFn :: initHelperFn :: !topDecls
380 :     end
381 : nseltzer 1870 (* register the global destruction part of a program *)
382 : jhr 2082 fun free (Prog{props, topDecls, ...}, free) = let
383 :     val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy props))
384 :     val globTy = CL.T_Ptr(CL.T_Named(N.globalTy props))
385 : nseltzer 1870 val free = CL.mkBlock(
386 :     CL.mkDeclInit(globTy, "glob", CL.mkIndirect(CL.mkVar "wrld", "globals")) ::
387 :     CL.unBlock free @ [CL.mkReturn(SOME(CL.mkVar "false"))])
388 :     val freeFn = CL.D_Func(
389 :     ["static"], CL.boolTy, N.freeGlobals,
390 :     [CL.PARAM([], worldTy, "wrld")],
391 :     free)
392 :     in
393 :     topDecls := freeFn :: !topDecls
394 :     end
395 : jhr 1671
396 :     (* create and register the initially function for a program *)
397 :     fun initially {
398 : jhr 2082 prog = Prog{props, strands, initially, numDims, ...},
399 : jhr 1671 isArray : bool,
400 :     iterPrefix : stm list,
401 :     iters : (var * exp * exp) list,
402 :     createPrefix : stm list,
403 :     strand : Atom.atom,
404 :     args : exp list
405 :     } = let
406 :     val name = Atom.toString strand
407 :     val nDims = List.length iters
408 : jhr 2082 val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy props))
409 : jhr 1671 fun mapi f xs = let
410 :     fun mapf (_, []) = []
411 :     | mapf (i, x::xs) = f(i, x) :: mapf(i+1, xs)
412 :     in
413 :     mapf (0, xs)
414 :     end
415 :     val baseInit = mapi (fn (i, (_, e, _)) => (i, CL.I_Exp e)) iters
416 :     val sizeInit = mapi
417 :     (fn (i, (CL.V(ty, _), lo, hi)) =>
418 :     (i, CL.I_Exp(CL.mkBinOp(hi, CL.#-, lo) #+# CL.mkIntTy(1, ty)))
419 :     ) iters
420 :     (* code to allocate the world and initial strands *)
421 :     val wrld = "wrld"
422 :     val allocCode = [
423 :     CL.mkComment["allocate initial block of strands"],
424 :     CL.mkDecl(CL.T_Array(CL.int32, SOME nDims), "base", SOME(CL.I_Array baseInit)),
425 :     CL.mkDecl(CL.T_Array(CL.uint32, SOME nDims), "size", SOME(CL.I_Array sizeInit)),
426 :     CL.mkDecl(worldTy, wrld,
427 :     SOME(CL.I_Exp(CL.mkApply(N.allocInitially, [
428 :     CL.mkVar "ProgramName",
429 :     CL.mkUnOp(CL.%&, CL.mkVar(N.strandDesc name)),
430 :     CL.mkBool isArray,
431 :     CL.mkIntTy(IntInf.fromInt nDims, CL.int32),
432 :     CL.mkVar "base",
433 :     CL.mkVar "size"
434 :     ]))))
435 :     ]
436 :     (* create the loop nest for the initially iterations *)
437 :     val indexVar = "ix"
438 :     val strandTy = CL.T_Ptr(CL.T_Named(N.strandTy name))
439 :     fun mkLoopNest [] = CL.mkBlock(createPrefix @ [
440 :     CL.mkDecl(strandTy, "sp",
441 :     SOME(CL.I_Exp(
442 :     CL.mkCast(strandTy,
443 :     CL.mkApply(N.inState, [CL.mkVar "wrld", CL.mkVar indexVar]))))),
444 :     CL.mkCall(N.strandInit name, CL.mkVar "sp" :: args),
445 :     CL.mkAssign(CL.mkVar indexVar, CL.mkVar indexVar #+# CL.mkIntTy(1, CL.uint32))
446 :     ])
447 :     | mkLoopNest ((CL.V(ty, param), lo, hi)::iters) = let
448 :     val body = mkLoopNest iters
449 :     in
450 :     CL.mkFor(
451 :     [(ty, param, lo)],
452 :     CL.mkBinOp(CL.mkVar param, CL.#<=, hi),
453 :     [CL.mkPostOp(CL.mkVar param, CL.^++)],
454 :     body)
455 :     end
456 :     val iterCode = [
457 :     CL.mkComment["initially"],
458 :     CL.mkDecl(CL.uint32, indexVar, SOME(CL.I_Exp(CL.mkIntTy(0, CL.uint32)))),
459 :     mkLoopNest iters
460 :     ]
461 :     val body = CL.mkBlock(
462 :     iterPrefix @
463 :     allocCode @
464 :     iterCode @
465 :     [CL.mkReturn(SOME(CL.mkVar "wrld"))])
466 : jhr 2082 val initFn = CL.D_Func([], worldTy, N.initially props, [], body)
467 : jhr 1671 in
468 :     numDims := nDims;
469 :     initially := initFn
470 :     end
471 :    
472 :    
473 :     (***** OUTPUT *****)
474 :    
475 :     fun genStrandPrint (Strand{name, tyName, state, output, code, ...}) = let
476 :     (* the print function *)
477 :     val prFnName = concat[name, "Print"]
478 :     val prFn = let
479 :     val params = [
480 :     CL.PARAM([], CL.T_Ptr(CL.T_Named "FILE"), "outS"),
481 :     CL.PARAM([], CL.T_Ptr(CL.T_Named(RN.strandShadowTy tyName)), "self")
482 :     ]
483 :     val (ty, x) = output
484 :     val outState = CL.mkIndirect(CL.mkVar "self", x)
485 :     val prArgs = (case ty
486 :     of Ty.IntTy => [CL.mkStr(!N.gIntFormat ^ "\n"), outState]
487 :     | Ty.SeqTy(Ty.IntTy, d) => let
488 :     fun sel i = CL.mkApply(
489 :     "VSUB",
490 :     [outState, CL.mkInt(IntInf.fromInt i)])
491 :     val fmt = CL.mkStr(
492 :     String.concatWith " " (List.tabulate(d, fn _ => !N.gIntFormat))
493 :     ^ "\n")
494 :     val args = List.tabulate (d, sel)
495 :     in
496 :     fmt :: args
497 :     end
498 :     | Ty.TensorTy[] => [CL.mkStr "%f\n", outState]
499 :     | Ty.TensorTy[d] => let
500 :     fun sel i = CL.mkApply(
501 :     "VSUB",
502 :     [outState, CL.mkInt(IntInf.fromInt i)])
503 :     val fmt = CL.mkStr(
504 :     String.concatWith " " (List.tabulate(d, fn _ => "%f"))
505 :     ^ "\n")
506 :     val args = List.tabulate (d, sel)
507 :     in
508 :     fmt :: args
509 :     end
510 :     | _ => raise Fail("genStrand: unsupported output type " ^ Ty.toString ty)
511 :     (* end case *))
512 :     in
513 :     CL.D_Func(["static"], CL.voidTy, prFnName, params,
514 :     CL.mkCall("fprintf", CL.mkVar "outS" :: prArgs))
515 :     end
516 :     in
517 :     prFn
518 :     end
519 :    
520 : jhr 1705 fun genStrandTyDef (targetTy,Strand{state,...}, tyName) = (case state
521 :     of [] => CL.D_Comment(["No Strand Defintiion Included"])
522 : jhr 1716 | _ => CL.D_StructDef(
523 :     NONE,
524 :     revmap (fn x => (targetTy x, #var x)) state,
525 :     SOME tyName)
526 : jhr 1705 (* end case *))
527 : jhr 1671
528 :     (* generates the globals buffers and arguments function *)
529 :     fun genConvertShadowTypes (Strand{name, tyName, state,...}) = let
530 : jhr 1705 (* Declare opencl setup objects *)
531 : jhr 1671 val errVar = "err"
532 :     val params = [
533 :     CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "selfOut"),
534 :     CL.PARAM([], CL.T_Ptr(CL.T_Named(RN.strandShadowTy tyName)), "selfIn")
535 :     ]
536 :     val body = List.map (fn (x:mirror_var) => #hToS x) state
537 :     in
538 :     CL.D_Func([], CL.voidTy, RN.strandConvertName name, params, CL.mkBlock body)
539 :     end
540 :    
541 :     (* generates the opencl buffers for the image data *)
542 :     fun getGlobalDataBuffers (globals, imgGlobals, contextVar, errVar) = let
543 :     val globalBuffErr = "error creating OpenCL global buffer\n"
544 :     fun errorFn msg = CL.mkIfThen(CL.mkBinOp(CL.mkVar errVar, CL.#!=, CL.mkVar "CL_SUCCESS"),
545 :     CL.mkBlock([CL.mkCall("fprintf",[CL.mkVar "stderr", CL.mkStr msg]),
546 :     CL.mkCall("exit",[CL.mkInt 1])]))
547 :     val shadowTypeDecl =
548 :     CL.mkDecl(CL.T_Named(RN.shadowGlobalsTy), RN.shadowGlaobalsName, NONE)
549 :     val globalToShadowStms = List.map (fn (x:mirror_var) => #hToS x) globals
550 :     val globalBufferDecl = CL.mkDecl(clMemoryTy,concat[RN.globalsVarName,"_cl"],NONE)
551 :     val globalBuffer = CL.mkAssign(CL.mkVar(concat[RN.globalsVarName,"_cl"]),
552 :     CL.mkApply("clCreateBuffer", [
553 :     CL.mkVar contextVar,
554 :     CL.mkBinOp(CL.mkVar "CL_MEM_READ_ONLY", CL.#|, CL.mkVar "CL_MEM_COPY_HOST_PTR"),
555 :     CL.mkSizeof(CL.T_Named RN.shadowGlobalsTy),
556 :     CL.mkUnOp(CL.%&,CL.mkVar RN.shadowGlaobalsName),
557 :     CL.mkUnOp(CL.%&,CL.mkVar errVar)
558 :     ]))
559 :     fun genDataBuffers ([],_,_,_) = []
560 :     | genDataBuffers ((var,nDims)::globals, contextVar, errVar,errFn) = let
561 :     val hostVar = CL.mkIndirect(CL.mkVar RN.globalsVarName, var)
562 :     val size = CL.mkIndirect(hostVar, "dataSzb")
563 :     in
564 :     CL.mkDecl(clMemoryTy, RN.addBufferSuffixData var ,NONE) ::
565 :     CL.mkAssign(CL.mkVar(RN.addBufferSuffixData var),
566 :     CL.mkApply("clCreateBuffer", [
567 :     CL.mkVar contextVar,
568 :     CL.mkVar "CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR",
569 :     size,
570 :     CL.mkIndirect(hostVar, "data"),
571 :     CL.mkUnOp(CL.%&,CL.mkVar errVar)
572 :     ])) ::
573 :     errFn(concat["error in creating ",RN.addBufferSuffixData var, " global buffer\n"]) ::
574 :     genDataBuffers(globals,contextVar,errVar,errFn)
575 :     end
576 :     in
577 :     [shadowTypeDecl] @ globalToShadowStms
578 :     @ [globalBufferDecl, globalBuffer,errorFn(globalBuffErr)]
579 :     @ genDataBuffers(imgGlobals,contextVar,errVar,errorFn)
580 :     end
581 :    
582 :     (* generates the kernel arguments for the image data *)
583 :     fun genGlobalArguments (globals, count, kernelVar, errVar) = let
584 :     val globalArgErr = "error creating OpenCL global argument\n"
585 :     fun errorFn msg = CL.mkIfThen(CL.mkBinOp(CL.mkVar errVar, CL.#!=, CL.mkVar "CL_SUCCESS"),
586 :     CL.mkBlock([CL.mkCall("fprintf",[CL.mkVar "stderr", CL.mkStr msg]),
587 :     CL.mkCall("exit",[CL.mkInt 1])]))
588 :     val globalArgument = CL.mkExpStm(CL.mkAssignOp(CL.mkVar errVar,CL.&=,
589 :     CL.mkApply("clSetKernelArg",
590 :     [CL.mkVar kernelVar,
591 :     CL.mkPostOp(CL.mkVar count, CL.^++),
592 :     CL.mkApply("sizeof",[CL.mkVar "cl_mem"]),
593 :     CL.mkUnOp(CL.%&,CL.mkVar(concat[RN.globalsVarName,"_cl"]))])))
594 :     fun genDataArguments ([],_,_,_,_) = []
595 :     | genDataArguments ((var,nDims)::globals,count,kernelVar,errVar,errFn) =
596 :     CL.mkExpStm(CL.mkAssignOp(CL.mkVar errVar,CL.$=,
597 :     CL.mkApply("clSetKernelArg",
598 :     [CL.mkVar kernelVar,
599 :     CL.mkPostOp(CL.mkVar count, CL.^++),
600 :     CL.mkApply("sizeof",[CL.mkVar "cl_mem"]),
601 :     CL.mkUnOp(CL.%&,CL.mkVar(RN.addBufferSuffixData var))]))) ::
602 :     errFn(concat["error in creating ",RN.addBufferSuffixData var, " argument\n"]) ::
603 :     genDataArguments (globals,count,kernelVar,errVar,errFn)
604 :     in
605 :     globalArgument :: errorFn globalArgErr ::
606 :     genDataArguments(globals, count, kernelVar, errVar,errorFn)
607 :     end
608 :    
609 :     (* generates the globals buffers and arguments function *)
610 :     fun genGlobalBuffersArgs (globals,imgGlobals) = let
611 :     (* Delcare opencl setup objects *)
612 :     val errVar = "err"
613 :     val params = [
614 :     CL.PARAM([],CL.T_Named("cl_context"), "context"),
615 :     CL.PARAM([],CL.T_Named("cl_kernel"), "kernel"),
616 :     CL.PARAM([],CL.T_Named("cl_command_queue"), "cmdQ"),
617 :     CL.PARAM([],CL.T_Named("int"), "argStart")
618 :     ]
619 :     val body = (case globals
620 :     of [] => [CL.mkReturn(NONE)]
621 :     | _ => let
622 :     val clGlobalBuffers =
623 :     getGlobalDataBuffers(globals, !imgGlobals, "context", errVar)
624 :     val clGlobalArguments =
625 :     genGlobalArguments(!imgGlobals, "argStart", "kernel", errVar)
626 :     in
627 :     (* Body: put all the statments together *)
628 :     CL.mkDecl(clIntTy, errVar, SOME(CL.I_Exp(CL.mkInt 0)))
629 :     :: clGlobalBuffers @ clGlobalArguments
630 :     end
631 :     (*end of case*))
632 :     in
633 :     CL.D_Func([],CL.voidTy,RN.globalsSetupName,params,CL.mkBlock(body))
634 :     end
635 :    
636 :     (* generate the global image meta-data and data parameters *)
637 :     fun genKeneralGlobalParams ((name,tyname)::[],line) =
638 :     concat[line, "__global void *", RN.addBufferSuffixData name]
639 :     | genKeneralGlobalParams ([],line) = line
640 :     | genKeneralGlobalParams ((name,tyname)::rest, line) =
641 :     genKeneralGlobalParams(rest, concat[line, "__global void *", RN.addBufferSuffixData name, ",\n"])
642 :    
643 :     fun genUpdateMethod (Strand{name, tyName, state,...}, globals, imgGlobals) = let
644 :     val imageDataStms = List.map
645 :     (fn (x,_) => concat[
646 :     RN.globalImageDataName, ".", RN.imageDataName x, " = ",
647 :     RN.addBufferSuffixData x, ";","\n"
648 :     ])
649 :     (!imgGlobals)
650 :     fun select ([], a, _) = a
651 :     | select (_, _, b) = b
652 :     val placeHolders = [
653 :     (RN.place_holders, tyName),
654 :     (RN.p_addDatPtr, select (!imgGlobals, "", ",")),
655 :     (RN.p_addGlobals, select (!globals, "", ",")),
656 :     (RN.p_globals, select (!globals, "", "__global Diderot_Globals_t *diderotGlobals")),
657 :     (RN.p_globalVar, select (!globals, "0", RN.globalsVarName)),
658 :     (RN.p_dataVar, select (!globals, "0", RN.globalImageDataName)),
659 :     (RN.p_dataPtr, genKeneralGlobalParams (!imgGlobals, "")),
660 :     (RN.p_dataAssign, select (!imgGlobals, "",
661 :     String.concat("Diderot_data_ptr_t diderotDataPtrs;\n" :: imageDataStms)))
662 :     ]
663 :     in
664 : jhr 2116 CL.verbatimDcl [CLUpdateFrag.text] placeHolders
665 : jhr 1671 end
666 :    
667 :     fun genStrandCopy(Strand{tyName,name,state,...}) = let
668 :     val params = [
669 :     CL.PARAM(["__global"], CL.T_Ptr(CL.T_Named tyName), "selfIn"),
670 :     CL.PARAM(["__global"], CL.T_Ptr(CL.T_Named tyName), "selfOut")
671 :     ]
672 :     val assignStms = List.rev(
673 :     List.map
674 :     (fn x => CL.mkAssign(lvalueSV(#var x), rvalueSV(#var x)))
675 :     state)
676 :     in
677 :     CL.D_Func([""], CL.voidTy, RN.strandCopy, params,CL.mkBlock(assignStms))
678 :     end
679 :    
680 :     (* generate a global structure type definition from the list of globals *)
681 :     fun genGlobalStruct (_, [], _) = CL.D_Comment(["No Global Definition"])
682 :     | genGlobalStruct (targetTy, globals, tyName) = let
683 :     val globs = List.map (fn (x : mirror_var) => (targetTy x, #var x)) globals
684 :     in
685 : jhr 1716 CL.D_StructDef(NONE, globs, SOME tyName)
686 : jhr 1671 end
687 :    
688 :     (* generate a global structure type definition from the image data of the image globals *)
689 :     fun genImageDataStruct ([], _) = CL.D_Comment(["No Image Data Ptrs Definition"])
690 :     | genImageDataStruct (imgGlobals, tyName) = let
691 :     val globs = List.map
692 :     (fn (x, _) => (globalPtr CL.voidTy, RN.imageDataName x))
693 :     imgGlobals
694 :     in
695 : jhr 1716 CL.D_StructDef(NONE, globs, SOME tyName)
696 : jhr 1671 end
697 :    
698 :     fun genGlobals (declFn, targetTy, globals) = let
699 :     fun doVar (x : mirror_var) = declFn (CL.D_Var([], targetTy x, #var x, NONE))
700 :     in
701 :     List.app doVar globals
702 :     end
703 :    
704 :     fun genOutputFun(Strand{name, output,tyName, state, code,...}) = let
705 :     (* the output function *)
706 :     val outFnName = concat[name, "_Output"]
707 :     val outFun = let
708 :     val params = [
709 :     CL.PARAM([], CL.T_Ptr CL.voidTy, "outS"),
710 :     CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "self")
711 :     ]
712 :     (* the type and access expression for the strand's output variable *)
713 :     val (outTy, outState) = (#1 output, CL.mkIndirect(CL.mkVar "self", #2 output))
714 :     val outState = CL.mkUnOp(CL.%&, outState)
715 :     in
716 :     CL.D_Func(["static"], CL.voidTy, outFnName, params,
717 :     CL.mkCall("memcpy", [CL.mkVar "outS", outState, CL.mkSizeof(shadowTy outTy)] ))
718 :     end
719 :     in
720 :     outFun
721 :     end
722 :    
723 :     fun genStrandDesc (outFn,Strand{name, output,tyName, state, code,...}) = let
724 :     (* the output function *)
725 :     val outFnName = concat[name, "_Output"]
726 :     (* the strand's descriptor object *)
727 :     val descI = let
728 :     fun fnPtr (ty, f) = CL.I_Exp(CL.mkCast(CL.T_Named ty, CL.mkVar f))
729 :     val (outTy, _) = output
730 :     in
731 :     CL.I_Struct[
732 :     ("name", CL.I_Exp(CL.mkStr name)),
733 :     ("stateSzb", CL.I_Exp(CL.mkSizeof(CL.T_Named(RN.strandTy name)))),
734 :     ("shadowStrandSzb", CL.I_Exp(CL.mkSizeof(CL.T_Named(RN.strandShadowTy (RN.strandTy name))))),
735 :     (* FIXME: we may need to add a shadowOutputSzb field too for OpenCL *)
736 :     ("outputSzb", CL.I_Exp(CL.mkSizeof(shadowTy outTy))),
737 : jhr 1789 (* DEPRECATED
738 : jhr 1671 ("nrrdType", CL.I_Exp(CL.mkInt (NrrdTypes.toNrrdType outTy))),
739 : jhr 1789 ("nrrdSzb", CL.I_Exp(CL.mkInt (NrrdTypes.toNrrdSize outTy))),
740 :     *)
741 : jhr 1671 ("update", fnPtr("update_method_t", "0")),
742 :     ("strandCopy", fnPtr("convert_method_t", RN.strandConvertName name)),
743 :     ("print", fnPtr("print_method_t", RN.strandPrintName name)),
744 :     ("output", fnPtr("output_method_t", outFnName)) (* FIXME *)
745 :     ]
746 :     end
747 :     val desc = CL.D_Var([], CL.T_Named N.strandDescTy, N.strandDesc name, SOME descI)
748 :     in
749 :     desc
750 :     end
751 :    
752 :     (* generate the table of strand descriptors *)
753 :     fun genStrandTable (declFn, strands) = let
754 :     val nStrands = length strands
755 :     fun genInit (Strand{name, ...}) = CL.I_Exp(CL.mkUnOp(CL.%&, CL.mkVar(N.strandDesc name)))
756 :     fun genInits (_, []) = []
757 :     | genInits (i, s::ss) = (i, genInit s) :: genInits(i+1, ss)
758 :     in
759 :     declFn (CL.D_Var([], CL.int32, N.numStrands,
760 :     SOME(CL.I_Exp(CL.mkIntTy(IntInf.fromInt nStrands, CL.int32)))));
761 :     declFn (CL.D_Var([],
762 :     CL.T_Array(CL.T_Ptr(CL.T_Named N.strandDescTy), SOME nStrands),
763 :     N.strands,
764 :     SOME(CL.I_Array(genInits (0, strands)))))
765 :     end
766 :    
767 :     fun genSrc (baseName, prog) = let
768 :     val Prog{
769 : jhr 2082 props,
770 : jhr 1705 globals, topDecls, strands, initially,
771 : jhr 1671 imgGlobals, numDims,outFn, ...
772 :     } = prog
773 :     val clFileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "cl"}
774 :     val cFileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"}
775 :     val clOutS = TextIO.openOut clFileName
776 :     val cOutS = TextIO.openOut cFileName
777 :     val clppStrm = PrintAsCL.new clOutS
778 :     val cppStrm = PrintAsC.new cOutS
779 :     fun cppDecl dcl = PrintAsC.output(cppStrm, dcl)
780 :     fun clppDecl dcl = PrintAsCL.output(clppStrm, dcl)
781 :     val strands = AtomTable.listItems strands
782 :     val [strand as Strand{name, tyName, code, init_code, ...}] = strands
783 :     in
784 :     (* Generate the OpenCL file *)
785 :     (* Retrieve the header information *)
786 : jhr 2116 clppDecl (CL.verbatimDcl [HF.text] [
787 : jhr 1671 ("OUTFILE", clFileName),
788 :     ("SRCFILE", OS.Path.joinBaseExt{base=baseName, ext=SOME "diderot"}),
789 : jhr 2082 ("DIDEROT_FLOAT_PRECISION", Properties.floatPrecisionDef props),
790 :     ("DIDEROT_INT_PRECISION", Properties.intPrecisionDef props)
791 : jhr 1671 ]);
792 :     (* if there are no globals, then define a dummy type *)
793 :     if List.null(!globals)
794 :     then clppDecl (CL.D_Verbatim["typedef void ", RN.globalsTy, ";\n"])
795 :     else ();
796 :     (* if there are no images, then define a dummy type *)
797 :     if List.null(!imgGlobals)
798 :     then clppDecl (CL.D_Verbatim["typedef void * ", RN.imageDataType, ";\n"])
799 :     else ();
800 :     (* Retrieve the scheduler kernels and functions *)
801 :     clppDecl (CL.D_Verbatim[SF.text]);
802 :     clppDecl (CL.D_Verbatim[CLEigen2x2Frag.text]);
803 :     clppDecl (CL.D_Verbatim[CLEigen3x3Frag.text]);
804 :     clppDecl (genGlobalStruct (#gpuTy, !globals, RN.globalsTy));
805 :     clppDecl (genImageDataStruct(!imgGlobals, RN.imageDataType));
806 :     clppDecl (genStrandTyDef(#gpuTy, strand, tyName));
807 :     List.app clppDecl (!code);
808 :     clppDecl (genStrandCopy strand);
809 :     clppDecl (genUpdateMethod(strand, globals, imgGlobals));
810 :     (* Generate the Host C file *)
811 :     cppDecl (CL.D_Verbatim[
812 : jhr 2082 concat["#define " ^ Properties.floatPrecisionDef props, "\n"],
813 :     concat["#define " ^ Properties.intPrecisionDef props, "\n"],
814 :     concat["#define " ^ Properties.targetDef props, "\n"],
815 : jhr 1671 "#include \"Diderot/diderot.h\"\n"
816 : jhr 1705 ]);
817 :     case !globals
818 :     of [] => cppDecl (CL.D_Verbatim[concat["typedef void ", RN.globalsTy, ";\n"] ])
819 :     | _ => ()
820 :     (* end case *);
821 : jhr 1671 cppDecl (CL.D_Var(["static"], CL.charPtr, "ProgramName",
822 : jhr 2082 SOME(CL.I_Exp(CL.mkStr(#srcFile props)))));
823 : jhr 1671 cppDecl (genGlobalStruct (#hostTy, !globals, RN.globalsTy));
824 :     cppDecl (genGlobalStruct (#shadowTy, !globals, RN.shadowGlobalsTy));
825 :     (* FIXME: does this really need to be a global? *)
826 :     cppDecl (CL.D_Var(["static"], globPtrTy, RN.globalsVarName, NONE));
827 :     cppDecl (genStrandTyDef (#hostTy, strand, tyName));
828 :     cppDecl (genStrandTyDef (#shadowTy, strand, RN.strandShadowTy tyName));
829 :     cppDecl (genConvertShadowTypes strand);
830 :     cppDecl (!init_code);
831 :     cppDecl (genStrandPrint strand);
832 :     cppDecl (genOutputFun strand);
833 :     List.app cppDecl (List.rev (!topDecls));
834 :     cppDecl (genGlobalBuffersArgs (!globals,imgGlobals));
835 :     List.app (fn strand => cppDecl (genStrandDesc (outFn,strand))) strands;
836 :     genStrandTable (cppDecl, strands);
837 :     cppDecl (!initially);
838 :     PrintAsC.close cppStrm;
839 :     PrintAsCL.close clppStrm;
840 :     TextIO.closeOut cOutS;
841 :     TextIO.closeOut clOutS
842 :     end
843 :    
844 :     (* output the code to the filesystem. The string is the basename of the source file *)
845 : jhr 2082 fun generate (prog as Prog{props, ...}) = let
846 :     val {outDir, outBase, exec, double, parallel, debug, ...} = props
847 : jhr 1671 fun condCons (true, x, xs) = x::xs
848 :     | condCons (false, _, xs) = xs
849 :     (* generate the C compiler flags *)
850 : jhr 2435 val cflags = ["-I" ^ Paths.diderotInclude(), "-I" ^ Paths.teemInclude()]
851 : jhr 1843 val cflags = condCons (#cl Paths.cflags <> "", #cl Paths.cflags, cflags)
852 : jhr 1671 val cflags = condCons (parallel, #pthread Paths.cflags, cflags)
853 :     val cflags = if debug
854 :     then #debug Paths.cflags :: cflags
855 :     else #ndebug Paths.cflags :: cflags
856 :     val cflags = #base Paths.cflags :: cflags
857 :     (* generate the loader flags *)
858 :     val extraLibs = condCons (parallel, #pthread Paths.extraLibs, [])
859 : jhr 2430 val extraLibs = Paths.teemLinkFlags() @ #base Paths.extraLibs :: extraLibs
860 : jhr 1671 val extraLibs = #cl Paths.extraLibs :: extraLibs
861 : jhr 2082 val rtLib = Properties.runtimeName props
862 : jhr 1671 val ldOpts = rtLib :: extraLibs
863 : jhr 1705 val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
864 : jhr 1671 in
865 :     genSrc (basename, prog);
866 :     RunCC.compile (basename, cflags);
867 : jhr 1714 RunCC.linkExec (basename, ldOpts)
868 : jhr 1671 end
869 :    
870 :     end (* Program *)
871 :    
872 :     end
873 :    
874 :     structure CLBackEnd = CodeGenFn(CLTarget)

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