From 3f0460094512d19dd5fdeeaa939e08e559edf45b Mon Sep 17 00:00:00 2001 From: Brian Wignall Date: Mon, 1 Jul 2024 07:42:20 -0400 Subject: [PATCH 1/5] Fix spurious type annotations --- .../breeze/linalg/DenseMatrixBenchmark.scala | 2 +- .../breeze/linalg/SparseVectorBenchmark.scala | 2 +- .../linalg/support/CanTraverseValues.scala | 22 +++++++++---------- .../main/scala/breeze/stats/ProbMonad.scala | 20 ++++++++--------- .../stats/mcmc/MetropolisHastings.scala | 8 +++---- 5 files changed, 27 insertions(+), 27 deletions(-) diff --git a/benchmark/src/main/scala/breeze/linalg/DenseMatrixBenchmark.scala b/benchmark/src/main/scala/breeze/linalg/DenseMatrixBenchmark.scala index 0fc820fcf..cb7bd933d 100644 --- a/benchmark/src/main/scala/breeze/linalg/DenseMatrixBenchmark.scala +++ b/benchmark/src/main/scala/breeze/linalg/DenseMatrixBenchmark.scala @@ -52,7 +52,7 @@ class DenseMatrixBenchmark extends BreezeBenchmark with BuildsRandomMatrices { // sin(dm) // } - def timeIntMatrixMultiply(reps: Int) = runWith(reps, randomIntMatrix(2500, 2500)): Unit = { dm => + def timeIntMatrixMultiply(reps: Int) = runWith(reps, randomIntMatrix(2500, 2500)) { dm => dm * dm } } diff --git a/benchmark/src/main/scala/breeze/linalg/SparseVectorBenchmark.scala b/benchmark/src/main/scala/breeze/linalg/SparseVectorBenchmark.scala index 92a6f6222..11f683916 100644 --- a/benchmark/src/main/scala/breeze/linalg/SparseVectorBenchmark.scala +++ b/benchmark/src/main/scala/breeze/linalg/SparseVectorBenchmark.scala @@ -23,7 +23,7 @@ import breeze.benchmark._ object SparseVectorBenchmark extends MyRunner(classOf[SparseVectorBenchmark]) class SparseVectorBenchmark extends BreezeBenchmark with BuildsRandomVectors { - def timeAllocate(reps: Int) = run(reps): Unit = { + def timeAllocate(reps: Int) = run(reps) { SparseVector.zeros[Double](1024) } diff --git a/benchmark/src/main/scala/breeze/linalg/support/CanTraverseValues.scala b/benchmark/src/main/scala/breeze/linalg/support/CanTraverseValues.scala index 26538861c..d6ce4916c 100644 --- a/benchmark/src/main/scala/breeze/linalg/support/CanTraverseValues.scala +++ b/benchmark/src/main/scala/breeze/linalg/support/CanTraverseValues.scala @@ -23,7 +23,7 @@ class CanTraverseValuesBenchmark extends BreezeBenchmark with BuildsRandomVector sum(arr) }) - def timePrimitiveSum(reps: Int) = runWith(reps, {randomArray(1024 * 8)}): Unit = { arr => + def timePrimitiveSum(reps: Int) = runWith(reps, {randomArray(1024 * 8)}){ arr => val d = arr.data var sum = 0.0 import breeze.macros._ @@ -37,7 +37,7 @@ class CanTraverseValuesBenchmark extends BreezeBenchmark with BuildsRandomVector sum(arr(0 to -1 by 5)) }) - def timePrimitiveSumStrided(reps: Int) = runWith(reps, {randomArray(1024 * 8 * 5)}): Unit = { arr => + def timePrimitiveSumStrided(reps: Int) = runWith(reps, {randomArray(1024 * 8 * 5)}){ arr => val d = arr.data var sum = 0.0 import breeze.macros._ @@ -49,15 +49,15 @@ class CanTraverseValuesBenchmark extends BreezeBenchmark with BuildsRandomVector */ /* - def timeSumMatrix(reps: Int) = runWith(reps, {randomMatrix(1024, 40)}): Unit = { arr => + def timeSumMatrix(reps: Int) = runWith(reps, {randomMatrix(1024, 40)}){ arr => sum(arr) } - def timeSumMatrixRows(reps: Int) = runWith(reps, {randomMatrix(1024, 40)}): Unit = { arr => + def timeSumMatrixRows(reps: Int) = runWith(reps, {randomMatrix(1024, 40)}){ arr => sum(arr(*, ::)) } - def timeSumMatrixRowsLoop(reps: Int) = runWith(reps, {randomMatrix(1024, 40)}): Unit = { arr => + def timeSumMatrixRowsLoop(reps: Int) = runWith(reps, {randomMatrix(1024, 40)}){ arr => val result = DenseVector.zeros[Double](1024) for (i <- 0 until arr.cols) { result += arr(::, i) @@ -65,11 +65,11 @@ class CanTraverseValuesBenchmark extends BreezeBenchmark with BuildsRandomVector result } - def timeSumMatrixCols(reps: Int) = runWith(reps, {randomMatrix(40, 1024)}): Unit = { arr => + def timeSumMatrixCols(reps: Int) = runWith(reps, {randomMatrix(40, 1024)}){ arr => sum(arr(::, *)) } - def timeSumMatrixColsLoop(reps: Int) = runWith(reps, {randomMatrix(40, 1024)}): Unit = { arr => + def timeSumMatrixColsLoop(reps: Int) = runWith(reps, {randomMatrix(40, 1024)}){ arr => val result = DenseVector.zeros[Double](1024) for (i <- 0 until arr.rows) { result += arr(i, ::).t @@ -78,19 +78,19 @@ class CanTraverseValuesBenchmark extends BreezeBenchmark with BuildsRandomVector } */ - def timeMaxMatrixCols(reps: Int) = runWith(reps, { randomMatrix(40, 1024) }): Unit = { arr => + def timeMaxMatrixCols(reps: Int) = runWith(reps, { randomMatrix(40, 1024) }) { arr => max(arr(::, *)) } - def timeMaxMatrixRows(reps: Int) = runWith(reps, { randomMatrix(40, 1024) }): Unit = { arr => + def timeMaxMatrixRows(reps: Int) = runWith(reps, { randomMatrix(40, 1024) }) { arr => max(arr(*, ::)) } - def timeMinMatrixCols(reps: Int) = runWith(reps, { randomMatrix(40, 1024) }): Unit = { arr => + def timeMinMatrixCols(reps: Int) = runWith(reps, { randomMatrix(40, 1024) }) { arr => min(arr(::, *)) } - def timeMinMatrixRows(reps: Int) = runWith(reps, { randomMatrix(40, 1024) }): Unit = { arr => + def timeMinMatrixRows(reps: Int) = runWith(reps, { randomMatrix(40, 1024) }) { arr => max(arr(*, ::)) } diff --git a/benchmark/src/main/scala/breeze/stats/ProbMonad.scala b/benchmark/src/main/scala/breeze/stats/ProbMonad.scala index e52ed8b69..e9c285b6d 100644 --- a/benchmark/src/main/scala/breeze/stats/ProbMonad.scala +++ b/benchmark/src/main/scala/breeze/stats/ProbMonad.scala @@ -18,7 +18,7 @@ class ProbMonadBenchmark extends BreezeBenchmark { val size = 1024 * 1024 - def timeMonad(reps: Int) = run(reps): Unit = { + def timeMonad(reps: Int) = run(reps) { /* The purpose of this benchmark is to compare monadic usage of rand to non-monadic usage (see timeRaw). */ val monadic = for { @@ -28,7 +28,7 @@ class ProbMonadBenchmark extends BreezeBenchmark { monadic.samplesVector(size) } - def timeRaw(reps: Int) = run(reps): Unit = { + def timeRaw(reps: Int) = run(reps) { /* The purpose of this benchmark is to compare monadic usage of rand to non-monadic usage (see timeMonad). */ val nonmonadic = new Rand[Double] { @@ -37,37 +37,37 @@ class ProbMonadBenchmark extends BreezeBenchmark { nonmonadic.samplesVector(size) } - def timeMap(reps: Int) = run(reps): Unit = { + def timeMap(reps: Int) = run(reps) { val mg = gaussian.map(f) mg.samplesVector(size) } - def timeMapRepeated(reps: Int) = run(reps): Unit = { + def timeMapRepeated(reps: Int) = run(reps) { val mg = gaussian.map(f).map(f2).map(f3) mg.samplesVector(size) } - def timeFlatMap(reps: Int) = run(reps): Unit = { + def timeFlatMap(reps: Int) = run(reps) { val mg = gaussian.flatMap(fm) mg.samplesVector(size) } - def timeFlatMapRepeated(reps: Int) = run(reps): Unit = { + def timeFlatMapRepeated(reps: Int) = run(reps) { val mg = gaussian.flatMap(fm).flatMap(fm).flatMap(fm) mg.samplesVector(size) } - def timeCondition(reps: Int) = run(reps): Unit = { + def timeCondition(reps: Int) = run(reps) { val mg = gaussian.condition(x => x > 0) mg.samplesVector(size) } - def timeRepeatCondition(reps: Int) = run(reps): Unit = { + def timeRepeatCondition(reps: Int) = run(reps) { val mg = gaussian.condition(x => x > 0).condition(x => x < 1).condition(x => x > -1) mg.samplesVector(size) } - def timeDrawOpt(reps: Int) = run(reps): Unit = { + def timeDrawOpt(reps: Int) = run(reps) { val mg = gaussian.condition(x => x > 0) val result = new Array[Option[Double]](size) cforRange(0 until size)(i => { @@ -75,7 +75,7 @@ class ProbMonadBenchmark extends BreezeBenchmark { }) result } - def timeDrawOptMultipleCondition(reps: Int) = run(reps): Unit = { + def timeDrawOptMultipleCondition(reps: Int) = run(reps) { val mg = gaussian.condition(x => x > 0).condition(x => x < 1).condition(x => x > -1) val result = new Array[Option[Double]](size) cforRange(0 until size)(i => { diff --git a/benchmark/src/main/scala/breeze/stats/mcmc/MetropolisHastings.scala b/benchmark/src/main/scala/breeze/stats/mcmc/MetropolisHastings.scala index 27328ed8e..11ce81466 100644 --- a/benchmark/src/main/scala/breeze/stats/mcmc/MetropolisHastings.scala +++ b/benchmark/src/main/scala/breeze/stats/mcmc/MetropolisHastings.scala @@ -42,13 +42,13 @@ class MetropolisHastingsBenchmark extends BreezeBenchmark { result } - def timeMarkovChainEquiv(reps: Int) = run(reps): Unit = { + def timeMarkovChainEquiv(reps: Int) = run(reps) { val m = ArbitraryMetropolisHastings(likelihood _, gaussianJump _, gaussianJumpLogProb _, 0.5, burnIn = 0, dropCount = 0) pullAllSamples(m) } - def timeMetropolisHastings(reps: Int) = run(reps): Unit = { + def timeMetropolisHastings(reps: Int) = run(reps) { val m = ArbitraryMetropolisHastings( likelihood _, (_: Double) => Uniform(0, 1), @@ -59,7 +59,7 @@ class MetropolisHastingsBenchmark extends BreezeBenchmark { pullAllSamples(m) } - def timeMetropolisHastingsWithWork(reps: Int) = run(reps): Unit = { + def timeMetropolisHastingsWithWork(reps: Int) = run(reps) { val m = ArbitraryMetropolisHastings( likelihood _, (_: Double) => Uniform(0, 1), @@ -70,7 +70,7 @@ class MetropolisHastingsBenchmark extends BreezeBenchmark { pullAllSamplesWithWork(m) } - def timeThreadedBufferedWithWork(reps: Int) = run(reps): Unit = { + def timeThreadedBufferedWithWork(reps: Int) = run(reps) { val wrapped = ArbitraryMetropolisHastings( likelihood _, (_: Double) => Uniform(0, 1), From ba1a6a0d1b0bbbcf3fb493a873b881e82e4bbbca Mon Sep 17 00:00:00 2001 From: Brian Wignall Date: Mon, 1 Jul 2024 07:43:39 -0400 Subject: [PATCH 2/5] Remove usage of extinct method --- .../breeze/linalg/DenseVectorBenchmark.scala | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/benchmark/src/main/scala/breeze/linalg/DenseVectorBenchmark.scala b/benchmark/src/main/scala/breeze/linalg/DenseVectorBenchmark.scala index ca2a02257..dc98db5b5 100644 --- a/benchmark/src/main/scala/breeze/linalg/DenseVectorBenchmark.scala +++ b/benchmark/src/main/scala/breeze/linalg/DenseVectorBenchmark.scala @@ -83,22 +83,7 @@ class DenseVectorBenchmark extends BreezeBenchmark with BuildsRandomVectors { def timeValueAt(reps: Int) = valueAtBench(reps, 1024 * 8, 1) def timeValueAtStride4(reps: Int) = valueAtBench(reps, 1024 * 8, 4) - - def unsafeValueAtBench(reps: Int, size: Int, stride: Int) = - runWith(reps, { randomArray(size, stride = stride) })(arr => { - var i = 0 - var t: Double = 0 - while (i < arr.size) { - t += arr - .unsafeValueAt(i) //This is not strictly part of the benchmark, but done so that the JIT doensn't eliminate everything - i += 1 - } - t - }) - - def timeUnsafeValueAt(reps: Int) = unsafeValueAtBench(reps, 1024 * 8, 1) - def timeUnsafeValueAtStride4(reps: Int) = unsafeValueAtBench(reps, 1024 * 8, 4) - + def updateBench(reps: Int, size: Int, stride: Int) = runWith(reps, { randomArray(size, stride = stride) })(arr => { var i = 0 From 878efd0b3b751cb92ccfb462ac3c2a25901d3ea5 Mon Sep 17 00:00:00 2001 From: Brian Wignall Date: Mon, 1 Jul 2024 07:51:00 -0400 Subject: [PATCH 3/5] Update BreezeBenchmark.scala to reflect Caliper API --- .../main/scala/breeze/benchmark/BreezeBenchmark.scala | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/benchmark/src/main/scala/breeze/benchmark/BreezeBenchmark.scala b/benchmark/src/main/scala/breeze/benchmark/BreezeBenchmark.scala index 5bb60bcac..e997ae2c7 100644 --- a/benchmark/src/main/scala/breeze/benchmark/BreezeBenchmark.scala +++ b/benchmark/src/main/scala/breeze/benchmark/BreezeBenchmark.scala @@ -18,12 +18,13 @@ package breeze.benchmark -import com.google.caliper.{Runner, SimpleBenchmark} +import com.google.caliper.Benchmark +import com.google.caliper.runner.CaliperMain /** * Extend this to create an actual benchmarking class. */ -trait BreezeBenchmark extends SimpleBenchmark { +trait BreezeBenchmark { /** * Sugar to run 'f' for 'reps' number of times. @@ -68,6 +69,6 @@ trait BreezeBenchmark extends SimpleBenchmark { /** * Extend this to create a main object which will run 'cls' (a benchmark). */ -abstract class MyRunner(val cls: java.lang.Class[_ <: com.google.caliper.Benchmark]) { - def main(args: Array[String]): Unit = Runner.main(cls, args: _*) +abstract class MyRunner(val cls: java.lang.Class[_]) { + def main(args: Array[String]): Unit = CaliperMain.main(cls, args) } From 9dd67b572d991ab66c729a9f1d95a7217f52e31a Mon Sep 17 00:00:00 2001 From: Brian Wignall Date: Mon, 1 Jul 2024 07:51:42 -0400 Subject: [PATCH 4/5] Import implicit for Semiring --- .../main/scala/breeze/polynomial/DensePolynomialBenchmark.scala | 1 + 1 file changed, 1 insertion(+) diff --git a/benchmark/src/main/scala/breeze/polynomial/DensePolynomialBenchmark.scala b/benchmark/src/main/scala/breeze/polynomial/DensePolynomialBenchmark.scala index e4dd1cd1a..ecf649310 100644 --- a/benchmark/src/main/scala/breeze/polynomial/DensePolynomialBenchmark.scala +++ b/benchmark/src/main/scala/breeze/polynomial/DensePolynomialBenchmark.scala @@ -4,6 +4,7 @@ import breeze.benchmark._ import breeze.linalg.BuildsRandomVectors import breeze.stats.distributions._ +import algebra.instances.all.doubleAlgebra import spire.math._ import spire.math.poly._ import breeze.macros._ From 43c74ac08f94a5ec996a334c96016a152d91fe1e Mon Sep 17 00:00:00 2001 From: Brian Wignall Date: Mon, 1 Jul 2024 07:56:38 -0400 Subject: [PATCH 5/5] Add explicit implicit RandBasis-s, update method call name --- .../src/main/scala/breeze/benchmark/BreezeBenchmark.scala | 2 ++ .../main/scala/breeze/linalg/DenseMatrixBenchmark.scala | 2 +- .../main/scala/breeze/linalg/DenseVectorBenchmark.scala | 8 ++++---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/benchmark/src/main/scala/breeze/benchmark/BreezeBenchmark.scala b/benchmark/src/main/scala/breeze/benchmark/BreezeBenchmark.scala index e997ae2c7..c57effa01 100644 --- a/benchmark/src/main/scala/breeze/benchmark/BreezeBenchmark.scala +++ b/benchmark/src/main/scala/breeze/benchmark/BreezeBenchmark.scala @@ -18,6 +18,7 @@ package breeze.benchmark +import breeze.stats.distributions.RandBasis import com.google.caliper.Benchmark import com.google.caliper.runner.CaliperMain @@ -25,6 +26,7 @@ import com.google.caliper.runner.CaliperMain * Extend this to create an actual benchmarking class. */ trait BreezeBenchmark { + implicit val randBasis: RandBasis = RandBasis.mt0 /** * Sugar to run 'f' for 'reps' number of times. diff --git a/benchmark/src/main/scala/breeze/linalg/DenseMatrixBenchmark.scala b/benchmark/src/main/scala/breeze/linalg/DenseMatrixBenchmark.scala index cb7bd933d..18c715626 100644 --- a/benchmark/src/main/scala/breeze/linalg/DenseMatrixBenchmark.scala +++ b/benchmark/src/main/scala/breeze/linalg/DenseMatrixBenchmark.scala @@ -8,7 +8,7 @@ import breeze.macros._ object DenseMatrixBenchmark extends MyRunner(classOf[DenseMatrixBenchmark]) trait BuildsRandomMatrices { - private val uniform = Uniform(0, 1) + private val uniform = Uniform(0, 1)(RandBasis.mt0) def randomMatrix(m: Int, n: Int, transpose: Boolean = false): DenseMatrix[Double] = { if (!transpose) { DenseMatrix.rand[Double](m, n) diff --git a/benchmark/src/main/scala/breeze/linalg/DenseVectorBenchmark.scala b/benchmark/src/main/scala/breeze/linalg/DenseVectorBenchmark.scala index dc98db5b5..d347e0199 100644 --- a/benchmark/src/main/scala/breeze/linalg/DenseVectorBenchmark.scala +++ b/benchmark/src/main/scala/breeze/linalg/DenseVectorBenchmark.scala @@ -7,14 +7,14 @@ import breeze.macros._ object DenseVectorBenchmark extends MyRunner(classOf[DenseVectorBenchmark]) trait BuildsRandomVectors { - private val uniform = Uniform(0, 1) + private val uniform = Uniform(0, 1)(RandBasis.mt0) def randomArray(size: Int, offset: Int = 0, stride: Int = 1): DenseVector[Double] = { require(offset >= 0) require(stride >= 1) val result = new DenseVector(new Array[Double](offset + stride * size), offset, stride, size) var i = 0 while (i < size) { - result.unsafeUpdate(i, uniform.draw()) + result.update(i, uniform.draw()) i += 1 } result @@ -83,7 +83,7 @@ class DenseVectorBenchmark extends BreezeBenchmark with BuildsRandomVectors { def timeValueAt(reps: Int) = valueAtBench(reps, 1024 * 8, 1) def timeValueAtStride4(reps: Int) = valueAtBench(reps, 1024 * 8, 4) - + def updateBench(reps: Int, size: Int, stride: Int) = runWith(reps, { randomArray(size, stride = stride) })(arr => { var i = 0 @@ -101,7 +101,7 @@ class DenseVectorBenchmark extends BreezeBenchmark with BuildsRandomVectors { runWith(reps, { randomArray(size, stride = stride) })(arr => { var i = 0 while (i < arr.size) { - arr.unsafeUpdate(i, i.toDouble) + arr.update(i, i.toDouble) i += 1 } arr