OpenMP Reduction Operations

OpenMP reduction operations can be used for simple cases, such as incrementing a shared numeric variable or the summation of an array into a shared numeric variable. To implement a reduction operation, add the reduction clause within a parallel region to instruct the compiler to perform the summation operation in parallel using the specified operation and variable.

Consider this annotated C/C++ serial code:

  int  i, n=500000; 
  float *array, total=0.0;
     ...
      for (i=0; i <n ; ++i  
      {
        ANNOTATE_LOCK_ACQUIRE(0);
        total+ = array[i];
        ANNOTATE_LOCK_RELEASE(0);
      }
. . .

The parallel C/C++ code after adding #include <omp.h> and #pragma omp parallel for reduction:

#include <omp.h>  //prevents a load-time problem with a .dll not being found
  int  i, n=500000; 
  float *array, total=0.0;
     ...
     #pragma omp parallel for reduction(+:total)
        for (i=0; i <n ; ++i  
        {
          total+ = array[i];
        }
. . .

Consider this annotated Fortran serial code:

  integer(4) n
  real(4) array(50000), total = 0.0
  n = 500000
  ...
     do i=1, n
     call annotate_lock_acquire(0)
        total = total + array(i)
     call annotate_lock_release(0)
 . . .
     end do

Consider this parallel Fortran code after adding use omp_lib, !$omp parallel do reduction(+:total), and !$omp end parallel do:

  use omp_lib
  integer(4) n
  real(4) array(50000), total = 0.0
  n = 500000
  ...

  !$omp parallel do reduction(+:total)
     do i=1, n
        total = total + array(i)
  !$omp end parallel do
 . . .
     end do
For more complete information about compiler optimizations, see our Optimization Notice.