### 15.2.3.9 PARALLEL Directive (TU*X only)

The PARALLEL directive defines a parallel region. It takes the following form:

  c$OMP PARALLEL [clause[[,] clause] . . . ]
  block
  c$OMP END PARALLEL
  c
  Is one of the following: C (or c), !, or * (see Section 15.2.1).
  clause
  Is one of the following:
  - COPYIN (list)

    See Section 15.2.2.1.

  - DEFAULT ( PRIVATE | SHARED | NONE )

    See Section 15.2.2.2.

  - FIRSTPRIVATE (list)

    See Section 15.2.2.3.

  - IF (scalar_logical_expression)

    Specifies that the enclosed code section is to be executed in parallel only if the scalar_logical_expression evaluates to .TRUE.. Otherwise, the parallel region is serialized. If this clause is not used, the region is executed as if an IF(.TRUE.) clause were specified.

    This clause is evaluated by the master thread before any data scope attributes take effect.

    Only a single IF clause can appear in the directive.

  - PRIVATE (list)

    See Section 15.2.2.5.

  - REDUCTION ( operator | intrinsic : list )

    See Section 15.2.2.6.

  - SHARED (list)

    See Section 15.2.2.7.

  block
  Is a structured block (section) of statements or constructs. You cannot branch into or out of the block (the parallel region).

Rules and Behavior

The PARALLEL and END PARALLEL directive pair must appear in the same routine in the executable section of the code.

The END PARALLEL directive denotes the end of the parallel region. There is an implied barrier at this point. Only the master thread of the team continues execution at the end of a parallel region.

When a thread encounters a parallel region, it creates a team of threads and it becomes the master of the team. The master thread is a member of the team and it has a thread number of zero within the team. The number of threads in the team can be controlled by environment variables and library calls.

Once created, the number of threads in the team remains constant for the duration of that parallel region. However, you can explicitly change the number of threads used in the next parallel region by calling the run-time library routine omp_set_num_threads from a serial portion of the program. This routine overrides any value you may have set using the environment variable OMP_NUM_THREADS.

The code contained within the dynamic extent of the parallel region is executed on each thread, and the code path can be different for different threads.

If a thread executing a parallel region encounters another parallel region, it creates a new team and becomes the master of that new team. By default, nested parallel regions are always serialized and executed by a team of one thread.

Examples

You can use the PARALLEL directive in coarse-grain parallel programs. In the following example, each thread in the parallel region decides what part of the global array X upon which to work based on the thread number:

      c$OMP PARALLEL DEFAULT(PRIVATE) SHARED(X,NPOINTS)
            IAM = OMP_GET_THREAD_NUM()
            NP = OMP_GET_NUM_THREADS()
            IPOINTS = NPOINTS/NP
            CALL SUBDOMAIN(X,IAM,IPOINTS)
      c$OMP END PARALLEL

Assuming you previously used the environment variable OMP_NUM_THREADS to set the number of threads to six, you can change the number of threads between parallel regions as follows:

            CALL OMP_SET_NUM_THREADS(3)
      !$OMP PARALLEL
      ...
      !$OMP END PARALLEL
            CALL OMP_SET_NUM_THREADS(4)
      !$OMP PARALLEL DO
      ...
      !$OMP END PARALLEL DO

For More Information:

- On environment variables, see your user manual.
- On parallel library routines, see Appendix F.

Previous Page Next Page Table of Contents