Lesson 13

Date: 4/19/2017
High Performance Computing (part I: OpenMP)
Linux System Administration


Reduction clause

Exercise
  • The Reduction clause performs a reduction on the variables that appear in its list.
    A private copy for each list variable is created for each thread. At the end of the reduction, the reduction variable is applied to all private copies of the shared variable, and the final result is written to the global shared variable.
    C / C++ - critical Directive Example
    #include <omp.h>
    #include <stdio.h>
    main ()  {
    
    int   i, n, chunk;
    float a[100], b[100], result;
    
    /* Some initializations */
    n = 100;
    chunk = 10;
    result = 0.0;
    for (i=0; i < n; i++)
      {
      a[i] = i * 1.0;
      b[i] = i * 2.0;
      }
    
    
    #pragma omp parallel for      \  
      default(shared) private(i)  \  
      schedule(static,chunk)      \  
      reduction(+:result)  
    
      for (i=0; i < n; i++)
        result = result + (a[i] * b[i]);
    
    printf("Final result= %f\n",result);
    
    }
    
    Copy the content of the code above into file reduction.c, then compile and run it as follows:
    gcc -fopenmp -o reduction.x reduction.c
    export OMP_NUM_THREADS=4
    ./reduction.x
    




  • Take me to the Course Website