From 9a9ebc730453735c5b6c0364d9fdbed1e92b3bc3 Mon Sep 17 00:00:00 2001 From: therealansh Date: Tue, 8 Jun 2021 19:19:55 +0530 Subject: [PATCH] add: benchmarks for Jafama --- benchmarks/build.gradle.kts | 7 +- .../kmath/benchmarks/JafamaBenchmark.kt | 48 ++++++++ .../kscience/kmath/jafama/KMathJafama.kt | 104 +++++++++--------- 3 files changed, 106 insertions(+), 53 deletions(-) create mode 100644 benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/JafamaBenchmark.kt diff --git a/benchmarks/build.gradle.kts b/benchmarks/build.gradle.kts index 98ffc5a96..182c182d9 100644 --- a/benchmarks/build.gradle.kts +++ b/benchmarks/build.gradle.kts @@ -30,6 +30,7 @@ kotlin { implementation(project(":kmath-stat")) implementation(project(":kmath-dimensions")) implementation(project(":kmath-for-real")) + implementation(project(":kmath-jafama")) implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.3.0") } } @@ -42,7 +43,6 @@ kotlin { implementation(project(":kmath-kotlingrad")) implementation(project(":kmath-viktor")) implementation("org.nd4j:nd4j-native:1.0.0-beta7") - // uncomment if your system supports AVX2 // val os = System.getProperty("os.name") // @@ -95,6 +95,11 @@ benchmark { commonConfiguration() include("BigIntBenchmark") } + + configurations.register("jafamaDouble") { + commonConfiguration() + include("JafamaBenchmark") + } } // Fix kotlinx-benchmarks bug diff --git a/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/JafamaBenchmark.kt b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/JafamaBenchmark.kt new file mode 100644 index 000000000..c828de82b --- /dev/null +++ b/benchmarks/src/jvmMain/kotlin/space/kscience/kmath/benchmarks/JafamaBenchmark.kt @@ -0,0 +1,48 @@ +/* + * 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/LICENSE.txt file. + */ + +package space.kscience.kmath.benchmarks + +import kotlinx.benchmark.Blackhole +import org.openjdk.jmh.annotations.Benchmark +import org.openjdk.jmh.annotations.Scope +import org.openjdk.jmh.annotations.State +import space.kscience.kmath.jafama.JafamaDoubleField +import space.kscience.kmath.jafama.StrictJafamaDoubleField +import space.kscience.kmath.operations.DoubleField +import space.kscience.kmath.operations.invoke + + +@State(Scope.Benchmark) +internal class JafamaBenchmark { + @Benchmark + fun jafamaBench(blackhole: Blackhole) = invokeBenchmarks(jafama, blackhole) + + @Benchmark + fun coreBench(blackhole: Blackhole) = invokeBenchmarks(core,blackhole) + + @Benchmark + fun strictJafamaBench(blackhole: Blackhole) = invokeBenchmarks(strictJafama,blackhole) + + private fun invokeBenchmarks(expr: Double, blackhole: Blackhole) { + blackhole.consume(expr) + } + + private companion object { + private val x: Double = Double.MAX_VALUE + + private val jafama = JafamaDoubleField{ + x * power(x, 1_000_000) * exp(x) / cos(x) + } + + private val core = DoubleField { + x * power(x, 1_000_000) * exp(x) / cos(x) + } + + private val strictJafama = StrictJafamaDoubleField { + x * power(x, 1_000_000) * exp(x) / cos(x) + } + } +} diff --git a/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt b/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt index 9d4341036..f37d8e7b3 100644 --- a/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt +++ b/kmath-jafama/src/main/kotlin/space/kscience/kmath/jafama/KMathJafama.kt @@ -8,8 +8,8 @@ import net.jafama.* */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") public object JafamaDoubleField : ExtendedField, Norm, ScaleOperations { - public override inline val zero: Double get() = 0.0 - public override inline val one: Double get() = 1.0 + public override val zero: Double get() = 0.0 + public override val one: Double get() = 1.0 public override fun number(value: Number): Double = value.toDouble() @@ -19,38 +19,38 @@ public object JafamaDoubleField : ExtendedField, Norm, S else -> super.binaryOperationFunction(operation) } - public override inline fun add(a: Double, b: Double): Double = a + b + public override fun add(a: Double, b: Double): Double = a + b - public override inline fun multiply(a: Double, b: Double): Double = a * b - public override inline fun divide(a: Double, b: Double): Double = a / b + public override fun multiply(a: Double, b: Double): Double = a * b + public override fun divide(a: Double, b: Double): Double = a / b public override fun scale(a: Double, value: Double): Double = a * value - public override inline fun sin(arg: Double): Double = FastMath.sin(arg) - public override inline fun cos(arg: Double): Double = FastMath.cos(arg) - public override inline fun tan(arg: Double): Double = FastMath.tan(arg) - public override inline fun acos(arg: Double): Double = FastMath.acos(arg) - public override inline fun asin(arg: Double): Double = FastMath.asin(arg) - public override inline fun atan(arg: Double): Double = FastMath.atan(arg) + public override fun sin(arg: Double): Double = FastMath.sin(arg) + public override fun cos(arg: Double): Double = FastMath.cos(arg) + public override fun tan(arg: Double): Double = FastMath.tan(arg) + public override fun acos(arg: Double): Double = FastMath.acos(arg) + public override fun asin(arg: Double): Double = FastMath.asin(arg) + public override fun atan(arg: Double): Double = FastMath.atan(arg) - public override inline fun sinh(arg: Double): Double = FastMath.sinh(arg) - public override inline fun cosh(arg: Double): Double = FastMath.cosh(arg) - public override inline fun tanh(arg: Double): Double = FastMath.tanh(arg) - public override inline fun asinh(arg: Double): Double = FastMath.asinh(arg) - public override inline fun acosh(arg: Double): Double = FastMath.acosh(arg) - public override inline fun atanh(arg: Double): Double = FastMath.atanh(arg) + public override fun sinh(arg: Double): Double = FastMath.sinh(arg) + public override fun cosh(arg: Double): Double = FastMath.cosh(arg) + public override fun tanh(arg: Double): Double = FastMath.tanh(arg) + public override fun asinh(arg: Double): Double = FastMath.asinh(arg) + public override fun acosh(arg: Double): Double = FastMath.acosh(arg) + public override fun atanh(arg: Double): Double = FastMath.atanh(arg) - public override inline fun power(arg: Double, pow: Number): Double = FastMath.pow(arg, pow.toDouble()) - public override inline fun exp(arg: Double): Double = FastMath.exp(arg) - public override inline fun ln(arg: Double): Double = FastMath.log(arg) + public override fun power(arg: Double, pow: Number): Double = FastMath.pow(arg, pow.toDouble()) + public override fun exp(arg: Double): Double = FastMath.exp(arg) + public override fun ln(arg: Double): Double = FastMath.log(arg) - public override inline fun norm(arg: Double): Double = FastMath.abs(arg) + public override fun norm(arg: Double): Double = FastMath.abs(arg) - public override inline fun Double.unaryMinus(): Double = -this - public override inline fun Double.plus(b: Double): Double = this + b - public override inline fun Double.minus(b: Double): Double = this - b - public override inline fun Double.times(b: Double): Double = this * b - public override inline fun Double.div(b: Double): Double = this / b + public override fun Double.unaryMinus(): Double = -this + public override fun Double.plus(b: Double): Double = this + b + public override fun Double.minus(b: Double): Double = this - b + public override fun Double.times(b: Double): Double = this * b + public override fun Double.div(b: Double): Double = this / b } /** @@ -58,8 +58,8 @@ public object JafamaDoubleField : ExtendedField, Norm, S */ @Suppress("EXTENSION_SHADOWED_BY_MEMBER", "OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE") public object StrictJafamaDoubleField : ExtendedField, Norm, ScaleOperations { - public override inline val zero: Double get() = 0.0 - public override inline val one: Double get() = 1.0 + public override val zero: Double get() = 0.0 + public override val one: Double get() = 1.0 public override fun number(value: Number): Double = value.toDouble() @@ -69,38 +69,38 @@ public object StrictJafamaDoubleField : ExtendedField, Norm super.binaryOperationFunction(operation) } - public override inline fun add(a: Double, b: Double): Double = a + b + public override fun add(a: Double, b: Double): Double = a + b - public override inline fun multiply(a: Double, b: Double): Double = a * b - public override inline fun divide(a: Double, b: Double): Double = a / b + public override fun multiply(a: Double, b: Double): Double = a * b + public override fun divide(a: Double, b: Double): Double = a / b public override fun scale(a: Double, value: Double): Double = a * value - public override inline fun sin(arg: Double): Double = StrictFastMath.sin(arg) - public override inline fun cos(arg: Double): Double = StrictFastMath.cos(arg) - public override inline fun tan(arg: Double): Double = StrictFastMath.tan(arg) - public override inline fun acos(arg: Double): Double = StrictFastMath.acos(arg) - public override inline fun asin(arg: Double): Double = StrictFastMath.asin(arg) - public override inline fun atan(arg: Double): Double = StrictFastMath.atan(arg) + public override fun sin(arg: Double): Double = StrictFastMath.sin(arg) + public override fun cos(arg: Double): Double = StrictFastMath.cos(arg) + public override fun tan(arg: Double): Double = StrictFastMath.tan(arg) + public override fun acos(arg: Double): Double = StrictFastMath.acos(arg) + public override fun asin(arg: Double): Double = StrictFastMath.asin(arg) + public override fun atan(arg: Double): Double = StrictFastMath.atan(arg) - public override inline fun sinh(arg: Double): Double = StrictFastMath.sinh(arg) - public override inline fun cosh(arg: Double): Double = StrictFastMath.cosh(arg) - public override inline fun tanh(arg: Double): Double = StrictFastMath.tanh(arg) - public override inline fun asinh(arg: Double): Double = StrictFastMath.asinh(arg) - public override inline fun acosh(arg: Double): Double = StrictFastMath.acosh(arg) - public override inline fun atanh(arg: Double): Double = StrictFastMath.atanh(arg) + public override fun sinh(arg: Double): Double = StrictFastMath.sinh(arg) + public override fun cosh(arg: Double): Double = StrictFastMath.cosh(arg) + public override fun tanh(arg: Double): Double = StrictFastMath.tanh(arg) + public override fun asinh(arg: Double): Double = StrictFastMath.asinh(arg) + public override fun acosh(arg: Double): Double = StrictFastMath.acosh(arg) + public override fun atanh(arg: Double): Double = StrictFastMath.atanh(arg) - public override inline fun power(arg: Double, pow: Number): Double = StrictFastMath.pow(arg, pow.toDouble()) - public override inline fun exp(arg: Double): Double = StrictFastMath.exp(arg) - public override inline fun ln(arg: Double): Double = StrictFastMath.log(arg) + public override fun power(arg: Double, pow: Number): Double = StrictFastMath.pow(arg, pow.toDouble()) + public override fun exp(arg: Double): Double = StrictFastMath.exp(arg) + public override fun ln(arg: Double): Double = StrictFastMath.log(arg) - public override inline fun norm(arg: Double): Double = StrictFastMath.abs(arg) + public override fun norm(arg: Double): Double = StrictFastMath.abs(arg) - public override inline fun Double.unaryMinus(): Double = -this - public override inline fun Double.plus(b: Double): Double = this + b - public override inline fun Double.minus(b: Double): Double = this - b - public override inline fun Double.times(b: Double): Double = this * b - public override inline fun Double.div(b: Double): Double = this / b + public override fun Double.unaryMinus(): Double = -this + public override fun Double.plus(b: Double): Double = this + b + public override fun Double.minus(b: Double): Double = this - b + public override fun Double.times(b: Double): Double = this * b + public override fun Double.div(b: Double): Double = this / b }