]> git.unchartedbackwaters.co.uk Git - francis/ofc.git/commitdiff
Have PPDFunctionSet generate indices.
authorFrancis Russell <francis@unchartedbackwaters.co.uk>
Sun, 8 Apr 2012 23:11:23 +0000 (00:11 +0100)
committerFrancis Russell <francis@unchartedbackwaters.co.uk>
Sun, 8 Apr 2012 23:11:23 +0000 (00:11 +0100)
src/ofc/codegen/Expression.scala
src/ofc/codegen/FortranGenerator.scala
src/ofc/codegen/Type.scala
src/ofc/generators/Onetep.scala
src/ofc/generators/onetep/Assignment.scala
src/ofc/generators/onetep/Index.scala
src/ofc/generators/onetep/IterationSpace.scala
src/ofc/generators/onetep/PPDFunctionSet.scala
src/ofc/generators/onetep/SPAM3.scala

index fd5b00a570d2199cf70514d99ef8c72ba576f5cc..474a4e2bc2ae15779f76fa5e03e0cd0fda261c9f 100644 (file)
@@ -104,3 +104,8 @@ class IntegerLiteral(value: Int) extends Expression[IntType] with LeafExpression
   def getValue = value
   def getType = new IntType
 }
+
+class FloatLiteral(value: Double) extends Expression[FloatType] with LeafExpression {
+  def getValue = value
+  def getType = new FloatType
+}
index 474644218e9821ee3858df7ee015a72a76e031db..3524a2990a4417dce0535aa3a289fd159392a68f 100644 (file)
@@ -158,6 +158,7 @@ class FortranGenerator {
   private def buildExpression(expression: Expression[_]) : ExpHolder = {
     expression match {
       case (i : IntegerLiteral) => ExpHolder(maxPrec, i.getValue.toString)
+      case (i : FloatLiteral) => ExpHolder(maxPrec, i.getValue.toString)
       case (a : FieldAccess[_]) => ExpHolder(maxPrec, "%s%%%s".format(buildExpression(a.getStructExpression), a.getField.getName))
       case (r : VarRef[_]) => r.getSymbol match {
         case (s: NamedUnboundVarSymbol[_]) => ExpHolder(maxPrec, s.getName)
index 8f90c1ed5a2d9c8021b7f2b655952615ba92f650..fbcfb900e103da684dde36b33fd3d1e7075751a7 100644 (file)
@@ -11,7 +11,7 @@ final case class IntType() extends PrimitiveType {
 }
 
 final case class FloatType() extends PrimitiveType {
-  def getFortranAttributes = Set("real(kind=DP")
+  def getFortranAttributes = Set("real(kind=DP)")
 }
 
 final case class BoolType() extends PrimitiveType {
index 3b9e6fde77fcf26c2cc5dc9ab12c0f7b5b6d2d21..61b5ba36016e3cee6998b92aea7b1912b97e3a61 100644 (file)
@@ -49,7 +49,7 @@ class Onetep extends Generator {
     call match {
       case Some(FunctionCall(fSetType, params)) => (fSetType, params) match {
         case (Identifier("PPDFunctionSet"), ParameterList(StringParameter(basis), StringParameter(data))) => 
-          dictionary.functionSets += id -> new PPDFunctionSet(basis, data)
+          dictionary.functionSets += id -> PPDFunctionSet(basis, data)
         case _ => throw new InvalidInputException("Unknown usage of type: "+fSetType.name)
       }
       case _ => throw new InvalidInputException("Undefined concrete type for function set: "+id.name)
index 584678610e303e37a14661f66093b82ff43bda53..b45c879ae4f926075989ff8a9fc0ae094daa8f04 100644 (file)
@@ -1,5 +1,5 @@
 package ofc.generators.onetep
-import ofc.codegen.NullStatement
+import ofc.codegen.{NullStatement,FloatLiteral}
 
 class Assignment(indexBindings: IndexBindings, lhs: DataSpace, rhs: IterationSpace) extends IterationSpace {
   def getOperands = List(lhs, rhs)
@@ -7,4 +7,5 @@ class Assignment(indexBindings: IndexBindings, lhs: DataSpace, rhs: IterationSpa
   def getDiscreteIndices = Nil
   def getReaderFragment = new NullStatement
   def getSuffixFragment = new NullStatement
+  def getDataValue = new FloatLiteral(0.0)
 }
index d340f256d85cacf08d1f4de03d32d8355b4d0da2..4811a71a3e17dbe81e6e5b108e0400b795d0c2a5 100644 (file)
@@ -2,13 +2,11 @@ package ofc.generators.onetep
 import ofc.codegen.{Expression,IntType}
 
 trait Index {
-  def getName : String
-  def getMinimumValue : Expression[IntType]
-  def getLength : Expression[IntType]
-  def isRandomAccess : Boolean
+  def getName: String
+  def getValue: Expression[IntType] 
+  //def getMinimumValue : Expression[IntType]
+  //def getLength : Expression[IntType]
 }
 
 trait SpatialIndex extends Index
 trait DiscreteIndex extends Index
-
-
index 3d7f16ed5c5a3ebe668a6f2ef154d5f3b145a19e..ff5de9cdeb89805f1950cc43ac98c8e8b9a4d016 100644 (file)
@@ -1,5 +1,5 @@
 package ofc.generators.onetep
-import ofc.codegen.{Statement,NullStatement}
+import ofc.codegen.{Statement,NullStatement,Expression,FloatType}
 
 object IterationSpace {
   def sort(spaces : Traversable[IterationSpace]) : Seq[IterationSpace] = {
@@ -21,6 +21,7 @@ trait IterationSpace {
   def getOperands : Seq[IterationSpace]
   def getSpatialIndices : Seq[SpatialIndex]
   def getDiscreteIndices : Seq[DiscreteIndex]
+  def getDataValue : Expression[FloatType]
   def getIndices : Set[Index] = (getSpatialIndices ++ getDiscreteIndices).toSet
   def getDependencies : Set[IterationSpace] = {
     val operands = getOperands
index 94ee3f39e8e047fa84cf33c720de6cf04660daed..5469db5f060e5502390e10fb2cf72279e1ee813a 100644 (file)
@@ -1,18 +1,29 @@
 package ofc.generators.onetep
 import ofc.codegen._
 
-class PPDFunctionSet(val basisName: String, dataName: String) extends FunctionSet {
-  import OnetepTypes._
+object PPDFunctionSet {
+  private val pubCell = new NamedUnboundVarSymbol[StructType]("pub_cell", OnetepTypes.CellInfo)
 
-  val basis = new NamedUnboundVarSymbol[StructType](basisName, FunctionBasis)
-  val data =  new NamedUnboundVarSymbol[ArrayType[FloatType]](dataName, new ArrayType[FloatType](1))
-  val pubCell = new NamedUnboundVarSymbol[StructType]("pub_cell", OnetepTypes.CellInfo)
+  private class SphereIndex(name: String, value: Expression[IntType]) extends DiscreteIndex {
+    def getName = name
+    def getValue = value
+  }
+
+  private class PositionIndex(name: String, value: Expression[IntType]) extends SpatialIndex {
+    def getName = name
+    def getValue = value
+  }
+
+  def apply(basisName: String, dataName: String) : PPDFunctionSet = {
+    import OnetepTypes._
+
+    val basis = new NamedUnboundVarSymbol[StructType](basisName, FunctionBasis)
+    val data =  new NamedUnboundVarSymbol[ArrayType[FloatType]](dataName, new ArrayType[FloatType](1))
 
-  val numSpheres = basis % FunctionBasis.num
-  val ppdWidths = for(dim <- 0 to 2) yield pubCell % CellInfo.ppdWidth(dim)
-  val cellWidthInPPDs = for(dim <- 0 to 2) yield pubCell % CellInfo.numPPDs(dim)
+    val numSpheres = basis % FunctionBasis.num
+    val ppdWidths = for(dim <- 0 to 2) yield pubCell % CellInfo.ppdWidth(dim)
+    val cellWidthInPPDs = for(dim <- 0 to 2) yield pubCell % CellInfo.numPPDs(dim)
 
-  def getSuffixFragment = {
     val producer = new ProducerStatement
     val sphereIndex = producer.addIteration("sphere_index", 1, numSpheres)
     val numPPDs = (~(basis % FunctionBasis.numPPDsInSphere)).readAt(sphereIndex)
@@ -62,150 +73,20 @@ class PPDFunctionSet(val basisName: String, dataName: String) extends FunctionSe
       + (ppdIndices(0)-1))
 
     val dataValue = producer.addExpression("data", data.readAt(ppdDataIndex))
-    
-    producer
-  }
-
-  def getDiscreteIndices = List.empty
-  def getSpatialIndices = List.empty
-/*
-  class SphereIndex(parent: PPDFunctionSet) extends DiscreteIndex {
-    def getName = "sphere_index"
-    def getDependencies = Set()
-    def getDenseWidth(names: NameManager) = parent.getNumSpheres(names)
-
-    def generateIterationHeader(names: NameManager) = {
-      val indexName = names(this)
-      "do "+indexName+"=1," + parent.getSphere(names) + "%n_ppds_sphere"
-    }
-
-    def generateIterationFooter(names: NameManager) = "end do"
-    def getDeclarations(names: NameManager) = List("integer :: "+names(this))
-  }
-  
-  class PPDIndex(parent: PPDFunctionSet) extends DiscreteIndex {
-    var denseIndexNames : List[String] = Nil
-    var startNames : List[String] = Nil
-    var finishNames : List[String] = Nil
-    var offsetNames : List[String] = Nil
-    var ppdPoint : String = ""
-
-    def getName = "ppd_index"
-    def getPPDPoint = ppdPoint
-    def getStartNames = startNames
-    def getFinishNames = finishNames
-    def getOffsetNames = offsetNames
-    def getDependencies = Set[Index](parent.getSphereIndex)
-    def getDensePPDIndices = denseIndexNames
-
-    def getDenseWidth(names: NameManager) = parent.basis+"%max_n_ppds_sphere"
-
-    def generateIterationHeader(names: NameManager) = {
-      val findPPD = "call basis_find_ppd_in_neighbour(" + denseIndexNames.mkString(",") + ", &\n" +
-        parent.getSphere(names) + "%ppd_list(1," + names(this) + "), &\n" +
-        parent.getSphere(names) + "%ppd_list(2," + names(this) + "), &\n" +
-        "pub_cell%n_ppds_a1, pub_cell%n_ppds_a2, pub_cell%n_ppds_a3)"
-
-      val computeRanges = for (dim <- 1 to 2) yield {
-        val tb = parent.getTightbox(names)
-
-        "call basis_lims_1d_in_ppd_in_tight("+startNames(dim)+", &\n"+finishNames(dim)+", &\n"+offsetNames(dim)+", &\n" +
-        denseIndexNames(dim)+", &\n"+tb+"%start_ppds2, &\n"+tb+"%finish_ppds2, &\n" +
-        tb+"%start_pts2, &\n"+tb+"%finish_pts2, pub_cell%n_pt"+dim+")"
-      }
-
-      val loopDeclaration = "do "+names(this)+"=1,"+parent.getSphere(names)+"%n_ppds_sphere"
-
-      val ppdOffsetCalc = ppdPoint + " = " + parent.getSphere(names)+"%offset + ("+names(this)+"-1)*pub_cell%n_pts &\n"+
-      "+ ("+startNames(2)+"-1)*pub_cell%n_pt2*pub_cell%n_pt1 &\n"+
-      "+ ("+startNames(1)+"-1)*pub_cell%n_pt1 + ("+startNames(0)+"-1)"
-
-      (List(findPPD) ++ computeRanges ++ List(loopDeclaration, ppdOffsetCalc)).mkString("\n")
-    }
-
-    def generateIterationFooter(names: NameManager) = "end do"
-
-    def getDeclarations(names: NameManager) = {
-      denseIndexNames = (for (dim <- 0 to 2) yield names.newIdentifier("derived_ppd_position_"+dim)).toList
-      startNames = (for (dim <- 0 to 2) yield names.newIdentifier("tightbox_start_"+dim)).toList
-      finishNames = (for (dim <- 0 to 2) yield names.newIdentifier("tightbox_finish_"+dim)).toList
-      offsetNames = (for (dim <- 0 to 2) yield names.newIdentifier("tightbox_offset_"+dim)).toList
-      ppdPoint = names.newIdentifier("fa_point")
-
-      val allDeclarations = denseIndexNames++startNames++finishNames++offsetNames++Some(ppdPoint)
-      allDeclarations.map(x => "integer :: " + x) ++ List("integer :: "+names(this))
-    }
-  }
-
-  class IntraPPDIndex(parent: PPDFunctionSet, dimension: Int) extends SpatialIndex {
-    var tbPoint : String = ""
-
-    def getName = "intra_ppd_index_" + dimension
-    private def getPPDIndex = parent.getPPDIndex
-    def getDependencies = {
-      val parentIndex = if (dimension < parent.getSpatialIndices.size-1) 
-        Some(parent.getSpatialIndices(dimension+1)) 
-      else 
-        None
-
-      Set[Index](getPPDIndex) ++ parentIndex
-    }
-    def getDenseWidth(names: NameManager) = "pub_cell%total_pt"+(dimension+1)
-    def generateIterationHeader(names: NameManager) = {
-      val findPoint = tbPoint + " = " + 
-        names(this) + " - " + getPPDIndex.getStartNames(dimension) + " + " +
-        getPPDIndex.getOffsetNames(dimension) + " + 1"
-
-      val header = "do "+names(this)+"="+getPPDIndex.getStartNames(dimension)+","+getPPDIndex.getFinishNames(dimension)
-
-      List(findPoint, header).mkString("\n")
-    }
-
-    def generateIterationFooter(names: NameManager) = {
-      val incPoint = if (dimension == 0) Some(getPPDIndex.ppdPoint + " = " + getPPDIndex.ppdPoint + " + 1") else None
-      val jumpOffset = "pub_cell%n_pt"+(dimension+1)+"-" + 
-                       getPPDIndex.getFinishNames(dimension)+"+"+
-                       getPPDIndex.getStartNames(dimension)+"-1"
-      val jumpPoint = (List(getPPDIndex.ppdPoint + " = " + getPPDIndex.ppdPoint + " + (" + jumpOffset +")") ++
-                      (for (dim <- 0 until dimension) yield "pub_cell%n_pt"+(dim+1))).mkString("*")
-      val endLoop = "end do"
-
-      (incPoint ++ List(endLoop, jumpPoint)).mkString("\n")
-    }
-
-    def getDeclarations(names: NameManager) = {
-      tbPoint = names.newIdentifier("tb_pt"+(dimension+1))
-      List("integer :: "+names(this), "integer :: "+tbPoint)
-    }
-
-    override def getDensePosition(names: NameManager) = 
-      tbPoint
-  }
-
-  val ppdIndex = new PPDIndex(this)
-  val sphereIndex = new SphereIndex(this)
-  val spatialIndices = for (dimension <- 0 to 2) yield new IntraPPDIndex(this, dimension)
-
-  def getPPDIndex = ppdIndex
-  def getSphereIndex = sphereIndex
-  def getSphere(names: NameManager) = basis + "%spheres("+names(getSphereIndex)+")"
-  def getTightbox(names: NameManager) = basis + "%tight_boxes("+names(getSphereIndex)+")"
 
-  def getNumSpheres(names: NameManager) = {
-    // TODO: This number is dependent on the parallel distribution
-    basis + "%node_num"
+    val discreteIndices = List[DiscreteIndex](new SphereIndex("sphere", sphereIndex))
+    val spatialIndices = {for ((name, index) <- List[String]("x", "y", "z") zip positions) yield new PositionIndex(name, index)}
+    
+    new PPDFunctionSet(discreteIndices, spatialIndices, dataValue, producer)
   }
-
-  def getSpatialIndices = spatialIndices.toList
-  def getDiscreteIndices = List(getSphereIndex)
-  def getExternalIndices = Set(getPPDIndex)
-
-  def getProducerGenerator = Some(new ProducerGenerator {
-    def generate(names: NameManager) = {
-      data+"("+getPPDIndex.getPPDPoint+")"
-    }
-  })
-*/
 }
 
+class PPDFunctionSet private(discreteIndices: Seq[DiscreteIndex], 
+  spatialIndices: Seq[SpatialIndex], data: Expression[FloatType], 
+  producer: Statement) extends FunctionSet {
 
+  def getSuffixFragment = producer
+  def getDiscreteIndices = discreteIndices
+  def getSpatialIndices = spatialIndices
+  def getDataValue = data
+}
index a8a2856515972fda884af6fc2843f945c8f3ce4d..6320d6a8b8bd1bfa58e4a45a49ec3de69d3ffd9c 100644 (file)
@@ -1,5 +1,5 @@
 package ofc.generators.onetep
-import ofc.codegen.{NullStatement,Comment}
+import ofc.codegen.{NullStatement,Comment, FloatLiteral}
 
 class SPAM3(name : String) extends Matrix {
   override def toString = name
@@ -7,5 +7,6 @@ class SPAM3(name : String) extends Matrix {
 
   def getSpatialIndices = Nil
   def getDiscreteIndices = Nil
+  def getDataValue = new FloatLiteral(0.0)
   def getSuffixFragment = new Comment("Suffix of "+toString+".")
 }