NERSC logo National Energy Research Scientific Computing Center
  A DOE Office of Science User Facility
  at Lawrence Berkeley National Laboratory
 

BARRIER Directive

!$OMP BARRIER

The BARRIER directive synchronizes all threads in a team. When encountered, each thread waits until all of the others threads in that team have reached this point.

Example

!Filename: barrier.f90
!
!This shows use of the BARRIER directive.
!

PROGRAM ABARRIER 
        IMPLICIT NONE
        INTEGER:: L
        INTEGER:: nthreads, OMP_GET_NUM_THREADS
        INTEGER:: tnumber, OMP_GET_THREAD_NUM

!$OMP PARALLEL SHARED(L) PRIVATE(nthreads,tnumber)
        nthreads = OMP_GET_NUM_THREADS()
        tnumber  = OMP_GET_THREAD_NUM()

!$OMP MASTER

        PRINT *, ' Enter a value for L'
        READ(5,*)  L

!$OMP END MASTER

!$OMP BARRIER

!$OMP CRITICAL

        PRINT *, ' My thread number         =',tnumber
        PRINT *, ' Number of threads        =',nthreads
        PRINT *, ' Value of L               =',L
        PRINT *, ''

!$OMP END CRITICAL

!$OMP END PARALLEL


END PROGRAM ABARRIER 

Compiling and running on franklin:

> cat barrier.pbs
#PBS -N barrier
#PBS -j oe
#PBS -o barrier.out
#PBS -q interactive
#PBS -S /bin/bash
#PBS -l mppwidth=1
#PBS -l mppnppn=1
#PBS -l mppdepth=2
#PBS -l walltime=00:05:00
#PBS -V

cd $PBS_O_WORKDIR

ftn -o barrier -mp=nonuma -Minfo=mp barrier.f90

export OMP_NUM_THREADS=2
aprun -n 1 -N 1 ./barrier < ninety
> cat ninety
90
> qsub barrier.pbs
500868.nid00003
> cat barrier.out
/opt/xt-pe/2.0.44a2/bin/snos64/ftn: INFO: linux target is being used
barrier.f90:
barrier:
    12, Parallel region activated
    16, Begin master section
    21, End master section
    23, Barrier
    27, Begin critical section __cs_unspc
    30, End critical section __cs_unspc
        Parallel region terminated
  Enter a value for L
  My thread number         =            0
  Number of threads        =            2
  Value of L               =           90
 
  My thread number         =            1
  Number of threads        =            2
  Value of L               =           90
 
Application 4736056 resources: utime 0, stime 0

LBNL Home
Page last modified: Mon, 05 May 2008 21:30:00 GMT
Page URL: http://www.nersc.gov/nusers/help/tutorials/openmp/barrier.php
Web contact: webmaster@nersc.gov
Computing questions: consult@nersc.gov

Privacy and Security Notice
DOE Office of Science