title: OpenMP and shared variable in Fortran which are not shared
body: <p>I encounter a problem with OpenMP and shared variables I cannot understand. Everything I do is in Fortran 90/95.</p>
  
  <p>Here is my problem: I have a parallel region defined in my <code>main</code> program, with the clause <code>DEFAULT(SHARED)</code>, in which I call a subroutine that does some computation. I have a local variable (an array) I allocate and on which I do the computations. I was expecting this array to be shared (because of the <code>DEFAULT(SHARED)</code> clause), but it seems that it is not the case.</p>
  
  <p>Here is an example of what I am trying to do and that reproduce the error I get:</p>
  
  <pre><code>program main
    !$ use OMP_LIB
    implicit none
  
    integer, parameter :: nx=10, ny=10
    real(8), dimension(:,:), allocatable :: array
  
    !$OMP PARALLEL DEFAULT(SHARED)
    !$OMP SINGLE
    allocate(array(nx,ny))
    !$OMP END SINGLE
  
    !$OMP WORKSHARE
    array = 1.
    !$OMP END WORKSHARE
  
    call compute(array,nx,ny)
  
    !$OMP SINGLE
    deallocate(array)
    !$OMP END SINGLE
    !$OMP END PARALLEL
  
  contains
    !=============================================================================
    ! SUBROUTINES
    !=============================================================================
    subroutine compute(array, nx, ny)
      !$ use OMP_LIB
      implicit none
  
      real(8), dimension(nx,ny) :: array
      integer :: nx, ny
      real(8), dimension(:,:), allocatable :: q
      integer :: i, j
  
      !$OMP SINGLE
      allocate(q(nx,ny))
      !$OMP END SINGLE
  
      !$OMP WORKSHARE
      q = 0.
      !$OMP END WORKSHARE
  
      print*, 'q before: ', q(1,1)
  
      !$OMP DO SCHEDULE(RUNTIME)
      do j = 1, ny
         do i = 1, nx
            if(mod(i,j).eq.0) then
               q(i,j) = array(i,j)*2.
            else
               q(i,j) = array(i,j)*0.5
            endif
         end do
      end do
      !$OMP END DO
  
      print*, 'q after: ', q(1,1)
  
      !$OMP SINGLE
      deallocate(q)
      !$OMP END SINGLE
  
    end subroutine compute
    !=============================================================================
  end program main
  </code></pre>
  
  <p>When I execute it like that, I get a segmentation fault, because the local array <code>q</code> is allocated on one thread but not on the others, and when the others try to access it in memory, it crashes.</p>
  
  <p>If I get rid of the <code>SINGLE</code> region the local array <code>q</code> is allocated (though sometimes it crashes, which make sense, if different threads try to allocate it whereas it is already the case (and actually it puzzles me why it does not crash everytime)) but then it is clearly as if the array <code>q</code> is private (therefore one thread returns me the expected value, whereas the others return me something else).</p>
  
  <p>It really puzzled me why the <code>q</code> array is not shared although I declared my parallel region with the clause <code>DEFAULT(SHARED)</code>. And since I am in an orphaned subroutine, I cannot declare explicitely <code>q</code> as shared, since it is known only in the subroutine <code>compute</code>... I am stuck with this problem so far, I could not find a workaround.</p>
  
  <p>Is it normal? Should I expect this behaviour? Is there a workaround? Do I miss something obvious?</p>
  
  <p>Any help would be highly appreciated!</p>