Fortran 程序中不可分类的 OpenMP 指令
Unclassifiable OpenMP directive in a Fortran program
我尝试使用 openMP 在 Fortran 中并行化代码,代码如下:
program pigreco
!----------------------------------------!
use OMP_LIB
implicit none
!----------------------------------------!
integer :: i
integer, parameter :: N = 100000
integer, parameter :: NCPU = 4
real*8 :: t0, t1
real :: h, totale, x, f
!----------------------------------------!
print '(a,2x,i15)', ' Number of intervals: ', N
totale = 0.0
h = 1. / N
call OMP_SET_NUM_THREADS(NCPU)
write(*, '(a,i10)') 'Numero di processori totali: ', NCPU
t0 = OMP_GET_WTIME()
!----------------------------------------!
#ifdef PARALLEL
!
print '(a)', "Scelta la versione parallela."
!
!$OMP PARALLEL DO PRIVATE(x, f) REDUCTION(+:totale)
!
do i = 1, N
x = (i - 0.5) * h
f = (4 * h) / (1 + x**2)
totale = totale + f
enddo
!$OMP END PARALLEL DO
!
#endif
!
t1 = OMP_GET_WTIME()
!
PRINT '(a,2x,f30.25)', ' Computed PI =', totale
PRINT '(a,2x,f30.25)', ' Total computational time =', t1 - t0
!
end program pigreco
然后当我尝试使用以下行进行编译时:gfortran prova.F90 -fopenmp -D PARALLEL
它给了我一个错误 "unclassifiable OpenMP directive at (1)".
问题是你用预处理器定义了PARALLEL
,所以编译器读取的不是OMP PARALLEL DO
,而是OMP 1 DO
,这当然没有意义。把#ifdef PARALLEL
改成#ifdef RUNPARALLEL
,把-DPARALLEL
改成-DRUNPARALLEL
,编译器就不会报错了。
或者,您可以使用这样一个事实,即在使用 OpenMP 编译时自动定义宏变量 _OPENMP
,因此您可以使用 #ifdef _OPENMP
,而不使用 -D
标志。
我尝试使用 openMP 在 Fortran 中并行化代码,代码如下:
program pigreco
!----------------------------------------!
use OMP_LIB
implicit none
!----------------------------------------!
integer :: i
integer, parameter :: N = 100000
integer, parameter :: NCPU = 4
real*8 :: t0, t1
real :: h, totale, x, f
!----------------------------------------!
print '(a,2x,i15)', ' Number of intervals: ', N
totale = 0.0
h = 1. / N
call OMP_SET_NUM_THREADS(NCPU)
write(*, '(a,i10)') 'Numero di processori totali: ', NCPU
t0 = OMP_GET_WTIME()
!----------------------------------------!
#ifdef PARALLEL
!
print '(a)', "Scelta la versione parallela."
!
!$OMP PARALLEL DO PRIVATE(x, f) REDUCTION(+:totale)
!
do i = 1, N
x = (i - 0.5) * h
f = (4 * h) / (1 + x**2)
totale = totale + f
enddo
!$OMP END PARALLEL DO
!
#endif
!
t1 = OMP_GET_WTIME()
!
PRINT '(a,2x,f30.25)', ' Computed PI =', totale
PRINT '(a,2x,f30.25)', ' Total computational time =', t1 - t0
!
end program pigreco
然后当我尝试使用以下行进行编译时:gfortran prova.F90 -fopenmp -D PARALLEL
它给了我一个错误 "unclassifiable OpenMP directive at (1)".
问题是你用预处理器定义了PARALLEL
,所以编译器读取的不是OMP PARALLEL DO
,而是OMP 1 DO
,这当然没有意义。把#ifdef PARALLEL
改成#ifdef RUNPARALLEL
,把-DPARALLEL
改成-DRUNPARALLEL
,编译器就不会报错了。
或者,您可以使用这样一个事实,即在使用 OpenMP 编译时自动定义宏变量 _OPENMP
,因此您可以使用 #ifdef _OPENMP
,而不使用 -D
标志。