omp_reduction.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  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. int main (int argc, char *argv[]) {
  17. int i, n;
  18. float a[100], b[100], sum;
  19. /* Some initializations */
  20. n = 100;
  21. for (i=0; i < n; i++)
  22. a[i] = b[i] = i * 1.0;
  23. sum = 0.0;
  24. #pragma omp parallel for reduction(+:sum)
  25. for (i=0; i < n; i++)
  26. sum = sum + (a[i] * b[i]);
  27. printf(" Sum = %f\n",sum);
  28. return 0;
  29. }