2013-02-03 66 views
1

此並行工作正常。OpenMP Fortran默認條款

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

這工作得很好。

爲什麼當我使用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 

非常感謝!

回答

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,無論是私人默認或以其他方式,是在進入私人部分未定義。這對於在循環中設置的循環索引irep無關緊要;但是如果(比如說)你的編譯器將每個線程的nrep設置爲零,那麼該循環永遠不會執行。更糟糕的是,每個線程對於nrep都可能有不同的值,任何事情都可能發生。所以你不希望nrepprivate。你仍然可以有default(private) shared(nrep),或者甚至firstprivate(nrep),但這裏沒有任何優勢讓每個線程都有自己的nrep

+0

非常感謝你! – Ling0k