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

1 : lamonts 1244 (* c-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 1273 structure ToCL = TreeToCL
16 : lamonts 1305 structure N = CNames
17 : lamonts 1244
18 : lamonts 1305 (* variable translation *)
19 :     structure TrVar =
20 :     struct
21 :     type env = CL.typed_var TreeIL.Var.Map.map
22 :     fun lookup (env, x) = (case V.Map.find (env, x)
23 :     of SOME(CL.V(_, x')) => x'
24 :     | NONE => raise Fail(concat["lookup(_, ", V.name x, ")"])
25 :     (* end case *))
26 :     (* translate a variable that occurs in an l-value context (i.e., as the target of an assignment) *)
27 :     fun lvalueVar (env, x) = (case V.kind x
28 :     of IL.VK_Global => CL.mkVar(lookup(env, x))
29 :     | IL.VK_State strand => CL.mkIndirect(CL.mkVar "selfOut", lookup(env, x))
30 :     | IL.VK_Local => CL.mkVar(lookup(env, x))
31 :     (* end case *))
32 :     (* translate a variable that occurs in an r-value context *)
33 :     fun rvalueVar (env, x) = (case V.kind x
34 :     of IL.VK_Global => CL.mkVar(lookup(env, x))
35 :     | IL.VK_State strand => CL.mkIndirect(CL.mkVar "selfIn", lookup(env, x))
36 :     | IL.VK_Local => CL.mkVar(lookup(env, x))
37 :     (* end case *))
38 :     end
39 :    
40 :     structure ToC = TreeToCFn (TrVar)
41 :    
42 : jhr 1285 (* C variable translation *)
43 :     structure TrCVar =
44 :     struct
45 :     type env = CL.typed_var TreeIL.Var.Map.map
46 :     fun lookup (env, x) = (case V.Map.find (env, x)
47 :     of SOME(CL.V(_, x')) => x'
48 :     | NONE => raise Fail(concat["TrCVar.lookup(_, ", V.name x, ")"])
49 :     (* end case *))
50 :     (* translate a variable that occurs in an l-value context (i.e., as the target of an assignment) *)
51 :     fun lvalueVar (env, x) = (case V.kind x
52 : jhr 1286 of IL.VK_Global => CL.mkIndirect(CL.mkVar RN.globalsVarName, lookup(env, x))
53 : jhr 1285 | IL.VK_State strand => raise Fail "unexpected strand context"
54 :     | IL.VK_Local => CL.mkVar(lookup(env, x))
55 :     (* end case *))
56 :     (* translate a variable that occurs in an r-value context *)
57 :     val rvalueVar = lvalueVar
58 :     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 1279 (* OpenCL specific types *)
67 :     val clProgramTy = CL.T_Named "cl_program"
68 :     val clKernelTy = CL.T_Named "cl_kernel"
69 :     val clCmdQueueTy = CL.T_Named "cl_command_queue"
70 :     val clContextTy = CL.T_Named "cl_context"
71 :     val clDeviceIdTy = CL.T_Named "cl_device_id"
72 :     val clPlatformIdTy = CL.T_Named "cl_platform_id"
73 :     val clMemoryTy = CL.T_Named "cl_mem"
74 :    
75 : lamonts 1244 datatype strand = Strand of {
76 : jhr 1261 name : string,
77 :     tyName : string,
78 :     state : var list ref,
79 :     output : (Ty.ty * CL.var) option ref, (* the strand's output variable (only one for now) *)
80 : lamonts 1271 code : CL.decl list ref,
81 : jhr 1273 init_code: CL.decl ref
82 : lamonts 1244 }
83 :    
84 :     datatype program = Prog of {
85 : lamonts 1305 name : string, (* stem of source file *)
86 : jhr 1261 double : bool, (* true for double-precision support *)
87 :     parallel : bool, (* true for multithreaded (or multi-GPU) target *)
88 :     debug : bool, (* true for debug support in executable *)
89 : lamonts 1305 globals : {target:TargetUtil.target, globalTy:CL.ty, name:CLang.var} list ref,
90 : jhr 1261 topDecls : CL.decl list ref,
91 :     strands : strand AtomTable.hash_table,
92 : lamonts 1305 initially : CL.decl ref,
93 : jhr 1273 numDims: int ref,
94 :     imgGlobals: (string * int) list ref,
95 :     prFn: CL.decl ref
96 :     }
97 : lamonts 1244
98 :     datatype env = ENV of {
99 : jhr 1261 info : env_info,
100 :     vMap : var V.Map.map,
101 :     scope : scope
102 : lamonts 1244 }
103 :    
104 :     and env_info = INFO of {
105 : jhr 1261 prog : program
106 : lamonts 1244 }
107 :    
108 :     and scope
109 :     = NoScope
110 :     | GlobalScope
111 :     | InitiallyScope
112 : jhr 1261 | StrandScope of TreeIL.var list (* strand initialization *)
113 :     | MethodScope of TreeIL.var list (* method body; vars are state variables *)
114 : lamonts 1244
115 : jhr 1273 (* the supprted widths of vectors of reals on the target. *)
116 :     (* FIXME: for OpenCL 1.1, 3 is also valid *)
117 :     fun vectorWidths () = [2, 4, 8, 16]
118 : lamonts 1244
119 :     (* tests for whether various expression forms can appear inline *)
120 : jhr 1261 fun inlineCons n = (n < 2) (* vectors are inline, but not matrices *)
121 :     val inlineMatrixExp = false (* can matrix-valued expressions appear inline? *)
122 : lamonts 1244
123 :     (* TreeIL to target translations *)
124 :     structure Tr =
125 :     struct
126 : jhr 1286 (* this function is used for the initially clause, so it generates OpenCL *)
127 : jhr 1261 fun fragment (ENV{info, vMap, scope}, blk) = let
128 : jhr 1273 val (vMap, stms) = ToCL.trFragment (vMap, blk)
129 : jhr 1261 in
130 :     (ENV{info=info, vMap=vMap, scope=scope}, stms)
131 :     end
132 :     fun saveState cxt stateVars (env, args, stm) = (
133 :     ListPair.foldrEq
134 : jhr 1273 (fn (x, e, stms) => ToCL.trAssign(env, x, e)@stms)
135 : jhr 1261 [stm]
136 :     (stateVars, args)
137 :     ) handle ListPair.UnequalLengths => (
138 :     print(concat["saveState ", cxt, ": length mismatch; ", Int.toString(List.length args), " args\n"]);
139 :     raise Fail(concat["saveState ", cxt, ": length mismatch"]))
140 :     fun block (ENV{vMap, scope, ...}, blk) = (case scope
141 : jhr 1273 of StrandScope stateVars => ToCL.trBlock (vMap, saveState "StrandScope" stateVars, blk)
142 :     | MethodScope stateVars => ToCL.trBlock (vMap, saveState "MethodScope" stateVars, blk)
143 : jhr 1286 | InitiallyScope => ToCL.trBlock (vMap, fn (_, _, stm) => [stm], blk)
144 :     | _ => ToC.trBlock (vMap, fn (_, _, stm) => [stm], blk)
145 : jhr 1261 (* end case *))
146 : jhr 1273 fun exp (ENV{vMap, ...}, e) = ToCL.trExp(vMap, e)
147 : lamonts 1244 end
148 :    
149 :     (* variables *)
150 :     structure Var =
151 :     struct
152 : jhr 1273 fun name (ToCL.V(_, name)) = name
153 : lamonts 1305 fun global (Prog{globals, imgGlobals, ...}, global_name, ty) = let
154 :     val cl_ty = ToCL.trType ty
155 :     val c_ty = ToC.trType ty
156 : jhr 1261 fun isImgGlobal (imgGlobals, Ty.ImageTy(ImageInfo.ImgInfo{dim, ...}), name) = imgGlobals := (name,dim):: !imgGlobals
157 :     | isImgGlobal (imgGlobals, _, _) = ()
158 :     in
159 : lamonts 1305 globals := {target =TargetUtil.TARGET_CL,globalTy = cl_ty, name = global_name} :: !globals;
160 :     globals := {target =TargetUtil.TARGET_C, globalTy = c_ty, name = global_name} :: !globals;
161 :     isImgGlobal(imgGlobals,ty,global_name);
162 :     ToCL.V(cl_ty, global_name)
163 : jhr 1261 end
164 : jhr 1273 fun param x = ToCL.V(ToCL.trType(V.ty x), V.name x)
165 : jhr 1261 fun state (Strand{state, ...}, x) = let
166 : jhr 1273 val ty' = ToCL.trType(V.ty x)
167 :     val x' = ToCL.V(ty', V.name x)
168 : jhr 1261 in
169 :     state := x' :: !state;
170 :     x'
171 :     end
172 : lamonts 1244 end
173 :    
174 :     (* environments *)
175 :     structure Env =
176 :     struct
177 :     (* create a new environment *)
178 : jhr 1261 fun new prog = ENV{
179 :     info=INFO{prog = prog},
180 :     vMap = V.Map.empty,
181 :     scope = NoScope
182 :     }
183 : lamonts 1244 (* define the current translation context *)
184 : jhr 1261 fun setScope scope (ENV{info, vMap, ...}) = ENV{info=info, vMap=vMap, scope=scope}
185 :     val scopeGlobal = setScope GlobalScope
186 :     val scopeInitially = setScope InitiallyScope
187 :     fun scopeStrand (env, svars) = setScope (StrandScope svars) env
188 :     fun scopeMethod (env, svars) = setScope (MethodScope svars) env
189 : lamonts 1244 (* bind a TreeIL varaiable to a target variable *)
190 : jhr 1261 fun bind (ENV{info, vMap, scope}, x, x') = ENV{
191 :     info = info,
192 :     vMap = V.Map.insert(vMap, x, x'),
193 :     scope = scope
194 :     }
195 : lamonts 1244 end
196 :    
197 :     (* programs *)
198 :     structure Program =
199 :     struct
200 : jhr 1278 fun new {name, double, parallel, debug} = (
201 : jhr 1261 RN.initTargetSpec double;
202 : jhr 1286 CNames.initTargetSpec double;
203 : jhr 1261 Prog{
204 : lamonts 1305 name = name,
205 : jhr 1261 double = double, parallel = parallel, debug = debug,
206 : lamonts 1271 globals = ref [],
207 : jhr 1261 topDecls = ref [],
208 :     strands = AtomTable.mkTable (16, Fail "strand table"),
209 : lamonts 1305 initially = ref(CL.D_Comment["missing initially"]),
210 : lamonts 1264 numDims = ref(0),
211 :     imgGlobals = ref[],
212 :     prFn = ref(CL.D_Comment(["No Print Function"]))
213 : jhr 1261 })
214 : lamonts 1244 (* register the global initialization part of a program *)
215 : lamonts 1264 fun globalIndirects (globals,stms) = let
216 : lamonts 1305 fun getGlobals ({name,target as TargetUtil.TARGET_CL}::rest) =
217 :     CL.mkAssign(CL.mkIndirect(CL.mkVar RN.globalsVarName,name),CL.mkVar name)
218 : jhr 1281 ::getGlobals rest
219 :     | getGlobals [] = []
220 :     | getGlobals (_::rest) = getGlobals rest
221 : jhr 1273 in
222 : jhr 1281 stms @ getGlobals globals
223 : jhr 1273 end
224 : jhr 1261
225 :     (* register the code that is used to register command-line options for input variables *)
226 :     fun inputs (Prog{topDecls, ...}, stm) = let
227 :     val inputsFn = CL.D_Func(
228 :     [], CL.voidTy, RN.registerOpts,
229 :     [CL.PARAM([], CL.T_Ptr(CL.T_Named RN.optionsTy), "opts")],
230 :     stm)
231 :     in
232 :     topDecls := inputsFn :: !topDecls
233 :     end
234 :    
235 : jhr 1286 (* register the global initialization part of a program *)
236 : lamonts 1305 fun init (Prog{topDecls, ...}, init) = let
237 : jhr 1286 val globPtrTy = CL.T_Ptr(CL.T_Named RN.globalsTy)
238 :     val initFn = CL.D_Func(
239 :     [], CL.voidTy, RN.initGlobals, [CL.PARAM([], globPtrTy, RN.globalsVarName)],
240 :     init)
241 :     val shutdownFn = CL.D_Func(
242 :     [], CL.voidTy, RN.shutdown,
243 :     [CL.PARAM([], CL.T_Ptr(CL.T_Named RN.worldTy), "wrld")],
244 :     CL.S_Block[])
245 :     in
246 :     topDecls := shutdownFn :: initFn :: !topDecls
247 :     end
248 : lamonts 1305 (* create and register the initially function for a program *)
249 :     fun initially {
250 :     prog = Prog{name=progName, strands, initially, ...},
251 :     isArray : bool,
252 :     iterPrefix : stm list,
253 :     iters : (var * exp * exp) list,
254 :     createPrefix : stm list,
255 :     strand : Atom.atom,
256 :     args : exp list
257 :     } = let
258 :     val name = Atom.toString strand
259 :     val nDims = List.length iters
260 :     val worldTy = CL.T_Ptr(CL.T_Named N.worldTy)
261 :     fun mapi f xs = let
262 :     fun mapf (_, []) = []
263 :     | mapf (i, x::xs) = f(i, x) :: mapf(i+1, xs)
264 :     in
265 :     mapf (0, xs)
266 :     end
267 :     val baseInit = mapi (fn (i, (_, e, _)) => (i, CL.I_Exp e)) iters
268 :     val sizeInit = mapi
269 :     (fn (i, (CL.V(ty, _), lo, hi)) =>
270 :     (i, CL.I_Exp(CL.mkBinOp(CL.mkBinOp(hi, CL.#-, lo), CL.#+, CL.E_Int(1, ty))))
271 :     ) iters
272 :     (* code to allocate the world and initial strands *)
273 :     val wrld = "wrld"
274 :     val allocCode = [
275 :     CL.mkComment["allocate initial block of strands"],
276 :     CL.mkDecl(CL.T_Array(CL.int32, SOME nDims), "base", SOME(CL.I_Array baseInit)),
277 :     CL.mkDecl(CL.T_Array(CL.uint32, SOME nDims), "size", SOME(CL.I_Array sizeInit)),
278 :     CL.mkDecl(worldTy, wrld,
279 :     SOME(CL.I_Exp(CL.E_Apply(N.allocInitially, [
280 :     CL.mkVar "ProgramName",
281 :     CL.mkUnOp(CL.%&, CL.E_Var(N.strandDesc name)),
282 :     CL.E_Bool isArray,
283 :     CL.E_Int(IntInf.fromInt nDims, CL.int32),
284 :     CL.E_Var "base",
285 :     CL.E_Var "size"
286 :     ]))))
287 : jhr 1278 ]
288 : lamonts 1305 (* create the loop nest for the initially iterations *)
289 :     val indexVar = "ix"
290 :     val strandTy = CL.T_Ptr(CL.T_Named(N.strandTy name))
291 :     fun mkLoopNest [] = CL.mkBlock(createPrefix @ [
292 :     CL.mkDecl(strandTy, "sp",
293 :     SOME(CL.I_Exp(
294 :     CL.E_Cast(strandTy,
295 :     CL.E_Apply(N.inState, [CL.E_Var "wrld", CL.E_Var indexVar]))))),
296 :     CL.mkCall(N.strandInit name, CL.E_Var "sp" :: args),
297 :     CL.mkAssign(CL.E_Var indexVar, CL.mkBinOp(CL.E_Var indexVar, CL.#+, CL.E_Int(1, CL.uint32)))
298 :     ])
299 :     | mkLoopNest ((CL.V(ty, param), lo, hi)::iters) = let
300 :     val body = mkLoopNest iters
301 :     in
302 :     CL.mkFor(
303 :     [(ty, param, lo)],
304 :     CL.mkBinOp(CL.E_Var param, CL.#<=, hi),
305 :     [CL.mkPostOp(CL.E_Var param, CL.^++)],
306 :     body)
307 : jhr 1281 end
308 : lamonts 1305 val iterCode = [
309 :     CL.mkComment["initially"],
310 :     CL.mkDecl(CL.uint32, indexVar, SOME(CL.I_Exp(CL.E_Int(0, CL.uint32)))),
311 :     mkLoopNest iters
312 :     ]
313 :     val body = CL.mkBlock(
314 :     iterPrefix @
315 :     allocCode @
316 :     iterCode @
317 :     [CL.mkReturn(SOME(CL.E_Var "wrld"))])
318 :     val initFn = CL.D_Func([], worldTy, N.initially, [], body)
319 :     in
320 :     initially := initFn
321 :     end
322 : jhr 1281
323 : lamonts 1305 (***** OUTPUT *****)
324 :     fun genStrandPrint (Strand{name, tyName, state, output, code,...}) = let
325 :     (* the print function *)
326 : lamonts 1264 val prFnName = concat[name, "_print"]
327 :     val prFn = let
328 :     val params = [
329 :     CL.PARAM([], CL.T_Ptr(CL.T_Named "FILE"), "outS"),
330 :     CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "self")
331 :     ]
332 : lamonts 1305 val SOME(ty, x) = !output
333 :     val outState = CL.mkIndirect(CL.mkVar "self", x)
334 : lamonts 1264 val prArgs = (case ty
335 : lamonts 1305 of Ty.IVecTy 1 => [CL.E_Str(!N.gIntFormat ^ "\n"), outState]
336 : lamonts 1264 | Ty.IVecTy d => let
337 : lamonts 1305 val fmt = CL.E_Str(
338 :     String.concatWith " " (List.tabulate(d, fn _ => !N.gIntFormat))
339 : lamonts 1264 ^ "\n")
340 : lamonts 1305 val args = List.tabulate (d, fn i => ToC.ivecIndex(outState, d, i))
341 : lamonts 1264 in
342 :     fmt :: args
343 :     end
344 : lamonts 1305 | Ty.TensorTy[] => [CL.E_Str "%f\n", outState]
345 : lamonts 1264 | Ty.TensorTy[d] => let
346 : lamonts 1305 val fmt = CL.E_Str(
347 : lamonts 1264 String.concatWith " " (List.tabulate(d, fn _ => "%f"))
348 :     ^ "\n")
349 : lamonts 1305 val args = List.tabulate (d, fn i => ToC.vecIndex(outState, d, i))
350 : lamonts 1264 in
351 :     fmt :: args
352 :     end
353 :     | _ => raise Fail("genStrand: unsupported output type " ^ Ty.toString ty)
354 :     (* end case *))
355 :     in
356 : lamonts 1305 CL.D_Func(["static"], CL.voidTy, prFnName, params,
357 :     CL.mkCall("fprintf", CL.mkVar "outS" :: prArgs))
358 : lamonts 1264 end
359 :     in
360 :     prFn
361 :     end
362 : lamonts 1305
363 : jhr 1261 fun genStrandTyDef (Strand{tyName, state,...}) =
364 :     (* the type declaration for the strand's state struct *)
365 :     CL.D_StructDef(
366 : lamonts 1305 List.rev (List.map (fn CL.V(ty, x) => (ty, x)) (!state)),
367 : jhr 1261 tyName)
368 : lamonts 1305
369 : jhr 1261
370 : lamonts 1264 (* generates the load kernel function *)
371 : lamonts 1305
372 : lamonts 1264 (* generates the opencl buffers for the image data *)
373 : lamonts 1305 fun getGlobalDataBuffers(globals,contextVar,errVar) = let
374 : jhr 1279 val globalBufferDecl = CL.mkDecl(clMemoryTy,concat[RN.globalsVarName,"_cl"],NONE)
375 : jhr 1278 val globalBuffer = CL.mkAssign(CL.mkVar(concat[RN.globalsVarName,"_cl"]), CL.mkApply("clCreateBuffer",
376 :     [CL.mkVar contextVar,
377 :     CL.mkVar "CL_MEM_COPY_HOST_PTR",
378 :     CL.mkApply("sizeof",[CL.mkVar RN.globalsTy]),
379 :     CL.mkVar RN.globalsVarName,
380 :     CL.mkUnOp(CL.%&,CL.mkVar errVar)]))
381 : jhr 1261
382 : lamonts 1305 fun genDataBuffers([],_,_) = []
383 :     | genDataBuffers((var,nDims)::globals,contextVar,errVar) = let
384 : lamonts 1264 (* FIXME: use CL constructors to build expressions (not strings) *)
385 :     val size = if nDims = 1 then
386 : jhr 1278 CL.mkBinOp(CL.mkApply("sizeof",[CL.mkVar "float"]), CL.#*,
387 :     CL.mkIndirect(CL.mkVar var, "size[0]"))
388 : lamonts 1264 else if nDims = 2 then
389 : jhr 1278 CL.mkBinOp(CL.mkApply("sizeof",[CL.mkVar "float"]), CL.#*,
390 :     CL.mkIndirect(CL.mkVar var, concat["size[0]", " * ", var, "->size[1]"]))
391 : lamonts 1264 else
392 : jhr 1278 CL.mkBinOp(CL.mkApply("sizeof",[CL.mkVar "float"]), CL.#*,
393 :     CL.mkIndirect(CL.mkVar var,concat["size[0]", " * ", var, "->size[1] * ", var, "->size[2]"]))
394 : lamonts 1264
395 :     in
396 : jhr 1279 CL.mkDecl(clMemoryTy, RN.addBufferSuffix var ,NONE)::
397 :     CL.mkDecl(clMemoryTy, RN.addBufferSuffixData var ,NONE)::
398 : jhr 1278 CL.mkAssign(CL.mkVar(RN.addBufferSuffix var), CL.mkApply("clCreateBuffer",
399 :     [CL.mkVar contextVar,
400 :     CL.mkVar "CL_MEM_COPY_HOST_PTR",
401 :     CL.mkApply("sizeof",[CL.mkVar (RN.imageTy nDims)]),
402 :     CL.mkVar var,
403 :     CL.mkUnOp(CL.%&,CL.mkVar errVar)])) ::
404 :     CL.mkAssign(CL.mkVar(RN.addBufferSuffixData var), CL.mkApply("clCreateBuffer",
405 :     [CL.mkVar contextVar,
406 :     CL.mkVar "CL_MEM_COPY_HOST_PTR",
407 : lamonts 1264 size,
408 : jhr 1278 CL.mkIndirect(CL.mkVar var,"data"),
409 : lamonts 1305 CL.mkUnOp(CL.%&,CL.mkVar errVar)])):: genDataBuffers(globals,contextVar,errVar)
410 : lamonts 1264 end
411 :     in
412 : lamonts 1305 [globalBufferDecl] @ [globalBuffer] @ genDataBuffers(globals,contextVar,errVar)
413 : lamonts 1264 end
414 :    
415 : jhr 1261
416 : lamonts 1264 (* generates the kernel arguments for the image data *)
417 :     fun genGlobalArguments(globals,count,kernelVar,errVar) = let
418 : jhr 1278 val globalArgument = CL.mkExpStm(CL.mkAssignOp(CL.mkVar errVar,CL.|=,CL.mkApply("clSetKernelArg",
419 :     [CL.mkVar kernelVar,
420 : lamonts 1305 CL.mkPostOp(CL.E_Var count, CL.^++),
421 : jhr 1278 CL.mkApply("sizeof",[CL.mkVar "cl_mem"]),
422 :     CL.mkUnOp(CL.%&,CL.mkVar(concat[RN.globalsVarName,"_cl"]))])))
423 : lamonts 1264
424 :     fun genDataArguments([],_,_,_) = []
425 :     | genDataArguments((var,nDims)::globals,count,kernelVar,errVar) =
426 :    
427 : jhr 1278 CL.mkExpStm(CL.mkAssignOp(CL.mkVar errVar,CL.|=, CL.mkApply("clSetKernelArg",
428 :     [CL.mkVar kernelVar,
429 : lamonts 1305 CL.mkPostOp(CL.E_Var count, CL.^++),
430 : jhr 1278 CL.mkApply("sizeof",[CL.mkVar "cl_mem"]),
431 :     CL.mkUnOp(CL.%&,CL.mkVar(RN.addBufferSuffix var))])))::
432 : lamonts 1264
433 : jhr 1278 CL.mkExpStm(CL.mkAssignOp(CL.mkVar errVar,CL.|=,CL.mkApply("clSetKernelArg",
434 :     [CL.mkVar kernelVar,
435 : lamonts 1305 CL.mkPostOp(CL.E_Var count, CL.^++),
436 : jhr 1278 CL.mkApply("sizeof",[CL.mkVar "cl_mem"]),
437 : lamonts 1305 CL.mkUnOp(CL.%&,CL.mkVar(RN.addBufferSuffixData var))]))):: genDataArguments (globals,count,kernelVar,errVar)
438 : lamonts 1264
439 :     in
440 :    
441 : lamonts 1305 [globalArgument] @ genDataArguments(globals,count,kernelVar,errVar)
442 : lamonts 1264
443 :     end
444 :    
445 : lamonts 1305 (* generates the globals buffers and arguments function *)
446 :     fun genGlobal_Buffers_Args (imgGlobals) = let
447 : jhr 1273 (* Delcare opencl setup objects *)
448 :     val errVar = "err"
449 :     val imgDataSizeVar = "image_dataSize"
450 :     val params = [
451 : lamonts 1305 CL.PARAM([],CL.T_Named("cl_context"), "context"),
452 :     CL.PARAM([],CL.T_Named("cl_kernel"), "kernel"),
453 :     CL.PARAM([],CL.T_Named("int"), "argStart")
454 : jhr 1273 ]
455 : lamonts 1305
456 : lamonts 1264
457 : lamonts 1305 val clGlobalBuffers = getGlobalDataBuffers(!imgGlobals, "context","err")
458 : lamonts 1264
459 : lamonts 1305 val clGlobalArguments = genGlobalArguments(!imgGlobals,"argStart","kernel","err")
460 : lamonts 1264
461 :     (* Body put all the statments together *)
462 : lamonts 1305 val body = clGlobalBuffers @ clGlobalArguments
463 : lamonts 1264 in
464 : lamonts 1305 CL.D_Func([],CL.voidTy,RN.globalsSetupName,params,CL.mkBlock(body))
465 : lamonts 1264 end
466 : lamonts 1305
467 : lamonts 1264 (* generate the data and global parameters *)
468 :     fun genKeneralGlobalParams ((name,tyname)::rest) =
469 : lamonts 1271 CL.PARAM([], CL.T_Ptr(CL.T_Named RN.globalsTy), concat[RN.globalsVarName]) ::
470 :     CL.PARAM([], CL.T_Ptr(CL.T_Named (RN.imageTy tyname)),RN.addBufferSuffix name) ::
471 :     CL.PARAM([], CL.T_Ptr(CL.voidTy),RN.addBufferSuffixData name) ::
472 : lamonts 1264 genKeneralGlobalParams(rest)
473 :    
474 :     | genKeneralGlobalParams ([]) = []
475 :    
476 :     (*generate code for intilizing kernel global data *)
477 :     fun initKernelGlobals (globals,imgGlobals) = let
478 : lamonts 1305 fun initGlobalStruct ({name,target as TargetUtil.TARGET_CL,globalTy}::rest) =
479 : jhr 1278 CL.mkAssign(CL.mkVar name, CL.mkIndirect(CL.mkVar RN.globalsVarName, name)) ::
480 : lamonts 1264 initGlobalStruct(rest)
481 :     | initGlobalStruct ( _::rest) = initGlobalStruct(rest)
482 :     | initGlobalStruct([]) = []
483 :    
484 :     fun initGlobalImages((name,tyname)::rest) =
485 : jhr 1278 CL.mkAssign(CL.mkVar name, CL.mkVar (RN.addBufferSuffix name)) ::
486 :     CL.mkAssign(CL.mkIndirect(CL.mkVar name,"data"),CL.mkVar (RN.addBufferSuffixData name)) ::
487 : lamonts 1264 initGlobalImages(rest)
488 :     | initGlobalImages([]) = []
489 :     in
490 :     initGlobalStruct(globals) @ initGlobalImages(imgGlobals)
491 :     end
492 :    
493 :     (* generate the main kernel function for the .cl file *)
494 : lamonts 1271 fun genKernelFun(Strand{name, tyName, state, output, code,...},nDims,globals,imgGlobals) = let
495 : lamonts 1264 val fName = RN.kernelFuncName;
496 :     val inState = "strand_in"
497 :     val outState = "strand_out"
498 :     val params = [
499 :     CL.PARAM(["__global"], CL.T_Ptr(CL.T_Named tyName), "selfIn"),
500 :     CL.PARAM(["__global"], CL.T_Ptr(CL.T_Named tyName), "selfOut"),
501 :     CL.PARAM(["__global"], CL.intTy, "width")
502 :     ] @ genKeneralGlobalParams(!imgGlobals)
503 :     val thread_ids = if nDims = 1
504 : jhr 1278 then [CL.mkDecl(CL.intTy, "x", SOME(CL.I_Exp(CL.mkInt(0, CL.intTy)))),
505 :     CL.mkAssign(CL.mkVar "x",CL.mkApply(RN.getGlobalThreadId,[CL.mkInt(0,CL.intTy)]))]
506 : lamonts 1264 else
507 : jhr 1278 [CL.mkDecl(CL.intTy, "x", SOME(CL.I_Exp(CL.mkInt(0, CL.intTy)))),
508 :     CL.mkDecl(CL.intTy, "y", SOME(CL.I_Exp(CL.mkInt(0, CL.intTy)))),
509 :     CL.mkAssign(CL.mkVar "x", CL.mkApply(RN.getGlobalThreadId,[CL.mkInt(0,CL.intTy)])),
510 :     CL.mkAssign(CL.mkVar "y",CL.mkApply(RN.getGlobalThreadId,[CL.mkInt(1,CL.intTy)]))]
511 : lamonts 1264
512 :     val strandDecl = [CL.mkDecl(CL.T_Named tyName, inState, NONE),
513 :     CL.mkDecl(CL.T_Named tyName, outState,NONE)]
514 :     val strandObjects = if nDims = 1
515 : lamonts 1305 then [CL.mkAssign( CL.mkVar inState, CL.mkSubscript(CL.mkVar "selfIn",CL.mkStr "x")),
516 :     CL.mkAssign(CL.mkVar outState,CL.mkSubscript(CL.mkVar "selfOut",CL.mkStr "x"))]
517 : lamonts 1264 else let
518 : jhr 1278 val index = CL.mkBinOp(CL.mkBinOp(CL.mkVar "x",CL.#*,CL.mkVar "width"),CL.#+,CL.mkVar "y")
519 : lamonts 1264 in
520 : lamonts 1305 [CL.mkAssign(CL.mkVar inState, CL.mkSubscript(CL.mkVar "selfIn",index)),
521 :     CL.mkAssign(CL.mkVar outState,CL.mkSubscript(CL.mkVar "selfOut",index))]
522 : lamonts 1264 end
523 :    
524 :    
525 : jhr 1278 val status = CL.mkDecl(CL.intTy, "status", SOME(CL.I_Exp(CL.mkInt(0, CL.intTy))))
526 : lamonts 1271 val local_vars = thread_ids @ initKernelGlobals(!globals,!imgGlobals) @ strandDecl @ strandObjects @ [status]
527 : jhr 1278 val while_exp = CL.mkBinOp(CL.mkBinOp(CL.mkVar "status",CL.#!=, CL.mkVar RN.kStabilize),CL.#||,CL.mkBinOp(CL.mkVar "status", CL.#!=, CL.mkVar RN.kDie))
528 :     val while_body = [CL.mkAssign(CL.mkVar "status", CL.mkApply(RN.strandUpdate name,[ CL.mkUnOp(CL.%&,CL.mkVar inState), CL.mkUnOp(CL.%&,CL.mkVar outState)])),
529 :     CL.mkCall(RN.strandStabilize name,[ CL.mkUnOp(CL.%&,CL.mkVar inState), CL.mkUnOp(CL.%&,CL.mkVar outState)])]
530 : lamonts 1264
531 :     val whileBlock = [CL.mkWhile(while_exp,CL.mkBlock while_body)]
532 :    
533 :     val body = CL.mkBlock(local_vars @ whileBlock)
534 :     in
535 :     CL.D_Func(["__kernel"], CL.voidTy, fName, params, body)
536 :     end
537 :     (* generate a global structure from the globals *)
538 : lamonts 1305 fun genGlobalStruct(target,globals) = let
539 :     fun getGlobals(_, []) = []
540 :     | getGlobals(target',{name,globalTy,target}::rest) =
541 :     if target = target' then
542 :     (globalTy,name)::getGlobals(target',rest)
543 :     else
544 :     getGlobals(target',rest)
545 : lamonts 1264 in
546 : lamonts 1305 CL.D_StructDef(getGlobals(target,globals),RN.globalsTy)
547 : lamonts 1264 end
548 :    
549 : lamonts 1305 fun genGlobals(_,_, []) =
550 :     ()
551 :     | genGlobals(declFun, target',{name,globalTy,target}::rest) =
552 :     if target = target' then
553 :     (declFun (CL.D_Var([], globalTy, name, NONE));
554 :     genGlobals (declFun,target',rest))
555 :     else
556 :     genGlobals (declFun,target',rest)
557 : lamonts 1244
558 : lamonts 1264 fun genSrc (baseName, Prog{double,globals, topDecls, strands, initially,imgGlobals,numDims,...}) = let
559 :     val clFileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "cl"}
560 :     val cFileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"}
561 :     val clOutS = TextIO.openOut clFileName
562 :     val cOutS = TextIO.openOut cFileName
563 : jhr 1273 (* FIXME: need to use PrintAsC and PrintAsCL *)
564 : lamonts 1305 val clppStrm = PrintAsCL.new clOutS
565 : lamonts 1264 val cppStrm = PrintAsC.new cOutS
566 :     fun cppDecl dcl = PrintAsC.output(cppStrm, dcl)
567 : jhr 1282 fun clppDecl dcl = PrintAsCL.output(clppStrm, dcl)
568 : lamonts 1264 val strands = AtomTable.listItems strands
569 : jhr 1273 val [strand as Strand{name, tyName, code,init_code, ...}] = strands
570 : lamonts 1264 in
571 : jhr 1273 (* Generate the OpenCl file *)
572 :     clppDecl (CL.D_Verbatim([
573 :     if double
574 :     then "#define DIDEROT_DOUBLE_PRECISION"
575 :     else "#define DIDEROT_SINGLE_PRECISION",
576 :     "#define DIDEROT_TARGET_CL",
577 : lamonts 1305 "#include \"Diderot/cl-diderot.h\""
578 : jhr 1273 ]));
579 : lamonts 1305 genGlobals(clppDecl,TargetUtil.TARGET_CL,!globals);
580 :     clppDecl (genGlobalStruct (TargetUtil.TARGET_CL,!globals));
581 :     clppDecl (genStrandTyDef (strand));
582 : jhr 1273 List.app clppDecl (!code);
583 :     clppDecl (genKernelFun (strand,!numDims,globals,imgGlobals));
584 : lamonts 1305
585 : jhr 1273 (* Generate the Host file .c *)
586 :     cppDecl (CL.D_Verbatim([
587 :     if double
588 :     then "#define DIDEROT_DOUBLE_PRECISION"
589 :     else "#define DIDEROT_SINGLE_PRECISION",
590 :     "#define DIDEROT_TARGET_CL",
591 :     "#include \"Diderot/diderot.h\""
592 :     ]));
593 : lamonts 1305 genGlobals(cppDecl,TargetUtil.TARGET_C,!globals);
594 :     cppDecl (genGlobalStruct (TargetUtil.TARGET_C,!globals));
595 :     cppDecl (genStrandTyDef (strand));
596 : jhr 1273 cppDecl (!init_code);
597 : lamonts 1305 cppDecl (genStrandPrint(strand));
598 : jhr 1273 List.app cppDecl (List.rev (!topDecls));
599 : lamonts 1305 cppDecl (genGlobal_Buffers_Args (imgGlobals));
600 :     cppDecl (!initially);
601 : lamonts 1264 PrintAsC.close cppStrm;
602 : jhr 1282 PrintAsCL.close clppStrm;
603 : lamonts 1264 TextIO.closeOut cOutS;
604 :     TextIO.closeOut clOutS
605 :     end
606 :    
607 : lamonts 1244 (* output the code to a file. The string is the basename of the file, the extension
608 :     * is provided by the target.
609 :     *)
610 : lamonts 1264 fun generate (basename, prog as Prog{double, parallel, debug, ...}) = let
611 :     fun condCons (true, x, xs) = x::xs
612 :     | condCons (false, _, xs) = xs
613 :     (* generate the C compiler flags *)
614 :     val cflags = ["-I" ^ Paths.diderotInclude, "-I" ^ Paths.teemInclude]
615 :     val cflags = condCons (parallel, #pthread Paths.cflags, cflags)
616 :     val cflags = if debug
617 :     then #debug Paths.cflags :: cflags
618 :     else #ndebug Paths.cflags :: cflags
619 :     val cflags = #base Paths.cflags :: cflags
620 :     (* generate the loader flags *)
621 :     val extraLibs = condCons (parallel, #pthread Paths.extraLibs, [])
622 :     val extraLibs = Paths.teemLinkFlags @ #base Paths.extraLibs :: extraLibs
623 : lamonts 1271 val extraLibs = #cl Paths.extraLibs :: extraLibs
624 : lamonts 1264 val rtLib = TargetUtil.runtimeName {
625 :     target = TargetUtil.TARGET_CL,
626 :     parallel = parallel, double = double, debug = debug
627 :     }
628 :     val ldOpts = rtLib :: extraLibs
629 :     in
630 : lamonts 1271 genSrc (basename, prog);
631 :     RunCC.compile (basename, cflags);
632 :     RunCC.link (basename, ldOpts)
633 : jhr 1273 end
634 : lamonts 1244
635 : jhr 1273 end
636 : lamonts 1264
637 : lamonts 1244 (* strands *)
638 :     structure Strand =
639 :     struct
640 : jhr 1261 fun define (Prog{strands, ...}, strandId) = let
641 :     val name = Atom.toString strandId
642 :     val strand = Strand{
643 :     name = name,
644 :     tyName = RN.strandTy name,
645 :     state = ref [],
646 :     output = ref NONE,
647 : lamonts 1271 code = ref [],
648 : jhr 1273 init_code = ref (CL.D_Comment(["no init code"]))
649 : jhr 1261 }
650 :     in
651 :     AtomTable.insert strands (strandId, strand);
652 :     strand
653 :     end
654 : lamonts 1244
655 :     (* return the strand with the given name *)
656 : jhr 1261 fun lookup (Prog{strands, ...}, strandId) = AtomTable.lookup strands strandId
657 : lamonts 1244
658 :     (* register the strand-state initialization code. The variables are the strand
659 :     * parameters.
660 :     *)
661 : lamonts 1271 fun init (Strand{name, tyName, code,init_code, ...}, params, init) = let
662 : jhr 1261 val fName = RN.strandInit name
663 :     val params =
664 :     CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "selfOut") ::
665 : jhr 1273 List.map (fn (ToCL.V(ty, x)) => CL.PARAM([], ty, x)) params
666 : jhr 1261 val initFn = CL.D_Func([], CL.voidTy, fName, params, init)
667 :     in
668 : jhr 1273 init_code := initFn
669 : jhr 1261 end
670 : lamonts 1244
671 :     (* register a strand method *)
672 : lamonts 1271 fun method (Strand{name, tyName, code,...}, methName, body) = let
673 : jhr 1261 val fName = concat[name, "_", methName]
674 :     val params = [
675 :     CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "selfIn"),
676 :     CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "selfOut")
677 :     ]
678 :     val methFn = CL.D_Func([], CL.int32, fName, params, body)
679 :     in
680 : jhr 1273 code := methFn :: !code
681 : jhr 1261 end
682 :    
683 : jhr 1273 fun output (Strand{output, ...}, ty, ToCL.V(_, x)) = output := SOME(ty, x)
684 : lamonts 1244
685 :     end
686 :    
687 :     end
688 :    
689 :     structure CLBackEnd = CodeGenFn(CLTarget)

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