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 1382 - (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 :     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 :     [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 :     fun genStrandCopy(Strand{tyName,name,state,...}) = let
404 :     val params = [
405 :     CL.PARAM(["__global"], CL.T_Ptr(CL.T_Named tyName), "selfIn"),
406 :     CL.PARAM(["__global"], CL.T_Ptr(CL.T_Named tyName), "selfOut")
407 : lamonts 1351 ]
408 : jhr 1358 val assignStms = List.rev(List.map(fn x => CL.mkAssign(CL.mkIndirect(CL.E_Var "selfOut", #var x), CL.mkIndirect(CL.E_Var "selfIn", #var x))) (!state))
409 :     in
410 :     CL.D_Func([""], CL.voidTy, RN.strandCopy name, params,CL.mkBlock(assignStms))
411 :     end
412 : lamonts 1305
413 : jhr 1307 (* generates the opencl buffers for the image data *)
414 : jhr 1358 fun getGlobalDataBuffers (globals, contextVar, errVar) = let
415 : jhr 1344 val globalBuffErr = "error creating OpenCL global buffer"
416 :     fun errorFn msg = CL.mkIfThen(CL.mkBinOp(CL.E_Var errVar, CL.#!=, CL.E_Var "CL_SUCCESS"),
417 :     CL.mkBlock([CL.mkCall("fprintf",[CL.E_Var "stderr", CL.E_Str msg]),
418 :     CL.mkCall("exit",[CL.mkInt 1])]))
419 : jhr 1358 val globalBufferDecl = CL.mkDecl(clMemoryTy,concat[RN.globalsVarName,"_cl"],NONE)
420 : jhr 1307 val globalBuffer = CL.mkAssign(CL.mkVar(concat[RN.globalsVarName,"_cl"]),
421 :     CL.mkApply("clCreateBuffer", [
422 :     CL.mkVar contextVar,
423 : lamonts 1328 CL.mkVar "CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR",
424 : jhr 1358 CL.mkSizeof(CL.T_Named RN.globalsTy),
425 : jhr 1307 CL.mkVar RN.globalsVarName,
426 :     CL.mkUnOp(CL.%&,CL.mkVar errVar)
427 :     ]))
428 : lamonts 1341 fun genDataBuffers ([],_,_,_) = []
429 :     | genDataBuffers ((var,nDims)::globals, contextVar, errVar,errFn) = let
430 : jhr 1326 val hostVar = CL.mkIndirect(CL.mkVar RN.globalsVarName, var)
431 : jhr 1313 (* FIXME: use CL constructors to build expressions (not strings) *)
432 : jhr 1326 fun sizeExp i = CL.mkSubscript(CL.mkIndirect(hostVar, "size"), CL.mkInt i)
433 : jhr 1358 (* FIXME: there is no reason that images have to be restricted to float elements! *)
434 :     val size = CL.mkBinOp(CL.mkSizeof(CL.float), CL.#*, sizeExp 0)
435 : jhr 1326 val size = if (nDims > 1)
436 :     then CL.mkBinOp(size, CL.#*, sizeExp 1)
437 :     else size
438 :     val size = if (nDims > 2)
439 :     then CL.mkBinOp(size, CL.#*, sizeExp 2)
440 :     else size
441 :     in
442 :     CL.mkDecl(clMemoryTy, RN.addBufferSuffix var ,NONE)::
443 :     CL.mkDecl(clMemoryTy, RN.addBufferSuffixData var ,NONE)::
444 :     CL.mkAssign(CL.mkVar(RN.addBufferSuffix var),
445 :     CL.mkApply("clCreateBuffer", [
446 :     CL.mkVar contextVar,
447 :     CL.mkVar "CL_MEM_COPY_HOST_PTR",
448 : jhr 1358 CL.mkSizeof(CL.T_Named(RN.imageTy nDims)),
449 : jhr 1326 hostVar,
450 :     CL.mkUnOp(CL.%&,CL.mkVar errVar)
451 : jhr 1344 ])) ::
452 : jhr 1358 errFn(concat["error in creating ",RN.addBufferSuffix var, " global buffer"]) ::
453 : jhr 1326 CL.mkAssign(CL.mkVar(RN.addBufferSuffixData var),
454 :     CL.mkApply("clCreateBuffer", [
455 :     CL.mkVar contextVar,
456 :     CL.mkVar "CL_MEM_COPY_HOST_PTR",
457 :     size,
458 :     CL.mkIndirect(hostVar, "data"),
459 :     CL.mkUnOp(CL.%&,CL.mkVar errVar)
460 : jhr 1344 ])) ::
461 : jhr 1358 errFn(concat["error in creating ",RN.addBufferSuffixData var, " global buffer"]) ::
462 :     genDataBuffers(globals,contextVar,errVar,errFn)
463 : jhr 1326 end
464 : jhr 1307 in
465 : jhr 1344 globalBufferDecl
466 : jhr 1358 :: globalBuffer
467 :     :: errorFn(globalBuffErr)
468 :     :: genDataBuffers(globals,contextVar,errVar,errorFn)
469 : jhr 1307 end
470 : lamonts 1264
471 : jhr 1309 (* generates the kernel arguments for the image data *)
472 : jhr 1313 fun genGlobalArguments (globals, count, kernelVar, errVar) = let
473 : jhr 1343 val globalArgErr = "error creating OpenCL global argument"
474 :     fun errorFn msg = CL.mkIfThen(CL.mkBinOp(CL.E_Var errVar, CL.#!=, CL.E_Var "CL_SUCCESS"),
475 : jhr 1344 CL.mkBlock([CL.mkCall("fprintf",[CL.E_Var "stderr", CL.E_Str msg]),
476 :     CL.mkCall("exit",[CL.mkInt 1])]))
477 : jhr 1326 val globalArgument = CL.mkExpStm(CL.mkAssignOp(CL.mkVar errVar,CL.|=,
478 :     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(concat[RN.globalsVarName,"_cl"]))])))
483 : lamonts 1341 fun genDataArguments ([],_,_,_,_) = []
484 :     | genDataArguments ((var,nDims)::globals,count,kernelVar,errVar,errFn) =
485 :     CL.mkExpStm(CL.mkAssignOp(CL.mkVar errVar,CL.$=,
486 : jhr 1326 CL.mkApply("clSetKernelArg",
487 :     [CL.mkVar kernelVar,
488 :     CL.mkPostOp(CL.E_Var count, CL.^++),
489 :     CL.mkApply("sizeof",[CL.mkVar "cl_mem"]),
490 : lamonts 1341 CL.mkUnOp(CL.%&,CL.mkVar(RN.addBufferSuffix var))]))) ::
491 : jhr 1344 errFn(concat["error in creating ",RN.addBufferSuffix var, " argument"]) ::
492 : lamonts 1341 CL.mkExpStm(CL.mkAssignOp(CL.mkVar errVar,CL.$=,
493 : jhr 1326 CL.mkApply("clSetKernelArg",
494 :     [CL.mkVar kernelVar,
495 :     CL.mkPostOp(CL.E_Var count, CL.^++),
496 :     CL.mkApply("sizeof",[CL.mkVar "cl_mem"]),
497 :     CL.mkUnOp(CL.%&,CL.mkVar(RN.addBufferSuffixData var))]))) ::
498 : jhr 1344 errFn(concat["error in creating ",RN.addBufferSuffixData var, " argument"]) ::
499 : lamonts 1341 genDataArguments (globals,count,kernelVar,errVar,errFn)
500 : jhr 1326 in
501 : lamonts 1341 [globalArgument,errorFn(globalArgErr)] @ genDataArguments(globals, count, kernelVar, errVar,errorFn)
502 : jhr 1326 end
503 : lamonts 1264
504 : lamonts 1305 (* generates the globals buffers and arguments function *)
505 : jhr 1358 fun genGlobalBuffersArgs imgGlobals = let
506 : jhr 1273 (* Delcare opencl setup objects *)
507 :     val errVar = "err"
508 :     val imgDataSizeVar = "image_dataSize"
509 : jhr 1315 val params = [
510 : lamonts 1305 CL.PARAM([],CL.T_Named("cl_context"), "context"),
511 : jhr 1307 CL.PARAM([],CL.T_Named("cl_kernel"), "kernel"),
512 : jhr 1344 CL.PARAM([],CL.T_Named("cl_command_queue"), "cmdQ"),
513 : jhr 1307 CL.PARAM([],CL.T_Named("int"), "argStart")
514 : jhr 1273 ]
515 : jhr 1313 val clGlobalBuffers = getGlobalDataBuffers(!imgGlobals, "context", errVar)
516 :     val clGlobalArguments = genGlobalArguments(!imgGlobals, "argStart", "kernel", errVar)
517 : jhr 1307 (* Body put all the statments together *)
518 : jhr 1314 val body = CL.mkDecl(clIntTy, errVar, SOME(CL.I_Exp(CL.mkInt 0)))
519 : jhr 1326 :: clGlobalBuffers @ clGlobalArguments
520 : jhr 1313 in
521 : jhr 1307 CL.D_Func([],CL.voidTy,RN.globalsSetupName,params,CL.mkBlock(body))
522 :     end
523 : lamonts 1305
524 : jhr 1307 (* generate the data and global parameters *)
525 :     fun genKeneralGlobalParams ((name,tyname)::rest) =
526 : jhr 1343 globalParam (CL.T_Ptr(CL.T_Named (RN.imageTy tyname)), RN.addBufferSuffix name) ::
527 :     globalParam (CL.T_Ptr(CL.voidTy), RN.addBufferSuffixData name) ::
528 : jhr 1307 genKeneralGlobalParams rest
529 :     | genKeneralGlobalParams [] = []
530 :    
531 :     (*generate code for intilizing kernel global data *)
532 :     (* FIXME: should use List.map here *)
533 : lamonts 1316 fun initGlobalImages ((name, tyname)::rest) =
534 : jhr 1357 CL.mkAssign(
535 :     CL.mkIndirect(CL.E_Var RN.globalsVarName, name),
536 :     CL.mkVar (RN.addBufferSuffix name)) ::
537 :     CL.mkAssign(
538 :     CL.mkIndirect(CL.mkIndirect(CL.E_Var RN.globalsVarName, name), "data"),
539 :     CL.mkVar (RN.addBufferSuffixData name)) ::
540 :     initGlobalImages rest
541 : lamonts 1316 | initGlobalImages [] = []
542 :    
543 : jhr 1331 (* generate the main kernel function for the .cl file *)
544 : jhr 1307 fun genKernelFun (strand, nDims, globals, imgGlobals) = let
545 :     val Strand{name, tyName, state, output, code,...} = strand
546 :     val fName = RN.kernelFuncName;
547 :     val inState = "strand_in"
548 : lamonts 1341 val outState = "strand_out"
549 : jhr 1345 val tempVar = "tmp"
550 : jhr 1307 val params = [
551 : lamonts 1346 CL.PARAM(["__global"], CL.T_Ptr(CL.T_Named tyName), "selfIn"),
552 :     CL.PARAM(["__global"], CL.T_Ptr(CL.T_Named tyName), "selfOut"),
553 : jhr 1358 CL.PARAM(["__global"], CL.T_Ptr(CL.T_Num(RawTypes.RT_UInt8)), "strandStatus"),
554 : lamonts 1346 CL.PARAM(["__global"], CL.intTy, "width"),
555 : jhr 1358 CL.PARAM(["__global"], globPtrTy, RN.globalsVarName)
556 : jhr 1307 ] @ genKeneralGlobalParams(!imgGlobals)
557 :     val thread_ids = if nDims = 1
558 : jhr 1331 then [
559 :     CL.mkDecl(CL.intTy, "x",
560 :     SOME(CL.I_Exp(CL.mkApply(RN.getGlobalThreadId,[CL.mkInt 0]))))
561 :     ]
562 :     else if nDims = 2
563 :     then [
564 :     CL.mkDecl(CL.intTy, "x",
565 :     SOME(CL.I_Exp(CL.mkApply(RN.getGlobalThreadId,[CL.mkInt 0])))),
566 :     CL.mkDecl(CL.intTy, "y",
567 :     SOME(CL.I_Exp(CL.mkApply(RN.getGlobalThreadId,[CL.mkInt 1]))))
568 :     ]
569 :     else raise Fail "nDims > 2"
570 : jhr 1307 val strandDecl = [
571 : lamonts 1341 CL.mkDecl(CL.T_Ptr(CL.T_Named (concat["__global ",tyName])), inState, NONE),
572 :     CL.mkDecl(CL.T_Ptr(CL.T_Named (concat["__global ",tyName])), outState, NONE),
573 : jhr 1344 CL.mkDecl(CL.T_Ptr(CL.T_Named (concat["__global ",tyName])), tempVar, NONE)
574 : jhr 1331 ]
575 : jhr 1358 val barrierCode = CL.mkCall(RN.strandCopy name, [CL.E_Var outState, CL.E_Var inState])
576 :     val barrierStm = CL.mkCall("barrier",[CL.E_Var "CLK_LOCAL_MEM_FENCE"])
577 : lamonts 1346 val index = if nDims = 1 then
578 :     CL.mkStr "x"
579 :     else
580 : jhr 1358 CL.mkBinOp(
581 : jhr 1332 CL.mkBinOp(CL.mkVar "x", CL.#*, CL.mkVar "width"), CL.#+, CL.mkVar "y")
582 : lamonts 1346
583 :     val strandObjects =
584 :     [ CL.mkAssign(CL.mkVar inState, CL.mkBinOp(CL.mkVar "selfIn",CL.#+,index)),
585 :     CL.mkAssign(CL.mkVar outState, CL.mkBinOp(CL.mkVar "selfOut",CL.#+,index))
586 : jhr 1358 ]
587 : lamonts 1346
588 : jhr 1358 val stabalizeStm = CL.mkAssign(CL.mkSubscript(CL.mkVar "strandStatus",index),
589 :     CL.E_Var "status")
590 : lamonts 1346 val status = CL.mkDecl(CL.intTy, "status", SOME(CL.I_Exp(CL.mkSubscript(CL.mkVar "strandStatus",index))))
591 : jhr 1333 val strandInitStm = CL.mkCall(RN.strandInit name, [
592 :     CL.E_Var RN.globalsVarName,
593 : lamonts 1351 CL.E_Var outState,
594 : jhr 1333 CL.E_Var "x",
595 :     (* FIXME: if nDims = 1, then "y" is not defined! the arguments to this call should really come from
596 :     * the initially code!
597 :     *)
598 :     CL.E_Var "y"])
599 :     val local_vars = thread_ids
600 :     @ initGlobalImages(!imgGlobals)
601 :     @ strandDecl
602 :     @ strandObjects
603 :     @ [strandInitStm,status]
604 : lamonts 1351 val while_exp = CL.mkBinOp(CL.mkVar "status",CL.#==, CL.mkVar RN.kActive)
605 : lamonts 1346 val whileBody = CL.mkBlock ([barrierCode,barrierStm] @ [
606 : jhr 1307 CL.mkAssign(CL.mkVar "status",
607 :     CL.mkApply(RN.strandUpdate name,
608 : lamonts 1341 [CL.mkVar inState, CL.mkVar outState,CL.E_Var RN.globalsVarName]))] )
609 : jhr 1307 val whileBlock = [CL.mkWhile(while_exp, whileBody)]
610 : lamonts 1346 val body = CL.mkBlock(local_vars @ whileBlock @ [stabalizeStm])
611 : jhr 1307 in
612 :     CL.D_Func(["__kernel"], CL.voidTy, fName, params, body)
613 :     end
614 : jhr 1358
615 :     (* generate a global structure type definition from the list of globals *)
616 :     fun genGlobalStruct (targetTy, globals, tyName) = let
617 : jhr 1307 val globs = List.map (fn (x : mirror_var) => (targetTy x, #var x)) globals
618 :     in
619 : jhr 1358 CL.D_StructDef(globs, tyName)
620 : jhr 1307 end
621 : lamonts 1341
622 : jhr 1307 fun genGlobals (declFn, targetTy, globals) = let
623 :     fun doVar (x : mirror_var) = declFn (CL.D_Var([], targetTy x, #var x, NONE))
624 :     in
625 :     List.app doVar globals
626 :     end
627 : lamonts 1264
628 : jhr 1326 fun genStrandDesc (Strand{name, output, ...}) = let
629 :     (* the strand's descriptor object *)
630 :     val descI = let
631 :     fun fnPtr (ty, f) = CL.I_Exp(CL.mkCast(CL.T_Named ty, CL.mkVar f))
632 :     val SOME(outTy, _) = !output
633 :     in
634 :     CL.I_Struct[
635 :     ("name", CL.I_Exp(CL.mkStr name)),
636 :     ("stateSzb", CL.I_Exp(CL.mkSizeof(CL.T_Named(N.strandTy name)))),
637 : jhr 1315 (*
638 : jhr 1326 ("outputSzb", CL.I_Exp(CL.mkSizeof(ToC.trTy outTy))),
639 : jhr 1315 *)
640 : jhr 1326 ("update", fnPtr("update_method_t", "0")),
641 : jhr 1382 ("print", fnPtr("print_method_t", name ^ "Print"))
642 : jhr 1326 ]
643 :     end
644 :     val desc = CL.D_Var([], CL.T_Named N.strandDescTy, N.strandDesc name, SOME descI)
645 :     in
646 :     desc
647 :     end
648 : jhr 1315
649 :     (* generate the table of strand descriptors *)
650 : jhr 1326 fun genStrandTable (declFn, strands) = let
651 :     val nStrands = length strands
652 :     fun genInit (Strand{name, ...}) = CL.I_Exp(CL.mkUnOp(CL.%&, CL.E_Var(N.strandDesc name)))
653 :     fun genInits (_, []) = []
654 :     | genInits (i, s::ss) = (i, genInit s) :: genInits(i+1, ss)
655 :     in
656 :     declFn (CL.D_Var([], CL.int32, N.numStrands,
657 :     SOME(CL.I_Exp(CL.E_Int(IntInf.fromInt nStrands, CL.int32)))));
658 :     declFn (CL.D_Var([],
659 :     CL.T_Array(CL.T_Ptr(CL.T_Named N.strandDescTy), SOME nStrands),
660 :     N.strands,
661 :     SOME(CL.I_Array(genInits (0, strands)))))
662 :     end
663 : jhr 1315
664 : jhr 1308 fun genSrc (baseName, prog) = let
665 : jhr 1326 val Prog{name,double, globals, topDecls, strands, initially, imgGlobals, numDims, ...} = prog
666 : jhr 1307 val clFileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "cl"}
667 :     val cFileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"}
668 :     val clOutS = TextIO.openOut clFileName
669 :     val cOutS = TextIO.openOut cFileName
670 :     val clppStrm = PrintAsCL.new clOutS
671 :     val cppStrm = PrintAsC.new cOutS
672 : jhr 1321 val progName = name
673 : jhr 1307 fun cppDecl dcl = PrintAsC.output(cppStrm, dcl)
674 :     fun clppDecl dcl = PrintAsCL.output(clppStrm, dcl)
675 :     val strands = AtomTable.listItems strands
676 :     val [strand as Strand{name, tyName, code, init_code, ...}] = strands
677 :     in
678 : jhr 1273 (* Generate the OpenCl file *)
679 :     clppDecl (CL.D_Verbatim([
680 :     if double
681 :     then "#define DIDEROT_DOUBLE_PRECISION"
682 :     else "#define DIDEROT_SINGLE_PRECISION",
683 :     "#define DIDEROT_TARGET_CL",
684 : lamonts 1305 "#include \"Diderot/cl-diderot.h\""
685 : jhr 1273 ]));
686 : jhr 1358 clppDecl (genGlobalStruct (#gpuTy, !globals, RN.globalsTy));
687 : jhr 1307 clppDecl (genStrandTyDef(#gpuTy, strand));
688 : jhr 1326 clppDecl (!init_code);
689 : lamonts 1351 clppDecl (genStrandCopy(strand));
690 : jhr 1273 List.app clppDecl (!code);
691 : lamonts 1328 clppDecl (genKernelFun (strand, !numDims, globals, imgGlobals));
692 :     (* Generate the Host C file *)
693 : jhr 1273 cppDecl (CL.D_Verbatim([
694 :     if double
695 :     then "#define DIDEROT_DOUBLE_PRECISION"
696 :     else "#define DIDEROT_SINGLE_PRECISION",
697 :     "#define DIDEROT_TARGET_CL",
698 :     "#include \"Diderot/diderot.h\""
699 :     ]));
700 : jhr 1326 cppDecl (CL.D_Var(["static"], CL.charPtr, "ProgramName",
701 :     SOME(CL.I_Exp(CL.mkStr progName))));
702 : jhr 1358 cppDecl (genGlobalStruct (#hostTy, !globals, RN.globalsTy));
703 :     cppDecl (genGlobalStruct (#shadowTy, !globals, RN.shadowGlobalsTy));
704 :     (* FIXME: does this really need to be a global? *)
705 :     cppDecl (CL.D_Var(["static"], globPtrTy, RN.globalsVarName, NONE));
706 : jhr 1308 cppDecl (genStrandTyDef (#hostTy, strand));
707 : jhr 1307 cppDecl (genStrandPrint strand);
708 : jhr 1273 List.app cppDecl (List.rev (!topDecls));
709 : jhr 1315 cppDecl (genGlobalBuffersArgs imgGlobals);
710 : jhr 1326 List.app (fn strand => cppDecl (genStrandDesc strand)) strands;
711 :     genStrandTable (cppDecl, strands);
712 : jhr 1307 cppDecl (!initially);
713 :     PrintAsC.close cppStrm;
714 :     PrintAsCL.close clppStrm;
715 :     TextIO.closeOut cOutS;
716 :     TextIO.closeOut clOutS
717 :     end
718 : lamonts 1264
719 : jhr 1358 (* output the code to the filesystem. The string is the basename of the source file *)
720 : jhr 1307 fun generate (basename, prog as Prog{double, parallel, debug, ...}) = let
721 :     fun condCons (true, x, xs) = x::xs
722 :     | condCons (false, _, xs) = xs
723 :     (* generate the C compiler flags *)
724 :     val cflags = ["-I" ^ Paths.diderotInclude, "-I" ^ Paths.teemInclude]
725 :     val cflags = condCons (parallel, #pthread Paths.cflags, cflags)
726 :     val cflags = if debug
727 :     then #debug Paths.cflags :: cflags
728 :     else #ndebug Paths.cflags :: cflags
729 :     val cflags = #base Paths.cflags :: cflags
730 :     (* generate the loader flags *)
731 :     val extraLibs = condCons (parallel, #pthread Paths.extraLibs, [])
732 :     val extraLibs = Paths.teemLinkFlags @ #base Paths.extraLibs :: extraLibs
733 :     val extraLibs = #cl Paths.extraLibs :: extraLibs
734 :     val rtLib = TargetUtil.runtimeName {
735 :     target = TargetUtil.TARGET_CL,
736 :     parallel = parallel, double = double, debug = debug
737 :     }
738 :     val ldOpts = rtLib :: extraLibs
739 :     in
740 : lamonts 1341 genSrc (basename, prog);
741 : jhr 1307 RunCC.compile (basename, cflags);
742 :     RunCC.link (basename, ldOpts)
743 : jhr 1273 end
744 : lamonts 1244
745 : jhr 1273 end
746 : lamonts 1264
747 : lamonts 1244 (* strands *)
748 :     structure Strand =
749 :     struct
750 : jhr 1261 fun define (Prog{strands, ...}, strandId) = let
751 :     val name = Atom.toString strandId
752 :     val strand = Strand{
753 :     name = name,
754 :     tyName = RN.strandTy name,
755 :     state = ref [],
756 :     output = ref NONE,
757 : lamonts 1271 code = ref [],
758 : jhr 1273 init_code = ref (CL.D_Comment(["no init code"]))
759 : jhr 1261 }
760 :     in
761 :     AtomTable.insert strands (strandId, strand);
762 :     strand
763 :     end
764 : lamonts 1244
765 :     (* return the strand with the given name *)
766 : jhr 1261 fun lookup (Prog{strands, ...}, strandId) = AtomTable.lookup strands strandId
767 : lamonts 1244
768 :     (* register the strand-state initialization code. The variables are the strand
769 :     * parameters.
770 :     *)
771 : jhr 1308 fun init (Strand{name, tyName, code, init_code, ...}, params, init) = let
772 : jhr 1261 val fName = RN.strandInit name
773 :     val params =
774 : jhr 1343 globalParam (globPtrTy, RN.globalsVarName) ::
775 :     globalParam (CL.T_Ptr(CL.T_Named tyName), "selfOut") ::
776 : jhr 1273 List.map (fn (ToCL.V(ty, x)) => CL.PARAM([], ty, x)) params
777 : jhr 1261 val initFn = CL.D_Func([], CL.voidTy, fName, params, init)
778 :     in
779 : jhr 1273 init_code := initFn
780 : jhr 1261 end
781 : jhr 1358
782 : lamonts 1244 (* register a strand method *)
783 : lamonts 1271 fun method (Strand{name, tyName, code,...}, methName, body) = let
784 : jhr 1358 val fName = concat[name, "_", methName]
785 : jhr 1261 val params = [
786 : jhr 1343 globalParam (CL.T_Ptr(CL.T_Named tyName), "selfIn"),
787 :     globalParam (CL.T_Ptr(CL.T_Named tyName), "selfOut"),
788 :     globalParam (CL.T_Ptr(CL.T_Named (RN.globalsTy)), RN.globalsVarName)
789 : jhr 1261 ]
790 :     val methFn = CL.D_Func([], CL.int32, fName, params, body)
791 :     in
792 : jhr 1273 code := methFn :: !code
793 : jhr 1261 end
794 :    
795 : jhr 1273 fun output (Strand{output, ...}, ty, ToCL.V(_, x)) = output := SOME(ty, x)
796 : lamonts 1244
797 :     end
798 :    
799 :     end
800 :    
801 :     structure CLBackEnd = CodeGenFn(CLTarget)

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