Intel® Advisor User Guide

ID 766448
Date 11/07/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

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.

TIP:
After you rewrite your code to use OpenMP* parallel framework, you can analyze its performance with Intel® Advisor perspectives. Use the Vectorization and Code Insights perspective to analyze how well you OpenMP code is vectorized or use the Offload Modeling perspective to model its performance on a GPU.

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