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

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