Binding of barrier Regions
9.3. Binding of barrier Regions#
The binding rules call for a barrier region to bind to the closest enclosing parallel region.
In the following example, the call from the main program to sub2 is conforming because the barrier region (in sub3 ) binds to the parallel region in sub2 . The call from the main program to sub1 is conforming because the barrier region binds to the parallel region in subroutine sub2 .
The call from the main program to sub3 is conforming because the barrier region binds to the implicit inactive parallel region enclosing the sequential part. Also note that the barrier region in sub3 when called from sub2 only synchronizes the team of threads in the enclosing parallel region and not all the threads created in sub1 .
//%compiler: clang
//%cflags: -fopenmp
/*
* name: barrier_regions.1
* type: C
*/
void work(int n) {}
void sub3(int n)
{
work(n);
#pragma omp barrier
work(n);
}
void sub2(int k)
{
#pragma omp parallel shared(k)
sub3(k);
}
void sub1(int n)
{
int i;
#pragma omp parallel private(i) shared(n)
{
#pragma omp for
for (i=0; i<n; i++)
sub2(i);
}
}
int main()
{
sub1(2);
sub2(2);
sub3(2);
return 0;
}
!!%compiler: gfortran
!!%cflags: -fopenmp
! name: barrier_regions.1
! type: F-fixed
SUBROUTINE WORK(N)
INTEGER N
END SUBROUTINE WORK
SUBROUTINE SUB3(N)
INTEGER N
CALL WORK(N)
!$OMP BARRIER
CALL WORK(N)
END SUBROUTINE SUB3
SUBROUTINE SUB2(K)
INTEGER K
!$OMP PARALLEL SHARED(K)
CALL SUB3(K)
!$OMP END PARALLEL
END SUBROUTINE SUB2
SUBROUTINE SUB1(N)
INTEGER N
INTEGER I
!$OMP PARALLEL PRIVATE(I) SHARED(N)
!$OMP DO
DO I = 1, N
CALL SUB2(I)
END DO
!$OMP END PARALLEL
END SUBROUTINE SUB1
PROGRAM EXAMPLE
CALL SUB1(2)
CALL SUB2(2)
CALL SUB3(2)
END PROGRAM EXAMPLE