java-romp / jromp

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

Private variable stores only the default value #32

Closed scastd closed 2 months ago

scastd commented 2 months ago

Describe the bug

The variable should keep the value passed to it in the constructor.

OpenMP reference (5.3 List Item Privatization):

The value and/or allocation status of the original list item will change only:

  • If accessed and modified via a pointer;
  • If possibly accessed in the region but outside of the construct;
  • As a side effect of directives or clauses.

Library version(s) affected

1.0.0

Steps to reproduce

public static void main(String[] args) {
    Variable<Integer> privateVariable = new PrivateVariable<>(0);
    privateVariable.set(-1);
    Variables variables = Variables.create().add("privateVariable", privateVariable);

    Parallel.defaultConfig()
            .block(variables, (id, vars) -> {
                Variable<Integer> privateVar = vars.get("privateVariable");
                privateVar.set(id);
                System.out.printf("Thread %d\n", privateVar.value());
            })
            .join();

    System.out.printf("Private variable %d%n", privateVariable.value());
}

In this code, the value of the private variable is restored to its default value after all threads have completed their execution.

Additional context, environment

For example, in this C code snippet the value -1 is kept until the end of the program:

#include <stdio.h>
#include <omp.h>

int main(int argc, char *argv[]) {
    int private_variable = -1;

    #pragma omp parallel private(private_variable)
    {
        private_variable = omp_get_thread_num();
        printf("Thread %d\n", private_variable);
    }

    printf("Private variable: %d\n", private_variable);

    return 0;
}

Logs

In the Java code:

Thread 0
Thread 1
Thread 2
Thread 3
Thread 4
Thread 5
Thread 6
Thread 7
Thread 8
Thread 9
Thread 10
Thread 11
Private variable 0

-------------------------------------------------------------

In the C code:

Thread 8
Thread 1
Thread 6
Thread 3
Thread 4
Thread 2
Thread 11
Thread 0
Thread 5
Thread 10
Thread 9
Thread 7
Private variable: -1
scastd commented 2 months ago

This can be done in the FirstPrivateVariable and LastPrivateVariable. Remove the call to InitialValues.getInitialValue.