OpenMP for dependent variables

你说的曾经没有我的故事 提交于 2019-12-11 00:30:17

问题


This is the first time I am using OpenMP, and I apply it for Fortran. I happened to have a problem adjusting the loop where there is a variable that requires update from its previous value. I tried using PRIVATE clause but the result is far from those resulted by serial computation (without OpenMP).

I looked somewhere in OpenMP website and I found one solution using !$OMP PARALLEL DO ORDERED which finally works (produce the same result with the serial one). But it seems that by using this, the speed of computation is considerably slower than using just PRIVATE clause.

Is there any other way to apply OpenMP in such as a case to get maximum speed?

Codes using ORDERED option.

!$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

Codes using only PRIVATE clause

!$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

回答1:


As Francois pointed out in his comments, qx and mij need to be thread 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 does not need to be private, since access to it is only via the index i. so all threads access a different portion of it. However, if you need to access the elements afterwards, make sure it is public (shared).



来源:https://stackoverflow.com/questions/30421249/openmp-for-dependent-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!