java-romp / jromp

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

`ArrayIndexOutOfBoundsException` thrown when submitting fewer sections than threads #13

Closed scastd closed 1 month ago

scastd commented 1 month ago

Describe the bug

The implementation uses a for loop that iterates over the threads attribute of the Parallel instance, throwing an ArrayIndexOutOfBoundsException if there are more threads than sections.

https://github.com/java-romp/jromp/blob/4e5211c2487141861af51a06306080df06616332/src/main/java/jromp/parallel/Parallel.java#L220-L228

Library version(s) affected

0.0.1

Steps to reproduce

I created a test case that crashes.

@Test
void testSectionsMoreThreadsThanSections() {
    int threads = 4;
    int[] result = new int[threads];

    Parallel.withThreads(threads)
            .sections(
                    (id, vars) -> result[id] = 1,
                    (id, vars) -> result[id] = 2,
                    (id, vars) -> result[id] = 3
            )
            .join();

    assertThat(result).containsOnly(1, 2, 3, 0);
}

Additional context, environment

No response

Logs

java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3

    at jromp.parallel.Parallel.sections(Parallel.java:237)
    at jromp.parallel.Parallel.sections(Parallel.java:219)
    at jromp.parallel.ParallelTests.testSectionsMoreThreadsThanSections(ParallelTests.java:92)
    at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
scastd commented 1 month ago

I made a mess because this issue did not close when merging the pull request.