package ofc.generators.onetep
-import scala.collection.mutable.{HashMap,HashSet,Set}
import ofc.parser
import ofc.parser.Identifier
import ofc.{InvalidInputException,UnimplementedException}
-trait Index
+trait Index {
+ def getName : String
+ def getDependencies : Set[Index]
+}
trait SpatialIndex extends Index
trait DiscreteIndex extends Index
trait IterationSpace {
+ def getAccessExpression(indexNames: IndexNames) : String
+ def getOperands() : List[IterationSpace]
def getSpatialIndices() : List[SpatialIndex]
def getDiscreteIndices() : List[DiscreteIndex]
}
trait DataSpace extends IterationSpace {
+ def getOperands() = Nil
}
trait Matrix extends DataSpace
trait FunctionSet extends DataSpace
+class Assignment(indexBindings: IndexBindings, lhs: DataSpace, rhs: IterationSpace) extends IterationSpace {
+ override def toString = indexBindings.toString
+ def getIndexBindings = indexBindings
+ def getOperands = List(lhs,rhs)
+ def getSpatialIndices = Nil
+ def getDiscreteIndices = Nil
+ def getAccessExpression(indexNames: IndexNames) = throw new UnimplementedException("Access failed")
+}
+
class Scalar(value: Double) extends IterationSpace {
+ def getOperands() = Nil
def getSpatialIndices() = Nil
def getDiscreteIndices() = Nil
+ def getAccessExpression(indexNames: IndexNames) = throw new UnimplementedException("Access failed")
}
-class GeneralInnerProduct(operands: List[IterationSpace], removedIndices: List[Index]) extends IterationSpace {
- def getSpatialIndices() = operands flatMap (op => op.getSpatialIndices filterNot (index => removedIndices.contains(index)))
- def getDiscreteIndices() = operands flatMap (op => op.getDiscreteIndices filterNot (index => removedIndices.contains(index)))
+class GeneralInnerProduct(operands: List[IterationSpace], removedIndices: Set[Index]) extends IterationSpace {
+
+ class DenseSpatialIndex(parent: GeneralInnerProduct) extends SpatialIndex{
+ def getDependencies = Set()
+ def getName = "dense_spatial_index"
+ }
+
+ class DenseDiscreteIndex(parent: GeneralInnerProduct) extends DiscreteIndex {
+ def getDependencies = Set()
+ def getName = "dense_discrete_index"
+ }
+
+ val spatialIndices =
+ for(op <- operands; index <- op.getSpatialIndices; if (!removedIndices.contains(index))) yield
+ if (index.getDependencies.intersect(removedIndices).isEmpty)
+ index
+ else
+ new DenseSpatialIndex(this)
+
+ val discreteIndices =
+ for(op <- operands; index <- op.getDiscreteIndices; if (!removedIndices.contains(index))) yield
+ if (index.getDependencies.intersect(removedIndices).isEmpty)
+ index
+ else
+ new DenseDiscreteIndex(this)
+
+ def getOperands = operands
+ def getSpatialIndices() = spatialIndices
+ def getDiscreteIndices() = discreteIndices
+ def getAccessExpression(indexNames: IndexNames) = throw new UnimplementedException("Access failed")
}
class Reciprocal(op: IterationSpace) extends IterationSpace {
- class BlockIndex(parent: Reciprocal, dimension: Int) extends SpatialIndex
+ class BlockIndex(parent: Reciprocal, dimension: Int) extends SpatialIndex {
+ def getName = "reciprocal_index_" + dimension
+ def getDependencies = Set()
+ }
val spatialIndices = for (dimension <- 0 to op.getSpatialIndices.size) yield new BlockIndex(this, dimension)
+ def getOperands = List(op)
def getSpatialIndices() = spatialIndices.toList
def getDiscreteIndices() = op.getDiscreteIndices
+ def getAccessExpression(indexNames: IndexNames) = throw new UnimplementedException("Access failed")
}
class Laplacian(op: IterationSpace) extends IterationSpace {
+ def getOperands() = List(op)
def getSpatialIndices() = op.getSpatialIndices
def getDiscreteIndices() = op.getDiscreteIndices
+ def getAccessExpression(indexNames: IndexNames) = throw new UnimplementedException("Access failed")
}
class SpatialRestriction(op: IterationSpace) extends IterationSpace {
- class RestrictedIndex(parent: SpatialRestriction, dimension: Int) extends SpatialIndex
+ class RestrictedIndex(parent: SpatialRestriction, dimension: Int) extends SpatialIndex {
+ def getName = "restriction_index_" + dimension
+ def getDependencies = Set()
+ }
+
val spatialIndices = for (dimension <- 0 to op.getSpatialIndices.size) yield new RestrictedIndex(this, dimension)
+ def getOperands() = List(op)
def getSpatialIndices() = spatialIndices.toList
def getDiscreteIndices() = op.getDiscreteIndices
+ def getAccessExpression(indexNames: IndexNames) = throw new UnimplementedException("Access failed")
}
class SPAM3(name : String) extends Matrix {
class RowIndex(parent: SPAM3) extends DiscreteIndex {
override def toString = parent + ".row"
+ def getName = "row_index"
+ def getDependencies = Set()
}
class ColIndex(parent: SPAM3) extends DiscreteIndex {
override def toString = parent + ".col"
+ def getName = "row_index"
+ def getDependencies = Set()
}
val rowIndex = new RowIndex(this)
def getSpatialIndices() = Nil
def getDiscreteIndices() = List(rowIndex, colIndex)
+ def getAccessExpression(indexNames: IndexNames) = throw new UnimplementedException("Access failed")
}
class PPDFunctionSet(basis : String, data : String) extends FunctionSet {
- class SphereIndex(parent: PPDFunctionSet) extends DiscreteIndex
- class PPDIndex(parent: PPDFunctionSet) extends DiscreteIndex
- class IntraPPDIndex(parent: PPDFunctionSet, dimension: Int) extends SpatialIndex
+ class SphereIndex(parent: PPDFunctionSet) extends DiscreteIndex {
+ def getName = "sphere_index"
+ def getDependencies = Set()
+ }
+
+ class PPDIndex(parent: PPDFunctionSet) extends DiscreteIndex {
+ def getName = "ppd_index"
+ def getDependencies = Set[Index](parent.getSphereIndex())
+ }
+
+ class IntraPPDIndex(parent: PPDFunctionSet, dimension: Int) extends SpatialIndex {
+ def getName = "intra_ppd_index_" + dimension
+ def getDependencies = Set[Index](parent.getPPDIndex)
+ }
val ppdIndex = new PPDIndex(this)
val sphereIndex = new SphereIndex(this)
def getSpatialIndices() = spatialIndices.toList
def getDiscreteIndices() = List(getSphereIndex(), getPPDIndex())
+ def getAccessExpression(indexNames: IndexNames) = throw new UnimplementedException("Access failed")
}
class BindingIndex(name : String) {
}
class Dictionary {
+ import scala.collection.mutable.HashMap
+
var matrices = new HashMap[Identifier, Matrix]
var functionSets = new HashMap[Identifier, FunctionSet]
var indices = new HashMap[Identifier, BindingIndex]
}
}
-class Assignment(indexBindings: IndexBindings, lhs: DataSpace, rhs: IterationSpace) {
- override def toString = indexBindings.toString
-}
-
class IndexBindings {
+ import scala.collection.mutable.{Set,HashSet, HashMap}
+
val spatial = new HashMap[BindingIndex, Set[SpatialIndex]]
- val discrete = new HashMap[BindingIndex, Set[DiscreteIndex]]
+ val discrete = new HashMap[BindingIndex,Set[DiscreteIndex]]
def add(binding: BindingIndex, index: SpatialIndex) = spatial.getOrElseUpdate(binding, new HashSet()) += index
def add(binding: BindingIndex, index: DiscreteIndex) = discrete.getOrElseUpdate(binding, new HashSet()) += index
lhsTree match {
case (lhsTree: DataSpace) => new Assignment(indexBindings, lhsTree, rhsTree)
- case _ => new InvalidInputException("Non-assignable expression on LHS of assignment.")
+ case _ => throw new InvalidInputException("Non-assignable expression on LHS of assignment.")
}
}
def buildIndexedTerm(term: parser.IndexedTerm) : IterationSpace = {
val dataSpace = dictionary.getData(term.id) match {
- case (functionSet : PPDFunctionSet) => new GeneralInnerProduct(List(functionSet), List(functionSet.getPPDIndex))
+ case (functionSet : PPDFunctionSet) => new GeneralInnerProduct(List(functionSet), Set(functionSet.getPPDIndex))
case v => v
}
case (t: IndexedTerm) => buildIndexedTerm(t)
case ScalarConstant(s) => new Scalar(s)
case Multiplication(a, b) =>
- new GeneralInnerProduct(List(buildExpression(a), buildExpression(b)), Nil)
+ new GeneralInnerProduct(List(buildExpression(a), buildExpression(b)), Set())
case Division(a, b) =>
throw new UnimplementedException("Semantics of division not yet defined, or implemented.")
case Operator(Identifier("inner"), List(a,b)) => {
indexBindings.add(bindingIndex, right)
}
- new GeneralInnerProduct(List(aExpression, bExpression), aExpression.getSpatialIndices ++ bExpression.getSpatialIndices)
+ new GeneralInnerProduct(List(aExpression, bExpression), (aExpression.getSpatialIndices ++ bExpression.getSpatialIndices).toSet)
}
case Operator(Identifier("reciprocal"), List(op)) => new Reciprocal(buildExpression(op))
case Operator(Identifier("laplacian"), List(op)) => new Laplacian(buildExpression(op))