Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/vis12-cl/src/compiler/cl-target/cl-target.sml
ViewVC logotype

Annotation of /branches/vis12-cl/src/compiler/cl-target/cl-target.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2405 - (view) (download)

1 : jhr 1671 (* cl-target.sml
2 :     *
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 : jhr 2405 structure CLN = CLNames
15 : jhr 1853 structure N = CNames
16 : jhr 2102 structure ToC = TreeToC
17 : jhr 1671 structure ToCL = TreeToCL
18 : jhr 2405 structure SU = StrandUtil
19 : jhr 1671
20 : jhr 2405 type props = Properties.props
21 : jhr 1671
22 :     type var = CL.typed_var
23 :     type exp = CL.exp
24 :     type stm = CL.stm
25 :    
26 :     (* variable or field that is mirrored between host and GPU *)
27 : jhr 2405 datatype mirror_var = MV of {
28 :     var : CL.var, (* variable name *)
29 :     ty : IL.Ty.ty, (* tree IL type *)
30 :     hToS : stm (* the statement that converts the variable to its *)
31 :     (* shadow representation *)
32 :     }
33 : jhr 1671
34 :     datatype strand = Strand of {
35 : jhr 2405 prog : program,
36 : jhr 1671 name : string,
37 :     tyName : string,
38 :     state : mirror_var list,
39 : jhr 2405 output : (Ty.ty * CL.var), (* the strand's output variable (only one for now) *)
40 :     code : CL.decl list ref
41 : jhr 1671 }
42 :    
43 : jhr 2405 and program = Prog of {
44 :     props : Properties.props,
45 : jhr 1671 globals : mirror_var list ref,
46 :     topDecls : CL.decl list ref,
47 :     strands : strand AtomTable.hash_table,
48 : jhr 2405 nAxes : int option ref, (* number of axes in initial grid (NONE means collection) *)
49 :     initially : CL.decl ref
50 : jhr 1671 }
51 :    
52 :     datatype env = ENV of {
53 :     info : env_info,
54 :     vMap : var V.Map.map,
55 :     scope : scope
56 :     }
57 :    
58 :     and env_info = INFO of {
59 :     prog : program
60 :     }
61 :    
62 :     and scope
63 :     = NoScope
64 :     | GlobalScope
65 :     | InitiallyScope
66 : jhr 2405 | StrandScope (* strand initialization *)
67 :     | MethodScope of StrandUtil.method_name (* method body; vars are state variables *)
68 : jhr 1671
69 :     (* the supprted widths of vectors of reals on the target. *)
70 :     (* FIXME: for OpenCL 1.1, 3 is also valid *)
71 :     fun vectorWidths () = [2, 4, 8, 16]
72 :    
73 :     (* we do not support printing on the OpenCL target *)
74 :     val supportsPrinting = false
75 :    
76 :     (* tests for whether various expression forms can appear inline *)
77 :     fun inlineCons n = (n < 2) (* vectors are inline, but not matrices *)
78 :     val inlineMatrixExp = false (* can matrix-valued expressions appear inline? *)
79 :    
80 : jhr 2405 (* helper functions for specifying parameters in various address spaces *)
81 :     local
82 :     fun param spc (ty, x) = CL.PARAM([spc], ty, x)
83 :     in
84 :     val globalParam = param "__global"
85 :     val constantParam = param "__constant"
86 :     val localParam = param "__local"
87 :     val privateParam = param "__private"
88 :     fun clParam (ty, x) = CL.PARAM([], ty, x)
89 :     end (* local *)
90 :    
91 :     (* OpenCL global pointer type *)
92 :     fun globalPtr ty = CL.T_Qual("__global", CL.T_Ptr ty)
93 :    
94 :     (* lvalue/rvalue state variable *)
95 :     fun lvalueSV name = CL.mkIndirect(CL.mkVar "selfOut", name)
96 :     fun rvalueSV name = CL.mkIndirect(CL.mkVar "selfIn", name)
97 :    
98 : jhr 1671 (* TreeIL to target translations *)
99 :     structure Tr =
100 :     struct
101 :     fun fragment (ENV{info, vMap, scope}, blk) = let
102 :     val (vMap, stms) = (case scope
103 :     of GlobalScope => ToC.trFragment (vMap, blk)
104 :     | InitiallyScope => ToC.trFragment (vMap, blk)
105 :     | _ => ToCL.trFragment (vMap, blk)
106 :     (* end case *))
107 :     in
108 :     (ENV{info=info, vMap=vMap, scope=scope}, stms)
109 :     end
110 :     fun block (ENV{vMap, scope, ...}, blk) = (case scope
111 : jhr 2405 of StrandScope => ToC.trBlock (vMap, blk)
112 :     | MethodScope name => ToCL.trBlock (vMap, blk)
113 :     | InitiallyScope => ToCL.trBlock (vMap, blk)
114 :     | _ => ToC.trBlock (vMap, blk)
115 :     (* end case *))
116 : nseltzer 1870 fun free (ENV{vMap, ...}, blk) = ToC.trFree (vMap, blk)
117 : jhr 1671 fun exp (ENV{vMap, ...}, e) = ToCL.trExp(vMap, e)
118 :     end
119 :    
120 :     (* variables *)
121 :     structure Var =
122 :     struct
123 : jhr 2405 fun mirror (ty, name, shadowEnv) = MV{
124 :     var = name,
125 :     ty = ty,
126 :     hToS = (case shadowEnv
127 :     of GLOBAL_SHADOW => convertToShadow (ty, name)
128 :     | STRAND_SHADOW => convertStrandToShadow(ty, name, "selfIn", "selfOut")
129 :     (* end case *))
130 : jhr 1671 }
131 :     fun name (ToCL.V(_, name)) = name
132 :     fun global (Prog{globals, imgGlobals, ...}, name, ty) = let
133 :     val x = mirror (ty, name, GLOBAL_SHADOW)
134 : jhr 1793 fun isImgGlobal (Ty.ImageTy info, name) =
135 :     imgGlobals := (name, ImageInfo.dim info) :: !imgGlobals
136 : jhr 1671 | isImgGlobal _ = ()
137 :     in
138 :     globals := x :: !globals;
139 :     isImgGlobal (ty, name);
140 :     ToCL.V(#gpuTy x, name)
141 :     end
142 : jhr 2405 fun param x = ToCL.V(CLTyTranslate.toGPUType(V.ty x), V.name x)
143 : jhr 1671 end
144 :    
145 :     (* environments *)
146 :     structure Env =
147 :     struct
148 :     (* create a new environment *)
149 :     fun new prog = ENV{
150 :     info=INFO{prog = prog},
151 :     vMap = V.Map.empty,
152 :     scope = NoScope
153 :     }
154 :     (* bind a TreeIL varaiable to a target variable *)
155 :     fun bind (ENV{info, vMap, scope}, x, x') = ENV{
156 :     info = info,
157 :     vMap = V.Map.insert(vMap, x, x'),
158 :     scope = scope
159 :     }
160 : jhr 2102 (* define the current translation context *)
161 : jhr 2405 fun setScope (scope, glob, selfIn, selfOut) (ENV{info, vMap, ...}) = ENV{
162 :     info = info,
163 :     vMap = List.foldl
164 :     (fn ((x, x'), vm) => V.Map.insert(vm, x, x'))
165 :     vMap [
166 :     (PseudoVars.global, CL.V(CL.voidTy, glob)),
167 :     (PseudoVars.selfIn, CL.V(CL.voidTy, selfIn)),
168 :     (PseudoVars.selfOut, CL.V(CL.voidTy, selfOut))
169 :     ],
170 :     scope = scope
171 :     }
172 : jhr 2102 (* define the current translation context *)
173 : jhr 2405 val scopeGlobal = setScope (GlobalScope, CLN.globalsVarName, "_bogus_", "_bogus_")
174 : jhr 2102 val scopeInitially =
175 : jhr 2405 setScope (InitiallyScope, CLN.globalsVarName, "_bogus_", "_bogus_")
176 :     val scopeStrand = setScope (StrandScope, CLN.globalsVarName, "selfIn", "selfOut")
177 : jhr 2102 fun scopeMethod (env, name) =
178 : jhr 2405 setScope (MethodScope name, CLN.globalsVarName, "selfIn", "selfOut") env
179 : jhr 1671 end
180 :    
181 : jhr 1853 (* strands *)
182 :     structure Strand =
183 :     struct
184 : jhr 2405 fun define (prog as Prog{strands, ...}, strandId, state) = let
185 : jhr 1853 val name = Atom.toString strandId
186 :     (* the output state variable *)
187 :     val outputVar = (case List.filter IL.StateVar.isOutput state
188 :     of [] => raise Fail("no output specified for strand " ^ name)
189 :     | [x] => (IL.StateVar.ty x, IL.StateVar.name x)
190 :     | _ => raise Fail("multiple outputs in " ^ name)
191 :     (* end case *))
192 :     (* the state variables *)
193 :     val state = let
194 :     fun cvt x = Var.mirror (IL.StateVar.ty x, IL.StateVar.name x, STRAND_SHADOW)
195 :     in
196 :     List.map cvt state
197 :     end
198 :     val strand = Strand{
199 : jhr 2405 prog = prog,
200 : jhr 1853 name = name,
201 : jhr 2405 tyName = N.strandTy name,
202 : jhr 1853 state = state,
203 :     output = outputVar,
204 : jhr 2405 code = ref []
205 : jhr 1853 }
206 :     in
207 :     AtomTable.insert strands (strandId, strand);
208 :     strand
209 :     end
210 :    
211 :     (* return the strand with the given name *)
212 :     fun lookup (Prog{strands, ...}, strandId) = AtomTable.lookup strands strandId
213 :    
214 :     (* register the strand-state initialization code. The variables are the strand
215 :     * parameters.
216 :     *)
217 : jhr 2405 fun init (Strand{prog=Prog{props, ...}, name, tyName, code, ...}, params, init) = let
218 :     val globTy = CL.T_Ptr(CL.T_Named(N.globalTy props))
219 :     val fName = N.strandInit name
220 :     val selfParam = if Properties.dualState props
221 :     then "selfOut"
222 :     else "self"
223 : jhr 1853 val params =
224 : jhr 2405 CL.PARAM([], globTy, "glob") ::
225 :     CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), selfParam) ::
226 :     List.map (fn (CL.V(ty, x)) => CL.PARAM([], ty, x)) params
227 : jhr 1853 val initFn = CL.D_Func([], CL.voidTy, fName, params, init)
228 :     in
229 : jhr 2405 code := initFn :: !code
230 : jhr 1853 end
231 : jhr 2405
232 : jhr 1853 (* register a strand method *)
233 : jhr 2405 fun method (Strand{prog=Prog{props, ...}, name, tyName, code, ...}, methName, body) = let
234 :     val globTy = CL.T_Ptr(CL.T_Named(N.globalTy props))
235 :     val fName = concat[name, "_", StrandUtil.nameToString methName]
236 :     val stateParams = if Properties.dualState props
237 :     then [
238 :     CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "selfIn"),
239 :     CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "selfOut")
240 :     ]
241 :     else [CL.PARAM([], CL.T_Ptr(CL.T_Named tyName), "self")]
242 :     val params =
243 :     globalParam (globTy, "glob") ::
244 :     clParam (CL.T_Named(CLN.imageDataType), CLN.globalImageDataName) ::
245 :     stateParams
246 :     val resTy = (case methName
247 :     of StrandUtil.Update => CL.T_Named "StrandStatus_t"
248 :     | StrandUtil.Stabilize => CL.voidTy
249 : jhr 1853 (* end case *))
250 :     val methFn = CL.D_Func([], resTy, fName, params, body)
251 :     in
252 :     code := methFn :: !code
253 :     end
254 :    
255 : jhr 2405 end
256 : jhr 1853
257 : jhr 1671 (* programs *)
258 :     structure Program =
259 :     struct
260 : jhr 2076 fun new (tgt : TargetUtil.target_desc, props) = (
261 : jhr 2405 N.initTargetSpec {double= #double tgt, long=false};
262 : jhr 1671 Prog{
263 : jhr 2082 props = Properties.mkProps (tgt, props),
264 : jhr 2405 inputs = ref [],
265 : jhr 1671 globals = ref [],
266 :     topDecls = ref [],
267 :     strands = AtomTable.mkTable (16, Fail "strand table"),
268 : jhr 2405 nAxes = ref(SOME ~1),
269 :     initially = ref(CL.D_Comment["missing initially"])
270 : jhr 1671 })
271 : jhr 2405 (* FIXME: for standalone exes, the defaults should be set in the inputs struct;
272 :     * not sure how to handle library inputs yet.
273 :     *)
274 :     (* DEPRECATED
275 :     (* register the code that is used to set defaults for input variables *)
276 :     fun inputs (Prog{props, inputs, topDecls, ...}, env, blk) = let
277 :     val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy props))
278 :     val globTy = CL.T_Ptr(CL.T_Named(N.globalTy props))
279 :     val body = CL.mkBlock(
280 :     CL.mkDeclInit(globTy, "glob", CL.mkIndirect(CL.mkVar "wrld", "globals")) ::
281 :     CL.unBlock (Tr.block (env, blk)))
282 : jhr 1671 val inputsFn = CL.D_Func(
283 : jhr 2405 ["static"], CL.voidTy, N.initDefaults,
284 :     [CL.PARAM([], worldTy, "wrld")],
285 :     body)
286 : jhr 1671 in
287 : jhr 2405 inputs := GenInputs.gatherInputs blk;
288 : jhr 1671 topDecls := inputsFn :: !topDecls
289 :     end
290 : jhr 2405 *)
291 :     (* gather the inputs *)
292 :     fun inputs (Prog{inputs, ...}, env, blk) = inputs := GenInputs.gatherInputs blk
293 : jhr 1671 (* register the global initialization part of a program *)
294 : jhr 2405 fun init (Prog{props, topDecls, ...}, init) = let
295 :     val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy props))
296 :     val globTy = CL.T_Ptr(CL.T_Named(N.globalTy props))
297 :     val wrldV = CL.mkVar "wrld"
298 :     (* the body of the global initializtion code *)
299 :     val initStms =
300 :     CL.mkDeclInit(globTy, "glob", CL.mkIndirect(wrldV, "globals")) ::
301 :     CL.unBlock init @ [CL.mkReturn(SOME(CL.mkVar "false"))]
302 :     (* for libraries, we need to make sure that the inputs are initialized *)
303 :     val initStms = if not(#exec props)
304 :     then CL.mkIfThen(
305 :     CL.mkApply(N.checkDefined props, [wrldV]),
306 :     CL.mkReturn(SOME(CL.mkBool true))) :: initStms
307 :     else initStms
308 : jhr 1671 val initFn = CL.D_Func(
309 : jhr 2405 ["static"], CL.boolTy, N.initGlobals,
310 :     [CL.PARAM([], worldTy, "wrld")],
311 :     CL.mkBlock initStms)
312 : jhr 1671 in
313 : jhr 2405 topDecls := initFn :: !topDecls
314 : jhr 1671 end
315 : nseltzer 1870 (* register the global destruction part of a program *)
316 : jhr 2082 fun free (Prog{props, topDecls, ...}, free) = let
317 :     val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy props))
318 :     val globTy = CL.T_Ptr(CL.T_Named(N.globalTy props))
319 : nseltzer 1870 val free = CL.mkBlock(
320 :     CL.mkDeclInit(globTy, "glob", CL.mkIndirect(CL.mkVar "wrld", "globals")) ::
321 :     CL.unBlock free @ [CL.mkReturn(SOME(CL.mkVar "false"))])
322 :     val freeFn = CL.D_Func(
323 :     ["static"], CL.boolTy, N.freeGlobals,
324 :     [CL.PARAM([], worldTy, "wrld")],
325 :     free)
326 :     in
327 :     topDecls := freeFn :: !topDecls
328 :     end
329 : jhr 1671 (* create and register the initially function for a program *)
330 :     fun initially {
331 : jhr 2082 prog = Prog{props, strands, initially, numDims, ...},
332 : jhr 1671 isArray : bool,
333 :     iterPrefix : stm list,
334 :     iters : (var * exp * exp) list,
335 :     createPrefix : stm list,
336 :     strand : Atom.atom,
337 :     args : exp list
338 :     } = let
339 :     val name = Atom.toString strand
340 :     val nDims = List.length iters
341 : jhr 2082 val worldTy = CL.T_Ptr(CL.T_Named(N.worldTy props))
342 : jhr 2405 val globTy = CL.T_Ptr(CL.T_Named(N.globalTy props))
343 : jhr 1671 fun mapi f xs = let
344 :     fun mapf (_, []) = []
345 :     | mapf (i, x::xs) = f(i, x) :: mapf(i+1, xs)
346 :     in
347 :     mapf (0, xs)
348 :     end
349 :     val baseInit = mapi (fn (i, (_, e, _)) => (i, CL.I_Exp e)) iters
350 :     val sizeInit = mapi
351 :     (fn (i, (CL.V(ty, _), lo, hi)) =>
352 : jhr 2405 (i, CL.I_Exp(CL.mkBinOp(CL.mkBinOp(hi, CL.#-, lo), CL.#+, CL.E_Int(1, ty))))
353 : jhr 1671 ) iters
354 :     (* code to allocate the world and initial strands *)
355 :     val allocCode = [
356 :     CL.mkComment["allocate initial block of strands"],
357 : jhr 2405 CL.mkDecl(CL.T_Array(CL.uint32, SOME nDims), "base", SOME(CL.I_Array baseInit)),
358 : jhr 1671 CL.mkDecl(CL.T_Array(CL.uint32, SOME nDims), "size", SOME(CL.I_Array sizeInit)),
359 : jhr 2405 CL.mkIfThen(CL.mkApply(N.allocInitially, [
360 :     CL.mkVar "wrld",
361 :     CL.E_Bool isArray,
362 :     CL.E_Int(IntInf.fromInt nDims, CL.int32),
363 :     CL.E_Var "base",
364 :     CL.E_Var "size"
365 :     ]),
366 :     (* then *)
367 :     CL.mkBlock [
368 :     (* FIXME: anything else? *)
369 :     CL.mkReturn(SOME(CL.mkVar "true"))
370 :     ])
371 :     (* endif *)
372 : jhr 1671 ]
373 :     (* create the loop nest for the initially iterations *)
374 :     val indexVar = "ix"
375 :     val strandTy = CL.T_Ptr(CL.T_Named(N.strandTy name))
376 : jhr 2405 fun statePtr inout = CL.mkSubscript(CL.mkIndirect(CL.mkVar "wrld", inout), CL.mkVar indexVar)
377 : jhr 1671 fun mkLoopNest [] = CL.mkBlock(createPrefix @ [
378 : jhr 2405 CL.mkCall(N.strandInit name, CL.mkVar "glob" :: statePtr "inState" :: args),
379 :     CL.mkCall("memcpy", [
380 :     statePtr "outState", statePtr "inState",
381 :     CL.mkSizeof(CL.T_Named(N.strandTy name))
382 :     ]),
383 :     CL.S_Exp(CL.mkPostOp(CL.mkVar indexVar, CL.^++))
384 : jhr 1671 ])
385 :     | mkLoopNest ((CL.V(ty, param), lo, hi)::iters) = let
386 :     val body = mkLoopNest iters
387 :     in
388 :     CL.mkFor(
389 :     [(ty, param, lo)],
390 : jhr 2405 CL.mkBinOp(CL.E_Var param, CL.#<=, hi),
391 :     [CL.mkPostOp(CL.E_Var param, CL.^++)],
392 : jhr 1671 body)
393 :     end
394 :     val iterCode = [
395 :     CL.mkComment["initially"],
396 : jhr 2405 CL.mkDecl(CL.uint32, indexVar, SOME(CL.I_Exp(CL.E_Int(0, CL.uint32)))),
397 : jhr 1671 mkLoopNest iters
398 :     ]
399 :     val body = CL.mkBlock(
400 : jhr 2405 CL.mkIfThen (CL.mkApply (N.initGlobals, [CL.mkVar "wrld"]),
401 :     CL.mkReturn(SOME(CL.mkVar "true"))
402 :     ) ::
403 :     CL.mkDeclInit (globTy, "glob", CL.mkIndirect(CL.mkVar "wrld", "globals")) ::
404 : jhr 1671 iterPrefix @
405 :     allocCode @
406 :     iterCode @
407 :     [CL.mkReturn(SOME(CL.mkVar "wrld"))])
408 : jhr 2082 val initFn = CL.D_Func([], worldTy, N.initially props, [], body)
409 : jhr 1671 in
410 : jhr 2405 nAxes := (if isArray then SOME nDims else NONE);
411 : jhr 1671 initially := initFn
412 :     end
413 :    
414 :     (***** OUTPUT *****)
415 :    
416 : jhr 2405 (* create the target-specific substitution list *)
417 :     fun mkSubs (props : props, Strand{name, tyName, ...}) = [
418 :     ("CFILE", OS.Path.joinBaseExt{base= #outBase props, ext= SOME "c"}),
419 :     ("HDRFILE", OS.Path.joinBaseExt{base= #outBase props, ext= SOME "h"}),
420 :     ("PREFIX", #namespace props),
421 :     ("SRCFILE", #srcFile props),
422 :     ("STRAND", name),
423 :     ("STRANDTY", tyName)
424 :     ]
425 : jhr 1671
426 : jhr 2405 fun condCons (true, x, xs) = x::xs
427 :     | condCons (false, _, xs) = xs
428 : jhr 1671
429 : jhr 2405 fun verbFrag (props : props, parFrag, seqFrag, subs) =
430 :     CL.verbatimDcl [if (#parallel props) then parFrag else seqFrag] subs
431 : jhr 1671
432 : jhr 2405 fun compile (props : props, basename) = let
433 :     (* generate the C compiler flags *)
434 :     val cflags = ["-I" ^ Paths.diderotInclude, "-I" ^ Paths.teemInclude]
435 :     val cflags = condCons (#parallel props, #pthread Paths.cflags, cflags)
436 :     val cflags = if #debug props
437 :     then #debug Paths.cflags :: cflags
438 :     else #ndebug Paths.cflags :: cflags
439 :     val cflags = #base Paths.cflags :: cflags
440 : jhr 1671 in
441 : jhr 2405 RunCC.compile (basename, cflags)
442 : jhr 1671 end
443 :    
444 : jhr 2405 fun ldFlags (props : props) = if #exec props
445 :     then let
446 :     val extraLibs = condCons (#parallel props, #pthread Paths.extraLibs, [])
447 :     val extraLibs = Paths.teemLinkFlags @ #base Paths.extraLibs :: extraLibs
448 :     val rtLib = Properties.runtimeName props
449 :     in
450 :     rtLib :: extraLibs
451 :     end
452 :     else [Properties.runtimeName props]
453 : jhr 1671
454 : jhr 2405 fun genStrand (Strand{prog=Prog{props, ...}, name, tyName, state, output, code}) = let
455 :     (* the type declaration for the strand's state struct *)
456 :     val selfTyDef = CL.D_StructDef(
457 :     SOME(concat[#namespace props, "struct_", name]),
458 :     List.rev (List.map (fn CL.V(ty, x) => (ty, x)) state),
459 :     NONE)
460 : jhr 1671 (* the type and access expression for the strand's output variable *)
461 :     val (outTy, outState) = (#1 output, CL.mkIndirect(CL.mkVar "self", #2 output))
462 :     (* the strand's descriptor object *)
463 :     val descI = let
464 :     fun fnPtr (ty, f) = CL.I_Exp(CL.mkCast(CL.T_Named ty, CL.mkVar f))
465 :     in
466 :     CL.I_Struct[
467 :     ("name", CL.I_Exp(CL.mkStr name)),
468 : jhr 2405 ("stateSzb", CL.I_Exp(CL.mkSizeof(CL.T_Named(N.strandTy name)))),
469 :     ("update", fnPtr("update_method_t", name ^ "_Update")),
470 :     ("stabilize", fnPtr("stabilize_method_t", name ^ "_Stabilize"))
471 : jhr 1671 ]
472 :     end
473 :     val desc = CL.D_Var([], CL.T_Named N.strandDescTy, N.strandDesc name, SOME descI)
474 :     in
475 : jhr 2405 selfTyDef :: List.rev (desc :: !code)
476 : jhr 1671 end
477 :    
478 : jhr 2405 fun genGlobalStruct (props : props, globals) =
479 :     CL.D_StructDef(NONE, globals, SOME(#namespace props ^ "Globals_t"))
480 :    
481 :     (* generate the struct declaration for the world representation *)
482 :     fun genWorldStruct (props, Strand{tyName, ...}) = let
483 :     val extras = [
484 :     (* target-specific world components *)
485 :     (CL.T_Ptr(CL.T_Named(N.globalsTy props)), "globals"),
486 :     (CL.T_Ptr CL.uint8, "status"),
487 :     (CL.T_Ptr(CL.T_Ptr(CL.T_Named tyName)), "inState"),
488 :     (CL.T_Ptr(CL.T_Ptr(CL.T_Named tyName)), "outState")
489 :     ]
490 :     val extras = if #exec props
491 :     then extras
492 :     else (CL.T_Named(N.definedInpTy props), "definedInp") :: extras
493 :     val extras = if #parallel props
494 :     then (CL.T_Ptr(CL.T_Named "Diderot_Sched_t"), "sched") :: extras
495 :     else (CL.T_Named "uint32_t", "numActive") :: extras
496 :     in
497 :     World.genStruct (props, extras)
498 :     end
499 :    
500 : jhr 1671 (* generate the table of strand descriptors *)
501 : jhr 2405 fun ppStrandTable (ppStrm, strands) = let
502 : jhr 1671 val nStrands = length strands
503 : jhr 2405 fun genInit (Strand{name, ...}) = CL.I_Exp(CL.mkUnOp(CL.%&, CL.E_Var(N.strandDesc name)))
504 : jhr 1671 fun genInits (_, []) = []
505 :     | genInits (i, s::ss) = (i, genInit s) :: genInits(i+1, ss)
506 : jhr 2405 fun ppDecl dcl = PrintAsC.output(ppStrm, dcl)
507 : jhr 1671 in
508 : jhr 2405 ppDecl (CL.D_Var(["static const"], CL.int32, "NumStrands",
509 :     SOME(CL.I_Exp(CL.E_Int(IntInf.fromInt nStrands, CL.int32)))));
510 :     ppDecl (CL.D_Var([],
511 : jhr 1671 CL.T_Array(CL.T_Ptr(CL.T_Named N.strandDescTy), SOME nStrands),
512 :     N.strands,
513 :     SOME(CL.I_Array(genInits (0, strands)))))
514 :     end
515 :    
516 : jhr 2405 (* generate the OpenCL source code. *)
517 :     fun outputCLSrc (baseName, prog as Prog{props, ...}) = let
518 :     val fileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "cl"}
519 :     val outS = TextIO.openOut fileName
520 :     val ppStrm = PrintAsCL.new outS
521 :     fun ppDecl dcl = PrintAsCL.output(ppStrm, dcl)
522 : jhr 1671 in
523 : jhr 2405 (* Retrieve the header information *)
524 :     ppDecl (CL.verbatimDcl [CLHeadFrag.text] [
525 :     ("OUTFILE", fileName),
526 :     ("SRCFILE", #srcFile props),
527 :     ("DIDEROT_FLOAT_PRECISION", Properties.floatPrecisionDef props),
528 :     ("DIDEROT_INT_PRECISION", Properties.intPrecisionDef props)
529 :     ]);
530 :     (* FIXME: check to see if we really need the DUAL_STATE define for OpenCL *)
531 :     if Properties.dualState props
532 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_DUAL_STATE\n"])
533 :     else ();
534 :     (* if there are no globals, then define a dummy type *)
535 :     if List.null(!globals)
536 :     then ppDecl (CL.D_Verbatim["typedef void ", CLN.globalsTy, ";\n"])
537 :     else ();
538 :     (* if there are no images, then define a dummy type *)
539 :     if List.null(!imgGlobals)
540 :     then ppDecl (CL.D_Verbatim["typedef void * ", CLN.imageDataType, ";\n"])
541 :     else ();
542 :     (* Retrieve the scheduler kernels and functions *)
543 :     ppDecl (CL.D_Verbatim[CLSchedFrag.text]);
544 :     (* FIXME: should only include eigen code fragments if they are being used! *)
545 :     ppDecl (CL.D_Verbatim[CLEigen2x2Frag.text]);
546 :     (* FIXME: should only include eigen code fragments if they are being used! *)
547 :     ppDecl (CL.D_Verbatim[CLEigen3x3Frag.text]);
548 :     ppDecl (genGlobalStruct (#gpuTy, !globals, CLN.globalsTy));
549 :     ppDecl (genImageDataStruct(!imgGlobals, CLN.imageDataType));
550 :     ppDecl (genStrandTyDef(#gpuTy, strand, tyName));
551 :     List.app clppDppDeclecl (!code);
552 :     ppDecl (genStrandCopy strand);
553 :     ppDecl (genUpdateMethod(strand, globals, imgGlobals))
554 : jhr 1671 end
555 :    
556 : jhr 2405 fun outputLibSrc (baseName, Prog{
557 :     props, inputs, globals, topDecls, strands, nAxes, initially, ...
558 :     }) = let
559 :     val [strand as Strand{name, tyName, state, output, ...}] = AtomTable.listItems strands
560 :     val outputs = GenOutput.gen (props, !nAxes) [output]
561 :     val substitutions = mkSubs (props, strand)
562 :     (* output to C file *)
563 :     val fileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"}
564 :     val outS = TextIO.openOut fileName
565 :     val ppStrm = PrintAsC.new outS
566 :     fun ppDecl dcl = PrintAsC.output(ppStrm, dcl)
567 : jhr 1671 in
568 : jhr 2405 ppDecl (CL.verbatimDcl [CHeadFrag.text] substitutions);
569 :     if Properties.dualState props
570 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_DUAL_STATE\n"])
571 :     else ();
572 :     ppDecl (GenInputs.genDefinedInpStruct (props, !inputs));
573 :     ppDecl (genGlobalStruct (props, List.rev(!globals)));
574 :     ppDecl (genWorldStruct(props, strand));
575 :     List.app ppDecl (GenInputs.genInputFuns(props, !inputs));
576 :     List.app ppDecl (List.rev (!topDecls));
577 :     List.app ppDecl (genStrand strand);
578 :     List.app ppDecl outputs;
579 :     ppStrandTable (ppStrm, [strand]);
580 :     ppDecl (CL.verbatimDcl [CBodyFrag.text] substitutions);
581 :     ppDecl (CL.verbatimDcl [InitFrag.text] substitutions);
582 :     ppDecl (CL.verbatimDcl [AllocFrag.text] substitutions);
583 :     ppDecl (!initially);
584 :     ppDecl (CL.verbatimDcl [RunFrag.text] substitutions);
585 :     ppDecl (CL.verbatimDcl [ShutdownFrag.text] substitutions);
586 :     PrintAsC.close ppStrm;
587 :     TextIO.closeOut outS
588 : jhr 1671 end
589 :    
590 : jhr 2405 fun generateLib (prog as Prog{props, inputs, strands, ...}) = let
591 :     val {outDir, outBase, exec, double, parallel, debug, ...} = props
592 :     val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
593 :     val [Strand{state, output, ...}] = AtomTable.listItems strands
594 :     in
595 :     (* generate the library .h file *)
596 :     GenLibraryInterface.gen {
597 :     props = props,
598 :     rt = SOME LibInterfaceCLFrag.text,
599 :     inputs = !inputs,
600 :     outputs = [output]
601 :     };
602 :     (* *)
603 :     outputLibSrc (basename, prog);
604 :     outputCLSrc (basename, prog);
605 :     (* compile and link *)
606 :     compile (props, basename);
607 :     RunCC.linkLib (basename, ldFlags props)
608 :     end
609 : jhr 1671
610 : jhr 2405 fun outputExecSrc (baseName, prog) = let
611 :     val Prog{props, inputs, globals, topDecls, strands, nAxes, initially, ...} = prog
612 :     val [strand as Strand{name, tyName, state, output, ...}] = AtomTable.listItems strands
613 :     val outputs = GenOutput.gen (props, !nAxes) [output]
614 :     val substitutions =
615 :     ("DIDEROT_FLOAT_PRECISION", Properties.floatPrecisionDef props) ::
616 :     ("DIDEROT_INT_PRECISION", Properties.intPrecisionDef props) ::
617 :     ("DIDEROT_TARGET", Properties.targetDef props) ::
618 :     mkSubs (props, strand)
619 :     val fileName = OS.Path.joinBaseExt{base=baseName, ext=SOME "c"}
620 :     val outS = TextIO.openOut fileName
621 :     val ppStrm = PrintAsC.new outS
622 :     fun ppDecl dcl = PrintAsC.output(ppStrm, dcl)
623 :     in
624 :     ppDecl (CL.verbatimDcl [ExecHdrFrag.text] substitutions);
625 :     if Properties.dualState props
626 :     then ppDecl (CL.D_Verbatim ["#define DIDEROT_DUAL_STATE\n"])
627 :     else ();
628 :     ppDecl (genGlobalStruct (props, List.rev(!globals)));
629 :     ppDecl (genWorldStruct(props, strand));
630 :     ppDecl (GenInputs.genInputsStruct (props, !inputs));
631 :     List.app ppDecl (List.rev (!topDecls));
632 :     List.app ppDecl (GenInputs.genExecInputFuns (props, !inputs));
633 :     List.app ppDecl (genStrand strand);
634 :     List.app ppDecl outputs;
635 :     ppStrandTable (ppStrm, [strand]);
636 :     ppDecl (CL.verbatimDcl [InitFrag.text] substitutions);
637 :     ppDecl (CL.verbatimDcl [AllocFrag.text] substitutions);
638 :     ppDecl (!initially);
639 :     ppDecl (CL.verbatimDcl [RunFrag.text] substitutions);
640 :     ppDecl (CL.verbatimDcl [ShutdownFrag.text] substitutions);
641 :     ppDecl (CL.verbatimDcl [MainFrag.text] substitutions);
642 :     PrintAsC.close ppStrm;
643 :     TextIO.closeOut outS
644 :     end
645 :    
646 :     (* output the code to a file. The string is the basename of the file, the extension
647 :     * is provided by the target.
648 :     *)
649 :     fun generateExec (prog as Prog{props, ...}) = let
650 :     val {outDir, outBase, exec, double, parallel, debug, ...} = props
651 :     val basename = OS.Path.joinDirFile{dir=outDir, file=outBase}
652 :     in
653 :     outputExecSrc (basename, prog);
654 :     outputCLSrc (basename, prog);
655 :     compile (props, basename);
656 :     RunCC.linkExec (basename, ldFlags props)
657 :     end
658 :    
659 :     fun generate (prog as Prog{props, ...}) = if #exec props
660 :     then generateExec prog
661 :     else generateLib prog
662 :    
663 :     end
664 :    
665 : jhr 1671 end
666 :    
667 :     structure CLBackEnd = CodeGenFn(CLTarget)

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