1
votes

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
1
"but it seems that !$omp parallel workshare doesn't work for allocatable array. " Why? What does mean "it seems"? What does mean "it doesn't work"? Don't use these phrase which don't mean anything. Tell us what have you tried and what errors have you encountered. BTW, real(8) is ugly: stackoverflow.com/questions/838310/fortran-90-kind-parameter/… stackoverflow.com/questions/3170239/… - Vladimir F
It means that the program run in serial without errors and the compiler doesn't take account of the line "!$omp parallel workshare". - x1hgg1x

1 Answers

4
votes

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.