Viktor prototype with inline

This commit is contained in:
Alexander Nozik 2019-12-09 17:37:17 +03:00
parent e42058e571
commit 9936b9e4b7
2 changed files with 35 additions and 12 deletions

View File

@ -4,12 +4,14 @@ import org.jetbrains.bio.viktor.F64Array
import scientifik.kmath.operations.RealField
import scientifik.kmath.viktor.ViktorNDField
fun main() {
val dim = 1000
val n = 400
// automatically build context most suited for given type.
val autoField = NDField.auto(RealField, dim, dim)
val realField = NDField.real(dim,dim)
val viktorField = ViktorNDField(intArrayOf(dim, dim))
@ -52,4 +54,23 @@ fun main() {
res = res + one
}
}
measureAndPrint("Automatic field log") {
realField.run {
val fortyTwo = produce { 42.0 }
var res = one
repeat(n) {
res = ln(fortyTwo)
}
}
}
measureAndPrint("Raw Viktor log") {
val fortyTwo = F64Array.full(dim, dim, init = 42.0)
var res: F64Array
repeat(n) {
res = fortyTwo.log()
}
}
}

View File

@ -6,14 +6,15 @@ import scientifik.kmath.structures.DefaultStrides
import scientifik.kmath.structures.MutableNDStructure
import scientifik.kmath.structures.NDField
@Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
inline class ViktorNDStructure(val f64Buffer: F64Array) : MutableNDStructure<Double> {
override val shape: IntArray get() = f64Buffer.shape
override fun get(index: IntArray): Double = f64Buffer.get(*index)
override inline fun get(index: IntArray): Double = f64Buffer.get(*index)
override fun set(index: IntArray, value: Double) {
f64Buffer.set(value = value, indices = *index)
override inline fun set(index: IntArray, value: Double) {
f64Buffer.set(*index, value = value)
}
override fun elements(): Sequence<Pair<IntArray, Double>> {
@ -23,6 +24,7 @@ inline class ViktorNDStructure(val f64Buffer: F64Array) : MutableNDStructure<Dou
fun F64Array.asStructure(): ViktorNDStructure = ViktorNDStructure(this)
@Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
class ViktorNDField(override val shape: IntArray) : NDField<Double, RealField, ViktorNDStructure> {
override val zero: ViktorNDStructure
get() = F64Array.full(init = 0.0, shape = *shape).asStructure()
@ -69,16 +71,16 @@ class ViktorNDField(override val shape: IntArray) : NDField<Double, RealField, V
return (a.f64Buffer + b.f64Buffer).asStructure()
}
override inline fun ViktorNDStructure.plus(b: ViktorNDStructure): ViktorNDStructure {
return (f64Buffer + b.f64Buffer).asStructure()
}
override fun multiply(a: ViktorNDStructure, k: Number): ViktorNDStructure =
(a.f64Buffer * k.toDouble()).asStructure()
override inline fun ViktorNDStructure.minus(b: ViktorNDStructure): ViktorNDStructure {
return (f64Buffer - b.f64Buffer).asStructure()
}
override inline fun ViktorNDStructure.plus(b: ViktorNDStructure): ViktorNDStructure =
(f64Buffer + b.f64Buffer).asStructure()
override inline fun ViktorNDStructure.minus(b: ViktorNDStructure): ViktorNDStructure =
(f64Buffer - b.f64Buffer).asStructure()
override fun multiply(a: ViktorNDStructure, k: Number): ViktorNDStructure {
return (a.f64Buffer * k.toDouble()).asStructure()
}
override inline fun ViktorNDStructure.times(k: Number): ViktorNDStructure = (f64Buffer * k.toDouble()).asStructure()
override inline fun ViktorNDStructure.plus(arg: Double): ViktorNDStructure = (f64Buffer.plus(arg)).asStructure()
}