File indexing completed on 2025-01-18 09:29:57
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_COMPUTE_ALGORITHM_SET_INTERSECTION_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_SET_INTERSECTION_HPP
0013
0014 #include <iterator>
0015
0016 #include <boost/static_assert.hpp>
0017
0018 #include <boost/compute/algorithm/detail/compact.hpp>
0019 #include <boost/compute/algorithm/detail/balanced_path.hpp>
0020 #include <boost/compute/algorithm/exclusive_scan.hpp>
0021 #include <boost/compute/algorithm/fill_n.hpp>
0022 #include <boost/compute/container/vector.hpp>
0023 #include <boost/compute/detail/iterator_range_size.hpp>
0024 #include <boost/compute/detail/meta_kernel.hpp>
0025 #include <boost/compute/system.hpp>
0026 #include <boost/compute/type_traits/is_device_iterator.hpp>
0027
0028 namespace boost {
0029 namespace compute {
0030 namespace detail {
0031
0032
0033
0034
0035
0036
0037 class serial_set_intersection_kernel : meta_kernel
0038 {
0039 public:
0040 unsigned int tile_size;
0041
0042 serial_set_intersection_kernel() : meta_kernel("set_intersection")
0043 {
0044 tile_size = 4;
0045 }
0046
0047 template<class InputIterator1, class InputIterator2,
0048 class InputIterator3, class InputIterator4,
0049 class OutputIterator1, class OutputIterator2>
0050 void set_range(InputIterator1 first1,
0051 InputIterator2 first2,
0052 InputIterator3 tile_first1,
0053 InputIterator3 tile_last1,
0054 InputIterator4 tile_first2,
0055 OutputIterator1 result,
0056 OutputIterator2 counts)
0057 {
0058 m_count = iterator_range_size(tile_first1, tile_last1) - 1;
0059
0060 *this <<
0061 "uint i = get_global_id(0);\n" <<
0062 "uint start1 = " << tile_first1[expr<uint_>("i")] << ";\n" <<
0063 "uint end1 = " << tile_first1[expr<uint_>("i+1")] << ";\n" <<
0064 "uint start2 = " << tile_first2[expr<uint_>("i")] << ";\n" <<
0065 "uint end2 = " << tile_first2[expr<uint_>("i+1")] << ";\n" <<
0066 "uint index = i*" << tile_size << ";\n" <<
0067 "uint count = 0;\n" <<
0068 "while(start1<end1 && start2<end2)\n" <<
0069 "{\n" <<
0070 " if(" << first1[expr<uint_>("start1")] << " == " <<
0071 first2[expr<uint_>("start2")] << ")\n" <<
0072 " {\n" <<
0073 result[expr<uint_>("index")] <<
0074 " = " << first1[expr<uint_>("start1")] << ";\n" <<
0075 " index++; count++;\n" <<
0076 " start1++; start2++;\n" <<
0077 " }\n" <<
0078 " else if(" << first1[expr<uint_>("start1")] << " < " <<
0079 first2[expr<uint_>("start2")] << ")\n" <<
0080 " start1++;\n" <<
0081 " else start2++;\n" <<
0082 "}\n" <<
0083 counts[expr<uint_>("i")] << " = count;\n";
0084 }
0085
0086 event exec(command_queue &queue)
0087 {
0088 if(m_count == 0) {
0089 return event();
0090 }
0091
0092 return exec_1d(queue, 0, m_count);
0093 }
0094
0095 private:
0096 size_t m_count;
0097 };
0098
0099 }
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 template<class InputIterator1, class InputIterator2, class OutputIterator>
0119 inline OutputIterator set_intersection(InputIterator1 first1,
0120 InputIterator1 last1,
0121 InputIterator2 first2,
0122 InputIterator2 last2,
0123 OutputIterator result,
0124 command_queue &queue = system::default_queue())
0125 {
0126 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
0127 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
0128 BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
0129
0130 typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
0131
0132 int tile_size = 1024;
0133
0134 int count1 = detail::iterator_range_size(first1, last1);
0135 int count2 = detail::iterator_range_size(first2, last2);
0136
0137 vector<uint_> tile_a((count1+count2+tile_size-1)/tile_size+1, queue.get_context());
0138 vector<uint_> tile_b((count1+count2+tile_size-1)/tile_size+1, queue.get_context());
0139
0140
0141 detail::balanced_path_kernel tiling_kernel;
0142 tiling_kernel.tile_size = tile_size;
0143 tiling_kernel.set_range(first1, last1, first2, last2,
0144 tile_a.begin()+1, tile_b.begin()+1);
0145 fill_n(tile_a.begin(), 1, 0, queue);
0146 fill_n(tile_b.begin(), 1, 0, queue);
0147 tiling_kernel.exec(queue);
0148
0149 fill_n(tile_a.end()-1, 1, count1, queue);
0150 fill_n(tile_b.end()-1, 1, count2, queue);
0151
0152 vector<value_type> temp_result(count1+count2, queue.get_context());
0153 vector<uint_> counts((count1+count2+tile_size-1)/tile_size + 1, queue.get_context());
0154 fill_n(counts.end()-1, 1, 0, queue);
0155
0156
0157 detail::serial_set_intersection_kernel intersection_kernel;
0158 intersection_kernel.tile_size = tile_size;
0159 intersection_kernel.set_range(first1, first2, tile_a.begin(), tile_a.end(),
0160 tile_b.begin(), temp_result.begin(), counts.begin());
0161
0162 intersection_kernel.exec(queue);
0163
0164 exclusive_scan(counts.begin(), counts.end(), counts.begin(), queue);
0165
0166
0167 detail::compact_kernel compact_kernel;
0168 compact_kernel.tile_size = tile_size;
0169 compact_kernel.set_range(temp_result.begin(), counts.begin(), counts.end(), result);
0170
0171 compact_kernel.exec(queue);
0172
0173 return result + (counts.end() - 1).read(queue);
0174 }
0175
0176 }
0177 }
0178
0179 #endif