serial_merge.hpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@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_SERIAL_MERGE_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_SERIAL_MERGE_HPP
  12. #include <iterator>
  13. #include <boost/compute/command_queue.hpp>
  14. #include <boost/compute/detail/meta_kernel.hpp>
  15. #include <boost/compute/detail/iterator_range_size.hpp>
  16. namespace boost {
  17. namespace compute {
  18. namespace detail {
  19. template<class InputIterator1,
  20. class InputIterator2,
  21. class OutputIterator,
  22. class Compare>
  23. inline OutputIterator serial_merge(InputIterator1 first1,
  24. InputIterator1 last1,
  25. InputIterator2 first2,
  26. InputIterator2 last2,
  27. OutputIterator result,
  28. Compare comp,
  29. command_queue &queue)
  30. {
  31. typedef typename
  32. std::iterator_traits<InputIterator1>::value_type
  33. input_type1;
  34. typedef typename
  35. std::iterator_traits<InputIterator2>::value_type
  36. input_type2;
  37. typedef typename
  38. std::iterator_traits<OutputIterator>::difference_type
  39. result_difference_type;
  40. std::ptrdiff_t size1 = std::distance(first1, last1);
  41. std::ptrdiff_t size2 = std::distance(first2, last2);
  42. meta_kernel k("serial_merge");
  43. k.add_set_arg<uint_>("size1", static_cast<uint_>(size1));
  44. k.add_set_arg<uint_>("size2", static_cast<uint_>(size2));
  45. k <<
  46. "uint i = 0;\n" << // index in result range
  47. "uint j = 0;\n" << // index in first input range
  48. "uint k = 0;\n" << // index in second input range
  49. // fetch initial values from each range
  50. k.decl<input_type1>("j_value") << " = " << first1[0] << ";\n" <<
  51. k.decl<input_type2>("k_value") << " = " << first2[0] << ";\n" <<
  52. // merge values from both input ranges to the result range
  53. "while(j < size1 && k < size2){\n" <<
  54. " if(" << comp(k.var<input_type1>("j_value"),
  55. k.var<input_type2>("k_value")) << "){\n" <<
  56. " " << result[k.var<uint_>("i++")] << " = j_value;\n" <<
  57. " j_value = " << first1[k.var<uint_>("++j")] << ";\n" <<
  58. " }\n" <<
  59. " else{\n"
  60. " " << result[k.var<uint_>("i++")] << " = k_value;\n"
  61. " k_value = " << first2[k.var<uint_>("++k")] << ";\n" <<
  62. " }\n"
  63. "}\n"
  64. // copy any remaining values from first range
  65. "while(j < size1){\n" <<
  66. result[k.var<uint_>("i++")] << " = " <<
  67. first1[k.var<uint_>("j++")] << ";\n" <<
  68. "}\n"
  69. // copy any remaining values from second range
  70. "while(k < size2){\n" <<
  71. result[k.var<uint_>("i++")] << " = " <<
  72. first2[k.var<uint_>("k++")] << ";\n" <<
  73. "}\n";
  74. // run kernel
  75. k.exec(queue);
  76. return result + static_cast<result_difference_type>(size1 + size2);
  77. }
  78. } // end detail namespace
  79. } // end compute namespace
  80. } // end boost namespace
  81. #endif // BOOST_COMPUTE_ALGORITHM_SERIAL_MERGE_HPP