Cleanup after nd optimization phase II

This commit is contained in:
Alexander Nozik 2019-01-08 20:23:24 +03:00
parent 876a363e0b
commit f356e8956b
3 changed files with 63 additions and 61 deletions

View File

@ -16,54 +16,54 @@ fun main(args: Array<String>) {
//A generic boxing field. It should be used for objects, not primitives. //A generic boxing field. It should be used for objects, not primitives.
val genericField = NDField.buffered(intArrayOf(dim, dim), RealField) val genericField = NDField.buffered(intArrayOf(dim, dim), RealField)
//
// val autoTime = measureTimeMillis { val autoTime = measureTimeMillis {
// autoField.run { autoField.run {
// var res = one var res = one
// repeat(n) { repeat(n) {
// res += 1.0 res += 1.0
// } }
// } }
// } }
//
// println("Buffered addition completed in $autoTime millis") println("Buffered addition completed in $autoTime millis")
//
// val elementTime = measureTimeMillis { val elementTime = measureTimeMillis {
// var res = genericField.one var res = genericField.one
// repeat(n) { repeat(n) {
// res += 1.0 res += 1.0
// } }
// } }
//
// println("Element addition completed in $elementTime millis") println("Element addition completed in $elementTime millis")
//
// val specializedTime = measureTimeMillis { val specializedTime = measureTimeMillis {
// specializedField.run { specializedField.run {
// var res:NDBuffer<Double> = one var res:NDBuffer<Double> = one
// repeat(n) { repeat(n) {
// res += 1.0 res += 1.0
// } }
// } }
// } }
//
// println("Specialized addition completed in $specializedTime millis") println("Specialized addition completed in $specializedTime millis")
//
//
// val lazyTime = measureTimeMillis { val lazyTime = measureTimeMillis {
// lazyField.run { lazyField.run {
// val res = one.map { val res = one.map {
// var c = 0.0 var c = 0.0
// repeat(n) { repeat(n) {
// c += 1.0 c += 1.0
// } }
// c c
// } }
//
// res.elements().forEach { it.second } res.elements().forEach { it.second }
// } }
// } }
//
// println("Lazy addition completed in $lazyTime millis") println("Lazy addition completed in $lazyTime millis")
val genericTime = measureTimeMillis { val genericTime = measureTimeMillis {
//genericField.run(action) //genericField.run(action)

View File

@ -65,14 +65,12 @@ interface NDSpace<T, S : Space<T>, N : NDStructure<T>> : Space<N>, NDAlgebra<T,
/** /**
* Element-by-element addition * Element-by-element addition
*/ */
override fun add(a: N, b: N): N = override fun add(a: N, b: N): N = combine(a, b) { aValue, bValue -> add(aValue, bValue) }
combine(a, b) { aValue, bValue -> add(aValue, bValue) }
/** /**
* Multiply all elements by constant * Multiply all elements by constant
*/ */
override fun multiply(a: N, k: Double): N = override fun multiply(a: N, k: Double): N = map(a) { multiply(it, k) }
map(a) { multiply(it, k) }
operator fun N.plus(arg: T) = map(this) { value -> add(arg, value) } operator fun N.plus(arg: T) = map(this) { value -> add(arg, value) }
operator fun N.minus(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 * Element-by-element multiplication
*/ */
override fun multiply(a: N, b: N): N = override fun multiply(a: N, b: N): N = combine(a, b) { aValue, bValue -> multiply(aValue, bValue) }
combine(a, b) { aValue, bValue -> aValue * bValue }
operator fun N.times(arg: T) = map(this) { value -> multiply(arg, value) } 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) } 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 * Element-by-element division
*/ */
override fun divide(a: N, b: N): N = override fun divide(a: N, b: N): N = combine(a, b) { aValue, bValue -> divide(aValue, bValue) }
combine(a, b) { aValue, bValue -> aValue / bValue }
operator fun N.div(arg: T) = map(this) { value -> arg / value } operator fun N.div(arg: T) = map(this) { value -> divide(arg, value) }
operator fun T.div(arg: N) = arg / this operator fun T.div(arg: N) = map(arg) { divide(it, this@div) }
companion object { 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 * Create a nd-field with boxing generic buffer
*/ */
fun <T : Any, F : Field<T>> buffered(shape: IntArray, field: F) = fun <T : Any, F : Field<T>> buffered(
BoxingNDField(shape, field, Buffer.Companion::boxing) 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. * Create a most suitable implementation for nd-field using reified class.

View File

@ -5,7 +5,9 @@ import scientifik.kmath.operations.RealField
import scientifik.kmath.operations.Ring import scientifik.kmath.operations.Ring
import scientifik.kmath.operations.Space 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> { interface NDElement<T, C, N : NDStructure<T>> : NDStructure<T> {
val context: NDAlgebra<T, C, N> val context: NDAlgebra<T, C, N>