| 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 <civlc.h>
|
|---|
| 19 | #include <stdio.h>
|
|---|
| 20 | #include <stdlib.h>
|
|---|
| 21 | #include "omp.cvh"
|
|---|
| 22 | $input int N;
|
|---|
| 23 |
|
|---|
| 24 | int main (int argc, char *argv[]) {
|
|---|
| 25 | int i;
|
|---|
| 26 | double a[N], b[N], sum;
|
|---|
| 27 |
|
|---|
| 28 | /* Some initializations */
|
|---|
| 29 | for (i=0; i < N; i++)
|
|---|
| 30 | a[i] = b[i] = i * 1.0;
|
|---|
| 31 | sum = 0.0;
|
|---|
| 32 |
|
|---|
| 33 | /* The procedure of each thread for the for loop */
|
|---|
| 34 | void __for_0(int __tid, int __start, int __end, int __extra){
|
|---|
| 35 | // if(__extra > 0)
|
|---|
| 36 | // printf(" __for_1 thread %d starts from index %d to %d, %d\n", __tid, __start, __end, __extra);
|
|---|
| 37 | // else
|
|---|
| 38 | // printf(" __for_1 thread %d starts from index %d to %d\n", __tid, __start, __end);
|
|---|
| 39 | for(int __i = __start; __i < __end; __i++) {
|
|---|
| 40 | sum = sum + (a[__i] * b[__i]);
|
|---|
| 41 | // printf("loop %d\n", __i);
|
|---|
| 42 | }
|
|---|
| 43 | if(__extra > 0) {
|
|---|
| 44 | sum = sum + (a[__extra] * b[__extra]);
|
|---|
| 45 | printf("loop %d\n", __extra);
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | /* Helper variales introduced for the parallel for loop */
|
|---|
| 50 | $proc __for_0_procs[OMP_NUM_THREADS];
|
|---|
| 51 |
|
|---|
| 52 | /* Create processes to conduct the parallel for loop */
|
|---|
| 53 | for(i = 0; i < OMP_NUM_THREADS; i++) {
|
|---|
| 54 | int __start = __for_start(i, N);
|
|---|
| 55 | int __end = __for_end(i, N);
|
|---|
| 56 | int __extra = __for_extra(i, N);
|
|---|
| 57 |
|
|---|
| 58 | __for_0_procs[i] = $spawn __for_0(i, __start, __end, __extra);
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /** The orginal OpenMP parallel for loop
|
|---|
| 62 | #pragma omp parallel for reduction(+:sum)
|
|---|
| 63 | for (i=0; i < n; i++){
|
|---|
| 64 | sum = sum + (a[i] * b[i]);
|
|---|
| 65 | printf("loop %d\n", i);
|
|---|
| 66 | }
|
|---|
| 67 | **/
|
|---|
| 68 |
|
|---|
| 69 | /* Join parallel for processes */
|
|---|
| 70 | for(i = 0; i < OMP_NUM_THREADS; i++) {
|
|---|
| 71 | $wait(__for_0_procs[i]);
|
|---|
| 72 | }
|
|---|
| 73 | /* Print result */
|
|---|
| 74 | printf(" Sum = %f\n",sum);
|
|---|
| 75 | }
|
|---|