OpenMP Fortran默认条款

时间:2013-02-03 01:29:28

标签: fortran openmp

此并行工作正常。

!$OMP PARALLEL Private(irep)
!$OMP DO
do irep = 1, nrep
  print *, "Using thread: ", omp_get_thread_num(), "irep: ", irep
end do
!$OMP END DO NOWAIT
!$OMP END PARALLEL

这也很好。

!$OMP PARALLEL
!$OMP DO
do irep = 1, nrep
  print *, "Using thread: ", omp_get_thread_num(), "irep: ", irep
end do
!$OMP END DO NOWAIT
!$OMP END PARALLEL

为什么在使用Default子句时它什么都不返回?

!$OMP PARALLEL DEFAULT(Private)
!$OMP DO
do irep = 1, nrep
  print *, "Using thread: ", omp_get_thread_num(), "irep: ", irep
end do
!$OMP END DO NOWAIT
!$OMP END PARALLEL

非常感谢你!

1 个答案:

答案 0 :(得分:6)

让我们来看一个更简单的案例:

program testprivate
use omp_lib

integer :: nrep
nrep=16

!$OMP PARALLEL DEFAULT(Private)
print *, "Thread: ", omp_get_thread_num(), "sees nrep = ", nrep
!$OMP END PARALLEL

end program testprivate

我们运行并得到:

$ gfortran -o private private.f90 -fopenmp
$ export OMP_NUM_THREADS=8
$ ./private
 Thread:            3 sees nrep =            0
 Thread:            0 sees nrep =            0
 Thread:            1 sees nrep =        32581
 Thread:            7 sees nrep =            0
 Thread:            4 sees nrep =            0
 Thread:            5 sees nrep =            0
 Thread:            2 sees nrep =            0
 Thread:            6 sees nrep =            0

OpenMP private variables,默认情况下是私有的,或者在进入私有部分时未定义。这对于在do循环中设置的循环索引irep无关紧要;但是如果(比方说)你的编译器在私有部分内将每个线程的nrep设置为零,则循环将永远不会执行。更糟糕的是,每个线程都可能有不同的nrep值,任何事情都可能发生。

因此,您不希望nrep成为private。你仍然可以default(private) shared(nrep),甚至firstprivate(nrep),尽管这里没有任何优势让每个帖子都有自己的nrep