The following examples show how to use the OpenMP* feature.
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.
void for_1 (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; } } } |
The example below 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.
void for_2 (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; } } } |