...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::compute::accumulate
// In header: <boost/compute/algorithm/accumulate.hpp> template<typename InputIterator, typename T, typename BinaryFunction> T accumulate(InputIterator first, InputIterator last, T init, BinaryFunction function, command_queue & queue = system::default_queue());
Returns the result of applying function
to the elements in the range [first
, last
) and init
.
If no function is specified, plus
will be used.
In specific situations the call to accumulate()
can be automatically optimized to a call to the more efficient reduce()
algorithm. This occurs when the binary reduction function is recognized as associative (such as the plus<int>
function).
Note that because floatingpoint addition is not associative, calling accumulate()
with plus<float>
results in a less efficient serial reduction algorithm being executed. If a slight loss in precision is acceptable, the more efficient parallel reduce()
algorithm should be used instead.
For example:
// with vec = boost::compute::vector<int> accumulate(vec.begin(), vec.end(), 0, plus<int>()); // fast reduce(vec.begin(), vec.end(), &result, plus<int>()); // fast // with vec = boost::compute::vector<float> accumulate(vec.begin(), vec.end(), 0, plus<float>()); // slow reduce(vec.begin(), vec.end(), &result, plus<float>()); // fast
Space complexity: \Omega(1)
Space complexity when optimized to reduce()
: \Omega(n)
See Also:
reduce()
Parameters: 


Returns: 
the accumulated result value 