Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flang][OpenMP] Incorrect execution result of a do-variable defined as shared in parallel construct #78938

Closed
ohno-fj opened this issue Jan 22, 2024 · 4 comments · Fixed by #86194

Comments

@ohno-fj
Copy link

ohno-fj commented Jan 22, 2024

Version of flang-new : 18.0.0(0fe86f9c518fb1296bba8d66ce495f9dfff2c435)

A do-variable defined as shared in parallel construct has an incorrect value after executing parallel construct.
A do-variable is executing by private, not shared.

The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.

snggz567_22.f90:

subroutine s1
  ip=100
  ip2=-100
!$omp parallel shared(ip) private(ip2)
  DO ip = 0, 1
  END DO
  DO ip2 = 0, 1
  END DO
!$omp end parallel
  print *,'ip  shared  :',ip
  print *,'ip2 private :',ip2
end subroutine s1

program main
  call s1
end program main
$ export OMP_NUM_THREADS=2; flang-new -fopenmp snggz567_22.f90; ./a.out
 ip  shared  : 100
 ip2 private : -100
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp snggz567_22.f90; ./a.out
 ip  shared  :           2
 ip2 private :        -100
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp snggz567_22.f90; ./a.out
 ip  shared  :           2
 ip2 private :        -100
$
@ohno-fj ohno-fj added openmp flang Flang issues not falling into any other category labels Jan 22, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 22, 2024

@llvm/issue-subscribers-openmp

Author: None (ohno-fj)

``` Version of flang-new : 18.0.0(0fe86f9)

A `do-variable` defined as `shared` in `parallel` construct  has an incorrect value after executing `parallel` construct.  
A `do-variable` is executing by `private`, not `shared`.

The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.

snggz567_22.f90:
```fortran
subroutine s1
  ip=100
  ip2=-100
!$omp parallel shared(ip) private(ip2)
  DO ip = 0, 1
  END DO
  DO ip2 = 0, 1
  END DO
!$omp end parallel
  print *,'ip  shared  :',ip
  print *,'ip2 private :',ip2
end subroutine s1

program main
  call s1
end program main
$ export OMP_NUM_THREADS=2; flang-new -fopenmp snggz567_22.f90; ./a.out
 ip  shared  : 100
 ip2 private : -100
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp snggz567_22.f90; ./a.out
 ip  shared  :           2
 ip2 private :        -100
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp snggz567_22.f90; ./a.out
 ip  shared  :           2
 ip2 private :        -100
$

@mjklemm
Copy link
Contributor

mjklemm commented Jan 22, 2024

The code is non-confirming OpenMP code. There is a race condition on the ip variable.

@luporl
Copy link
Contributor

luporl commented Mar 21, 2024

Even removing the race condition, there is still an issue with flang, as the program below still prints ip shared : 100.

subroutine s1
  ip=100
!$omp parallel shared(ip)
!$omp single
  DO ip = 0, 1
  END DO
!$omp end single
!$omp end parallel
  print *,'ip  shared  :',ip
end subroutine s1

program main
  call s1
end program main

It seems the problem is that flang is treating ip as predetermined, even with the shared clause which seems to be allowed by OpenMP 5.2 5.1.1 in this case:

Loop iteration variables of loops that are not associated with any OpenMP directive may be listed in data-sharing attribute clauses on the surrounding teams, parallel or task generating construct, and on enclosed constructs, subject to other restrictions.

@luporl luporl self-assigned this Mar 21, 2024
luporl added a commit to luporl/llvm-project that referenced this issue Mar 21, 2024
Iteration variables of non-associated loops may be listed in DSA
clauses.

Fixes llvm#78938
@EugeneZelenko EugeneZelenko added flang:frontend flang:openmp and removed openmp flang Flang issues not falling into any other category labels Mar 21, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Mar 21, 2024

@llvm/issue-subscribers-flang-frontend

Author: None (ohno-fj)

``` Version of flang-new : 18.0.0(0fe86f9)

A `do-variable` defined as `shared` in `parallel` construct  has an incorrect value after executing `parallel` construct.  
A `do-variable` is executing by `private`, not `shared`.

The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.

snggz567_22.f90:
```fortran
subroutine s1
  ip=100
  ip2=-100
!$omp parallel shared(ip) private(ip2)
  DO ip = 0, 1
  END DO
  DO ip2 = 0, 1
  END DO
!$omp end parallel
  print *,'ip  shared  :',ip
  print *,'ip2 private :',ip2
end subroutine s1

program main
  call s1
end program main
$ export OMP_NUM_THREADS=2; flang-new -fopenmp snggz567_22.f90; ./a.out
 ip  shared  : 100
 ip2 private : -100
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp snggz567_22.f90; ./a.out
 ip  shared  :           2
 ip2 private :        -100
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp snggz567_22.f90; ./a.out
 ip  shared  :           2
 ip2 private :        -100
$

luporl added a commit that referenced this issue Mar 25, 2024
Iteration variables of non-associated loops may be listed in DSA
clauses.

Fixes #78938
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

5 participants