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_DIFFERENCE_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_SET_DIFFERENCE_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_difference_kernel : meta_kernel
0038 {
0039 public:
0040 unsigned int tile_size;
0041
0042 serial_set_difference_kernel() : meta_kernel("set_difference")
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 " start1++; start2++;\n" <<
0074 " }\n" <<
0075 " else if(" << first1[expr<uint_>("start1")] << " < " <<
0076 first2[expr<uint_>("start2")] << ")\n" <<
0077 " {\n" <<
0078 result[expr<uint_>("index")] <<
0079 " = " << first1[expr<uint_>("start1")] << ";\n" <<
0080 " index++; count++;\n" <<
0081 " start1++;\n" <<
0082 " }\n" <<
0083 " else\n" <<
0084 " {\n" <<
0085 " start2++;\n" <<
0086 " }\n" <<
0087 "}\n" <<
0088 "while(start1<end1)\n" <<
0089 "{\n" <<
0090 result[expr<uint_>("index")] <<
0091 " = " << first1[expr<uint_>("start1")] << ";\n" <<
0092 " index++; count++;\n" <<
0093 " start1++;\n" <<
0094 "}\n" <<
0095 counts[expr<uint_>("i")] << " = count;\n";
0096 }
0097
0098 event exec(command_queue &queue)
0099 {
0100 if(m_count == 0) {
0101 return event();
0102 }
0103
0104 return exec_1d(queue, 0, m_count);
0105 }
0106
0107 private:
0108 size_t m_count;
0109 };
0110
0111 }
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130 template<class InputIterator1, class InputIterator2, class OutputIterator>
0131 inline OutputIterator set_difference(InputIterator1 first1,
0132 InputIterator1 last1,
0133 InputIterator2 first2,
0134 InputIterator2 last2,
0135 OutputIterator result,
0136 command_queue &queue = system::default_queue())
0137 {
0138 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
0139 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
0140 BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
0141
0142 typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
0143
0144 int tile_size = 1024;
0145
0146 int count1 = detail::iterator_range_size(first1, last1);
0147 int count2 = detail::iterator_range_size(first2, last2);
0148
0149 vector<uint_> tile_a((count1+count2+tile_size-1)/tile_size+1, queue.get_context());
0150 vector<uint_> tile_b((count1+count2+tile_size-1)/tile_size+1, queue.get_context());
0151
0152
0153 detail::balanced_path_kernel tiling_kernel;
0154 tiling_kernel.tile_size = tile_size;
0155 tiling_kernel.set_range(first1, last1, first2, last2,
0156 tile_a.begin()+1, tile_b.begin()+1);
0157 fill_n(tile_a.begin(), 1, 0, queue);
0158 fill_n(tile_b.begin(), 1, 0, queue);
0159 tiling_kernel.exec(queue);
0160
0161 fill_n(tile_a.end()-1, 1, count1, queue);
0162 fill_n(tile_b.end()-1, 1, count2, queue);
0163
0164 vector<value_type> temp_result(count1+count2, queue.get_context());
0165 vector<uint_> counts((count1+count2+tile_size-1)/tile_size + 1, queue.get_context());
0166 fill_n(counts.end()-1, 1, 0, queue);
0167
0168
0169 detail::serial_set_difference_kernel difference_kernel;
0170 difference_kernel.tile_size = tile_size;
0171 difference_kernel.set_range(first1, first2, tile_a.begin(), tile_a.end(),
0172 tile_b.begin(), temp_result.begin(), counts.begin());
0173
0174 difference_kernel.exec(queue);
0175
0176 exclusive_scan(counts.begin(), counts.end(), counts.begin(), queue);
0177
0178
0179 detail::compact_kernel compact_kernel;
0180 compact_kernel.tile_size = tile_size;
0181 compact_kernel.set_range(temp_result.begin(), counts.begin(), counts.end(), result);
0182
0183 compact_kernel.exec(queue);
0184
0185 return result + (counts.end() - 1).read(queue);
0186 }
0187
0188 }
0189 }
0190
0191 #endif