Loop Collapse

Loop Collapse in OpenMP 3.0

The COLLAPSE clause can appear on a do/for loop directive. It is used to parallelize perfectly nested loops without using nested parallelism. The COLLAPSE clause takes a constant positive integer expression as parameter. The parameter specifies how many loops should be collapsed by the compiler into one loop before parallelizing the resulting loop.

The COLLAPSE clause is described in Section 2.5.1 (p. 39) of the OpenMP specification.

Example (Fortran):

       program test
      use omp_lib

      integer, parameter :: N = 10
      integer i, j, x

      call omp_set_dynamic (.false.)
      call omp_set_num_threads (4)

      x = 10

!$OMP PARALLEL SHARED(x) PRIVATE(i, j)
!$OMP DO COLLAPSE(2)
      do i = 1, N
         do j = 1, N
!$OMP CRITICAL
            x = x + 1
!$OMP END CRITICAL
         end do
      end do
!$OMP END PARALLEL

      print *, "x = ", x
      end


% f90 -xopenmp -xO3 test.f
% a.out
 x =  110

 Example (C/C++):

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

#define N 10

int main(void)
{
  int i, j, x;

  omp_set_dynamic(0);
  omp_set_num_threads(4);

  x = 10;

  #pragma omp parallel shared(x) private(i, j)
  {
     #pragma omp for collapse(2)
     for (i = 0; i < N; i = i + 2)
     {
        for (j = 0; j < N; j = j + 2)
        {
           #pragma omp atomic
           x++;
        }
     }
  }

  printf ("x = %d\n", x);
}


% cc -xopenmp -xO3 test.c
% a.out
x = 35
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Sign up or Log in to add a comment or watch this page.


The individuals who post here are part of the extended Sun Microsystems community and they might not be employed or in any way formally affiliated with Sun Microsystems. The opinions expressed here are their own, are not necessarily reviewed in advance by anyone but the individual authors, and neither Sun nor any other party necessarily agrees with them.

Copyright 1994-2009 Sun Microsystems, Inc.
Powered by Atlassian Confluence
Sun Guidelines on Public Discourse Privacy Policy Terms of Use Trademarks Site Map Employment Investor Relations Contact