merge.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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_MERGE_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_MERGE_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/command_queue.hpp>
  15. #include <boost/compute/algorithm/copy.hpp>
  16. #include <boost/compute/algorithm/detail/merge_with_merge_path.hpp>
  17. #include <boost/compute/algorithm/detail/serial_merge.hpp>
  18. #include <boost/compute/detail/iterator_range_size.hpp>
  19. #include <boost/compute/detail/parameter_cache.hpp>
  20. #include <boost/compute/type_traits/is_device_iterator.hpp>
  21. namespace boost {
  22. namespace compute {
  23. /// Merges the sorted values in the range [\p first1, \p last1) with the sorted
  24. /// values in the range [\p first2, last2) and stores the result in the range
  25. /// beginning at \p result. Values are compared using the \p comp function. If
  26. /// no comparision function is given, \c less is used.
  27. ///
  28. /// \param first1 first element in the first range to merge
  29. /// \param last1 last element in the first range to merge
  30. /// \param first2 first element in the second range to merge
  31. /// \param last2 last element in the second range to merge
  32. /// \param result first element in the result range
  33. /// \param comp comparison function (by default \c less)
  34. /// \param queue command queue to perform the operation
  35. ///
  36. /// \return \c OutputIterator to the end of the result range
  37. ///
  38. /// Space complexity: \Omega(distance(\p first1, \p last1) + distance(\p first2, \p last2))
  39. ///
  40. /// \see inplace_merge()
  41. template<class InputIterator1,
  42. class InputIterator2,
  43. class OutputIterator,
  44. class Compare>
  45. inline OutputIterator merge(InputIterator1 first1,
  46. InputIterator1 last1,
  47. InputIterator2 first2,
  48. InputIterator2 last2,
  49. OutputIterator result,
  50. Compare comp,
  51. command_queue &queue = system::default_queue())
  52. {
  53. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
  54. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
  55. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  56. typedef typename std::iterator_traits<InputIterator1>::value_type input1_type;
  57. typedef typename std::iterator_traits<InputIterator2>::value_type input2_type;
  58. typedef typename std::iterator_traits<OutputIterator>::value_type output_type;
  59. const device &device = queue.get_device();
  60. std::string cache_key =
  61. std::string("__boost_merge_") + type_name<input1_type>() + "_"
  62. + type_name<input2_type>() + "_" + type_name<output_type>();
  63. boost::shared_ptr<detail::parameter_cache> parameters =
  64. detail::parameter_cache::get_global_cache(device);
  65. // default serial merge threshold depends on device type
  66. size_t default_serial_merge_threshold = 32768;
  67. if(device.type() & device::gpu) {
  68. default_serial_merge_threshold = 2048;
  69. }
  70. // loading serial merge threshold parameter
  71. const size_t serial_merge_threshold =
  72. parameters->get(cache_key, "serial_merge_threshold",
  73. static_cast<uint_>(default_serial_merge_threshold));
  74. // choosing merge algorithm
  75. const size_t total_count =
  76. detail::iterator_range_size(first1, last1)
  77. + detail::iterator_range_size(first2, last2);
  78. // for small inputs serial merge turns out to outperform
  79. // merge with merge path algorithm
  80. if(total_count <= serial_merge_threshold){
  81. return detail::serial_merge(first1, last1, first2, last2, result, comp, queue);
  82. }
  83. return detail::merge_with_merge_path(first1, last1, first2, last2, result, comp, queue);
  84. }
  85. /// \overload
  86. template<class InputIterator1, class InputIterator2, class OutputIterator>
  87. inline OutputIterator merge(InputIterator1 first1,
  88. InputIterator1 last1,
  89. InputIterator2 first2,
  90. InputIterator2 last2,
  91. OutputIterator result,
  92. command_queue &queue = system::default_queue())
  93. {
  94. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
  95. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
  96. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  97. typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
  98. less<value_type> less_than;
  99. return merge(first1, last1, first2, last2, result, less_than, queue);
  100. }
  101. } // end compute namespace
  102. } // end boost namespace
  103. #endif // BOOST_COMPUTE_ALGORITHM_MERGE_HPP