Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile benchmark directory #859

Merged
merged 5 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions benchmark/src/main/scala/breeze/benchmark/BreezeBenchmark.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@

package breeze.benchmark

import com.google.caliper.{Runner, SimpleBenchmark}
import breeze.stats.distributions.RandBasis
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 {
implicit val randBasis: RandBasis = RandBasis.mt0

/**
* Sugar to run 'f' for 'reps' number of times.
Expand Down Expand Up @@ -68,6 +71,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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
21 changes: 3 additions & 18 deletions benchmark/src/main/scala/breeze/linalg/DenseVectorBenchmark.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -84,21 +84,6 @@ 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
Expand All @@ -116,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand All @@ -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._
Expand All @@ -49,27 +49,27 @@ 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)
}
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
Expand All @@ -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(*, ::))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down
20 changes: 10 additions & 10 deletions benchmark/src/main/scala/breeze/stats/ProbMonad.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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] {
Expand All @@ -37,45 +37,45 @@ 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 => {
result(i) = mg.drawOpt()
})
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 => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand Down
Loading