Noa scopes runners

This commit is contained in:
Roland Grinis 2021-07-09 09:25:19 +01:00
parent 7744880ce7
commit 00a04a1931
3 changed files with 65 additions and 9 deletions

View File

@ -0,0 +1,49 @@
/*
* 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/LICENSE.txt file.
*/
package space.kscience.kmath.noa
import space.kscience.kmath.noa.memory.NoaScope
import space.kscience.kmath.noa.memory.withNoaScope
public class NoaDouble
internal constructor(scope: NoaScope) :
NoaDoubleAlgebra(scope)
public fun <R> NoaDouble(block: NoaDouble.() -> R): R? =
withNoaScope { NoaDouble(this).block() }
public fun <R> NoaDouble(scope: NoaScope, block: NoaDouble.() -> R): R? =
withNoaScope(scope) { NoaDouble(this).block() }
public class NoaFloat
internal constructor(scope: NoaScope) :
NoaFloatAlgebra(scope)
public fun <R> NoaFloat(block: NoaFloat.() -> R): R? =
withNoaScope { NoaFloat(this).block() }
public fun <R> NoaFloat(scope: NoaScope, block: NoaFloat.() -> R): R? =
withNoaScope(scope) { NoaFloat(this).block() }
public class NoaLong
internal constructor(scope: NoaScope) :
NoaLongAlgebra(scope)
public fun <R> NoaLong(block: NoaLong.() -> R): R? =
withNoaScope { NoaLong(this).block() }
public fun <R> NoaLong(scope: NoaScope, block: NoaLong.() -> R): R? =
withNoaScope(scope) { NoaLong(this).block() }
public class NoaInt
internal constructor(scope: NoaScope) :
NoaIntAlgebra(scope)
public fun <R> NoaInt(block: NoaInt.() -> R): R? =
withNoaScope { NoaInt(this).block() }
public fun <R> NoaInt(scope: NoaScope, block: NoaInt.() -> R): R? =
withNoaScope(scope) { NoaInt(this).block() }

View File

@ -15,7 +15,8 @@ import space.kscience.kmath.tensors.core.TensorLinearStructure
public sealed class NoaAlgebra<T, TensorType : NoaTensor<T>>
constructor(protected val scope: NoaScope) : TensorAlgebra<T> {
protected constructor(protected val scope: NoaScope) :
TensorAlgebra<T> {
protected abstract val Tensor<T>.tensor: TensorType
@ -132,8 +133,10 @@ constructor(protected val scope: NoaScope) : TensorAlgebra<T> {
}
public abstract class NoaPartialDivisionAlgebra<T, TensorType : NoaTensor<T>>
internal constructor(scope: NoaScope) : NoaAlgebra<T, TensorType>(scope), LinearOpsTensorAlgebra<T>,
public sealed class NoaPartialDivisionAlgebra<T, TensorType : NoaTensor<T>>
protected constructor(scope: NoaScope) :
NoaAlgebra<T, TensorType>(scope),
LinearOpsTensorAlgebra<T>,
AnalyticTensorAlgebra<T> {
override operator fun Tensor<T>.div(other: Tensor<T>): TensorType {
@ -274,7 +277,8 @@ internal constructor(scope: NoaScope) : NoaAlgebra<T, TensorType>(scope), Linear
}
public class NoaDoubleAlgebra(scope: NoaScope) :
public sealed class NoaDoubleAlgebra
protected constructor(scope: NoaScope) :
NoaPartialDivisionAlgebra<Double, NoaDoubleTensor>(scope) {
private fun Tensor<Double>.castHelper(): NoaDoubleTensor =
@ -349,7 +353,8 @@ public class NoaDoubleAlgebra(scope: NoaScope) :
}
public class NoaFloatAlgebra(scope: NoaScope) :
public sealed class NoaFloatAlgebra
protected constructor(scope: NoaScope) :
NoaPartialDivisionAlgebra<Float, NoaFloatTensor>(scope) {
private fun Tensor<Float>.castHelper(): NoaFloatTensor =
@ -424,7 +429,8 @@ public class NoaFloatAlgebra(scope: NoaScope) :
}
public class NoaLongAlgebra(scope: NoaScope) :
public sealed class NoaLongAlgebra
protected constructor(scope: NoaScope) :
NoaAlgebra<Long, NoaLongTensor>(scope) {
private fun Tensor<Long>.castHelper(): NoaLongTensor =
@ -484,7 +490,8 @@ public class NoaLongAlgebra(scope: NoaScope) :
}
public class NoaIntAlgebra(scope: NoaScope) :
public sealed class NoaIntAlgebra
protected constructor(scope: NoaScope) :
NoaAlgebra<Int, NoaIntTensor>(scope) {
private fun Tensor<Int>.castHelper(): NoaIntTensor =

View File

@ -30,14 +30,14 @@ public class NoaScope {
}
}
internal fun <R> withNoaScope(block: NoaScope.() -> R): R? {
internal inline fun <R> withNoaScope(block: NoaScope.() -> R): R? {
val noaScope = NoaScope()
val result = try { noaScope.block() } catch (e: Throwable) { null }
noaScope.disposeAll()
return result
}
internal fun <R> withNoaScope(scope: NoaScope, block: NoaScope.() -> R): R? {
internal inline fun <R> withNoaScope(scope: NoaScope, block: NoaScope.() -> R): R? {
val noaScope = NoaScope()
val result = try { noaScope.block() } catch (e: Throwable) { null }
if (result == null){