Purpose
The SECTIONS construct defines distinct blocks of code to be executed in parallel by threads in the team.
The SECTIONS and END SECTIONS directives only take effect if you specify the -qsmp compiler option.
Format
.----------------------------. V | >>-SECTIONS----+------------------------+-+-------------------->< '-+---+--sections_clause-' '-,-' .--------------------. V | >>-+---------+--block----+----------------+-+------------------>< '-SECTION-' '-SECTION--block-' >>-END SECTIONS--+--------+------------------------------------>< '-NOWAIT-' |
where sections_clause is:
>>-+-firstprivate_clause-+------------------------------------->< +-lastprivate_clause--+ +-private_clause------+ '-reduction_clause----' |
Rules
The SECTIONS construct must be encountered by all threads in a team or by none of the threads in a team. All work-sharing constructs and BARRIER directives that are encountered must be encountered in the same order by all threads in the team.
The SECTIONS construct includes the delimiting directives, and the blocks of code they enclose. At least one block of code must appear in the construct.
You must specify the SECTION directive at the beginning of each block of code except for the first. The end of a block is delimited by either another SECTION directive or by the END SECTIONS directive.
It is illegal to branch into or out of any block of code that is enclosed in the SECTIONS construct. All SECTION directives must appear within the lexical extent of the SECTIONS/END SECTIONS directive pair.
The compiler determines how to divide the work among the threads based on a number of factors, such as the number of threads in the team and the number of sections to be executed in parallel. Therefore, a single thread might execute more than one SECTION. It is also possible that a thread in the team might not execute any SECTION.
In order for the directive to execute in parallel, you must place the SECTIONS/END SECTIONS pair within the dynamic extent of a parallel region. Otherwise, the blocks will be executed serially.
If you specify NOWAIT on the SECTIONS directive, a thread that completes its sections early will proceed to the instructions following the SECTIONS construct. If you do not specify the NOWAIT clause, each thread will wait for all of the other threads in the same team to reach the END SECTIONS directive. However, there is no implied BARRIER at the start of the SECTIONS construct.
You cannot specify a SECTIONS directive within the dynamic extent of a CRITICAL or MASTER directive.
You cannot nest SECTIONS, DO or SINGLE directives that bind to the same PARALLEL directive.
BARRIER and MASTER directives are not permitted in the dynamic extent of a SECTIONS directive.
The END SECTIONS directive implies the FLUSH directive.
Examples
Example 1: This example shows a valid use of the SECTIONS construct within a PARALLEL region.
INTEGER :: I, B(500), S, SUM ! ... S = 0 SUM = 0 !$OMP PARALLEL SHARED(SUM), FIRSTPRIVATE(S) !$OMP SECTIONS REDUCTION(+: SUM), LASTPRIVATE(I) !$OMP SECTION S = FCT1(B(1::2)) ! Array B is not altered in FCT1. SUM = SUM + S ! ... !$OMP SECTION S = FCT2(B(2::2)) ! Array B is not altered in FCT2. SUM = SUM + S ! ... !$OMP SECTION DO I = 1, 500 ! The local copy of S is initialized S = S + B(I) ! to zero. END DO SUM = SUM + S ! ... !$OMP END SECTIONS ! ... !$OMP DO REDUCTION(-: SUM) DO J=I-1, 1, -1 ! The loop starts at 500 -- the last ! value from the previous loop. SUM = SUM - B(J) END DO !$OMP MASTER SUM = SUM - FCT1(B(1::2)) - FCT2(B(2::2)) !$OMP END MASTER !$OMP END PARALLEL ! ... ! Upon termination of the PARALLEL ! region, the value of SUM remains zero.
Example 2: This example shows a valid use of nested SECTIONS.
!$OMP PARALLEL !$OMP MASTER CALL RANDOM_NUMBER(CX) CALL RANDOM_NUMBER(CY) CALL RANDOM_NUMBER(CZ) !$OMP END MASTER !$OMP SECTIONS !$OMP SECTION !$OMP PARALLEL !$OMP SECTIONS PRIVATE(I) !$OMP SECTION DO I=1, 5000 X(I) = X(I) + CX END DO !$OMP SECTION DO I=1, 5000 Y(I) = Y(I) + CY END DO !$OMP END SECTIONS !$OMP END PARALLEL !$OMP SECTION !$OMP PARALLEL SHARED(CZ,Z) !$OMP DO DO I=1, 5000 Z(I) = Z(I) + CZ END DO !$OMP END DO !$OMP END PARALLEL !$OMP END SECTIONS NOWAIT ! The following computations do not ! depend on the results from the ! previous section. !$OMP DO DO I=1, 5000 T(I) = T(I) * CT END DO !$OMP END DO !$OMP END PARALLEL
Related Information