main
| Line | |
|---|
| 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 | /**
|
|---|
| 14 | * This program computes the dot product of two vectors.
|
|---|
| 15 | * Online source:
|
|---|
| 16 | * https://computing.llnl.gov/tutorials/openMP/samples/C/omp_reduction.c
|
|---|
| 17 | **/
|
|---|
| 18 | #include <omp.h>
|
|---|
| 19 | #include <stdio.h>
|
|---|
| 20 | #include <stdlib.h>
|
|---|
| 21 |
|
|---|
| 22 | int main (int argc, char *argv[])
|
|---|
| 23 | {
|
|---|
| 24 | int i, n;
|
|---|
| 25 | float a[8], b[8], sum;
|
|---|
| 26 |
|
|---|
| 27 | /* Some initializations */
|
|---|
| 28 | n = 8;
|
|---|
| 29 | for (i=0; i < n; i++)
|
|---|
| 30 | a[i] = b[i] = i * 1.0;
|
|---|
| 31 | sum = 0.0;
|
|---|
| 32 |
|
|---|
| 33 | #pragma omp parallel for reduction(+:sum)
|
|---|
| 34 | for (i=0; i < n; i++){
|
|---|
| 35 | sum = sum + (a[i] * b[i]);
|
|---|
| 36 | printf("loop %d\n", i);
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | printf(" Sum = %f\n",sum);
|
|---|
| 40 |
|
|---|
| 41 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.