Thrust

◆ inner_product() [1/4]

template<typename DerivedPolicy , typename InputIterator1 , typename InputIterator2 , typename OutputType >
__host__ __device__ OutputType thrust::inner_product ( const thrust::detail::execution_policy_base< DerivedPolicy > &  exec,
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)) + ...

The algorithm's execution is parallelized as determined by exec.

Parameters
execThe execution policy to use for parallelization.
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
DerivedPolicyThe name of the derived execution policy.
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 using the thrust::host execution policy for parallelization.

...
float vec1[3] = {1.0f, 2.0f, 5.0f};
float vec2[3] = {4.0f, 1.0f, 5.0f};
float result = thrust::inner_product(thrust::host, vec1, vec1 + 3, vec2, 0.0f);
// result == 31.0f
See also
https://en.cppreference.com/w/cpp/algorithm/inner_product