When the largest integer is used as the upper bound of a for loop which is
parallelised using OpenMP, thread n-1 will execute at most one iteration. The
code below demonstrates this by printing at most one message.

This is shown most clearly by first setting the OMP_NUM_THREADS environment
variable to 1. We would then expect ten "I get printed once or less." messages,
but we get only one. (With OMP_NUM_THREADS set to 2, we would again expect ten,
but only get six messages).

The OpenMP runtime library is used in this example only to make explicit that
the error relates to the last thread of the team. ( i.e. "use omp_lib" allows
us to call omp_get_thread_num() and omp_get_num_threads() )

I tried using the -fdump-tree-original switch, and it occurs to me that the use
of <= in the generated loops may play a part in this. ( OpenMP "for loop" tests
are permitted only to use <,<=,> or >= )

The example code follows, and is compiled with
gfortran -Wall -save-temps -fopenmp ompbug3.f95
gcc version 4.3.2 (Ubuntu 4.3.2-1ubuntu12) x86_64-linux-gnu Ubuntu 8.10 

----------------------------------------------------

program main

  use omp_lib
  integer(kind=4) :: i, lower, upper

  upper = huge(maxint)
  lower = upper - 9    ! The 9 can of course be changed

!$omp parallel do
  do i = lower, upper
    if (omp_get_thread_num() == (omp_get_num_threads() - 1)) then
      print *, "I get printed once or less."
    end if
  end do
!$end omp parallel do

end program

----------------------------------------------------


-- 
           Summary: OpenMP do loop with MAXINT gives wrong trip count
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: pkeir at dcs dot gla dot ac dot uk
 GCC build triplet: x86_64-linux-gnu
  GCC host triplet: x86_64-linux-gnu
GCC target triplet: x86_64-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40205

Reply via email to