Search code examples
fortranopenmpallocatable-array

Openmp parallel workshare for allocatable array


I want to do some element-wise calculation on arrays in Fortran 90, while parallelize my code with openmp. I have now the following code :

program test
implicit none

integer,parameter :: n=50
integer :: i
integer(8) :: t1,t2,freq
real(8) :: seq(n),r(n,n,n,n)
real(8),dimension(n,n,n,n) :: x

call system_clock(COUNT_RATE=freq)

seq=[(i,i=1,n)]
x=spread(spread(spread(seq,2,n),3,n),4,n)

call system_clock(t1)

!$omp parallel workshare
! do some array calculation
r=atan(exp(-x))
!$omp end parallel workshare

call system_clock(t2)

print*, sum(r)
print '(f6.3)',(t2-t1)/real(freq)

end program test

I want now to replace the static arrays x and r with allocatable arrays, so I type :

real(8),dimension(:,:,:,:),allocatable :: x,r
allocate(x(n,n,n,n))
allocate(r(n,n,n,n))

but that the program run in serial without errors and the compiler doesn't take account of the line "!$omp parallel workshare".

What options should I use to parallelize in this case? I have tried with omp parallel do with loops but it is much slower.

I am compiling my code with gfortran 5.1.0 on windows :

gfortran -ffree-form test.f -o main.exe -O3 -fopenmp -fno-automatic

Solution

  • I have come across this issue in gfortran before. The solution is to specify the array in the following form:

    !$omp parallel workshare
    ! do some array calculation
    r(:,:,:,:) = atan(exp(-x))
    !$omp end parallel workshare
    

    Here is the reference.