File indexing completed on 2025-01-18 09:29:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_COMPUTE_ALGORITHM_DETAIL_MERGE_PATH_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_DETAIL_MERGE_PATH_HPP
0013
0014 #include <iterator>
0015
0016 #include <boost/compute/algorithm/find_if.hpp>
0017 #include <boost/compute/container/vector.hpp>
0018 #include <boost/compute/detail/iterator_range_size.hpp>
0019 #include <boost/compute/detail/meta_kernel.hpp>
0020 #include <boost/compute/lambda.hpp>
0021 #include <boost/compute/system.hpp>
0022
0023 namespace boost {
0024 namespace compute {
0025 namespace detail {
0026
0027
0028
0029
0030
0031
0032
0033 class merge_path_kernel : public meta_kernel
0034 {
0035 public:
0036 unsigned int tile_size;
0037
0038 merge_path_kernel() : meta_kernel("merge_path")
0039 {
0040 tile_size = 4;
0041 }
0042
0043 template<class InputIterator1, class InputIterator2,
0044 class OutputIterator1, class OutputIterator2,
0045 class Compare>
0046 void set_range(InputIterator1 first1,
0047 InputIterator1 last1,
0048 InputIterator2 first2,
0049 InputIterator2 last2,
0050 OutputIterator1 result_a,
0051 OutputIterator2 result_b,
0052 Compare comp)
0053 {
0054 m_a_count = iterator_range_size(first1, last1);
0055 m_a_count_arg = add_arg<uint_>("a_count");
0056
0057 m_b_count = iterator_range_size(first2, last2);
0058 m_b_count_arg = add_arg<uint_>("b_count");
0059
0060 *this <<
0061 "uint i = get_global_id(0);\n" <<
0062 "uint target = (i+1)*" << tile_size << ";\n" <<
0063 "uint start = max(convert_int(0),convert_int(target)-convert_int(b_count));\n" <<
0064 "uint end = min(target,a_count);\n" <<
0065 "uint a_index, b_index;\n" <<
0066 "while(start<end)\n" <<
0067 "{\n" <<
0068 " a_index = (start + end)/2;\n" <<
0069 " b_index = target - a_index - 1;\n" <<
0070 " if(!(" << comp(first2[expr<uint_>("b_index")],
0071 first1[expr<uint_>("a_index")]) << "))\n" <<
0072 " start = a_index + 1;\n" <<
0073 " else end = a_index;\n" <<
0074 "}\n" <<
0075 result_a[expr<uint_>("i")] << " = start;\n" <<
0076 result_b[expr<uint_>("i")] << " = target - start;\n";
0077 }
0078
0079 template<class InputIterator1, class InputIterator2,
0080 class OutputIterator1, class OutputIterator2>
0081 void set_range(InputIterator1 first1,
0082 InputIterator1 last1,
0083 InputIterator2 first2,
0084 InputIterator2 last2,
0085 OutputIterator1 result_a,
0086 OutputIterator2 result_b)
0087 {
0088 typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
0089 ::boost::compute::less<value_type> less_than;
0090 set_range(first1, last1, first2, last2, result_a, result_b, less_than);
0091 }
0092
0093 event exec(command_queue &queue)
0094 {
0095 if((m_a_count + m_b_count)/tile_size == 0) {
0096 return event();
0097 }
0098
0099 set_arg(m_a_count_arg, uint_(m_a_count));
0100 set_arg(m_b_count_arg, uint_(m_b_count));
0101
0102 return exec_1d(queue, 0, (m_a_count + m_b_count)/tile_size);
0103 }
0104
0105 private:
0106 size_t m_a_count;
0107 size_t m_a_count_arg;
0108 size_t m_b_count;
0109 size_t m_b_count_arg;
0110 };
0111
0112 }
0113 }
0114 }
0115
0116 #endif