Rookie HPC

Work sharing

FORTRAN

do

Definition

The do construct tells OpenMP that the iteration set of the do loop that follows is to be distributed across the threads present in the team. Without the do construct, the entire iteration set of the do loop concerned will be executed by each thread in the team. From a syntactic point of view, note that the do construct must be immediately followed by the do loop; it does not allow curly brackets between it and the do loop.

Copy

Feedback

!$omp do [private(list)]
         [firstprivate(list)]
         [lastprivate([modifier:] list)]
         linear(list [: linear-step])
         [reduction([modifier,] identifier: list)]
         [schedule([modifier[, modifier]:] kind [, chunk_size])]
         [collapse(n)]
         [ordered[(n)]]
         [allocate([allocator:] list)]
         [order(concurrent)]
!$omp end do [nowait]

Example

Copy

Feedback

!> @brief Illustrates how to use the do construct.
!> @details This application contains a do loop that initialises an array. This
!> do loop is parallelised by using a do construct inside a parallel region.
PROGRAM main
    USE omp_lib

    IMPLICIT NONE

    INTEGER :: a(0:9)
    INTEGER :: i

    ! Use 2 threads when creating OpenMP parallel regions
    CALL omp_set_num_threads(2)

    ! Spawn the threads
    !$omp parallel
        ! Tell the threads to share these iterations rather than running the entire iteration set each
        !$omp do
        DO i = 0, 9
            a(i) = i
        END DO
        !$omp end do
    !$omp end parallel
END PROGRAM main