1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_STABLE_SORT_HPP
12 #define BOOST_COMPUTE_ALGORITHM_STABLE_SORT_HPP
16 #include <boost/static_assert.hpp>
18 #include <boost/compute/system.hpp>
19 #include <boost/compute/command_queue.hpp>
20 #include <boost/compute/algorithm/detail/merge_sort_on_cpu.hpp>
21 #include <boost/compute/algorithm/detail/merge_sort_on_gpu.hpp>
22 #include <boost/compute/algorithm/detail/radix_sort.hpp>
23 #include <boost/compute/algorithm/detail/insertion_sort.hpp>
24 #include <boost/compute/algorithm/reverse.hpp>
25 #include <boost/compute/functional/operator.hpp>
26 #include <boost/compute/detail/iterator_range_size.hpp>
27 #include <boost/compute/type_traits/is_device_iterator.hpp>
33 template<class Iterator, class Compare>
34 inline void dispatch_gpu_stable_sort(Iterator first,
39 size_t count = detail::iterator_range_size(first, last);
42 detail::serial_insertion_sort(
43 first, last, compare, queue
46 detail::merge_sort_on_gpu(
47 first, last, compare, true /* stable */, queue
53 inline typename boost::enable_if_c<is_radix_sortable<T>::value>::type
54 dispatch_gpu_stable_sort(buffer_iterator<T> first,
55 buffer_iterator<T> last,
59 ::boost::compute::detail::radix_sort(first, last, queue);
63 inline typename boost::enable_if_c<is_radix_sortable<T>::value>::type
64 dispatch_gpu_stable_sort(buffer_iterator<T> first,
65 buffer_iterator<T> last,
69 // radix sorts in descending order
70 ::boost::compute::detail::radix_sort(first, last, false, queue);
73 } // end detail namespace
75 /// Sorts the values in the range [\p first, \p last) according to
76 /// \p compare. The relative order of identical values is preserved.
78 /// Space complexity: \Omega(n)
80 /// \see sort(), is_sorted()
81 template<class Iterator, class Compare>
82 inline void stable_sort(Iterator first,
85 command_queue &queue = system::default_queue())
87 BOOST_STATIC_ASSERT(is_device_iterator<Iterator>::value);
89 if(queue.get_device().type() & device::gpu) {
90 ::boost::compute::detail::dispatch_gpu_stable_sort(
91 first, last, compare, queue
95 ::boost::compute::detail::merge_sort_on_cpu(first, last, compare, queue);
99 template<class Iterator>
100 inline void stable_sort(Iterator first,
102 command_queue &queue = system::default_queue())
104 BOOST_STATIC_ASSERT(is_device_iterator<Iterator>::value);
105 typedef typename std::iterator_traits<Iterator>::value_type value_type;
107 ::boost::compute::less<value_type> less;
109 ::boost::compute::stable_sort(first, last, less, queue);
112 } // end compute namespace
113 } // end boost namespace
115 #endif // BOOST_COMPUTE_ALGORITHM_STABLE_SORT_HPP