diff --git a/build.gradle.kts b/build.gradle.kts index d1667d7b9..4d15bfe0d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,6 +4,7 @@ buildscript { val kotlinVersion: String by rootProject.extra("1.3.20") val ioVersion: String by rootProject.extra("0.1.2") val coroutinesVersion: String by rootProject.extra("1.1.1") + val atomicfuVersion: String by rootProject.extra("0.12.1") repositories { //maven("https://dl.bintray.com/kotlin/kotlin-eap") diff --git a/kmath-coroutines/build.gradle b/kmath-coroutines/build.gradle index 7149e8bb9..2c58a4112 100644 --- a/kmath-coroutines/build.gradle +++ b/kmath-coroutines/build.gradle @@ -3,13 +3,8 @@ plugins { } kotlin { - targets { - fromPreset(presets.jvm, 'jvm') - // For ARM, preset should be changed to presets.iosArm32 or presets.iosArm64 - // For Linux, preset should be changed to e.g. presets.linuxX64 - // For MacOS, preset should be changed to e.g. presets.macosX64 - //fromPreset(presets.mingwX64, 'mingw') - } + jvm() + js() sourceSets { commonMain { dependencies { @@ -34,6 +29,11 @@ kotlin { implementation 'org.jetbrains.kotlin:kotlin-test-junit' } } + jsMain{ + dependencies{ + api "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$coroutinesVersion" + } + } // mingwMain { // } // mingwTest { diff --git a/kmath-coroutines/src/jsMain/kotlin/scientifik/kmath/structures/runBlocking.kt b/kmath-coroutines/src/jsMain/kotlin/scientifik/kmath/structures/runBlocking.kt new file mode 100644 index 000000000..7331d1801 --- /dev/null +++ b/kmath-coroutines/src/jsMain/kotlin/scientifik/kmath/structures/runBlocking.kt @@ -0,0 +1,11 @@ +package scientifik.kmath.structures + +import kotlinx.coroutines.CoroutineScope +import kotlin.coroutines.CoroutineContext + +actual fun runBlocking( + context: CoroutineContext, + function: suspend CoroutineScope.() -> R +): R { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. +} \ No newline at end of file diff --git a/kmath-coroutines/src/jvmMain/kotlin/scientifik/kmath/structures/_CoroutinesExtra.kt b/kmath-coroutines/src/jvmMain/kotlin/scientifik/kmath/structures/runBlocking.kt similarity index 100% rename from kmath-coroutines/src/jvmMain/kotlin/scientifik/kmath/structures/_CoroutinesExtra.kt rename to kmath-coroutines/src/jvmMain/kotlin/scientifik/kmath/structures/runBlocking.kt diff --git a/kmath-sequential/build.gradle.kts b/kmath-sequential/build.gradle.kts index 57467d7b8..391504f23 100644 --- a/kmath-sequential/build.gradle.kts +++ b/kmath-sequential/build.gradle.kts @@ -1,19 +1,20 @@ plugins { kotlin("multiplatform") - id("kotlinx-atomicfu") + id("kotlinx-atomicfu") version "0.12.1" } +val atomicfuVersion: String by rootProject.extra kotlin { jvm () - js() + //js() sourceSets { val commonMain by getting { dependencies { api(project(":kmath-core")) api(project(":kmath-coroutines")) - compileOnly("org.jetbrains.kotlinx:atomicfu-common:0.12.1") + compileOnly("org.jetbrains.kotlinx:atomicfu-common:$atomicfuVersion") } } val commonTest by getting { @@ -24,7 +25,7 @@ kotlin { } val jvmMain by getting { dependencies { - compileOnly("org.jetbrains.kotlinx:atomicfu:0.12.1") + compileOnly("org.jetbrains.kotlinx:atomicfu:$atomicfuVersion") } } val jvmTest by getting { @@ -35,17 +36,18 @@ kotlin { } // val jsMain by getting { // dependencies { -// api(kotlin("stdlib-js")) +// compileOnly("org.jetbrains.kotlinx:atomicfu-js:$atomicfuVersion") // } // } - val jsTest by getting { - dependencies { - implementation(kotlin("test-js")) - } - } -// mingwMain { -// } -// mingwTest { +// val jsTest by getting { +// dependencies { +// implementation(kotlin("test-js")) +// } // } + } +} + +atomicfu { + variant = "VH" } \ No newline at end of file diff --git a/kmath-sequential/src/commonMain/kotlin/scientifik/kmath/sequential/Accumulators.kt b/kmath-sequential/src/commonMain/kotlin/scientifik/kmath/sequential/Accumulators.kt index 94ac0ac2e..ef7950519 100644 --- a/kmath-sequential/src/commonMain/kotlin/scientifik/kmath/sequential/Accumulators.kt +++ b/kmath-sequential/src/commonMain/kotlin/scientifik/kmath/sequential/Accumulators.kt @@ -1,10 +1,7 @@ package scientifik.kmath.sequential import kotlinx.atomicfu.atomic -import kotlinx.atomicfu.atomicArrayOfNulls import kotlinx.atomicfu.getAndUpdate -import kotlinx.coroutines.GlobalScope -import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.ReceiveChannel import scientifik.kmath.operations.Space @@ -46,7 +43,7 @@ suspend fun Accumulator.offerAll(channel: ReceiveChannel) { */ class GenericMean(val context: Space) : Accumulator { //TODO add guard against overflow - val counter = atomic(0) + private val counter = atomic(0) val sum = atomic(context.zero) val value get() = with(context) { sum.value / counter.value } diff --git a/kmath-sequential/src/commonMain/kotlin/scientifik/kmath/sequential/Chain.kt b/kmath-sequential/src/commonMain/kotlin/scientifik/kmath/sequential/Chain.kt index abecf014d..7633b2223 100644 --- a/kmath-sequential/src/commonMain/kotlin/scientifik/kmath/sequential/Chain.kt +++ b/kmath-sequential/src/commonMain/kotlin/scientifik/kmath/sequential/Chain.kt @@ -97,11 +97,12 @@ class MarkovChain(private val seed: () -> R, private val gen: suspe constructor(seed: R, gen: suspend (R) -> R) : this({ seed }, gen) - private val atomicValue by lazy { atomic(seed()) } - override val value: R get() = atomicValue.value + private val atomicValue = atomic(null) + override val value: R get() = atomicValue.value ?: seed() override suspend fun next(): R { - atomicValue.lazySet(gen(value)) + val newValue = gen(value) + atomicValue.lazySet(newValue) return value } @@ -122,11 +123,12 @@ class StatefulChain( constructor(state: S, seed: R, gen: suspend S.(R) -> R) : this(state, { seed }, gen) - private val atomicValue by lazy { atomic(seed(state)) } - override val value: R get() = atomicValue.value + private val atomicValue = atomic(null) + override val value: R get() = atomicValue.value ?: seed(state) override suspend fun next(): R { - atomicValue.lazySet(gen(state, value)) + val newValue = gen(state, value) + atomicValue.lazySet(newValue) return value } diff --git a/kmath-core/src/commonTest/kotlin/scientifik/kmath/misc/CumulativeKtTest.kt b/kmath-sequential/src/commonTest/scientifik/kmath/sequential/CumulativeKtTest.kt similarity index 100% rename from kmath-core/src/commonTest/kotlin/scientifik/kmath/misc/CumulativeKtTest.kt rename to kmath-sequential/src/commonTest/scientifik/kmath/sequential/CumulativeKtTest.kt diff --git a/kmath-sequential/src/jvmMain/kotlin/scientifik/kmath/sequential/AccumulatorsExt.kt b/kmath-sequential/src/jvmMain/kotlin/scientifik/kmath/sequential/AccumulatorsExt.kt index cc9a07668..37190f8f3 100644 --- a/kmath-sequential/src/jvmMain/kotlin/scientifik/kmath/sequential/AccumulatorsExt.kt +++ b/kmath-sequential/src/jvmMain/kotlin/scientifik/kmath/sequential/AccumulatorsExt.kt @@ -2,6 +2,8 @@ package scientifik.kmath.sequential import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.ReceiveChannel +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import scientifik.kmath.operations.Space import scientifik.kmath.structures.runBlocking import java.util.* @@ -12,13 +14,16 @@ import java.util.* class MovingAverage(val window: Int, val context: Space) : Accumulator { private val outputChannel = Channel() private val queue = ArrayDeque(window) + private val mutex = Mutex() override suspend fun send(value: T) { - queue.add(value) - if (queue.size == window) { - val sum = queue.fold(context.zero) { a, b -> context.run { a + b } } - outputChannel.send(context.run { sum / window }) - queue.pop() + mutex.withLock { + queue.add(value) + if (queue.size == window) { + val sum = queue.fold(context.zero) { a, b -> context.run { a + b } } + outputChannel.send(context.run { sum / window }) + queue.pop() + } } } diff --git a/settings.gradle.kts b/settings.gradle.kts index 5638223fc..7045009ca 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,13 +1,3 @@ -buildscript { - dependencies { - classpath("org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.12.1") - } - - repositories { - jcenter() - } -} - pluginManagement { repositories { mavenCentral() @@ -18,7 +8,7 @@ pluginManagement { eachPlugin { when (requested.id.id) { "kotlinx-atomicfu" -> { - useModule("org.jetbrains.kotlinx:atomicfu-gradle-plugin:0.12.1") + useModule("org.jetbrains.kotlinx:atomicfu-gradle-plugin:${requested.version}") } } }