[Omp] Still on the barrier

James Beyer beyerj at cray.com
Mon Mar 12 06:25:41 PDT 2007


Shengyan Hong wrote:
>       Since you tell me that barrier can not be added into the parallel 
> region. Now can you tell me where I can add the barrier. I think that the 
> place of the code should have 8 threads. Right? To the end, how can I use 
> barrier in the fortran code? Thank you very much.
>                                               Shengyan Hong
> _______________________________________________
> Omp mailing list
> Omp at openmp.org
> http://openmp.org/mailman/listinfo/omp
>   
Just to be clear, no one has said you can not put a barrier in a 
parallel region.  What has been stated is that barriers may not be 
placed in work sharing constructs.  There are at least two types of 
parallel regions, redundant and work sharing.  Barriers are properly 
defined in redundant code as there is a reasonable guarantee that every 
thread in the team will hit the same barrier.  In a work sharing 
construct like a "parallel do" does the exact opposite of a redundant 
region.  It ensures that no two threads will ever execute the same 
iteration of a loop which means that barriers inside of the loop will 
not have the same semantics as barriers in redundant regions.  I hope 
this helps a bit.

One other point, semantically "may not" means you are not allowed to do 
something.  Whereas "can not" means it is not possible to do.  As far as 
barriers inside of work sharing constructs are concerned the spec says 
you "may not" place them there because the program will become 
non-compliant.  However, to say "can not" would require the a compiler 
to flag the barrier inside of a work sharing construct as an error.  
There is a very fine line being drawn here, but it is an important 
line.  It is actually very possible to write "safe" programs which have 
barriers inside of work sharing constructs, however, it is dangerous 
enough that the casual programmer should not attempt it.

BTW, if you look at page 154 of the 2.5 spec it explicitly shows that it 
is incorrect to place a barrier inside of an omp do construct.
A modified version of A.20.4f follows.
!$omp parallel default(shared)
!$omp do
    do i = 1,n
       call work(i,1)
! the following barrier shows incorrect nesting of a barrier in a loop 
region.
!$omp barrier
       call work(i,2)
    end do
! the following barrier shows correct nesting of a barrier in a parallel 
region.
!$omp barrier
!$omp end parallel

Does this help?

james


More information about the Omp mailing list