icp/oer/courses/c-openmp/sections/02-advanced/03-reduce/content.md

14 lines
800 B
Markdown
Raw Normal View History

2018-05-05 22:18:02 +00:00
Some race conditions can be resolved by every thread calculating the result in a private variable and combining all the results in the end.
This is often faster, because it involves less waiting.
## Knowledge
To use the reduction, you have to add the reduction clause ``reduction(operator : var)`` to your compiler directive.
You can use reductions for a handful of operations. Most of the arithmetic operators and some of the logic and bitwise ones plus min and max.
For a complete list check the [quick reference](http://www.openmp.org/wp-content/uploads/OpenMP3.1-CCard.pdf).
Reduction only need to synchronize as often as there are threads instead of how many operations are actually done.
## Task
Instead of using a critical section as before, try your hand at a reduction variable instead.