简体   繁体   English

openmp为内部循环并行化

[英]openmp parallelize for inner loop

I have a Fortran code. 我有一个Fortran代码。 Code contains two loops. 代码包含两个循环。 I want to parallelize ONLY inner loop. 我想并行ONLY内循环。 Here is my code: 这是我的代码:

!$OMP PARALLEL PRIVATE(i,j)
do i=1, N 
  !$OMP PARALLEL DO
  do j=1, M
    ! do some calculations
  end do
  !$OMP END PARALLEL DO
end do
!$OMP END PARALLEL

Is parallelization correct? 并行化正确吗? I am not sure, whether it is needed to put !$OMP PARALLEL PRIVATE(i,j) at the beginning? 我不确定,是否需要在开始时加!$OMP PARALLEL PRIVATE(i,j) Should I omit it and declare just PRIVATE(i) before the second loop? 我应该忽略它,而在第二个循环之前仅声明PRIVATE(i)吗? Anyways, I am a little bit confused, please explain what is correct behaviour. 无论如何,我有点困惑,请解释什么是正确的行为。

Why is i private? 为什么i的私人? Isn't it required for all threads? 不是所有线程都需要它吗? It mustn't change during the whole of the inner loop (since it is the loop counter of the outer one). 它在整个内部循环中都不得更改(因为它是外部循环的循环计数器)。 If it is declared private, it is undefined at the beginning of the parallel section unless firstprivate is used. 如果声明为私有,则除非使用firstprivate否则在并行部分的开头未定义它。

OpenMP in Fortran notices that j is the loop counter of the parallelized loop, so it is implicitly private. Fortran中的OpenMP注意到j是并行化循环的循环计数器,因此它是隐式私有的。 So there is no need to declare it explicitly. 因此,无需显式声明它。

The next thing is that you should avoid nested OpenMP section (ie a second !$OMP PARALLEL ) 接下来的事情是,您应该避免嵌套OpenMP节(即第二个!$OMP PARALLEL )。

Because I like to be explicit, I would write it as 因为我喜欢明确,所以我将其写为

!$OMP PARALLEL PRIVATE(j) SHARED(i)
do i=1, N 
  !$OMP DO
  do j=1, M
    ! do some calculations
  end do
  !$OMP END DO
end do
!$OMP END PARALLEL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM