From 974d73e25c3e5c44956848a2b877cdc20f7c7436 Mon Sep 17 00:00:00 2001 From: Alexander Nozik Date: Wed, 22 Sep 2021 22:09:46 +0300 Subject: [PATCH] Adjust benchmarks. --- .../kmath/benchmarks/BigIntBenchmark.kt | 29 +++++++++--- .../kscience/kmath/benchmarks/DotBenchmark.kt | 44 +++++++++---------- .../kscience/kmath/linear/dotPerformance.kt | 34 ++++++++++++++ 3 files changed, 77 insertions(+), 30 deletions(-) create mode 100644 examples/src/main/kotlin/space/kscience/kmath/linear/dotPerformance.kt diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt index 6f501dedb..f2b2d4d7a 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/BigIntBenchmark.kt @@ -11,7 +11,10 @@ import org.openjdk.jmh.annotations.Benchmark import org.openjdk.jmh.annotations.Scope import org.openjdk.jmh.annotations.State import space.kscience.kmath.misc.UnstableKMathAPI -import space.kscience.kmath.operations.* +import space.kscience.kmath.operations.BigIntField +import space.kscience.kmath.operations.JBigIntegerField +import space.kscience.kmath.operations.invoke +import space.kscience.kmath.operations.parseBigInteger import java.math.BigInteger @@ -19,12 +22,24 @@ import java.math.BigInteger @State(Scope.Benchmark) internal class BigIntBenchmark { + val kmSmallNumber = BigIntField.number(100) + val jvmSmallNumber = JBigIntegerField.number(100) val kmNumber = BigIntField.number(Int.MAX_VALUE) val jvmNumber = JBigIntegerField.number(Int.MAX_VALUE) - val largeKmNumber = BigIntField { number(11).pow(100_000U) } - val largeJvmNumber: BigInteger = JBigIntegerField { number(11).pow(100_000) } + val kmLargeNumber = BigIntField { number(11).pow(100_000U) } + val jvmLargeNumber: BigInteger = JBigIntegerField { number(11).pow(100_000) } val bigExponent = 50_000 + @Benchmark + fun kmSmallAdd(blackhole: Blackhole) = BigIntField { + blackhole.consume(kmSmallNumber + kmSmallNumber + kmSmallNumber) + } + + @Benchmark + fun jvmSmallAdd(blackhole: Blackhole) = JBigIntegerField { + blackhole.consume(jvmSmallNumber + jvmSmallNumber + jvmSmallNumber) + } + @Benchmark fun kmAdd(blackhole: Blackhole) = BigIntField { blackhole.consume(kmNumber + kmNumber + kmNumber) @@ -37,12 +52,12 @@ internal class BigIntBenchmark { @Benchmark fun kmAddLarge(blackhole: Blackhole) = BigIntField { - blackhole.consume(largeKmNumber + largeKmNumber + largeKmNumber) + blackhole.consume(kmLargeNumber + kmLargeNumber + kmLargeNumber) } @Benchmark fun jvmAddLarge(blackhole: Blackhole) = JBigIntegerField { - blackhole.consume(largeJvmNumber + largeJvmNumber + largeJvmNumber) + blackhole.consume(jvmLargeNumber + jvmLargeNumber + jvmLargeNumber) } @Benchmark @@ -52,7 +67,7 @@ internal class BigIntBenchmark { @Benchmark fun kmMultiplyLarge(blackhole: Blackhole) = BigIntField { - blackhole.consume(largeKmNumber*largeKmNumber) + blackhole.consume(kmLargeNumber*kmLargeNumber) } @Benchmark @@ -62,7 +77,7 @@ internal class BigIntBenchmark { @Benchmark fun jvmMultiplyLarge(blackhole: Blackhole) = JBigIntegerField { - blackhole.consume(largeJvmNumber*largeJvmNumber) + blackhole.consume(jvmLargeNumber*jvmLargeNumber) } @Benchmark diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt index 6373d1ce5..33cb57c6f 100644 --- a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/DotBenchmark.kt @@ -11,7 +11,6 @@ import kotlinx.benchmark.Scope import kotlinx.benchmark.State import space.kscience.kmath.commons.linear.CMLinearSpace import space.kscience.kmath.ejml.EjmlLinearSpaceDDRM -import space.kscience.kmath.linear.LinearSpace import space.kscience.kmath.linear.invoke import space.kscience.kmath.linear.linearSpace import space.kscience.kmath.operations.DoubleField @@ -26,8 +25,12 @@ internal class DotBenchmark { const val dim = 1000 //creating invertible matrix - val matrix1 = LinearSpace.double.buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } - val matrix2 = LinearSpace.double.buildMatrix(dim, dim) { i, j -> if (i <= j) random.nextDouble() else 0.0 } + val matrix1 = Double.algebra.linearSpace.buildMatrix(dim, dim) { i, j -> + if (i <= j) random.nextDouble() else 0.0 + } + val matrix2 = Double.algebra.linearSpace.buildMatrix(dim, dim) { i, j -> + if (i <= j) random.nextDouble() else 0.0 + } val cmMatrix1 = CMLinearSpace { matrix1.toCM() } val cmMatrix2 = CMLinearSpace { matrix2.toCM() } @@ -37,37 +40,32 @@ internal class DotBenchmark { } @Benchmark - fun cmDot(blackhole: Blackhole) { - CMLinearSpace { - blackhole.consume(cmMatrix1 dot cmMatrix2) - } + fun cmDotWithConversion(blackhole: Blackhole) = CMLinearSpace { + blackhole.consume(matrix1 dot matrix2) } @Benchmark - fun ejmlDot(blackhole: Blackhole) { - EjmlLinearSpaceDDRM { - blackhole.consume(ejmlMatrix1 dot ejmlMatrix2) - } + fun cmDot(blackhole: Blackhole) = CMLinearSpace { + blackhole.consume(cmMatrix1 dot cmMatrix2) } @Benchmark - fun ejmlDotWithConversion(blackhole: Blackhole) { - EjmlLinearSpaceDDRM { - blackhole.consume(matrix1 dot matrix2) - } + fun ejmlDot(blackhole: Blackhole) = EjmlLinearSpaceDDRM { + blackhole.consume(ejmlMatrix1 dot ejmlMatrix2) } @Benchmark - fun bufferedDot(blackhole: Blackhole) { - with(DoubleField.linearSpace(Buffer.Companion::auto)) { - blackhole.consume(matrix1 dot matrix2) - } + fun ejmlDotWithConversion(blackhole: Blackhole) = EjmlLinearSpaceDDRM { + blackhole.consume(matrix1 dot matrix2) } @Benchmark - fun doubleDot(blackhole: Blackhole) { - with(Double.algebra.linearSpace) { - blackhole.consume(matrix1 dot matrix2) - } + fun bufferedDot(blackhole: Blackhole) = with(DoubleField.linearSpace(Buffer.Companion::auto)) { + blackhole.consume(matrix1 dot matrix2) + } + + @Benchmark + fun doubleDot(blackhole: Blackhole) = with(Double.algebra.linearSpace) { + blackhole.consume(matrix1 dot matrix2) } } diff --git a/examples/src/main/kotlin/space/kscience/kmath/linear/dotPerformance.kt b/examples/src/main/kotlin/space/kscience/kmath/linear/dotPerformance.kt new file mode 100644 index 000000000..31762b6d8 --- /dev/null +++ b/examples/src/main/kotlin/space/kscience/kmath/linear/dotPerformance.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2018-2021 KMath contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. + */ + +package space.kscience.kmath.linear + +import space.kscience.kmath.operations.algebra +import kotlin.random.Random +import kotlin.system.measureTimeMillis + +fun main() { + val random = Random(12224) + val dim = 1000 + + //creating invertible matrix + val matrix1 = Double.algebra.linearSpace.buildMatrix(dim, dim) { i, j -> + if (i <= j) random.nextDouble() else 0.0 + } + val matrix2 = Double.algebra.linearSpace.buildMatrix(dim, dim) { i, j -> + if (i <= j) random.nextDouble() else 0.0 + } + + val time = measureTimeMillis { + with(Double.algebra.linearSpace) { + repeat(10) { + val res = matrix1 dot matrix2 + } + } + } + + println(time) + +} \ No newline at end of file