Allocating module-data in subroutine called in par. region

General OpenMP discussion

Allocating module-data in subroutine called in par. region

Postby Andrew » Thu Oct 02, 2008 2:24 am

Dear users,

I have created the code below to reproduce an error I am receiving in a much larger and more complex program.
An Array is defined in a module. The main program calls a subroutine which uses this module. The Subroutine allocates the array, does something and deallocates it again. Without OpenMP or with $OMP_NUM_THREADS=1 this works fine. As soon as the subroutine is called by multiple threads, only the first subroutine can allocate, because the Array is obviously shared.
I wonder if there is any possiblity to set the scope of this array to private, without declaring and allocating it before the parallel section or even within its lexical extent.
I was hoping that if the module is "used" the first time in the subroutine which is called from within the parallel region, a default(private) should help, but it didnt. Any suggestions would be very welcome.

If this question was already discussed I am sorry and would be grateful if anybody could shoot me a link. I did search but did not find!
Here is the example code:

Code: Select all
module common
integer, dimension(:),allocatable :: field1
end module common

program hbtest
  !use common
  IMPLICIT NONE
  integer :: i

      !$omp parallel do default(private)
      do i = 1,8
         call sr
      end do
      !$omp end parallel do

      print*,'all good, goodbye'
end program hbtest


subroutine sr
  use common
  IMPLICIT NONE
  integer :: OMP_GET_THREAD_NUM
  integer:: i


  allocate(field1(20))

  !$ print*,OMP_GET_THREAD_NUM(),' is allocating'

  do i = 1,10000000
     field1(1) = i * i / (i*1.0)
  enddo

  !$ print*,OMP_GET_THREAD_NUM(),' is deallocating'
  deallocate(field1)

end subroutine sr


For simplicity i ran it with 2 Threads. The resulting error is:
forrtl: severe (151): allocatable array is already allocated


Thanks in advance
Andrew
 
Posts: 2
Joined: Thu Oct 02, 2008 1:58 am

Re: Allocating module-data in subroutine called in par. region

Postby ejd » Thu Oct 02, 2008 5:22 am

From the OpenMP V2.5 spec, section 2.8.3.1 default clause, Summary, p70:3-5:
The default clause allows the user to control the sharing attributes of variables which are referenced in a parallel construct, and whose sharing attributes are implicitly determined.

In your case, the key part of this is "implicitly determined". The variable "field1" is not implicitly determined. The other part, is that "field1" would not be affected by the "default(private)" or "private(field1)" because it is used in the called routine sr and not in hbtest.

From the OpenMP V2.5 spec, section 2.8.1.2 Sharing Attribute Rules for Variables Referenced in a Region, but not in a Construct, p65:28-30:
The sharing attributes of variables which are referenced in a region, but not in a construct, are determined as follows:
...
• Variables belonging to common blocks, or declared in modules, and referenced in called routines in the region are shared unless they appear in a threadprivate directive.

This applies to your program. You can see that "field1" is pre-determined to be shared. If you want it to be private, then you have to use a threadprivate clause like this:
Code: Select all
module common
integer, dimension(:),allocatable :: field1
!$omp threadprivate(field1)
end module common

Now each thread will get a private copy of "field1".
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Allocating module-data in subroutine called in par. region

Postby Andrew » Thu Oct 02, 2008 6:00 am

Thank you very much. I changed the code and it worked right away. You have been a great help. I'll take a closer look at the specs next time.

Regards, Andrew
Andrew
 
Posts: 2
Joined: Thu Oct 02, 2008 1:58 am


Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 12 guests