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 or a run-time library call, or both.

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, call the OMP_SET_NUM_THREADS run-time library routine from a serial portion of the program. This routine overrides any value you may have set using the OMP_NUM_THREADS environment variable.

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

CALL OMP_SET_NUM_THREADS(3)

!$OMP PARALLEL

...

!$OMP PARALLEL

  CALL OMP_SET_NUM_THREADS(4)

!$OMP PARALLEL DO

...

!$OMP END PARALLEL DO

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 !$OMP DO and !$OMP 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 !$OMP DO and !$OMP 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 in the Intel® Fortran Language Reference.