flush Construct without a List
9.6. flush Construct without a List#
The following example distinguishes the shared variables affected by a flush construct with no list from the shared objects that are not affected:
//%compiler: clang
//%cflags: -fopenmp
/*
* name: flush_nolist.1
* type: C
*/
int x, *p = &x;
void f1(int *q)
{
*q = 1;
#pragma omp flush
/* x, p, and *q are flushed */
/* because they are shared and accessible */
/* q is not flushed because it is not shared. */
}
void f2(int *q)
{
#pragma omp barrier
*q = 2;
#pragma omp barrier
/* a barrier implies a flush */
/* x, p, and *q are flushed */
/* because they are shared and accessible */
/* q is not flushed because it is not shared. */
}
int g(int n)
{
int i = 1, j, sum = 0;
*p = 1;
#pragma omp parallel reduction(+: sum) num_threads(10)
{
f1(&j);
/* i, n and sum were not flushed */
/* because they were not accessible in f1 */
/* j was flushed because it was accessible */
sum += j;
f2(&j);
/* i, n, and sum were not flushed */
/* because they were not accessible in f2 */
/* j was flushed because it was accessible */
sum += i + j + *p + n;
}
return sum;
}
int main()
{
int result = g(7);
return result;
}
!!%compiler: gfortran
!!%cflags: -fopenmp
! name: flush_nolist.1
! type: F-fixed
SUBROUTINE F1(Q)
COMMON /DATA/ X, P
INTEGER, TARGET :: X
INTEGER, POINTER :: P
INTEGER Q
Q = 1
!$OMP FLUSH
! X, P and Q are flushed
! because they are shared and accessible
END SUBROUTINE F1
SUBROUTINE F2(Q)
COMMON /DATA/ X, P
INTEGER, TARGET :: X
INTEGER, POINTER :: P
INTEGER Q
!$OMP BARRIER
Q = 2
!$OMP BARRIER
! a barrier implies a flush
! X, P and Q are flushed
! because they are shared and accessible
END SUBROUTINE F2
INTEGER FUNCTION G(N)
COMMON /DATA/ X, P
INTEGER, TARGET :: X
INTEGER, POINTER :: P
INTEGER N
INTEGER I, J, SUM
I = 1
SUM = 0
P = 1
!$OMP PARALLEL REDUCTION(+: SUM) NUM_THREADS(10)
CALL F1(J)
! I, N and SUM were not flushed
! because they were not accessible in F1
! J was flushed because it was accessible
SUM = SUM + J
CALL F2(J)
! I, N, and SUM were not flushed
! because they were not accessible in f2
! J was flushed because it was accessible
SUM = SUM + I + J + P + N
!$OMP END PARALLEL
G = SUM
END FUNCTION G
PROGRAM FLUSH_NOLIST
COMMON /DATA/ X, P
INTEGER, TARGET :: X
INTEGER, POINTER :: P
INTEGER RESULT, G
P => X
RESULT = G(7)
PRINT *, RESULT
END PROGRAM FLUSH_NOLIST