OpenMP for dependent variables
This is the first time I use OpenMP and I am using it for Fortran. I am having a problem setting up a loop where there is a variable that needs to be updated from a previous value. I tried to use the suggestion PRIVATE
, but the result is far from the results obtained with a sequential computation (without OpenMP).
I looked at the OpenMP site somewhere and I found one solution using !$OMP PARALLEL DO ORDERED
that finally works (produces the same result with sequential) But it seems that using this the computation speed is significantly slower than using suggestions alone PRIVATE
.
Is there any other way to use OpenMP, for example, to get maximum speed?
Codes using option ORDERED
.
!$OMP PARALLEL DO ORDERED PRIVATE(i,j)
do i = ca,cb
incre(i) = 0.0d0
value = 17.0d0*li(i)
do j = cx,cy
qx = hi(i) - hj(j)
mij = dsqrt(qx)
if( mij <= value ) then
beta = li(i)*li(j)
!$OMP ORDERED
incre(i) = incre(i) + beta
!$OMP END ORDERED
end if
end do
end do
!$OMP END PARALLEL DO
Offer-only codes PRIVATE
!$OMP PARALLEL DO PRIVATE(i,j,incre,value,beta)
do i = ca,cb
incre(i) = 0.0d0
value = 17.0d0*li(i)
do j = cx,cy
qx = hi(i) - hj(j)
mij = dsqrt(qx)
if( mij <= value ) then
beta = li(i)*li(j)
incre(i) = incre(i) + beta
end if
end do
end do
!$OMP END PARALLEL DO
source to share
As François pointed out in his comments, qx
and mij
should be streaming private:
!$OMP PARALLEL DO PRIVATE(i,j,value,beta,qx,mij)
do i = ca,cb
incre(i) = 0.0d0
value = 17.0d0*li(i)
do j = cx,cy
qx = hi(i) - hj(j)
mij = dsqrt(qx)
if( mij <= value ) then
beta = li(i)*li(j)
incre(i) = incre(i) + beta
end if
end do
end do
!$OMP END PARALLEL DO
incre
it does not have to be private as it is only accessed through the index i
. therefore all threads access the other part. However, if you need to access the items afterwards, make sure they are public (shared).
source to share