Remove kotlinx-io dependency, use DeferScope to dispose native memory

This commit is contained in:
Iaroslav Postovalov 2020-10-11 21:24:10 +07:00
parent 3c069398a2
commit 3ae2be06e2
No known key found for this signature in database
GPG Key ID: 46E15E4A31B3BCD7
12 changed files with 155 additions and 112 deletions

View File

@ -12,7 +12,6 @@ allprojects {
maven("https://dl.bintray.com/kotlin/kotlin-eap")
maven("https://dl.bintray.com/kotlin/kotlinx")
maven("https://dl.bintray.com/hotkeytlt/maven")
maven("https://dl.bintray.com/commandertvis/kotlinx-io/")
}
group = "kscience.kmath"

View File

@ -19,8 +19,9 @@ private fun KtPsiFactory.createMatrixClass(
@Language("kotlin") val text = """internal class $className(
override val nativeHandle: CPointer<$structName>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<$kotlinTypeName, $structName>() {
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<$kotlinTypeName, $structName>(scope) {
override val rowNum: Int
get() = nativeHandle.pointed.size1.toInt()
@ -30,7 +31,7 @@ private fun KtPsiFactory.createMatrixClass(
override val features: Set<MatrixFeature> = features
override fun suggestFeature(vararg features: MatrixFeature): $className =
${className}(nativeHandle, this.features + features)
${className}(nativeHandle, this.features + features, scope)
override operator fun get(i: Int, j: Int): $kotlinTypeName = ${
fn("gsl_matrixRget", cTypeName)
@ -42,7 +43,7 @@ private fun KtPsiFactory.createMatrixClass(
override fun copy(): $className {
val new = requireNotNull(${fn("gsl_matrixRalloc", cTypeName)}(rowNum.toULong(), colNum.toULong()))
${fn("gsl_matrixRmemcpy", cTypeName)}(new, nativeHandle)
return $className(new, features)
return $className(new, features, scope)
}
override fun close(): Unit = ${fn("gsl_matrixRfree", cTypeName)}(nativeHandle)

View File

@ -18,7 +18,8 @@ private fun KtPsiFactory.createVectorClass(
val structName = sn("gsl_vectorR", cTypeName)
@Language("kotlin") val text =
"""internal class $className(override val nativeHandle: CPointer<$structName>) : GslVector<$kotlinTypeName, $structName>() {
"""internal class $className(override val nativeHandle: CPointer<$structName>, scope: DeferScope)
: GslVector<$kotlinTypeName, $structName>(scope) {
override val size: Int
get() = nativeHandle.pointed.size.toInt()
@ -30,7 +31,7 @@ private fun KtPsiFactory.createVectorClass(
override fun copy(): $className {
val new = requireNotNull(${fn("gsl_vectorRalloc", cTypeName)}(size.toULong()))
${fn("gsl_vectorRmemcpy", cTypeName)}(new, nativeHandle)
return ${className}(new)
return ${className}(new, scope)
}
override fun equals(other: Any?): Boolean {

View File

@ -8,6 +8,8 @@ plugins {
}
kotlin {
explicitApiWarning()
val nativeTarget = when (System.getProperty("os.name")) {
"Mac OS X" -> macosX64("native")
"Linux" -> linuxX64("native")
@ -20,7 +22,11 @@ kotlin {
val main by nativeTarget.compilations.getting {
cinterops {
val libgsl by creating { includeDirs { headerFilterOnly("/usr/include/", "/usr/local/") } }
val libgsl by creating {
includeDirs {
headerFilterOnly("/usr/include/", "/usr/local/")
}
}
}
}
@ -35,7 +41,6 @@ kotlin {
dependencies {
api(project(":kmath-core"))
api("org.jetbrains.kotlinx:kotlinx-io:0.2.0-tvis-3")
}
}
}

View File

@ -14,8 +14,9 @@ internal fun Complex.toGsl(): CValue<gsl_complex> = cValue {
internal class GslComplexMatrix(
override val nativeHandle: CPointer<gsl_matrix_complex>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Complex, gsl_matrix_complex>() {
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<Complex, gsl_matrix_complex>(scope) {
override val rowNum: Int
get() = nativeHandle.pointed.size1.toInt()
@ -25,7 +26,7 @@ internal class GslComplexMatrix(
override val features: Set<MatrixFeature> = features
override fun suggestFeature(vararg features: MatrixFeature): GslComplexMatrix =
GslComplexMatrix(nativeHandle, this.features + features)
GslComplexMatrix(nativeHandle, this.features + features, scope)
override operator fun get(i: Int, j: Int): Complex =
gsl_matrix_complex_get(nativeHandle, i.toULong(), j.toULong()).toKMath()
@ -36,7 +37,7 @@ internal class GslComplexMatrix(
override fun copy(): GslComplexMatrix {
val new = requireNotNull(gsl_matrix_complex_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_complex_memcpy(new, nativeHandle)
return GslComplexMatrix(new, features)
return GslComplexMatrix(new, features, scope)
}
override fun close(): Unit = gsl_matrix_complex_free(nativeHandle)
@ -47,18 +48,20 @@ internal class GslComplexMatrix(
}
}
internal class GslComplexVector(override val nativeHandle: CPointer<gsl_vector_complex>) :
GslVector<Complex, gsl_vector_complex>() {
internal class GslComplexVector(override val nativeHandle: CPointer<gsl_vector_complex>, scope: DeferScope) :
GslVector<Complex, gsl_vector_complex>(scope) {
override val size: Int
get() = nativeHandle.pointed.size.toInt()
override fun get(index: Int): Complex = gsl_vector_complex_get(nativeHandle, index.toULong()).toKMath()
override fun set(index: Int, value: Complex): Unit = gsl_vector_complex_set(nativeHandle, index.toULong(), value.toGsl())
override fun set(index: Int, value: Complex): Unit =
gsl_vector_complex_set(nativeHandle, index.toULong(), value.toGsl())
override fun copy(): GslComplexVector {
val new = requireNotNull(gsl_vector_complex_alloc(size.toULong()))
gsl_vector_complex_memcpy(new, nativeHandle)
return GslComplexVector(new)
return GslComplexVector(new, scope)
}
override fun equals(other: Any?): Boolean {

View File

@ -1,10 +1,12 @@
package kscience.kmath.gsl
import kotlinx.cinterop.CStructVar
import kotlinx.cinterop.DeferScope
import kscience.kmath.linear.FeaturedMatrix
import kscience.kmath.structures.NDStructure
public abstract class GslMatrix<T : Any, H : CStructVar> internal constructor(): GslMemoryHolder<H>(),
public abstract class GslMatrix<T : Any, H : CStructVar> internal constructor(scope: DeferScope) :
GslMemoryHolder<H>(scope),
FeaturedMatrix<T> {
internal abstract operator fun set(i: Int, j: Int, value: T)
internal abstract fun copy(): GslMatrix<T, H>

View File

@ -1,6 +1,7 @@
package kscience.kmath.gsl
import kotlinx.cinterop.CStructVar
import kotlinx.cinterop.DeferScope
import kotlinx.cinterop.pointed
import kscience.kmath.linear.MatrixContext
import kscience.kmath.linear.Point
@ -18,8 +19,9 @@ internal inline fun <T : Any, H : CStructVar> GslMatrix<T, H>.fill(initializer:
internal inline fun <T : Any, H : CStructVar> GslVector<T, H>.fill(initializer: (Int) -> T): GslVector<T, H> =
apply { (0 until size).forEach { index -> this[index] = initializer(index) } }
public abstract class GslMatrixContext<T : Any, H1 : CStructVar, H2 : CStructVar> internal constructor() :
MatrixContext<T> {
public abstract class GslMatrixContext<T : Any, H1 : CStructVar, H2 : CStructVar> internal constructor(
internal val scope: DeferScope
) : MatrixContext<T> {
@Suppress("UNCHECKED_CAST")
public fun Matrix<T>.toGsl(): GslMatrix<T, H1> = (if (this is GslMatrix<*, *>)
this as GslMatrix<T, H1>
@ -37,19 +39,19 @@ public abstract class GslMatrixContext<T : Any, H1 : CStructVar, H2 : CStructVar
produceDirtyMatrix(rows, columns).fill(initializer)
}
public object GslRealMatrixContext : GslMatrixContext<Double, gsl_matrix, gsl_vector>() {
public class GslRealMatrixContext(scope: DeferScope) : GslMatrixContext<Double, gsl_matrix, gsl_vector>(scope) {
override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix<Double, gsl_matrix> =
GslRealMatrix(requireNotNull(gsl_matrix_alloc(rows.toULong(), columns.toULong())))
GslRealMatrix(nativeHandle = requireNotNull(gsl_matrix_alloc(rows.toULong(), columns.toULong())), scope = scope)
override fun produceDirtyVector(size: Int): GslVector<Double, gsl_vector> =
GslRealVector(requireNotNull(gsl_vector_alloc(size.toULong())))
GslRealVector(nativeHandle = requireNotNull(gsl_vector_alloc(size.toULong())), scope = scope)
public override fun Matrix<Double>.dot(other: Matrix<Double>): GslMatrix<Double, gsl_matrix> {
val x = toGsl().nativeHandle
val a = other.toGsl().nativeHandle
val result = requireNotNull(gsl_matrix_calloc(a.pointed.size1, a.pointed.size2))
gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, x, a, 1.0, result)
return GslRealMatrix(result)
return GslRealMatrix(result, scope = scope)
}
public override fun Matrix<Double>.dot(vector: Point<Double>): GslVector<Double, gsl_vector> {
@ -57,7 +59,7 @@ public object GslRealMatrixContext : GslMatrixContext<Double, gsl_matrix, gsl_ve
val a = vector.toGsl().nativeHandle
val result = requireNotNull(gsl_vector_calloc(a.pointed.size))
gsl_blas_dgemv(CblasNoTrans, 1.0, x, a, 1.0, result)
return GslRealVector(result)
return GslRealVector(result, scope = scope)
}
public override fun Matrix<Double>.times(value: Double): GslMatrix<Double, gsl_matrix> {
@ -79,19 +81,20 @@ public object GslRealMatrixContext : GslMatrixContext<Double, gsl_matrix, gsl_ve
}
}
public object GslFloatMatrixContext : GslMatrixContext<Float, gsl_matrix_float, gsl_vector_float>() {
public class GslFloatMatrixContext(scope: DeferScope) :
GslMatrixContext<Float, gsl_matrix_float, gsl_vector_float>(scope) {
override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix<Float, gsl_matrix_float> =
GslFloatMatrix(requireNotNull(gsl_matrix_float_alloc(rows.toULong(), columns.toULong())))
GslFloatMatrix(requireNotNull(gsl_matrix_float_alloc(rows.toULong(), columns.toULong())), scope = scope)
override fun produceDirtyVector(size: Int): GslVector<Float, gsl_vector_float> =
GslFloatVector(requireNotNull(gsl_vector_float_alloc(size.toULong())))
GslFloatVector(requireNotNull(gsl_vector_float_alloc(size.toULong())), scope)
public override fun Matrix<Float>.dot(other: Matrix<Float>): GslMatrix<Float, gsl_matrix_float> {
val x = toGsl().nativeHandle
val a = other.toGsl().nativeHandle
val result = requireNotNull(gsl_matrix_float_calloc(a.pointed.size1, a.pointed.size2))
gsl_blas_sgemm(CblasNoTrans, CblasNoTrans, 1f, x, a, 1f, result)
return GslFloatMatrix(result)
return GslFloatMatrix(nativeHandle = result, scope = scope)
}
public override fun Matrix<Float>.dot(vector: Point<Float>): GslVector<Float, gsl_vector_float> {
@ -99,7 +102,7 @@ public object GslFloatMatrixContext : GslMatrixContext<Float, gsl_matrix_float,
val a = vector.toGsl().nativeHandle
val result = requireNotNull(gsl_vector_float_calloc(a.pointed.size))
gsl_blas_sgemv(CblasNoTrans, 1f, x, a, 1f, result)
return GslFloatVector(result)
return GslFloatVector(nativeHandle = result, scope = scope)
}
public override fun Matrix<Float>.times(value: Float): GslMatrix<Float, gsl_matrix_float> {
@ -121,19 +124,22 @@ public object GslFloatMatrixContext : GslMatrixContext<Float, gsl_matrix_float,
}
}
public object GslComplexMatrixContext : GslMatrixContext<Complex, gsl_matrix_complex, gsl_vector_complex>() {
override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix<Complex, gsl_matrix_complex> =
GslComplexMatrix(requireNotNull(gsl_matrix_complex_alloc(rows.toULong(), columns.toULong())))
public class GslComplexMatrixContext(scope: DeferScope) :
GslMatrixContext<Complex, gsl_matrix_complex, gsl_vector_complex>(scope) {
override fun produceDirtyMatrix(rows: Int, columns: Int): GslMatrix<Complex, gsl_matrix_complex> = GslComplexMatrix(
nativeHandle = requireNotNull(gsl_matrix_complex_alloc(rows.toULong(), columns.toULong())),
scope = scope
)
override fun produceDirtyVector(size: Int): GslVector<Complex, gsl_vector_complex> =
GslComplexVector(requireNotNull(gsl_vector_complex_alloc(size.toULong())))
GslComplexVector(requireNotNull(gsl_vector_complex_alloc(size.toULong())), scope)
public override fun Matrix<Complex>.dot(other: Matrix<Complex>): GslMatrix<Complex, gsl_matrix_complex> {
val x = toGsl().nativeHandle
val a = other.toGsl().nativeHandle
val result = requireNotNull(gsl_matrix_complex_calloc(a.pointed.size1, a.pointed.size2))
gsl_blas_zgemm(CblasNoTrans, CblasNoTrans, ComplexField.one.toGsl(), x, a, ComplexField.one.toGsl(), result)
return GslComplexMatrix(result)
return GslComplexMatrix(nativeHandle = result, scope = scope)
}
public override fun Matrix<Complex>.dot(vector: Point<Complex>): GslVector<Complex, gsl_vector_complex> {
@ -141,7 +147,7 @@ public object GslComplexMatrixContext : GslMatrixContext<Complex, gsl_matrix_com
val a = vector.toGsl().nativeHandle
val result = requireNotNull(gsl_vector_complex_calloc(a.pointed.size))
gsl_blas_zgemv(CblasNoTrans, ComplexField.one.toGsl(), x, a, ComplexField.one.toGsl(), result)
return GslComplexVector(result)
return GslComplexVector(result, scope)
}
public override fun Matrix<Complex>.times(value: Complex): GslMatrix<Complex, gsl_matrix_complex> {

View File

@ -2,8 +2,14 @@ package kscience.kmath.gsl
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.CStructVar
import kotlinx.io.Closeable
import kotlinx.cinterop.DeferScope
public abstract class GslMemoryHolder<H : CStructVar> internal constructor() : Closeable {
public abstract class GslMemoryHolder<H : CStructVar> internal constructor(internal val scope: DeferScope) {
internal abstract val nativeHandle: CPointer<H>
init {
scope.defer(::close)
}
internal abstract fun close()
}

View File

@ -1,9 +1,11 @@
package kscience.kmath.gsl
import kotlinx.cinterop.CStructVar
import kotlinx.cinterop.DeferScope
import kscience.kmath.linear.Point
public abstract class GslVector<T, H : CStructVar> internal constructor() : GslMemoryHolder<H>(), Point<T> {
public abstract class GslVector<T, H : CStructVar> internal constructor(scope: DeferScope) :
GslMemoryHolder<H>(scope), Point<T> {
internal abstract operator fun set(index: Int, value: T)
internal abstract fun copy(): GslVector<T, H>

View File

@ -6,8 +6,9 @@ import org.gnu.gsl.*
internal class GslRealMatrix(
override val nativeHandle: CPointer<gsl_matrix>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Double, gsl_matrix>() {
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<Double, gsl_matrix>(scope) {
override val rowNum: Int
get() = nativeHandle.pointed.size1.toInt()
@ -17,7 +18,7 @@ internal class GslRealMatrix(
override val features: Set<MatrixFeature> = features
override fun suggestFeature(vararg features: MatrixFeature): GslRealMatrix =
GslRealMatrix(nativeHandle, this.features + features)
GslRealMatrix(nativeHandle, this.features + features, scope)
override operator fun get(i: Int, j: Int): Double = gsl_matrix_get(nativeHandle, i.toULong(), j.toULong())
@ -27,7 +28,7 @@ internal class GslRealMatrix(
override fun copy(): GslRealMatrix {
val new = requireNotNull(gsl_matrix_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_memcpy(new, nativeHandle)
return GslRealMatrix(new, features)
return GslRealMatrix(new, features, scope)
}
override fun close(): Unit = gsl_matrix_free(nativeHandle)
@ -40,8 +41,9 @@ internal class GslRealMatrix(
internal class GslFloatMatrix(
override val nativeHandle: CPointer<gsl_matrix_float>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Float, gsl_matrix_float>() {
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<Float, gsl_matrix_float>(scope) {
override val rowNum: Int
get() = nativeHandle.pointed.size1.toInt()
@ -51,7 +53,7 @@ internal class GslFloatMatrix(
override val features: Set<MatrixFeature> = features
override fun suggestFeature(vararg features: MatrixFeature): GslFloatMatrix =
GslFloatMatrix(nativeHandle, this.features + features)
GslFloatMatrix(nativeHandle, this.features + features, scope)
override operator fun get(i: Int, j: Int): Float = gsl_matrix_float_get(nativeHandle, i.toULong(), j.toULong())
@ -61,7 +63,7 @@ internal class GslFloatMatrix(
override fun copy(): GslFloatMatrix {
val new = requireNotNull(gsl_matrix_float_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_float_memcpy(new, nativeHandle)
return GslFloatMatrix(new, features)
return GslFloatMatrix(new, features, scope)
}
override fun close(): Unit = gsl_matrix_float_free(nativeHandle)
@ -74,8 +76,9 @@ internal class GslFloatMatrix(
internal class GslShortMatrix(
override val nativeHandle: CPointer<gsl_matrix_short>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Short, gsl_matrix_short>() {
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<Short, gsl_matrix_short>(scope) {
override val rowNum: Int
get() = nativeHandle.pointed.size1.toInt()
@ -85,7 +88,7 @@ internal class GslShortMatrix(
override val features: Set<MatrixFeature> = features
override fun suggestFeature(vararg features: MatrixFeature): GslShortMatrix =
GslShortMatrix(nativeHandle, this.features + features)
GslShortMatrix(nativeHandle, this.features + features, scope)
override operator fun get(i: Int, j: Int): Short = gsl_matrix_short_get(nativeHandle, i.toULong(), j.toULong())
@ -95,7 +98,7 @@ internal class GslShortMatrix(
override fun copy(): GslShortMatrix {
val new = requireNotNull(gsl_matrix_short_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_short_memcpy(new, nativeHandle)
return GslShortMatrix(new, features)
return GslShortMatrix(new, features, scope)
}
override fun close(): Unit = gsl_matrix_short_free(nativeHandle)
@ -108,8 +111,9 @@ internal class GslShortMatrix(
internal class GslUShortMatrix(
override val nativeHandle: CPointer<gsl_matrix_ushort>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<UShort, gsl_matrix_ushort>() {
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<UShort, gsl_matrix_ushort>(scope) {
override val rowNum: Int
get() = nativeHandle.pointed.size1.toInt()
@ -119,7 +123,7 @@ internal class GslUShortMatrix(
override val features: Set<MatrixFeature> = features
override fun suggestFeature(vararg features: MatrixFeature): GslUShortMatrix =
GslUShortMatrix(nativeHandle, this.features + features)
GslUShortMatrix(nativeHandle, this.features + features, scope)
override operator fun get(i: Int, j: Int): UShort = gsl_matrix_ushort_get(nativeHandle, i.toULong(), j.toULong())
@ -129,7 +133,7 @@ internal class GslUShortMatrix(
override fun copy(): GslUShortMatrix {
val new = requireNotNull(gsl_matrix_ushort_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_ushort_memcpy(new, nativeHandle)
return GslUShortMatrix(new, features)
return GslUShortMatrix(new, features, scope)
}
override fun close(): Unit = gsl_matrix_ushort_free(nativeHandle)
@ -142,8 +146,9 @@ internal class GslUShortMatrix(
internal class GslLongMatrix(
override val nativeHandle: CPointer<gsl_matrix_long>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Long, gsl_matrix_long>() {
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<Long, gsl_matrix_long>(scope) {
override val rowNum: Int
get() = nativeHandle.pointed.size1.toInt()
@ -153,7 +158,7 @@ internal class GslLongMatrix(
override val features: Set<MatrixFeature> = features
override fun suggestFeature(vararg features: MatrixFeature): GslLongMatrix =
GslLongMatrix(nativeHandle, this.features + features)
GslLongMatrix(nativeHandle, this.features + features, scope)
override operator fun get(i: Int, j: Int): Long = gsl_matrix_long_get(nativeHandle, i.toULong(), j.toULong())
@ -163,7 +168,7 @@ internal class GslLongMatrix(
override fun copy(): GslLongMatrix {
val new = requireNotNull(gsl_matrix_long_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_long_memcpy(new, nativeHandle)
return GslLongMatrix(new, features)
return GslLongMatrix(new, features, scope)
}
override fun close(): Unit = gsl_matrix_long_free(nativeHandle)
@ -176,8 +181,9 @@ internal class GslLongMatrix(
internal class GslULongMatrix(
override val nativeHandle: CPointer<gsl_matrix_ulong>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<ULong, gsl_matrix_ulong>() {
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<ULong, gsl_matrix_ulong>(scope) {
override val rowNum: Int
get() = nativeHandle.pointed.size1.toInt()
@ -187,7 +193,7 @@ internal class GslULongMatrix(
override val features: Set<MatrixFeature> = features
override fun suggestFeature(vararg features: MatrixFeature): GslULongMatrix =
GslULongMatrix(nativeHandle, this.features + features)
GslULongMatrix(nativeHandle, this.features + features, scope)
override operator fun get(i: Int, j: Int): ULong = gsl_matrix_ulong_get(nativeHandle, i.toULong(), j.toULong())
@ -197,7 +203,7 @@ internal class GslULongMatrix(
override fun copy(): GslULongMatrix {
val new = requireNotNull(gsl_matrix_ulong_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_ulong_memcpy(new, nativeHandle)
return GslULongMatrix(new, features)
return GslULongMatrix(new, features, scope)
}
override fun close(): Unit = gsl_matrix_ulong_free(nativeHandle)
@ -210,8 +216,9 @@ internal class GslULongMatrix(
internal class GslIntMatrix(
override val nativeHandle: CPointer<gsl_matrix_int>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<Int, gsl_matrix_int>() {
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<Int, gsl_matrix_int>(scope) {
override val rowNum: Int
get() = nativeHandle.pointed.size1.toInt()
@ -221,7 +228,7 @@ internal class GslIntMatrix(
override val features: Set<MatrixFeature> = features
override fun suggestFeature(vararg features: MatrixFeature): GslIntMatrix =
GslIntMatrix(nativeHandle, this.features + features)
GslIntMatrix(nativeHandle, this.features + features, scope)
override operator fun get(i: Int, j: Int): Int = gsl_matrix_int_get(nativeHandle, i.toULong(), j.toULong())
@ -231,7 +238,7 @@ internal class GslIntMatrix(
override fun copy(): GslIntMatrix {
val new = requireNotNull(gsl_matrix_int_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_int_memcpy(new, nativeHandle)
return GslIntMatrix(new, features)
return GslIntMatrix(new, features, scope)
}
override fun close(): Unit = gsl_matrix_int_free(nativeHandle)
@ -244,8 +251,9 @@ internal class GslIntMatrix(
internal class GslUIntMatrix(
override val nativeHandle: CPointer<gsl_matrix_uint>,
features: Set<MatrixFeature> = emptySet()
) : GslMatrix<UInt, gsl_matrix_uint>() {
features: Set<MatrixFeature> = emptySet(),
scope: DeferScope
) : GslMatrix<UInt, gsl_matrix_uint>(scope) {
override val rowNum: Int
get() = nativeHandle.pointed.size1.toInt()
@ -255,7 +263,7 @@ internal class GslUIntMatrix(
override val features: Set<MatrixFeature> = features
override fun suggestFeature(vararg features: MatrixFeature): GslUIntMatrix =
GslUIntMatrix(nativeHandle, this.features + features)
GslUIntMatrix(nativeHandle, this.features + features, scope)
override operator fun get(i: Int, j: Int): UInt = gsl_matrix_uint_get(nativeHandle, i.toULong(), j.toULong())
@ -265,7 +273,7 @@ internal class GslUIntMatrix(
override fun copy(): GslUIntMatrix {
val new = requireNotNull(gsl_matrix_uint_alloc(rowNum.toULong(), colNum.toULong()))
gsl_matrix_uint_memcpy(new, nativeHandle)
return GslUIntMatrix(new, features)
return GslUIntMatrix(new, features, scope)
}
override fun close(): Unit = gsl_matrix_uint_free(nativeHandle)

View File

@ -3,7 +3,8 @@ package kscience.kmath.gsl
import kotlinx.cinterop.*
import org.gnu.gsl.*
internal class GslRealVector(override val nativeHandle: CPointer<gsl_vector>) : GslVector<Double, gsl_vector>() {
internal class GslRealVector(override val nativeHandle: CPointer<gsl_vector>, scope: DeferScope)
: GslVector<Double, gsl_vector>(scope) {
override val size: Int
get() = nativeHandle.pointed.size.toInt()
@ -13,7 +14,7 @@ internal class GslRealVector(override val nativeHandle: CPointer<gsl_vector>) :
override fun copy(): GslRealVector {
val new = requireNotNull(gsl_vector_alloc(size.toULong()))
gsl_vector_memcpy(new, nativeHandle)
return GslRealVector(new)
return GslRealVector(new, scope)
}
override fun equals(other: Any?): Boolean {
@ -24,7 +25,8 @@ internal class GslRealVector(override val nativeHandle: CPointer<gsl_vector>) :
override fun close(): Unit = gsl_vector_free(nativeHandle)
}
internal class GslFloatVector(override val nativeHandle: CPointer<gsl_vector_float>) : GslVector<Float, gsl_vector_float>() {
internal class GslFloatVector(override val nativeHandle: CPointer<gsl_vector_float>, scope: DeferScope)
: GslVector<Float, gsl_vector_float>(scope) {
override val size: Int
get() = nativeHandle.pointed.size.toInt()
@ -34,7 +36,7 @@ internal class GslFloatVector(override val nativeHandle: CPointer<gsl_vector_flo
override fun copy(): GslFloatVector {
val new = requireNotNull(gsl_vector_float_alloc(size.toULong()))
gsl_vector_float_memcpy(new, nativeHandle)
return GslFloatVector(new)
return GslFloatVector(new, scope)
}
override fun equals(other: Any?): Boolean {
@ -45,7 +47,8 @@ internal class GslFloatVector(override val nativeHandle: CPointer<gsl_vector_flo
override fun close(): Unit = gsl_vector_float_free(nativeHandle)
}
internal class GslShortVector(override val nativeHandle: CPointer<gsl_vector_short>) : GslVector<Short, gsl_vector_short>() {
internal class GslShortVector(override val nativeHandle: CPointer<gsl_vector_short>, scope: DeferScope)
: GslVector<Short, gsl_vector_short>(scope) {
override val size: Int
get() = nativeHandle.pointed.size.toInt()
@ -55,7 +58,7 @@ internal class GslShortVector(override val nativeHandle: CPointer<gsl_vector_sho
override fun copy(): GslShortVector {
val new = requireNotNull(gsl_vector_short_alloc(size.toULong()))
gsl_vector_short_memcpy(new, nativeHandle)
return GslShortVector(new)
return GslShortVector(new, scope)
}
override fun equals(other: Any?): Boolean {
@ -66,7 +69,8 @@ internal class GslShortVector(override val nativeHandle: CPointer<gsl_vector_sho
override fun close(): Unit = gsl_vector_short_free(nativeHandle)
}
internal class GslUShortVector(override val nativeHandle: CPointer<gsl_vector_ushort>) : GslVector<UShort, gsl_vector_ushort>() {
internal class GslUShortVector(override val nativeHandle: CPointer<gsl_vector_ushort>, scope: DeferScope)
: GslVector<UShort, gsl_vector_ushort>(scope) {
override val size: Int
get() = nativeHandle.pointed.size.toInt()
@ -76,7 +80,7 @@ internal class GslUShortVector(override val nativeHandle: CPointer<gsl_vector_us
override fun copy(): GslUShortVector {
val new = requireNotNull(gsl_vector_ushort_alloc(size.toULong()))
gsl_vector_ushort_memcpy(new, nativeHandle)
return GslUShortVector(new)
return GslUShortVector(new, scope)
}
override fun equals(other: Any?): Boolean {
@ -87,7 +91,8 @@ internal class GslUShortVector(override val nativeHandle: CPointer<gsl_vector_us
override fun close(): Unit = gsl_vector_ushort_free(nativeHandle)
}
internal class GslLongVector(override val nativeHandle: CPointer<gsl_vector_long>) : GslVector<Long, gsl_vector_long>() {
internal class GslLongVector(override val nativeHandle: CPointer<gsl_vector_long>, scope: DeferScope)
: GslVector<Long, gsl_vector_long>(scope) {
override val size: Int
get() = nativeHandle.pointed.size.toInt()
@ -97,7 +102,7 @@ internal class GslLongVector(override val nativeHandle: CPointer<gsl_vector_long
override fun copy(): GslLongVector {
val new = requireNotNull(gsl_vector_long_alloc(size.toULong()))
gsl_vector_long_memcpy(new, nativeHandle)
return GslLongVector(new)
return GslLongVector(new, scope)
}
override fun equals(other: Any?): Boolean {
@ -108,7 +113,8 @@ internal class GslLongVector(override val nativeHandle: CPointer<gsl_vector_long
override fun close(): Unit = gsl_vector_long_free(nativeHandle)
}
internal class GslULongVector(override val nativeHandle: CPointer<gsl_vector_ulong>) : GslVector<ULong, gsl_vector_ulong>() {
internal class GslULongVector(override val nativeHandle: CPointer<gsl_vector_ulong>, scope: DeferScope)
: GslVector<ULong, gsl_vector_ulong>(scope) {
override val size: Int
get() = nativeHandle.pointed.size.toInt()
@ -118,7 +124,7 @@ internal class GslULongVector(override val nativeHandle: CPointer<gsl_vector_ulo
override fun copy(): GslULongVector {
val new = requireNotNull(gsl_vector_ulong_alloc(size.toULong()))
gsl_vector_ulong_memcpy(new, nativeHandle)
return GslULongVector(new)
return GslULongVector(new, scope)
}
override fun equals(other: Any?): Boolean {
@ -129,7 +135,8 @@ internal class GslULongVector(override val nativeHandle: CPointer<gsl_vector_ulo
override fun close(): Unit = gsl_vector_ulong_free(nativeHandle)
}
internal class GslIntVector(override val nativeHandle: CPointer<gsl_vector_int>) : GslVector<Int, gsl_vector_int>() {
internal class GslIntVector(override val nativeHandle: CPointer<gsl_vector_int>, scope: DeferScope)
: GslVector<Int, gsl_vector_int>(scope) {
override val size: Int
get() = nativeHandle.pointed.size.toInt()
@ -139,7 +146,7 @@ internal class GslIntVector(override val nativeHandle: CPointer<gsl_vector_int>)
override fun copy(): GslIntVector {
val new = requireNotNull(gsl_vector_int_alloc(size.toULong()))
gsl_vector_int_memcpy(new, nativeHandle)
return GslIntVector(new)
return GslIntVector(new, scope)
}
override fun equals(other: Any?): Boolean {
@ -150,7 +157,8 @@ internal class GslIntVector(override val nativeHandle: CPointer<gsl_vector_int>)
override fun close(): Unit = gsl_vector_int_free(nativeHandle)
}
internal class GslUIntVector(override val nativeHandle: CPointer<gsl_vector_uint>) : GslVector<UInt, gsl_vector_uint>() {
internal class GslUIntVector(override val nativeHandle: CPointer<gsl_vector_uint>, scope: DeferScope)
: GslVector<UInt, gsl_vector_uint>(scope) {
override val size: Int
get() = nativeHandle.pointed.size.toInt()
@ -160,7 +168,7 @@ internal class GslUIntVector(override val nativeHandle: CPointer<gsl_vector_uint
override fun copy(): GslUIntVector {
val new = requireNotNull(gsl_vector_uint_alloc(size.toULong()))
gsl_vector_uint_memcpy(new, nativeHandle)
return GslUIntVector(new)
return GslUIntVector(new, scope)
}
override fun equals(other: Any?): Boolean {

View File

@ -1,5 +1,6 @@
package kscience.kmath.gsl
import kotlinx.cinterop.memScoped
import kscience.kmath.linear.RealMatrixContext
import kscience.kmath.operations.invoke
import kscience.kmath.structures.RealBuffer
@ -11,36 +12,37 @@ import kotlin.test.assertTrue
internal class RealTest {
@Test
fun testScale() = GslRealMatrixContext {
val ma = GslRealMatrixContext.produce(10, 10) { _, _ -> 0.1 }
fun testScale() = memScoped {
(GslRealMatrixContext(this)) {
val ma = produce(10, 10) { _, _ -> 0.1 }
val mb = (ma * 20.0)
assertEquals(mb[0, 1], 2.0)
mb.close()
ma.close()
}
}
@Test
fun testDotOfMatrixAndVector() {
val ma = GslRealMatrixContext.produce(2, 2) { _, _ -> 100.0 }
fun testDotOfMatrixAndVector() = memScoped {
(GslRealMatrixContext(this)) {
val ma = produce(2, 2) { _, _ -> 100.0 }
val vb = RealBuffer(2) { 0.1 }
val res1 = GslRealMatrixContext { ma dot vb }
val res1 = ma dot vb
val res2 = RealMatrixContext { ma dot vb }
println(res1.asSequence().toList())
println(res2.asSequence().toList())
assertTrue(res1.contentEquals(res2))
res1.close()
}
}
@Test
fun testDotOfMatrixAndMatrix() {
val ma = GslRealMatrixContext.produce(2, 2) { _, _ -> 100.0 }
val mb = GslRealMatrixContext.produce(2, 2) { _, _ -> 100.0 }
val res1 = GslRealMatrixContext { ma dot mb }
fun testDotOfMatrixAndMatrix() = memScoped {
(GslRealMatrixContext(this)) {
val ma = produce(2, 2) { _, _ -> 100.0 }
val mb = produce(2, 2) { _, _ -> 100.0 }
val res1 = ma dot mb
val res2 = RealMatrixContext { ma dot mb }
println(res1.rows.asIterable().map { it.asSequence() }.flatMap(Sequence<*>::toList))
println(res2.rows.asIterable().map { it.asSequence() }.flatMap(Sequence<*>::toList))
assertEquals(res1, res2)
ma.close()
mb.close()
}
}
}