]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/compute/algorithm/set_difference.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / compute / algorithm / set_difference.hpp
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_SET_DIFFERENCE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_SET_DIFFERENCE_HPP
13
14 #include <iterator>
15
16 #include <boost/compute/algorithm/detail/compact.hpp>
17 #include <boost/compute/algorithm/detail/balanced_path.hpp>
18 #include <boost/compute/algorithm/exclusive_scan.hpp>
19 #include <boost/compute/algorithm/fill_n.hpp>
20 #include <boost/compute/container/vector.hpp>
21 #include <boost/compute/detail/iterator_range_size.hpp>
22 #include <boost/compute/detail/meta_kernel.hpp>
23 #include <boost/compute/system.hpp>
24
25 namespace boost {
26 namespace compute {
27 namespace detail {
28
29 ///
30 /// \brief Serial set difference kernel class
31 ///
32 /// Subclass of meta_kernel to perform serial set difference after tiling
33 ///
34 class serial_set_difference_kernel : meta_kernel
35 {
36 public:
37 unsigned int tile_size;
38
39 serial_set_difference_kernel() : meta_kernel("set_difference")
40 {
41 tile_size = 4;
42 }
43
44 template<class InputIterator1, class InputIterator2,
45 class InputIterator3, class InputIterator4,
46 class OutputIterator1, class OutputIterator2>
47 void set_range(InputIterator1 first1,
48 InputIterator2 first2,
49 InputIterator3 tile_first1,
50 InputIterator3 tile_last1,
51 InputIterator4 tile_first2,
52 OutputIterator1 result,
53 OutputIterator2 counts)
54 {
55 m_count = iterator_range_size(tile_first1, tile_last1) - 1;
56
57 *this <<
58 "uint i = get_global_id(0);\n" <<
59 "uint start1 = " << tile_first1[expr<uint_>("i")] << ";\n" <<
60 "uint end1 = " << tile_first1[expr<uint_>("i+1")] << ";\n" <<
61 "uint start2 = " << tile_first2[expr<uint_>("i")] << ";\n" <<
62 "uint end2 = " << tile_first2[expr<uint_>("i+1")] << ";\n" <<
63 "uint index = i*" << tile_size << ";\n" <<
64 "uint count = 0;\n" <<
65 "while(start1<end1 && start2<end2)\n" <<
66 "{\n" <<
67 " if(" << first1[expr<uint_>("start1")] << " == " <<
68 first2[expr<uint_>("start2")] << ")\n" <<
69 " {\n" <<
70 " start1++; start2++;\n" <<
71 " }\n" <<
72 " else if(" << first1[expr<uint_>("start1")] << " < " <<
73 first2[expr<uint_>("start2")] << ")\n" <<
74 " {\n" <<
75 result[expr<uint_>("index")] <<
76 " = " << first1[expr<uint_>("start1")] << ";\n" <<
77 " index++; count++;\n" <<
78 " start1++;\n" <<
79 " }\n" <<
80 " else\n" <<
81 " {\n" <<
82 " start2++;\n" <<
83 " }\n" <<
84 "}\n" <<
85 "while(start1<end1)\n" <<
86 "{\n" <<
87 result[expr<uint_>("index")] <<
88 " = " << first1[expr<uint_>("start1")] << ";\n" <<
89 " index++; count++;\n" <<
90 " start1++;\n" <<
91 "}\n" <<
92 counts[expr<uint_>("i")] << " = count;\n";
93 }
94
95 event exec(command_queue &queue)
96 {
97 if(m_count == 0) {
98 return event();
99 }
100
101 return exec_1d(queue, 0, m_count);
102 }
103
104 private:
105 size_t m_count;
106 };
107
108 } //end detail namespace
109
110 ///
111 /// \brief Set difference algorithm
112 ///
113 /// Finds the difference of the sorted range [first2, last2) from the sorted
114 /// range [first1, last1) and stores it in range starting at result
115 /// \return Iterator pointing to end of difference
116 ///
117 /// \param first1 Iterator pointing to start of first set
118 /// \param last1 Iterator pointing to end of first set
119 /// \param first2 Iterator pointing to start of second set
120 /// \param last2 Iterator pointing to end of second set
121 /// \param result Iterator pointing to start of range in which the difference
122 /// will be stored
123 /// \param queue Queue on which to execute
124 ///
125 /// Space complexity:
126 /// \Omega(2(distance(\p first1, \p last1) + distance(\p first2, \p last2)))
127 template<class InputIterator1, class InputIterator2, class OutputIterator>
128 inline OutputIterator set_difference(InputIterator1 first1,
129 InputIterator1 last1,
130 InputIterator2 first2,
131 InputIterator2 last2,
132 OutputIterator result,
133 command_queue &queue = system::default_queue())
134 {
135 typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
136
137 int tile_size = 1024;
138
139 int count1 = detail::iterator_range_size(first1, last1);
140 int count2 = detail::iterator_range_size(first2, last2);
141
142 vector<uint_> tile_a((count1+count2+tile_size-1)/tile_size+1, queue.get_context());
143 vector<uint_> tile_b((count1+count2+tile_size-1)/tile_size+1, queue.get_context());
144
145 // Tile the sets
146 detail::balanced_path_kernel tiling_kernel;
147 tiling_kernel.tile_size = tile_size;
148 tiling_kernel.set_range(first1, last1, first2, last2,
149 tile_a.begin()+1, tile_b.begin()+1);
150 fill_n(tile_a.begin(), 1, 0, queue);
151 fill_n(tile_b.begin(), 1, 0, queue);
152 tiling_kernel.exec(queue);
153
154 fill_n(tile_a.end()-1, 1, count1, queue);
155 fill_n(tile_b.end()-1, 1, count2, queue);
156
157 vector<value_type> temp_result(count1+count2, queue.get_context());
158 vector<uint_> counts((count1+count2+tile_size-1)/tile_size + 1, queue.get_context());
159 fill_n(counts.end()-1, 1, 0, queue);
160
161 // Find individual differences
162 detail::serial_set_difference_kernel difference_kernel;
163 difference_kernel.tile_size = tile_size;
164 difference_kernel.set_range(first1, first2, tile_a.begin(), tile_a.end(),
165 tile_b.begin(), temp_result.begin(), counts.begin());
166
167 difference_kernel.exec(queue);
168
169 exclusive_scan(counts.begin(), counts.end(), counts.begin(), queue);
170
171 // Compact the results
172 detail::compact_kernel compact_kernel;
173 compact_kernel.tile_size = tile_size;
174 compact_kernel.set_range(temp_result.begin(), counts.begin(), counts.end(), result);
175
176 compact_kernel.exec(queue);
177
178 return result + (counts.end() - 1).read(queue);
179 }
180
181 } //end compute namespace
182 } //end boost namespace
183
184 #endif // BOOST_COMPUTE_ALGORITHM_SET_DIFFERENCE_HPP