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"
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"
+, *, max, min. - is deprecated as of OpenMP 5.2&, &&, |, ||, ^
1
#pragma omp declare reduction (identifier : typelist : combiner) initializer (initializer expression)
\ is neededinitializer (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"