The following examples show how to use the OpenMP* features.
This example shows a simple parallel loop where the amount of work in each iteration is different. Dynamic scheduling is used to get good load balancing. The for has a nowait because there is an implicit barrier at the end of the parallel region.
Example |
---|
void for1(float a[], float b[], int n) { int i, j; #pragma omp parallel shared(a,b,n) private(i,j) { #pragma omp for schedule(dynamic,1) nowait for (i = 1; i < n; i++) for (j = 0; j <= i; j++) b[j + n*i] = (a[j + n*i] + a[j + n*(i-1)]) / 2.0; } } |
This example uses two parallel loops fused to reduce fork/join overhead. The first for has a nowait because all the data used in the second loop is different than all the data used in the first loop.
Example |
---|
void for2(float a[], float b[], float c[], float d[], int n, int m) { int i, j; #pragma omp parallel shared(a,b,c,d,n,m) private(i,j) { #pragma omp for schedule(dynamic,1) nowait for (i = 1; i < n; i++) for (j = 0; j <= i; j++) b[j + n*i] = ( a[j + n*i] + a[j + n*(i-1)] )/2.0; #pragma omp for schedule(dynamic,1) nowait for (i = 1; i < m; i++) for (j = 0; j <= i; j++) d[j + m*i] = ( c[j + m*i] + c[j + m*(i-1)] )/2.0; } } |
See more examples in the OpenMP C/C++ version 2.5 specifications (http://www.openmp.org/specs).