Text-only | Table of Contents (frame/ no frame) |

(11) Reduction Clause |

An example of a reduction operation is a summation:

! Fortran example`do i = 1, n`

sum = sum + a(i)

enddo

`/* C/C++ Example */`

`for(i=1; i<=n; i++){`

sum = sum + a[i];

}

How reduction works:

- sum is the reduction variable
- cannot be declared shared
- threads would overwrite the value of sum
- cannot be declared private
- private variables don't persist outside of parallel region
- specified reduction operation performed on individual values from each thread

Example of reduction clause

! Fortran example`!$omp parallel do reduction(+:sum)`

do i = 1, n

sum = sum + a(i)

enddo

`/* C/C++ example */`

`#pragma omp parallel for reduction(+:sum)`

{

for(i=1; i<=n; i++){

sum = sum + a[i];

}

}

C/C++ Reduction Operands

Operator | Initial value |

+ | 0 |

* | 1 |

- | 0 |

& | ~0 |

| | 0 |

^ | 0 |

&& | 1 |

|| | 0 |

Fortran Reduction Operands

Operator | Initial Value |

+ | 0 |

* | 1 |

- | 0 |

.AND. | .true. |

.OR. | .false. |

.IEOR. | 0 |

.IOR. | 0 |

.IAND. | All bits on |

.EQV. | .true. |

MIN | Largest positive # |

MAX | Most negative # |

reduction_clause.src last modified Mar 23, 2009 | Introduction | Table of Contents (frame/no frame) |
Printable (single file) |
© Dartmouth College |