Combined parallel work-sharing constructs are shortcuts for specifying a parallel region that contains only one work-sharing construct. The semantics of these directives are identical to that of explicitly specifying a parallel directive followed by a single work-sharing construct.
The following sections describe the combined parallel work-sharing constructs:
Section 9.6.1, “parallel for construct” on page 347
Section 9.6.2, “parallel sections construct” on page 348
The parallel for directive is a shortcut for a parallel region that contains only a single for directive. The syntax of the parallel for directive is as follows:
#pragma omp parallel for [clause[[,] clause] ...] new-line for-loop
This directive allows all the clauses of the parallel directive and the for directive, except the nowait clause, with identical meanings and restrictions. The semantics are identical to explicitly specifying a parallel directive immediately followed by a for directive.
The following example illustrates the use of the parallel for construct. Iterations of the for loop will be distributed in equal sized blocks to each thread in the team (schedule static):
/* File : omp_parallel_for.c */ #include <stdlib.h> #define N 10 #define CHUNKSIZE 5 int main(int argc, char *argv[]) { int i, chunk_size; float a[N], b[N], total[N]; /* Some initializations. */ for (i = 0; i < N; i++) a[i] = b[i] = i * 1.0; chunk_size = CHUNKSIZE; #pragma omp parallel for shared(a,b,total,chunk_size) private(i) schedule(static,chunk_size) for (i = 0; i < N; i++) total[i] = a[i] + b[i]; for (i = 0; i < N; i++) printf("Total value is = %f ", total[i]); }
When executed, the program prints the same output shown in Example 9-3 on page 343.
The parallel sections directive provides a shortcut form for specifying a parallel region containing only a single sections directive. The semantics are identical to explicitly specifying a parallel directive immediately followed by a sections directive. The syntax of the parallel sections directive is as follows:
#pragma omp parallel sections [clause[[,] clause] ...] new-line { [#pragma omp section new-line] structured-block [#pragma omp section new-line structured-block ] ... }
The clause can be one of the clauses accepted by the parallel and sections directives, except the nowait clause.