omp sum减少不更新变量

时间:2014-05-29 15:55:38

标签: fortran openmp fortran90

我在代码中使用OpenMP减少时遇到问题。

en_par = 0.0d0

!$omp parallel do reduction(+:en_par) default(private) shared(r,listvar,it,ic)  

!--- loop over neighboring cells
do cell_index = 1,26       

   !-- new_cell is an int. Neighbor of cell ic
   new_cell = listvar%cv(cell_index,ic)%cnum

   !--- loop over atoms in cell neigh_cell
   do j = 1, listvar%cl(new_cell)%num

      !--- pick particle in the cell list
      !--- particle is an integer
      particle = listvar%cl(new_cell)%cmem(j)

      !--- obtain displacements
      !--- apply minimum image here
      !--- min variables are doubls
      dx = r(1,particle)-r(1,it)-listvar%cv(cell_index,ic)%min_x
      dy = r(2,particle)-r(2,it)-listvar%cv(cell_index,ic)%min_y
      dz = r(3,particle)-r(3,it)-listvar%cv(cell_index,ic)%min_z

      dxmin = r(1,particle)-r(1,it)
      dymin = r(2,particle)-r(2,it)
      dzmin = r(3,particle)-r(3,it)


      dr2 = dx*dx+dy*dy+dz*dz
      if(dr2.lt.param%rcut2)then

         dr2i  = 1.0d0/dr2
         dr6i  = dr2i*dr2i*dr2i
         dr12i = dr6i*dr6i
         en_par = en_par + dr12i-dr6i


      endif
   enddo 
   print*,'enpar inside',en_par      
enddo
!$omp end parallel do

每当我使用-fopenmp编译并运行时,en_par0.0打印到屏幕上。但是,当在串行执行中运行时,该值不为零。谁能告诉我为什么?

1 个答案:

答案 0 :(得分:2)

缩小变量(此处en_par)无法在parallel do内访问,您必须在完成后打印它。