Arrays allocated by subroutines in parallel region

General OpenMP discussion

Arrays allocated by subroutines in parallel region

Postby olpo » Tue Jun 24, 2008 5:59 am

Hi,

I have a problem with a code where my parallel region contains a series of subroutines which allocates and deallocates allocatable variables declared in the main program.
The following example is a very schematic representation of the code. All variables in the parallel region are private, and work is done on them independently by the threads.

PROGRAM TEST
USE SUB_INTERFACE
IMPLICIT NONE
REAL(8),ALLOCATABLE :: V(:)
INTEGER(4) I
!$OMP PARALLEL
!$DO
DO I=1,10
CALL SUB(V)
ENDDO
!$END DO
!$OMP END PARALLEL
END PROGRAM TEST

SUBROUTINE SUB(V)
IMPLICIT NONE
REAL(8),ALLOCATABLE,INTENT(INOUT) :: V(:)
ALLOCATE(V(1000))
! Do some work on V here...
DEALLOCATE(V)
END SUBROUTINE

MODULE SUB_INTERFACE
INTERFACE
SUBROUTINE SUB(V)
REAL(8),ALLOCATABLE,INTENT(INOUT) :: V(:)
END SUBROUTINE
END INTERFACE
END MODULE

The program just crashes.

I use the Intel Fortran Compiler 10.
Any idea?

Thanks in advance for helping me out for this first post on the forum!

Olivier
olpo
 
Posts: 2
Joined: Tue Jun 24, 2008 5:17 am

Re: Arrays allocated by subroutines in parallel region

Postby ejd » Tue Jun 24, 2008 7:28 am

The problem with the code that you have presented here, is that variable "v" is shared and multiple threads are trying to allocate it. The do loop is split into multiple parts ("chunks"), with each chunk assigned to a thread. Each of these threads calls subroutine sub, passing v, which is then allocated in the subroutine. The first thread allocates it fine, but the next thread will find that it is already allocated. Most runtimes will stop at this point - because this is an error - with an error message.
ejd
 
Posts: 1025
Joined: Wed Jan 16, 2008 7:21 am

Re: Arrays allocated by subroutines in parallel region

Postby olpo » Tue Jun 24, 2008 8:21 am

Thanks ejd,

I thought the variables were PRIVATE by default... It turns out they are SHARED by default in the parallel region. Now my code works with this modification.

Thanks a lot!

Olivier
olpo
 
Posts: 2
Joined: Tue Jun 24, 2008 5:17 am


Return to Using OpenMP

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests