| 1 |
/****************************************************************************** |
| 2 |
* FILE: omp_reduction.c |
| 3 |
* DESCRIPTION: |
| 4 |
* OpenMP Example - Combined Parallel Loop Reduction - C/C++ Version |
| 5 |
* This example demonstrates a sum reduction within a combined parallel loop |
| 6 |
* construct. Notice that default data element scoping is assumed - there |
| 7 |
* are no clauses specifying shared or private variables. OpenMP will |
| 8 |
* automatically make loop index variables private within team threads, and |
| 9 |
* global variables shared. |
| 10 |
* AUTHOR: Blaise Barney 5/99 |
| 11 |
* LAST REVISED: 04/06/05 |
| 12 |
******************************************************************************/ |
| 13 |
#include <omp.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
|
| 17 |
int main (int argc, char *argv[]) { |
| 18 |
|
| 19 |
int i, n; |
| 20 |
float a[100], b[100], sum; |
| 21 |
|
| 22 |
/* Some initializations */ |
| 23 |
n = 100; |
| 24 |
for (i=0; i < n; i++) |
| 25 |
a[i] = b[i] = i * 1.0; |
| 26 |
sum = 0.0; |
| 27 |
|
| 28 |
#pragma omp parallel for reduction(+:sum) |
| 29 |
for (i=0; i < n; i++) |
| 30 |
sum = sum + (a[i] * b[i]); |
| 31 |
|
| 32 |
printf(" Sum = %f\n",sum); |
| 33 |
|
| 34 |
return 0; |
| 35 |
} |