The Loop Directive

The omp for construct is very restrictive. The OpenMP standard introduced the more flexible omp loop construct to overcome such limitations and most especially to make it easier to instantiate threads on devices such as GPUs.

We have not discussed thread scheduling, but this can be a significant issue with devices, and implementations can choose scheduling (if not specified by the programmer) to be more efficient on devices. The loop directive can also be used with newer parallel region constructs such as teams as well as parallel.

Another significant difference between for and loop is that the former contains an implicit barrier at termination, so that all threads will synchronize, whereas the latter does not and on much be added explicitly if it is needed.

The loop construct still requires canonical form for for/do loops (contrary to some online sources), but newer OpenMP standards have considerably expanded what is accepted as “canonical.”

The directive is the same for both C/C++ and Fortran.

Syntax:

C/C++

#pragma omp parallel
{
    code
    #pragma omp loop
    for (int i=0; i<N; i++) {
        code
    }
}

Example

C

Contents of omp_parallel.c

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

int main(int argc, char *argv[]){

    #pragma omp parallel
    {
        int tid=omp_get_thread_num();
        printf("Hello from thread %d\n",tid);
    }
    
    return 0;
}

Download omp_parallel.c file

Fortran

!$omp parallel
    code
    !$omp loop
       code
    !$omp end parallel
!$omp end parallel

Reminder to Fortran programmers: Fortran parallel regions often require a private clause because most Fortran programs do not use block statements and variables are not declared within the parallel region.

Example

Fortran

Contents of omp_parallel.f90

program omp_par
use omp_lib

   integer :: tid

   !$omp parallel private(tid)
   tid=omp_get_thread_num()
   write(*,'(a,i4)') "Hello from thread ",tid
   !$omp end parallel

end program

Download omp_parallel.f90 file

Python (for omp4py)

with omp("parallel"):
   code
   with omp("for"):

Like Fortran, Python can also require a private clause. In this example, the omp for directive can only include a for loop, nothing more, so we initialize its loop variable outside it and thus must add the private clause.

Example

Python

Contents of omp_parallel.py

from omp4py import *

@omp
def hello():
    with omp("parallel"):
        tid=omp_get_thread_num()
        print(f"Hello from thread {tid}")
    
hello()

Download omp_parallel.py file

Previous
Next
© 2026 The Rector and Visitors of the University of Virginia