Previous: Structured Parallel Loops: parfor Up: Structured Parallel Loops: parfor Next: Sharing Data

Introduction

The construct for parallel composition of a variable number of statements is parfor. With the exception of the keyword, the syntax of a parfor statement is the same as the usual C++ for statement:


parfor (int i=0; i<N; i++)  {
  statement_1;
  statement_2;
  ...
  statement_N
}

This is a parallel loop construct in which the iterations are executed in parallel with each other. As with the usual C or C++ for loop, the body of each iteration is executed sequentially. Similar to the parallel block discussed in Section , there is an implicit barrier at the end of a parfor. The parfor statement completes only when all the iterations have completed.

As a simple example, consider:


{
  int A[N];
  parfor (int i=0; i<N; i++) 
    A[i] = i; 
}

Here there are N parallel threads of control. Each element of the array is assigned (by a different thread of control) to the value of its index. The parfor statement terminates only when all elements of the array have been assigned their values. The flow of control for this example is illustrated in Figure .

paolo@cs.caltech.edu