llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Flang][OpenMP] Incorrect execution result of a firstprivate variable in parallel construct

Open ohno-fj opened this issue 2 years ago • 3 comments

Version of flang-new : 18.0.0(770dc47659d41a5ca7b7daf5b3134c900ca8c33d)

The value of a firstprivate variable (a) in parallel construct executed by each thread is incorrect.

The following write statement expects that the value of variable (a) is 200 for all threads.

  write(6,*) "2 : a = ", a

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

sngf_firstprivate11_42.f90:

subroutine sub4()
  integer :: a = 100
!$omp parallel reduction(max:a)
  a = 200
!$omp parallel firstprivate(a)
  if (a/=200) print *,14,a
  a = 300
!$omp end parallel
  write(6,*) "2 : a = ", a
  if (a/=200) print *,24,a
  a = 400
!$omp end parallel
  write(6,*) "3 : a = ", a
end subroutine sub4

program main
  call sub4
end program main
$ export OMP_NUM_THREADS=2; flang-new -fopenmp sngf_firstprivate11_42.f90; ./a.out
 2 : a =  200
 2 : a =  400
 24 400
 3 : a =  400
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp sngf_firstprivate11_42.f90; ./a.out
 2 : a =          200
 2 : a =          200
 3 : a =          400
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp sngf_firstprivate11_42.f90; ./a.out
 2 : a =          200
 2 : a =          200
 3 : a =          400
$

ohno-fj avatar Dec 12 '23 04:12 ohno-fj

@llvm/issue-subscribers-openmp

Author: None (ohno-fj)

``` Version of flang-new : 18.0.0(770dc47659d41a5ca7b7daf5b3134c900ca8c33d) ``` The value of a `firstprivate` variable (`a`) in `parallel` construct executed by each thread is incorrect.

The following write statement expects that the value of variable (a) is 200 for all threads.

  write(6,*) "2 : a = ", a

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

sngf_firstprivate11_42.f90:

subroutine sub4()
  integer :: a = 100
!$omp parallel reduction(max:a)
  a = 200
!$omp parallel firstprivate(a)
  if (a/=200) print *,14,a
  a = 300
!$omp end parallel
  write(6,*) "2 : a = ", a
  if (a/=200) print *,24,a
  a = 400
!$omp end parallel
  write(6,*) "3 : a = ", a
end subroutine sub4

program main
  call sub4
end program main
$ export OMP_NUM_THREADS=2; flang-new -fopenmp sngf_firstprivate11_42.f90; ./a.out
 2 : a =  200
 2 : a =  400
 24 400
 3 : a =  400
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp sngf_firstprivate11_42.f90; ./a.out
 2 : a =          200
 2 : a =          200
 3 : a =          400
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp sngf_firstprivate11_42.f90; ./a.out
 2 : a =          200
 2 : a =          200
 3 : a =          400
$

llvmbot avatar Dec 12 '23 04:12 llvmbot

This seems to work alrite. Could @ohno-fj or @luporl check and close if it is fine?

kiranchandramohan avatar May 21 '24 14:05 kiranchandramohan

This is fixed.

ceseo avatar May 21 '24 16:05 ceseo