gfortran会利用DO CONCURRENT吗? [英] Does gfortran take advantage of DO CONCURRENT?
问题描述
我当前正在使用gfortran 4.9.2,我想知道编译器是否真的知道如何利用DO CONCURRENT构造(Fortran 2008).我知道编译器支持"它,但是尚不清楚它需要什么.例如,如果打开了自动并行化(指定了一定数量的线程),那么编译器是否知道如何并行化并发循环?
I'm currently using gfortran 4.9.2 and I was wondering if the compiler actually know hows to take advantage of the DO CONCURRENT construct (Fortran 2008). I know that the compiler "supports" it, but it is not clear what that entails. For example, if automatic parallelization is turned on (with some number of threads specified), does the compiler know how to parallelize a do concurrent loop?
如评论中所述,此先前的问题关于SO的内容与我的非常相似,但它是从2012年开始的,并且只有非常新的gfortran版本实现了现代Fortran的最新功能,因此我认为值得询问2015年编译器的当前状态
As mentioned in the comment, this previous question on SO is very similar to mine, but it is from 2012, and only very recent versions of gfortran have implemented the newest features of modern Fortran, so I thought it was worth asking about the current state of the compiler in 2015.
推荐答案
gfortran中的DO CONCURRENT
并未明确启用某些新功能,而是对程序员施加了限制,以便使隐式地允许在需要时(使用选项-ftree-parallelize-loops=NPROC
)并行化循环.
Rather than explicitly enabling some new functionality, DO CONCURRENT
in gfortran seems to put restrictions on the programmer in order to implicitly allow parallelization of the loop when required (using the option -ftree-parallelize-loops=NPROC
).
DO
循环可以包含任何函数调用,而DO CONCURRENT
的内容仅限于PURE
函数(即没有副作用).因此,当尝试在DO CONCURRENT
中使用例如RANDOM_NUMBER
(不是PURE
,因为它需要保持生成器的状态)时,gfortran会提出抗议:
While a DO
loop can contain any function call, the content of DO CONCURRENT
is restricted to PURE
functions (i.e., having no side effects). So when one attempts to use, e.g., RANDOM_NUMBER
(which is not PURE
as it needs to maintain the state of the generator) in DO CONCURRENT
, gfortran will protest:
prog.f90:25:29:
25 | call random_number(x)
| 1
Error: Subroutine call to intrinsic ‘random_number’ in DO CONCURRENT block at (1) is not PURE
否则,DO CONCURRENT
的行为与正常的DO
相同.它仅强制使用可并行化的代码,因此-ftree-parallelize-loops=NPROC
成功.例如,对于gfortran 9.1和-fopenmp -Ofast -ftree-parallelize-loops=4
,以下程序中的标准DO
和F08 DO CONCURRENT
循环都在4个线程中运行,并且计时基本相同:
Otherwise, DO CONCURRENT
behaves as normal DO
. It only enforces use of parallelizable code, so that -ftree-parallelize-loops=NPROC
succeeds. For instance, with gfortran 9.1 and -fopenmp -Ofast -ftree-parallelize-loops=4
, both the standard DO
and the F08 DO CONCURRENT
loops in the following program run in 4 threads and with virtually identical timing:
program test_do
use omp_lib, only: omp_get_wtime
integer, parameter :: n = 1000000, m = 10000
real, allocatable :: q(:)
integer :: i
real :: x, t0
allocate(q(n))
t0 = omp_get_wtime()
do i = 1, n
q(i) = i
do j = 1, m
q(i) = 0.5 * (q(i) + i / q(i))
end do
end do
print *, omp_get_wtime() - t0
t0 = omp_get_wtime()
do concurrent (i = 1:n)
q(i) = i
do j = 1, m
q(i) = 0.5 * (q(i) + i / q(i))
end do
end do
print *, omp_get_wtime() - t0
end program test_do
这篇关于gfortran会利用DO CONCURRENT吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!