next up previous contents
Next: 5 Private and Shared Up: ADAPTOR OpenMP Programmers Guide Previous: 3 OpenMP Blocking Execution   Contents

4 Parallel Regions

The PARALLEL and END PARALLEL directives define a parallel region. A parallel region is a block of code that is executed by multiple threads in parallel. This is the fundamental parallel construct in OpenMP that starts parallel execution.

!OMP PARALLEL [clause [,] clause] ...]
     <block>
!OMP END PARALLEL

The directives PARALLEL and END PARALLEL must be within the same routine, and must contain a structured block of code. It is illegal to branch in or out of a parallel region. The ADAPTOR compiler gives an error message if this is the case.

By default, thread share variables and have a single address space. Private data can also be defined. This is usually done by the PRIVATE clause. The use and defintion of private data will be described in more detail in Section 5)

``Dynamic lexical context'' extends to routines called within a parallel region. This is new to OpenMP and introduces the idea of ``orphaned'' directives.

!$OMP parallel 
      call PARSUB()
!$OMP end parallel

      subroutine PARSUB()
      ...
      end

All subroutines (including library routines and Fortran intrinsics (e.g. ALLOCATE and READ or WRITE) must be ``thread safe''. That is, they must be able to be called in parallel without side effects. You must ensure this in your program. Compilers are not required to check for dependencies, conflicts, deadlocks, race conditions or other problems that result from incorrect program execution or incorrectly used directives.

OpenMP comes along with an OpenMP library. The follwoing example illustrates how the OpenMP library can be used to find out how many processors are avaiable in the system. A team of threads is then started, one per processor, which write a message to the standard output UNIT=6. The variable INODE becomes a private variable so that every thread can define and use its own value.

      program HELLO
      implicit none
      integer :: INODE, NNODE
      integer, external :: omp_get_num_procs, &
     &                     omp_get_thread_num
      NNODE=omp_get_num_procs()
      write (6,'('' NNODE = '',I3)') NNODE
      call omp_set_num_threads (NNODE)
!$OMP parallel private (INODE)
      INODE = omp_get_thread_num()
      write (6,'('' hello world from '',I3)') INODE
!$OMP end parallel
      end program HELLO


next up previous contents
Next: 5 Private and Shared Up: ADAPTOR OpenMP Programmers Guide Previous: 3 OpenMP Blocking Execution   Contents
Thomas Brandes 2004-03-18