Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of boost. Click here for the latest Boost documentation.
PrevUpHomeNext

Function accumulate

boost::compute::accumulate

Synopsis

// 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());

Description

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:

first

first element in the input range

function

binary reduction function

init

initial value

last

last element in the input range

queue

command queue to perform the operation

Returns:

the accumulated result value


PrevUpHomeNext