1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2014 Roshan <thisisroshansmail@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_SET_UNION_HPP
12 #define BOOST_COMPUTE_ALGORITHM_SET_UNION_HPP
16 #include <boost/static_assert.hpp>
18 #include <boost/compute/algorithm/detail/balanced_path.hpp>
19 #include <boost/compute/algorithm/detail/compact.hpp>
20 #include <boost/compute/algorithm/exclusive_scan.hpp>
21 #include <boost/compute/algorithm/fill_n.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/system.hpp>
26 #include <boost/compute/type_traits/is_device_iterator.hpp>
33 /// \brief Serial set union kernel class
35 /// Subclass of meta_kernel to perform serial set union after tiling
37 class serial_set_union_kernel : meta_kernel
40 unsigned int tile_size;
42 serial_set_union_kernel() : meta_kernel("set_union")
47 template<class InputIterator1, class InputIterator2,
48 class InputIterator3, class InputIterator4,
49 class OutputIterator1, class OutputIterator2>
50 void set_range(InputIterator1 first1,
51 InputIterator2 first2,
52 InputIterator3 tile_first1,
53 InputIterator3 tile_last1,
54 InputIterator4 tile_first2,
55 OutputIterator1 result,
56 OutputIterator2 counts)
58 m_count = iterator_range_size(tile_first1, tile_last1) - 1;
61 "uint i = get_global_id(0);\n" <<
62 "uint start1 = " << tile_first1[expr<uint_>("i")] << ";\n" <<
63 "uint end1 = " << tile_first1[expr<uint_>("i+1")] << ";\n" <<
64 "uint start2 = " << tile_first2[expr<uint_>("i")] << ";\n" <<
65 "uint end2 = " << tile_first2[expr<uint_>("i+1")] << ";\n" <<
66 "uint index = i*" << tile_size << ";\n" <<
67 "uint count = 0;\n" <<
68 "while(start1<end1 && start2<end2)\n" <<
70 " if(" << first1[expr<uint_>("start1")] << " == " <<
71 first2[expr<uint_>("start2")] << ")\n" <<
73 result[expr<uint_>("index")] <<
74 " = " << first1[expr<uint_>("start1")] << ";\n" <<
75 " index++; count++;\n" <<
76 " start1++; start2++;\n" <<
78 " else if(" << first1[expr<uint_>("start1")] << " < " <<
79 first2[expr<uint_>("start2")] << ")\n" <<
81 result[expr<uint_>("index")] <<
82 " = " << first1[expr<uint_>("start1")] << ";\n" <<
83 " index++; count++;\n" <<
88 result[expr<uint_>("index")] <<
89 " = " << first2[expr<uint_>("start2")] << ";\n" <<
90 " index++; count++;\n" <<
94 "while(start1<end1)\n" <<
96 result[expr<uint_>("index")] <<
97 " = " << first1[expr<uint_>("start1")] << ";\n" <<
98 " index++; count++;\n" <<
101 "while(start2<end2)\n" <<
103 result[expr<uint_>("index")] <<
104 " = " << first2[expr<uint_>("start2")] << ";\n" <<
105 " index++; count++;\n" <<
108 counts[expr<uint_>("i")] << " = count;\n";
111 event exec(command_queue &queue)
117 return exec_1d(queue, 0, m_count);
124 } //end detail namespace
127 /// \brief Set union algorithm
129 /// Finds the union of the sorted range [first1, last1) with the sorted
130 /// range [first2, last2) and stores it in range starting at result
131 /// \return Iterator pointing to end of union
133 /// \param first1 Iterator pointing to start of first set
134 /// \param last1 Iterator pointing to end of first set
135 /// \param first2 Iterator pointing to start of second set
136 /// \param last2 Iterator pointing to end of second set
137 /// \param result Iterator pointing to start of range in which the union
139 /// \param queue Queue on which to execute
141 /// Space complexity:
142 /// \Omega(2(distance(\p first1, \p last1) + distance(\p first2, \p last2)))
143 template<class InputIterator1, class InputIterator2, class OutputIterator>
144 inline OutputIterator set_union(InputIterator1 first1,
145 InputIterator1 last1,
146 InputIterator2 first2,
147 InputIterator2 last2,
148 OutputIterator result,
149 command_queue &queue = system::default_queue())
151 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
152 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
153 BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
155 typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
157 int tile_size = 1024;
159 int count1 = detail::iterator_range_size(first1, last1);
160 int count2 = detail::iterator_range_size(first2, last2);
162 vector<uint_> tile_a((count1+count2+tile_size-1)/tile_size+1, queue.get_context());
163 vector<uint_> tile_b((count1+count2+tile_size-1)/tile_size+1, queue.get_context());
166 detail::balanced_path_kernel tiling_kernel;
167 tiling_kernel.tile_size = tile_size;
168 tiling_kernel.set_range(first1, last1, first2, last2,
169 tile_a.begin()+1, tile_b.begin()+1);
170 fill_n(tile_a.begin(), 1, 0, queue);
171 fill_n(tile_b.begin(), 1, 0, queue);
172 tiling_kernel.exec(queue);
174 fill_n(tile_a.end()-1, 1, count1, queue);
175 fill_n(tile_b.end()-1, 1, count2, queue);
177 vector<value_type> temp_result(count1+count2, queue.get_context());
178 vector<uint_> counts((count1+count2+tile_size-1)/tile_size + 1, queue.get_context());
179 fill_n(counts.end()-1, 1, 0, queue);
181 // Find individual unions
182 detail::serial_set_union_kernel union_kernel;
183 union_kernel.tile_size = tile_size;
184 union_kernel.set_range(first1, first2, tile_a.begin(), tile_a.end(),
185 tile_b.begin(), temp_result.begin(), counts.begin());
187 union_kernel.exec(queue);
189 exclusive_scan(counts.begin(), counts.end(), counts.begin(), queue);
191 // Compact the results
192 detail::compact_kernel compact_kernel;
193 compact_kernel.tile_size = tile_size;
194 compact_kernel.set_range(temp_result.begin(), counts.begin(), counts.end(), result);
196 compact_kernel.exec(queue);
198 return result + (counts.end() - 1).read(queue);
201 } //end compute namespace
202 } //end boost namespace
204 #endif // BOOST_COMPUTE_ALGORITHM_SET_UNION_HPP