merge.hpp 4.3 KB

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