3.1. A Simple Parallel Loop#

The following example demonstrates how to parallelize a simple loop using the parallel worksharing-loop construct. The loop iteration variable is private by default, so it is not necessary to specify it explicitly in a private clause.

//%compiler: clang
//%cflags: -fopenmp

/*
* name: ploop.1
* type: C
*/
void simple(int n, float *a, float *b)
{
    int i;

#pragma omp parallel for
    for (i=1; i<n; i++) /* i is private by default */
        b[i] = (a[i] + a[i-1]) / 2.0;
}

!!%compiler: gfortran
!!%cflags: -fopenmp

! name: ploop.1
! type: F-fixed
      SUBROUTINE SIMPLE(N, A, B)

      INTEGER I, N
      REAL B(N), A(N)

!$OMP PARALLEL DO  !I is private by default
      DO I=2,N
          B(I) = (A(I) + A(I-1)) / 2.0
      ENDDO
!$OMP END PARALLEL DO

      END SUBROUTINE SIMPLE