template<typename DerivedPolicy , typename InputIterator1 , typename InputIterator2 , typename OutputIterator1 , typename OutputIterator2 >
__host__ __device__ thrust::pair<OutputIterator1,OutputIterator2> thrust::reduce_by_key |
( |
const thrust::detail::execution_policy_base< DerivedPolicy > & |
exec, |
|
|
InputIterator1 |
keys_first, |
|
|
InputIterator1 |
keys_last, |
|
|
InputIterator2 |
values_first, |
|
|
OutputIterator1 |
keys_output, |
|
|
OutputIterator2 |
values_output |
|
) |
| |
reduce_by_key
is a generalization of reduce
to key-value pairs. For each group of consecutive keys in the range [keys_first, keys_last)
that are equal, reduce_by_key
copies the first element of the group to the keys_output
. The corresponding values in the range are reduced using the plus
and the result copied to values_output
.
This version of reduce_by_key
uses the function object equal_to
to test for equality and plus
to reduce values with equal keys.
The algorithm's execution is parallelized as determined by exec
.
- Parameters
-
exec | The execution policy to use for parallelization. |
keys_first | The beginning of the input key range. |
keys_last | The end of the input key range. |
values_first | The beginning of the input value range. |
keys_output | The beginning of the output key range. |
values_output | The beginning of the output value range. |
- Returns
- A pair of iterators at end of the ranges
[keys_output, keys_output_last)
and [values_output, values_output_last)
.
- Template Parameters
-
DerivedPolicy | The name of the derived execution policy. |
InputIterator1 | is a model of Input Iterator, |
InputIterator2 | is a model of Input Iterator, |
OutputIterator1 | is a model of Output Iterator and and InputIterator1's value_type is convertible to OutputIterator1's value_type . |
OutputIterator2 | is a model of Output Iterator and and InputIterator2's value_type is convertible to OutputIterator2's value_type . |
- Precondition
- The input ranges shall not overlap either output range.
The following code snippet demonstrates how to use reduce_by_key
to compact a sequence of key/value pairs and sum values with equal keys using the thrust::host
execution policy for parallelization:
...
const int N = 7;
int A[N] = {1, 3, 3, 3, 2, 2, 1};
int B[N] = {9, 8, 7, 6, 5, 4, 3};
int C[N];
int D[N];
- See also
- reduce
-
unique_copy
-
unique_by_key
-
unique_by_key_copy