Skip to content

Commit

Permalink
Use Scalaxy/loops to optimize Range integer macros
Browse files Browse the repository at this point in the history
Removal of the type annotations is because of
nativelibs4java/Scalaxy#29
  • Loading branch information
huitseeker committed Nov 10, 2015
1 parent a44d941 commit 0fde2a8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
2 changes: 2 additions & 0 deletions SpectralLDA-Tensor/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ libraryDependencies ++= Seq(
"org.scalanlp" %% "breeze-viz" % "0.11.2"
)

libraryDependencies += "com.nativelibs4java" %% "scalaxy-loops" % "0.3.4"

libraryDependencies += "org.apache.spark" %% "spark-streaming" % "1.5.1"

libraryDependencies += "org.apache.commons" % "commons-math3" % "3.0"
Expand Down
12 changes: 7 additions & 5 deletions SpectralLDA-Tensor/src/main/scala/Algorithm/ALS.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.util.MLUtils
import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}
import scalaxy.loops._
import scala.language.postfixOps

import scala.collection.mutable
import scala.util.control.Breaks._
Expand Down Expand Up @@ -55,7 +57,7 @@ class ALS(slices: Int, dimK: Int, myData: DataCumulant) extends Serializable{
// println("Mode A...")
val A_array: Array[DenseVector[Double]] = T_RDD.map(thisT => updateALSiteration(dimK, A, C, B, thisT)).collect()
// A_array = pseudoRDD.map(i => updateALSiteration(dimK, A, C, B, T(i, ::).t)).collect()
for (idx: Int <- 0 until dimK) {
for (idx <- 0 until dimK optimized) {
A(idx, ::) := A_array(idx).t
}
lambda = AlgebraUtil.colWiseNorm2(A)
Expand All @@ -66,7 +68,7 @@ class ALS(slices: Int, dimK: Int, myData: DataCumulant) extends Serializable{
// println("Mode B...")
val B_array: Array[DenseVector[Double]] = T_RDD.map(thisT => updateALSiteration(dimK, B, A, C,thisT)).collect()
// B_array = pseudoRDD.map(i => updateALSiteration(dimK, B, A, C, T(i, ::).t)).collect()
for (idx: Int <- 0 until dimK) {
for (idx <- 0 until dimK optimized) {
B(idx, ::) := B_array(idx).t
}
B = AlgebraUtil.matrixNormalization(B)
Expand All @@ -75,7 +77,7 @@ class ALS(slices: Int, dimK: Int, myData: DataCumulant) extends Serializable{
// println("Mode C...")
val C_array: Array[DenseVector[Double]] = T_RDD.map(thisT => updateALSiteration(dimK, C, B, A,thisT)).collect()
// C_array = pseudoRDD.map(i => updateALSiteration(dimK, C, B, A, T(i, ::).t)).collect()
for (idx: Int <- 0 until dimK) {
for (idx <- 0 until dimK optimized) {
C(idx, ::) := C_array(idx).t
}
C = AlgebraUtil.matrixNormalization(C)
Expand Down Expand Up @@ -114,7 +116,7 @@ class ALS(slices: Int, dimK: Int, myData: DataCumulant) extends Serializable{

private def simplexProj_Matrix(M :DenseMatrix[Double]): DenseMatrix[Double] ={
val M_onSimplex: DenseMatrix[Double] = DenseMatrix.zeros[Double](M.rows, M.cols)
for(i: Int<- 0 until M.cols){
for(i <- 0 until M.cols optimized){
val thisColumn = M(::,i)

val tmp1 = simplexProj(thisColumn)
Expand Down Expand Up @@ -142,7 +144,7 @@ class ALS(slices: Int, dimK: Int, myData: DataCumulant) extends Serializable{
var maxIndex : Int = 0
// find maxIndex
breakable{
for (i: Int<- 0 until len){
for (i <- 0 until len optimized){
if (TobefindMax(len - i - 1) > 0){
maxIndex = len - i - 1
break()
Expand Down
18 changes: 12 additions & 6 deletions SpectralLDA-Tensor/src/main/scala/DataMoments/DataCumulant.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class DataCumulant(sc: SparkContext, slices: Int, dimK: Int, alpha0: Double, tol
println("Finished calculating first order moments.")

val (thirdOrderMoments: DenseMatrix[Double], unwhiteningMatrix: DenseMatrix[Double]) = {
import scalaxy.loops._
import scala.language.postfixOps


println("Start calculating second order moments...")
val (eigenVectors: DenseMatrix[Double], eigenValues: DenseVector[Double]) = whiten(sc, alpha0, dimVocab, dimK, numDocs, firstOrderMoments, documents)
println("Finished calculating second order moments and whitening matrix.")
Expand All @@ -39,9 +43,9 @@ class DataCumulant(sc: SparkContext, slices: Int, dimK: Int, alpha0: Double, tol

val alpha0sq: Double = alpha0 * alpha0
val Ta_shift = DenseMatrix.zeros[Double](dimK, dimK * dimK)
for (id_i: Int <- 0 until dimK) {
for (id_j: Int <- 0 until dimK) {
for (id_l: Int <- 0 until dimK) {
for (id_i <- 0 until dimK optimized) {
for (id_j <- 0 until dimK optimized) {
for (id_l <- 0 until dimK optimized) {
Ta_shift(id_i, id_j * dimK + id_l) += alpha0sq * firstOrderMoments_whitened(id_i) * firstOrderMoments_whitened(id_j) * firstOrderMoments_whitened(id_l)
}
}
Expand Down Expand Up @@ -96,9 +100,11 @@ class DataCumulant(sc: SparkContext, slices: Int, dimK: Int, alpha0: Double, tol
val scale2fac: Double = alpha0 * (alpha0 + 1.0) / (2.0 * len_calibrated * (len_calibrated - 1.0))
val Ta = breeze.linalg.DenseMatrix.zeros[Double](dimK, dimK * dimK)

for (i: Int <- 0 until dimK) {
for (j: Int <- 0 until dimK) {
for (l: Int <- 0 until dimK) {
import scalaxy.loops._
import scala.language.postfixOps
for (i <- 0 until dimK optimized) {
for (j <- 0 until dimK optimized) {
for (l <- 0 until dimK optimized) {
Ta(i, dimK * j + l) += scale3fac * Wc(i) * Wc(j) * Wc(l)

Ta(i, dimK * j + l) -= scale2fac * Wc(i) * Wc(j) * m1(l)
Expand Down
16 changes: 9 additions & 7 deletions SpectralLDA-Tensor/src/main/scala/Utils/AlgebraUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package Utils
*/

import breeze.linalg.{DenseMatrix, DenseVector}
import scalaxy.loops._
import scala.language.postfixOps

object AlgebraUtil{
private val SEED_random: Long = System.currentTimeMillis
Expand All @@ -32,8 +34,8 @@ object AlgebraUtil{
def orthogonalizeMatCols(B: DenseMatrix[Double]): DenseMatrix[Double] = {
val A:DenseMatrix[Double] = B.copy

for (j: Int <- 0 until A.cols) {
for (i: Int <- 0 until j) {
for (j <- 0 until A.cols optimized) {
for (i <- 0 until j optimized) {
val dotij = A(::, j) dot A(::, i)
A(::, j) :-= (A(::, i) :* dotij)

Expand All @@ -48,7 +50,7 @@ object AlgebraUtil{

def colWiseNorm2(A: breeze.linalg.DenseMatrix[Double]): breeze.linalg.DenseVector[Double] = {
val normVec = breeze.linalg.DenseVector.zeros[Double](A.cols)
for (i: Int <- 0 until A.cols) {
for (i <- 0 until A.cols optimized) {
val thisnorm: Double = Math.sqrt(A(::, i) dot A(::, i))
normVec(i) = (if (thisnorm > TOLERANCE) thisnorm else TOLERANCE)
}
Expand All @@ -58,7 +60,7 @@ object AlgebraUtil{

def matrixNormalization(B: DenseMatrix[Double]): DenseMatrix[Double] = {
val A: DenseMatrix[Double] = B.copy
for (i: Int <- 0 until A.cols) {
for (i <- 0 until A.cols optimized) {
val thisnorm: Double = Math.sqrt(A(::, i) dot A(::, i))
A(::, i) :*= (if (thisnorm > TOLERANCE) (1.0 / thisnorm) else TOLERANCE)
}
Expand All @@ -73,7 +75,7 @@ object AlgebraUtil{
def KhatrioRao(A: DenseMatrix[Double], B: DenseMatrix[Double]): DenseMatrix[Double] = {
assert(B.cols == A.cols)
val Out = DenseMatrix.zeros[Double](B.rows * A.rows, A.cols)
for (i: Int <- 0 until A.cols) {
for (i <- 0 until A.cols optimized) {
Out(::, i) := KhatrioRao(A(::, i), B(::, i))

}
Expand All @@ -89,7 +91,7 @@ object AlgebraUtil{
def Multip_KhatrioRao(T: DenseVector[Double], C: DenseMatrix[Double], B: DenseMatrix[Double]): DenseVector[Double] = {
assert(B.cols == C.cols)
val Out = DenseVector.zeros[Double](C.cols)
for (i: Int <- 0 until C.cols) {
for (i <- 0 until C.cols optimized) {
Out(i) = Multip_KhatrioRao(T, C(::, i), B(::, i))

}
Expand All @@ -100,7 +102,7 @@ object AlgebraUtil{
def Multip_KhatrioRao(T: DenseMatrix[Double], C: DenseMatrix[Double], B: DenseMatrix[Double]): DenseMatrix[Double] = {
assert(B.cols == C.cols)
val Out = DenseMatrix.zeros[Double](C.cols, T.rows)
for (i: Int <- 0 until T.rows) {
for (i <- 0 until T.rows optimized) {
val thisRowOfT: DenseVector[Double] = T(i, ::).t
Out(::, i) := Multip_KhatrioRao(thisRowOfT, C, B)
}
Expand Down

0 comments on commit 0fde2a8

Please sign in to comment.