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_SORT_HPP
12 #define BOOST_COMPUTE_ALGORITHM_SORT_HPP
16 #include <boost/utility/enable_if.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/container/mapped_view.hpp>
26 #include <boost/compute/detail/iterator_range_size.hpp>
27 #include <boost/compute/iterator/buffer_iterator.hpp>
28 #include <boost/compute/type_traits/is_device_iterator.hpp>
35 inline void dispatch_gpu_sort(buffer_iterator<T> first,
36 buffer_iterator<T> last,
39 typename boost::enable_if_c<
40 is_radix_sortable<T>::value
43 size_t count = detail::iterator_range_size(first, last);
50 ::boost::compute::detail::serial_insertion_sort(first, last, queue);
53 ::boost::compute::detail::radix_sort(first, last, queue);
58 inline void dispatch_gpu_sort(buffer_iterator<T> first,
59 buffer_iterator<T> last,
62 typename boost::enable_if_c<
63 is_radix_sortable<T>::value
66 size_t count = detail::iterator_range_size(first, last);
73 ::boost::compute::detail::serial_insertion_sort(
74 first, last, compare, queue
78 // radix sorts in descending order
79 ::boost::compute::detail::radix_sort(first, last, false, queue);
83 template<class Iterator, class Compare>
84 inline void dispatch_gpu_sort(Iterator first,
89 size_t count = detail::iterator_range_size(first, last);
96 ::boost::compute::detail::serial_insertion_sort(
97 first, last, compare, queue
101 ::boost::compute::detail::merge_sort_on_gpu(
102 first, last, compare, queue
107 // sort() for device iterators
108 template<class Iterator, class Compare>
109 inline void dispatch_sort(Iterator first,
112 command_queue &queue,
113 typename boost::enable_if<
114 is_device_iterator<Iterator>
117 if(queue.get_device().type() & device::gpu) {
118 dispatch_gpu_sort(first, last, compare, queue);
121 ::boost::compute::detail::merge_sort_on_cpu(first, last, compare, queue);
124 // sort() for host iterators
125 template<class Iterator, class Compare>
126 inline void dispatch_sort(Iterator first,
129 command_queue &queue,
130 typename boost::disable_if<
131 is_device_iterator<Iterator>
134 typedef typename std::iterator_traits<Iterator>::value_type T;
136 size_t size = static_cast<size_t>(std::distance(first, last));
138 // create mapped buffer
140 boost::addressof(*first), size, queue.get_context()
143 // sort mapped buffer
144 dispatch_sort(view.begin(), view.end(), compare, queue);
146 // return results to host
150 } // end detail namespace
152 /// Sorts the values in the range [\p first, \p last) according to
155 /// \param first first element in the range to sort
156 /// \param last last element in the range to sort
157 /// \param compare comparison function (by default \c less)
158 /// \param queue command queue to perform the operation
160 /// For example, to sort a vector on the device:
162 /// // create vector on the device with data
163 /// float data[] = { 2.f, 4.f, 1.f, 3.f };
164 /// boost::compute::vector<float> vec(data, data + 4, queue);
166 /// // sort the vector on the device
167 /// boost::compute::sort(vec.begin(), vec.end(), queue);
170 /// The sort() algorithm can also be directly used with host iterators. This
171 /// example will automatically transfer the data to the device, sort it, and
172 /// then transfer the data back to the host:
174 /// std::vector<int> data = { 9, 3, 2, 5, 1, 4, 6, 7 };
176 /// boost::compute::sort(data.begin(), data.end(), queue);
179 /// Space complexity: \Omega(n)
182 template<class Iterator, class Compare>
183 inline void sort(Iterator first,
186 command_queue &queue = system::default_queue())
188 ::boost::compute::detail::dispatch_sort(first, last, compare, queue);
192 template<class Iterator>
193 inline void sort(Iterator first,
195 command_queue &queue = system::default_queue())
197 typedef typename std::iterator_traits<Iterator>::value_type value_type;
199 ::boost::compute::sort(
200 first, last, ::boost::compute::less<value_type>(), queue
204 } // end compute namespace
205 } // end boost namespace
207 #endif // BOOST_COMPUTE_ALGORITHM_SORT_HPP