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_SYMMETRIC_DIFFERENCE_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_SET_SYMMETRIC_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
0038 class serial_set_symmetric_difference_kernel : meta_kernel
0039 {
0040 public:
0041 unsigned int tile_size;
0042
0043 serial_set_symmetric_difference_kernel() : meta_kernel("set_symmetric_difference")
0044 {
0045 tile_size = 4;
0046 }
0047
0048 template<class InputIterator1, class InputIterator2,
0049 class InputIterator3, class InputIterator4,
0050 class OutputIterator1, class OutputIterator2>
0051 void set_range(InputIterator1 first1,
0052 InputIterator2 first2,
0053 InputIterator3 tile_first1,
0054 InputIterator3 tile_last1,
0055 InputIterator4 tile_first2,
0056 OutputIterator1 result,
0057 OutputIterator2 counts)
0058 {
0059 m_count = iterator_range_size(tile_first1, tile_last1) - 1;
0060
0061 *this <<
0062 "uint i = get_global_id(0);\n" <<
0063 "uint start1 = " << tile_first1[expr<uint_>("i")] << ";\n" <<
0064 "uint end1 = " << tile_first1[expr<uint_>("i+1")] << ";\n" <<
0065 "uint start2 = " << tile_first2[expr<uint_>("i")] << ";\n" <<
0066 "uint end2 = " << tile_first2[expr<uint_>("i+1")] << ";\n" <<
0067 "uint index = i*" << tile_size << ";\n" <<
0068 "uint count = 0;\n" <<
0069 "while(start1<end1 && start2<end2)\n" <<
0070 "{\n" <<
0071 " if(" << first1[expr<uint_>("start1")] << " == " <<
0072 first2[expr<uint_>("start2")] << ")\n" <<
0073 " {\n" <<
0074 " start1++; start2++;\n" <<
0075 " }\n" <<
0076 " else if(" << first1[expr<uint_>("start1")] << " < " <<
0077 first2[expr<uint_>("start2")] << ")\n" <<
0078 " {\n" <<
0079 result[expr<uint_>("index")] <<
0080 " = " << first1[expr<uint_>("start1")] << ";\n" <<
0081 " index++; count++;\n" <<
0082 " start1++;\n" <<
0083 " }\n" <<
0084 " else\n" <<
0085 " {\n" <<
0086 result[expr<uint_>("index")] <<
0087 " = " << first2[expr<uint_>("start2")] << ";\n" <<
0088 " index++; count++;\n" <<
0089 " start2++;\n" <<
0090 " }\n" <<
0091 "}\n" <<
0092 "while(start1<end1)\n" <<
0093 "{\n" <<
0094 result[expr<uint_>("index")] <<
0095 " = " << first1[expr<uint_>("start1")] << ";\n" <<
0096 " index++; count++;\n" <<
0097 " start1++;\n" <<
0098 "}\n" <<
0099 "while(start2<end2)\n" <<
0100 "{\n" <<
0101 result[expr<uint_>("index")] <<
0102 " = " << first2[expr<uint_>("start2")] << ";\n" <<
0103 " index++; count++;\n" <<
0104 " start2++;\n" <<
0105 "}\n" <<
0106 counts[expr<uint_>("i")] << " = count;\n";
0107 }
0108
0109 event exec(command_queue &queue)
0110 {
0111 if(m_count == 0) {
0112 return event();
0113 }
0114
0115 return exec_1d(queue, 0, m_count);
0116 }
0117
0118 private:
0119 size_t m_count;
0120 };
0121
0122 }
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141 template<class InputIterator1, class InputIterator2, class OutputIterator>
0142 inline OutputIterator
0143 set_symmetric_difference(InputIterator1 first1,
0144 InputIterator1 last1,
0145 InputIterator2 first2,
0146 InputIterator2 last2,
0147 OutputIterator result,
0148 command_queue &queue = system::default_queue())
0149 {
0150 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
0151 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
0152 BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
0153
0154 typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
0155
0156 int tile_size = 1024;
0157
0158 int count1 = detail::iterator_range_size(first1, last1);
0159 int count2 = detail::iterator_range_size(first2, last2);
0160
0161 vector<uint_> tile_a((count1+count2+tile_size-1)/tile_size+1, queue.get_context());
0162 vector<uint_> tile_b((count1+count2+tile_size-1)/tile_size+1, queue.get_context());
0163
0164
0165 detail::balanced_path_kernel tiling_kernel;
0166 tiling_kernel.tile_size = tile_size;
0167 tiling_kernel.set_range(first1, last1, first2, last2,
0168 tile_a.begin()+1, tile_b.begin()+1);
0169 fill_n(tile_a.begin(), 1, 0, queue);
0170 fill_n(tile_b.begin(), 1, 0, queue);
0171 tiling_kernel.exec(queue);
0172
0173 fill_n(tile_a.end()-1, 1, count1, queue);
0174 fill_n(tile_b.end()-1, 1, count2, queue);
0175
0176 vector<value_type> temp_result(count1+count2, queue.get_context());
0177 vector<uint_> counts((count1+count2+tile_size-1)/tile_size + 1, queue.get_context());
0178 fill_n(counts.end()-1, 1, 0, queue);
0179
0180
0181 detail::serial_set_symmetric_difference_kernel symmetric_difference_kernel;
0182 symmetric_difference_kernel.tile_size = tile_size;
0183 symmetric_difference_kernel.set_range(first1, first2, tile_a.begin(),
0184 tile_a.end(), tile_b.begin(),
0185 temp_result.begin(), counts.begin());
0186
0187 symmetric_difference_kernel.exec(queue);
0188
0189 exclusive_scan(counts.begin(), counts.end(), counts.begin(), queue);
0190
0191
0192 detail::compact_kernel compact_kernel;
0193 compact_kernel.tile_size = tile_size;
0194 compact_kernel.set_range(temp_result.begin(), counts.begin(), counts.end(), result);
0195
0196 compact_kernel.exec(queue);
0197
0198 return result + (counts.end() - 1).read(queue);
0199 }
0200
0201 }
0202 }
0203
0204 #endif