Reduction

Reduction

Reduction

Motivation
  • Let’s review sum_series_openmp_for.c:

    c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/sum_series_openmp_for.c"

  • We used a shared data array for partial sums from the threads, this is to prevent data races.

  • Another solution: reduction clause.

    c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/sum_series.c"

More reduction
  • Can be done over section

    c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/function_sections.c"

  • Can be done without for

    c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/reduction_rand.c"

Hands-on
  • Implement the parallel dot product for Lab 1 using reduction

Reduction operators

Built-in reductions
  • Arithmetic reductions: +, *, max, min.
    • - is deprecated as of OpenMP 5.2
  • Logical reductions: &, &&, |, ||, ^
Custom reductions
  • Formula:
1
#pragma omp declare reduction (identifier : typelist : combiner) initializer (initializer expression)
  • This is all written on a single line!!!
  • To write this over multiple lines, a \ is needed
  • initializer (initializer expression) is optional
1
2
3
#pragma omp declare reduction ( \
    identifier : typelist : combiner) \
    initializer (initializer expression)
  • Example code for sum series:

    c linenums="1" --8<-- "docs/csc466/lectures/data/openmp/sum_series_custom.c"

Midterm Exam