Parallel Region Directives

The PARALLEL and END PARALLEL directives define a parallel region as follows:

Example

!$OMP PARALLEL

  ! parallel region

!$OMP END PARALLEL

When a thread encounters a parallel region, it creates a team of threads and becomes the master of the team. You can control the number of threads in a team by the use of an environment variable, run-time library call, or using the NUM_THREADS clause, or by combining these means.

Clauses Used

The PARALLEL directive takes an optional comma-separated list of clauses that specify as follows:

Changing the Number of Threads

Once created, the number of threads in the team remains constant for the duration of that parallel region. To explicitly change the number of threads used in the next parallel region, you can call the NUM_THREADS clause, call the OMP_SET_NUM_THREADS run-time library routine from a serial portion of the program, or use the OMP_NUM_THREADS environment variable. The order of precedence, or priority, is NUM_THREADS clause, OMP_SET_NUM_THREADS run-time library routine, then OMP_NUM_THREADS environment variable. For example, calling the NUM_THREADS clause overrides the OMP_SET_NUM_THREADS routine, and so forth.

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

Example

!$OMP PARALLEL

... ! This region is executed by 6 threads.

!$OMP END PARALLEL

CALL OMP_SET_NUM_THREADS(3)

!$OMP PARALLEL

... ! This region is executed by 3 threads.

!$OMP PARALLEL

!$OMP PARALLEL NUM_THREADS(4)

... ! This region is executed by 4 threads.

!$OMP END PARALLEL

Setting Units of Work

Use the worksharing directives such as DO, SECTIONS, and SINGLE to divide the statements in the parallel region into units of work and to distribute those units so that each unit is executed by one thread.

In the following example, the !$OMP DO and !$OMP END DO directives and all the statements enclosed by them comprise the static extent of the parallel region:

Example

!$OMP PARALLEL

  !$OMP DO

    DO I=1,N

      B(I) = (A(I) + A(I-1))/ 2.0

    END DO

  !$OMP END DO

!$OMP END PARALLEL

In the following example, the DO and END DO directives and all the statements enclosed by them, including all statements contained in the WORK subroutine, comprise the dynamic extent of the parallel region:

Example

!$OMP PARALLEL DEFAULT(SHARED)

  !$OMP DO

    DO I=1,N

      CALL WORK(I,N)

    END DO

  !$OMP END DO

!$OMP END PARALLEL

Setting Conditional Parallel Region Execution

When an IF clause is present on the PARALLEL directive, the enclosed code region is executed in parallel only if the scalar logical expression evaluates to .TRUE.. Otherwise, the parallel region is serialized. When there is no IF clause, the region is executed in parallel by default.

In the following example, the statements enclosed within the DO and END DO directives are executed in parallel only if there are more than three processors available. Otherwise the statements are executed serially:

Example

!$OMP PARALLEL IF (OMP_GET_NUM_PROCS() .GT. 3)

  !$OMP DO

    DO I=1,N

      Y(I) = SQRT(Z(I))

    END DO

  !$OMP END DO

!$OMP END PARALLEL

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 executed by a team of one thread.

Note

To achieve better performance than sequential execution, a parallel region must contain one or more worksharing constructs so that the team of threads can execute work in parallel. It is the contained worksharing constructs that lead to the performance enhancements offered by parallel processing.

For more details on this directive, see OpenMP* Fortran Compiler Directives.