Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 3/31/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

BARRIER

OpenMP* Fortran Compiler Directive: Synchronizes all the threads in a team. It causes each thread to wait until all of the other threads in the team have reached the barrier.

!$OMP BARRIER

The binding thread set for a BARRIER construct is the current team. A barrier region binds to the innermost enclosing parallel region.

Each barrier region must be encountered by all threads in a team or by none at all, unless cancellation has been requested for the innermost enclosing parallel region.

The barrier region must also be encountered in the same order by all threads in a team.

Example


INTEGER K
K = 17

!$OMP PARALLEL SHARED (K) NUM_THREADS (2)
IF (OMP_GET_THREAD_NUM() == 0) THEN 
    X = 5
ELSE 
    ! The following read access of K creates a race condition 
    PRINT *,"1: THREAD# ", OMP_GET_THREAD_NUM (), "K = ", K 
ENDIF

! This barrier contains implicit flushes on all threads, as well as a thread 
! synchronization: this guarantees that the value 5 will be printed by 
! both PRINT 2 and PRINT 3 below.

!$OMP BARRIER

IF (OMP_GET_THREAD_NUM() == 0) THEN 
    PRINT *,"2: THREAD# ", OMP_GET_THREAD_NUM (), "K = ", K 
ELSE 
    PRINT *,"3: THREAD# ", OMP_GET_THREAD_NUM (), "K = ", K 
ENDIF 
!$OMP END PARALLEL