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.
The PARALLEL directive takes an optional comma-separated list of clauses that specify as follows:
IF: whether the statements in the parallel region are executed in parallel by a team of threads or serially by a single thread.
PRIVATE, FIRSTPRIVATE, SHARED, or REDUCTION: variable types
DEFAULT: variable data scope attribute
COPYIN: master thread common block values are copied to THREADPRIVATE copies of the common block
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 |
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 DO I=1,N !$OMP END DO |
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) DO I=1,N !$OMP END DO |
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) DO I=1,N !$OMP END DO |
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.