The For Directive
Within a parallel region we often want to implement a for loop. If we use for alone, the entire loop will be replicated by each thread, when we want it distributed among the team. But using parallel for will not usually work or do what we want. OpenMP provides a directive specifically for this situation, the fo directive. It should be used within a parallel region.
Like the omp parallel for, this construct requires that the for/do loop be in canonical form, as defined by the OpenMP
standard. For the most part, this means the loop variable must be an integer, the upper bound must be a simple arithmetic expression involving the loop variable, and the increment must be an addition and/or multiplication of the loop variable. (See the standard section cited above for detailed specifics.)
Syntax:
C/C++
#pragma omp parallel
{
code
#pragma omp for
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 do
code
!$omp end do
!$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"):
code
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