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

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