balanced_path.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_BALANCED_PATH_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_DETAIL_BALANCED_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 Balanced Path kernel class
  24. ///
  25. /// Subclass of meta_kernel to break two sets into tiles according
  26. /// to their balanced path.
  27. ///
  28. class balanced_path_kernel : public meta_kernel
  29. {
  30. public:
  31. unsigned int tile_size;
  32. balanced_path_kernel() : meta_kernel("balanced_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. typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
  48. m_a_count = iterator_range_size(first1, last1);
  49. m_a_count_arg = add_arg<uint_>("a_count");
  50. m_b_count = iterator_range_size(first2, last2);
  51. m_b_count_arg = add_arg<uint_>("b_count");
  52. *this <<
  53. "uint i = get_global_id(0);\n" <<
  54. "uint target = (i+1)*" << tile_size << ";\n" <<
  55. "uint start = max(convert_int(0),convert_int(target)-convert_int(b_count));\n" <<
  56. "uint end = min(target,a_count);\n" <<
  57. "uint a_index, b_index;\n" <<
  58. "while(start<end)\n" <<
  59. "{\n" <<
  60. " a_index = (start + end)/2;\n" <<
  61. " b_index = target - a_index - 1;\n" <<
  62. " if(!(" << comp(first2[expr<uint_>("b_index")],
  63. first1[expr<uint_>("a_index")]) << "))\n" <<
  64. " start = a_index + 1;\n" <<
  65. " else end = a_index;\n" <<
  66. "}\n" <<
  67. "a_index = start;\n" <<
  68. "b_index = target - start;\n" <<
  69. "if(b_index < b_count)\n" <<
  70. "{\n" <<
  71. " " << decl<const value_type>("x") << " = " <<
  72. first2[expr<uint_>("b_index")] << ";\n" <<
  73. " uint a_start = 0, a_end = a_index, a_mid;\n" <<
  74. " uint b_start = 0, b_end = b_index, b_mid;\n" <<
  75. " while(a_start<a_end)\n" <<
  76. " {\n" <<
  77. " a_mid = (a_start + a_end)/2;\n" <<
  78. " if(" << comp(first1[expr<uint_>("a_mid")], expr<value_type>("x")) << ")\n" <<
  79. " a_start = a_mid+1;\n" <<
  80. " else a_end = a_mid;\n" <<
  81. " }\n" <<
  82. " while(b_start<b_end)\n" <<
  83. " {\n" <<
  84. " b_mid = (b_start + b_end)/2;\n" <<
  85. " if(" << comp(first2[expr<uint_>("b_mid")], expr<value_type>("x")) << ")\n" <<
  86. " b_start = b_mid+1;\n" <<
  87. " else b_end = b_mid;\n" <<
  88. " }\n" <<
  89. " uint a_run = a_index - a_start;\n" <<
  90. " uint b_run = b_index - b_start;\n" <<
  91. " uint x_count = a_run + b_run;\n" <<
  92. " uint b_advance = max(x_count / 2, x_count - a_run);\n" <<
  93. " b_end = min(b_count, b_start + b_advance + 1);\n" <<
  94. " uint temp_start = b_index, temp_end = b_end, temp_mid;" <<
  95. " while(temp_start < temp_end)\n" <<
  96. " {\n" <<
  97. " temp_mid = (temp_start + temp_end + 1)/2;\n" <<
  98. " if(" << comp(expr<value_type>("x"), first2[expr<uint_>("temp_mid")]) << ")\n" <<
  99. " temp_end = temp_mid-1;\n" <<
  100. " else temp_start = temp_mid;\n" <<
  101. " }\n" <<
  102. " b_run = temp_start - b_start + 1;\n" <<
  103. " b_advance = min(b_advance, b_run);\n" <<
  104. " uint a_advance = x_count - b_advance;\n" <<
  105. " uint star = convert_uint((a_advance == b_advance + 1) " <<
  106. "&& (b_advance < b_run));\n" <<
  107. " a_index = a_start + a_advance;\n" <<
  108. " b_index = target - a_index + star;\n" <<
  109. "}\n" <<
  110. result_a[expr<uint_>("i")] << " = a_index;\n" <<
  111. result_b[expr<uint_>("i")] << " = b_index;\n";
  112. }
  113. template<class InputIterator1, class InputIterator2,
  114. class OutputIterator1, class OutputIterator2>
  115. void set_range(InputIterator1 first1,
  116. InputIterator1 last1,
  117. InputIterator2 first2,
  118. InputIterator2 last2,
  119. OutputIterator1 result_a,
  120. OutputIterator2 result_b)
  121. {
  122. typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
  123. ::boost::compute::less<value_type> less_than;
  124. set_range(first1, last1, first2, last2, result_a, result_b, less_than);
  125. }
  126. event exec(command_queue &queue)
  127. {
  128. if((m_a_count + m_b_count)/tile_size == 0) {
  129. return event();
  130. }
  131. set_arg(m_a_count_arg, uint_(m_a_count));
  132. set_arg(m_b_count_arg, uint_(m_b_count));
  133. return exec_1d(queue, 0, (m_a_count + m_b_count)/tile_size);
  134. }
  135. private:
  136. size_t m_a_count;
  137. size_t m_a_count_arg;
  138. size_t m_b_count;
  139. size_t m_b_count_arg;
  140. };
  141. } //end detail namespace
  142. } //end compute namespace
  143. } //end boost namespace
  144. #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_BALANCED_PATH_HPP