Cleanup after nd optimization phase II
This commit is contained in:
parent
876a363e0b
commit
f356e8956b
@ -16,54 +16,54 @@ fun main(args: Array<String>) {
|
||||
//A generic boxing field. It should be used for objects, not primitives.
|
||||
val genericField = NDField.buffered(intArrayOf(dim, dim), RealField)
|
||||
|
||||
//
|
||||
// val autoTime = measureTimeMillis {
|
||||
// autoField.run {
|
||||
// var res = one
|
||||
// repeat(n) {
|
||||
// res += 1.0
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// println("Buffered addition completed in $autoTime millis")
|
||||
//
|
||||
// val elementTime = measureTimeMillis {
|
||||
// var res = genericField.one
|
||||
// repeat(n) {
|
||||
// res += 1.0
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// println("Element addition completed in $elementTime millis")
|
||||
//
|
||||
// val specializedTime = measureTimeMillis {
|
||||
// specializedField.run {
|
||||
// var res:NDBuffer<Double> = one
|
||||
// repeat(n) {
|
||||
// res += 1.0
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// println("Specialized addition completed in $specializedTime millis")
|
||||
//
|
||||
//
|
||||
// val lazyTime = measureTimeMillis {
|
||||
// lazyField.run {
|
||||
// val res = one.map {
|
||||
// var c = 0.0
|
||||
// repeat(n) {
|
||||
// c += 1.0
|
||||
// }
|
||||
// c
|
||||
// }
|
||||
//
|
||||
// res.elements().forEach { it.second }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// println("Lazy addition completed in $lazyTime millis")
|
||||
|
||||
val autoTime = measureTimeMillis {
|
||||
autoField.run {
|
||||
var res = one
|
||||
repeat(n) {
|
||||
res += 1.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println("Buffered addition completed in $autoTime millis")
|
||||
|
||||
val elementTime = measureTimeMillis {
|
||||
var res = genericField.one
|
||||
repeat(n) {
|
||||
res += 1.0
|
||||
}
|
||||
}
|
||||
|
||||
println("Element addition completed in $elementTime millis")
|
||||
|
||||
val specializedTime = measureTimeMillis {
|
||||
specializedField.run {
|
||||
var res:NDBuffer<Double> = one
|
||||
repeat(n) {
|
||||
res += 1.0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println("Specialized addition completed in $specializedTime millis")
|
||||
|
||||
|
||||
val lazyTime = measureTimeMillis {
|
||||
lazyField.run {
|
||||
val res = one.map {
|
||||
var c = 0.0
|
||||
repeat(n) {
|
||||
c += 1.0
|
||||
}
|
||||
c
|
||||
}
|
||||
|
||||
res.elements().forEach { it.second }
|
||||
}
|
||||
}
|
||||
|
||||
println("Lazy addition completed in $lazyTime millis")
|
||||
|
||||
val genericTime = measureTimeMillis {
|
||||
//genericField.run(action)
|
||||
|
@ -65,14 +65,12 @@ interface NDSpace<T, S : Space<T>, N : NDStructure<T>> : Space<N>, NDAlgebra<T,
|
||||
/**
|
||||
* Element-by-element addition
|
||||
*/
|
||||
override fun add(a: N, b: N): N =
|
||||
combine(a, b) { aValue, bValue -> add(aValue, bValue) }
|
||||
override fun add(a: N, b: N): N = combine(a, b) { aValue, bValue -> add(aValue, bValue) }
|
||||
|
||||
/**
|
||||
* Multiply all elements by constant
|
||||
*/
|
||||
override fun multiply(a: N, k: Double): N =
|
||||
map(a) { multiply(it, k) }
|
||||
override fun multiply(a: N, k: Double): N = map(a) { multiply(it, k) }
|
||||
|
||||
operator fun N.plus(arg: T) = map(this) { value -> add(arg, value) }
|
||||
operator fun N.minus(arg: T) = map(this) { value -> add(arg, -value) }
|
||||
@ -89,8 +87,7 @@ interface NDRing<T, R : Ring<T>, N : NDStructure<T>> : Ring<N>, NDSpace<T, R, N>
|
||||
/**
|
||||
* Element-by-element multiplication
|
||||
*/
|
||||
override fun multiply(a: N, b: N): N =
|
||||
combine(a, b) { aValue, bValue -> aValue * bValue }
|
||||
override fun multiply(a: N, b: N): N = combine(a, b) { aValue, bValue -> multiply(aValue, bValue) }
|
||||
|
||||
operator fun N.times(arg: T) = map(this) { value -> multiply(arg, value) }
|
||||
operator fun T.times(arg: N) = map(arg) { value -> multiply(this@times, value) }
|
||||
@ -109,11 +106,10 @@ interface NDField<T, F : Field<T>, N : NDStructure<T>> : Field<N>, NDRing<T, F,
|
||||
/**
|
||||
* Element-by-element division
|
||||
*/
|
||||
override fun divide(a: N, b: N): N =
|
||||
combine(a, b) { aValue, bValue -> aValue / bValue }
|
||||
override fun divide(a: N, b: N): N = combine(a, b) { aValue, bValue -> divide(aValue, bValue) }
|
||||
|
||||
operator fun N.div(arg: T) = map(this) { value -> arg / value }
|
||||
operator fun T.div(arg: N) = arg / this
|
||||
operator fun N.div(arg: T) = map(this) { value -> divide(arg, value) }
|
||||
operator fun T.div(arg: N) = map(arg) { divide(it, this@div) }
|
||||
|
||||
companion object {
|
||||
/**
|
||||
@ -124,8 +120,12 @@ interface NDField<T, F : Field<T>, N : NDStructure<T>> : Field<N>, NDRing<T, F,
|
||||
/**
|
||||
* Create a nd-field with boxing generic buffer
|
||||
*/
|
||||
fun <T : Any, F : Field<T>> buffered(shape: IntArray, field: F) =
|
||||
BoxingNDField(shape, field, Buffer.Companion::boxing)
|
||||
fun <T : Any, F : Field<T>> buffered(
|
||||
shape: IntArray,
|
||||
field: F,
|
||||
bufferFactory: BufferFactory<T> = Buffer.Companion::boxing
|
||||
) =
|
||||
BoxingNDField(shape, field, bufferFactory)
|
||||
|
||||
/**
|
||||
* Create a most suitable implementation for nd-field using reified class.
|
||||
|
@ -5,7 +5,9 @@ import scientifik.kmath.operations.RealField
|
||||
import scientifik.kmath.operations.Ring
|
||||
import scientifik.kmath.operations.Space
|
||||
|
||||
|
||||
/**
|
||||
* The root for all [NDStructure] based algebra elements. Does not implement algebra element root because of problems with recursive self-types
|
||||
*/
|
||||
interface NDElement<T, C, N : NDStructure<T>> : NDStructure<T> {
|
||||
|
||||
val context: NDAlgebra<T, C, N>
|
||||
|
Loading…
Reference in New Issue
Block a user