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

PARALLEL Directive

A Parallel Region is a block of code that is to be executed in parallel by a number of threads. Each thread executes the enclosed code separately.

Note that all code within a parallel region is executed by each thread unless other OpenMP directives specify otherwise. For instance, a DO loop that lies within a parallel region will be executed completely (and redundantly) by each thread unless a parallel DO directive is inserted before the loop. A DO or PARALLEL DO directive is necessary if you want the loop to be executed once with different threads performing different iterations of the loop in parallel.

It is illegal to branch out of a Parallel Region.

!$OMP PARALLEL [clause] 

	code block       

!$OMP END PARALLEL 

There are many possible values of [clause].

Examples

!Filename: parallel.f90
!
!This simply shows that code in a PARALLEL
!region is executed by each thread.

PROGRAM PARALLEL 
        IMPLICIT NONE
        INTEGER I

        I=1

!$OMP PARALLEL FIRSTPRIVATE(I)

        PRINT *, I

!$OMP END PARALLEL 

END PROGRAM PARALLEL 

To run on franklin:

> cat parallel.pbs
#PBS -N parallel
#PBS -j oe
#PBS -o parallel.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 parallel -mp=nonuma -Minfo=mp parallel.f90

export OMP_NUM_THREADS=2
aprun -n 1 -N 1 ./parallel
> qsub parallel.pbs
498022.nid00003
> cat parallel.out
/opt/xt-pe/2.0.44a2/bin/snos64/ftn: INFO: linux target is being used
parallel.f90:
parallel:
    12, Parallel region activated
    14, Parallel region terminated
            1
            1
Application 4673065 resources: utime 0, stime 0

The next example shows use of the REDUCTION clause. This simple example shows how the values of the variables are combined when leaving the parallel region when a REDUCTION clause is used. Also note that each thread executes the PRINT statement in the parallel region.

!Filename: reduction.f90
!
!This program shows the use of the REDUCTION clause.

PROGRAM REDUCTION 
        IMPLICIT NONE
        INTEGER tnumber, OMP_GET_THREAD_NUM
        INTEGER I,J,K
        I=1
        J=1
        K=1
        PRINT *, "Before Par Region: I=",I," J=", J," K=",K
        PRINT *, ""

!$OMP PARALLEL DEFAULT(PRIVATE) REDUCTION(+:I)&
!$OMP			 REDUCTION(*:J) REDUCTION(MAX:K)

        tnumber=OMP_GET_THREAD_NUM()

        I = tnumber
        J = tnumber
        K = tnumber

        PRINT *, "Thread ",tnumber, "         I=",I," J=", J," K=",K

!$OMP END PARALLEL

        PRINT *, ""
        print *, "Operator            +     *    MAX"
        PRINT *, "After Par Region:  I=",I," J=", J," K=",K

END PROGRAM REDUCTION 

To run on franklin:

> cat reduction.pbs
#PBS -N reduction
#PBS -j oe
#PBS -o reduction.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 reduction -mp=nonuma -Minfo=mp reduction.f90

export OMP_NUM_THREADS=2
aprun -n 1 -N 1 ./reduction
> qsub reduction.pbs
498041.nid00003
> cat reduction.out
/opt/xt-pe/2.0.44a2/bin/snos64/ftn: INFO: linux target is being used
reduction.f90:
reduction:
    15, Parallel region activated
    25, Begin critical section
        End critical section
        Parallel region terminated
 Before Parallel Region: I=            1  J=            1  K=            1
 
 Thread             0               I=            0  J=            0  K= 
            0
 Thread             1               I=            1  J=            1  K= 
            1
 
 Operator                 +     *    MAX
 After Parallel Region:  I=            2  J=            0  K=            1
Application 4673534 resources: utime 0, stime 0

LBNL Home
Page last modified: Mon, 05 May 2008 19:12:54 GMT
Page URL: http://www.nersc.gov/nusers/help/tutorials/openmp/parallel.php
Web contact: webmaster@nersc.gov
Computing questions: consult@nersc.gov

Privacy and Security Notice
DOE Office of Science