Module 11: The “lastprivate” Clause
  Lecture 21: Clause and Routines
 


Reduction

How to handle this case?

Example
double ave = 0.0, A [ MAX ]; int i;
for (i = 0; i < MAX; i++) {
ave + = A[i];
}
ave = ave/MAX;
  • We are combining values into a single accumulation variable (ave) there is a true dependence between loop iterations that can not be trivially removed
  • This is a very common situation.it is called a “reduction”
  • Support for reduction operations is included in most parallel programming environments.

Data Scope Attribute Clauses

“reduction” Clause
Purpose: Reduction
Format:
reduction (operation : list)
  • A local copy of each list variable is made and initialized depending on the “op” (e.g. 0 for “+”)
  • Compiler finds standard reduction expressions containing “op” and uses them to update the local copy.
  • Local copies are reduced into a single value and combined with the original global value
  • Variables in the list must be named scalar variables. They can not be array or structure type variables.
  • Reduction variables must be shared in the enclosing context.
  • Note that the value of a reduction variable is undefined from the moment the first thread reaches the clause till the operation has completed