title: What will be the proper assigning of variables (private and shared) in the parallelized do loop of the given subroutine GAUSSLEG?
body: <p>I am new about openmp. I am trying to parallelize do loop in subroutine GAUSSLEG. Variables Xg, Wg and Ng are taken from module matric. I am getting the unexpected results. I am confused about proper assigning of  variables(private and shared). Can somebody help me ?</p>
  
  <pre><code>SUBROUTINE GAUSSLEG(f,a,b,s)
       USE OMP_LIB
       USE MATRIC , ONLY : XG ,WG , NG
       IMPLICIT DOUBLE PRECISION(A-H,O-Z)
       external f
       xm = 0.5d0*(b+a)
       xl = 0.5d0*(b-a)
       s = 0.d0
      !$omp parallel do reduction ( + : s) default(none)
      !$omp private(j) shared(xm,xl,wg,xg,ng,dx)
  
       do  j=1,ng
           dx = xl*xg(j)
           s = s + wg(j)*(func(xm+dx)+func(xm-dx))
       end do
     !$omp end parallel do
  
      s = xl*s/2.0
      return
    END
  </code></pre>
  
  <hr>
  
  <p>Hi, I have used the subroutine gaussleg to calculate the integration of sin(x) from 0 to pi, I get the same result (2.5464790894) whether i make dx private or shared but the exact result is 2.0.   I have also tried by putting xl*xg(j) directly and removing dx,  still getting same result as above.Without -openmp option in the compilation,  i get  the exact result 2.0.This is whole program.</p>
  
  <pre><code>  MODULE MATRIC
     IMPLICIT NONE
      INTEGER , PARAMETER :: NG = 40
      DOUBLE PRECISION , PARAMETER :: PI=2.0D0*ACOS(0.0D0)
      DOUBLE PRECISION ::  XG(60) , WG(60)
    END MODULE MATRIC
    program gauss
     use matric, only : xg,wg,pi
     implicit none
     double precision :: x1,x2,a,b,ans
     external :: f
     x1 = -1.0d0 ; x2 = 1.0d0
     a  = 0.0    ; b  = PI
     call gauleg(x1,x2)
     call gaussleg(f,a,b,ans)
     write(*,*)ans
    end program gauss
    !function to be integrated
    double precision function f(x)
          implicit none
          double precision, intent(in) :: x
          f = sin(x)
    end function f
    SUBROUTINE GAUSSLEG(func,a,b,ss)
       USE OMP_LIB
       USE MATRIC , ONLY : XG ,WG , NG
       double precision,intent(in) :: a , b
       double precision,intent(out)::ss
       double precision :: xm , xl , dx
       integer :: j
       double precision,external::func
       xm = 0.5d0*(b+a)
       xl = 0.5d0*(b-a)
       ss = 0.d0
      !$OMP PARALLEL DO REDUCTION( + : ss) default(none) &amp;
      !$OMP PRIVATE(j,dx) SHARED(xm,xl,xg,wg)
         do  j=1,ng
             dx = xl*xg(j)
             ss = ss + wg(j)*(func(xm+dx)+func(xm-dx))
         end do
      !$OMP END PARALLEL DO
        ss = xl*ss/2.0
        return
        END
  </code></pre>