• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com>
3 //
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
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #ifndef BOOST_COMPUTE_ALGORITHM_UNIQUE_COPY_HPP
12 #define BOOST_COMPUTE_ALGORITHM_UNIQUE_COPY_HPP
13 
14 #include <boost/static_assert.hpp>
15 
16 #include <boost/compute/command_queue.hpp>
17 #include <boost/compute/lambda.hpp>
18 #include <boost/compute/system.hpp>
19 #include <boost/compute/algorithm/copy_if.hpp>
20 #include <boost/compute/algorithm/transform.hpp>
21 #include <boost/compute/algorithm/gather.hpp>
22 #include <boost/compute/container/vector.hpp>
23 #include <boost/compute/detail/iterator_range_size.hpp>
24 #include <boost/compute/detail/meta_kernel.hpp>
25 #include <boost/compute/functional/operator.hpp>
26 #include <boost/compute/type_traits/is_device_iterator.hpp>
27 
28 namespace boost {
29 namespace compute {
30 namespace detail {
31 
32 template<class InputIterator, class OutputIterator, class BinaryPredicate>
serial_unique_copy(InputIterator first,InputIterator last,OutputIterator result,BinaryPredicate op,command_queue & queue)33 inline OutputIterator serial_unique_copy(InputIterator first,
34                                          InputIterator last,
35                                          OutputIterator result,
36                                          BinaryPredicate op,
37                                          command_queue &queue)
38 {
39     if(first == last){
40         return result;
41     }
42 
43     typedef typename std::iterator_traits<InputIterator>::value_type value_type;
44 
45     const context &context = queue.get_context();
46 
47     size_t count = detail::iterator_range_size(first, last);
48 
49     detail::meta_kernel k("serial_unique_copy");
50 
51     vector<uint_> unique_count_vector(1, context);
52 
53     size_t size_arg = k.add_arg<const uint_>("size");
54     size_t unique_count_arg = k.add_arg<uint_ *>(memory_object::global_memory, "unique_count");
55 
56     k << k.decl<uint_>("index") << " = 0;\n"
57       << k.decl<value_type>("current") << " = " << first[k.var<uint_>("0")] << ";\n"
58       << result[k.var<uint_>("0")] << " = current;\n"
59       << "for(uint i = 1; i < size; i++){\n"
60       << "    " << k.decl<value_type>("next") << " = " << first[k.var<uint_>("i")] << ";\n"
61       << "    if(!" << op(k.var<value_type>("current"), k.var<value_type>("next")) << "){\n"
62       << "        " << result[k.var<uint_>("++index")] << " = next;\n"
63       << "        " << "current = next;\n"
64       << "    }\n"
65       << "}\n"
66       << "*unique_count = index + 1;\n";
67 
68     k.set_arg<const uint_>(size_arg, count);
69     k.set_arg(unique_count_arg, unique_count_vector.get_buffer());
70 
71     k.exec_1d(queue, 0, 1, 1);
72 
73     uint_ unique_count;
74     copy_n(unique_count_vector.begin(), 1, &unique_count, queue);
75 
76     return result + unique_count;
77 }
78 
79 template<class InputIterator, class OutputIterator, class BinaryPredicate>
unique_copy(InputIterator first,InputIterator last,OutputIterator result,BinaryPredicate op,command_queue & queue)80 inline OutputIterator unique_copy(InputIterator first,
81                                   InputIterator last,
82                                   OutputIterator result,
83                                   BinaryPredicate op,
84                                   command_queue &queue)
85 {
86     if(first == last){
87         return result;
88     }
89 
90     const context &context = queue.get_context();
91     size_t count = detail::iterator_range_size(first, last);
92 
93     // flags marking unique elements
94     vector<uint_> flags(count, context);
95 
96     // find each unique element and mark it with a one
97     transform(
98         first, last - 1, first + 1, flags.begin() + 1, not2(op), queue
99     );
100 
101     // first element is always unique
102     fill_n(flags.begin(), 1, 1, queue);
103 
104     // storage for desination indices
105     vector<uint_> indices(count, context);
106 
107     // copy indices for each unique element
108     vector<uint_>::iterator last_index = detail::copy_index_if(
109         flags.begin(), flags.end(), indices.begin(), lambda::_1 == 1, queue
110     );
111 
112     // copy unique values from input to output using the computed indices
113     gather(indices.begin(), last_index, first, result, queue);
114 
115     // return an iterator to the end of the unique output range
116     return result + std::distance(indices.begin(), last_index);
117 }
118 
119 } // end detail namespace
120 
121 /// Makes a copy of the range [first, last) and removes all consecutive
122 /// duplicate elements (determined by \p op) from the copy. If \p op is not
123 /// provided, the equality operator is used.
124 ///
125 /// \param first first element in the input range
126 /// \param last last element in the input range
127 /// \param result first element in the result range
128 /// \param op binary operator used to check for uniqueness
129 /// \param queue command queue to perform the operation
130 ///
131 /// \return \c OutputIterator to the end of the result range
132 ///
133 /// Space complexity: \Omega(4n)
134 ///
135 /// \see unique()
136 template<class InputIterator, class OutputIterator, class BinaryPredicate>
unique_copy(InputIterator first,InputIterator last,OutputIterator result,BinaryPredicate op,command_queue & queue=system::default_queue ())137 inline OutputIterator unique_copy(InputIterator first,
138                                   InputIterator last,
139                                   OutputIterator result,
140                                   BinaryPredicate op,
141                                   command_queue &queue = system::default_queue())
142 {
143     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
144     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
145 
146     size_t count = detail::iterator_range_size(first, last);
147     if(count < 32){
148         return detail::serial_unique_copy(first, last, result, op, queue);
149     }
150     else {
151         return detail::unique_copy(first, last, result, op, queue);
152     }
153 }
154 
155 /// \overload
156 template<class InputIterator, class OutputIterator>
unique_copy(InputIterator first,InputIterator last,OutputIterator result,command_queue & queue=system::default_queue ())157 inline OutputIterator unique_copy(InputIterator first,
158                                   InputIterator last,
159                                   OutputIterator result,
160                                   command_queue &queue = system::default_queue())
161 {
162     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
163     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
164 
165     typedef typename std::iterator_traits<InputIterator>::value_type value_type;
166 
167     return ::boost::compute::unique_copy(
168         first, last, result, ::boost::compute::equal_to<value_type>(), queue
169     );
170 }
171 
172 } // end compute namespace
173 } // end boost namespace
174 
175 #endif // BOOST_COMPUTE_ALGORITHM_UNIQUE_COPY_HPP
176