如何为并行任务实现协同程序

时间:2018-05-08 07:35:15

标签: kotlin kotlin-coroutines

所以,我有this piece of code

for (z in 0 until texture.extent.z) {
    println(z)
    for (y in 0 until texture.extent.y)
        for (x in 0 until texture.extent.x) {

            val v = Vec3(x, y, z) / texture.extent
            var n = when {
                FRACTAL -> FractalNoise().noise(v * noiseScale)
                else -> 20f * glm.perlin(v)
            }
            n -= glm.floor(n)

            data[x + y * texture.extent.x + z * texture.extent.x * texture.extent.y] = glm.floor(n * 255).b
        }
}

在jvm上占用超过4米。 cpp中的original sample使用OpenMp来加速计算。 我听说过coroutines,我希望在这种情况下我可以利用它们。

我首先尝试将整个for包装成runBlocking,因为我确实希望所有协同程序在我继续之前完成。

runBlocking {

    for (z in 0 until texture.extent.z) {
        println(z)
        for (y in 0 until texture.extent.y)
            for (x in 0 until texture.extent.x) {
                launch {
                    val v = Vec3(x, y, z) / texture.extent
                    var n = when {
                        FRACTAL -> FractalNoise().noise(v * noiseScale)
                        else -> 20f * glm.perlin(v)
                    }
                    n -= glm.floor(n)

                    data[x + y * texture.extent.x + z * texture.extent.x * texture.extent.y] = glm.floor(n * 255).b
                }
            }
    }
}

但这会引发不同的线程错误加上最终的jvm崩溃

[thread 27624 also had an error][thread 23784 also had an error]# A fatal error has been detected by the Java Runtime Environment:


#
[thread 27624 also had an error][thread 23784 also had an error]# A fatal error has been detected by the Java Runtime Environment:


#
#  [thread 14004 also had an error]EXCEPTION_ACCESS_VIOLATION
[thread 32652 also had an error] (0xc0000005)[thread 32616 also had an error]
 at pc=0x0000000002d2fd50
, pid=23452[thread 21264 also had an error], tid=0x0000000000007b68

#
# JRE version: Java(TM) SE Runtime Environment (8.0_144-b01) (build 1.8.0_144-b01)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.144-b01 mixed mode windows-amd64 compressed oops)
# Problematic frame:
# J 1431 C2 java.util.concurrent.ForkJoinPool$WorkQueue.runTask(Ljava/util/concurrent/ForkJoinTask;)V (86 bytes) @ 0x0000000002d2fd50 [0x0000000002d2f100+0xc50]
#
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\gBarbieri\IdeaProjects\Vulkan\hs_err_pid23452.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#

Process finished with exit code 1

我还尝试将所有job收集到一个arrayList中,最后收集join()个,但没有成功..

是否可以将coroutine用于像这样的并行任务? 如果是的话,我做错了什么?

1 个答案:

答案 0 :(得分:1)

您应该考虑JDK中内置的并行计算引擎java.util.stream,而不是协同程序。你在这里有一个令人尴尬的可并行化的任务,一个完美的用例。

我会沿着这些方向使用:

IntStream.range(0, extent.x)
        .boxed()
        .parallel()
        .flatMap { x ->
            IntStream.range(0, extent.y).boxed().flatMap { y ->
                IntStream.range(0, extent.z).mapToObj { z ->
                    Vec(x, y, z)
                }
            }
        }
        .forEach { vec ->
            data[vecToArrayIndex(vec)] = computeValue(vec)
        }