forked from kscience/kmath
native memory
This commit is contained in:
parent
e6104e13e8
commit
7d315e37d1
2
.gitignore
vendored
2
.gitignore
vendored
@ -8,5 +8,3 @@ out/
|
||||
|
||||
# Cache of project
|
||||
.gradletasknamecache
|
||||
|
||||
gradle.properties
|
@ -5,7 +5,7 @@
|
||||
- `fun` annotation for SAM interfaces in library
|
||||
- Explicit `public` visibility for all public APIs
|
||||
- Better trigonometric and hyperbolic functions for `AutoDiffField` (https://github.com/mipt-npm/kmath/pull/140).
|
||||
- Automatic documentation generation for features (#139)
|
||||
- Automatic README generation for features (#139)
|
||||
|
||||
### Changed
|
||||
- Package changed from `scientifik` to `kscience.kmath`.
|
||||
|
9
gradle.properties
Normal file
9
gradle.properties
Normal file
@ -0,0 +1,9 @@
|
||||
kotlin.code.style=official
|
||||
kotlin.parallel.tasks.in.project=true
|
||||
kotlin.mpp.enableGranularSourceSetsMetadata=true
|
||||
kotlin.native.enableDependencyPropagation=false
|
||||
kotlin.mpp.stability.nowarn=true
|
||||
|
||||
org.gradle.jvmargs=-XX:MaxMetaspaceSize=512m
|
||||
org.gradle.parallel=true
|
||||
systemProp.org.gradle.internal.publish.checksums.insecure=true
|
@ -1,5 +1,6 @@
|
||||
plugins {
|
||||
id("ru.mipt.npm.mpp")
|
||||
id("ru.mipt.npm.native")
|
||||
}
|
||||
|
||||
kotlin.sourceSets.commonMain {
|
||||
|
@ -1,11 +1,11 @@
|
||||
package kscience.kmath.operations
|
||||
|
||||
import kscience.kmath.memory.MemoryReader
|
||||
import kscience.kmath.memory.MemorySpec
|
||||
import kscience.kmath.memory.MemoryWriter
|
||||
import kscience.kmath.structures.Buffer
|
||||
import kscience.kmath.structures.MemoryBuffer
|
||||
import kscience.kmath.structures.MutableBuffer
|
||||
import kscience.memory.MemoryReader
|
||||
import kscience.memory.MemorySpec
|
||||
import kscience.memory.MemoryWriter
|
||||
import kotlin.math.*
|
||||
|
||||
/**
|
||||
|
@ -1,6 +1,6 @@
|
||||
package kscience.kmath.structures
|
||||
|
||||
import kscience.memory.*
|
||||
import kscience.kmath.memory.*
|
||||
|
||||
/**
|
||||
* A non-boxing buffer over [Memory] object.
|
||||
|
@ -1 +1,4 @@
|
||||
plugins { id("ru.mipt.npm.mpp") }
|
||||
plugins {
|
||||
id("ru.mipt.npm.mpp")
|
||||
id("ru.mipt.npm.native")
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package kscience.memory
|
||||
package kscience.kmath.memory
|
||||
|
||||
import kotlin.contracts.InvocationKind
|
||||
import kotlin.contracts.contract
|
@ -1,4 +1,4 @@
|
||||
package kscience.memory
|
||||
package kscience.kmath.memory
|
||||
|
||||
/**
|
||||
* A specification to read or write custom objects with fixed size in bytes.
|
@ -1,4 +1,4 @@
|
||||
package kscience.memory
|
||||
package kscience.kmath.memory
|
||||
|
||||
import org.khronos.webgl.ArrayBuffer
|
||||
import org.khronos.webgl.DataView
|
@ -1,4 +1,4 @@
|
||||
package kscience.memory
|
||||
package kscience.kmath.memory
|
||||
|
||||
import java.io.IOException
|
||||
import java.nio.ByteBuffer
|
@ -0,0 +1,93 @@
|
||||
package kscience.kmath.memory
|
||||
|
||||
@PublishedApi
|
||||
internal class NativeMemory(
|
||||
val array: ByteArray,
|
||||
val startOffset: Int = 0,
|
||||
override val size: Int = array.size
|
||||
) : Memory {
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
private inline fun position(o: Int): Int = startOffset + o
|
||||
|
||||
override fun view(offset: Int, length: Int): Memory {
|
||||
require(offset >= 0) { "offset shouldn't be negative: $offset" }
|
||||
require(length >= 0) { "length shouldn't be negative: $length" }
|
||||
require(offset + length <= size) { "Can't view memory outside the parent region." }
|
||||
return NativeMemory(array, position(offset), length)
|
||||
}
|
||||
|
||||
override fun copy(): Memory {
|
||||
val copy = array.copyOfRange(startOffset, startOffset + size)
|
||||
return NativeMemory(copy)
|
||||
}
|
||||
|
||||
private val reader: MemoryReader = object : MemoryReader {
|
||||
override val memory: Memory get() = this@NativeMemory
|
||||
|
||||
override fun readDouble(offset: Int) = array.getDoubleAt(position(offset))
|
||||
|
||||
override fun readFloat(offset: Int) = array.getFloatAt(position(offset))
|
||||
|
||||
override fun readByte(offset: Int) = array[position(offset)]
|
||||
|
||||
override fun readShort(offset: Int) = array.getShortAt(position(offset))
|
||||
|
||||
override fun readInt(offset: Int) = array.getIntAt(position(offset))
|
||||
|
||||
override fun readLong(offset: Int) = array.getLongAt(position(offset))
|
||||
|
||||
override fun release() {
|
||||
// does nothing on JVM
|
||||
}
|
||||
}
|
||||
|
||||
override fun reader(): MemoryReader = reader
|
||||
|
||||
private val writer: MemoryWriter = object : MemoryWriter {
|
||||
override val memory: Memory get() = this@NativeMemory
|
||||
|
||||
override fun writeDouble(offset: Int, value: Double) {
|
||||
array.setDoubleAt(position(offset), value)
|
||||
}
|
||||
|
||||
override fun writeFloat(offset: Int, value: Float) {
|
||||
array.setFloatAt(position(offset), value)
|
||||
}
|
||||
|
||||
override fun writeByte(offset: Int, value: Byte) {
|
||||
array.set(position(offset), value)
|
||||
}
|
||||
|
||||
override fun writeShort(offset: Int, value: Short) {
|
||||
array.setShortAt(position(offset), value)
|
||||
}
|
||||
|
||||
override fun writeInt(offset: Int, value: Int) {
|
||||
array.setIntAt(position(offset), value)
|
||||
}
|
||||
|
||||
override fun writeLong(offset: Int, value: Long) {
|
||||
array.setLongAt(position(offset), value)
|
||||
}
|
||||
|
||||
override fun release() {
|
||||
// does nothing on JVM
|
||||
}
|
||||
}
|
||||
|
||||
override fun writer(): MemoryWriter = writer
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps a [Memory] around existing [ByteArray]. This operation is unsafe since the array is not copied
|
||||
* and could be mutated independently from the resulting [Memory].
|
||||
*/
|
||||
public actual fun Memory.Companion.wrap(array: ByteArray): Memory = NativeMemory(array)
|
||||
|
||||
/**
|
||||
* Allocates the most effective platform-specific memory.
|
||||
*/
|
||||
public actual fun Memory.Companion.allocate(length: Int): Memory {
|
||||
val array = ByteArray(length)
|
||||
return NativeMemory(array)
|
||||
}
|
Loading…
Reference in New Issue
Block a user