java-romp / jromp

Java Runtime implementation of OpenMP.
https://java-romp.github.io/jromp/
MIT License
5 stars 0 forks source link

Multiple parallelFor makes the lib to crash when doing the merge for `ReductionVariables` #49

Closed scastd closed 2 months ago

scastd commented 2 months ago

Describe the bug

When doing a multi-parallelFor region, any ReductionVariable passed to the parallel block is reduced more than once.

Library version(s) affected

1.1.0

Steps to reproduce

Parallel.defaultConfig()
        .withVariables(variables)
        .parallelFor(0, m, false, (id, start, end, vars) -> {
            for (int i = start; i < end; i++) {
                // ...
            }
        })
        .parallelFor(0, m, false, (id, start, end, vars) -> {
            for (int i = start; i < end; i++) {
                // ...
            }
        })
        .join();

Additional context, environment

No response

Logs

Exception in thread "main" java.lang.IllegalStateException: ReductionVariable has already been merged.
    at jromp.parallel.var.ReductionVariable.merge(ReductionVariable.java:95)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
    at jromp.parallel.Parallel.join(Parallel.java:136)
    at jromp.examples.mandelbrot.MandelbrotParallel.$main(MandelbrotParallel.java:116)
    at jromp.examples.mandelbrot.MandelbrotParallel.main(MandelbrotParallel.java:17)
scastd commented 2 months ago

We can check if the variable is merged before calling it or simply return nothing in the method instead of throwing an exception.