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_DETAIL_SEARCH_N_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_DETAIL_SEARCH_N_HPP
0013
0014 #include <iterator>
0015
0016 #include <boost/static_assert.hpp>
0017
0018 #include <boost/compute/algorithm/find.hpp>
0019 #include <boost/compute/container/vector.hpp>
0020 #include <boost/compute/detail/iterator_range_size.hpp>
0021 #include <boost/compute/detail/meta_kernel.hpp>
0022 #include <boost/compute/system.hpp>
0023 #include <boost/compute/type_traits/is_device_iterator.hpp>
0024
0025 namespace boost {
0026 namespace compute {
0027 namespace detail {
0028
0029
0030
0031
0032
0033
0034 template<class TextIterator, class OutputIterator>
0035 class search_n_kernel : public meta_kernel
0036 {
0037 public:
0038 typedef typename std::iterator_traits<TextIterator>::value_type value_type;
0039
0040 search_n_kernel() : meta_kernel("search_n")
0041 {}
0042
0043 void set_range(TextIterator t_first,
0044 TextIterator t_last,
0045 value_type value,
0046 size_t n,
0047 OutputIterator result)
0048 {
0049 m_n = n;
0050 m_n_arg = add_arg<uint_>("n");
0051
0052 m_value = value;
0053 m_value_arg = add_arg<value_type>("value");
0054
0055 m_count = iterator_range_size(t_first, t_last);
0056 m_count = m_count + 1 - m_n;
0057
0058 *this <<
0059 "uint i = get_global_id(0);\n" <<
0060 "uint i1 = i;\n" <<
0061 "uint j;\n" <<
0062 "for(j = 0; j<n; j++,i++)\n" <<
0063 "{\n" <<
0064 " if(value != " << t_first[expr<uint_>("i")] << ")\n" <<
0065 " j = n + 1;\n" <<
0066 "}\n" <<
0067 "if(j == n)\n" <<
0068 result[expr<uint_>("i1")] << " = 1;\n" <<
0069 "else\n" <<
0070 result[expr<uint_>("i1")] << " = 0;\n";
0071 }
0072
0073 event exec(command_queue &queue)
0074 {
0075 if(m_count == 0) {
0076 return event();
0077 }
0078
0079 set_arg(m_n_arg, uint_(m_n));
0080 set_arg(m_value_arg, m_value);
0081
0082 return exec_1d(queue, 0, m_count);
0083 }
0084
0085 private:
0086 size_t m_n;
0087 size_t m_n_arg;
0088 size_t m_count;
0089 value_type m_value;
0090 size_t m_value_arg;
0091 };
0092
0093 }
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109 template<class TextIterator, class ValueType>
0110 inline TextIterator search_n(TextIterator t_first,
0111 TextIterator t_last,
0112 size_t n,
0113 ValueType value,
0114 command_queue &queue = system::default_queue())
0115 {
0116 BOOST_STATIC_ASSERT(is_device_iterator<TextIterator>::value);
0117
0118
0119 vector<uint_> matching_indices(
0120 detail::iterator_range_size(t_first, t_last) + 1 - n,
0121 queue.get_context()
0122 );
0123
0124
0125
0126 detail::search_n_kernel<TextIterator,
0127 vector<uint_>::iterator> kernel;
0128
0129 kernel.set_range(t_first, t_last, value, n, matching_indices.begin());
0130 kernel.exec(queue);
0131
0132 vector<uint_>::iterator index = ::boost::compute::find(
0133 matching_indices.begin(), matching_indices.end(), uint_(1), queue
0134 );
0135
0136
0137 if(index == matching_indices.end())
0138 return t_last;
0139
0140 return t_first + detail::iterator_range_size(matching_indices.begin(), index);
0141 }
0142
0143 }
0144 }
0145
0146 #endif