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

SCM Repository

[diderot] Diff of /branches/vis12/src/compiler/c-target/gen-output.sml
ViewVC logotype

Diff of /branches/vis12/src/compiler/c-target/gen-output.sml

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

revision 1791, Wed Apr 4 11:25:58 2012 UTC revision 1792, Wed Apr 4 11:28:02 2012 UTC
# Line 3  Line 3 
3   * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)   * COPYRIGHT (c) 2012 The Diderot Project (http://diderot-language.cs.uchicago.edu)
4   * All rights reserved.   * All rights reserved.
5   *   *
6   * Generate strand output functions.  There are four cases that we handle:   * Generate strand output functions.  The output formats always have a single axis for the
7     * data elements followed by one, or more, axes for the output structure.  There are four
8     * cases that we handle:
9   *   *
10   *      grid, fixed-size elements:   *      grid, fixed-size elements:
11   *              nrrd has object dimensions followed by axes   *              nrrd has object axis followed by grid axes
12   *   *
13   *      collection, fixed-size elements   *      collection, fixed-size elements
14   *              nrrd has object dims followed by a single axis   *              nrrd has object axis followed by a single axis
15   *   *
16   *      grid, dynamic-size elements   *      grid, dynamic-size elements
17   *              nLengths nrrd has size 2 for objects (offset, length) followed by axes   *              nLengths nrrd has size 2 for objects (offset, length) followed by grid axes
18   *              nData nrrd has object dimensions followed by a single axis   *              nData nrrd has object axis followed by a single axis
19   *   *
20   *      collection, dynamic-size elements   *      collection, dynamic-size elements
21   *              nLengths nrrd has size 2 for objects (offset, length) followed by a single axis   *              nLengths nrrd has size 2 for objects (offset, length) followed by a single axis
22   *              nData nrrd has object dimensions followed by a single axis   *              nData nrrd has object axis followed by a single axis
23     *
24     * The object axis kind depends on the output type, but it will either be one of the tensor types
25     * that Teem knows about or else nrrdKindList.  In any case, the data elements are written as a
26     * flat vector following the in-memory layout.  The other axes in the file will have nrrdKindSpace
27     * as their kind.
28   *   *
29   * TODO: some of this code will be common across all targets (e.g., writing outputs to files), so   * TODO: some of this code will be common across all targets (e.g., writing outputs to files), so
30   * we will want to refactor it.   * we will want to refactor it.
# Line 50  Line 57 
57    
58      fun mkInt i = CL.mkInt(IntInf.fromInt i)      fun mkInt i = CL.mkInt(IntInf.fromInt i)
59    
60      (* return information about the output type.  This is a tuple
61       *
62       *    (c-type, nrrd-type, nrrd-axis-kind, # elements)
63       *)
64      fun infoOf (tgt : TargetUtil.target_desc, ty) = (case ty      fun infoOf (tgt : TargetUtil.target_desc, ty) = (case ty
65             of Ty.IntTy => if #longint tgt             of Ty.IntTy => if #longint tgt
66                  then (CL.int64, Nrrd.TypeLLong, [1])                  then (CL.int64, Nrrd.TypeLLong, Nrrd.KindScalar, 1)
67                  else (CL.int32, Nrrd.TypeInt, [1])                  else (CL.int32, Nrrd.TypeInt, Nrrd.KindScalar, 1)
68              | Ty.TensorTy [] => if #double tgt              | Ty.TensorTy [] => if #double tgt
69                  then (CL.double, Nrrd.TypeDouble, [1])                  then (CL.double, Nrrd.TypeDouble, Nrrd.KindScalar, 1)
70                  else (CL.float, Nrrd.TypeFloat, [1])                  else (CL.float, Nrrd.TypeFloat, Nrrd.KindScalar, 1)
71              | Ty.TensorTy dims => if #double tgt              | Ty.TensorTy dims => let
72                  then (CL.double, Nrrd.TypeDouble, dims)                  val (axisKind, nElems) = (case dims
73                  else (CL.float, Nrrd.TypeFloat, dims)                         of [2] => (Nrrd.Kind2Vector, 2)
74              | Ty.SeqTy(ty, n) => let                          | [3] => (Nrrd.Kind3Vector, 3)
75                            | [4] => (Nrrd.Kind4Vector, 4)
76                            | [2,2] => (Nrrd.Kind2DMatrix, 4)
77                            | [3,3] => (Nrrd.Kind3DMatrix, 9)
78                            | _ => (Nrrd.KindList, List.foldl Int.* 1 dims)
79                          (* end case *))
80                    in
81                      if #double tgt
82                        then (CL.double, Nrrd.TypeDouble, axisKind, nElems)
83                        else (CL.float, Nrrd.TypeFloat, axisKind, nElems)
84                    end
85                | Ty.SeqTy(ty, n) => raise Fail "FIXME" (*let
86                  val (elemTy, nrrdTy, dims) = infoOf (tgt, ty)                  val (elemTy, nrrdTy, dims) = infoOf (tgt, ty)
87                  in                  in
88                    (elemTy, nrrdTy, n::dims)                    (elemTy, nrrdTy, n::dims)
89                  end                  end*)
90              | _ => raise Fail(concat["GetOutput.infoOf(", Ty.toString ty, ")"])              | _ => raise Fail(concat["GetOutput.infoOf(", Ty.toString ty, ")"])
91            (* end case *))            (* end case *))
92    
# Line 139  Line 161 
161                ]                ]
162            (* endif*))            (* endif*))
163    
164      (* code fragment to initialize the axes kinds; axis[0] is given, the others are always KindSpace *)
165        fun initAxisKinds (nrrd, dataAxisKind, nAxes) = let
166            (* nData->axis[0].kind *)
167              fun axisKind i = CL.mkSelect(CL.mkSubscript(CL.mkIndirect(nrrd, "axis"), mkInt i), "kind")
168              fun init (i, k) = CL.mkAssign (axisKind i, CL.mkVar(Nrrd.kindToEnum  k))
169              in
170                init(0, dataAxisKind) :: List.tabulate(nAxes, fn i => init(i+1, Nrrd.KindSpace))
171              end
172    
173    (* create the body of an output function for dynamic-size outputs.  The structure of the    (* create the body of an output function for dynamic-size outputs.  The structure of the
174     * function body is:     * function body is:
175     *     *
# Line 150  Line 181 
181     *    copy data from strands to nrrd     *    copy data from strands to nrrd
182     *)     *)
183      fun genDynOutput (tgt, nAxes, ty, name) = let      fun genDynOutput (tgt, nAxes, ty, name) = let
184            val (elemCTy, nrrdType, dims) = infoOf (tgt, ty)            val (elemCTy, nrrdType, axisKind, nElems) = infoOf (tgt, ty)
           val nDims = List.length dims  
           val nElems = List.foldl Int.* 1 dims  
185            val (isArray, nAxes) = (case nAxes of NONE => (false, 1) | SOME n => (true, n))            val (isArray, nAxes) = (case nAxes of NONE => (false, 1) | SOME n => (true, n))
186          (* declarations *)          (* declarations *)
187            val sizesDecl = CL.mkDecl(CL.T_Array(sizeTy, SOME(nDims+nAxes)), "sizes", NONE)            val sizesDecl = CL.mkDecl(CL.T_Array(sizeTy, SOME(nAxes+1)), "sizes", NONE)
188          (* count number of elements (and stable strands) *)          (* count number of elements (and stable strands) *)
189            val countElems = let            val countElems = let
190                  val nElemsInit = CL.mkDeclInit(CL.uint32, "numElems", CL.mkInt 0)                  val nElemsInit = CL.mkDeclInit(CL.uint32, "numElems", CL.mkInt 0)
# Line 184  Line 213 
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)))) @
214                        [maybeAlloc (nLengthsV, Nrrd.tyToEnum Nrrd.TypeInt, nAxes+1)]                        [maybeAlloc (nLengthsV, Nrrd.tyToEnum Nrrd.TypeInt, nAxes+1)]
215                      else [                      else [
216                          dimSizes, setSizes(nDims, numStableV),                          dimSizes, setSizes(1, numStableV),
217                          maybeAlloc (nLengthsV, Nrrd.tyToEnum Nrrd.TypeInt, 2)                          maybeAlloc (nLengthsV, Nrrd.tyToEnum Nrrd.TypeInt, 2)
218                        ])                        ])
219                  end                  end
220          (* generate code to allocate the data nrrd *)          (* generate code to allocate the data nrrd *)
221            val dataNrrd = let            val dataNrrd = [
222                  val dimSizes = mapi (fn (i, d) => setSizes(i, mkInt d)) dims                    CL.mkComment["allocate nData nrrd"],
223                  val sizes = setSizes(nDims, numElemsV)                    setSizes(0, mkInt nElems),
224                  in                    setSizes(1, numElemsV),
225                    CL.mkComment["allocate nData nrrd"] ::                    maybeAlloc (nDataV, Nrrd.tyToEnum nrrdType, 2)
                   dimSizes @ [  
                       sizes,  
                       maybeAlloc (nDataV, Nrrd.tyToEnum nrrdType, nDims+1)  
226                      ]                      ]
                 end  
227          (* generate the nLengths copy code *)          (* generate the nLengths copy code *)
228            val copyLengths = let            val copyLengths = let
229                  val pInit = CL.mkDeclInit(CL.T_Ptr CL.uint32, "ip",                  val pInit = CL.mkDeclInit(CL.T_Ptr CL.uint32, "ip",
# Line 214  Line 239 
239                        then copyBlk                        then copyBlk
240                        else ifStable copyBlk                        else ifStable copyBlk
241                  in                  in
242                    [CL.mkComment["initialize nLengths nrrd"], pInit, offsetDecl, forStrands copyStm]                    CL.mkComment["initialize nLengths nrrd"] ::
243                      pInit ::
244                      offsetDecl ::
245                      forStrands copyStm ::
246                      initAxisKinds (nLengthsV, Nrrd.Kind2Vector, nAxes)
247                  end                  end
248          (* generate the nData copy code *)          (* generate the nData copy code *)
249            val copyData = let            val copyData = let
# Line 226  Line 255 
255                        then copyStm                        then copyStm
256                        else ifStable copyStm                        else ifStable copyStm
257                  in                  in
258                    [CL.mkComment["initialize nLengths nrrd"], pInit, forStrands copyStm]                    CL.mkComment["initialize nLengths nrrd"] ::
259                      pInit ::
260                      forStrands copyStm ::
261                      initAxisKinds (nDataV, axisKind, 1)
262                  end                  end
263          (* the function body *)          (* the function body *)
264            val stms =            val stms =
# Line 249  Line 281 
281     *    copy data from strands to nrrd     *    copy data from strands to nrrd
282     *)     *)
283      fun genFixedOutput (tgt, nAxes, ty, name) = let      fun genFixedOutput (tgt, nAxes, ty, name) = let
284            val (elemCTy, nrrdType, dims) = infoOf (tgt, ty)            val (elemCTy, nrrdType, axisKind, nElems) = infoOf (tgt, ty)
           val nDims = List.length dims  
           val nElems = List.foldl Int.* 1 dims  
285            val (isArray, nAxes) = (case nAxes of NONE => (false, 1) | SOME n => (true, n))            val (isArray, nAxes) = (case nAxes of NONE => (false, 1) | SOME n => (true, n))
286          (* generate the sizes initialization code *)          (* generate the sizes initialization code *)
287            val initSizes = let            val initSizes = let
288                  val sizesDcl = CL.mkDecl(CL.T_Array(sizeTy, SOME(nDims+nAxes)), "sizes", NONE)                  val dimSizes = [
289                  val dimSizes = sizesDcl :: mapi (fn (i, d) => setSizes(i, mkInt d)) dims                          CL.mkDecl(CL.T_Array(sizeTy, SOME(nAxes+1)), "sizes", NONE),
290                            setSizes(0, mkInt nElems)
291                          ]
292                  in                  in
293                    if isArray                    if isArray
294                      then dimSizes @                      then dimSizes @
295                        List.tabulate (nAxes, fn i =>                        List.tabulate (nAxes, fn i =>
296                          setSizes(nDims+i, CL.mkSubscript(CL.mkIndirect(wrldV, "size"), mkInt(nAxes-i-1))))                          setSizes(i+1, CL.mkSubscript(CL.mkIndirect(wrldV, "size"), mkInt(nAxes-i-1))))
297                      else                      else
298                        CL.mkDeclInit(sizeTy, "numStable", mkInt 0) ::                        CL.mkDeclInit(sizeTy, "numStable", mkInt 0) ::
299                        forStrands (ifStable(CL.S_Exp(CL.mkPostOp(numStableV, CL.^++)))) ::                        forStrands (ifStable(CL.S_Exp(CL.mkPostOp(numStableV, CL.^++)))) ::
300                        dimSizes @ [setSizes(nDims, numStableV)]                        dimSizes @ [setSizes(1, numStableV)]
301                  end                  end
302          (* generate the copy code *)          (* generate the copy code *)
303            val copyCode = let            val copyCode = let
# Line 284  Line 316 
316                        then copyBlk                        then copyBlk
317                        else ifStable copyBlk                        else ifStable copyBlk
318                  in                  in
319                    [pDecl, forStrands copyStm]                    pDecl :: forStrands copyStm :: initAxisKinds (nDataV, axisKind, nAxes)
320                  end                  end
321          (* the function body *)          (* the function body *)
322            val stms =            val stms =
323                  CL.mkComment["Compute sizes of nrrd file"] ::                  CL.mkComment["Compute sizes of nrrd file"] ::
324                  initSizes @                  initSizes @
325                  CL.mkComment["Allocate nData nrrd"] ::                  CL.mkComment["Allocate nData nrrd"] ::
326                  maybeAlloc (nDataV, Nrrd.tyToEnum  nrrdType, nDims+nAxes) ::                  maybeAlloc (nDataV, Nrrd.tyToEnum  nrrdType, nAxes+1) ::
327                  CL.mkComment["copy data to output nrrd"] ::                  CL.mkComment["copy data to output nrrd"] ::
328                  copyCode @                  copyCode @
329                  [CL.mkReturn(SOME(CL.mkVar "false"))]                  [CL.mkReturn(SOME(CL.mkVar "false"))]

Legend:
Removed from v.1791  
changed lines
  Added in v.1792

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