From 20b6b28f3e41367633dbd757480aa20efe70b9c6 Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Mon, 9 Apr 2012 00:11:23 +0100 Subject: [PATCH] Have PPDFunctionSet generate indices. --- src/ofc/codegen/Expression.scala | 5 + src/ofc/codegen/FortranGenerator.scala | 1 + src/ofc/codegen/Type.scala | 2 +- src/ofc/generators/Onetep.scala | 2 +- src/ofc/generators/onetep/Assignment.scala | 3 +- src/ofc/generators/onetep/Index.scala | 10 +- .../generators/onetep/IterationSpace.scala | 3 +- .../generators/onetep/PPDFunctionSet.scala | 183 +++--------------- src/ofc/generators/onetep/SPAM3.scala | 3 +- 9 files changed, 50 insertions(+), 162 deletions(-) diff --git a/src/ofc/codegen/Expression.scala b/src/ofc/codegen/Expression.scala index fd5b00a..474a4e2 100644 --- a/src/ofc/codegen/Expression.scala +++ b/src/ofc/codegen/Expression.scala @@ -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 +} diff --git a/src/ofc/codegen/FortranGenerator.scala b/src/ofc/codegen/FortranGenerator.scala index 4746442..3524a29 100644 --- a/src/ofc/codegen/FortranGenerator.scala +++ b/src/ofc/codegen/FortranGenerator.scala @@ -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) diff --git a/src/ofc/codegen/Type.scala b/src/ofc/codegen/Type.scala index 8f90c1e..fbcfb90 100644 --- a/src/ofc/codegen/Type.scala +++ b/src/ofc/codegen/Type.scala @@ -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 { diff --git a/src/ofc/generators/Onetep.scala b/src/ofc/generators/Onetep.scala index 3b9e6fd..61b5ba3 100644 --- a/src/ofc/generators/Onetep.scala +++ b/src/ofc/generators/Onetep.scala @@ -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) diff --git a/src/ofc/generators/onetep/Assignment.scala b/src/ofc/generators/onetep/Assignment.scala index 5846786..b45c879 100644 --- a/src/ofc/generators/onetep/Assignment.scala +++ b/src/ofc/generators/onetep/Assignment.scala @@ -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) } diff --git a/src/ofc/generators/onetep/Index.scala b/src/ofc/generators/onetep/Index.scala index d340f25..4811a71 100644 --- a/src/ofc/generators/onetep/Index.scala +++ b/src/ofc/generators/onetep/Index.scala @@ -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 - - diff --git a/src/ofc/generators/onetep/IterationSpace.scala b/src/ofc/generators/onetep/IterationSpace.scala index 3d7f16e..ff5de9c 100644 --- a/src/ofc/generators/onetep/IterationSpace.scala +++ b/src/ofc/generators/onetep/IterationSpace.scala @@ -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 diff --git a/src/ofc/generators/onetep/PPDFunctionSet.scala b/src/ofc/generators/onetep/PPDFunctionSet.scala index 94ee3f3..5469db5 100644 --- a/src/ofc/generators/onetep/PPDFunctionSet.scala +++ b/src/ofc/generators/onetep/PPDFunctionSet.scala @@ -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 +} diff --git a/src/ofc/generators/onetep/SPAM3.scala b/src/ofc/generators/onetep/SPAM3.scala index a8a2856..6320d6a 100644 --- a/src/ofc/generators/onetep/SPAM3.scala +++ b/src/ofc/generators/onetep/SPAM3.scala @@ -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+".") } -- 2.47.3