Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28334 Discussions

Accessing global variables from a subroutine called from OpenMP task

mcgavinp
Beginner
390 Views

Is it legal/valid to access global variables from a subroutine called from an OpenMP task?

ifort 2021.7.0 20220726 doesn't report an error, but appears to produce random results with -check all.  Example:

program test1
  implicit none

  integer :: g, i, j
  g = 42

!$OMP PARALLEL DEFAULT(SHARED)
!$OMP SINGLE
  i = 0
  j = 1
  do while(j<60)
     i = i + 1
!$OMP TASK DEFAULT(SHARED) FIRSTPRIVATE(i,j)
     call sub(i,j)
!$OMP END TASK
     j = j + j
  end do
!$OMP END SINGLE
!$OMP END PARALLEL
  stop

contains

  subroutine sub(i,j)
    implicit none
    integer i,j
!$OMP CRITICAL(unit6)
    write(6,*) i,j,g
!$OMP END CRITICAL(unit6)
  end subroutine sub

end program test1

 Compiled with:  ifort -o test1 test1.f90 -qopenmp -warn all -check all

Expected result:

           5          16          42
           2           2          42
           6          32          42
           1           1          42
           4           8          42
           3           4          42

Obtained result:

           2           2  -858993460
           4           8  -858993460
           6          32  -858993460
           5          16  -858993460
           1           1  -858993460
           3           4  -858993460

 Note: the order lines are output doesn't matter --- just the number in the third column should be 42.

Of course g could be passed to sub() as an argument, but the program I'm assisting with working on has dozens of shared global variables (that don't change in the parallel part) and subroutine calls go several layers deep.

Thanks, Peter McGavin.

0 Kudos
1 Reply
Barbara_P_Intel
Moderator
371 Views

This is similar to the issue you reported in this thread. Using "-O0" or "-check all" causes the wrong answers. "-check all" disables optimizations.

I added your reproducer to the bug report in that thread.

 

0 Kudos
Reply