【问题标题】:How can I implement coroutines for a parallel task如何为并行任务实现协程
【发布时间】:2018-05-08 07:35:15
【问题描述】:

所以,我有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 上占用了 4m。 cpp中的original sample使用OpenMp加速计算。 我听说过协程,我希望我能在这种情况下利用它们。

我首先尝试将整个 fors 包装到 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

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

协程可以用于像这样的并行任务吗? 如果是,我做错了什么?

【问题讨论】:

    标签: kotlin kotlin-coroutines


    【解决方案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)
            }
    

    【讨论】:

    • 我很挣扎,我不断收到Calls to static methods in Java interfaces are prohibited in JVM target 1.6. Recompile with '-jvm-target 1.8' .range()..
    • 您必须设置您的构建环境。目前,您已设置为以 Java 6 为目标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多