From 510261c1ae745211081623e5527d5b29c5cf9b5c Mon Sep 17 00:00:00 2001 From: Francis Russell Date: Tue, 8 May 2012 09:56:09 +0100 Subject: [PATCH] Start work on assignment iteration. --- src/ofc/codegen/FortranGenerator.scala | 2 +- ...IteratedStatement.scala => IterationContext.scala} | 10 +++++----- src/ofc/generators/onetep/Field.scala | 2 +- src/ofc/generators/onetep/InnerProduct.scala | 6 ++++++ src/ofc/generators/onetep/Laplacian.scala | 3 +++ src/ofc/generators/onetep/OnetepTypes.scala | 4 ++++ src/ofc/generators/onetep/OnetepVariables.scala | 8 ++++++++ src/ofc/generators/onetep/Operand.scala | 11 +++++++++++ src/ofc/generators/onetep/PPDFunctionSet.scala | 7 +++++++ src/ofc/generators/onetep/SPAM3.scala | 7 +++++++ src/ofc/generators/onetep/Scalar.scala | 2 +- src/ofc/generators/onetep/ScalarLiteral.scala | 3 +++ src/ofc/generators/onetep/ScaledField.scala | 2 ++ 13 files changed, 59 insertions(+), 8 deletions(-) rename src/ofc/codegen/{IteratedStatement.scala => IterationContext.scala} (95%) create mode 100644 src/ofc/generators/onetep/OnetepVariables.scala create mode 100644 src/ofc/generators/onetep/Operand.scala diff --git a/src/ofc/codegen/FortranGenerator.scala b/src/ofc/codegen/FortranGenerator.scala index efccf6d..aa73dd1 100644 --- a/src/ofc/codegen/FortranGenerator.scala +++ b/src/ofc/codegen/FortranGenerator.scala @@ -139,7 +139,7 @@ class FortranGenerator { case (x : NullStatement) => () case (x : Comment) => addLine("!" + x.getValue) case (x : BlockStatement) => processScope(x) - case (x : IteratedStatement) => processStatement(x.toConcrete) + case (x : IterationContext) => processStatement(x.toConcrete) case (x : ForLoop) => processForLoop(x) case (a : AssignStatement) => processAssignment(a) case (i : IfStatement) => processIf(i) diff --git a/src/ofc/codegen/IteratedStatement.scala b/src/ofc/codegen/IterationContext.scala similarity index 95% rename from src/ofc/codegen/IteratedStatement.scala rename to src/ofc/codegen/IterationContext.scala index c50b3b1..c6876aa 100644 --- a/src/ofc/codegen/IteratedStatement.scala +++ b/src/ofc/codegen/IterationContext.scala @@ -2,7 +2,7 @@ package ofc.codegen import ofc.LogicError import ofc.util.DirectedGraph -object IteratedStatement { +object IterationContext { object Context { private def priority(context: Context) : Int = { // This ensures that the nesting ordering is Predicate, DerivedExpression, VariableRange @@ -80,8 +80,8 @@ object IteratedStatement { } } -class IteratedStatement extends Statement { - import IteratedStatement._ +class IterationContext extends Statement { + import IterationContext._ var statement = new Comment("Placeholder statement for consumer.") var ranges : Seq[VariableRange] = Nil @@ -104,8 +104,8 @@ class IteratedStatement extends Statement { predicates +:= new Predicate(condition) } - def merge(statement: IteratedStatement) : IteratedStatement = { - val result = new IteratedStatement + def merge(statement: IterationContext) : IterationContext = { + val result = new IterationContext result.ranges = ranges ++ statement.ranges result.predicates = predicates ++ statement.predicates result.expressions = expressions ++ statement.expressions diff --git a/src/ofc/generators/onetep/Field.scala b/src/ofc/generators/onetep/Field.scala index 1e8514c..86d16c7 100644 --- a/src/ofc/generators/onetep/Field.scala +++ b/src/ofc/generators/onetep/Field.scala @@ -1,6 +1,6 @@ package ofc.generators.onetep import ofc.codegen._ -trait Field { +trait Field extends Operand { def getFragment(indices: Map[NamedIndex, Expression[IntType]]) : FieldFragment } diff --git a/src/ofc/generators/onetep/InnerProduct.scala b/src/ofc/generators/onetep/InnerProduct.scala index 67e4134..b13220b 100644 --- a/src/ofc/generators/onetep/InnerProduct.scala +++ b/src/ofc/generators/onetep/InnerProduct.scala @@ -56,4 +56,10 @@ class InnerProduct(left: Field, right: Field) extends Scalar { def getFragment(indices: Map[NamedIndex, Expression[IntType]]) : ScalarFragment = new LocalFragment(left.getFragment(indices), right.getFragment(indices)) + + def getIterationInfo : IterationInfo = { + var leftInfo = left.getIterationInfo + var rightInfo = right.getIterationInfo + leftInfo merge rightInfo + } } diff --git a/src/ofc/generators/onetep/Laplacian.scala b/src/ofc/generators/onetep/Laplacian.scala index 46a295e..445337e 100644 --- a/src/ofc/generators/onetep/Laplacian.scala +++ b/src/ofc/generators/onetep/Laplacian.scala @@ -75,4 +75,7 @@ class Laplacian(op: Field) extends Field { def getFragment(indices: Map[NamedIndex, Expression[IntType]]) = new LocalFragment(this, indices) + + def getIterationInfo : IterationInfo = + op.getIterationInfo } diff --git a/src/ofc/generators/onetep/OnetepTypes.scala b/src/ofc/generators/onetep/OnetepTypes.scala index f2f1d7e..455db5c 100644 --- a/src/ofc/generators/onetep/OnetepTypes.scala +++ b/src/ofc/generators/onetep/OnetepTypes.scala @@ -64,4 +64,8 @@ object OnetepTypes { val totalPts = {for (dim <- 1 to 3) yield new FieldSymbol[IntType]("total_pt"+dim)}.toSeq def getFortranAttributes = Set("type(FFTBOX_INFO)") } + + object SPAM3 extends StructType { + def getFortranAttributes = Set("type(SPAM3)") + } } diff --git a/src/ofc/generators/onetep/OnetepVariables.scala b/src/ofc/generators/onetep/OnetepVariables.scala new file mode 100644 index 0000000..f08d80c --- /dev/null +++ b/src/ofc/generators/onetep/OnetepVariables.scala @@ -0,0 +1,8 @@ +package ofc.generators.onetep +import ofc.codegen._ + +object OnetepVariables { + // parallel_strategy + val pub_first_atom_on_node = new NamedUnboundVarSymbol[ArrayType[IntType]]("pub_first_atom_on_node", new ArrayType[IntType](1)) + val pub_num_atoms_on_node = new NamedUnboundVarSymbol[ArrayType[IntType]]("pub_num_atoms_on_node", new ArrayType[IntType](1)) +} diff --git a/src/ofc/generators/onetep/Operand.scala b/src/ofc/generators/onetep/Operand.scala new file mode 100644 index 0000000..4f0d428 --- /dev/null +++ b/src/ofc/generators/onetep/Operand.scala @@ -0,0 +1,11 @@ +package ofc.generators.onetep +import ofc.codegen._ + +class IterationInfo(val context: IterationContext, val indexMappings: Map[NamedIndex, Expression[IntType]]) { + def merge(other: IterationInfo) : IterationInfo = + new IterationInfo(context merge other.context, indexMappings ++ other.indexMappings) +} + +trait Operand { + def getIterationInfo : IterationInfo +} diff --git a/src/ofc/generators/onetep/PPDFunctionSet.scala b/src/ofc/generators/onetep/PPDFunctionSet.scala index d624003..24b68f3 100644 --- a/src/ofc/generators/onetep/PPDFunctionSet.scala +++ b/src/ofc/generators/onetep/PPDFunctionSet.scala @@ -174,4 +174,11 @@ class PPDFunctionSet(basisName: String, dataName: String, indices: Seq[NamedInde def getFragment(indices: Map[NamedIndex, Expression[IntType]]) : FieldFragment = new LocalFragment(this, indices) + + def getIterationInfo : IterationInfo = { + val context = new IterationContext + val numSpheres = basis % OnetepTypes.FunctionBasis.num + val sphereIndexExpr = context.addIteration("sphere_index", 1, numSpheres) + new IterationInfo(context, Map(getSphereIndex -> sphereIndexExpr)) + } } diff --git a/src/ofc/generators/onetep/SPAM3.scala b/src/ofc/generators/onetep/SPAM3.scala index d2d97e1..38be253 100644 --- a/src/ofc/generators/onetep/SPAM3.scala +++ b/src/ofc/generators/onetep/SPAM3.scala @@ -2,6 +2,8 @@ package ofc.generators.onetep import ofc.codegen._ class SPAM3(name : String, indices: Seq[NamedIndex]) extends Scalar { + val mat = new NamedUnboundVarSymbol[StructType](name, OnetepTypes.SPAM3) + class LocalFragment extends ScalarFragment { def setup(context: GenerationContext) { } @@ -14,4 +16,9 @@ class SPAM3(name : String, indices: Seq[NamedIndex]) extends Scalar { def getFragment(indices: Map[NamedIndex, Expression[IntType]]) : ScalarFragment = new LocalFragment + + def getIterationInfo : IterationInfo = { + val context = new IterationContext + throw new ofc.UnimplementedException("not yet implemented.") + } } diff --git a/src/ofc/generators/onetep/Scalar.scala b/src/ofc/generators/onetep/Scalar.scala index 89a0f51..e151dad 100644 --- a/src/ofc/generators/onetep/Scalar.scala +++ b/src/ofc/generators/onetep/Scalar.scala @@ -1,6 +1,6 @@ package ofc.generators.onetep import ofc.codegen._ -trait Scalar { +trait Scalar extends Operand { def getFragment(indices: Map[NamedIndex, Expression[IntType]]) : ScalarFragment } diff --git a/src/ofc/generators/onetep/ScalarLiteral.scala b/src/ofc/generators/onetep/ScalarLiteral.scala index e3ed04b..8c0704c 100644 --- a/src/ofc/generators/onetep/ScalarLiteral.scala +++ b/src/ofc/generators/onetep/ScalarLiteral.scala @@ -15,4 +15,7 @@ class ScalarLiteral(s: Double) extends Scalar { def getFragment(indices: Map[NamedIndex, Expression[IntType]]) : ScalarFragment = new LocalFragment(s) + + def getIterationInfo : IterationInfo = + new IterationInfo(new IterationContext, Map.empty) } diff --git a/src/ofc/generators/onetep/ScaledField.scala b/src/ofc/generators/onetep/ScaledField.scala index d488327..1e44f05 100644 --- a/src/ofc/generators/onetep/ScaledField.scala +++ b/src/ofc/generators/onetep/ScaledField.scala @@ -62,4 +62,6 @@ class ScaledField(op: Field, factor: Scalar) extends Field { def getFragment(indices: Map[NamedIndex, Expression[IntType]]) = new LocalFragment(this, indices) + def getIterationInfo : IterationInfo = + op.getIterationInfo merge factor.getIterationInfo } -- 2.47.3