[Flang][OpenMP] Incorrect execution result when common block name is specified in copyin clause
Version of flang-new : 19.0.0(dd7386d85f11cf6ad911b9827c7018fb08c6c205)
When common block is specified in copyin clause, the execution result is incorrect.
The copyin directive appears to be invalid.
The above program is the first program (ompgal036_5.f90).
If a statement referencing an array declared in common block is added to parallel do construct with copyin clause that specifies common block, the execution result is correct.
The above program is the second program (ompgal036_52.f90).
The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.
ompgal036_5.f90:
program main
integer err
real,dimension(2):: a, b, c
common /blk/ a, b
!$omp threadprivate(/blk/)
a = (/(i,i=1,2)/)
b = (/(i*(i+1)/2,i=1,2)/)
c = 0
!$omp parallel do private(sum), copyin(/blk/)
do i=1, 2
sum = i
call sub(sum, i, c(i))
end do
!$omp end parallel do
err = 0
do i=1, 2
if(c(i) .ne. b(i)) err = err+1
end do
if(err .ne. 0) then
write(6,*) "NG"
else
write(6,*) "OK"
end if
end program main
subroutine sub(sum, i, c)
real,dimension(2):: a, b
common /blk/ a, b
real c
!$omp threadprivate(/blk/)
c = b(i)-a(i)+sum
write(6,*) "sub : b(",i,") = ",b(i), " a(",i,") = ", a(i)
end subroutine sub
$ export OMP_NUM_THREADS=2; flang-new -fopenmp ompgal036_5.f90; ./a.out
sub : b( 1 ) = 1. a( 1 ) = 1.
sub : b( 2 ) = 0. a( 2 ) = 0.
NG
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp ompgal036_5.f90; ./a.out
sub : b( 1 ) = 1.00000000 a( 1 ) = 1.00000000
sub : b( 2 ) = 3.00000000 a( 2 ) = 2.00000000
OK
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp -diag-disable=10448 ompgal036_5.f90; ./a.out
sub : b( 1 ) = 1.000000 a( 1 ) = 1.000000
sub : b( 2 ) = 3.000000 a( 2 ) = 2.000000
OK
$
ompgal036_52.f90:
program main
integer err
real,dimension(2):: a, b, c
common /blk/ a, b
!$omp threadprivate(/blk/)
a = (/(i,i=1,2)/)
b = (/(i*(i+1)/2,i=1,2)/)
c = 0
!$omp parallel do private(sum), copyin(/blk/)
do i=1, 2
write(6,*) "main : b(",i,") = ",b(i), " a(",i,") = ", a(i) ! add
sum = i
call sub(sum, i, c(i))
end do
!$omp end parallel do
err = 0
do i=1, 2
if(c(i) .ne. b(i)) err = err+1
end do
if(err .ne. 0) then
write(6,*) "NG"
else
write(6,*) "OK"
end if
end program main
subroutine sub(sum, i, c)
real,dimension(2):: a, b
common /blk/ a, b
real c
!$omp threadprivate(/blk/)
c = b(i)-a(i)+sum
write(6,*) "sub : b(",i,") = ",b(i), " a(",i,") = ", a(i)
end subroutine sub
$ export OMP_NUM_THREADS=2; flang-new -fopenmp ompgal036_52.f90; ./a.out
main : b( 1 ) = 1. a( 1 ) = 1.
sub : b( 1 ) = 1. a( 1 ) = 1.
main : b( 2 ) = 3. a( 2 ) = 2.
sub : b( 2 ) = 3. a( 2 ) = 2.
OK
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp ompgal036_52.f90; ./a.out
main : b( 1 ) = 1.00000000 a( 1 ) = 1.00000000
sub : b( 1 ) = 1.00000000 a( 1 ) = 1.00000000
main : b( 2 ) = 3.00000000 a( 2 ) = 2.00000000
sub : b( 2 ) = 3.00000000 a( 2 ) = 2.00000000
OK
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp -diag-disable=10448 ompgal036_52.f90; ./a.out
main : b( 1 ) = 1.000000 a( 1 ) = 1.000000
sub : b( 1 ) = 1.000000 a( 1 ) = 1.000000
main : b( 2 ) = 3.000000 a( 2 ) = 2.000000
sub : b( 2 ) = 3.000000 a( 2 ) = 2.000000
OK
$
@llvm/issue-subscribers-flang-frontend
Author: None (ohno-fj)
If a statement referencing an array declared in common block is added to parallel do construct with copyin clause that specifies common block, the execution result is correct.
The above program is the second program (ompgal036_52.f90).
The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.
ompgal036_5.f90:
program main
integer err
real,dimension(2):: a, b, c
common /blk/ a, b
!$omp threadprivate(/blk/)
a = (/(i,i=1,2)/)
b = (/(i*(i+1)/2,i=1,2)/)
c = 0
!$omp parallel do private(sum), copyin(/blk/)
do i=1, 2
sum = i
call sub(sum, i, c(i))
end do
!$omp end parallel do
err = 0
do i=1, 2
if(c(i) .ne. b(i)) err = err+1
end do
if(err .ne. 0) then
write(6,*) "NG"
else
write(6,*) "OK"
end if
end program main
subroutine sub(sum, i, c)
real,dimension(2):: a, b
common /blk/ a, b
real c
!$omp threadprivate(/blk/)
c = b(i)-a(i)+sum
write(6,*) "sub : b(",i,") = ",b(i), " a(",i,") = ", a(i)
end subroutine sub
$ export OMP_NUM_THREADS=2; flang-new -fopenmp ompgal036_5.f90; ./a.out
sub : b( 1 ) = 1. a( 1 ) = 1.
sub : b( 2 ) = 0. a( 2 ) = 0.
NG
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp ompgal036_5.f90; ./a.out
sub : b( 1 ) = 1.00000000 a( 1 ) = 1.00000000
sub : b( 2 ) = 3.00000000 a( 2 ) = 2.00000000
OK
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp -diag-disable=10448 ompgal036_5.f90; ./a.out
sub : b( 1 ) = 1.000000 a( 1 ) = 1.000000
sub : b( 2 ) = 3.000000 a( 2 ) = 2.000000
OK
$
ompgal036_52.f90:
program main
integer err
real,dimension(2):: a, b, c
common /blk/ a, b
!$omp threadprivate(/blk/)
a = (/(i,i=1,2)/)
b = (/(i*(i+1)/2,i=1,2)/)
c = 0
!$omp parallel do private(sum), copyin(/blk/)
do i=1, 2
write(6,*) "main : b(",i,") = ",b(i), " a(",i,") = ", a(i) ! add
sum = i
call sub(sum, i, c(i))
end do
!$omp end parallel do
err = 0
do i=1, 2
if(c(i) .ne. b(i)) err = err+1
end do
if(err .ne. 0) then
write(6,*) "NG"
else
write(6,*) "OK"
end if
end program main
subroutine sub(sum, i, c)
real,dimension(2):: a, b
common /blk/ a, b
real c
!$omp threadprivate(/blk/)
c = b(i)-a(i)+sum
write(6,*) "sub : b(",i,") = ",b(i), " a(",i,") = ", a(i)
end subroutine sub
$ export OMP_NUM_THREADS=2; flang-new -fopenmp ompgal036_52.f90; ./a.out
main : b( 1 ) = 1. a( 1 ) = 1.
sub : b( 1 ) = 1. a( 1 ) = 1.
main : b( 2 ) = 3. a( 2 ) = 2.
sub : b( 2 ) = 3. a( 2 ) = 2.
OK
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp ompgal036_52.f90; ./a.out
main : b( 1 ) = 1.00000000 a( 1 ) = 1.00000000
sub : b( 1 ) = 1.00000000 a( 1 ) = 1.00000000
main : b( 2 ) = 3.00000000 a( 2 ) = 2.00000000
sub : b( 2 ) = 3.00000000 a( 2 ) = 2.00000000
OK
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp -diag-disable=10448 ompgal036_52.f90; ./a.out
main : b( 1 ) = 1.000000 a( 1 ) = 1.000000
sub : b( 1 ) = 1.000000 a( 1 ) = 1.000000
main : b( 2 ) = 3.000000 a( 2 ) = 2.000000
sub : b( 2 ) = 3.000000 a( 2 ) = 2.000000
OK
$
@llvm/issue-subscribers-openmp
Author: None (ohno-fj)
If a statement referencing an array declared in common block is added to parallel do construct with copyin clause that specifies common block, the execution result is correct.
The above program is the second program (ompgal036_52.f90).
The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.
ompgal036_5.f90:
program main
integer err
real,dimension(2):: a, b, c
common /blk/ a, b
!$omp threadprivate(/blk/)
a = (/(i,i=1,2)/)
b = (/(i*(i+1)/2,i=1,2)/)
c = 0
!$omp parallel do private(sum), copyin(/blk/)
do i=1, 2
sum = i
call sub(sum, i, c(i))
end do
!$omp end parallel do
err = 0
do i=1, 2
if(c(i) .ne. b(i)) err = err+1
end do
if(err .ne. 0) then
write(6,*) "NG"
else
write(6,*) "OK"
end if
end program main
subroutine sub(sum, i, c)
real,dimension(2):: a, b
common /blk/ a, b
real c
!$omp threadprivate(/blk/)
c = b(i)-a(i)+sum
write(6,*) "sub : b(",i,") = ",b(i), " a(",i,") = ", a(i)
end subroutine sub
$ export OMP_NUM_THREADS=2; flang-new -fopenmp ompgal036_5.f90; ./a.out
sub : b( 1 ) = 1. a( 1 ) = 1.
sub : b( 2 ) = 0. a( 2 ) = 0.
NG
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp ompgal036_5.f90; ./a.out
sub : b( 1 ) = 1.00000000 a( 1 ) = 1.00000000
sub : b( 2 ) = 3.00000000 a( 2 ) = 2.00000000
OK
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp -diag-disable=10448 ompgal036_5.f90; ./a.out
sub : b( 1 ) = 1.000000 a( 1 ) = 1.000000
sub : b( 2 ) = 3.000000 a( 2 ) = 2.000000
OK
$
ompgal036_52.f90:
program main
integer err
real,dimension(2):: a, b, c
common /blk/ a, b
!$omp threadprivate(/blk/)
a = (/(i,i=1,2)/)
b = (/(i*(i+1)/2,i=1,2)/)
c = 0
!$omp parallel do private(sum), copyin(/blk/)
do i=1, 2
write(6,*) "main : b(",i,") = ",b(i), " a(",i,") = ", a(i) ! add
sum = i
call sub(sum, i, c(i))
end do
!$omp end parallel do
err = 0
do i=1, 2
if(c(i) .ne. b(i)) err = err+1
end do
if(err .ne. 0) then
write(6,*) "NG"
else
write(6,*) "OK"
end if
end program main
subroutine sub(sum, i, c)
real,dimension(2):: a, b
common /blk/ a, b
real c
!$omp threadprivate(/blk/)
c = b(i)-a(i)+sum
write(6,*) "sub : b(",i,") = ",b(i), " a(",i,") = ", a(i)
end subroutine sub
$ export OMP_NUM_THREADS=2; flang-new -fopenmp ompgal036_52.f90; ./a.out
main : b( 1 ) = 1. a( 1 ) = 1.
sub : b( 1 ) = 1. a( 1 ) = 1.
main : b( 2 ) = 3. a( 2 ) = 2.
sub : b( 2 ) = 3. a( 2 ) = 2.
OK
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp ompgal036_52.f90; ./a.out
main : b( 1 ) = 1.00000000 a( 1 ) = 1.00000000
sub : b( 1 ) = 1.00000000 a( 1 ) = 1.00000000
main : b( 2 ) = 3.00000000 a( 2 ) = 2.00000000
sub : b( 2 ) = 3.00000000 a( 2 ) = 2.00000000
OK
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp -diag-disable=10448 ompgal036_52.f90; ./a.out
main : b( 1 ) = 1.000000 a( 1 ) = 1.000000
sub : b( 1 ) = 1.000000 a( 1 ) = 1.000000
main : b( 2 ) = 3.000000 a( 2 ) = 2.000000
sub : b( 2 ) = 3.000000 a( 2 ) = 2.000000
OK
$
Hey @ceseo, how far have you been working on this issue? (I have analysed the issue and currently finding a place to add a required patch.) I also started working on this and forgot to self-assign. Sorry about that.
Here is a MRE:
program test
implicit none
integer :: x, y, i
common /d/ x, y
!$omp threadprivate(/d/)
x = 4
y = 3
!$omp parallel num_threads(5) copyin(/d/)
! print *, x, y
call sub
!$omp end parallel
contains
subroutine sub()
integer :: x, y
common /d/ x, y
!$omp threadprivate(/d/)
print *, x, y
end subroutine sub
end program test
I have a fix, I will submit a PR soon.
@Thirumalai-Shaktivel np, I'll leave you to it.
@llvm/issue-subscribers-flang-ir
Author: None (ohno-fj)
If a statement referencing an array declared in common block is added to parallel do construct with copyin clause that specifies common block, the execution result is correct.
The above program is the second program (ompgal036_52.f90).
The following are the test program, Flang-new, Gfortran and ifort compilation/execution result.
ompgal036_5.f90:
program main
integer err
real,dimension(2):: a, b, c
common /blk/ a, b
!$omp threadprivate(/blk/)
a = (/(i,i=1,2)/)
b = (/(i*(i+1)/2,i=1,2)/)
c = 0
!$omp parallel do private(sum), copyin(/blk/)
do i=1, 2
sum = i
call sub(sum, i, c(i))
end do
!$omp end parallel do
err = 0
do i=1, 2
if(c(i) .ne. b(i)) err = err+1
end do
if(err .ne. 0) then
write(6,*) "NG"
else
write(6,*) "OK"
end if
end program main
subroutine sub(sum, i, c)
real,dimension(2):: a, b
common /blk/ a, b
real c
!$omp threadprivate(/blk/)
c = b(i)-a(i)+sum
write(6,*) "sub : b(",i,") = ",b(i), " a(",i,") = ", a(i)
end subroutine sub
$ export OMP_NUM_THREADS=2; flang-new -fopenmp ompgal036_5.f90; ./a.out
sub : b( 1 ) = 1. a( 1 ) = 1.
sub : b( 2 ) = 0. a( 2 ) = 0.
NG
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp ompgal036_5.f90; ./a.out
sub : b( 1 ) = 1.00000000 a( 1 ) = 1.00000000
sub : b( 2 ) = 3.00000000 a( 2 ) = 2.00000000
OK
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp -diag-disable=10448 ompgal036_5.f90; ./a.out
sub : b( 1 ) = 1.000000 a( 1 ) = 1.000000
sub : b( 2 ) = 3.000000 a( 2 ) = 2.000000
OK
$
ompgal036_52.f90:
program main
integer err
real,dimension(2):: a, b, c
common /blk/ a, b
!$omp threadprivate(/blk/)
a = (/(i,i=1,2)/)
b = (/(i*(i+1)/2,i=1,2)/)
c = 0
!$omp parallel do private(sum), copyin(/blk/)
do i=1, 2
write(6,*) "main : b(",i,") = ",b(i), " a(",i,") = ", a(i) ! add
sum = i
call sub(sum, i, c(i))
end do
!$omp end parallel do
err = 0
do i=1, 2
if(c(i) .ne. b(i)) err = err+1
end do
if(err .ne. 0) then
write(6,*) "NG"
else
write(6,*) "OK"
end if
end program main
subroutine sub(sum, i, c)
real,dimension(2):: a, b
common /blk/ a, b
real c
!$omp threadprivate(/blk/)
c = b(i)-a(i)+sum
write(6,*) "sub : b(",i,") = ",b(i), " a(",i,") = ", a(i)
end subroutine sub
$ export OMP_NUM_THREADS=2; flang-new -fopenmp ompgal036_52.f90; ./a.out
main : b( 1 ) = 1. a( 1 ) = 1.
sub : b( 1 ) = 1. a( 1 ) = 1.
main : b( 2 ) = 3. a( 2 ) = 2.
sub : b( 2 ) = 3. a( 2 ) = 2.
OK
$
$ export OMP_NUM_THREADS=2; gfortran -fopenmp ompgal036_52.f90; ./a.out
main : b( 1 ) = 1.00000000 a( 1 ) = 1.00000000
sub : b( 1 ) = 1.00000000 a( 1 ) = 1.00000000
main : b( 2 ) = 3.00000000 a( 2 ) = 2.00000000
sub : b( 2 ) = 3.00000000 a( 2 ) = 2.00000000
OK
$
$ export OMP_NUM_THREADS=2; ifort -qopenmp -diag-disable=10448 ompgal036_52.f90; ./a.out
main : b( 1 ) = 1.000000 a( 1 ) = 1.000000
sub : b( 1 ) = 1.000000 a( 1 ) = 1.000000
main : b( 2 ) = 3.000000 a( 2 ) = 2.000000
sub : b( 2 ) = 3.000000 a( 2 ) = 2.000000
OK
$