From 0fde2a88067bef2fc505d7e7e78b35101369b279 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Thu, 10 Nov 2011 19:10:00 +0200 Subject: [PATCH] Use Scalaxy/loops to optimize Range integer macros Removal of the type annotations is because of https://github.com/nativelibs4java/Scalaxy/issues/29 --- SpectralLDA-Tensor/build.sbt | 2 ++ .../src/main/scala/Algorithm/ALS.scala | 12 +++++++----- .../main/scala/DataMoments/DataCumulant.scala | 18 ++++++++++++------ .../src/main/scala/Utils/AlgebraUtil.scala | 16 +++++++++------- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/SpectralLDA-Tensor/build.sbt b/SpectralLDA-Tensor/build.sbt index d1c1129..727c359 100644 --- a/SpectralLDA-Tensor/build.sbt +++ b/SpectralLDA-Tensor/build.sbt @@ -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" diff --git a/SpectralLDA-Tensor/src/main/scala/Algorithm/ALS.scala b/SpectralLDA-Tensor/src/main/scala/Algorithm/ALS.scala index 5e6c7a4..32abf7e 100644 --- a/SpectralLDA-Tensor/src/main/scala/Algorithm/ALS.scala +++ b/SpectralLDA-Tensor/src/main/scala/Algorithm/ALS.scala @@ -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._ @@ -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) @@ -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) @@ -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) @@ -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) @@ -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() diff --git a/SpectralLDA-Tensor/src/main/scala/DataMoments/DataCumulant.scala b/SpectralLDA-Tensor/src/main/scala/DataMoments/DataCumulant.scala index 11b0f21..cf59361 100644 --- a/SpectralLDA-Tensor/src/main/scala/DataMoments/DataCumulant.scala +++ b/SpectralLDA-Tensor/src/main/scala/DataMoments/DataCumulant.scala @@ -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.") @@ -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) } } @@ -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) diff --git a/SpectralLDA-Tensor/src/main/scala/Utils/AlgebraUtil.scala b/SpectralLDA-Tensor/src/main/scala/Utils/AlgebraUtil.scala index 7f8ab16..da27f5b 100644 --- a/SpectralLDA-Tensor/src/main/scala/Utils/AlgebraUtil.scala +++ b/SpectralLDA-Tensor/src/main/scala/Utils/AlgebraUtil.scala @@ -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 @@ -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) @@ -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) } @@ -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) } @@ -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)) } @@ -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)) } @@ -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) }