Viktor prototype with inline
This commit is contained in:
parent
e42058e571
commit
9936b9e4b7
@ -4,12 +4,14 @@ import org.jetbrains.bio.viktor.F64Array
|
|||||||
import scientifik.kmath.operations.RealField
|
import scientifik.kmath.operations.RealField
|
||||||
import scientifik.kmath.viktor.ViktorNDField
|
import scientifik.kmath.viktor.ViktorNDField
|
||||||
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
val dim = 1000
|
val dim = 1000
|
||||||
val n = 400
|
val n = 400
|
||||||
|
|
||||||
// automatically build context most suited for given type.
|
// automatically build context most suited for given type.
|
||||||
val autoField = NDField.auto(RealField, dim, dim)
|
val autoField = NDField.auto(RealField, dim, dim)
|
||||||
|
val realField = NDField.real(dim,dim)
|
||||||
|
|
||||||
val viktorField = ViktorNDField(intArrayOf(dim, dim))
|
val viktorField = ViktorNDField(intArrayOf(dim, dim))
|
||||||
|
|
||||||
@ -52,4 +54,23 @@ fun main() {
|
|||||||
res = res + one
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -6,14 +6,15 @@ import scientifik.kmath.structures.DefaultStrides
|
|||||||
import scientifik.kmath.structures.MutableNDStructure
|
import scientifik.kmath.structures.MutableNDStructure
|
||||||
import scientifik.kmath.structures.NDField
|
import scientifik.kmath.structures.NDField
|
||||||
|
|
||||||
|
@Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
|
||||||
inline class ViktorNDStructure(val f64Buffer: F64Array) : MutableNDStructure<Double> {
|
inline class ViktorNDStructure(val f64Buffer: F64Array) : MutableNDStructure<Double> {
|
||||||
|
|
||||||
override val shape: IntArray get() = f64Buffer.shape
|
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) {
|
override inline fun set(index: IntArray, value: Double) {
|
||||||
f64Buffer.set(value = value, indices = *index)
|
f64Buffer.set(*index, value = value)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun elements(): Sequence<Pair<IntArray, Double>> {
|
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)
|
fun F64Array.asStructure(): ViktorNDStructure = ViktorNDStructure(this)
|
||||||
|
|
||||||
|
@Suppress("OVERRIDE_BY_INLINE", "NOTHING_TO_INLINE")
|
||||||
class ViktorNDField(override val shape: IntArray) : NDField<Double, RealField, ViktorNDStructure> {
|
class ViktorNDField(override val shape: IntArray) : NDField<Double, RealField, ViktorNDStructure> {
|
||||||
override val zero: ViktorNDStructure
|
override val zero: ViktorNDStructure
|
||||||
get() = F64Array.full(init = 0.0, shape = *shape).asStructure()
|
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()
|
return (a.f64Buffer + b.f64Buffer).asStructure()
|
||||||
}
|
}
|
||||||
|
|
||||||
override inline fun ViktorNDStructure.plus(b: ViktorNDStructure): ViktorNDStructure {
|
override fun multiply(a: ViktorNDStructure, k: Number): ViktorNDStructure =
|
||||||
return (f64Buffer + b.f64Buffer).asStructure()
|
(a.f64Buffer * k.toDouble()).asStructure()
|
||||||
}
|
|
||||||
|
|
||||||
override inline fun ViktorNDStructure.minus(b: ViktorNDStructure): ViktorNDStructure {
|
override inline fun ViktorNDStructure.plus(b: ViktorNDStructure): ViktorNDStructure =
|
||||||
return (f64Buffer - b.f64Buffer).asStructure()
|
(f64Buffer + b.f64Buffer).asStructure()
|
||||||
}
|
|
||||||
|
|
||||||
|
override inline fun ViktorNDStructure.minus(b: ViktorNDStructure): ViktorNDStructure =
|
||||||
|
(f64Buffer - b.f64Buffer).asStructure()
|
||||||
|
|
||||||
override fun multiply(a: ViktorNDStructure, k: Number): ViktorNDStructure {
|
override inline fun ViktorNDStructure.times(k: Number): ViktorNDStructure = (f64Buffer * k.toDouble()).asStructure()
|
||||||
return (a.f64Buffer * k.toDouble()).asStructure()
|
|
||||||
}
|
override inline fun ViktorNDStructure.plus(arg: Double): ViktorNDStructure = (f64Buffer.plus(arg)).asStructure()
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user