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

SCM Repository

[diderot] Diff of /branches/vis15/src/compiler/target-cpu/gen-outputs.sml
ViewVC logotype

Diff of /branches/vis15/src/compiler/target-cpu/gen-outputs.sml

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3916, Sat May 28 12:01:02 2016 UTC revision 3917, Sat May 28 16:41:39 2016 UTC
# Line 47  Line 47 
47     *        nAxes       - the number of axes in the grid of strands (NONE for a collection)     *        nAxes       - the number of axes in the grid of strands (NONE for a collection)
48     *        outputs     - the list of output state variables paired with their API types     *        outputs     - the list of output state variables paired with their API types
49     *)     *)
50      val gen : CodeGenEnv.t * int option -> (APITypes.t * string) list -> CLang.decl list      val gen : CodeGenEnv.t * int option * OutputUtil.output_info list -> CLang.decl list
51    
52    end = struct    end = struct
53    
# Line 55  Line 55 
55      structure V = TreeVar      structure V = TreeVar
56      structure Ty = APITypes      structure Ty = APITypes
57      structure CL = CLang      structure CL = CLang
     structure N = CNames  
58      structure Nrrd = NrrdEnums      structure Nrrd = NrrdEnums
59      structure U = OutputUtil      structure U = OutputUtil
60        structure RN = CxxNames
61        structure Env = CodeGenEnv
62    
63      fun mapi f l = let      fun mapi f l = let
64            fun mapf (i, [], l) = List.rev l            fun mapf (i, [], l) = List.rev l
# Line 68  Line 69 
69    
70      val nrrdPtrTy = CL.T_Ptr(CL.T_Named "Nrrd")      val nrrdPtrTy = CL.T_Ptr(CL.T_Named "Nrrd")
71      val sizeTy = CL.T_Named "size_t"      val sizeTy = CL.T_Named "size_t"
72      fun wrldPtr tgt = N.worldPtrTy tgt      val wrldPtr = RN.worldPtrTy
73      fun mkInt i = CL.mkInt(IntInf.fromInt i)      fun mkInt i = CL.mkInt(IntInf.fromInt i)
74    
75    (* variables in the generated code *)    (* variables in the generated code *)
# Line 85  Line 86 
86      val numStableV = CL.mkVar "numStable"      val numStableV = CL.mkVar "numStable"
87      val numElemsV = CL.mkVar "numElems"      val numElemsV = CL.mkVar "numElems"
88      val outSV = CL.mkVar "outS"      val outSV = CL.mkVar "outS"
89      val DIDEROT_DIE = CL.mkVar "DIDEROT_DIE"      val DIDEROT_DEAD = CL.mkVar "diderot::kDead"
90      val DIDEROT_STABLE = CL.mkVar "DIDEROT_STABLE"      val DIDEROT_STABLE = CL.mkVar "diderot::kStable"
91      val NRRD = CL.mkVar "NRRD"      val NRRD = CL.mkVar "NRRD"
92    
93    (* dymanic sequence operations *)    (* dymanic sequence operations *)
94      fun seqLength arg = CL.mkApply("Diderot_DynSeqLength", [arg])      fun seqLength arg = CL.mkApply("diderot::DynSeqLength", [arg])
95      fun seqCopy (elemSz, dst, seq) = CL.mkApply("Diderot_DynSeqCopy", [elemSz, dst, seq])      fun seqCopy (elemSz, dst, seq) = CL.mkApply("diderot::DynSeqCopy", [elemSz, dst, seq])
96    
97    (* utility functions for initializing the sizes array *)    (* utility functions for initializing the sizes array *)
98      fun sizes i = CL.mkSubscript(sizesV, mkInt i)      fun sizes i = CL.mkSubscript(sizesV, mkInt i)
# Line 102  Line 103 
103     * or     * or
104          wrld->state[i].name          wrld->state[i].name
105     *)     *)
106      fun stateVar props name = if Properties.dualState props      fun stateVar spec name = if TargetSpec.dualState spec
107            then CL.mkIndirect(CL.mkSubscript(CL.mkIndirect(wrldV, "outState"), iV), name)            then CL.mkIndirect(CL.mkSubscript(CL.mkIndirect(wrldV, "_outState"), iV), name)
108            else CL.mkSelect(CL.mkSubscript(CL.mkIndirect(wrldV, "state"), iV), name)            else CL.mkSelect(CL.mkSubscript(CL.mkIndirect(wrldV, "_state"), iV), name)
109    
110    (* code fragment to loop over strands    (* code fragment to loop over strands
111          for (unsigned int i = 0;  i < wrld->numStrands;  i++) ...          for (unsigned int i = 0;  i < wrld->_num_strands;  i++) ...
112     *)     *)
113      fun forStrands stm = CL.mkFor(      fun forStrands stm = CL.mkFor(
114            [(CL.uint32, "i", mkInt 0)],            [(CL.uint32, "i", mkInt 0)],
115            CL.mkBinOp(iV, CL.#<, CL.mkIndirect(wrldV, "numStrands")),            CL.mkBinOp(iV, CL.#<, CL.mkIndirect(wrldV, "_num_strands")),
116            [CL.mkPostOp(iV, CL.^++)],            [CL.mkPostOp(iV, CL.^++)],
117            stm)            stm)
118    
119    (* code fragment to test for stable strands in a loop    (* code fragment to test for stable strands in a loop
120          if (wrld->status[i] == DIDEROT_STABLE)          if (wrld->_status[i] == diderot::kStable)
121              ...              ...
122     *)     *)
123      fun ifStable stm = CL.mkIfThen(      fun ifStable stm = CL.mkIfThen(
124            CL.mkBinOp(CL.mkSubscript(CL.mkIndirect(wrldV, "status"), iV), CL.#==, DIDEROT_STABLE),            CL.mkBinOp(CL.mkSubscript(CL.mkIndirect(wrldV, "_status"), iV), CL.#==, DIDEROT_STABLE),
125            stm)            stm)
126    
127    (* code fragment to test for active strands in a loop; note that NEW strands are considered active.    (* code fragment to test for active strands in a loop; note that NEW strands are considered active.
128          if (wrld->status[i] != DIDEROT_DIE)          if (wrld->_status[i] != DIDEROT_DIE)
129              ...              ...
130     *)     *)
131      fun ifActive stm = CL.mkIfThen(      fun ifActive stm = CL.mkIfThen(
132            CL.mkBinOp(CL.mkSubscript(CL.mkIndirect(wrldV, "status"), iV), CL.#!=, DIDEROT_DIE),            CL.mkBinOp(CL.mkSubscript(CL.mkIndirect(wrldV, "_status"), iV), CL.#!=, DIDEROT_DEAD),
133            stm)            stm)
134    
135    (* code fragment to initialize the axes kinds; the data axis (axis[0]) is given, but we skip it    (* code fragment to initialize the axes kinds; the data axis (axis[0]) is given, but we skip it
# Line 156  Line 157 
157     *    allocate nrrd for nData     *    allocate nrrd for nData
158     *    copy data from strands to nrrd     *    copy data from strands to nrrd
159     *)     *)
160      fun genDynOutput (tgt, snapshot, nAxes, ty, name) = let      fun genDynOutput (env, snapshot, nAxes, ty, name) = let
161            val (elemCTy, nrrdType, axisKind, nElems) = U.infoOf (tgt, ty)            val spec = Env.target env
162            val stateVar = stateVar tgt            val (elemCTy, nrrdType, axisKind, nElems) = U.infoOf (env, ty)
163              val stateVar = stateVar spec
164            val (nAxes, domAxisKind) = (case nAxes            val (nAxes, domAxisKind) = (case nAxes
165                   of NONE => (1, Nrrd.KindList)                   of NONE => (1, Nrrd.KindList)
166                    | SOME n => (n, Nrrd.KindSpace)                    | SOME n => (n, Nrrd.KindSpace)
# Line 170  Line 172 
172                  val nElemsInit = CL.mkDeclInit(CL.uint32, "numElems", CL.mkInt 0)                  val nElemsInit = CL.mkDeclInit(CL.uint32, "numElems", CL.mkInt 0)
173                  val cntElems = CL.S_Exp(CL.mkAssignOp(numElemsV, CL.+=, seqLength(stateVar name)))                  val cntElems = CL.S_Exp(CL.mkAssignOp(numElemsV, CL.+=, seqLength(stateVar name)))
174                  in                  in
175                    if #isArray tgt                    if #isArray spec
176                      then [                      then [
177                          CL.mkComment["count number of elements"],                          CL.mkComment["count number of elements"],
178                          nElemsInit, forStrands cntElems                          nElemsInit, forStrands cntElems
# Line 188  Line 190 
190                        ] end                        ] end
191                  end                  end
192          (* code to check for zero outputs, which happens for collections with no active strands *)          (* code to check for zero outputs, which happens for collections with no active strands *)
193            val checkForNoStrands = if #isArray tgt            val checkForNoStrands = if #isArray spec
194                  then []                  then []
195                  else [                  else [
196                      CL.mkComment["check for no output"],                      CL.mkComment["check for no output"],
# Line 205  Line 207 
207                  val dimSizes = setSizes(0, CL.mkInt 2)  (* nLengths is 2-element vector *)                  val dimSizes = setSizes(0, CL.mkInt 2)  (* nLengths is 2-element vector *)
208                  in                  in
209                    CL.mkComment["allocate nLengths nrrd"] ::                    CL.mkComment["allocate nLengths nrrd"] ::
210                    (if #isArray tgt                    (if #isArray spec
211                      then dimSizes ::                      then dimSizes ::
212                        List.tabulate (nAxes, fn i =>                        List.tabulate (nAxes, fn i =>
213                          setSizes(i+1, CL.mkSubscript(CL.mkIndirect(wrldV, "size"), mkInt(nAxes-i-1)))) @                          setSizes(i+1, CL.mkSubscript(CL.mkIndirect(wrldV, "size"), mkInt(nAxes-i-1)))) @
# Line 249  Line 251 
251                          CL.mkAssign(CL.mkUnOp(CL.%*, CL.mkPostOp(ipV, CL.^++)), nV),                          CL.mkAssign(CL.mkUnOp(CL.%*, CL.mkPostOp(ipV, CL.^++)), nV),
252                          CL.S_Exp(CL.mkAssignOp(offsetV, CL.+=, nV))                          CL.S_Exp(CL.mkAssignOp(offsetV, CL.+=, nV))
253                        ]                        ]
254                  val copyStm = if #isArray tgt                  val copyStm = if #isArray spec
255                        then copyBlk                        then copyBlk
256                        else if #snapshot tgt                        else if #snapshot spec
257                          then ifActive copyBlk                          then ifActive copyBlk
258                          else ifStable copyBlk                          else ifStable copyBlk
259                  in                  in
# Line 267  Line 269 
269                        CL.mkCast(CL.charPtr, CL.mkIndirect(nDataV, "data")))                        CL.mkCast(CL.charPtr, CL.mkIndirect(nDataV, "data")))
270                  val copyStm = CL.mkAssign(cpV, seqCopy(                  val copyStm = CL.mkAssign(cpV, seqCopy(
271                        CL.mkBinOp(mkInt nElems, CL.#*, CL.mkSizeof(elemCTy)), cpV, stateVar name))                        CL.mkBinOp(mkInt nElems, CL.#*, CL.mkSizeof(elemCTy)), cpV, stateVar name))
272                  val copyStm = if #isArray tgt                  val copyStm = if #isArray spec
273                         then copyStm                         then copyStm
274                        else if #snapshot tgt                        else if #snapshot spec
275                          then ifActive copyStm                          then ifActive copyStm
276                          else ifStable copyStm                          else ifStable copyStm
277                  in                  in
# Line 300  Line 302 
302     *    allocate nrrd nData     *    allocate nrrd nData
303     *    copy data from strands to nrrd     *    copy data from strands to nrrd
304     *)     *)
305      fun genFixedOutput (tgt, snapshot, nAxes, ty, name) = let      fun genFixedOutput (env, snapshot, nAxes, ty, name) = let
306            val (elemCTy, nrrdType, axisKind, nElems) = U.infoOf (tgt, ty)            val spec = Env.target env
307            val stateVar = stateVar tgt            val (elemCTy, nrrdType, axisKind, nElems) = U.infoOf (env, ty)
308              val stateVar = stateVar spec
309            val (nAxes, domAxisKind) = (case nAxes            val (nAxes, domAxisKind) = (case nAxes
310                   of NONE => (1, Nrrd.KindList)                   of NONE => (1, Nrrd.KindList)
311                    | SOME n => (n, Nrrd.KindSpace)                    | SOME n => (n, Nrrd.KindSpace)
# Line 318  Line 321 
321                            else [dcl, setSizes(0, mkInt nElems)]                            else [dcl, setSizes(0, mkInt nElems)]
322                        end                        end
323                  in                  in
324                    if #isArray tgt                    if #isArray spec
325                      then dimSizes @                      then dimSizes @
326                        List.tabulate (nAxes, fn i =>                        List.tabulate (nAxes, fn i =>
327                          setSizes(i+nDataAxes, CL.mkSubscript(CL.mkIndirect(wrldV, "size"), mkInt(nAxes-i-1))))                          setSizes(i+nDataAxes, CL.mkSubscript(CL.mkIndirect(wrldV, "size"), mkInt(nAxes-i-1))))
# Line 334  Line 337 
337                        end                        end
338                  end                  end
339          (* code to check for no data to output (i.e., no active strands) *)          (* code to check for no data to output (i.e., no active strands) *)
340            val checkForEmpty = if (#isArray tgt)            val checkForEmpty = if (#isArray spec)
341                  then []                  then []
342                  else [                  else [
343                      CL.mkComment["check for empty output"],                      CL.mkComment["check for empty output"],
# Line 358  Line 361 
361                          CL.mkExpStm(CL.mkAssignOp(cpV, CL.+=,                          CL.mkExpStm(CL.mkAssignOp(cpV, CL.+=,
362                            CL.mkBinOp(mkInt nElems, CL.#*, CL.mkSizeof elemCTy)))                            CL.mkBinOp(mkInt nElems, CL.#*, CL.mkSizeof elemCTy)))
363                        ]                        ]
364                  val copyStm = if #isArray tgt                  val copyStm = if #isArray spec
365                         then copyBlk                         then copyBlk
366                        else if snapshot                        else if snapshot
367                          then ifActive copyBlk                          then ifActive copyBlk
# Line 380  Line 383 
383              ([CL.PARAM([], nrrdPtrTy, "nData")], CL.mkBlock stms)              ([CL.PARAM([], nrrdPtrTy, "nData")], CL.mkBlock stms)
384            end            end
385    
386      fun gen (tgt : Properties.props, nAxes) = let      fun gen (env, nAxes, outputs) = let
387            fun getFn snapshot (ty, name) = let            val spec = Env.target env
388              fun getFn snapshot {name, ty, isGlobal} = let
389                  val funcName = if snapshot                  val funcName = if snapshot
390                        then N.snapshotGet(tgt, name)                        then GenLibraryInterface.snapshotGet(spec, name)
391                        else N.outputGet(tgt, name)                        else GenLibraryInterface.outputGet(spec, name)
392                  val fldName = "sv_" ^ name                  val fldName = "sv_" ^ name
393                  fun mkFunc (params, body) =                  fun mkFunc (params, body) =
394                        CL.D_Func([], CL.boolTy, funcName, CL.PARAM([], wrldPtr tgt, "wrld")::params, body)                        CL.D_Func(
395                            [], CL.boolTy, funcName, CL.PARAM([], RN.worldPtrTy, "wrld")::params, body)
396                  in                  in
397                    case ty                    case ty
398                     of Ty.DynSeqTy ty' => mkFunc (genDynOutput(tgt, snapshot, nAxes, ty', fldName))                     of Ty.SeqTy(ty', NONE) => mkFunc (genDynOutput(env, snapshot, nAxes, ty', fldName))
399                      | _ => mkFunc (genFixedOutput(tgt, snapshot, nAxes, ty, fldName))                      | _ => mkFunc (genFixedOutput(env, snapshot, nAxes, ty, fldName))
400                    (* end case *)                    (* end case *)
401                  end                  end
           fun gen' outputs = let  
402                  val getFns = List.map (getFn false) outputs                  val getFns = List.map (getFn false) outputs
403                  in                  in
404                    if (#exec tgt)              if (#exec spec)
405                      then getFns @ U.genOutput(tgt, outputs)                then getFns @ U.genOutput(env, outputs)
406                    else if (#snapshot tgt)              else if (#snapshot spec)
407                      then List.map (getFn true) outputs @ getFns                      then List.map (getFn true) outputs @ getFns
408                      else getFns                      else getFns
409                  end                  end
           in  
             gen'  
           end  
410    
411    end    end

Legend:
Removed from v.3916  
changed lines
  Added in v.3917

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