forked from kscience/kmath
Add nd add benchmarks
This commit is contained in:
parent
d9f36365d9
commit
30e3e80397
@ -36,6 +36,7 @@ kotlin {
|
|||||||
implementation(project(":kmath-dimensions"))
|
implementation(project(":kmath-dimensions"))
|
||||||
implementation(project(":kmath-for-real"))
|
implementation(project(":kmath-for-real"))
|
||||||
implementation(project(":kmath-jafama"))
|
implementation(project(":kmath-jafama"))
|
||||||
|
implementation(project(":kmath-tensors"))
|
||||||
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.3.1")
|
implementation("org.jetbrains.kotlinx:kotlinx-benchmark-runtime:0.3.1")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -81,6 +82,11 @@ benchmark {
|
|||||||
include("BufferBenchmark")
|
include("BufferBenchmark")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configurations.register("nd") {
|
||||||
|
commonConfiguration()
|
||||||
|
include("NDFieldBenchmark")
|
||||||
|
}
|
||||||
|
|
||||||
configurations.register("dot") {
|
configurations.register("dot") {
|
||||||
commonConfiguration()
|
commonConfiguration()
|
||||||
include("DotBenchmark")
|
include("DotBenchmark")
|
||||||
|
@ -12,44 +12,63 @@ import kotlinx.benchmark.State
|
|||||||
import space.kscience.kmath.nd.StructureND
|
import space.kscience.kmath.nd.StructureND
|
||||||
import space.kscience.kmath.nd.autoNdAlgebra
|
import space.kscience.kmath.nd.autoNdAlgebra
|
||||||
import space.kscience.kmath.nd.ndAlgebra
|
import space.kscience.kmath.nd.ndAlgebra
|
||||||
|
import space.kscience.kmath.nd4j.nd4j
|
||||||
import space.kscience.kmath.operations.DoubleField
|
import space.kscience.kmath.operations.DoubleField
|
||||||
import space.kscience.kmath.structures.Buffer
|
import space.kscience.kmath.structures.Buffer
|
||||||
|
import space.kscience.kmath.tensors.core.DoubleTensor
|
||||||
|
import space.kscience.kmath.tensors.core.ones
|
||||||
|
import space.kscience.kmath.tensors.core.tensorAlgebra
|
||||||
|
|
||||||
@State(Scope.Benchmark)
|
@State(Scope.Benchmark)
|
||||||
internal class NDFieldBenchmark {
|
internal class NDFieldBenchmark {
|
||||||
@Benchmark
|
@Benchmark
|
||||||
fun autoFieldAdd(blackhole: Blackhole) {
|
fun autoFieldAdd(blackhole: Blackhole) = with(autoField) {
|
||||||
with(autoField) {
|
var res: StructureND<Double> = one
|
||||||
var res: StructureND<Double> = one
|
repeat(n) { res += one }
|
||||||
repeat(n) { res += one }
|
blackhole.consume(res)
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
fun specializedFieldAdd(blackhole: Blackhole) {
|
fun specializedFieldAdd(blackhole: Blackhole) = with(specializedField) {
|
||||||
with(specializedField) {
|
var res: StructureND<Double> = one
|
||||||
var res: StructureND<Double> = one
|
repeat(n) { res += 1.0 }
|
||||||
repeat(n) { res += 1.0 }
|
blackhole.consume(res)
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
fun boxingFieldAdd(blackhole: Blackhole) {
|
fun boxingFieldAdd(blackhole: Blackhole) = with(genericField) {
|
||||||
with(genericField) {
|
var res: StructureND<Double> = one
|
||||||
var res: StructureND<Double> = one
|
repeat(n) { res += 1.0 }
|
||||||
repeat(n) { res += 1.0 }
|
blackhole.consume(res)
|
||||||
blackhole.consume(res)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
fun tensorAdd(blackhole: Blackhole) = with(Double.tensorAlgebra) {
|
||||||
|
var res: DoubleTensor = ones(dim, dim)
|
||||||
|
repeat(n) { res = res + 1.0 }
|
||||||
|
blackhole.consume(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Benchmark
|
||||||
|
fun tensorInPlaceAdd(blackhole: Blackhole) = with(Double.tensorAlgebra) {
|
||||||
|
val res: DoubleTensor = ones(dim, dim)
|
||||||
|
repeat(n) { res += 1.0 }
|
||||||
|
blackhole.consume(res)
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Benchmark
|
||||||
|
// fun nd4jAdd(blackhole: Blackhole) = with(nd4jField) {
|
||||||
|
// var res: StructureND<Double> = one
|
||||||
|
// repeat(n) { res += 1.0 }
|
||||||
|
// blackhole.consume(res)
|
||||||
|
// }
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
private const val dim = 1000
|
private const val dim = 1000
|
||||||
private const val n = 100
|
private const val n = 100
|
||||||
private val autoField = DoubleField.autoNdAlgebra(dim, dim)
|
private val autoField = DoubleField.autoNdAlgebra(dim, dim)
|
||||||
private val specializedField = DoubleField.ndAlgebra(dim, dim)
|
private val specializedField = DoubleField.ndAlgebra(dim, dim)
|
||||||
private val genericField = DoubleField.ndAlgebra(Buffer.Companion::boxing, dim, dim)
|
private val genericField = DoubleField.ndAlgebra(Buffer.Companion::boxing, dim, dim)
|
||||||
|
private val nd4jField = DoubleField.nd4j(dim, dim)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ dependencies {
|
|||||||
api(project(":kmath-tensors"))
|
api(project(":kmath-tensors"))
|
||||||
api("org.nd4j:nd4j-api:1.0.0-M1")
|
api("org.nd4j:nd4j-api:1.0.0-M1")
|
||||||
testImplementation("org.nd4j:nd4j-native-platform:1.0.0-M1")
|
testImplementation("org.nd4j:nd4j-native-platform:1.0.0-M1")
|
||||||
testImplementation("org.slf4j:slf4j-simple:1.7.31")
|
testImplementation("org.slf4j:slf4j-simple:1.7.32")
|
||||||
}
|
}
|
||||||
|
|
||||||
readme {
|
readme {
|
||||||
|
@ -45,7 +45,6 @@ public sealed interface Nd4jArrayAlgebra<T, out C : Algebra<T>> : AlgebraND<T, C
|
|||||||
return struct
|
return struct
|
||||||
}
|
}
|
||||||
|
|
||||||
@PerformancePitfall
|
|
||||||
override fun StructureND<T>.map(transform: C.(T) -> T): Nd4jArrayStructure<T> {
|
override fun StructureND<T>.map(transform: C.(T) -> T): Nd4jArrayStructure<T> {
|
||||||
val newStruct = ndArray.dup().wrap()
|
val newStruct = ndArray.dup().wrap()
|
||||||
newStruct.elements().forEach { (idx, value) -> newStruct[idx] = elementContext.transform(value) }
|
newStruct.elements().forEach { (idx, value) -> newStruct[idx] = elementContext.transform(value) }
|
||||||
@ -265,6 +264,8 @@ public class DoubleNd4jArrayField(override val shape: IntArray) : Nd4jArrayExten
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public fun DoubleField.nd4j(vararg shape: Int): DoubleNd4jArrayField = DoubleNd4jArrayField(intArrayOf(*shape))
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents [FieldND] over [Nd4jArrayStructure] of [Float].
|
* Represents [FieldND] over [Nd4jArrayStructure] of [Float].
|
||||||
*/
|
*/
|
||||||
|
@ -158,7 +158,6 @@ public object DoubleNd4jTensorAlgebra : Nd4jTensorAlgebra<Double> {
|
|||||||
if (shape contentEquals intArrayOf(1)) ndArray.getDouble(0) else null
|
if (shape contentEquals intArrayOf(1)) ndArray.getDouble(0) else null
|
||||||
|
|
||||||
// TODO rewrite
|
// TODO rewrite
|
||||||
@PerformancePitfall
|
|
||||||
override fun diagonalEmbedding(
|
override fun diagonalEmbedding(
|
||||||
diagonalEntries: Tensor<Double>,
|
diagonalEntries: Tensor<Double>,
|
||||||
offset: Int,
|
offset: Int,
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
/*
|
||||||
|
* 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.tensors.core
|
||||||
|
|
||||||
|
public fun DoubleTensorAlgebra.ones(vararg shape: Int): DoubleTensor = ones(intArrayOf(*shape))
|
Loading…
Reference in New Issue
Block a user