1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2015 Jakub Szuppe <j.szuppe@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_REDUCE_BY_KEY_HPP
12 #define BOOST_COMPUTE_ALGORITHM_REDUCE_BY_KEY_HPP
17 #include <boost/compute/command_queue.hpp>
18 #include <boost/compute/device.hpp>
19 #include <boost/compute/functional.hpp>
20 #include <boost/compute/system.hpp>
21 #include <boost/compute/algorithm/detail/reduce_by_key.hpp>
26 /// The \c reduce_by_key() algorithm performs reduction for each contiguous
27 /// subsequence of values determinate by equivalent keys.
29 /// Returns a pair of iterators at the end of the ranges [\p keys_result, keys_result_last)
30 /// and [\p values_result, \p values_result_last).
32 /// If no function is specified, \c plus will be used.
33 /// If no predicate is specified, \c equal_to will be used.
35 /// \param keys_first the first key
36 /// \param keys_last the last key
37 /// \param values_first the first input value
38 /// \param keys_result iterator pointing to the key output
39 /// \param values_result iterator pointing to the reduced value output
40 /// \param function binary reduction function
41 /// \param predicate binary predicate which returns true only if two keys are equal
42 /// \param queue command queue to perform the operation
44 /// The \c reduce_by_key() algorithm assumes that the binary reduction function
45 /// is associative. When used with non-associative functions the result may
46 /// be non-deterministic and vary in precision. Notably this affects the
47 /// \c plus<float>() function as floating-point addition is not associative
48 /// and may produce slightly different results than a serial algorithm.
50 /// For example, to calculate the sum of the values for each key:
52 /// \snippet test/test_reduce_by_key.cpp reduce_by_key_int
55 template<class InputKeyIterator, class InputValueIterator,
56 class OutputKeyIterator, class OutputValueIterator,
57 class BinaryFunction, class BinaryPredicate>
58 inline std::pair<OutputKeyIterator, OutputValueIterator>
59 reduce_by_key(InputKeyIterator keys_first,
60 InputKeyIterator keys_last,
61 InputValueIterator values_first,
62 OutputKeyIterator keys_result,
63 OutputValueIterator values_result,
64 BinaryFunction function,
65 BinaryPredicate predicate,
66 command_queue &queue = system::default_queue())
68 return detail::dispatch_reduce_by_key(keys_first, keys_last, values_first,
69 keys_result, values_result,
75 template<class InputKeyIterator, class InputValueIterator,
76 class OutputKeyIterator, class OutputValueIterator,
78 inline std::pair<OutputKeyIterator, OutputValueIterator>
79 reduce_by_key(InputKeyIterator keys_first,
80 InputKeyIterator keys_last,
81 InputValueIterator values_first,
82 OutputKeyIterator keys_result,
83 OutputValueIterator values_result,
84 BinaryFunction function,
85 command_queue &queue = system::default_queue())
87 typedef typename std::iterator_traits<InputKeyIterator>::value_type key_type;
89 return reduce_by_key(keys_first, keys_last, values_first,
90 keys_result, values_result,
91 function, equal_to<key_type>(),
96 template<class InputKeyIterator, class InputValueIterator,
97 class OutputKeyIterator, class OutputValueIterator>
98 inline std::pair<OutputKeyIterator, OutputValueIterator>
99 reduce_by_key(InputKeyIterator keys_first,
100 InputKeyIterator keys_last,
101 InputValueIterator values_first,
102 OutputKeyIterator keys_result,
103 OutputValueIterator values_result,
104 command_queue &queue = system::default_queue())
106 typedef typename std::iterator_traits<InputKeyIterator>::value_type key_type;
107 typedef typename std::iterator_traits<InputValueIterator>::value_type value_type;
109 return reduce_by_key(keys_first, keys_last, values_first,
110 keys_result, values_result,
111 plus<value_type>(), equal_to<key_type>(),
115 } // end compute namespace
116 } // end boost namespace
118 #endif // BOOST_COMPUTE_ALGORITHM_REDUCE_BY_KEY_HPP