https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102698

            Bug ID: 102698
           Summary: omp atomic capture Abnormal results after running
                    multiple times
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: han...@compiler-dev.com
  Target Milestone: ---

test case:
program main
  interface
    subroutine aaaa_ieor(x, o, idx, idy, n)
      integer(2) :: x(8, 2), o(8, 2)
      integer :: n, idx(*), idy(*)
    end subroutine
  end interface
  integer, parameter :: n = 64, n1 = 8, n2 = 2
  integer(2) :: x(8, 2), o(8, 2), expect1(8, 2), a(64), b(64), c(64)
  integer :: idx(n), idy(n)
  logical(1) :: rst(32), res(32)
  integer :: i, j

  do i = 1, n
    idx(i) = mod(i, n1) + 1
    if (i > 32) then
      idy(i) = mod(i, n2) + 1
    else
      idy(i) = mod(i + 1, n2) + 1
    end if
  end do

  expect1 = reshape([120 ,25, 58, 27, 60, 29, 62, 31, &
    56, 57, 26, 59, 28, 61, 30, 63], (/8, 2/))
  x = 0
  call aaaa_ieor(x, o, idx, idy, n)

  res(1:16) = reshape((x .eq. expect1), (/16/))
  res(17:32) = reshape((o .eq. expect1), (/16/))
  !print *, x
  do i = 1, 32
    if (res(i) .neqv. .true.) print *,  i 
  end do

  if (any(res .neqv. .true.)) stop 1
    !print *, "PASS"

end program

function fun(i) result(c)
  integer :: i
  integer(2) :: c
  c = i
end function

subroutine aaaa_ieor(x, o, idx, idy, n)
  integer(2) :: x(8, 2), o(8, 2)
  integer :: n, idx(*), idy(*)

  interface
    function fun(i) result(c)
      integer :: i
      integer(2) :: c
    end function
  end interface
  !num_threads(4)
  !$omp parallel do shared(x, o)
  do i = 1, 64
    !$omp atomic capture
    x(idx(i), idy(i)) = ior(x(idx(i), idy(i)), fun(i))
    o(idx(i), idy(i)) = x(idx(i), idy(i))
    !$omp end atomic
  end do
end subroutine


next is the shell to exec:
#!/bin/bash
gfortran  -fopenmp    ieor1.f90
for((i=1;i<=1000;i++))
do
 ./a.out
done

Once executed 1,000 times in one shell, the result may be wrong one time
If you are lucky, run a.out 6000 times may only one result is wrong

How did the error happen? Looking forward to your help

Reply via email to