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

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