stable_sort.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_STABLE_SORT_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_STABLE_SORT_HPP
  12. #include <iterator>
  13. #include <boost/static_assert.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/command_queue.hpp>
  16. #include <boost/compute/algorithm/detail/merge_sort_on_cpu.hpp>
  17. #include <boost/compute/algorithm/detail/merge_sort_on_gpu.hpp>
  18. #include <boost/compute/algorithm/detail/radix_sort.hpp>
  19. #include <boost/compute/algorithm/detail/insertion_sort.hpp>
  20. #include <boost/compute/algorithm/reverse.hpp>
  21. #include <boost/compute/functional/operator.hpp>
  22. #include <boost/compute/detail/iterator_range_size.hpp>
  23. #include <boost/compute/type_traits/is_device_iterator.hpp>
  24. namespace boost {
  25. namespace compute {
  26. namespace detail {
  27. template<class Iterator, class Compare>
  28. inline void dispatch_gpu_stable_sort(Iterator first,
  29. Iterator last,
  30. Compare compare,
  31. command_queue &queue)
  32. {
  33. size_t count = detail::iterator_range_size(first, last);
  34. if(count < 32){
  35. detail::serial_insertion_sort(
  36. first, last, compare, queue
  37. );
  38. } else {
  39. detail::merge_sort_on_gpu(
  40. first, last, compare, true /* stable */, queue
  41. );
  42. }
  43. }
  44. template<class T>
  45. inline typename boost::enable_if_c<is_radix_sortable<T>::value>::type
  46. dispatch_gpu_stable_sort(buffer_iterator<T> first,
  47. buffer_iterator<T> last,
  48. less<T>,
  49. command_queue &queue)
  50. {
  51. ::boost::compute::detail::radix_sort(first, last, queue);
  52. }
  53. template<class T>
  54. inline typename boost::enable_if_c<is_radix_sortable<T>::value>::type
  55. dispatch_gpu_stable_sort(buffer_iterator<T> first,
  56. buffer_iterator<T> last,
  57. greater<T>,
  58. command_queue &queue)
  59. {
  60. // radix sorts in descending order
  61. ::boost::compute::detail::radix_sort(first, last, false, queue);
  62. }
  63. } // end detail namespace
  64. /// Sorts the values in the range [\p first, \p last) according to
  65. /// \p compare. The relative order of identical values is preserved.
  66. ///
  67. /// Space complexity: \Omega(n)
  68. ///
  69. /// \see sort(), is_sorted()
  70. template<class Iterator, class Compare>
  71. inline void stable_sort(Iterator first,
  72. Iterator last,
  73. Compare compare,
  74. command_queue &queue = system::default_queue())
  75. {
  76. BOOST_STATIC_ASSERT(is_device_iterator<Iterator>::value);
  77. if(queue.get_device().type() & device::gpu) {
  78. ::boost::compute::detail::dispatch_gpu_stable_sort(
  79. first, last, compare, queue
  80. );
  81. return;
  82. }
  83. ::boost::compute::detail::merge_sort_on_cpu(first, last, compare, queue);
  84. }
  85. /// \overload
  86. template<class Iterator>
  87. inline void stable_sort(Iterator first,
  88. Iterator last,
  89. command_queue &queue = system::default_queue())
  90. {
  91. BOOST_STATIC_ASSERT(is_device_iterator<Iterator>::value);
  92. typedef typename std::iterator_traits<Iterator>::value_type value_type;
  93. ::boost::compute::less<value_type> less;
  94. ::boost::compute::stable_sort(first, last, less, queue);
  95. }
  96. } // end compute namespace
  97. } // end boost namespace
  98. #endif // BOOST_COMPUTE_ALGORITHM_STABLE_SORT_HPP