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

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