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

DO Directive

The DO directive specifies that the iterations of the immediately following DO loop must be executed in parallel. The DO directive must be enclosed in a parallel region; it creates no threads by itself. The following do loop can not be a DO WHILE.

!$OMP DO [clause[[,]clause ...]  
	do_loop 
!$OMP END DO [NOWAIT] 

The DO clause can have various values.

It is illegal to branch out of a DO loop associated with the DO directive.

Example

!Filename: dodir.f90
!

PROGRAM DODIR 
        IMPLICIT NONE
        INTEGER I,L
        INTEGER, PARAMETER:: DIM=16
        REAL A(DIM),B(DIM),S
        INTEGER nthreads,tnumber
        INTEGER OMP_GET_NUM_THREADS,OMP_GET_THREAD_NUM

        CALL RANDOM_NUMBER(A)
        CALL RANDOM_NUMBER(B)

!$OMP PARALLEL DEFAULT(PRIVATE) SHARED(A,B)
!$OMP DO SCHEDULE(STATIC,2)
        DO I=2,DIM
                B(I) = ( A(I) - A(I-1) ) / 2.0

                nthreads=OMP_GET_NUM_THREADS()
                tnumber=OMP_GET_THREAD_NUM()
                print *, "Thread",tnumber," of",nthreads," has I=",I
        END DO
!$OMP END DO
!$OMP END PARALLEL

        S=MAXVAL(B)
        L=MAXLOC(B,1)

        PRINT *, "Maximum gradient: ",S," at location:",L

END PROGRAM DODIR 

Compiling and running on Franklin:

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

export OMP_NUM_THREADS=2
aprun -n 1 -N 1 ./dodir
> qsub dodir.pbs
500611.nid00003
> cat dodir.out
/opt/xt-pe/2.0.44a2/bin/snos64/ftn: INFO: linux target is being used
dodir.f90:
dodir:
    15, Parallel region activated
    17, Parallel loop activated; static block-cyclic iteration allocation
    24, Barrier
        Parallel region terminated
 Thread            1  of            2  has I=            4
 Thread            1  of            2  has I=            5
 Thread            1  of            2  has I=            8
 Thread            1  of            2  has I=            9
 Thread            1  of            2  has I=           12
 Thread            1  of            2  has I=           13
 Thread            1  of            2  has I=           16
 Thread            0  of            2  has I=            2
 Thread            0  of            2  has I=            3
 Thread            0  of            2  has I=            6
 Thread            0  of            2  has I=            7
 Thread            0  of            2  has I=           10
 Thread            0  of            2  has I=           11
 Thread            0  of            2  has I=           14
 Thread            0  of            2  has I=           15
 Maximum gradient:    0.6280164      at location:            1
Application 4733417 resources: utime 0, stime 0

Notice that the loop was divided among the 2 threads as we requested with the SCHEDULE(STATIC,2) clause. Also note that if had not enclosed the do loop in a DO/END DO directive block, the loop would not have been split, but would have been executed OMP_NUM_THREADS number of times.


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

Privacy and Security Notice
DOE Office of Science