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