|
| 1 | +.. _rvalue_reduce: |
| 2 | + |
| 3 | +Parallel Reduction for rvalues |
| 4 | +============================== |
| 5 | + |
| 6 | +.. contents:: |
| 7 | + :local: |
| 8 | + :depth: 1 |
| 9 | + |
| 10 | +Description |
| 11 | +*********** |
| 12 | + |
| 13 | +|full_name| implementation extends the `ParallelReduceFunc <https://spec.oneapi.io/versions/latest/elements/oneTBB/source/named_requirements/algorithms/par_reduce_func.html>`_ and |
| 14 | +`ParallelReduceReduction <https://spec.oneapi.io/versions/latest/elements/oneTBB/source/named_requirements/algorithms/par_reduce_reduction.html>`_ |
| 15 | +to optimize operating with ``rvalues`` using functional form of ``tbb::parallel_reduce`` and ``tbb::parallel_deterministic_reduce`` algorithms. |
| 16 | + |
| 17 | +API |
| 18 | +*** |
| 19 | + |
| 20 | +Header |
| 21 | +------ |
| 22 | + |
| 23 | +.. code:: cpp |
| 24 | +
|
| 25 | + #include <oneapi/tbb/parallel_reduce.h> |
| 26 | +
|
| 27 | +ParallelReduceFunc Requirements: Pseudo-Signature, Semantics |
| 28 | +------------------------------------------------------------ |
| 29 | + |
| 30 | +.. cpp:function:: Value Func::operator()(const Range& range, Value&& x) const |
| 31 | + |
| 32 | +or |
| 33 | + |
| 34 | +.. cpp:function:: Value Func::operator()(const Range& range, const Value& x) const |
| 35 | + |
| 36 | + Accumulates the result for a subrange, starting with initial value ``x``. The ``Range`` type must meet the `Range requirements <https://spec.oneapi.io/versions/latest/elements/oneTBB/source/named_requirements/algorithms/range.html>_`. |
| 37 | + The ``Value`` type must be the same as a corresponding template parameter for the `parallel_reduce algorithm <https://spec.oneapi.io/versions/latest/elements/oneTBB/source/algorithms/functions/parallel_reduce_func.html>`_. |
| 38 | + |
| 39 | + If both ``rvalue`` and ``lvalue`` forms are provided, the ``rvalue`` is preferred. |
| 40 | + |
| 41 | +ParallelReduceReduction Requirements: Pseudo-Signature, Semantics |
| 42 | +----------------------------------------------------------------- |
| 43 | + |
| 44 | +.. cpp:function:: Value Reduction::operator()(Value&& x, Value&& y) const |
| 45 | + |
| 46 | +or |
| 47 | + |
| 48 | +.. cpp:function:: Value Reduction::operator()(const Value& x, const Value& y) const |
| 49 | + |
| 50 | + Combines the ``x`` and ``y`` results. The ``Value`` type must be the same as a corresponding template parameter for the `parallel_reduce algorithm <https://spec.oneapi.io/versions/latest/elements/oneTBB/source/algorithms/functions/parallel_reduce_func.html>`_. |
| 51 | + |
| 52 | + If both ``rvalue`` and ``lvalue`` forms are provided, the ``rvalue`` is preferred. |
| 53 | + |
| 54 | +Example |
| 55 | +******* |
| 56 | + |
| 57 | +.. code:: cpp |
| 58 | + // C++17 |
| 59 | + #include <oneapi/tbb/parallel_reduce.h> |
| 60 | + #include <oneapi/tbb/blocked_range.h> |
| 61 | + #include <vector> |
| 62 | + #include <set> |
| 63 | +
|
| 64 | + int main() { |
| 65 | + std::vector<std::set<int>> sets = ...; |
| 66 | +
|
| 67 | + oneapi::tbb::parallel_reduce(oneapi::tbb::blocked_range<size_t>(0, sets.size()), |
| 68 | + std::set<int>{}, // identity element - empty set |
| 69 | + [&](const oneapi::tbb::blocked_range<size_t>& range, std::set<int>&& value) { |
| 70 | + for (size_t i = range.begin(); i < range.end(); ++i) { |
| 71 | + // Having value as a non-const rvalue reference allows to efficiently |
| 72 | + // transfer nodes from sets[i] without copying/moving the data |
| 73 | + value.merge(std::move(sets[i])); |
| 74 | + } |
| 75 | + return value; |
| 76 | + }, |
| 77 | + [&](std::set<int>&& x, std::set<int>&& y) { |
| 78 | + x.merge(std::move(y)); |
| 79 | + return x; |
| 80 | + } |
| 81 | + ); |
| 82 | + } |
| 83 | +
|
| 84 | +.. rubric:: See also |
| 85 | + |
| 86 | +* `oneapi::tbb::parallel_reduce specification <https://spec.oneapi.io/versions/latest/elements/oneTBB/source/algorithms/functions/parallel_reduce_func.html>`_ |
| 87 | +* `oneapi::tbb::parallel_deterministic_reduce specification <https://spec.oneapi.io/versions/latest/elements/oneTBB/source/algorithms/functions/parallel_deterministic_reduce_func.html>`_ |
| 88 | +* `ParallelReduceFunc specification <https://spec.oneapi.io/versions/latest/elements/oneTBB/source/named_requirements/algorithms/par_reduce_func.html>`_ |
| 89 | +* `ParallelReduceReduction specification <https://spec.oneapi.io/versions/latest/elements/oneTBB/source/named_requirements/algorithms/par_reduce_reduction.html>`_ |
0 commit comments