thrust
Functions
Transformed Reductions
Reductions

Functions

template<typename InputIterator1 , typename InputIterator2 , typename OutputType >
OutputType thrust::inner_product (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputType init)
template<typename InputIterator1 , typename InputIterator2 , typename OutputType , typename BinaryFunction1 , typename BinaryFunction2 >
OutputType thrust::inner_product (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, OutputType init, BinaryFunction1 binary_op1, BinaryFunction2 binary_op2)
template<typename InputIterator , typename UnaryFunction , typename OutputType , typename BinaryFunction >
OutputType thrust::transform_reduce (InputIterator first, InputIterator last, UnaryFunction unary_op, OutputType init, BinaryFunction binary_op)

Function Documentation

template<typename InputIterator1 , typename InputIterator2 , typename OutputType >
OutputType thrust::inner_product ( InputIterator1  first1,
InputIterator1  last1,
InputIterator2  first2,
OutputType  init 
)

inner_product calculates an inner product of the ranges [first1, last1) and [first2, first2 + (last1 - first1)).

Specifically, this version of inner_product computes the sum init + (*first1 * *first2) + (*(first1+1) * *(first2+1)) + ...

Unlike the C++ Standard Template Library function std::inner_product, this version offers no guarantee on order of execution.

Parameters:
first1The beginning of the first sequence.
last1The end of the first sequence.
first2The beginning of the second sequence.
initInitial value of the result.
Returns:
The inner product of sequences [first1, last1) and [first2, last2) plus init.
Template Parameters:
InputIterator1is a model of Input Iterator,
InputIterator2is a model of Input Iterator,
OutputTypeis a model of Assignable, and if x is an object of type OutputType, and y is an object of InputIterator1's value_type, and z is an object of InputIterator2's value_type, then x + y * z is defined and is convertible to OutputType.

The following code demonstrates how to use inner_product to compute the dot product of two vectors.

  #include <thrust/inner_product.h>
  ...
  float vec1[3] = {1.0f, 2.0f, 5.0f};
  float vec2[3] = {4.0f, 1.0f, 5.0f};

  float result = thrust::inner_product(vec1, vec1 + 3, vec2, 0.0f);

  // result == 31.0f
See also:
http://www.sgi.com/tech/stl/inner_product.html
template<typename InputIterator1 , typename InputIterator2 , typename OutputType , typename BinaryFunction1 , typename BinaryFunction2 >
OutputType thrust::inner_product ( InputIterator1  first1,
InputIterator1  last1,
InputIterator2  first2,
OutputType  init,
BinaryFunction1  binary_op1,
BinaryFunction2  binary_op2 
)

inner_product calculates an inner product of the ranges [first1, last1) and [first2, first2 + (last1 - first1)).

This version of inner_product is identical to the first, except that is uses two user-supplied function objects instead of operator+ and operator*.

Specifically, this version of inner_product computes the sum binary_op1( init, binary_op2(*first1, *first2) ), ...

Unlike the C++ Standard Template Library function std::inner_product, this version offers no guarantee on order of execution.

Parameters:
first1The beginning of the first sequence.
last1The end of the first sequence.
first2The beginning of the second sequence.
initInitial value of the result.
binary_op1Generalized addition operation.
binary_op2Generalized multiplication operation.
Returns:
The inner product of sequences [first1, last1) and [first2, last2).
Template Parameters:
InputIterator1is a model of Input Iterator, and InputIterator1's value_type is convertible to BinaryFunction2's first_argument_type.
InputIterator2is a model of Input Iterator. and InputIterator2's value_type is convertible to BinaryFunction2's second_argument_type.
OutputTypeis a model of Assignable, and OutputType is convertible to BinaryFunction1's first_argument_type.
BinaryFunction1is a model of Binary Function, and BinaryFunction1's return_type is convertible to OutputType.
BinaryFunction2is a model of Binary Function, and BinaryFunction2's return_type is convertible to BinaryFunction1's second_argument_type.
  #include <thrust/inner_product.h>
  ...
  float vec1[3] = {1.0f, 2.0f, 5.0f};
  float vec2[3] = {4.0f, 1.0f, 5.0f};

  float init = 0.0f;
  thrust::plus<float>       binary_op1;
  thrust::multiplies<float> binary_op2;

  float result = thrust::inner_product(vec1, vec1 + 3, vec2, init, binary_op1, binary_op2);

  // result == 31.0f
See also:
http://www.sgi.com/tech/stl/inner_product.html
template<typename InputIterator , typename UnaryFunction , typename OutputType , typename BinaryFunction >
OutputType thrust::transform_reduce ( InputIterator  first,
InputIterator  last,
UnaryFunction  unary_op,
OutputType  init,
BinaryFunction  binary_op 
)

transform_reduce fuses the transform and reduce operations. transform_reduce is equivalent to performing a transformation defined by unary_op into a temporary sequence and then performing reduce on the transformed sequence. In most cases, fusing these two operations together is more efficient, since fewer memory reads and writes are required.

transform_reduce performs a reduction on the transformation of the sequence [first, last) according to unary_op. Specifically, unary_op is applied to each element of the sequence and then the result is reduced to a single value with binary_op using the initial value init. Note that the transformation unary_op is not applied to the initial value init. The order of reduction is not specified, so binary_op must be both commutative and associative.

Parameters:
firstThe beginning of the sequence.
lastThe end of the sequence.
unary_opThe function to apply to each element of the input sequence.
initThe result is initialized to this value.
binary_opThe reduction operation.
Returns:
The result of the transformed reduction.
Template Parameters:
InputIteratoris a model of Input Iterator, and InputIterator's value_type is convertible to UnaryFunction's argument_type.
UnaryFunctionis a model of Unary Function, and UnaryFunction's result_type is convertible to OutputType.
OutputTypeis a model of Assignable, and is convertible to BinaryFunction's first_argument_type and second_argument_type.
BinaryFunctionis a model of Binary Function, and BinaryFunction's result_type is convertible to OutputType.

The following code snippet demonstrates how to use transform_reduce to compute the maximum value of the absolute value of the elements of a range.

  #include <thrust/transform_reduce.h>
  #include <thrust/functional.h>

  template<typename T>
  struct absolute_value : public unary_function<T,T>
  {
    __host__ __device__ T operator()(const T &x) const
    {
      return x < T(0) ? -x : x;
    }
  };

  ...

  int data[6] = {-1, 0, -2, -2, 1, -3};
  int result = thrust::transform_reduce(data, data + 6,
                                        absolute_value<int>(),
                                        0,
                                        thrust::maximum<int>());
  // result == 3
See also:
transform
reduce