elect
elect

Reputation: 7190

How can I implement coroutines for a parallel task

So, I have 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
        }
}

That takes over 4m on the jvm. The original sample in cpp uses OpenMp to accelerate the calculation. I've heard about coroutines and I hope I could take advantage of them in this case.

I tried first to wrap the whole fors into a runBlocking because I do want that all the coroutines have finished before I move on.

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
                }
            }
    }
}

But this is throwing different thread errors plus a final jvm crash

[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

I tried also to collect all the jobs into an arrayList and join() them at the end, but without success..

May coroutine be used for a parallel task like this one? If yes, what am I doing wrong?

Upvotes: 0

Views: 497

Answers (1)

Marko Topolnik
Marko Topolnik

Reputation: 200138

Instead of coroutines you should consider the parallel computation engine built into the JDK: java.util.stream. What you have here is an embarrassingly parallelizable task, a perfect use case for it.

I'd use something along these lines:

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)
        }

Upvotes: 1

Related Questions