OMP: What is the difference between OMP PARALLEL DO and OMP DO (Without parallel directive at all)

心不动则不痛 提交于 2020-12-12 13:23:14

问题


OK, I hope this was not asked before, because this is a little tricky to find on the search.

I have looked over the F95 manual, but still find this vague:

For the simple case of:
DO i=0,99
    <some functionality>
END DO

I'm trying to figure out what is the difference between:

!$OMP DO PRIVATE(i)
DO i=0,99
    <some functionality>
END DO
!$OMP END DO

And:

!$OMP PARALLEL DO PRIVATE(i)
DO i=0,99
    <some functionality>
END DO
!$OMP PARALLEL END DO

(Just to point out the difference: the first one has OMP DO but no PARALLEL directive AT ALL. The second just has the PARALLEL directive added)

Thanks!


回答1:


If the OpenMP do directive is encountered outside a parallel region it is executed in serial by one thread -- it behaves as if it were not parallelised at all. Of course, that's because it isn't.

The first of your snippets isn't parallelised, the second is.

I'm not sure what you mean by the F95 manual nor why you would look there for information about OpenMP.




回答2:


The !$OMP DO PRIVATE(i) instructs the compiler how to divide the work between the threads, but does not start any threads. It will do any worksharing only if it is (even indirectly) inside a $OMP PARALLEL region, otherwise it will not do anything.

!$OMP PARALLEL DO PRIVATE(i)
!$OMP END PARALLEL DO

does the same as

!$OMP PARALLELPRIVATE(i)
!$OMP DO
!$OMP END DO
!$OMP END PARALLEL

So it both starts the threads and distributes the work beteen them.

If you had just

!$OMP PARALLEL PRIVATE(i)
!$OMP END PARALLEL

all threads would do all the work inside the parallel region.



来源:https://stackoverflow.com/questions/31832676/omp-what-is-the-difference-between-omp-parallel-do-and-omp-do-without-parallel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!