From 78b1cd41da179b1ef5adeb759a75cf091040afcd Mon Sep 17 00:00:00 2001 From: Roland Grinis Date: Wed, 7 Jul 2021 11:58:48 +0100 Subject: [PATCH] disposable pattern --- kmath-noa/build.gradle.kts | 2 +- .../java/space/kscience/kmath/noa/JNoa.java | 5 ++- .../space/kscience/kmath/noa/algebras.kt | 8 +++++ .../kscience/kmath/noa/memory/NoaResource.kt | 15 ++++++++ .../kscience/kmath/noa/memory/NoaScope.kt | 34 +++++++++++++++++++ .../space/kscience/kmath/noa/tensors.kt | 23 +++++++++++++ .../resources/space_kscience_kmath_noa_JNoa.h | 8 +++++ .../space/kscience/kmath/noa/TestUtils.kt | 4 +++ 8 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 kmath-noa/src/main/kotlin/space/kscience/kmath/noa/algebras.kt create mode 100644 kmath-noa/src/main/kotlin/space/kscience/kmath/noa/memory/NoaResource.kt create mode 100644 kmath-noa/src/main/kotlin/space/kscience/kmath/noa/memory/NoaScope.kt create mode 100644 kmath-noa/src/main/kotlin/space/kscience/kmath/noa/tensors.kt diff --git a/kmath-noa/build.gradle.kts b/kmath-noa/build.gradle.kts index 175bd9133..57b9fe6d0 100644 --- a/kmath-noa/build.gradle.kts +++ b/kmath-noa/build.gradle.kts @@ -15,7 +15,7 @@ plugins { description = "Wrapper for the Bayesian Computation library NOA on top of LibTorch" dependencies { - implementation(project(":kmath-tensors")) + api(project(":kmath-tensors")) } val home: String = System.getProperty("user.home") diff --git a/kmath-noa/src/main/java/space/kscience/kmath/noa/JNoa.java b/kmath-noa/src/main/java/space/kscience/kmath/noa/JNoa.java index 91396285b..c75d385e7 100644 --- a/kmath-noa/src/main/java/space/kscience/kmath/noa/JNoa.java +++ b/kmath-noa/src/main/java/space/kscience/kmath/noa/JNoa.java @@ -5,7 +5,7 @@ package space.kscience.kmath.noa; -public class JNoa { +class JNoa { static { String jNoaPath = System.getProperty("user.home") + @@ -30,5 +30,8 @@ public class JNoa { public static native void setNumThreads(int numThreads); public static native void setSeed(int seed); + + public static native void disposeTensor(long tensorHandle); + } diff --git a/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/algebras.kt b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/algebras.kt new file mode 100644 index 000000000..d36173264 --- /dev/null +++ b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/algebras.kt @@ -0,0 +1,8 @@ +/* + * 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 + +public sealed class NoaAlgebra{} \ No newline at end of file diff --git a/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/memory/NoaResource.kt b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/memory/NoaResource.kt new file mode 100644 index 000000000..c4e4e7c6e --- /dev/null +++ b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/memory/NoaResource.kt @@ -0,0 +1,15 @@ +/* + * 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.memory + +public abstract class NoaResource +internal constructor(scope: NoaScope) { + init { + scope.add(::dispose) + } + + protected abstract fun dispose(): Unit +} \ No newline at end of file diff --git a/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/memory/NoaScope.kt b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/memory/NoaScope.kt new file mode 100644 index 000000000..26f36b934 --- /dev/null +++ b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/memory/NoaScope.kt @@ -0,0 +1,34 @@ +/* + * 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.memory + +private typealias Disposable = () -> Unit + +public class NoaScope { + + private val disposables: ArrayDeque = ArrayDeque(0) + + public fun disposeAll() { + disposables.forEach(Disposable::invoke) + disposables.clear() + } + + internal inline fun add(crossinline disposable: Disposable) { + disposables += { + try { + disposable() + } catch (ignored: Throwable) { + } + } + } +} + +internal inline fun withNoaScope(i: Int, block: NoaScope.() -> R): R { + val noaScope = NoaScope() + val result = noaScope.block() + noaScope.disposeAll() + return result +} \ No newline at end of file diff --git a/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/tensors.kt b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/tensors.kt new file mode 100644 index 000000000..1c3287a45 --- /dev/null +++ b/kmath-noa/src/main/kotlin/space/kscience/kmath/noa/tensors.kt @@ -0,0 +1,23 @@ +/* + * 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.NoaResource +import space.kscience.kmath.noa.memory.NoaScope +import space.kscience.kmath.tensors.api.Tensor + +internal typealias TensorHandle = Long + +public sealed class NoaTensor +constructor(scope: NoaScope, internal val tensorHandle: TensorHandle) : + NoaResource(scope){ + + override fun dispose(): Unit = JNoa.disposeTensor(tensorHandle) +} + +public class NoaDoubleTensor +internal constructor(scope: NoaScope, tensorHandle: TensorHandle) : + NoaTensor(scope, tensorHandle) diff --git a/kmath-noa/src/main/resources/space_kscience_kmath_noa_JNoa.h b/kmath-noa/src/main/resources/space_kscience_kmath_noa_JNoa.h index fb7480fa5..33905098f 100644 --- a/kmath-noa/src/main/resources/space_kscience_kmath_noa_JNoa.h +++ b/kmath-noa/src/main/resources/space_kscience_kmath_noa_JNoa.h @@ -47,6 +47,14 @@ JNIEXPORT void JNICALL Java_space_kscience_kmath_noa_JNoa_setNumThreads JNIEXPORT void JNICALL Java_space_kscience_kmath_noa_JNoa_setSeed (JNIEnv *, jclass, jint); +/* + * Class: space_kscience_kmath_noa_JNoa + * Method: disposeTensor + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_space_kscience_kmath_noa_JNoa_disposeTensor + (JNIEnv *, jclass, jlong); + #ifdef __cplusplus } #endif diff --git a/kmath-noa/src/test/kotlin/space/kscience/kmath/noa/TestUtils.kt b/kmath-noa/src/test/kotlin/space/kscience/kmath/noa/TestUtils.kt index 6b8386b8d..8d597e85e 100644 --- a/kmath-noa/src/test/kotlin/space/kscience/kmath/noa/TestUtils.kt +++ b/kmath-noa/src/test/kotlin/space/kscience/kmath/noa/TestUtils.kt @@ -5,6 +5,8 @@ package space.kscience.kmath.noa +import space.kscience.kmath.noa.memory.NoaScope +import space.kscience.kmath.noa.memory.withNoaScope import kotlin.test.Test import kotlin.test.assertEquals @@ -26,4 +28,6 @@ class TestUtils { setNumThreads(numThreads) assertEquals(numThreads, getNumThreads()) } + + } \ No newline at end of file