![]() |
Home | Libraries | People | FAQ | More |
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()); template<typename InputIterator, typename T> T accumulate(InputIterator first, InputIterator last, T init, 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 floating-point 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: (1)
Space complexity when optimized to reduce(): (n)
See Also:
reduce()
Parameters: |
|
||||||||||
Returns: |
the accumulated result value |