OpenMP和gfortran

时间:2017-03-20 12:43:50

标签: fortran openmp gfortran

我正在尝试一个非常简单的openmp fortran程序,因为我遇到了嵌套并行区域的问题。代码如下:

PROGRAM TEST_OPENMP
IMPLICIT NONE
INTEGER :: omp_get_num_threads, omp_get_thread_num
CALL omp_set_num_threads( 4 )
WRITE(*,*) "I am the master thread"
!$OMP PARALLEL
write(*,*) "Hello, I am in the first parallel region", omp_get_thread_num()
!$OMP PARALLEL
write(*,*) "I am a nested parallel region : ", omp_get_thread_num()
!$OMP END PARALLEL
!$OMP END PARALLEL
write(*,*) "The master thread is back: serial region", omp_get_thread_num()
END PROGRAM TEST_OPENMP

我希望有4个字符串实例以“你好,我在......”和16个“我是嵌套的......”开头,但我得到的是以下内容:

I am the master thread
Hello, I am in the first parallel region           3
Hello, I am in the first parallel region           2
Hello, I am in the first parallel region           1
I am a nested parallel region :            0
I am a nested parallel region :            0
Hello, I am in the first parallel region           0
I am a nested parallel region :            0
I am a nested parallel region :            0
The master thread is back: serial region           0

因此,第二平行区域被视为线性的。我用'gfortran -fopenmp filename'编译。我做了一件可怕的错事吗?

1 个答案:

答案 0 :(得分:2)

感谢蒂姆提示。我正在使用一个没有提到OMP_NESTED的教程。抱歉这个愚蠢的问题。

现在它运作顺利:

$ export OMP_NESTED=True
$ gfortran -fopenmp openmp_example_3.f90
$ a.out 
I am the master thread
Hello, I am in the first parallel region           0
Hello, I am in the first parallel region           3
I am a nested parallel region :            0
I am a nested parallel region :            2
Hello, I am in the first parallel region           2
I am a nested parallel region :            1
Hello, I am in the first parallel region           1
I am a nested parallel region :            3
I am a nested parallel region :            0
I am a nested parallel region :            1
I am a nested parallel region :            1
I am a nested parallel region :            0
I am a nested parallel region :            1
I am a nested parallel region :            3
I am a nested parallel region :            2
I am a nested parallel region :            3
I am a nested parallel region :            0 
I am a nested parallel region :            2
I am a nested parallel region :            3
I am a nested parallel region :            2
The master thread is back: serial region           0