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