Race condition in Fortran's shared array

General OpenMP discussion

Race condition in Fortran's shared array

Postby wirawan0 » Mon Jul 14, 2008 2:23 pm


I saw on OpenMP spec 2.5, example A.25.1f, "A.25 Race Conditions Caused by Implied Copies of Shared Variables in Fortran". This example worries me quite a bit if the following code construct would introduce race condition in Fortran (Fortran 95):

Code: Select all
subroutine blah
  real :: A(10,10,16)
  integer :: i

! ... initialize A

!$omp parallel do shared(A) private(i)
  do i = 1, 16
    call do_something(A(:,:,i), 10, 10)
!$omp end parallel do
end subroutine blah

subroutine do_something(AA, n1, n2)
  integer, intent(in) :: n1, n2
  real, intent(inout) :: AA(n1,n2)
!  ... do read-write operation with AA (let's assume that this operation is thread-safe)
end subroutine do_something

In this case, the first and second array indices of A are "local" to each thread, since each thread will have a specific 3rd array index of A to work with. Will there be race condition here among the threads? I think there should not be, but the example in the OpenMP standard above worries me. Is it always guaranteed that there be no race condition in this case?


Re: Race condition in Fortran's shared array

Postby ejd » Mon Jul 21, 2008 6:18 am

The example is to illustrate a possible problem that can occur. Page 72 of the OpenMP V2.5 spec tries to outline the problem more fully. Unfortunately, there is no guarantee that a race will or will not occur under the given circumstances. It all depends on how the compiler optimizes the code and unfortunately since the program may or may not be compiled all at once, the compiler may not know that OpenMP is being used.

Your example seems to meet the three criteria on page 72, so it is possible that a race might occur. But like I said, it depends on the compiler. I tried it with the current development version of Sun Studio and it works correctly without a race. I however, can not guarantee that will always be the case. If this is really what you need to do, then try and make sure that at least one of the criteria on Page 72 doesn't hold. Another approach would be to make sure that the compiler you use doesn't copy the array under the given conditions - but this might change if an update to the compiler is made or you move to a new version of the compiler.
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 2 guests