Sections Construct
Sometimes, the work that is to be divided up among threads isn't iterative. In that case, the OpenMP sections construct makes it possible to execute blocks of code in separate threads using the OpenMP framework. The sections directive begins with the OpenMP sentinel and can be followed on the same line (or continuation of that line) by a number of modifiers in clauses. A complete sections construct must occur in a parallel region, or if the parallel region is the same size as the sections region, they can be combined in a parallel sections construct. Each block to be executed by a thread is introduced with the OpenMP section (singular) directive, which takes no clauses. In C/C++ the block is enclosed in {}; in Fortran, it is delimited either by the next SECTION directive, or by the final END SECTIONS directive. There is an implicit barrier at the end of a sections construct unless the nowait clause is present.
Example
!$OMP PARALLEL [clause[,clause]]
⋮
!$OMP SECTIONS [clause [,clause]]
!$OMP SECTION
⋮
!$OMP SECTION
⋮
!$OMP END SECTIONS [nowait]
⋮
!$OMP END PARALLEL
The available clauses for the sections directive are:
- private(list)
- firstprivate(list)
- lastprivate(list)
- reduction(operator:list)
- nowait
Fortran has one other wrinkle: if the nowait clause is desired, it does not appear with the SECTIONS directive, but rather with the END SECTIONS directive.
The embedded section directives take no clauses.
If the sections construct appears in nested parallel regions, it will bind to the threads of the innermost enclosing region.
CVW material development is supported by NSF OAC awards 1854828, 2321040, 2323116 (UT Austin) and 2005506 (Indiana University)