| Home | Libraries | People | FAQ | More

## Function template reduce

boost::compute::reduce

## Synopsis

    // In header: <boost/compute/algorithm/reduce.hpp>

    template<typename InputIterator, typename OutputIterator,
             typename BinaryFunction>
      void reduce(InputIterator first, InputIterator last, OutputIterator result,
                  BinaryFunction function,
                  command_queue & queue = system::default_queue());

## Description

Returns the result of applying `function` to the elements in the range [`first`, `last`).

If no function is specified, `plus` will be used.

The `reduce()` algorithm assumes that the binary reduction function is associative. When used with non-associative functions the result may be non-deterministic and vary in precision. Notably this affects the `plus<float>()` function as floating-point addition is not associative and may produce slightly different results than a serial algorithm.

This algorithm supports both host and device iterators for the result argument. This allows for values to be reduced and copied to the host all with a single function call.

For example, to calculate the sum of the values in a device vector and copy the result to a value on the host:

Note that while the the `reduce()` algorithm is conceptually identical to the `accumulate()` algorithm, its implementation is substantially more efficient on parallel hardware. For more information, see the documentation on the `accumulate()` algorithm.

Space complexity on GPUs: \Omega(n)
Space complexity on CPUs: \Omega(1)

See Also: accumulate()

Parameters: |
`first` | first element in the input range
`last` | last element in the input range
`result` | iterator pointing to the output
`function` | binary reduction function
`queue` | command queue to perform the operation