var nextIndexID = 0
val names = new HashMap[Index, String]()
- def addIndex(index: Index) {
+ def addIndex(index: Index) = {
val name = index.getName + "_" + nextIndexID
nextIndexID += 1
names += (index -> name)
+ name
}
- def apply(index: Index) = names(index)
+ def apply(index: Index) =
+ if (names.contains(index))
+ names(index)
+ else
+ addIndex(index)
}
class CodeGenerator {
name
}
+ def collectDeclarations(term: IterationSpace) : Set[String] = {
+ val declarations = for(index <- term.getSpatialIndices ++ term.getDiscreteIndices;
+ declaration <- index.getDeclarations(indexNames)) yield declaration
+
+ var declarationsSet = declarations.toSet
+ for (op <- term.getOperands) declarationsSet ++= collectDeclarations(op)
+ declarationsSet
+ }
+
def apply(assignment: Assignment) {
+ collectDeclarations(assignment)
generateCode(assignment)
}
code append "real(kind=DP), allocatable, dimension" + (":"*concreteIndexList.size).mkString("(",",",")") + " :: " +
storageName + "\n"
code append "allocate("+ storageName +
- (concreteIndexList map ((x : Index) => x.getDenseWidth)).mkString("(",",",")") + ", stat=ierr)"
+ (concreteIndexList map ((x : Index) => x.getDenseWidth)).mkString("(",",",")") + ", stat=ierr)\n"
// We've declared temporary storage, now create the loops to populate it
- //for (index <- concreteIndexList) code append index.generateIterationHeader(indexNames)
+ for (index <- concreteIndexList) code append index.generateIterationHeader(indexNames) + "\n"
println(code.mkString)
System.exit(0)
def getName : String
def getDependencies : Set[Index]
def getDenseWidth : String
- //def generateIterationHeader(names: IndexNames) : String
+ def generateIterationHeader(names: IndexNames) : String
+ def getDeclarations(names: IndexNames) : List[String]
}
trait SpatialIndex extends Index
def getDependencies = Set()
def getName = "dense_spatial_index"
def getDenseWidth = original.getDenseWidth
+ def generateIterationHeader(names: IndexNames) = "do "+names(this)+"=1,"+getDenseWidth
+ def getDeclarations(names: IndexNames) = List("integer :: "+names(this))
}
class DenseDiscreteIndex(parent: GeneralInnerProduct, original: DiscreteIndex) extends DiscreteIndex {
def getDependencies = Set()
def getName = "dense_discrete_index"
def getDenseWidth = original.getDenseWidth
+ def generateIterationHeader(names: IndexNames) = "do "+names(this)+"=1,"+getDenseWidth
+ def getDeclarations(names: IndexNames) = List("integer :: "+names(this))
}
val spatialIndices =
def getName = "reciprocal_index_" + dimension
def getDependencies = Set()
def getDenseWidth = original.getDenseWidth
+ def generateIterationHeader(names: IndexNames) = "do "+names(this)+"=1,"+getDenseWidth
+ def getDeclarations(names: IndexNames) = List("integer :: "+names(this))
}
val spatialIndices = for (dimension <- 0 until op.getSpatialIndices.size) yield
def getName = "restriction_index_" + dimension
def getDependencies = Set()
def getDenseWidth = throw new UnimplementedException("Restriction unimplemnted")
+
+ def generateIterationHeader(names: IndexNames) = throw new UnimplementedException("how the hell does this work?")
+ def getDeclarations(names: IndexNames) = Nil
}
val spatialIndices = for (dimension <- 0 until op.getSpatialIndices.size) yield new RestrictedIndex(this, dimension)
def getName = "row_index"
def getDependencies = Set()
def getDenseWidth = throw new UnimplementedException("Matrix size unimplemented")
+
+ def generateIterationHeader(names: IndexNames) = {
+ val indexName = names(this)
+ "do "+indexName+"=1,"+getDenseWidth
+ }
+
+ def getDeclarations(names: IndexNames) = List("integer :: "+names(this))
}
+
class ColIndex(parent: SPAM3) extends DiscreteIndex {
override def toString = parent + ".col"
def getName = "row_index"
def getDependencies = Set()
def getDenseWidth = throw new UnimplementedException("Matrix size unimplemented")
+
+ def generateIterationHeader(names: IndexNames) = {
+ val indexName = names(this)
+ "do "+indexName+"=1,"+getDenseWidth
+ }
+
+ def getDeclarations(names: IndexNames) = List("integer :: "+names(this))
}
val rowIndex = new RowIndex(this)
def getName = "sphere_index"
def getDependencies = Set()
def getDenseWidth = throw new UnimplementedException("Sphere count unimplemented")
- //TODO: def getSphere = parent.basis + "%spheres(????)"
+ def getSphere(names: IndexNames) = parent.basis + "%spheres("+names(this)+")"
+
+ def generateIterationHeader(names: IndexNames) = {
+ val indexName = names(this)
+ "do "+indexName+"=1,"+getDenseWidth
+ }
+
+ def getDeclarations(names: IndexNames) = List("integer :: "+names(this))
}
class PPDIndex(parent: PPDFunctionSet) extends DiscreteIndex {
def getDependencies = Set[Index](parent.getSphereIndex())
//TODO: def getDenseWidth = parent.getSphereIndex.getSphere + "%n_ppds_sphere"
def getDenseWidth = parent.basis+"%max_n_ppds_sphere"
+
+ def generateIterationHeader(names: IndexNames) =
+ "do "+names(this)+"=1,"+parent.getSphereIndex.getSphere(names)+"%n_ppds_sphere"
+
+ def getDeclarations(names: IndexNames) = List("integer :: "+names(this))
}
class IntraPPDIndex(parent: PPDFunctionSet, dimension: Int) extends SpatialIndex {
def getName = "intra_ppd_index_" + dimension
def getDependencies = Set[Index](parent.getPPDIndex)
def getDenseWidth = "pub_cell%total_pt"+(dimension+1)
+ def generateIterationHeader(names: IndexNames) = "do "+names(this)+"=1,"+"pub_cell%n_pt"+(dimension+1)
+ def getDeclarations(names: IndexNames) = List("integer :: "+names(this))
}
val ppdIndex = new PPDIndex(this)