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

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