merge_path.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_ALGORITHM_DETAIL_MERGE_PATH_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_DETAIL_MERGE_PATH_HPP
  12. #include <iterator>
  13. #include <boost/compute/algorithm/find_if.hpp>
  14. #include <boost/compute/container/vector.hpp>
  15. #include <boost/compute/detail/iterator_range_size.hpp>
  16. #include <boost/compute/detail/meta_kernel.hpp>
  17. #include <boost/compute/lambda.hpp>
  18. #include <boost/compute/system.hpp>
  19. namespace boost {
  20. namespace compute {
  21. namespace detail {
  22. ///
  23. /// \brief Merge Path kernel class
  24. ///
  25. /// Subclass of meta_kernel to break two sets into tiles according
  26. /// to their merge path
  27. ///
  28. class merge_path_kernel : public meta_kernel
  29. {
  30. public:
  31. unsigned int tile_size;
  32. merge_path_kernel() : meta_kernel("merge_path")
  33. {
  34. tile_size = 4;
  35. }
  36. template<class InputIterator1, class InputIterator2,
  37. class OutputIterator1, class OutputIterator2,
  38. class Compare>
  39. void set_range(InputIterator1 first1,
  40. InputIterator1 last1,
  41. InputIterator2 first2,
  42. InputIterator2 last2,
  43. OutputIterator1 result_a,
  44. OutputIterator2 result_b,
  45. Compare comp)
  46. {
  47. m_a_count = iterator_range_size(first1, last1);
  48. m_a_count_arg = add_arg<uint_>("a_count");
  49. m_b_count = iterator_range_size(first2, last2);
  50. m_b_count_arg = add_arg<uint_>("b_count");
  51. *this <<
  52. "uint i = get_global_id(0);\n" <<
  53. "uint target = (i+1)*" << tile_size << ";\n" <<
  54. "uint start = max(convert_int(0),convert_int(target)-convert_int(b_count));\n" <<
  55. "uint end = min(target,a_count);\n" <<
  56. "uint a_index, b_index;\n" <<
  57. "while(start<end)\n" <<
  58. "{\n" <<
  59. " a_index = (start + end)/2;\n" <<
  60. " b_index = target - a_index - 1;\n" <<
  61. " if(!(" << comp(first2[expr<uint_>("b_index")],
  62. first1[expr<uint_>("a_index")]) << "))\n" <<
  63. " start = a_index + 1;\n" <<
  64. " else end = a_index;\n" <<
  65. "}\n" <<
  66. result_a[expr<uint_>("i")] << " = start;\n" <<
  67. result_b[expr<uint_>("i")] << " = target - start;\n";
  68. }
  69. template<class InputIterator1, class InputIterator2,
  70. class OutputIterator1, class OutputIterator2>
  71. void set_range(InputIterator1 first1,
  72. InputIterator1 last1,
  73. InputIterator2 first2,
  74. InputIterator2 last2,
  75. OutputIterator1 result_a,
  76. OutputIterator2 result_b)
  77. {
  78. typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
  79. ::boost::compute::less<value_type> less_than;
  80. set_range(first1, last1, first2, last2, result_a, result_b, less_than);
  81. }
  82. event exec(command_queue &queue)
  83. {
  84. if((m_a_count + m_b_count)/tile_size == 0) {
  85. return event();
  86. }
  87. set_arg(m_a_count_arg, uint_(m_a_count));
  88. set_arg(m_b_count_arg, uint_(m_b_count));
  89. return exec_1d(queue, 0, (m_a_count + m_b_count)/tile_size);
  90. }
  91. private:
  92. size_t m_a_count;
  93. size_t m_a_count_arg;
  94. size_t m_b_count;
  95. size_t m_b_count_arg;
  96. };
  97. } //end detail namespace
  98. } //end compute namespace
  99. } //end boost namespace
  100. #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_MERGE_PATH_HPP